# C Integration Tests
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
- [Prerequisites](#prerequisites)
- [Building Tests](#building-tests)
- [Using Make](#using-make)
- [Using CMake](#using-cmake)
- [Test Suites](#test-suites)
- [test_roundtrip.c (7 tests)](#test_roundtripc-7-tests)
- [test_errors.c (9 tests)](#test_errorsc-9-tests)
- [test_memory.c (9 tests)](#test_memoryc-9-tests)
- [Running Tests](#running-tests)
- [Basic Test Run](#basic-test-run)
- [Memory Leak Detection with Valgrind](#memory-leak-detection-with-valgrind)
- [AddressSanitizer](#addresssanitizer)
- [UndefinedBehaviorSanitizer](#undefinedbehaviorsanitizer)
- [Test Results](#test-results)
- [See Also](#see-also)
This directory contains integration tests for the Synta C FFI layer.
## Prerequisites
Build the Synta FFI library:
```bash
cd ../..
cargo build --release -p synta-ffi
```
## Building Tests
### Using Make
```bash
make # Build all tests
make test # Build and run all tests
make valgrind # Run tests under Valgrind
make asan # Run tests with AddressSanitizer
make ubsan # Run tests with UndefinedBehaviorSanitizer
make clean # Clean build artifacts
```
### Using CMake
```bash
mkdir build && cd build
cmake ..
make
ctest # Run tests
```
## Test Suites
### test_roundtrip.c (7 tests)
Tests encode/decode roundtrip for all primitive types:
- Integer values (int64_t)
- Boolean values (true/false)
- Octet strings
- Object identifiers
- Sequences with nested elements
- UTF-8 strings
- REAL values (zero, ±∞, NaN, finite f64)
All tests verify that encoding then decoding produces the original value.
Special-value encoding bytes are also verified (0x40/0x41/0x42 for ±∞/NaN).
### test_errors.c (9 tests)
Tests error handling and validation:
- Invalid tag detection
- Truncated data detection
- NULL pointer handling
- Empty data handling
- Error message retrieval
- Invalid UTF-8 strings
- Integer overflow detection
- Invalid OID strings
- Tag mismatch detection
### test_memory.c (9 tests)
Tests memory management and cleanup:
- Decoder lifecycle (1000 iterations)
- Encoder lifecycle (1000 iterations)
- Integer helper cleanup
- OID helper cleanup
- OctetString helper cleanup
- ByteArray helper cleanup
- Nested sequence cleanup
- Large data allocation (1 MB)
- Error path cleanup
All tests are designed to be run under Valgrind to detect memory leaks.
## Running Tests
### Basic Test Run
```bash
export LD_LIBRARY_PATH=../../target/release
./test_roundtrip
./test_errors
./test_memory
```
Or use make:
```bash
make test
```
### Memory Leak Detection with Valgrind
```bash
make valgrind
```
Expected output: "All tests passed with no leaks!"
### AddressSanitizer
```bash
make asan
```
Detects:
- Use-after-free
- Buffer overflows
- Memory leaks
- Double-free
### UndefinedBehaviorSanitizer
```bash
make ubsan
```
Detects:
- Signed integer overflow
- Null pointer dereference
- Misaligned pointer access
- Division by zero
## Test Results
All tests pass:
- **25 total tests** (7 roundtrip + 9 error + 9 memory)
- **0 memory leaks** (verified with Valgrind)
- **0 memory errors** (verified with ASAN)
- **0 undefined behavior** (verified with UBSAN)
## See Also
- [C Examples](../../synta-ffi/synta-ffi/examples/c/) - Example programs using the FFI
- [C API Reference](../../docs/C_API.md) - Complete API documentation
- [Memory Management Guide](../../docs/C_MEMORY.md) - Memory safety guide