Skip to content
Closed
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
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI
on:
push:
branches: [main]
pull_request:

jobs:
ubuntu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: sudo apt-get update && sudo apt-get install -y build-essential pkg-config libcmocka-dev
- run: make test

fedora:
runs-on: ubuntu-latest
container: fedora:latest
steps:
- run: dnf install -y git gcc make pkgconf-pkg-config libcmocka-devel libasan libubsan
- uses: actions/checkout@v4
- run: make test
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


tests/test_*
!tests/test_*.c
*.o
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CC ?= gcc
CFLAGS += -std=c11 -Wall -Wextra -g -Isrc
SAN = -fsanitize=address,undefined -fno-omit-frame-pointer
CMOCKA_LIBS := $(shell pkg-config --libs cmocka)

TEST_SRCS := $(wildcard tests/test_*.c)
TESTS := $(TEST_SRCS:.c=)

.PHONY: test clean
test: $(TESTS)
@for t in $(TESTS); do echo "== $$t"; ./$$t || exit 1; done

tests/test_%: tests/test_%.c
$(CC) $(CFLAGS) $(SAN) $< $(wildcard src/$*.c) -o $@ $(CMOCKA_LIBS) $(WRAP_test_$*)

clean:
rm -f $(TESTS)
78 changes: 78 additions & 0 deletions tests/template.c.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* cmocka test template
*
* HOW TO USE:
* 1. Copy this file: `cp tests/template.c.example tests/test_<module>.c` - <module> must match the src file name
* 2. Replace <module> placeholders
* 3. Write tests
* 4. List every test created in the `tests[]` in main
* 5. Run: `make test`
*
* - list of common assertions at bottom
*/

// these headers are required by cmocka - keep in order
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <stdlib.h>
//TODO: #include "<module>.h" // header of the code under test

// ex: basic test
static void test_<behavior>(void **state) {
(void)state;

// set up inputs
// call function under test
// check result
assert_int_equal(1 + 1, 2); //FIXME
}

// ex: edge/error case
static void test_<error>(void **state) {
(void)state;
assert_int_equal(my_function(NULL), -1);
}

// ex: setup/teardown
static int setup(void **state) {
*state = malloc(sizeof(int));
return (*state == NULL) ? -1 : 0;
}
static int teardown(void **state) {
free(*state);
return 0;
}

// ex: mock syst call (needs a matching `WRAP_test_<module> line in Makefile - `WRAP_test_actions = -Wl,--wrap=kill`)
int __wrap_kill(pid_t pid, int sig) {
check_expected(pid);
check_expected(sig);
return mock_type(int);
}
/* to use in a test:
* expect_value(__wrap_kill, pid 1234);
* expect_value(__wrap_kill, sig, SIGTERM);
* will_return(__wrap_kill, 0);
*/


// register tests
int main(void) {
const struct CMUnitTest tests[] = {
cmock_unit_test(test_<behavior>),
cmocka_unit_test_setup_teardown(test_x, setup, teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

/* COMMON ASSERTIONS:
* assert_true(x) / assert_false(x)
* assert_int_equal(actual, expected) / assert_int_not_equal(a, b)
* assert_string_equal(actual, expected)
* assert_null(p) / assert_non_null(p)
* assert_float_equal(actual, expected, epsilon)
* assert_in_range(x, min, max)
* assert_memory_equal(a, b, size)
*/
17 changes: 17 additions & 0 deletions tests/test_smoke.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

static void test_cmocka_works(void **state) {
(void)state;
assert_int_equal(1 + 1, 2);
}

int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_cmocka_works),
};

return cmocka_run_group_tests(tests, NULL, NULL);
}
Loading