# Testing Guide for systemd-netlogd This document provides comprehensive testing instructions for systemd-netlogd. ## Table of Contents - [Unit Tests](#unit-tests) - [Integration Tests](#integration-tests) - [Manual Testing](#manual-testing) - [Protocol Compliance Testing](#protocol-compliance-testing) - [Security Testing](#security-testing) - [Performance Testing](#performance-testing) - [Continuous Integration](#continuous-integration) ## Unit Tests ### Running Unit Tests ```bash # Build with tests meson setup build meson compile -C build # Run all tests meson test -C build # Run with verbose output meson test -C build -v # Run specific test meson test -C build test-protocol -v meson test -C build test-string-tables -v ``` ### Test Suite Overview #### test-protocol Tests RFC 3339 timestamp formatting: - Specific timestamp formatting - NULL timestamp (current time) - Structure validation (T separator, timezone format) #### test-string-tables Tests string table conversions: - Protocol names (udp, tcp, tls, dtls) - Log formats (rfc5424, rfc3164, rfc5425) - Syslog facilities (kern, user, mail, etc.) - Syslog levels (emerg, alert, crit, etc.) ### Writing New Tests Create a new test file in `tests/`: ```c /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include #include #include #include #include "your-module.h" static void test_your_feature(void **state) { int result = your_function(); assert_int_equal(result, EXPECTED_VALUE); } int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_your_feature), }; return cmocka_run_group_tests(tests, NULL, NULL); } ``` Add to `tests/meson.build`: ```meson test_your_module = executable( 'test-your-module', 'test-your-module.c', '../src/netlog/your-module.c', include_directories : includes, link_with : libshared, dependencies : [cmocka, test_libsystemd], ) test('your-module', test_your_module) ``` ## Integration Tests ### Test With Local Syslog Server #### Option 1: Using netcat Start a simple UDP receiver: ```bash # Terminal 1: Start receiver nc -ul 514 # Or for TCP nc -l 514 ``` Configure systemd-netlogd: ```ini # /etc/systemd/netlogd.conf [Network] Address=127.0.0.1:514 Protocol=udp ``` Generate test logs: ```bash logger -p user.info "Test message from systemd-netlogd" logger -p user.warning "Another test message" ``` #### Option 2: Using rsyslog Install and configure rsyslog: ```bash sudo dnf install rsyslog ``` Configure `/etc/rsyslog.conf`: ``` # UDP listener module(load="imudp") input(type="imudp" port="514") # TCP listener module(load="imtcp") input(type="imtcp" port="514") # Log to file *.* /var/log/netlogd-test.log ``` Restart rsyslog: ```bash sudo systemctl restart rsyslog ``` Monitor logs: ```bash tail -f /var/log/netlogd-test.log ``` #### Option 3: Using syslog-ng ```bash sudo dnf install syslog-ng ``` Configure `/etc/syslog-ng/syslog-ng.conf`: ``` source s_network { udp(port(514)); tcp(port(514)); }; destination d_netlogd_test { file("/var/log/netlogd-test.log"); }; log { source(s_network); destination(d_netlogd_test); }; ``` ### TLS Testing #### Generate Test Certificates Create CA and server certificates: ```bash #!/bin/bash # generate-certs.sh # Create CA openssl genrsa -out ca-key.pem 2048 openssl req -new -x509 -days 365 -key ca-key.pem -out ca-cert.pem \ -subj "/C=US/ST=State/L=City/O=Test/CN=Test CA" # Create server certificate openssl genrsa -out server-key.pem 2048 openssl req -new -key server-key.pem -out server-req.pem \ -subj "/C=US/ST=State/L=City/O=Test/CN=localhost" openssl x509 -req -days 365 -in server-req.pem -CA ca-cert.pem \ -CAkey ca-key.pem -CAcreateserial -out server-cert.pem # Cleanup rm server-req.pem ``` #### Start TLS Syslog Receiver Using stunnel: ```bash sudo dnf install stunnel # Create stunnel config cat > /etc/stunnel/syslog-tls.conf < /etc/systemd/netlogd.conf < /etc/systemd/netlogd.conf < /etc/systemd/netlogd.conf < /etc/systemd/netlogd.conf <\d+)>(?P\d+) ' r'(?P\S+) ' r'(?P\S+) ' r'(?P\S+) ' r'(?P\S+) ' r'(?P\S+) ' r'(?P\S+|\[.*?\]) ' r'(?P.*)$' ) # Start UDP listener sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(('0.0.0.0', 514)) print("Listening for syslog messages on UDP 514...") while True: data, addr = sock.recvfrom(4096) msg = data.decode('utf-8', errors='replace') print(f"\nReceived from {addr}:") print(f" Raw: {repr(msg)}") match = RFC5424_PATTERN.match(msg) if match: print(" ✓ RFC 5424 compliant") print(f" Priority: {match.group('pri')}") print(f" Version: {match.group('ver')}") print(f" Timestamp: {match.group('ts')}") print(f" Hostname: {match.group('host')}") else: print(" ✗ NOT RFC 5424 compliant") ``` ### RFC 3339 Timestamp Validation ```python from datetime import datetime def validate_rfc3339(timestamp): """Validate RFC 3339 timestamp format""" try: # Format: YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM datetime.fromisoformat(timestamp.replace(':', '', 2).replace(':', '', 1)) return True except ValueError: return False # Test ts = "2024-01-20T10:30:15.123456+05:30" print(f"{ts}: {validate_rfc3339(ts)}") ``` ## Security Testing ### Certificate Validation Testing Test different validation modes: ```bash # Test 1: Valid certificate (should succeed) cat > /etc/systemd/netlogd.conf <.c` 3. Add test to `tests/meson.build` 4. Run and verify: `meson test -C build -v` 5. Submit PR with tests ## Troubleshooting Tests ### Test Failures ```bash # View detailed test log cat build/meson-logs/testlog.txt # Run failing test with GDB gdb --args build/tests/test-protocol # Run with valgrind valgrind --leak-check=full build/tests/test-protocol ``` ### Common Issues **Issue: cmocka not found** ```bash # Install cmocka sudo dnf install libcmocka-devel # Fedora sudo apt install libcmocka-dev # Debian/Ubuntu ``` **Issue: Tests time out** ```bash # Increase timeout meson test -C build -t 10 # 10x default timeout ``` **Issue: Tests pass locally but fail in CI** - Check for race conditions - Verify CI environment matches local - Review CI logs for environment differences ## Resources - [cmocka Documentation](https://cmocka.org/) - [Meson Testing](https://mesonbuild.com/Unit-tests.html) - [RFC 5424 Test Vectors](https://tools.ietf.org/html/rfc5424#section-6.5) - [systemd Journal Testing](https://www.freedesktop.org/software/systemd/man/systemd-journal-remote.service.html)