nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

commit d5dcaef57ca6f9bc0ab861aeecdb2e9e038816d9
parent f29c38c11b375a20532b3a0be8e93606c9739086
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 27 Dec 2023 14:54:43 -0800

bech32: add some initial tests

since we modified this recently, let's add some tests to make sure
we didn't break anything

Diffstat:
Msrc/nostr_bech32.c | 54+++++++++++++++++++++++++++++++++++-------------------
Msrc/nostr_bech32.h | 8++++++--
Mtest.c | 20++++++++++++++++++++
3 files changed, 61 insertions(+), 21 deletions(-)

diff --git a/src/nostr_bech32.c b/src/nostr_bech32.c @@ -216,31 +216,31 @@ int parse_nostr_bech32_buffer(struct cursor *cur, switch (obj->type) { case NOSTR_BECH32_NOTE: - if (!parse_nostr_bech32_note(cur, &obj->data.note)) + if (!parse_nostr_bech32_note(cur, &obj->note)) return 0; break; case NOSTR_BECH32_NPUB: - if (!parse_nostr_bech32_npub(cur, &obj->data.npub)) + if (!parse_nostr_bech32_npub(cur, &obj->npub)) return 0; break; case NOSTR_BECH32_NSEC: - if (!parse_nostr_bech32_nsec(cur, &obj->data.nsec)) + if (!parse_nostr_bech32_nsec(cur, &obj->nsec)) return 0; break; case NOSTR_BECH32_NEVENT: - if (!parse_nostr_bech32_nevent(cur, &obj->data.nevent)) + if (!parse_nostr_bech32_nevent(cur, &obj->nevent)) return 0; break; case NOSTR_BECH32_NADDR: - if (!parse_nostr_bech32_naddr(cur, &obj->data.naddr)) + if (!parse_nostr_bech32_naddr(cur, &obj->naddr)) return 0; break; case NOSTR_BECH32_NPROFILE: - if (!parse_nostr_bech32_nprofile(cur, &obj->data.nprofile)) + if (!parse_nostr_bech32_nprofile(cur, &obj->nprofile)) return 0; break; case NOSTR_BECH32_NRELAY: - if (!parse_nostr_bech32_nrelay(cur, &obj->data.nrelay)) + if (!parse_nostr_bech32_nrelay(cur, &obj->nrelay)) return 0; break; } @@ -248,36 +248,52 @@ int parse_nostr_bech32_buffer(struct cursor *cur, return 1; } -int parse_nostr_bech32_str(struct cursor *bech32) { - enum nostr_bech32_type type; - - if (!parse_nostr_bech32_type((const char *)bech32->p, &type)) + +int parse_nostr_bech32_str(struct cursor *bech32, enum nostr_bech32_type *type) { + if (!parse_nostr_bech32_type((const char *)bech32->p, type)) return 0; if (!consume_until_non_alphanumeric(bech32, 1)) return 0; return 1; +} + - /* - *parsed_len = bech32->p - start; +int parse_nostr_bech32(unsigned char *buf, int buflen, + const char *bech32_str, size_t bech32_len, + struct nostr_bech32 *obj) { + unsigned char *start; + size_t parsed_len, u5_out_len, u8_out_len; + enum nostr_bech32_type type; + static const int MAX_PREFIX = 8; + struct cursor cur, bech32; + + make_cursor(buf, buf + buflen, &cur); + make_cursor((unsigned char*)bech32_str, (unsigned char*)bech32_str + bech32_len, &bech32); + + start = bech32.p; + if (!parse_nostr_bech32_str(&bech32, &type)) + return 0; + + parsed_len = bech32.p - start; // some random sanity checking - if (*parsed_len < 10 || *parsed_len > 10000) + if (parsed_len < 10 || parsed_len > 10000) return 0; - const char u5[*parsed_len]; + unsigned char u5[parsed_len]; + char prefix[MAX_PREFIX]; if (bech32_decode_len(prefix, u5, &u5_out_len, (const char*)start, - *parsed_len, MAX_PREFIX) == BECH32_ENCODING_NONE) + parsed_len, MAX_PREFIX) == BECH32_ENCODING_NONE) { return 0; } - if (!parse_nostr_bech32_type(prefix, type)) + if (!bech32_convert_bits(cur.p, &u8_out_len, 8, u5, u5_out_len, 5, 0)) return 0; - */ - return 1; + return parse_nostr_bech32_buffer(&cur, type, obj); } diff --git a/src/nostr_bech32.h b/src/nostr_bech32.h @@ -73,16 +73,20 @@ struct nostr_bech32 { struct bech32_nprofile nprofile; struct bech32_naddr naddr; struct bech32_nrelay nrelay; - } data; + }; }; -int parse_nostr_bech32_str(struct cursor *bech32); +int parse_nostr_bech32_str(struct cursor *bech32, enum nostr_bech32_type *type); int parse_nostr_bech32_type(const char *prefix, enum nostr_bech32_type *type); int parse_nostr_bech32_buffer(struct cursor *cur, enum nostr_bech32_type type, struct nostr_bech32 *obj); +int parse_nostr_bech32(unsigned char *buf, int buflen, + const char *bech32_str, size_t bech32_len, + struct nostr_bech32 *obj); + /* int parse_nostr_bech32(const char *bech32, size_t input_len, unsigned char *outbuf, size_t outlen, diff --git a/test.c b/test.c @@ -782,6 +782,25 @@ static void test_parse_content() { assert(blocks->num_blocks == 4); } +static void test_bech32_objects() { + struct nostr_bech32 obj; + unsigned char buf[4096]; + const char *nevent = "nevent1qqstjtqmd3lke9m3ftv49pagzxth4q2va4hy2m6kprl0p4y6es4vvnspz3mhxue69uhhyetvv9ujuerpd46hxtnfduedelhq"; + + unsigned char id[32] = { + 0xb9, 0x2c, 0x1b, 0x6c, 0x7f, 0x6c, 0x97, 0x71, 0x4a, 0xd9, 0x52, 0x87, + 0xa8, 0x11, 0x97, 0x7a, 0x81, 0x4c, 0xed, 0x6e, 0x45, 0x6f, 0x56, 0x08, + 0xfe, 0xf0, 0xd4, 0x9a, 0xcc, 0x2a, 0xc6, 0x4e }; + + assert(parse_nostr_bech32(buf, sizeof(buf), nevent, strlen(nevent), &obj)); + assert(obj.type == NOSTR_BECH32_NEVENT); + assert(!memcmp(obj.nevent.event_id, id, 32)); + assert(obj.nevent.relays.num_relays == 1); + const char damus_relay[] = "wss://relay.damus.io"; + assert(sizeof(damus_relay)-1 == obj.nevent.relays.relays[0].len); + assert(!memcmp(obj.nevent.relays.relays[0].str, damus_relay, sizeof(damus_relay)-1)); +} + static void test_tce_eose() { unsigned char buf[1024]; const char json[] = "[\"EOSE\",\"s\"]"; @@ -1071,6 +1090,7 @@ static int test_varints() { int main(int argc, const char *argv[]) { test_parse_content(); test_varints(); + test_bech32_objects(); test_encode_decode_invoice(); test_filters(); //test_migrate();