diff options
Diffstat (limited to 'tests/unit/test_nostr_event.c')
| -rw-r--r-- | tests/unit/test_nostr_event.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/unit/test_nostr_event.c b/tests/unit/test_nostr_event.c new file mode 100644 index 0000000..12bdb93 --- /dev/null +++ b/tests/unit/test_nostr_event.c | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../main/nostr_event.h" | ||
| 3 | #include "../../main/identity.h" | ||
| 4 | #include <string.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | |||
| 7 | static const char *TEST_NSEC = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"; | ||
| 8 | |||
| 9 | static int time_override = 1700000000; | ||
| 10 | |||
| 11 | int main(void) | ||
| 12 | { | ||
| 13 | printf("=== test_nostr_event ===\n"); | ||
| 14 | |||
| 15 | identity_init(TEST_NSEC); | ||
| 16 | const tollgate_identity_t *id = identity_get(); | ||
| 17 | |||
| 18 | printf("\n--- Event ID computation (NIP-01) ---\n"); | ||
| 19 | nostr_event_t event; | ||
| 20 | esp_err_t ret = nostr_event_init(&event, id->npub_hex, 1, "[]", "Hello TollGate"); | ||
| 21 | ASSERT_EQ_INT(ESP_OK, ret, "nostr_event_init succeeds"); | ||
| 22 | |||
| 23 | ASSERT_EQ_STR(id->npub_hex, event.pubkey, "Event pubkey matches npub"); | ||
| 24 | ASSERT_EQ_INT(1, event.kind, "Event kind is 1"); | ||
| 25 | ASSERT_EQ_INT(64, (int)strlen(event.id), "Event ID is 64 hex chars"); | ||
| 26 | |||
| 27 | printf("\n--- Schnorr signing ---\n"); | ||
| 28 | ret = nostr_event_sign(&event, id->nsec); | ||
| 29 | ASSERT_EQ_INT(ESP_OK, ret, "nostr_event_sign succeeds"); | ||
| 30 | ASSERT_EQ_INT(128, (int)strlen(event.sig), "Signature is 128 hex chars"); | ||
| 31 | ASSERT(event.sig[0] != '\0', "Signature is not empty"); | ||
| 32 | |||
| 33 | printf("\n--- JSON serialization ---\n"); | ||
| 34 | char json_buf[2048]; | ||
| 35 | ret = nostr_event_to_json(&event, json_buf, sizeof(json_buf)); | ||
| 36 | ASSERT_EQ_INT(ESP_OK, ret, "nostr_event_to_json succeeds"); | ||
| 37 | |||
| 38 | ASSERT(strstr(json_buf, "\"id\"") != NULL, "JSON has 'id' field"); | ||
| 39 | ASSERT(strstr(json_buf, "\"pubkey\"") != NULL, "JSON has 'pubkey' field"); | ||
| 40 | ASSERT(strstr(json_buf, "\"created_at\"") != NULL, "JSON has 'created_at' field"); | ||
| 41 | ASSERT(strstr(json_buf, "\"kind\"") != NULL, "JSON has 'kind' field"); | ||
| 42 | ASSERT(strstr(json_buf, "\"tags\"") != NULL, "JSON has 'tags' field"); | ||
| 43 | ASSERT(strstr(json_buf, "\"content\"") != NULL, "JSON has 'content' field"); | ||
| 44 | ASSERT(strstr(json_buf, "\"sig\"") != NULL, "JSON has 'sig' field"); | ||
| 45 | ASSERT(strstr(json_buf, "Hello TollGate") != NULL, "JSON contains content"); | ||
| 46 | |||
| 47 | printf("\n--- Buffer too small ---\n"); | ||
| 48 | char tiny_buf[10]; | ||
| 49 | ret = nostr_event_to_json(&event, tiny_buf, sizeof(tiny_buf)); | ||
| 50 | ASSERT(ret != ESP_OK, "Returns error when buffer too small"); | ||
| 51 | |||
| 52 | printf("\n--- Kind 38787 event (wifistr) ---\n"); | ||
| 53 | nostr_event_t ws_event; | ||
| 54 | const char *ws_tags = "[[\"d\",\"test-npub\"],[\"ssid\",\"TollGate-TEST\"],[\"g\",\"u281w0dfz\"]]"; | ||
| 55 | ret = nostr_event_init(&ws_event, id->npub_hex, 38787, ws_tags, | ||
| 56 | "TollGate WiFi hotspot: TollGate-TEST"); | ||
| 57 | ASSERT_EQ_INT(ESP_OK, ret, "Kind 38787 init succeeds"); | ||
| 58 | ASSERT_EQ_INT(38787, ws_event.kind, "Event kind is 38787"); | ||
| 59 | ASSERT_EQ_INT(64, (int)strlen(ws_event.id), "Kind 38787 event has valid ID"); | ||
| 60 | |||
| 61 | ret = nostr_event_sign(&ws_event, id->nsec); | ||
| 62 | ASSERT_EQ_INT(ESP_OK, ret, "Kind 38787 signing succeeds"); | ||
| 63 | ASSERT_EQ_INT(128, (int)strlen(ws_event.sig), "Kind 38787 signature is 128 hex chars"); | ||
| 64 | |||
| 65 | printf("\n--- Determinism: same input → same ID ---\n"); | ||
| 66 | nostr_event_t event2; | ||
| 67 | nostr_event_init(&event2, id->npub_hex, 1, "[]", "Hello TollGate"); | ||
| 68 | ASSERT(strcmp(event.id, event2.id) == 0 || event.created_at != event2.created_at, | ||
| 69 | "Same input produces same ID (if timestamp matches) or differs only by time"); | ||
| 70 | |||
| 71 | TEST_SUMMARY(); | ||
| 72 | } | ||