diff options
Diffstat (limited to 'tests/unit')
| -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 |
3 files changed, 271 insertions, 1 deletions
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 | } | ||