diff --git a/cmake/project.cmake b/cmake/project.cmake index de43eec..9054365 100644 --- a/cmake/project.cmake +++ b/cmake/project.cmake @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -set(SAMCONF_VERSION 0.60.0) +set(SAMCONF_VERSION 0.60.1) # Attention: Aside from the version, as many things as possible in this file # should be put into functions, as this solves potential issues with commands diff --git a/src/samconf/CMakeLists.txt b/src/samconf/CMakeLists.txt index 68104ea..ee16d05 100644 --- a/src/samconf/CMakeLists.txt +++ b/src/samconf/CMakeLists.txt @@ -14,6 +14,7 @@ add_library( private/samconf.c private/config_backend.c private/json_backend.c + private/dummy_backend.c private/env_backend.c private/crypto_utils.c private/signature.c diff --git a/src/samconf/interface/samconf/samconf_types.h b/src/samconf/interface/samconf/samconf_types.h index a970b76..34b0142 100644 --- a/src/samconf/interface/samconf/samconf_types.h +++ b/src/samconf/interface/samconf/samconf_types.h @@ -9,6 +9,13 @@ struct samconfConfigBackend; struct samconfConfigBackendOps; +typedef enum samconfBackendOptionsE { + BACKEND_JSON = 0, + BACKEND_ENV, + BACKEND_DUMMY, + BACKEND_COUNT +} samconfBackendOptions_t; + typedef enum samconfConfigStatusE { SAMCONF_CONFIG_ERROR = -1, SAMCONF_CONFIG_OK = 0, diff --git a/src/samconf/private/dummy_backend.c b/src/samconf/private/dummy_backend.c new file mode 100644 index 0000000..29b6a42 --- /dev/null +++ b/src/samconf/private/dummy_backend.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MIT +#include "samconf/dummy_backend.h" + +#include "samconf/config_backend.h" +#include "samconf/samconf.h" + +samconfConfigStatusE_t samconfSupportsDummy(UNUSED const char *location, UNUSED bool *isSupported) { + return SAMCONF_CONFIG_NOT_IMPLEMENTED; +} + +samconfConfigStatusE_t samconfOpenDummy(UNUSED const char *location, UNUSED samconfConfigBackend_t *backend) { + return SAMCONF_CONFIG_NOT_IMPLEMENTED; +} + +samconfConfigStatusE_t samconfLoadDummy(UNUSED samconfConfigBackend_t *backend, UNUSED bool isSigned, + UNUSED samconfConfig_t **config) { + return SAMCONF_CONFIG_NOT_IMPLEMENTED; +} + +samconfConfigStatusE_t samconfCloseDummy(UNUSED samconfConfigBackend_t *backend) { + return SAMCONF_CONFIG_NOT_IMPLEMENTED; +} + +const samconfConfigBackendOps_t samconfDummyOps = { + .supports = samconfSupportsDummy, + .open = samconfOpenDummy, + .load = samconfLoadDummy, + .close = samconfCloseDummy, +}; diff --git a/src/samconf/private/samconf.c b/src/samconf/private/samconf.c index 11fc0da..4b16401 100644 --- a/src/samconf/private/samconf.c +++ b/src/samconf/private/samconf.c @@ -9,61 +9,40 @@ #include "samconf/config_backend.h" #include "samconf/crypto_utils.h" +#include "samconf/dummy_backend.h" #include "samconf/env_backend.h" #include "samconf/json_backend.h" #include "samconf/signature.h" -static samconfConfigStatusE_t samconfSupportsDummy(UNUSED const char *location, UNUSED bool *isSupported) { - return SAMCONF_CONFIG_NOT_IMPLEMENTED; -} - -static samconfConfigStatusE_t samconfOpenDummy(UNUSED const char *location, UNUSED samconfConfigBackend_t *backend) { - return SAMCONF_CONFIG_NOT_IMPLEMENTED; -} - -static samconfConfigStatusE_t samconfLoadDummy(UNUSED samconfConfigBackend_t *backend, UNUSED bool isSigned, - UNUSED samconfConfig_t **config) { - return SAMCONF_CONFIG_NOT_IMPLEMENTED; -} - -static samconfConfigStatusE_t samconfCloseDummy(UNUSED samconfConfigBackend_t *backend) { - return SAMCONF_CONFIG_NOT_IMPLEMENTED; -} - -static const samconfConfigBackendOps_t samconfDummyOps = { - .supports = samconfSupportsDummy, - .open = samconfOpenDummy, - .load = samconfLoadDummy, - .close = samconfCloseDummy, -}; - -static const samconfConfigBackendOps_t *const samconfBackendOps[] = { +static const samconfConfigBackendOps_t *const samconfBackendOps[BACKEND_COUNT] = { #ifdef SAMCONF_ENABLE_CONFIG_BACKEND_JSON - &samconfJsonOps, + [BACKEND_JSON] = &samconfJsonOps, #endif #ifdef SAMCONF_ENABLE_CONFIG_BACKEND_ENV - &samconfEnvOps, + [BACKEND_ENV] = &samconfEnvOps, #endif - &samconfDummyOps, + [BACKEND_DUMMY] = &samconfDummyOps, }; -NOINLINE const samconfConfigBackendOps_t *samconfGetBackendOps(size_t idx) { - if (idx < ARRAY_SIZE(samconfBackendOps)) { - return samconfBackendOps[idx]; +NOINLINE const samconfConfigBackendOps_t *samconfGetBackendOps(samconfBackendOptions_t idx) { + const samconfConfigBackendOps_t *backend = &samconfDummyOps; + if (idx < BACKEND_COUNT && samconfBackendOps[idx] != NULL) { + backend = samconfBackendOps[idx]; } - return NULL; + return backend; } NOINLINE samconfConfigStatusE_t samconfLookupBackend(const char *location, samconfConfigBackend_t **backend) { samconfConfigStatusE_t status = SAMCONF_CONFIG_ERROR; - bool isSupported; + bool isSupported = false; + const samconfConfigBackendOps_t *backendOps = NULL; - const samconfConfigBackendOps_t *backendOps = samconfGetBackendOps(0); - for (size_t i = 0; backendOps != NULL; backendOps = samconfGetBackendOps(++i)) { + for (samconfBackendOptions_t idx = BACKEND_JSON; idx < BACKEND_COUNT; ++idx) { + backendOps = samconfGetBackendOps(idx); if (backendOps->supports(location, &isSupported) == SAMCONF_CONFIG_OK && isSupported) { - status = samconfConfigBackendNew(backend, samconfGetBackendOps(i)); + status = samconfConfigBackendNew(backend, backendOps); break; } } diff --git a/src/samconf/public/samconf/dummy_backend.h b/src/samconf/public/samconf/dummy_backend.h new file mode 100644 index 0000000..39f1c55 --- /dev/null +++ b/src/samconf/public/samconf/dummy_backend.h @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: MIT +#pragma once + +#include "samconf/config_backend.h" +#include "samconf/samconf.h" + +extern const samconfConfigBackendOps_t samconfDummyOps; + +__BEGIN_DECLS + +samconfConfigStatusE_t samconfSupportsDummy(UNUSED const char *location, UNUSED bool *isSupported); +samconfConfigStatusE_t samconfOpenDummy(UNUSED const char *location, UNUSED samconfConfigBackend_t *backend); +samconfConfigStatusE_t samconfLoadDummy(UNUSED samconfConfigBackend_t *backend, UNUSED bool isSigned, + UNUSED samconfConfig_t **config); +samconfConfigStatusE_t samconfCloseDummy(UNUSED samconfConfigBackend_t *backend); + +__END_DECLS diff --git a/src/samconf/public/samconf/samconf.h b/src/samconf/public/samconf/samconf.h index 56ac801..9881593 100644 --- a/src/samconf/public/samconf/samconf.h +++ b/src/samconf/public/samconf/samconf.h @@ -5,7 +5,7 @@ __BEGIN_DECLS -const struct samconfConfigBackendOps *samconfGetBackendOps(size_t idx); +const struct samconfConfigBackendOps *samconfGetBackendOps(samconfBackendOptions_t idx); samconfConfigStatusE_t samconfLookupBackend(const char *location, struct samconfConfigBackend **backend); samconfConfigStatusE_t samconfVerifySignature(const char *location); samconfConfigStatusE_t samconfLoad(const char *location, bool enforceSignature, samconfConfig_t **const config); diff --git a/test/utest/mocks/samconf/mock_samconf.c b/test/utest/mocks/samconf/mock_samconf.c index 55a06a7..3bef25e 100644 --- a/test/utest/mocks/samconf/mock_samconf.c +++ b/test/utest/mocks/samconf/mock_samconf.c @@ -4,7 +4,7 @@ #include -MOCK_FUNC_BODY(samconfGetBackendOps, const samconfConfigBackendOps_t *, size_t idx) { +MOCK_FUNC_BODY(samconfGetBackendOps, const samconfConfigBackendOps_t *, samconfBackendOptions_t idx) { if (MOCK_IS_ACTIVE(samconfGetBackendOps)) { check_expected(idx); return mock_type(const samconfConfigBackendOps_t *); diff --git a/test/utest/mocks/samconf/mock_samconf.h b/test/utest/mocks/samconf/mock_samconf.h index a67b90d..ff877b5 100644 --- a/test/utest/mocks/samconf/mock_samconf.h +++ b/test/utest/mocks/samconf/mock_samconf.h @@ -18,7 +18,7 @@ #include "samconf/samconf.h" #include "samconf/uri.h" -MOCK_FUNC_PROTOTYPE(samconfGetBackendOps, const samconfConfigBackendOps_t *, size_t idx) +MOCK_FUNC_PROTOTYPE(samconfGetBackendOps, const samconfConfigBackendOps_t *, samconfBackendOptions_t idx) MOCK_FUNC_PROTOTYPE(samconfLookupBackend, samconfConfigStatusE_t, const char *location, samconfConfigBackend_t **backend) MOCK_FUNC_PROTOTYPE(samconfVerifySignature, samconfConfigStatusE_t, const char *location) diff --git a/test/utest/samconf/samconf/samconfGetBackendOps/CMakeLists.txt b/test/utest/samconf/samconf/samconfGetBackendOps/CMakeLists.txt index ba618ff..774609d 100644 --- a/test/utest/samconf/samconf/samconfGetBackendOps/CMakeLists.txt +++ b/test/utest/samconf/samconf/samconfGetBackendOps/CMakeLists.txt @@ -7,7 +7,6 @@ create_unit_test( SOURCES case_success_get_dummy_backend.c case_success_get_json_backend.c - case_error.c samconfGetBackendOps_utest.c LIBRARIES cmocka_extensions::cmocka_extensions diff --git a/test/utest/samconf/samconf/samconfGetBackendOps/case_error.c b/test/utest/samconf/samconf/samconfGetBackendOps/case_error.c deleted file mode 100644 index 707740a..0000000 --- a/test/utest/samconf/samconf/samconfGetBackendOps/case_error.c +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-License-Identifier: MIT -#include -#include - -#include "samconf/config_backend.h" -#include "samconf/samconf.h" -#include "samconfGetBackendOps_utest.h" - -void samconfTestSamconfGetBackendOpsError(UNUSED void **state) { - const samconfConfigBackendOps_t *ops; - - TEST("samconfGetBackendOps with invalid input"); - SHOULD("%s", "return Null"); - - ops = samconfGetBackendOps(3); - - assert_null(ops); -} diff --git a/test/utest/samconf/samconf/samconfGetBackendOps/case_success_get_dummy_backend.c b/test/utest/samconf/samconf/samconfGetBackendOps/case_success_get_dummy_backend.c index 74671e6..73a3c29 100644 --- a/test/utest/samconf/samconf/samconfGetBackendOps/case_success_get_dummy_backend.c +++ b/test/utest/samconf/samconf/samconfGetBackendOps/case_success_get_dummy_backend.c @@ -12,7 +12,7 @@ void samconfTestSamconfGetBackendOpsSuccessGetDummyBackend(UNUSED void **state) TEST("samconfGetBackendOps with index of dummy Ops"); SHOULD("%s", "return dummy Ops"); - opsDummy = samconfGetBackendOps(2); + opsDummy = samconfGetBackendOps(BACKEND_DUMMY); assert_int_equal(opsDummy->supports("", NULL), SAMCONF_CONFIG_NOT_IMPLEMENTED); assert_int_equal(opsDummy->open(NULL, NULL), SAMCONF_CONFIG_NOT_IMPLEMENTED); diff --git a/test/utest/samconf/samconf/samconfGetBackendOps/case_success_get_json_backend.c b/test/utest/samconf/samconf/samconfGetBackendOps/case_success_get_json_backend.c index f18c011..6e64603 100644 --- a/test/utest/samconf/samconf/samconfGetBackendOps/case_success_get_json_backend.c +++ b/test/utest/samconf/samconf/samconfGetBackendOps/case_success_get_json_backend.c @@ -3,6 +3,7 @@ #include #include "mock_samconf.h" +#include "samconf/config_backend.h" #include "samconfGetBackendOps_utest.h" void samconfTestSamconfGetBackendOpsSuccessGetJsonBackend(UNUSED void **state) { @@ -37,7 +38,7 @@ void samconfTestSamconfGetBackendOpsSuccessGetJsonBackend(UNUSED void **state) { will_return(samconfJsonBackendLoad, SAMCONF_CONFIG_OK); will_return(samconfJsonBackendClose, SAMCONF_CONFIG_OK); - opsJson = samconfGetBackendOps(0); + opsJson = samconfGetBackendOps(BACKEND_JSON); assert_int_equal(opsJson->supports(location, &testBool), SAMCONF_CONFIG_OK); assert_int_equal(opsJson->open(location, &testBackend), SAMCONF_CONFIG_OK); diff --git a/test/utest/samconf/samconf/samconfGetBackendOps/samconfGetBackendOps_utest.c b/test/utest/samconf/samconf/samconfGetBackendOps/samconfGetBackendOps_utest.c index 2eeb2ad..5be501d 100644 --- a/test/utest/samconf/samconf/samconfGetBackendOps/samconfGetBackendOps_utest.c +++ b/test/utest/samconf/samconf/samconfGetBackendOps/samconfGetBackendOps_utest.c @@ -7,7 +7,6 @@ int main() { const struct CMUnitTest tests[] = { cmocka_unit_test(samconfTestSamconfGetBackendOpsSuccessGetDummyBackend), cmocka_unit_test(samconfTestSamconfGetBackendOpsSuccessGetJsonBackend), - cmocka_unit_test(samconfTestSamconfGetBackendOpsError), }; return cmocka_run_group_tests(tests, NULL, NULL); diff --git a/test/utest/samconf/samconf/samconfLookupBackend/CMakeLists.txt b/test/utest/samconf/samconf/samconfLookupBackend/CMakeLists.txt index 2bddc32..531d615 100644 --- a/test/utest/samconf/samconf/samconfLookupBackend/CMakeLists.txt +++ b/test/utest/samconf/samconf/samconfLookupBackend/CMakeLists.txt @@ -6,7 +6,6 @@ create_unit_test( test_event_samconfLookupBackend_utest SOURCES case_success.c - case_error_no_loop.c case_error_support_fail.c case_error_backend_alloc.c samconfLookupBackend_utest.c diff --git a/test/utest/samconf/samconf/samconfLookupBackend/case_error_no_loop.c b/test/utest/samconf/samconf/samconfLookupBackend/case_error_no_loop.c deleted file mode 100644 index b24bdd0..0000000 --- a/test/utest/samconf/samconf/samconfLookupBackend/case_error_no_loop.c +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-License-Identifier: MIT -#include -#include - -#include "safu/mock_safu.h" -#include "samconfLookupBackend_utest.h" - -void samconfTestSamconfLookupBackendErrorNoLoop(UNUSED void **state) { - samconfConfigStatusE_t status; - samconfConfigBackend_t *backendPtr; - - const char *location = "config.json"; - - TEST("samconfTestSamconfLookupBackend"); - SHOULD("%s", "fail when no backends are loaded"); - - MOCK_FUNC_ALWAYS(samconfGetBackendOps); - expect_any(samconfGetBackendOps, idx); - will_return(samconfGetBackendOps, NULL); - - status = samconfLookupBackend(location, &backendPtr); - - MOCK_FUNC_DISABLE(samconfGetBackendOps); - - assert_int_equal(status, SAMCONF_CONFIG_ERROR); -} diff --git a/test/utest/samconf/samconf/samconfLookupBackend/case_error_support_fail.c b/test/utest/samconf/samconf/samconfLookupBackend/case_error_support_fail.c index 8dc9898..1956b22 100644 --- a/test/utest/samconf/samconf/samconfLookupBackend/case_error_support_fail.c +++ b/test/utest/samconf/samconf/samconfLookupBackend/case_error_support_fail.c @@ -36,15 +36,14 @@ void samconfTestSamconfLookupBackendErrorSupportFail(UNUSED void **state) { // Test fail on error return // HINT: Some optimization seems to reduce the calls to 1 - expect_value(samconfGetBackendOps, idx, 0); - expect_value(samconfGetBackendOps, idx, 1); - expect_value(samconfGetBackendOps, idx, 2); + expect_value(samconfGetBackendOps, idx, BACKEND_JSON); + expect_value(samconfGetBackendOps, idx, BACKEND_ENV); + expect_value(samconfGetBackendOps, idx, BACKEND_DUMMY); - will_return_count(samconfGetBackendOps, &samconfDummyOps, 2); - will_return(samconfGetBackendOps, NULL); + will_return_count(samconfGetBackendOps, &samconfDummyOps, BACKEND_COUNT); status = samconfLookupBackend(location, &backendPtr); - assert_int_equal(samconfSupportsDummyCounter, 2); + assert_int_equal(samconfSupportsDummyCounter, BACKEND_COUNT); assert_int_equal(status, SAMCONF_CONFIG_ERROR); // Reset callback counter @@ -53,15 +52,14 @@ void samconfTestSamconfLookupBackendErrorSupportFail(UNUSED void **state) { // Test fail on supports false samconfDummyOps.supports = samconfSupportsDummyFalse; - expect_value(samconfGetBackendOps, idx, 0); - expect_value(samconfGetBackendOps, idx, 1); - expect_value(samconfGetBackendOps, idx, 2); + expect_value(samconfGetBackendOps, idx, BACKEND_JSON); + expect_value(samconfGetBackendOps, idx, BACKEND_ENV); + expect_value(samconfGetBackendOps, idx, BACKEND_DUMMY); - will_return_count(samconfGetBackendOps, &samconfDummyOps, 2); - will_return(samconfGetBackendOps, NULL); + will_return_count(samconfGetBackendOps, &samconfDummyOps, BACKEND_COUNT); status = samconfLookupBackend(location, &backendPtr); - assert_int_equal(samconfSupportsDummyCounter, 2); + assert_int_equal(samconfSupportsDummyCounter, BACKEND_COUNT); assert_int_equal(status, SAMCONF_CONFIG_ERROR); MOCK_FUNC_DISABLE(samconfGetBackendOps); diff --git a/test/utest/samconf/samconf/samconfLookupBackend/samconfLookupBackend_utest.c b/test/utest/samconf/samconf/samconfLookupBackend/samconfLookupBackend_utest.c index b42979e..d43a93f 100644 --- a/test/utest/samconf/samconf/samconfLookupBackend/samconfLookupBackend_utest.c +++ b/test/utest/samconf/samconf/samconfLookupBackend/samconfLookupBackend_utest.c @@ -4,7 +4,6 @@ int main() { const struct CMUnitTest tests[] = { cmocka_unit_test(samconfTestSamconfLookupBackendSuccess), - cmocka_unit_test(samconfTestSamconfLookupBackendErrorNoLoop), cmocka_unit_test(samconfTestSamconfLookupBackendErrorSupportFail), cmocka_unit_test(samconfTestSamconfLookupBackendErrorAlloc), };