Tests
Unit tests
There is a comprehensive test suite employing CMocka. You can run all of them using ctest in the build directory. Individual tests are themselves runnable. Please refer to CTest documentation for detailed information on how to specify particular subset of tests.
CBOR Working Group test vectors
The test vectors maintained by the IETF CBOR Working Group
are vendored in test/data/cbor-test-vectors and run by cbor_test_vectors_test.
Every vector is decoded with cbor_load(), compared against the expected
value, and re-serialized; vectors that are not well-formed must be rejected. See
the README.md in the data directory for the container format and the pinned
upstream revision.
The few vectors that libcbor deliberately does not satisfy – well-formed but
invalid inputs (see Well-formedness and validity) and NaN payloads, which are
canonicalized on encoding – are listed with their rationale in
test/cbor_test_vectors_test.c. The test fails if any of them starts passing,
so the list stays current.
Testing for memory leaks
Every release is tested for memory correctness. You can run these tests by passing the -T memcheck flag to ctest. [1]
Code coverage
Every release is inspected using GCOV/LCOV. Platform-independent code should be fully covered by the test suite. Simply run
make coverage
or alternatively run lcov by hand using
lcov --capture --directory . --output-file coverage.info
genhtml coverage.info --output-directory out
Fuzz testing
Every release is tested using a fuzz test. In this test, a huge buffer filled with random data is passed to the decoder. We require that it either succeeds or fail with a sensible error, without leaking any memory. This is intended to simulate real-world situations where data received from the network are CBOR-decoded before any further processing.
Each input is also replayed with every allocation failing in turn, including
realloc calls. These runs must return CBOR_ERR_MEMERROR and release all
allocations made before the failure. A fixed nested input containing every item
type exercises container growth and partial cleanup on every run, independent
of the random seed.
The test accepts an optional random seed (for example, test/fuzz_test 42)
to reproduce a run.