From 878e335ed883b0764e3a02884f25eeec27269614 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 1 Aug 2024 14:07:19 -0700 Subject: [PATCH] fix bech32 parsing and add test was off by one Signed-off-by: William Casarin --- src/nostr_bech32.c | 5 +++-- test.c | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/nostr_bech32.c b/src/nostr_bech32.c index bf7f59b..c1e39b7 100644 --- a/src/nostr_bech32.c +++ b/src/nostr_bech32.c @@ -269,8 +269,9 @@ int parse_nostr_bech32_str(struct cursor *bech32, enum nostr_bech32_type *type) return 0; } - // must be at least 60 chars for the data part - if (bech32->p - data_start < 60) { + // must be at least 59 chars for the data part + //ndb_debug("bech32_data_size %ld '%c' '%c' '%.*s'\n", bech32->p - data_start, *(bech32->p-1), *data_start, (int)(bech32->p - data_start), data_start); + if (bech32->p - data_start < 59) { bech32->p = start; return 0; } diff --git a/test.c b/test.c index f1636ad..1df69fd 100644 --- a/test.c +++ b/test.c @@ -866,6 +866,42 @@ static void test_parse_nevent() { assert(ok == 3); } +static void test_bech32_parsing() { + unsigned char buf[4096]; + const char *content = "https://damus.io/notedeck nostr:note1thp5828zk5xujrcuwdppcjnwlz43altca6269demenja3vqm5m2qclq35h"; + + struct ndb_blocks *blocks; + struct ndb_block *block; + struct ndb_str_block *str; + struct ndb_block_iterator iterator, *iter; + + iter = &iterator; + assert(ndb_parse_content(buf, sizeof(buf), content, strlen(content), &blocks)); + assert(blocks->num_blocks == 3); + + ndb_blocks_iterate_start(content, blocks, iter); + int i = 0; + while ((block = ndb_blocks_iterate_next(iter))) { + str = ndb_block_str(block); + switch (++i) { + case 1: + assert(ndb_get_block_type(block) == BLOCK_URL); + assert(!strncmp(str->str, "https://damus.io/notedeck", str->len)); + break; + case 2: + assert(ndb_get_block_type(block) == BLOCK_TEXT); + assert(!strncmp(str->str, " ", str->len)); + break; + case 3: + assert(ndb_get_block_type(block) == BLOCK_MENTION_BECH32); + assert(!strncmp(str->str, "note1thp5828zk5xujrcuwdppcjnwlz43altca6269demenja3vqm5m2qclq35h", str->len)); + break; + } + } + + assert(i == 3); +} + static void test_single_url_parsing() { unsigned char buf[4096]; const char *content = "https://damus.io/notedeck"; @@ -1507,12 +1543,13 @@ static void test_weird_note_corruption() { } int main(int argc, const char *argv[]) { + test_bech32_parsing(); test_single_url_parsing(); + test_url_parsing(); test_weird_note_corruption(); test_query(); test_tag_query(); test_parse_content(); - test_url_parsing(); test_subscriptions(); test_comma_url_parsing(); test_varints();