summaryrefslogtreecommitdiff
path: root/tests/unit/mongo/utils
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/mongo/utils')
-rw-r--r--tests/unit/mongo/utils/oid_as_string.c26
-rw-r--r--tests/unit/mongo/utils/oid_init.c19
-rw-r--r--tests/unit/mongo/utils/oid_new.c49
-rw-r--r--tests/unit/mongo/utils/oid_new_with_time.c46
-rw-r--r--tests/unit/mongo/utils/parse_addr.c244
5 files changed, 384 insertions, 0 deletions
diff --git a/tests/unit/mongo/utils/oid_as_string.c b/tests/unit/mongo/utils/oid_as_string.c
new file mode 100644
index 0000000..9cf740c
--- /dev/null
+++ b/tests/unit/mongo/utils/oid_as_string.c
@@ -0,0 +1,26 @@
+#include "test.h"
+#include "mongo.h"
+
+void
+test_mongo_utils_oid_as_string (void)
+{
+ guint8 *oid;
+ gchar *oid_str;
+
+ mongo_util_oid_init (0);
+
+ oid = mongo_util_oid_new (1);
+
+ ok (mongo_util_oid_as_string (NULL) == NULL,
+ "mongo_util_oid_as_string() should fail with a NULL oid");
+
+ oid_str = mongo_util_oid_as_string (oid);
+
+ ok (oid_str != NULL,
+ "mongo_util_oid_as_string() works");
+
+ g_free (oid_str);
+ g_free (oid);
+}
+
+RUN_TEST (2, mongo_utils_oid_as_string);
diff --git a/tests/unit/mongo/utils/oid_init.c b/tests/unit/mongo/utils/oid_init.c
new file mode 100644
index 0000000..42d0db1
--- /dev/null
+++ b/tests/unit/mongo/utils/oid_init.c
@@ -0,0 +1,19 @@
+#include "tap.h"
+#include "test.h"
+#include "mongo-utils.h"
+
+void
+test_mongo_utils_oid_init (void)
+{
+ mongo_util_oid_init (0);
+ mongo_util_oid_init (1234);
+
+ /* We don't do any real testing here, only check if it does not
+ crash. To verify that it works, we need to create a new OID, and
+ that will be tested by other unit tests.
+ */
+ ok (TRUE,
+ "mongo_util_oid_init() does not crash.");
+}
+
+RUN_TEST (1, mongo_utils_oid_init);
diff --git a/tests/unit/mongo/utils/oid_new.c b/tests/unit/mongo/utils/oid_new.c
new file mode 100644
index 0000000..b8f7f0a
--- /dev/null
+++ b/tests/unit/mongo/utils/oid_new.c
@@ -0,0 +1,49 @@
+#include "tap.h"
+#include "test.h"
+#include "mongo-utils.h"
+
+#include <string.h>
+#include <unistd.h>
+
+void
+test_mongo_utils_oid_new (void)
+{
+ guint8 *oid1, *oid2, *oid3;
+ gchar *oid1_s, *oid2_s;
+
+ ok (mongo_util_oid_new (0) == NULL,
+ "mongo_util_oid_new() should fail before mongo_util_oid_init()");
+
+ mongo_util_oid_init (0);
+ ok ((oid1 = mongo_util_oid_new (1)) != NULL,
+ "mongo_util_oid_new() works");
+ cmp_ok (oid1[11], "==", 1,
+ "mongo_util_oid_new() returns an OID with the currect seq ID");
+
+ oid2 = mongo_util_oid_new (2);
+ oid3 = mongo_util_oid_new (2);
+
+ ok (memcmp (oid2, oid1, 12) > 0,
+ "OIDs with higher sequence ID sort higher");
+ ok (memcmp (oid2, oid3, 12) == 0,
+ "OIDs with the same sequence ID are equal (within a second)");
+ g_free (oid2);
+ g_free (oid3);
+
+ sleep (2);
+ oid2 = mongo_util_oid_new (0);
+
+ oid1_s = mongo_util_oid_as_string (oid1);
+ oid2_s = mongo_util_oid_as_string (oid2);
+
+ ok (memcmp (oid2, oid1, 12) > 0,
+ "OIDs with the same sequence ID, a few seconds later sort higher; "
+ "oid1=%s; oid2=%s", oid1_s, oid2_s);
+
+ g_free (oid2_s);
+ g_free (oid1_s);
+ g_free (oid2);
+ g_free (oid1);
+}
+
+RUN_TEST (6, mongo_utils_oid_new);
diff --git a/tests/unit/mongo/utils/oid_new_with_time.c b/tests/unit/mongo/utils/oid_new_with_time.c
new file mode 100644
index 0000000..290fdab
--- /dev/null
+++ b/tests/unit/mongo/utils/oid_new_with_time.c
@@ -0,0 +1,46 @@
+#include "tap.h"
+#include "test.h"
+#include "mongo-utils.h"
+
+#include <string.h>
+
+void
+test_mongo_utils_oid_new_with_time (void)
+{
+ guint8 *oid1, *oid2, *oid3;
+ gchar *oid1_s, *oid2_s;
+
+ ok (mongo_util_oid_new_with_time (0, 0) == NULL,
+ "mongo_util_oid_new_with_time() should fail before mongo_util_oid_init()");
+
+ mongo_util_oid_init (0);
+ ok ((oid1 = mongo_util_oid_new_with_time (0, 1)) != NULL,
+ "mongo_util_oid_new_with_time() works");
+ cmp_ok (oid1[11], "==", 1,
+ "mongo_util_oid_new_with_time() returns an OID with the currect seq ID");
+
+ oid2 = mongo_util_oid_new_with_time (0, 2);
+ oid3 = mongo_util_oid_new_with_time (0, 2);
+
+ ok (memcmp (oid2, oid1, 12) > 0,
+ "OIDs with higher sequence ID sort higher");
+ ok (memcmp (oid2, oid3, 12) == 0,
+ "OIDs with the same sequence ID are equal (within a second)");
+ g_free (oid2);
+ g_free (oid3);
+
+ oid2 = mongo_util_oid_new_with_time (1, 0);
+
+ oid1_s = mongo_util_oid_as_string (oid1);
+ oid2_s = mongo_util_oid_as_string (oid2);
+
+ ok (memcmp (oid2, oid1, 12) > 0,
+ "OIDs with the same sequence ID, a few seconds later sort higher; "
+ "oid1=%s; oid2=%s", oid1_s, oid2_s);
+ g_free (oid2_s);
+ g_free (oid1_s);
+ g_free (oid2);
+ g_free (oid1);
+}
+
+RUN_TEST (6, mongo_utils_oid_new_with_time);
diff --git a/tests/unit/mongo/utils/parse_addr.c b/tests/unit/mongo/utils/parse_addr.c
new file mode 100644
index 0000000..13b16d1
--- /dev/null
+++ b/tests/unit/mongo/utils/parse_addr.c
@@ -0,0 +1,244 @@
+#include "tap.h"
+#include "test.h"
+#include "mongo-utils.h"
+
+#include <string.h>
+
+void
+test_mongo_utils_parse_addr (void)
+{
+ gchar *host = "deadbeef";
+ gint port = 42;
+
+ ok (mongo_util_parse_addr (NULL, &host, &port) == FALSE,
+ "mongo_util_parse_addr() fails with a NULL address");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("127.0.0.1:27017", &host, NULL) == FALSE,
+ "mongo_util_parse_addr() fails when port is NULL");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("127.0.0.1:27017", NULL, &port) == FALSE,
+ "mongo_util_parse_addr() fails when host is NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("127.0.0.1:27017", &host, &port),
+ "mongo_util_parse_addr() can parse HOST:PORT pairs");
+ is (host, "127.0.0.1",
+ "Host parsed successfully");
+ cmp_ok (port, "==", 27017,
+ "Port parsed successfully");
+ g_free (host);
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr (":27017", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail when no host is specified");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("localhost:27017garbage", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if there is garbage after "
+ "the port");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("localhost:garbage", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if the port is not a number");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("localhost:-10", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if the port is out of bounds");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("localhost:9999999999999999999",
+ &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if the port is out of bounds");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("localhost:9999999999",
+ &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if the port is out of bounds");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ /* IPv6 */
+ ok (mongo_util_parse_addr ("::1:27017", &host, &port),
+ "mongo_util_parse_addr() can deal with IPv6 addresses");
+ is (host, "::1",
+ "Host parsed successfully");
+ cmp_ok (port, "==", 27017,
+ "Port parsed successfully");
+ g_free (host);
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("::1", &host, &port),
+ "mongo_util_parse_addr() should silently misparse ambigous "
+ "IPv6 addresses");
+ isnt (host, "::1",
+ "Host is misparsed, as expected");
+ cmp_ok (port, "==", 1,
+ "Port is misparsed, as expected");
+ g_free (host);
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail on invalid IPv6 literals");
+ is (host, NULL,
+ "Host should be NULL");
+ cmp_ok (port, "==", -1,
+ "Port should be -1");
+ g_free (host);
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1]:1", &host, &port),
+ "mongo_util_parse_addr() works with IPv6 literal + port");
+ is (host, "::1",
+ "Host should be ::1");
+ cmp_ok (port, "==", 1,
+ "Port should be 1");
+ g_free (host);
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1]:27017", &host, &port),
+ "mongo_util_parse_addr() works with IPv6 literal + port");
+ is (host, "::1",
+ "Host should be ::1");
+ cmp_ok (port, "==", 27017,
+ "Port should be 27017");
+ g_free (host);
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[]:27017", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail when no host is specified");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1]:27017garbage", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if there is garbage after "
+ "the port");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1]:garbage", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if the port is not a number");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1]:-10", &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if the port is out of bounds");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1]:9999999999999999999",
+ &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if the port is out of bounds");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1]:9999999999",
+ &host, &port) == FALSE,
+ "mongo_util_parse_addr() should fail if the port is out of bounds");
+ is (host, NULL,
+ "Failed parsing sets host to NULL");
+ cmp_ok (port, "==", -1,
+ "Failed parsing sets port to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("/var/run/mongodb/mongodb.socket",
+ &host, &port) == TRUE,
+ "mongo_util_parse_addr() works with unix domain sockets");
+ is (host, "/var/run/mongodb/mongodb.socket",
+ "Parsing a Unix domain socket sets host to the socket name");
+ cmp_ok (port, "==", -1,
+ "Port is set to -1");
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("[::1]", &host, &port),
+ "mongo_util_parse_addr() can handle IPv6 literals without port set");
+ is (host, "::1",
+ "Host parsed successfully");
+ cmp_ok (port, "==", -1,
+ "Port is set to -1");
+ g_free (host);
+ host = "deadbeef";
+ port = 42;
+
+ ok (mongo_util_parse_addr ("/var/run/mongodb/mongodb.socket:-1",
+ &host, &port) == TRUE,
+ "mongo_util_parse_addr() can parse unix domain sockets with -1 port");
+ is (host, "/var/run/mongodb/mongodb.socket",
+ "Parsing a unix domain socket sets host to the socket name");
+ cmp_ok (port, "==", -1,
+ "Parsing a unix domain socket with a port set to -1, works");
+ g_free (host);
+ host = "deadbeef";
+ port = 42;
+}
+
+RUN_TEST (70, mongo_utils_parse_addr);