Skip to content

Commit

Permalink
Merge pull request #12 from Elektrobit/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
gehwolf authored Dec 10, 2024
2 parents 9e9b259 + 27eb3b2 commit 9010d4f
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 102 deletions.
2 changes: 1 addition & 1 deletion cmake/project.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions src/samconf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/samconf/interface/samconf/samconf_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions src/samconf/private/dummy_backend.c
Original file line number Diff line number Diff line change
@@ -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,
};
51 changes: 15 additions & 36 deletions src/samconf/private/samconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/samconf/public/samconf/dummy_backend.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/samconf/public/samconf/samconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/utest/mocks/samconf/mock_samconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <cmocka.h>

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 *);
Expand Down
2 changes: 1 addition & 1 deletion test/utest/mocks/samconf/mock_samconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 0 additions & 18 deletions test/utest/samconf/samconf/samconfGetBackendOps/case_error.c

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdint.h>

#include "mock_samconf.h"
#include "samconf/config_backend.h"
#include "samconfGetBackendOps_utest.h"

void samconfTestSamconfGetBackendOpsSuccessGetJsonBackend(UNUSED void **state) {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand Down

0 comments on commit 9010d4f

Please sign in to comment.