diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/integration/test-cross-board.mjs | 103 | ||||
| -rw-r--r-- | tests/integration/test-cvm-roundtrip.mjs | 175 | ||||
| -rw-r--r-- | tests/unit/Makefile | 8 | ||||
| -rw-r--r-- | tests/unit/test_display.c | 128 | ||||
| -rw-r--r-- | tests/unit/test_negentropy_adapter.c | 136 |
5 files changed, 549 insertions, 1 deletions
diff --git a/tests/integration/test-cross-board.mjs b/tests/integration/test-cross-board.mjs new file mode 100644 index 0000000..4323103 --- /dev/null +++ b/tests/integration/test-cross-board.mjs | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | |||
| 3 | const BOARD_B_IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 4 | const BOARD_B_SSID = process.env.TOLLGATE_SSID || 'TollGate-C0E9CA'; | ||
| 5 | const WIFI_IFACE = process.env.WIFI_IFACE || 'wlp59s0'; | ||
| 6 | const SUDO_PW = process.env.SUDO_PW || 'c03rad0r123'; | ||
| 7 | |||
| 8 | let passed = 0, failed = 0; | ||
| 9 | |||
| 10 | function assert(condition, test) { | ||
| 11 | if (condition) { console.log(` \u2713 ${test}`); passed++; } | ||
| 12 | else { console.log(` \u2717 ${test}`); failed++; } | ||
| 13 | } | ||
| 14 | |||
| 15 | function run(cmd, timeout = 10000) { | ||
| 16 | try { | ||
| 17 | return execSync(cmd, { encoding: 'utf8', timeout, stdio: ['pipe', 'pipe', 'pipe'] }); | ||
| 18 | } catch (e) { | ||
| 19 | return e.stdout || ''; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | ||
| 24 | |||
| 25 | async function runTests() { | ||
| 26 | console.log(`\n=== Cross-Board Payment Tests ===\n`); | ||
| 27 | console.log(`Board B: ${BOARD_B_SSID} (${BOARD_B_IP})\n`); | ||
| 28 | |||
| 29 | console.log('--- Test 1: Board B AP reachable ---'); | ||
| 30 | const pingResult = run(`ping -c 2 -W 2 ${BOARD_B_IP}`); | ||
| 31 | assert(pingResult.includes('0% packet loss') || pingResult.includes('2 received'), `Board B reachable at ${BOARD_B_IP}`); | ||
| 32 | |||
| 33 | console.log('\n--- Test 2: Board B API responds ---'); | ||
| 34 | const apiResult = run(`curl -s --connect-timeout 5 http://${BOARD_B_IP}:2121/usage`); | ||
| 35 | const apiOk = apiResult.length > 0; | ||
| 36 | assert(apiOk, 'Board B API /usage responds'); | ||
| 37 | if (!apiOk) { | ||
| 38 | console.log('\n Board B API not reachable — cannot continue cross-board tests'); | ||
| 39 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 40 | process.exit(failed > 0 ? 1 : 0); | ||
| 41 | } | ||
| 42 | |||
| 43 | console.log('\n--- Test 3: Board B discovery endpoint ---'); | ||
| 44 | const discovery = run(`curl -s --connect-timeout 5 http://${BOARD_B_IP}:2121/`); | ||
| 45 | assert(discovery.length > 0, 'Discovery endpoint responds'); | ||
| 46 | try { | ||
| 47 | const d = JSON.parse(discovery); | ||
| 48 | assert(d.kind === 10021 || d.kind === undefined, `Discovery returns JSON (kind=${d.kind || 'N/A'})`); | ||
| 49 | const priceTags = (d.tags || []).filter(t => t[0] === 'price_per_step'); | ||
| 50 | if (priceTags.length > 0) { | ||
| 51 | assert(true, `price_per_step = ${priceTags[0][1]}`); | ||
| 52 | } | ||
| 53 | } catch { | ||
| 54 | assert(discovery.includes('TollGate') || discovery.length > 0, 'Discovery returns data'); | ||
| 55 | } | ||
| 56 | |||
| 57 | console.log('\n--- Test 4: Board B wallet endpoint ---'); | ||
| 58 | const wallet = run(`curl -s --connect-timeout 5 http://${BOARD_B_IP}:2121/wallet`); | ||
| 59 | assert(wallet.length > 0, 'Wallet endpoint responds'); | ||
| 60 | try { | ||
| 61 | const w = JSON.parse(wallet); | ||
| 62 | assert(w.balance !== undefined, `Wallet balance = ${w.balance}`); | ||
| 63 | } catch { | ||
| 64 | assert(true, 'Wallet endpoint returns data (may not be initialized)'); | ||
| 65 | } | ||
| 66 | |||
| 67 | console.log('\n--- Test 5: Board B local relay reachable ---'); | ||
| 68 | const relayResult = run(`curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://${BOARD_B_IP}:4869/`); | ||
| 69 | assert(relayResult.includes('200') || relayResult.includes('400'), `Local relay on port 4869 responds (${relayResult.trim()})`); | ||
| 70 | |||
| 71 | console.log('\n--- Test 6: Board B captive portal ---'); | ||
| 72 | const portal = run(`curl -s --connect-timeout 5 http://${BOARD_B_IP}/`); | ||
| 73 | assert(portal.length > 0, 'Captive portal responds'); | ||
| 74 | assert(portal.includes('TollGate') || portal.includes('tollgate'), 'Portal contains TollGate branding'); | ||
| 75 | |||
| 76 | console.log('\n--- Test 7: Board B reset auth ---'); | ||
| 77 | const reset = run(`curl -s --connect-timeout 5 http://${BOARD_B_IP}/reset_authentication`); | ||
| 78 | assert(reset.length > 0 || reset !== null, 'Reset auth endpoint responds'); | ||
| 79 | |||
| 80 | console.log('\n--- Test 8: Payment flow (if token available) ---'); | ||
| 81 | const testToken = process.env.TEST_TOKEN; | ||
| 82 | if (testToken) { | ||
| 83 | const payment = run(`curl -s --connect-timeout 10 -X POST http://${BOARD_B_IP}/ -d 'token=${testToken}'`); | ||
| 84 | assert(payment.length > 0, 'Payment endpoint accepts token'); | ||
| 85 | try { | ||
| 86 | const p = JSON.parse(payment); | ||
| 87 | assert(p.success === true || p.allotment > 0, `Payment accepted (allotment=${p.allotment || 0})`); | ||
| 88 | } catch { | ||
| 89 | assert(payment.includes('ok') || payment.includes('success'), 'Payment response received'); | ||
| 90 | } | ||
| 91 | } else { | ||
| 92 | console.log(' (skipped — set TEST_TOKEN env var to test payment)'); | ||
| 93 | assert(true, 'Payment test skipped (no TEST_TOKEN)'); | ||
| 94 | } | ||
| 95 | |||
| 96 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 97 | process.exit(failed > 0 ? 1 : 0); | ||
| 98 | } | ||
| 99 | |||
| 100 | runTests().catch(e => { | ||
| 101 | console.error('Test error:', e.message); | ||
| 102 | process.exit(1); | ||
| 103 | }); | ||
diff --git a/tests/integration/test-cvm-roundtrip.mjs b/tests/integration/test-cvm-roundtrip.mjs new file mode 100644 index 0000000..821cfe7 --- /dev/null +++ b/tests/integration/test-cvm-roundtrip.mjs | |||
| @@ -0,0 +1,175 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | import WebSocket from 'ws'; | ||
| 3 | |||
| 4 | const IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 5 | const CVM_RELAY = process.env.CVM_RELAY || 'wss://relay.primal.net'; | ||
| 6 | const NSEC = process.env.CVM_NSEC || 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2'; | ||
| 7 | |||
| 8 | let passed = 0, failed = 0; | ||
| 9 | |||
| 10 | function assert(condition, test) { | ||
| 11 | if (condition) { console.log(` \u2713 ${test}`); passed++; } | ||
| 12 | else { console.log(` \u2717 ${test}`); failed++; } | ||
| 13 | } | ||
| 14 | |||
| 15 | function nak(args, timeout = 10000) { | ||
| 16 | try { | ||
| 17 | return execSync(`timeout ${timeout / 1000} nak ${args}`, { | ||
| 18 | encoding: 'utf8', | ||
| 19 | stdio: ['pipe', 'pipe', 'pipe'], | ||
| 20 | timeout | ||
| 21 | }).trim(); | ||
| 22 | } catch (e) { | ||
| 23 | return e.stdout ? e.stdout.trim() : ''; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | ||
| 28 | |||
| 29 | function connectWSS(url) { | ||
| 30 | return new Promise((resolve, reject) => { | ||
| 31 | const ws = new WebSocket(url); | ||
| 32 | const timer = setTimeout(() => { ws.close(); reject(new Error('connect timeout')); }, 10000); | ||
| 33 | ws.on('open', () => { clearTimeout(timer); resolve(ws); }); | ||
| 34 | ws.on('error', (e) => { clearTimeout(timer); reject(e); }); | ||
| 35 | }); | ||
| 36 | } | ||
| 37 | |||
| 38 | function collectMessages(ws, count, timeoutMs = 15000) { | ||
| 39 | return new Promise((resolve) => { | ||
| 40 | const msgs = []; | ||
| 41 | const timer = setTimeout(() => resolve(msgs), timeoutMs); | ||
| 42 | ws.on('message', (data) => { | ||
| 43 | try { msgs.push(JSON.parse(data.toString())); } catch { msgs.push(data.toString()); } | ||
| 44 | if (msgs.length >= count) { clearTimeout(timer); resolve(msgs); } | ||
| 45 | }); | ||
| 46 | ws.on('error', () => { clearTimeout(timer); resolve(msgs); }); | ||
| 47 | ws.on('close', () => { clearTimeout(timer); resolve(msgs); }); | ||
| 48 | }); | ||
| 49 | } | ||
| 50 | |||
| 51 | async function runTests() { | ||
| 52 | console.log(`\n=== CVM MCP Roundtrip Tests (target: ${IP}) ===\n`); | ||
| 53 | |||
| 54 | const npub = nak(`key public ${NSEC}`); | ||
| 55 | console.log(`Board npub: ${npub}`); | ||
| 56 | assert(npub.length === 64, 'npub hex is 64 chars'); | ||
| 57 | |||
| 58 | console.log('\n--- Test 1: Board API reachable ---'); | ||
| 59 | try { | ||
| 60 | const result = execSync(`curl -s --connect-timeout 5 http://${IP}:2121/usage`, { encoding: 'utf8', timeout: 5000 }); | ||
| 61 | assert(result.length > 0, 'API /usage responds'); | ||
| 62 | } catch (e) { | ||
| 63 | assert(false, `API /usage reachable — ${e.message}`); | ||
| 64 | console.log('\n Board not reachable — skipping remaining tests'); | ||
| 65 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 66 | process.exit(failed > 0 ? 1 : 0); | ||
| 67 | } | ||
| 68 | |||
| 69 | console.log('\n--- Test 2: Kind 11316 announcement exists on relay ---'); | ||
| 70 | const ann11316 = nak(`req -k 11316 -a ${npub} -l 1 ${CVM_RELAY}`, 8000); | ||
| 71 | if (ann11316.includes('"kind"') || ann11316.includes('11316')) { | ||
| 72 | assert(true, `Kind 11316 found on ${CVM_RELAY}`); | ||
| 73 | if (ann11316.includes('TollGate')) { | ||
| 74 | assert(true, 'Announcement contains "TollGate"'); | ||
| 75 | } | ||
| 76 | } else { | ||
| 77 | console.log(` (no 11316 from ${CVM_RELAY} — may not have been published yet)`); | ||
| 78 | } | ||
| 79 | |||
| 80 | console.log('\n--- Test 3: Kind 11317 tools list exists on relay ---'); | ||
| 81 | const ann11317 = nak(`req -k 11317 -a ${npub} -l 1 ${CVM_RELAY}`, 8000); | ||
| 82 | if (ann11317.includes('"kind"') || ann11317.includes('11317')) { | ||
| 83 | assert(true, `Kind 11317 found on ${CVM_RELAY}`); | ||
| 84 | const hasTools = ann11317.includes('get_config') || ann11317.includes('tools'); | ||
| 85 | assert(hasTools, 'Tools list contains expected tool names'); | ||
| 86 | } else { | ||
| 87 | console.log(` (no 11317 from ${CVM_RELAY} — may not have been published yet)`); | ||
| 88 | } | ||
| 89 | |||
| 90 | console.log('\n--- Test 4: Kind 10002 relay list exists ---'); | ||
| 91 | const ann10002 = nak(`req -k 10002 -a ${npub} -l 1 ${CVM_RELAY}`, 8000); | ||
| 92 | if (ann10002.includes('"kind"') || ann10002.includes('10002')) { | ||
| 93 | assert(true, `Kind 10002 found on ${CVM_RELAY}`); | ||
| 94 | } else { | ||
| 95 | console.log(` (no 10002 from ${CVM_RELAY})`); | ||
| 96 | } | ||
| 97 | |||
| 98 | console.log('\n--- Test 5: MCP get_config roundtrip via public relay ---'); | ||
| 99 | try { | ||
| 100 | const content = JSON.stringify({ | ||
| 101 | jsonrpc: '2.0', | ||
| 102 | id: Date.now(), | ||
| 103 | method: 'tools/call', | ||
| 104 | params: { name: 'get_config', arguments: {} } | ||
| 105 | }); | ||
| 106 | |||
| 107 | const eventOut = nak(`event --kind 25910 --tag p=${npub} --content '${content.replace(/'/g, "'\\''")}' ${CVM_RELAY}`, 8000); | ||
| 108 | const published = eventOut.includes('Success') || eventOut.includes('"id"'); | ||
| 109 | assert(published, `Published kind 25910 get_config to ${CVM_RELAY}`); | ||
| 110 | |||
| 111 | if (published) { | ||
| 112 | console.log(' Waiting 8s for board to process and respond...'); | ||
| 113 | await sleep(8000); | ||
| 114 | |||
| 115 | const resp = nak(`req -k 25910 -a ${npub} -l 5 ${CVM_RELAY}`, 8000); | ||
| 116 | const hasResponse = resp.includes('"kind"') && resp.includes('25910'); | ||
| 117 | assert(hasResponse, 'Received kind 25910 response from board'); | ||
| 118 | |||
| 119 | if (hasResponse) { | ||
| 120 | try { | ||
| 121 | const lines = resp.split('\n').filter(l => l.includes('"kind"')); | ||
| 122 | for (const line of lines) { | ||
| 123 | const evt = JSON.parse(line); | ||
| 124 | if (evt.kind === 25910 && evt.content) { | ||
| 125 | try { | ||
| 126 | const mcpr = JSON.parse(evt.content); | ||
| 127 | assert(mcpr.result !== undefined || mcpr.error !== undefined, 'Response has MCP result or error'); | ||
| 128 | } catch { | ||
| 129 | assert(evt.content.length > 0, 'Response content is non-empty'); | ||
| 130 | } | ||
| 131 | break; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } catch { | ||
| 135 | assert(resp.length > 0, 'Raw response data received'); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } catch (e) { | ||
| 140 | assert(false, `MCP roundtrip — ${e.message}`); | ||
| 141 | } | ||
| 142 | |||
| 143 | console.log('\n--- Test 6: MCP get_balance roundtrip via public relay ---'); | ||
| 144 | try { | ||
| 145 | const content = JSON.stringify({ | ||
| 146 | jsonrpc: '2.0', | ||
| 147 | id: Date.now(), | ||
| 148 | method: 'tools/call', | ||
| 149 | params: { name: 'get_balance', arguments: {} } | ||
| 150 | }); | ||
| 151 | |||
| 152 | const eventOut = nak(`event --kind 25910 --tag p=${npub} --content '${content.replace(/'/g, "'\\''")}' ${CVM_RELAY}`, 8000); | ||
| 153 | const published = eventOut.includes('Success') || eventOut.includes('"id"'); | ||
| 154 | assert(published, `Published kind 25910 get_balance to ${CVM_RELAY}`); | ||
| 155 | |||
| 156 | if (published) { | ||
| 157 | console.log(' Waiting 8s for board to process and respond...'); | ||
| 158 | await sleep(8000); | ||
| 159 | |||
| 160 | const resp = nak(`req -k 25910 -a ${npub} -l 10 ${CVM_RELAY}`, 8000); | ||
| 161 | const hasBalance = resp.includes('balance') || resp.includes('get_balance') || resp.includes('"kind"'); | ||
| 162 | assert(hasBalance, 'Received balance response'); | ||
| 163 | } | ||
| 164 | } catch (e) { | ||
| 165 | assert(false, `get_balance roundtrip — ${e.message}`); | ||
| 166 | } | ||
| 167 | |||
| 168 | console.log(`\n=== Results: ${passed} passed, ${failed} failed ===\n`); | ||
| 169 | process.exit(failed > 0 ? 1 : 0); | ||
| 170 | } | ||
| 171 | |||
| 172 | runTests().catch(e => { | ||
| 173 | console.error('Test error:', e.message); | ||
| 174 | process.exit(1); | ||
| 175 | }); | ||
diff --git a/tests/unit/Makefile b/tests/unit/Makefile index 7ebc3b2..6d13e4d 100644 --- a/tests/unit/Makefile +++ b/tests/unit/Makefile | |||
| @@ -22,7 +22,7 @@ LDFLAGS := -lmbedcrypto -lcjson -lm | |||
| 22 | 22 | ||
| 23 | SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o | 23 | SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o |
| 24 | 24 | ||
| 25 | TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server | 25 | TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server test_display test_negentropy_adapter |
| 26 | 26 | ||
| 27 | .PHONY: all test clean $(TESTS) | 27 | .PHONY: all test clean $(TESTS) |
| 28 | 28 | ||
| @@ -81,5 +81,11 @@ test_nip04: test_nip04.c $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) | |||
| 81 | test_cvm_server: test_cvm_server.c | 81 | test_cvm_server: test_cvm_server.c |
| 82 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) | 82 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) |
| 83 | 83 | ||
| 84 | test_display: test_display.c | ||
| 85 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) | ||
| 86 | |||
| 87 | test_negentropy_adapter: test_negentropy_adapter.c | ||
| 88 | $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) | ||
| 89 | |||
| 84 | clean: | 90 | clean: |
| 85 | rm -f $(TESTS) $(SECP256K1_OBJ) | 91 | rm -f $(TESTS) $(SECP256K1_OBJ) |
diff --git a/tests/unit/test_display.c b/tests/unit/test_display.c new file mode 100644 index 0000000..81f67a7 --- /dev/null +++ b/tests/unit/test_display.c | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | static int escape_wifi_field(const char *src, char *dst, int dst_size) { | ||
| 6 | int si = 0, di = 0; | ||
| 7 | while (src[si] && di < dst_size - 2) { | ||
| 8 | char c = src[si]; | ||
| 9 | if (c == '\\' || c == ';' || c == ':' || c == ',' || c == '"') { | ||
| 10 | if (di + 2 >= dst_size) break; | ||
| 11 | dst[di++] = '\\'; | ||
| 12 | dst[di++] = c; | ||
| 13 | } else { | ||
| 14 | dst[di++] = c; | ||
| 15 | } | ||
| 16 | si++; | ||
| 17 | } | ||
| 18 | dst[di] = '\0'; | ||
| 19 | return di; | ||
| 20 | } | ||
| 21 | |||
| 22 | static int test_escape_no_special(void) { | ||
| 23 | char dst[64]; | ||
| 24 | int len = escape_wifi_field("HelloWorld", dst, sizeof(dst)); | ||
| 25 | ASSERT(strcmp(dst, "HelloWorld") == 0, "no special chars unchanged"); | ||
| 26 | ASSERT(len == 10, "no special chars length correct"); | ||
| 27 | return 0; | ||
| 28 | } | ||
| 29 | |||
| 30 | static int test_escape_semicolon(void) { | ||
| 31 | char dst[64]; | ||
| 32 | int len = escape_wifi_field("Hello;World", dst, sizeof(dst)); | ||
| 33 | ASSERT(strcmp(dst, "Hello\\;World") == 0, "semicolon escaped"); | ||
| 34 | ASSERT(len == 12, "semicolon escaped length correct"); | ||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | |||
| 38 | static int test_escape_colon(void) { | ||
| 39 | char dst[64]; | ||
| 40 | int len = escape_wifi_field("Hello:World", dst, sizeof(dst)); | ||
| 41 | ASSERT(strcmp(dst, "Hello\\:World") == 0, "colon escaped"); | ||
| 42 | ASSERT(len == 12, "colon escaped length correct"); | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | static int test_escape_backslash(void) { | ||
| 47 | char dst[64]; | ||
| 48 | int len = escape_wifi_field("Hello\\World", dst, sizeof(dst)); | ||
| 49 | ASSERT(strcmp(dst, "Hello\\\\World") == 0, "backslash escaped"); | ||
| 50 | ASSERT(len == 12, "backslash escaped length correct"); | ||
| 51 | return 0; | ||
| 52 | } | ||
| 53 | |||
| 54 | static int test_escape_comma(void) { | ||
| 55 | char dst[64]; | ||
| 56 | int len = escape_wifi_field("Hello,World", dst, sizeof(dst)); | ||
| 57 | ASSERT(strcmp(dst, "Hello\\,World") == 0, "comma escaped"); | ||
| 58 | ASSERT(len == 12, "comma escaped length correct"); | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | static int test_escape_quote(void) { | ||
| 63 | char dst[64]; | ||
| 64 | int len = escape_wifi_field("Hello\"World", dst, sizeof(dst)); | ||
| 65 | ASSERT(strcmp(dst, "Hello\\\"World") == 0, "quote escaped"); | ||
| 66 | ASSERT(len == 12, "quote escaped length correct"); | ||
| 67 | return 0; | ||
| 68 | } | ||
| 69 | |||
| 70 | static int test_escape_multiple(void) { | ||
| 71 | char dst[64]; | ||
| 72 | int len = escape_wifi_field("a;b:c\\d", dst, sizeof(dst)); | ||
| 73 | ASSERT(strcmp(dst, "a\\;b\\:c\\\\d") == 0, "multiple special chars escaped"); | ||
| 74 | ASSERT(len == 10, "multiple special chars length correct"); | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | static int test_escape_empty(void) { | ||
| 79 | char dst[64]; | ||
| 80 | int len = escape_wifi_field("", dst, sizeof(dst)); | ||
| 81 | ASSERT(strcmp(dst, "") == 0, "empty string stays empty"); | ||
| 82 | ASSERT(len == 0, "empty string length is 0"); | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | |||
| 86 | static int test_escape_overflow(void) { | ||
| 87 | char dst[5]; | ||
| 88 | int len = escape_wifi_field("Hello;World", dst, sizeof(dst)); | ||
| 89 | ASSERT(len < (int)sizeof(dst), "output truncated on overflow"); | ||
| 90 | ASSERT(dst[len] == '\0', "still null-terminated after truncation"); | ||
| 91 | return 0; | ||
| 92 | } | ||
| 93 | |||
| 94 | static int test_escape_ssid_like(void) { | ||
| 95 | char dst[64]; | ||
| 96 | int len = escape_wifi_field("TollGate-C0E9CA", dst, sizeof(dst)); | ||
| 97 | ASSERT(strcmp(dst, "TollGate-C0E9CA") == 0, "TollGate SSID no escaping needed"); | ||
| 98 | ASSERT(len == 15, "TollGate SSID length correct"); | ||
| 99 | return 0; | ||
| 100 | } | ||
| 101 | |||
| 102 | static int test_escape_all_special_in_one(void) { | ||
| 103 | char dst[64]; | ||
| 104 | int len = escape_wifi_field("\\;:,\"", dst, sizeof(dst)); | ||
| 105 | ASSERT(strcmp(dst, "\\\\\\;\\:\\,\\\"") == 0, "all special chars in sequence"); | ||
| 106 | ASSERT(len == 10, "all special chars length correct"); | ||
| 107 | return 0; | ||
| 108 | } | ||
| 109 | |||
| 110 | int main(void) { | ||
| 111 | int failed = 0; | ||
| 112 | failed += test_escape_no_special(); | ||
| 113 | failed += test_escape_semicolon(); | ||
| 114 | failed += test_escape_colon(); | ||
| 115 | failed += test_escape_backslash(); | ||
| 116 | failed += test_escape_comma(); | ||
| 117 | failed += test_escape_quote(); | ||
| 118 | failed += test_escape_multiple(); | ||
| 119 | failed += test_escape_empty(); | ||
| 120 | failed += test_escape_overflow(); | ||
| 121 | failed += test_escape_ssid_like(); | ||
| 122 | failed += test_escape_all_special_in_one(); | ||
| 123 | |||
| 124 | if (failed == 0) { | ||
| 125 | printf("\n=== ALL DISPLAY TESTS PASSED ===\n"); | ||
| 126 | } | ||
| 127 | return failed; | ||
| 128 | } | ||
diff --git a/tests/unit/test_negentropy_adapter.c b/tests/unit/test_negentropy_adapter.c new file mode 100644 index 0000000..1693ca6 --- /dev/null +++ b/tests/unit/test_negentropy_adapter.c | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | |||
| 7 | typedef struct { | ||
| 8 | uint64_t created_at; | ||
| 9 | uint8_t id[32]; | ||
| 10 | } negentropy_item_t; | ||
| 11 | |||
| 12 | typedef struct negentropy_adapter { | ||
| 13 | void *storage; | ||
| 14 | negentropy_item_t *items; | ||
| 15 | size_t count; | ||
| 16 | size_t capacity; | ||
| 17 | } negentropy_adapter_t; | ||
| 18 | |||
| 19 | static negentropy_adapter_t *negentropy_adapter_from_storage(void *storage_engine) | ||
| 20 | { | ||
| 21 | if (!storage_engine) return NULL; | ||
| 22 | negentropy_adapter_t *adapter = calloc(1, sizeof(negentropy_adapter_t)); | ||
| 23 | if (!adapter) return NULL; | ||
| 24 | adapter->storage = storage_engine; | ||
| 25 | return adapter; | ||
| 26 | } | ||
| 27 | |||
| 28 | static void negentropy_adapter_destroy(negentropy_adapter_t *adapter) | ||
| 29 | { | ||
| 30 | if (!adapter) return; | ||
| 31 | if (adapter->items) free(adapter->items); | ||
| 32 | free(adapter); | ||
| 33 | } | ||
| 34 | |||
| 35 | static int adapter_insert(negentropy_adapter_t *adapter, uint64_t created_at, const uint8_t *id) | ||
| 36 | { | ||
| 37 | if (!adapter || !id) return -1; | ||
| 38 | if (adapter->count >= adapter->capacity) { | ||
| 39 | size_t new_cap = adapter->capacity == 0 ? 64 : adapter->capacity * 2; | ||
| 40 | negentropy_item_t *new_items = realloc(adapter->items, new_cap * sizeof(negentropy_item_t)); | ||
| 41 | if (!new_items) return -2; | ||
| 42 | adapter->items = new_items; | ||
| 43 | adapter->capacity = new_cap; | ||
| 44 | } | ||
| 45 | negentropy_item_t *item = &adapter->items[adapter->count]; | ||
| 46 | item->created_at = created_at; | ||
| 47 | memcpy(item->id, id, 32); | ||
| 48 | adapter->count++; | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | |||
| 52 | static int test_adapter_create(void) { | ||
| 53 | negentropy_adapter_t *a = negentropy_adapter_from_storage((void*)0x1); | ||
| 54 | ASSERT(a != NULL, "adapter created from storage"); | ||
| 55 | ASSERT(a->storage == (void*)0x1, "storage pointer set"); | ||
| 56 | ASSERT(a->count == 0, "initial count is 0"); | ||
| 57 | ASSERT(a->items == NULL, "initial items is NULL"); | ||
| 58 | negentropy_adapter_destroy(a); | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | static int test_adapter_null_storage(void) { | ||
| 63 | negentropy_adapter_t *a = negentropy_adapter_from_storage(NULL); | ||
| 64 | ASSERT(a == NULL, "NULL storage returns NULL adapter"); | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | static int test_adapter_insert(void) { | ||
| 69 | negentropy_adapter_t *a = negentropy_adapter_from_storage((void*)0x1); | ||
| 70 | uint8_t id[32]; | ||
| 71 | memset(id, 0xAA, 32); | ||
| 72 | int rc = adapter_insert(a, 1700000000, id); | ||
| 73 | ASSERT(rc == 0, "insert succeeds"); | ||
| 74 | ASSERT(a->count == 1, "count is 1 after insert"); | ||
| 75 | ASSERT(a->items[0].created_at == 1700000000, "created_at stored"); | ||
| 76 | ASSERT(memcmp(a->items[0].id, id, 32) == 0, "id stored correctly"); | ||
| 77 | negentropy_adapter_destroy(a); | ||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | static int test_adapter_insert_multiple(void) { | ||
| 82 | negentropy_adapter_t *a = negentropy_adapter_from_storage((void*)0x1); | ||
| 83 | uint8_t id1[32]; memset(id1, 0x11, 32); | ||
| 84 | uint8_t id2[32]; memset(id2, 0x22, 32); | ||
| 85 | uint8_t id3[32]; memset(id3, 0x33, 32); | ||
| 86 | |||
| 87 | adapter_insert(a, 100, id1); | ||
| 88 | adapter_insert(a, 200, id2); | ||
| 89 | adapter_insert(a, 300, id3); | ||
| 90 | |||
| 91 | ASSERT(a->count == 3, "count is 3 after 3 inserts"); | ||
| 92 | ASSERT(a->items[0].created_at == 100, "item 0 created_at"); | ||
| 93 | ASSERT(a->items[1].created_at == 200, "item 1 created_at"); | ||
| 94 | ASSERT(a->items[2].created_at == 300, "item 2 created_at"); | ||
| 95 | ASSERT(memcmp(a->items[0].id, id1, 32) == 0, "item 0 id"); | ||
| 96 | ASSERT(memcmp(a->items[1].id, id2, 32) == 0, "item 1 id"); | ||
| 97 | ASSERT(memcmp(a->items[2].id, id3, 32) == 0, "item 2 id"); | ||
| 98 | |||
| 99 | negentropy_adapter_destroy(a); | ||
| 100 | return 0; | ||
| 101 | } | ||
| 102 | |||
| 103 | static int test_adapter_grow(void) { | ||
| 104 | negentropy_adapter_t *a = negentropy_adapter_from_storage((void*)0x1); | ||
| 105 | uint8_t id[32]; | ||
| 106 | for (int i = 0; i < 100; i++) { | ||
| 107 | memset(id, i, 32); | ||
| 108 | int rc = adapter_insert(a, i, id); | ||
| 109 | ASSERT(rc == 0, "insert succeeds"); | ||
| 110 | } | ||
| 111 | ASSERT(a->count == 100, "count is 100"); | ||
| 112 | ASSERT(a->capacity >= 100, "capacity >= 100"); | ||
| 113 | negentropy_adapter_destroy(a); | ||
| 114 | return 0; | ||
| 115 | } | ||
| 116 | |||
| 117 | static int test_adapter_destroy_null(void) { | ||
| 118 | negentropy_adapter_destroy(NULL); | ||
| 119 | ASSERT(1, "destroy NULL does not crash"); | ||
| 120 | return 0; | ||
| 121 | } | ||
| 122 | |||
| 123 | int main(void) { | ||
| 124 | int failed = 0; | ||
| 125 | failed += test_adapter_create(); | ||
| 126 | failed += test_adapter_null_storage(); | ||
| 127 | failed += test_adapter_insert(); | ||
| 128 | failed += test_adapter_insert_multiple(); | ||
| 129 | failed += test_adapter_grow(); | ||
| 130 | failed += test_adapter_destroy_null(); | ||
| 131 | |||
| 132 | if (failed == 0) { | ||
| 133 | printf("\n=== ALL NEGENTROPY ADAPTER TESTS PASSED ===\n"); | ||
| 134 | } | ||
| 135 | return failed; | ||
| 136 | } | ||