From bf38a4c841a8b464dc0665594ae26eec5ab33c21 Mon Sep 17 00:00:00 2001 From: Courtcircuits Date: Sun, 7 Jun 2026 17:09:56 +0200 Subject: [PATCH 1/2] feat: add unit tests --- .github/workflows/unit.yml | 28 +++++++++++++++++++++++ Makefile | 21 ++++++++++++++++-- {tests => configs}/alice/charon.conf | 0 {tests => configs}/bob/charon.conf | 0 {tests => configs}/charon.conf | 0 docker-compose.yml | 4 ++-- docker/alice.conf | 7 ------ docker/bob.conf | 8 ------- tests/config_test.c | 33 ++++++++++++++++++++++++++++ tests/configs/working.conf | 8 +++++++ tests/test.h | 32 +++++++++++++++++++++++++++ 11 files changed, 122 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/unit.yml rename {tests => configs}/alice/charon.conf (100%) rename {tests => configs}/bob/charon.conf (100%) rename {tests => configs}/charon.conf (100%) delete mode 100644 docker/alice.conf delete mode 100644 docker/bob.conf create mode 100644 tests/config_test.c create mode 100644 tests/configs/working.conf create mode 100644 tests/test.h diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml new file mode 100644 index 0000000..900e875 --- /dev/null +++ b/.github/workflows/unit.yml @@ -0,0 +1,28 @@ +name: Unit tests + +on: [push, pull_request] + +jobs: + unit: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Cache packages + uses: actions/cache@v4 + with: + path: /var/cache/apt/archives + key: apt-${{ runner.os }}-${{ hashFiles('.github/apt-packages.txt') }} + restore-keys: | + apt-${{ runner.os }}- + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y \ + make \ + protobuf-c-compiler \ + protobuf-compiler \ + libprotobuf-c-dev + + - name: Run unit tests + run: make test TEST=config diff --git a/Makefile b/Makefile index 6471218..4cb1f3f 100644 --- a/Makefile +++ b/Makefile @@ -7,10 +7,20 @@ LDFLAGS = -lprotobuf-c -lpthread SRC = src/*.c \ src/proto/*.c - + + +TEST_SRC = $(filter-out src/main.c, $(wildcard src/*.c)) \ + $(wildcard src/proto/*.c) + +TEST_DIR = tests +BUILD_DIR = build + +test_%: $(BUILD_DIR)/test_% + ./$(BUILD_DIR)/test_$* + BIN = build/charon -.PHONY: all clean dev clean-dev proto +.PHONY: all clean dev clean-dev proto test all: $(BIN) @@ -22,3 +32,10 @@ clean: proto: protoc proto/aap2.proto --c_out=./src + +test: $(BUILD_DIR)/test_$(TEST) + ./$(BUILD_DIR)/test_$(TEST) + +$(BUILD_DIR)/test_%: $(TEST_DIR)/%_test.c $(TEST_SRC) + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + diff --git a/tests/alice/charon.conf b/configs/alice/charon.conf similarity index 100% rename from tests/alice/charon.conf rename to configs/alice/charon.conf diff --git a/tests/bob/charon.conf b/configs/bob/charon.conf similarity index 100% rename from tests/bob/charon.conf rename to configs/bob/charon.conf diff --git a/tests/charon.conf b/configs/charon.conf similarity index 100% rename from tests/charon.conf rename to configs/charon.conf diff --git a/docker-compose.yml b/docker-compose.yml index f10443f..39bbfa9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,7 @@ services: cap_add: - NET_ADMIN volumes: - - ./tests/alice/:/app/conf/ + - ./configs/alice/:/app/conf/ devices: - /dev/net/tun profiles: @@ -42,7 +42,7 @@ services: - /app/build/charon - /app/conf/charon.conf volumes: - - ./tests/bob:/app/conf/ + - ./configs/bob:/app/conf/ devices: - /dev/net/tun profiles: diff --git a/docker/alice.conf b/docker/alice.conf deleted file mode 100644 index 1f6b082..0000000 --- a/docker/alice.conf +++ /dev/null @@ -1,7 +0,0 @@ -[bundle] -aap2_socket=./aap2.sock -remote_eid=dtn://charon.dtn/ -secret_name=D3TN_AAP2_KEY - -[interface] -address=10.0.0.1/32 diff --git a/docker/bob.conf b/docker/bob.conf deleted file mode 100644 index 35febd5..0000000 --- a/docker/bob.conf +++ /dev/null @@ -1,8 +0,0 @@ -[bundle] -aap2_socket=./aap2.sock -remote_eid=dtn://charon.dtn/ -secret_name=D3TN_AAP2_KEY - -[interface] -address=10.0.0.1/32 - diff --git a/tests/config_test.c b/tests/config_test.c new file mode 100644 index 0000000..55a2e69 --- /dev/null +++ b/tests/config_test.c @@ -0,0 +1,33 @@ +#include "../src/config.h" +#include "test.h" +#include + +int test_config_parsed() { + char *config_file_path = "./tests/configs/working.conf"; + + charon_config *conf = read_config(config_file_path); + + char *aap2_address = conf->aap2_address; + char *remote_eid = conf->remote_eid; + char *secret_name = conf->secret_name; + + char *address = conf->address; + int mtu = conf->mtu; + + ASSERT_TRUE(strcmp(aap2_address, "tcp://0.0.0.0:4225") == 0); + ASSERT_TRUE(strcmp(remote_eid, "dtn://bob.dtn/hey") == 0); + ASSERT_TRUE(strcmp(secret_name, "D3TN_AAP2_KEY") == 0); + ASSERT_TRUE(strcmp(address, "10.0.0.1") == 0); + ASSERT_EQ(mtu, 1500); + + free_config(conf); + TEST_PASS(); + return 0; +} + +int main() { + if (test_config_parsed() > 0) { + return 1; + } + return 0; +} diff --git a/tests/configs/working.conf b/tests/configs/working.conf new file mode 100644 index 0000000..46783ab --- /dev/null +++ b/tests/configs/working.conf @@ -0,0 +1,8 @@ +[bundle] +aap2_address=tcp://0.0.0.0:4225 +remote_eid=dtn://bob.dtn/hey +secret_name=D3TN_AAP2_KEY + +[interface] +address=10.0.0.1 +mtu=1500 diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..99b8a17 --- /dev/null +++ b/tests/test.h @@ -0,0 +1,32 @@ +#include +#define TEST_PASS() printf(" [PASS] %s\n", __func__) +#define TEST_FAIL(msg) \ + do { \ + printf(" [FAIL] %s: %s\n", __func__, msg); \ + return 1; \ + } while (0) + +#define ASSERT_EQ(a, b) \ + do { \ + if ((a) != (b)) { \ + printf(" [FAIL] %s: %s != %s at line %d\n", __func__, #a, #b, __LINE__); \ + return 1; \ + } \ + } while (0) + +#define ASSERT_NE(a, b) \ + do { \ + if ((a) == (b)) { \ + printf(" [FAIL] %s: %s == %s at line %d\n", __func__, #a, #b, __LINE__); \ + return 1; \ + } \ + } while (0) + +#define ASSERT_TRUE(cond) \ + do { \ + if (!(cond)) { \ + printf(" [FAIL] %s: %s is false at line %d\n", __func__, #cond, __LINE__); \ + return 1; \ + } \ + } while (0) + From 9acbf9bda61ecc94496bee9230bb12b2ca520cb2 Mon Sep 17 00:00:00 2001 From: Courtcircuits Date: Sun, 7 Jun 2026 17:13:24 +0200 Subject: [PATCH 2/2] fix: adding build directory before build --- .github/workflows/unit.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml index 900e875..f2ac89f 100644 --- a/.github/workflows/unit.yml +++ b/.github/workflows/unit.yml @@ -25,4 +25,6 @@ jobs: libprotobuf-c-dev - name: Run unit tests - run: make test TEST=config + run: | + mkdir build + make test TEST=config