upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/unit/test_sub_manager.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/test_sub_manager.c')
-rw-r--r--tests/unit/test_sub_manager.c162
1 files changed, 162 insertions, 0 deletions
diff --git a/tests/unit/test_sub_manager.c b/tests/unit/test_sub_manager.c
new file mode 100644
index 0000000..1a39a67
--- /dev/null
+++ b/tests/unit/test_sub_manager.c
@@ -0,0 +1,162 @@
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <stdbool.h>
6
7#define ASSERT(cond, msg) do { if (!(cond)) { printf("FAIL: %s (%s:%d)\n", msg, __FILE__, __LINE__); return 1; } } while(0)
8
9static bool hex_prefix_match(const char *prefix, size_t prefix_len,
10 const char *full, size_t full_len)
11{
12 if (prefix_len == 0) return true;
13 if (prefix_len > full_len) return false;
14 return memcmp(prefix, full, prefix_len) == 0;
15}
16
17typedef struct {
18 char *authors[4];
19 size_t authors_count;
20 int32_t kinds[4];
21 size_t kinds_count;
22 int64_t since;
23 int64_t until;
24} test_filter_t;
25
26static bool filter_matches_event(const test_filter_t *f, int event_kind,
27 const char *pubkey_hex, uint64_t created_at)
28{
29 if (f->kinds_count > 0) {
30 bool found = false;
31 for (size_t i = 0; i < f->kinds_count; i++) {
32 if (f->kinds[i] == event_kind) { found = true; break; }
33 }
34 if (!found) return false;
35 }
36 if (f->authors_count > 0) {
37 bool found = false;
38 for (size_t i = 0; i < f->authors_count; i++) {
39 if (hex_prefix_match(f->authors[i], strlen(f->authors[i]),
40 pubkey_hex, strlen(pubkey_hex))) {
41 found = true; break;
42 }
43 }
44 if (!found) return false;
45 }
46 if (f->since > 0 && (int64_t)created_at < f->since) return false;
47 if (f->until > 0 && (int64_t)created_at > f->until) return false;
48 return true;
49}
50
51int main(void)
52{
53 int passed = 0;
54
55 printf("--- hex_prefix_match empty prefix ---\n");
56 {
57 ASSERT(hex_prefix_match("", 0, "abcdef", 6) == true, "empty prefix matches");
58 passed++;
59 }
60
61 printf("--- hex_prefix_match exact match ---\n");
62 {
63 ASSERT(hex_prefix_match("abc", 3, "abcdef", 6) == true, "prefix match");
64 passed++;
65 }
66
67 printf("--- hex_prefix_match no match ---\n");
68 {
69 ASSERT(hex_prefix_match("xyz", 3, "abcdef", 6) == false, "no match");
70 passed++;
71 }
72
73 printf("--- hex_prefix_match prefix longer than full ---\n");
74 {
75 ASSERT(hex_prefix_match("abcdefgh", 8, "abc", 3) == false, "prefix too long");
76 passed++;
77 }
78
79 printf("--- hex_prefix_match exact full ---\n");
80 {
81 ASSERT(hex_prefix_match("abcdef", 6, "abcdef", 6) == true, "exact match");
82 passed++;
83 }
84
85 printf("--- filter_matches_event empty filter ---\n");
86 {
87 test_filter_t f = {0};
88 ASSERT(filter_matches_event(&f, 1, "abc", 100) == true, "empty filter matches all");
89 passed++;
90 }
91
92 printf("--- filter_matches_event kind match ---\n");
93 {
94 test_filter_t f = {0};
95 f.kinds[0] = 1;
96 f.kinds_count = 1;
97 ASSERT(filter_matches_event(&f, 1, "abc", 100) == true, "kind 1 matches");
98 ASSERT(filter_matches_event(&f, 2, "abc", 100) == false, "kind 2 no match");
99 passed += 2;
100 }
101
102 printf("--- filter_matches_event multiple kinds ---\n");
103 {
104 test_filter_t f = {0};
105 f.kinds[0] = 1;
106 f.kinds[1] = 5;
107 f.kinds_count = 2;
108 ASSERT(filter_matches_event(&f, 5, "abc", 100) == true, "kind 5 in list");
109 ASSERT(filter_matches_event(&f, 3, "abc", 100) == false, "kind 3 not in list");
110 passed += 2;
111 }
112
113 printf("--- filter_matches_event author match ---\n");
114 {
115 test_filter_t f = {0};
116 f.authors[0] = "abcd";
117 f.authors_count = 1;
118 ASSERT(filter_matches_event(&f, 1, "abcdef", 100) == true, "author prefix match");
119 ASSERT(filter_matches_event(&f, 1, "xyz123", 100) == false, "author no match");
120 passed += 2;
121 }
122
123 printf("--- filter_matches_event since ---\n");
124 {
125 test_filter_t f = {0};
126 f.since = 50;
127 ASSERT(filter_matches_event(&f, 1, "abc", 100) == true, "created_at >= since");
128 ASSERT(filter_matches_event(&f, 1, "abc", 49) == false, "created_at < since");
129 ASSERT(filter_matches_event(&f, 1, "abc", 50) == true, "created_at == since");
130 passed += 3;
131 }
132
133 printf("--- filter_matches_event until ---\n");
134 {
135 test_filter_t f = {0};
136 f.until = 100;
137 ASSERT(filter_matches_event(&f, 1, "abc", 99) == true, "created_at <= until");
138 ASSERT(filter_matches_event(&f, 1, "abc", 101) == false, "created_at > until");
139 ASSERT(filter_matches_event(&f, 1, "abc", 100) == true, "created_at == until");
140 passed += 3;
141 }
142
143 printf("--- filter_matches_event combined ---\n");
144 {
145 test_filter_t f = {0};
146 f.kinds[0] = 1;
147 f.kinds_count = 1;
148 f.since = 50;
149 f.until = 150;
150 f.authors[0] = "abc";
151 f.authors_count = 1;
152 ASSERT(filter_matches_event(&f, 1, "abcdef", 100) == true, "all match");
153 ASSERT(filter_matches_event(&f, 2, "abcdef", 100) == false, "wrong kind");
154 ASSERT(filter_matches_event(&f, 1, "xyz", 100) == false, "wrong author");
155 ASSERT(filter_matches_event(&f, 1, "abcdef", 40) == false, "before since");
156 ASSERT(filter_matches_event(&f, 1, "abcdef", 160) == false, "after until");
157 passed += 5;
158 }
159
160 printf("\n=== Results: %d passed, 0 failed ===\n", passed);
161 return 0;
162}