static void test_reader_tell (void) { bson_reader_t *reader; const bson_t *b; uint32_t i; bson_iter_t iter; bool eof; int fd; fd = bson_open(BINARY_DIR"/stream.bson", O_RDONLY); assert(-1 != fd); reader = bson_reader_new_from_handle ((void *)&fd, &test_reader_from_handle_read, &test_reader_from_handle_destroy); for (i = 0; i < 1000; i++) { if (i) { assert_cmpint(5 * i, ==, bson_reader_tell(reader)); } else { assert_cmpint(0, ==, bson_reader_tell(reader)); } eof = false; b = bson_reader_read(reader, &eof); assert(b); assert(bson_iter_init(&iter, b)); assert(!bson_iter_next(&iter)); }
static void test_bson_corrupt_binary (void) { uint8_t *buf; bson_t b; size_t buflen = 1024; char *str; int fd; ssize_t r; buf = bson_malloc0(buflen); fd = bson_open(BINARY_DIR"/test57.bson", O_RDONLY); BSON_ASSERT(-1 != fd); r = bson_read(fd, buf, buflen); BSON_ASSERT(r == 26); r = bson_init_static(&b, buf, (uint32_t)r); BSON_ASSERT(r); str = bson_as_json(&b, NULL); BSON_ASSERT(!str); bson_destroy(&b); bson_free(buf); }
static void test_reader_tell (void) { bson_reader_t *reader; const bson_t *b; bson_uint32_t i; bson_iter_t iter; bson_bool_t eof; int fd; fd = bson_open("tests/binary/stream.bson", BSON_O_RDONLY); assert(fd >= 0); reader = bson_reader_new_from_fd(fd, TRUE); for (i = 0; i < 1000; i++) { if (i) { assert_cmpint(5 * i, ==, bson_reader_tell(reader)); } else { assert_cmpint(0, ==, bson_reader_tell(reader)); } eof = FALSE; b = bson_reader_read(reader, &eof); assert(b); assert(bson_iter_init(&iter, b)); assert(!bson_iter_next(&iter)); }
static void test_bson_as_json_stack_overflow (void) { uint8_t *buf; bson_t b; size_t buflen = 1024 * 1024 * 17; char *str; int fd; ssize_t r; buf = bson_malloc0(buflen); fd = bson_open(BINARY_DIR"/stackoverflow.bson", O_RDONLY); BSON_ASSERT(-1 != fd); r = bson_read(fd, buf, buflen); BSON_ASSERT(r == 16777220); r = bson_init_static(&b, buf, 16777220); BSON_ASSERT(r); str = bson_as_json(&b, NULL); BSON_ASSERT(str); r = !!strstr(str, "..."); BSON_ASSERT(str); bson_free(str); bson_destroy(&b); bson_free(buf); }
static bson_t * get_bson (const char *filename) { uint8_t buf[4096]; bson_t *b; ssize_t len; int fd; if (-1 == (fd = bson_open(filename, O_RDONLY))) { fprintf(stderr, "Failed to open: %s\n", filename); abort(); } if ((len = bson_read(fd, buf, sizeof buf)) < 0) { fprintf(stderr, "Failed to read: %s\n", filename); abort(); } assert(len > 0); b = bson_new_from_data(buf, (uint32_t)len); bson_close(fd); return b; }