From fd841e416881cc0392e61ec312c1870f3a0004bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Tue, 2 Dec 2014 10:06:21 +0100 Subject: Initial import of libmongo-client version 0.1.8-2 --- tests/func/bson/f_weird_types.c | 71 +++++++++++++++++++++++++++++++++++++++++ tests/func/bson/huge_doc.c | 51 +++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 tests/func/bson/f_weird_types.c create mode 100644 tests/func/bson/huge_doc.c (limited to 'tests/func/bson') diff --git a/tests/func/bson/f_weird_types.c b/tests/func/bson/f_weird_types.c new file mode 100644 index 0000000..100db8c --- /dev/null +++ b/tests/func/bson/f_weird_types.c @@ -0,0 +1,71 @@ +#include "bson.h" +#include "tap.h" +#include "test.h" + +#include "libmongo-private.h" + +#include + +static void +test_func_weird_types (void) +{ + bson *b; + bson_cursor *c; + guint8 type = BSON_TYPE_DBPOINTER; + gint32 slen; + + b = bson_new (); + bson_append_int32 (b, "int32", 42); + + /* Append weird stuff */ + b->data = g_byte_array_append (b->data, (const guint8 *)&type, sizeof (type)); + b->data = g_byte_array_append (b->data, (const guint8 *)"dbpointer", + strlen ("dbpointer") + 1); + slen = GINT32_TO_LE (strlen ("refname") + 1); + b->data = g_byte_array_append (b->data, (const guint8 *)&slen, sizeof (gint32)); + b->data = g_byte_array_append (b->data, (const guint8 *)"refname", + strlen ("refname") + 1); + b->data = g_byte_array_append (b->data, (const guint8 *)"0123456789ABCDEF", + 12); + + bson_append_boolean (b, "Here be dragons?", TRUE); + bson_finish (b); + + c = bson_find (b, "Here be dragons?"); + ok (c != NULL, + "bson_find() can find elements past unsupported BSON types"); + bson_cursor_free (c); + bson_free (b); + + /* Now do it again, but append a type we can't iterate over */ + b = bson_new (); + bson_append_int32 (b, "int32", 42); + + /* Append BSON_TYPE_NONE */ + type = BSON_TYPE_NONE; + b->data = g_byte_array_append (b->data, (const guint8 *)&type, sizeof (type)); + b->data = g_byte_array_append (b->data, (const guint8 *)"dbpointer", + strlen ("dbpointer") + 1); + b->data = g_byte_array_append (b->data, (const guint8 *)"0123456789ABCDEF", + 12); + + bson_append_boolean (b, "Here be dragons?", TRUE); + bson_finish (b); + + c = bson_find (b, "Here be dragons?"); + ok (c == NULL, + "bson_find() should bail out when encountering an invalid element."); + bson_cursor_free (c); + + c = bson_cursor_new (b); + bson_cursor_next (c); /* This will find the first element, and + position us there. */ + bson_cursor_next (c); /* This positions after the first element. */ + ok (bson_cursor_next (c) == FALSE, + "bson_cursor_next() should bail out when encountering an invalid element."); + bson_cursor_free (c); + + bson_free (b); +} + +RUN_TEST (3, func_weird_types); diff --git a/tests/func/bson/huge_doc.c b/tests/func/bson/huge_doc.c new file mode 100644 index 0000000..d5daafe --- /dev/null +++ b/tests/func/bson/huge_doc.c @@ -0,0 +1,51 @@ +#include "bson.h" +#include "tap.h" +#include "test.h" + +#ifndef HUGE_DOC_SIZE +#define HUGE_DOC_SIZE (1024 * 1024) +#endif + +#include + +static void +test_bson_huge_doc (void) +{ + bson *b, *s; + bson_cursor *c; + gchar *buffer; + gint32 ds1; + + buffer = (gchar *)g_malloc (HUGE_DOC_SIZE); + memset (buffer, 'a', HUGE_DOC_SIZE); + buffer[HUGE_DOC_SIZE - 1] = '\0'; + + b = bson_new (); + bson_append_int32 (b, "preamble", 1); + bson_append_string (b, "huge", buffer, -1); + bson_append_int32 (b, "post", 1234); + bson_finish (b); + ds1 = bson_size (b); + + g_free (buffer); + + s = bson_new (); + bson_append_document (s, "hugedoc", b); + bson_finish (s); + bson_free (b); + + cmp_ok (bson_size (s), ">", ds1, + "Document embedding another huge one, has bigger size"); + + c = bson_find (s, "hugedoc"); + bson_cursor_get_document (c, &b); + + cmp_ok (bson_size (b), "==", ds1, + "The embedded document has the correct, huge size"); + + bson_cursor_free (c); + bson_free (s); + bson_free (b); +} + +RUN_TEST (2, bson_huge_doc); -- cgit v1.2.3