-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.c
1793 lines (1435 loc) · 56.6 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "nostrdb.h"
#include "hex.h"
#include "io.h"
#include "bolt11/bolt11.h"
#include "invoice.h"
#include "block.h"
#include "protected_queue.h"
#include "memchr.h"
#include "print_util.h"
#include "bindings/c/profile_reader.h"
#include "bindings/c/profile_verifier.h"
#include "bindings/c/meta_reader.h"
#include "bindings/c/meta_verifier.h"
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
int ndb_print_kind_keys(struct ndb_txn *txn);
static const char *test_dir = "./testdata/db";
static NdbProfile_table_t lookup_profile(struct ndb_txn *txn, uint64_t pk)
{
void *root;
size_t len;
assert((root = ndb_get_profile_by_key(txn, pk, &len)));
assert(root);
NdbProfileRecord_table_t profile_record = NdbProfileRecord_as_root(root);
NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record);
return profile;
}
static void print_search(struct ndb_txn *txn, struct ndb_search *search)
{
NdbProfile_table_t profile = lookup_profile(txn, search->profile_key);
const char *name = NdbProfile_name_get(profile);
const char *display_name = NdbProfile_display_name_get(profile);
printf("searched_name name:'%s' display_name:'%s' pk:%" PRIu64 " ts:%" PRIu64 " id:", name, display_name, search->profile_key, search->key->timestamp);
print_hex(search->key->id, 32);
printf("\n");
}
static void test_filters()
{
struct ndb_filter filter, *f;
struct ndb_filter_elements *current;
struct ndb_note *note;
unsigned char buffer[4096];
const char *test_note = "{\"id\": \"160e76ca67405d7ce9ef7d2dd72f3f36401c8661a73d45498af842d40b01b736\",\"pubkey\": \"67c67870aebc327eb2a2e765e6dbb42f0f120d2c4e4e28dc16b824cf72a5acc1\",\"created_at\": 1700688516,\"kind\": 1337,\"tags\": [[\"t\",\"hashtag\"],[\"t\",\"grownostr\"],[\"p\",\"4d2e7a6a8e08007ace5a03391d21735f45caf1bf3d67b492adc28967ab46525e\"]],\"content\": \"\",\"sig\": \"20c2d070261ed269559ada40ca5ac395c389681ee3b5f7d50de19dd9b328dd70cf27d9d13875e87c968d9b49fa05f66e90f18037be4529b9e582c7e2afac3f06\"}";
f = &filter;
assert(ndb_note_from_json(test_note, strlen(test_note), ¬e, buffer, sizeof(buffer)));
assert(ndb_filter_init(f));
assert(ndb_filter_start_field(f, NDB_FILTER_KINDS));
assert(ndb_filter_add_int_element(f, 1337));
assert(ndb_filter_add_int_element(f, 2));
current = ndb_filter_current_element(f);
assert(current->count == 2);
assert(current->field.type == NDB_FILTER_KINDS);
// can't start if we've already started
assert(ndb_filter_start_field(f, NDB_FILTER_KINDS) == 0);
assert(ndb_filter_start_field(f, NDB_FILTER_TAGS) == 0);
ndb_filter_end_field(f);
ndb_filter_end(f);
// should be sorted after end
assert(current->elements[0] == 2);
assert(current->elements[1] == 1337);
// try matching the filter
assert(ndb_filter_matches(f, note));
_ndb_note_set_kind(note, 1);
// inverse match
assert(!ndb_filter_matches(f, note));
// should also match 2
_ndb_note_set_kind(note, 2);
assert(ndb_filter_matches(f, note));
// don't free, just reset data pointers
ndb_filter_destroy(f);
ndb_filter_init(f);
// now try generic matches
assert(ndb_filter_start_tag_field(f, 't'));
assert(ndb_filter_add_str_element(f, "grownostr"));
ndb_filter_end_field(f);
assert(ndb_filter_start_field(f, NDB_FILTER_KINDS));
assert(ndb_filter_add_int_element(f, 3));
ndb_filter_end_field(f);
ndb_filter_end(f);
// shouldn't match the kind filter
assert(!ndb_filter_matches(f, note));
_ndb_note_set_kind(note, 3);
// now it should
assert(ndb_filter_matches(f, note));
ndb_filter_destroy(f);
ndb_filter_init(f);
assert(ndb_filter_start_field(f, NDB_FILTER_AUTHORS));
assert(ndb_filter_add_id_element(f, ndb_note_pubkey(note)));
ndb_filter_end_field(f);
ndb_filter_end(f);
assert(f->current == -1);
assert(ndb_filter_matches(f, note));
ndb_filter_destroy(f);
}
static void test_filter_json()
{
struct ndb_filter filter, *f = &filter;
char buf[1024];
unsigned char pk[32] = { 0x32, 0xe1, 0x82, 0x76, 0x35, 0x45, 0x0e, 0xbb, 0x3c, 0x5a, 0x7d, 0x12, 0xc1, 0xf8, 0xe7, 0xb2, 0xb5, 0x14, 0x43, 0x9a, 0xc1, 0x0a, 0x67, 0xee, 0xf3, 0xd9, 0xfd, 0x9c, 0x5c, 0x68, 0xe2, 0x45 };
unsigned char pk2[32] = {
0xd1, 0x2c, 0x17, 0xbd, 0xe3, 0x09, 0x4a, 0xd3, 0x2f, 0x4a, 0xb8, 0x62, 0xa6, 0xcc, 0x6f, 0x5c, 0x28, 0x9c, 0xfe, 0x7d, 0x58, 0x02, 0x27, 0x0b, 0xdf, 0x34, 0x90, 0x4d, 0xf5, 0x85, 0xf3, 0x49
};
ndb_filter_init(f);
assert(ndb_filter_start_field(f, NDB_FILTER_UNTIL));
assert(ndb_filter_add_int_element(f, 42));
ndb_filter_end_field(f);
ndb_filter_end(f);
assert(ndb_filter_json(f, buf, sizeof(buf)));
assert(!strcmp("{\"until\":42}", buf));
ndb_filter_destroy(f);
ndb_filter_init(f);
assert(ndb_filter_start_field(f, NDB_FILTER_UNTIL));
assert(ndb_filter_add_int_element(f, 42));
ndb_filter_end_field(f);
assert(ndb_filter_start_field(f, NDB_FILTER_KINDS));
assert(ndb_filter_add_int_element(f, 1));
assert(ndb_filter_add_int_element(f, 2));
ndb_filter_end_field(f);
ndb_filter_end(f);
assert(ndb_filter_json(f, buf, sizeof(buf)));
assert(!strcmp("{\"until\":42,\"kinds\":[1,2]}", buf));
ndb_filter_destroy(f);
ndb_filter_init(f);
assert(ndb_filter_start_field(f, NDB_FILTER_IDS));
assert(ndb_filter_add_id_element(f, pk));
assert(ndb_filter_add_id_element(f, pk2));
ndb_filter_end_field(f);
ndb_filter_end(f);
assert(ndb_filter_json(f, buf, sizeof(buf)));
assert(!strcmp("{\"ids\":[\"32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245\",\"d12c17bde3094ad32f4ab862a6cc6f5c289cfe7d5802270bdf34904df585f349\"]}", buf));
ndb_filter_destroy(f);
}
static void test_invoice_encoding(const char *bolt11_str)
{
unsigned char buf[4096];
char *fail = NULL;
struct cursor cur;
struct ndb_invoice invoice;
struct bolt11 *bolt11;
bolt11 = bolt11_decode_minimal(NULL, bolt11_str, &fail);
make_cursor(buf, buf + sizeof(buf), &cur);
assert(fail == NULL);
assert(ndb_encode_invoice(&cur, bolt11));
cur.p = cur.start;
assert(ndb_decode_invoice(&cur, &invoice));
assert(bolt11->msat->millisatoshis == invoice.amount);
assert(bolt11->timestamp == invoice.timestamp);
assert(bolt11->expiry == invoice.expiry);
if (bolt11->description != NULL && invoice.description != NULL)
assert(!strcmp(bolt11->description, invoice.description));
else if (bolt11->description_hash != NULL && invoice.description_hash != NULL)
assert(!memcmp(bolt11->description_hash->u.u8, invoice.description_hash, 32));
else
assert(0);
tal_free(bolt11);
}
static void test_encode_decode_invoice()
{
const char *deschash = "lnbc12n1pjctuljsp57l6za0xry37prkrz7vuv4324ljnssm8ukr2vrf6qvvrgclsmpyhspp5xqfuk89duzjlt2yg56ym7p3enrfxxltyfpc364qc8nsu3kznkl8shp5eugmd894yph7wq68u09gke5x2hmn7mg3zrwd06fs57gmcrjm0uxsxqyjw5qcqpjrzjqd7yw3w4kvhx8uvcj7qusfw4uqre3j56zjz9t07nd2u55yuya3awsrqdlcqqdzcqqqqqqqqqqqqqqzqqyg9qxpqysgqwm2tsc448ellvf5xem2c95hfvc07lakph9r8hffh704uxqhs22r9s4ly0jel48zv6f7fy8zjkgmjt5h2l4jc9gyj4av42s40qvve2ysqwuega8";
const char *desc = "lnbc12u1pjctuklsp5lg8wdhq2g5xfphkqd5k6gf0femt06wfevu94uuqfprc4ggyqma7spp54lmpmz0mhv3lczepdckr0acf3gdany2654u4k2s8fp5xh0yanjhsdq5w3jhxapdd9h8vmmfvdjsxqyjw5qcqpjrzjqgtsq68q0s9wdadpg32gcfu7hslgkhdpaysj2ha3dtnm8882wa6jyzahpqqqpsgqqyqqqqlgqqqqqpsq9q9qxpqysgqdqzhl8gz46nmalhg27stl25z2u7mqtclv3zz223mjwut90m24fa46xqprjewsqys78j2uljfznz5vtefctu6fw7375ee66e62tj965gpcs85tc";
test_invoice_encoding(deschash);
test_invoice_encoding(desc);
}
// Test the created_at query plan via a contact-list query
static void test_timeline_query()
{
struct ndb *ndb;
struct ndb_filter filter;
struct ndb_config config;
struct ndb_txn txn;
struct ndb_query_result results[10];
int count;
ndb_default_config(&config);
assert(ndb_init(&ndb, test_dir, &config));
ndb_filter_init(&filter);
ndb_filter_start_field(&filter, NDB_FILTER_AUTHORS);
#include "testdata/author-filter.c"
ndb_filter_end_field(&filter);
ndb_filter_end(&filter);
ndb_begin_query(ndb, &txn);
assert(ndb_query(&txn, &filter, 1, results,
sizeof(results)/sizeof(results[0]), &count));
ndb_end_query(&txn);
assert(count == 10);
}
// Test fetched_at profile records. These are saved when new profiles are
// processed, or the last time we've fetched the profile.
static void test_fetched_at()
{
struct ndb *ndb;
struct ndb_txn txn;
uint64_t fetched_at, t1, t2;
struct ndb_config config;
ndb_default_config(&config);
assert(ndb_init(&ndb, test_dir, &config));
const unsigned char pubkey[] = { 0x87, 0xfb, 0xc6, 0xd5, 0x98, 0x31, 0xa8, 0x23, 0xa4, 0x5d, 0x10, 0x1f,
0x86, 0x94, 0x2c, 0x41, 0xcd, 0xe2, 0x90, 0x23, 0xf4, 0x09, 0x20, 0x24,
0xa2, 0x7c, 0x50, 0x10, 0x3c, 0x15, 0x40, 0x01 };
const char profile_1[] = "[\"EVENT\",{\"id\": \"a44eb8fb6931d6155b04038bef0624407e46c85c61e5758392cbb615f00184ca\",\"pubkey\": \"87fbc6d59831a823a45d101f86942c41cde29023f4092024a27c50103c154001\",\"created_at\": 1695593354,\"kind\": 0,\"tags\": [],\"content\": \"{\\\"name\\\":\\\"b\\\"}\",\"sig\": \"7540bbde4b4479275e20d95acaa64027359a73989927f878825093cba2f468bd8e195919a77b4c230acecddf92e6b4bee26918b0c0842f84ec7c1fae82453906\"}]";
t1 = time(NULL);
// process the first event, this should set the fetched_at
assert(ndb_process_client_event(ndb, profile_1, sizeof(profile_1)));
// we sleep for a second because we want to make sure the fetched_at is not
// updated for the next record, which is an older profile.
sleep(1);
assert(ndb_begin_query(ndb, &txn));
// this should be set to t1
fetched_at = ndb_read_last_profile_fetch(&txn, pubkey);
if (fetched_at != t1) {
printf("fetched_at != t1? %" PRIu64 " != %" PRIu64 "\n", fetched_at, t1);
}
assert(fetched_at == t1);
t2 = time(NULL);
assert(t1 != t2); // sanity
const char profile_2[] = "[\"EVENT\",{\"id\": \"9b2861dda8fc602ec2753f92f1a443c9565de606e0c8f4fd2db4f2506a3b13ca\",\"pubkey\": \"87fbc6d59831a823a45d101f86942c41cde29023f4092024a27c50103c154001\",\"created_at\": 1695593347,\"kind\": 0,\"tags\": [],\"content\": \"{\\\"name\\\":\\\"a\\\"}\",\"sig\": \"f48da228f8967d33c3caf0a78f853b5144631eb86c7777fd25949123a5272a92765a0963d4686dd0efe05b7a9b986bfac8d43070b234153acbae5006d5a90f31\"}]";
t2 = time(NULL);
// process the second event, since this is older it should not change
// fetched_at
assert(ndb_process_client_event(ndb, profile_2, sizeof(profile_2)));
// we sleep for a second because we want to make sure the fetched_at is not
// updated for the next record, which is an older profile.
sleep(1);
fetched_at = ndb_read_last_profile_fetch(&txn, pubkey);
assert(fetched_at == t1);
}
static void test_reaction_counter()
{
static const int alloc_size = 1024 * 1024;
char *json = malloc(alloc_size);
struct ndb *ndb;
size_t len;
void *root;
int written, reactions, results;
NdbEventMeta_table_t meta;
struct ndb_txn txn;
struct ndb_config config;
ndb_default_config(&config);
static const int num_reactions = 3;
uint64_t note_ids[num_reactions], subid;
assert(ndb_init(&ndb, test_dir, &config));
read_file("testdata/reactions.json", (unsigned char*)json, alloc_size, &written);
assert((subid = ndb_subscribe(ndb, NULL, 0)));
assert(ndb_process_client_events(ndb, json, written));
for (reactions = 0; reactions < num_reactions;) {
results = ndb_wait_for_notes(ndb, subid, note_ids, num_reactions);
reactions += results;
fprintf(stderr, "got %d notes, total %d\n", results, reactions);
assert(reactions > 0);
}
assert(ndb_begin_query(ndb, &txn));
const unsigned char id[32] = {
0x1a, 0x41, 0x56, 0x30, 0x31, 0x09, 0xbb, 0x4a, 0x66, 0x0a, 0x6a, 0x90,
0x04, 0xb0, 0xcd, 0xce, 0x8d, 0x83, 0xc3, 0x99, 0x1d, 0xe7, 0x86, 0x4f,
0x18, 0x76, 0xeb, 0x0f, 0x62, 0x2c, 0x68, 0xe8
};
assert((root = ndb_get_note_meta(&txn, id, &len)));
assert(0 == NdbEventMeta_verify_as_root(root, len));
assert((meta = NdbEventMeta_as_root(root)));
reactions = NdbEventMeta_reactions_get(meta);
//printf("counted reactions: %d\n", reactions);
assert(reactions == 2);
ndb_end_query(&txn);
ndb_destroy(ndb);
}
static void test_profile_search(struct ndb *ndb)
{
struct ndb_txn txn;
struct ndb_search search;
int i;
const char *name;
NdbProfile_table_t profile;
assert(ndb_begin_query(ndb, &txn));
assert(ndb_search_profile(&txn, &search, "jean"));
//print_search(&txn, &search);
profile = lookup_profile(&txn, search.profile_key);
name = NdbProfile_name_get(profile);
assert(!strncmp(name, "jean", 4));
assert(ndb_search_profile_next(&search));
//print_search(&txn, &search);
profile = lookup_profile(&txn, search.profile_key);
name = NdbProfile_name_get(profile);
//assert(strncmp(name, "jean", 4));
for (i = 0; i < 3; i++) {
ndb_search_profile_next(&search);
//print_search(&txn, &search);
}
//assert(!strcmp(name, "jb55"));
ndb_search_profile_end(&search);
ndb_end_query(&txn);
}
static void test_profile_updates()
{
static const int alloc_size = 1024 * 1024;
static const int num_notes = 3;
char *json;
int written, i;
size_t len;
struct ndb *ndb;
struct ndb_config config;
struct ndb_txn txn;
uint64_t key, subid;
uint64_t note_ids[num_notes];
void *record;
json = malloc(alloc_size);
ndb_default_config(&config);
assert(ndb_init(&ndb, test_dir, &config));
subid = ndb_subscribe(ndb, NULL, 0);
ndb_debug("testing profile updates\n");
read_file("testdata/profile-updates.json", (unsigned char*)json, alloc_size, &written);
assert(ndb_process_client_events(ndb, json, written));
for (i = 0; i < num_notes;)
i += ndb_wait_for_notes(ndb, subid, note_ids, num_notes);
assert(ndb_begin_query(ndb, &txn));
const unsigned char pk[32] = {
0x1c, 0x55, 0x46, 0xe4, 0xf5, 0x93, 0x3b, 0xbe, 0x86, 0x66,
0x2a, 0x8e, 0xc3, 0x28, 0x9a, 0x29, 0x87, 0xc0, 0x5d, 0xab,
0x25, 0x6c, 0x06, 0x8b, 0x77, 0x42, 0x9f, 0x0f, 0x08, 0xa7,
0xa0, 0x90
};
record = ndb_get_profile_by_pubkey(&txn, pk, &len, &key);
assert(record);
int res = NdbProfileRecord_verify_as_root(record, len);
assert(res == 0);
NdbProfileRecord_table_t profile_record = NdbProfileRecord_as_root(record);
NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record);
const char *name = NdbProfile_name_get(profile);
assert(!strcmp(name, "c"));
ndb_destroy(ndb);
}
static void test_load_profiles()
{
static const int alloc_size = 1024 * 1024;
char *json = malloc(alloc_size);
struct ndb *ndb;
int written;
struct ndb_config config;
ndb_default_config(&config);
assert(ndb_init(&ndb, test_dir, &config));
read_file("testdata/profiles.json", (unsigned char*)json, alloc_size, &written);
assert(ndb_process_events(ndb, json, written));
ndb_destroy(ndb);
assert(ndb_init(&ndb, test_dir, &config));
unsigned char id[32] = {
0x22, 0x05, 0x0b, 0x6d, 0x97, 0xbb, 0x9d, 0xa0, 0x9e, 0x90, 0xed, 0x0c,
0x6d, 0xd9, 0x5e, 0xed, 0x1d, 0x42, 0x3e, 0x27, 0xd5, 0xcb, 0xa5, 0x94,
0xd2, 0xb4, 0xd1, 0x3a, 0x55, 0x43, 0x09, 0x07 };
const char *expected_content = "{\"website\":\"selenejin.com\",\"lud06\":\"\",\"nip05\":\"[email protected]\",\"picture\":\"https://nostr.build/i/3549697beda0fe1f4ae621f359c639373d92b7c8d5c62582b656c5843138c9ed.jpg\",\"display_name\":\"Selene Jin\",\"about\":\"INTJ | Founding Designer @Blockstream\",\"name\":\"SeleneJin\"}";
struct ndb_txn txn;
assert(ndb_begin_query(ndb, &txn));
struct ndb_note *note = ndb_get_note_by_id(&txn, id, NULL, NULL);
assert(note != NULL);
assert(!strcmp(ndb_note_content(note), expected_content));
ndb_end_query(&txn);
test_profile_search(ndb);
ndb_destroy(ndb);
free(json);
}
static void test_fuzz_events() {
struct ndb *ndb;
const char *str = "[\"EVENT\"\"\"{\"content\"\"created_at\":0 \"id\"\"5086a8f76fe1da7fb56a25d1bebbafd70fca62e36a72c6263f900ff49b8f8604\"\"kind\":0 \"pubkey\":9c87f94bcbe2a837adc28d46c34eeaab8fc2e1cdf94fe19d4b99ae6a5e6acedc \"sig\"\"27374975879c94658412469cee6db73d538971d21a7b580726a407329a4cafc677fb56b946994cea59c3d9e118fef27e4e61de9d2c46ac0a65df14153 ea93cf5\"\"tags\"[[][\"\"]]}]";
struct ndb_config config;
ndb_default_config(&config);
ndb_init(&ndb, test_dir, &config);
ndb_process_event(ndb, str, strlen(str));
ndb_destroy(ndb);
}
static void test_migrate() {
static const char *v0_dir = "testdata/db/v0";
struct ndb *ndb;
struct ndb_config config;
struct ndb_txn txn;
ndb_default_config(&config);
ndb_config_set_flags(&config, NDB_FLAG_NOMIGRATE);
fprintf(stderr, "testing migrate on v0\n");
assert(ndb_init(&ndb, v0_dir, &config));
assert(ndb_begin_query(ndb, &txn));
assert(ndb_db_version(&txn) == 0);
assert(ndb_end_query(&txn));
ndb_destroy(ndb);
ndb_config_set_flags(&config, 0);
assert(ndb_init(&ndb, v0_dir, &config));
ndb_destroy(ndb);
assert(ndb_init(&ndb, v0_dir, &config));
assert(ndb_begin_query(ndb, &txn));
assert(ndb_db_version(&txn) == 3);
assert(ndb_end_query(&txn));
test_profile_search(ndb);
ndb_destroy(ndb);
}
static void test_basic_event() {
unsigned char buf[512];
struct ndb_builder builder, *b = &builder;
struct ndb_note *note;
int ok;
unsigned char id[32];
memset(id, 1, 32);
unsigned char pubkey[32];
memset(pubkey, 2, 32);
unsigned char sig[64];
memset(sig, 3, 64);
const char *hex_pk = "5d9b81b2d4d5609c5565286fc3b511dc6b9a1b3d7d1174310c624d61d1f82bb9";
ok = ndb_builder_init(b, buf, sizeof(buf));
assert(ok);
note = builder.note;
//memset(note->padding, 3, sizeof(note->padding));
ok = ndb_builder_set_content(b, hex_pk, strlen(hex_pk)); assert(ok);
ndb_builder_set_id(b, id); assert(ok);
ndb_builder_set_pubkey(b, pubkey); assert(ok);
ndb_builder_set_sig(b, sig); assert(ok);
ok = ndb_builder_new_tag(b); assert(ok);
ok = ndb_builder_push_tag_str(b, "p", 1); assert(ok);
ok = ndb_builder_push_tag_str(b, hex_pk, 64); assert(ok);
ok = ndb_builder_new_tag(b); assert(ok);
ok = ndb_builder_push_tag_str(b, "word", 4); assert(ok);
ok = ndb_builder_push_tag_str(b, "words", 5); assert(ok);
ok = ndb_builder_push_tag_str(b, "w", 1); assert(ok);
ok = ndb_builder_finalize(b, ¬e, NULL);
assert(ok);
// content should never be packed id
// TODO: figure out how to test this now that we don't expose it
// assert(note->content.packed.flag != NDB_PACKED_ID);
assert(ndb_tags_count(ndb_note_tags(note)) == 2);
// test iterator
struct ndb_iterator iter, *it = &iter;
ndb_tags_iterate_start(note, it);
ok = ndb_tags_iterate_next(it);
assert(ok);
assert(ndb_tag_count(it->tag) == 2);
const char *p = ndb_iter_tag_str(it, 0).str;
struct ndb_str hpk = ndb_iter_tag_str(it, 1);
hex_decode(hex_pk, 64, id, 32);
assert(hpk.flag == NDB_PACKED_ID);
assert(memcmp(hpk.id, id, 32) == 0);
assert(!strcmp(p, "p"));
ok = ndb_tags_iterate_next(it);
assert(ok);
assert(ndb_tag_count(it->tag) == 3);
assert(!strcmp(ndb_iter_tag_str(it, 0).str, "word"));
assert(!strcmp(ndb_iter_tag_str(it, 1).str, "words"));
assert(!strcmp(ndb_iter_tag_str(it, 2).str, "w"));
ok = ndb_tags_iterate_next(it);
assert(!ok);
}
static void test_empty_tags() {
struct ndb_builder builder, *b = &builder;
struct ndb_iterator iter, *it = &iter;
struct ndb_note *note;
int ok;
unsigned char buf[1024];
ok = ndb_builder_init(b, buf, sizeof(buf));
assert(ok);
ok = ndb_builder_finalize(b, ¬e, NULL);
assert(ok);
assert(ndb_tags_count(ndb_note_tags(note)) == 0);
ndb_tags_iterate_start(note, it);
ok = ndb_tags_iterate_next(it);
assert(!ok);
}
static void print_tag(struct ndb_note *note, struct ndb_tag *tag) {
struct ndb_str str;
int tag_count = ndb_tag_count(tag);
for (int i = 0; i < tag_count; i++) {
str = ndb_tag_str(note, tag, i);
if (str.flag == NDB_PACKED_ID) {
printf("<id> ");
} else {
printf("%s ", str.str);
}
}
printf("\n");
}
static void test_parse_contact_list()
{
int size, written = 0;
unsigned char id[32];
static const int alloc_size = 2 << 18;
unsigned char *json = malloc(alloc_size);
unsigned char *buf = malloc(alloc_size);
struct ndb_note *note;
read_file("testdata/contacts.json", json, alloc_size, &written);
size = ndb_note_from_json((const char*)json, written, ¬e, buf, alloc_size);
printf("ndb_note_from_json size %d\n", size);
assert(size > 0);
assert(size == 34328);
memcpy(id, ndb_note_id(note), 32);
memset(ndb_note_id(note), 0, 32);
assert(ndb_calculate_id(note, json, alloc_size));
assert(!memcmp(ndb_note_id(note), id, 32));
const char* expected_content =
"{\"wss://nos.lol\":{\"write\":true,\"read\":true},"
"\"wss://relay.damus.io\":{\"write\":true,\"read\":true},"
"\"ws://monad.jb55.com:8080\":{\"write\":true,\"read\":true},"
"\"wss://nostr.wine\":{\"write\":true,\"read\":true},"
"\"wss://welcome.nostr.wine\":{\"write\":true,\"read\":true},"
"\"wss://eden.nostr.land\":{\"write\":true,\"read\":true},"
"\"wss://relay.mostr.pub\":{\"write\":true,\"read\":true},"
"\"wss://nostr-pub.wellorder.net\":{\"write\":true,\"read\":true}}";
assert(!strcmp(expected_content, ndb_note_content(note)));
assert(ndb_note_created_at(note) == 1689904312);
assert(ndb_note_kind(note) == 3);
assert(ndb_tags_count(ndb_note_tags(note)) == 786);
//printf("note content length %d\n", ndb_note_content_length(note));
printf("ndb_content_len %d, expected_len %ld\n",
ndb_note_content_length(note),
strlen(expected_content));
assert(ndb_note_content_length(note) == strlen(expected_content));
struct ndb_iterator iter, *it = &iter;
ndb_tags_iterate_start(note, it);
int tags = 0;
int total_elems = 0;
while (ndb_tags_iterate_next(it)) {
total_elems += ndb_tag_count(it->tag);
//printf("tag %d: ", tags);
if (tags == 0 || tags == 1 || tags == 2)
assert(ndb_tag_count(it->tag) == 3);
if (tags == 6)
assert(ndb_tag_count(it->tag) == 2);
if (tags == 7)
assert(!strcmp(ndb_tag_str(note, it->tag, 2).str, "wss://nostr-pub.wellorder.net"));
if (tags == 786) {
static unsigned char h[] = { 0x74, 0xfa, 0xe6, 0x66, 0x4c, 0x9e, 0x79, 0x98, 0x0c, 0x6a, 0xc1, 0x1c, 0x57, 0x75, 0xed, 0x30, 0x93, 0x2b, 0xe9, 0x26, 0xf5, 0xc4, 0x5b, 0xe8, 0xd6, 0x55, 0xe0, 0x0e, 0x35, 0xec, 0xa2, 0x88 };
assert(!memcmp(ndb_tag_str(note, it->tag, 1).id, h, 32));
}
//print_tag(it->note, it->tag);
tags += 1;
}
assert(tags == 786);
//printf("total_elems %d\n", total_elems);
assert(total_elems == 1580);
write_file("test_contacts_ndb_note", (unsigned char *)note, size);
printf("wrote test_contacts_ndb_note (raw ndb_note)\n");
free(json);
free(buf);
}
static void test_replacement()
{
static const int alloc_size = 1024 * 1024;
char *json = malloc(alloc_size);
unsigned char *buf = malloc(alloc_size);
struct ndb *ndb;
size_t len;
int written;
struct ndb_config config;
ndb_default_config(&config);
assert(ndb_init(&ndb, test_dir, &config));
read_file("testdata/old-new.json", (unsigned char*)json, alloc_size, &written);
assert(ndb_process_events(ndb, json, written));
ndb_destroy(ndb);
assert(ndb_init(&ndb, test_dir, &config));
struct ndb_txn txn;
assert(ndb_begin_query(ndb, &txn));
unsigned char pubkey[32] = { 0x1e, 0x48, 0x9f, 0x6a, 0x4f, 0xc5, 0xc7, 0xac, 0x47, 0x5e, 0xa9, 0x04, 0x17, 0x43, 0xb8, 0x53, 0x11, 0x73, 0x25, 0x92, 0x61, 0xec, 0x71, 0x54, 0x26, 0x41, 0x05, 0x1e, 0x22, 0xa3, 0x82, 0xac };
void *root = ndb_get_profile_by_pubkey(&txn, pubkey, &len, NULL);
assert(root);
int res = NdbProfileRecord_verify_as_root(root, len);
assert(res == 0);
NdbProfileRecord_table_t profile_record = NdbProfileRecord_as_root(root);
NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record);
const char *name = NdbProfile_name_get(profile);
assert(!strcmp(name, "jb55"));
ndb_end_query(&txn);
free(json);
free(buf);
}
static void test_fetch_last_noteid()
{
static const int alloc_size = 1024 * 1024;
char *json = malloc(alloc_size);
unsigned char *buf = malloc(alloc_size);
struct ndb *ndb;
size_t len;
int written;
struct ndb_config config;
ndb_default_config(&config);
assert(ndb_init(&ndb, test_dir, &config));
read_file("testdata/random.json", (unsigned char*)json, alloc_size, &written);
assert(ndb_process_events(ndb, json, written));
ndb_destroy(ndb);
assert(ndb_init(&ndb, test_dir, &config));
unsigned char id[32] = { 0xdc, 0x96, 0x4f, 0x4c, 0x89, 0x83, 0x64, 0x13, 0x8e, 0x81, 0x96, 0xf0, 0xc7, 0x33, 0x38, 0xc8, 0xcc, 0x3e, 0xbf, 0xa3, 0xaf, 0xdd, 0xbc, 0x7d, 0xd1, 0x58, 0xb4, 0x84, 0x7c, 0x1e, 0xbf, 0xa0 };
struct ndb_txn txn;
assert(ndb_begin_query(ndb, &txn));
struct ndb_note *note = ndb_get_note_by_id(&txn, id, &len, NULL);
assert(note != NULL);
assert(ndb_note_created_at(note) == 1650054135);
unsigned char pk[32] = { 0x32, 0xe1, 0x82, 0x76, 0x35, 0x45, 0x0e, 0xbb, 0x3c, 0x5a, 0x7d, 0x12, 0xc1, 0xf8, 0xe7, 0xb2, 0xb5, 0x14, 0x43, 0x9a, 0xc1, 0x0a, 0x67, 0xee, 0xf3, 0xd9, 0xfd, 0x9c, 0x5c, 0x68, 0xe2, 0x45 };
unsigned char profile_note_id[32] = {
0xd1, 0x2c, 0x17, 0xbd, 0xe3, 0x09, 0x4a, 0xd3, 0x2f, 0x4a, 0xb8, 0x62, 0xa6, 0xcc, 0x6f, 0x5c, 0x28, 0x9c, 0xfe, 0x7d, 0x58, 0x02, 0x27, 0x0b, 0xdf, 0x34, 0x90, 0x4d, 0xf5, 0x85, 0xf3, 0x49
};
void *root = ndb_get_profile_by_pubkey(&txn, pk, &len, NULL);
assert(root);
int res = NdbProfileRecord_verify_as_root(root, len);
printf("NdbProfileRecord verify result %d\n", res);
assert(res == 0);
NdbProfileRecord_table_t profile_record = NdbProfileRecord_as_root(root);
NdbProfile_table_t profile = NdbProfileRecord_profile_get(profile_record);
const char *lnurl = NdbProfileRecord_lnurl_get(profile_record);
const char *name = NdbProfile_name_get(profile);
uint64_t key = NdbProfileRecord_note_key_get(profile_record);
assert(name);
assert(lnurl);
assert(!strcmp(name, "jb55"));
assert(!strcmp(lnurl, "fixme"));
printf("note_key %" PRIu64 "\n", key);
struct ndb_note *n = ndb_get_note_by_key(&txn, key, NULL);
ndb_end_query(&txn);
assert(memcmp(profile_note_id, ndb_note_id(n), 32) == 0);
//fwrite(profile, len, 1, stdout);
ndb_destroy(ndb);
free(json);
free(buf);
}
static void test_parse_contact_event()
{
int written;
static const int alloc_size = 2 << 18;
char *json = malloc(alloc_size);
unsigned char *buf = malloc(alloc_size);
struct ndb_tce tce;
assert(read_file("testdata/contacts-event.json", (unsigned char*)json,
alloc_size, &written));
assert(ndb_ws_event_from_json(json, written, &tce, buf, alloc_size, NULL));
assert(tce.evtype == NDB_TCE_EVENT);
free(json);
free(buf);
}
static void test_content_len()
{
int written;
static const int alloc_size = 2 << 18;
char *json = malloc(alloc_size);
unsigned char *buf = malloc(alloc_size);
struct ndb_tce tce;
assert(read_file("testdata/failed_size.json", (unsigned char*)json,
alloc_size, &written));
assert(ndb_ws_event_from_json(json, written, &tce, buf, alloc_size, NULL));
assert(tce.evtype == NDB_TCE_EVENT);
assert(ndb_note_content_length(tce.event.note) == 0);
free(json);
free(buf);
}
static void test_parse_filter_json()
{
int i;
unsigned char buffer[1024];
unsigned char *pid;
const char *str;
uint64_t val;
struct ndb_filter_elements *elems;
const unsigned char id_bytes[] = { 0x50, 0x04, 0xa0, 0x81, 0xe3, 0x97,
0xc6, 0xda, 0x9d, 0xc2, 0xf2, 0xd6, 0xb3, 0x13, 0x40, 0x06,
0xa9, 0xd0, 0xe8, 0xc1, 0xb4, 0x66, 0x89, 0xd9, 0xfe, 0x15,
0x0b, 0xb2, 0xf2, 0x1a, 0x20, 0x4d };
const unsigned char id2_bytes[] = { 0xb1, 0x69, 0xf5, 0x96, 0x96, 0x89,
0x17, 0xa1, 0xab, 0xeb, 0x42, 0x34, 0xd3, 0xcf, 0x3a, 0xa9,
0xba, 0xee, 0x21, 0x12, 0xe5, 0x89, 0x98, 0xd1, 0x7c, 0x6d,
0xb4, 0x16, 0xad, 0x33, 0xfe, 0x40 };
#define HEX_ID "5004a081e397c6da9dc2f2d6b3134006a9d0e8c1b46689d9fe150bb2f21a204d"
#define HEX_PK "b169f596968917a1abeb4234d3cf3aa9baee2112e58998d17c6db416ad33fe40"
static const char *json = "{\"ids\": [\"" HEX_ID "\", \"" HEX_PK "\"], \"kinds\": [1,2,3], \"limit\": 10, \"#e\":[\"" HEX_PK "\"], \"#t\": [\"hashtag\"]}";
struct ndb_filter filter, *f = &filter;
ndb_filter_init(f);
assert(ndb_filter_from_json(json, strlen(json), f, buffer, sizeof(buffer)));
assert(filter.finalized);
for (i = 0; i < filter.num_elements; i++) {
elems = ndb_filter_get_elements(f, i);
switch (i) {
case 0:
assert(elems->field.type == NDB_FILTER_IDS);
assert(elems->count == 2);
pid = ndb_filter_get_id_element(f, elems, 0);
assert(!memcmp(pid, id_bytes, 32));
pid = ndb_filter_get_id_element(f, elems, 1);
assert(!memcmp(pid, id2_bytes, 32));
break;
case 1:
assert(elems->field.type == NDB_FILTER_KINDS);
assert(elems->count == 3);
val = ndb_filter_get_int_element(elems, 0);
assert(val == 1);
val = ndb_filter_get_int_element(elems, 1);
assert(val == 2);
val = ndb_filter_get_int_element(elems, 2);
assert(val == 3);
break;
case 2:
assert(elems->field.type == NDB_FILTER_LIMIT);
val = ndb_filter_get_int_element(elems, 0);
assert(val == 10);
break;
case 3:
assert(elems->field.type == NDB_FILTER_TAGS);
assert(elems->field.tag == 'e');
pid = ndb_filter_get_id_element(f, elems, 0);
assert(pid != NULL);
assert(!memcmp(pid, id2_bytes, 32));
break;
case 4:
assert(elems->field.type == NDB_FILTER_TAGS);
assert(elems->field.tag == 't');
str = ndb_filter_get_string_element(f, elems, 0);
assert(!strcmp(str, "hashtag"));
break;
}
}
}
static void test_parse_json() {
char hex_id[32] = {0};
unsigned char buffer[1024];
struct ndb_note *note;
#define HEX_ID "5004a081e397c6da9dc2f2d6b3134006a9d0e8c1b46689d9fe150bb2f21a204d"
#define HEX_PK "b169f596968917a1abeb4234d3cf3aa9baee2112e58998d17c6db416ad33fe40"
static const char *json =
"{\"id\": \"" HEX_ID "\",\"pubkey\": \"" HEX_PK "\",\"created_at\": 1689836342,\"kind\": 1,\"tags\": [[\"p\",\"" HEX_ID "\"], [\"word\", \"words\", \"w\"]],\"content\": \"共通語\",\"sig\": \"e4d528651311d567f461d7be916c37cbf2b4d530e672f29f15f353291ed6df60c665928e67d2f18861c5ca88\"}";
int ok;
ok = ndb_note_from_json(json, strlen(json), ¬e, buffer, sizeof(buffer));
assert(ok);
const char *content = ndb_note_content(note);
unsigned char *id = ndb_note_id(note);
hex_decode(HEX_ID, 64, hex_id, sizeof(hex_id));
assert(!strcmp(content, "共通語"));
assert(!memcmp(id, hex_id, 32));
assert(ndb_tags_count(ndb_note_tags(note)) == 2);
struct ndb_iterator iter, *it = &iter;
ndb_tags_iterate_start(note, it); assert(ok);
ok = ndb_tags_iterate_next(it); assert(ok);
assert(ndb_tag_count(it->tag) == 2);
assert(!strcmp(ndb_iter_tag_str(it, 0).str, "p"));
assert(!memcmp(ndb_iter_tag_str(it, 1).id, hex_id, 32));
ok = ndb_tags_iterate_next(it); assert(ok);
assert(ndb_tag_count(it->tag) == 3);
assert(!strcmp(ndb_iter_tag_str(it, 0).str, "word"));
assert(!strcmp(ndb_iter_tag_str(it, 1).str, "words"));
assert(!strcmp(ndb_iter_tag_str(it, 2).str, "w"));
}
static void test_strings_work_before_finalization() {
struct ndb_builder builder, *b = &builder;
struct ndb_note *note;
int ok;
unsigned char buf[1024];
ok = ndb_builder_init(b, buf, sizeof(buf)); assert(ok);
ndb_builder_set_content(b, "hello", 5);
assert(!strcmp(ndb_note_content(b->note), "hello"));
assert(ndb_builder_finalize(b, ¬e, NULL));
assert(!strcmp(ndb_note_content(note), "hello"));
}
static void test_parse_content() {
}
static void test_parse_nevent() {
unsigned char buf[4096];
const char *content = "nostr:nevent1qqs9qhc0pjvp6jl2w6ppk5cft8ets8fhxy7fcqcjnp7g38whjy0x5aqpzpmhxue69uhkummnw3ezuamfdejsyg86np9a0kajstc8u9h846rmy6320wdepdeydfz8w8cv7kh9sqv02g947d58,#hashtag";
struct ndb_blocks *blocks;
struct ndb_block *block = NULL;
struct nostr_bech32 *bech32;
struct ndb_block_iterator iterator, *iter;
iter = &iterator;
int ok = 0;
static unsigned char event_id[] = { 0x50, 0x5f, 0x0f, 0x0c, 0x98, 0x1d, 0x4b, 0xea, 0x76, 0x82, 0x1b, 0x53,
0x09, 0x59, 0xf2, 0xb8, 0x1d, 0x37, 0x31, 0x3c, 0x9c, 0x03, 0x12, 0x98,
0x7c, 0x88, 0x9d, 0xd7, 0x91, 0x1e, 0x6a, 0x74 };
assert(ndb_parse_content(buf, sizeof(buf), content, strlen(content), &blocks));
ndb_blocks_iterate_start(content, blocks, iter);
assert(blocks->num_blocks == 3);
while ((block = ndb_blocks_iterate_next(iter))) {
switch (++ok) {
case 1:
assert(ndb_get_block_type(block) == BLOCK_MENTION_BECH32);
bech32 = ndb_bech32_block(block);
assert(bech32->type == NOSTR_BECH32_NEVENT);
assert(!memcmp(bech32->nevent.event_id, event_id, 32));
break;
case 2:
assert(ndb_get_block_type(block) == BLOCK_TEXT);
assert(ndb_str_block_ptr(ndb_block_str(block))[0] == ',');
break;
case 3:
assert(ndb_get_block_type(block) == BLOCK_HASHTAG);
assert(!strncmp("hashtag", ndb_str_block_ptr(ndb_block_str(block)), 7));
break;
}
}
assert(ok == 3);
}