Skip to content

Commit

Permalink
fix bech32 parsing and add test
Browse files Browse the repository at this point in the history
was off by one

Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Aug 1, 2024
1 parent 5727bb4 commit 878e335
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/nostr_bech32.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
39 changes: 38 additions & 1 deletion test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 878e335

Please sign in to comment.