Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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: |
mkdir build
make test TEST=config
21 changes: 19 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ services:
cap_add:
- NET_ADMIN
volumes:
- ./tests/alice/:/app/conf/
- ./configs/alice/:/app/conf/
devices:
- /dev/net/tun
profiles:
Expand All @@ -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:
Expand Down
7 changes: 0 additions & 7 deletions docker/alice.conf

This file was deleted.

8 changes: 0 additions & 8 deletions docker/bob.conf

This file was deleted.

33 changes: 33 additions & 0 deletions tests/config_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "../src/config.h"
#include "test.h"
#include <string.h>

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;
}
8 changes: 8 additions & 0 deletions tests/configs/working.conf
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions tests/test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#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)

Loading