diff options
| author | Your Name <you@example.com> | 2026-05-17 01:31:49 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-17 01:31:49 +0530 |
| commit | 347d29658959c7e4b368a15134c183f4ce7a25bc (patch) | |
| tree | 362b3e40273e3c1435bdd0745de61006041bb803 /tests/unit/test_identity.c | |
| parent | 4c47ae188b288e7d24bd9566ab3e6a6805d9484f (diff) | |
Testing infrastructure: AGENTS.md rules + unit test framework + geohash tests (11/11 pass)
- Add AGENTS.md: full project context + mandatory testing rules for AI sessions
- Add tests/unit/ with host-compiled C unit test infrastructure
- Clean stubs approach: ESP-IDF type stubs in tests/unit/stubs/, no source modifications
- Fix geohash.c bit extraction bug (3-byte span) found by unit tests
- test_geohash: 11/11 passing with reference vectors (Munich, NYC, origin, boundaries)
Diffstat (limited to 'tests/unit/test_identity.c')
| -rw-r--r-- | tests/unit/test_identity.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/unit/test_identity.c b/tests/unit/test_identity.c new file mode 100644 index 0000000..cf4028f --- /dev/null +++ b/tests/unit/test_identity.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../main/identity.h" | ||
| 3 | #include <string.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | |||
| 6 | static const char *TEST_NSEC = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"; | ||
| 7 | static const char *TEST_NSEC2 = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"; | ||
| 8 | |||
| 9 | int main(void) | ||
| 10 | { | ||
| 11 | printf("=== test_identity ===\n"); | ||
| 12 | |||
| 13 | printf("\n--- identity_init with valid nsec ---\n"); | ||
| 14 | esp_err_t ret = identity_init(TEST_NSEC); | ||
| 15 | ASSERT_EQ_INT(ESP_OK, ret, "identity_init returns ESP_OK"); | ||
| 16 | |||
| 17 | const tollgate_identity_t *id = identity_get(); | ||
| 18 | ASSERT(id != NULL, "identity_get returns non-NULL"); | ||
| 19 | ASSERT(id->initialized, "identity is marked initialized"); | ||
| 20 | |||
| 21 | printf("\n--- npub derivation ---\n"); | ||
| 22 | ASSERT_EQ_INT(64, (int)strlen(id->npub_hex), "npub is 64 hex chars"); | ||
| 23 | ASSERT(id->npub_hex[0] != '\0', "npub is not empty"); | ||
| 24 | |||
| 25 | printf("\n--- STA MAC derivation ---\n"); | ||
| 26 | uint8_t expected_sta[] = {0xF2, 0x4D, 0x55, 0x33, 0xDC, 0x9C}; | ||
| 27 | ASSERT_MEM_EQ(expected_sta, id->sta_mac, 6, "STA MAC matches golden vector"); | ||
| 28 | ASSERT_EQ_INT(2, id->sta_mac[0] & 0x02, "STA MAC has locally-administered bit set"); | ||
| 29 | ASSERT_EQ_INT(0, id->sta_mac[0] & 0x01, "STA MAC has multicast bit cleared"); | ||
| 30 | |||
| 31 | printf("\n--- AP MAC derivation ---\n"); | ||
| 32 | uint8_t expected_ap[] = {0x3A, 0x2A, 0xEB, 0xC0, 0xE9, 0xCA}; | ||
| 33 | ASSERT_MEM_EQ(expected_ap, id->ap_mac, 6, "AP MAC matches golden vector"); | ||
| 34 | ASSERT_EQ_INT(2, id->ap_mac[0] & 0x02, "AP MAC has locally-administered bit set"); | ||
| 35 | ASSERT_EQ_INT(0, id->ap_mac[0] & 0x01, "AP MAC has multicast bit cleared"); | ||
| 36 | |||
| 37 | printf("\n--- SSID derivation ---\n"); | ||
| 38 | ASSERT_EQ_STR("TollGate-C0E9CA", id->ap_ssid, "SSID derived from AP MAC last 3 bytes"); | ||
| 39 | |||
| 40 | printf("\n--- AP IP derivation ---\n"); | ||
| 41 | ASSERT_EQ_STR("10.192.45.1", id->ap_ip_str, "AP IP derived from AP MAC bytes"); | ||
| 42 | |||
| 43 | printf("\n--- Determinism ---\n"); | ||
| 44 | ret = identity_init(TEST_NSEC); | ||
| 45 | ASSERT_EQ_INT(ESP_OK, ret, "Second init with same nsec succeeds"); | ||
| 46 | const tollgate_identity_t *id2 = identity_get(); | ||
| 47 | ASSERT_MEM_EQ(id->sta_mac, id2->sta_mac, 6, "STA MAC is deterministic"); | ||
| 48 | ASSERT_MEM_EQ(id->ap_mac, id2->ap_mac, 6, "AP MAC is deterministic"); | ||
| 49 | ASSERT_EQ_STR(id->ap_ssid, id2->ap_ssid, "SSID is deterministic"); | ||
| 50 | |||
| 51 | printf("\n--- Different nsec produces different identity ---\n"); | ||
| 52 | ret = identity_init(TEST_NSEC2); | ||
| 53 | ASSERT_EQ_INT(ESP_OK, ret, "Init with different nsec succeeds"); | ||
| 54 | const tollgate_identity_t *id3 = identity_get(); | ||
| 55 | ASSERT(memcmp(id->sta_mac, id3->sta_mac, 6) != 0, "Different nsec produces different STA MAC"); | ||
| 56 | ASSERT(memcmp(id->ap_mac, id3->ap_mac, 6) != 0, "Different nsec produces different AP MAC"); | ||
| 57 | ASSERT(strcmp(id->ap_ssid, id3->ap_ssid) != 0, "Different nsec produces different SSID"); | ||
| 58 | |||
| 59 | printf("\n--- Invalid nsec ---\n"); | ||
| 60 | ret = identity_init(NULL); | ||
| 61 | ASSERT(ret != ESP_OK, "NULL nsec returns error"); | ||
| 62 | ret = identity_init("tooshort"); | ||
| 63 | ASSERT(ret != ESP_OK, "Short nsec returns error"); | ||
| 64 | ret = identity_init("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"); | ||
| 65 | ASSERT(ret != ESP_OK, "Invalid hex nsec returns error"); | ||
| 66 | |||
| 67 | TEST_SUMMARY(); | ||
| 68 | } | ||