summaryrefslogtreecommitdiff
path: root/tests/unit/mongo/client
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/mongo/client')
-rw-r--r--tests/unit/mongo/client/connect.c34
-rw-r--r--tests/unit/mongo/client/connection_get_requestid.c44
-rw-r--r--tests/unit/mongo/client/connection_set_timeout.c33
-rw-r--r--tests/unit/mongo/client/disconnect.c32
-rw-r--r--tests/unit/mongo/client/packet_recv.c56
-rw-r--r--tests/unit/mongo/client/packet_send.c75
6 files changed, 274 insertions, 0 deletions
diff --git a/tests/unit/mongo/client/connect.c b/tests/unit/mongo/client/connect.c
new file mode 100644
index 0000000..fc390ea
--- /dev/null
+++ b/tests/unit/mongo/client/connect.c
@@ -0,0 +1,34 @@
+#include "test.h"
+#include "tap.h"
+#include "mongo-client.h"
+
+#include <errno.h>
+
+void
+test_mongo_connect (void)
+{
+ mongo_connection *c;
+
+ ok (mongo_connect (NULL, 27010) == NULL,
+ "mongo_connect() fails with a NULL host");
+ ok (errno == EINVAL,
+ "mongo_connect() should fail with EINVAL if host is NULL");
+
+ begin_network_tests (4);
+
+ ok (mongo_connect ("invalid.example.com", 27017) == NULL,
+ "Connecting to an invalid host fails");
+ ok (mongo_connect ("example.com", 27017) == NULL,
+ "Connecting to an unavailable host/port fails");
+ ok (mongo_connect ("/does/not/exist.sock", MONGO_CONN_LOCAL) == NULL,
+ "Connecting to an unavailable unix socket fails");
+
+ ok ((c = mongo_connect (config.primary_host,
+ config.primary_port)) != NULL,
+ "Connecting to the primary server works");
+ mongo_disconnect (c);
+
+ end_network_tests ();
+}
+
+RUN_TEST (6, mongo_connect);
diff --git a/tests/unit/mongo/client/connection_get_requestid.c b/tests/unit/mongo/client/connection_get_requestid.c
new file mode 100644
index 0000000..9232689
--- /dev/null
+++ b/tests/unit/mongo/client/connection_get_requestid.c
@@ -0,0 +1,44 @@
+#include "test.h"
+#include "mongo.h"
+
+#include "libmongo-private.h"
+
+void
+test_mongo_connection_get_requestid (void)
+{
+ mongo_connection c, *conn;
+ mongo_packet *p;
+ bson *b;
+ gint reqid;
+
+ c.request_id = 42;
+
+ ok (mongo_connection_get_requestid (NULL) == -1,
+ "mongo_connection_get_requestid() fails with a NULL connection");
+ ok (mongo_connection_get_requestid (&c) == 42,
+ "mongo_connection_get_requestid() works");
+
+ begin_network_tests (2);
+
+ b = bson_new ();
+ bson_append_int32 (b, "getnonce", 1);
+ bson_finish (b);
+
+ p = mongo_wire_cmd_custom (42, config.db, 0, b);
+ bson_free (b);
+
+ conn = mongo_connect (config.primary_host, config.primary_port);
+ cmp_ok ((reqid = mongo_connection_get_requestid (conn)), "==", 0,
+ "Initial request id is 0");
+ mongo_packet_send (conn, p);
+ mongo_wire_packet_free (p);
+
+ cmp_ok (reqid, "<", mongo_connection_get_requestid (conn),
+ "Old request ID is smaller than the new one");
+
+ mongo_disconnect (conn);
+
+ end_network_tests ();
+}
+
+RUN_TEST (4, mongo_connection_get_requestid);
diff --git a/tests/unit/mongo/client/connection_set_timeout.c b/tests/unit/mongo/client/connection_set_timeout.c
new file mode 100644
index 0000000..02468bf
--- /dev/null
+++ b/tests/unit/mongo/client/connection_set_timeout.c
@@ -0,0 +1,33 @@
+#include "test.h"
+#include "mongo.h"
+
+#include "libmongo-private.h"
+
+void
+test_mongo_connection_set_timeout (void)
+{
+ mongo_connection c, *conn;
+
+ c.fd = -1;
+
+ ok (mongo_connection_set_timeout (NULL, 100) == FALSE,
+ "mongo_connection_set_timeout() should fail with a NULL connection");
+ ok (mongo_connection_set_timeout (&c, -1) == FALSE,
+ "mongo_connection_set_timeout() should fail with a negative timeout");
+ ok (mongo_connection_set_timeout (&c, 100) == FALSE,
+ "mongo_connection_set_timeout() should fail with an invalid FD");
+
+ begin_network_tests (0);
+
+ conn = mongo_connect (config.primary_host, config.primary_port);
+
+ /* No verification here, as some systems may or may not support
+ this, thus, failing in a test is not fatal. */
+ mongo_connection_set_timeout (conn, 100);
+
+ mongo_disconnect (conn);
+
+ end_network_tests ();
+}
+
+RUN_TEST (3, mongo_connection_set_timeout);
diff --git a/tests/unit/mongo/client/disconnect.c b/tests/unit/mongo/client/disconnect.c
new file mode 100644
index 0000000..1b0be93
--- /dev/null
+++ b/tests/unit/mongo/client/disconnect.c
@@ -0,0 +1,32 @@
+#include "test.h"
+#include "tap.h"
+#include "mongo-client.h"
+
+#include "libmongo-private.h"
+#include <errno.h>
+
+void
+test_mongo_disconnect (void)
+{
+ mongo_connection *conn;
+
+ conn = g_new0 (mongo_connection, 1);
+ conn->fd = -1;
+
+ errno = 0;
+ mongo_disconnect (NULL);
+ ok (errno == ENOTCONN,
+ "mongo_disconnect() fails with ENOTCONN when passed a NULL connection");
+
+ mongo_disconnect (conn);
+ ok (errno == 0,
+ "mongo_disconnect() works");
+
+ conn = g_new0 (mongo_connection, 1);
+ conn->fd = 100;
+ mongo_disconnect (conn);
+ ok (errno == 0,
+ "mongo_disconnect() works, even with a bogus FD");
+}
+
+RUN_TEST (3, mongo_disconnect);
diff --git a/tests/unit/mongo/client/packet_recv.c b/tests/unit/mongo/client/packet_recv.c
new file mode 100644
index 0000000..51ccb3d
--- /dev/null
+++ b/tests/unit/mongo/client/packet_recv.c
@@ -0,0 +1,56 @@
+#include "test.h"
+#include "mongo.h"
+
+#include <errno.h>
+#include <sys/socket.h>
+
+#include "libmongo-private.h"
+
+void
+test_mongo_packet_recv (void)
+{
+ mongo_connection c, *conn;
+ mongo_packet *p;
+ bson *b;
+
+ c.fd = -1;
+
+ ok (mongo_packet_recv (NULL) == NULL,
+ "mongo_packet_recv() fails with a NULL connection");
+ ok (errno == ENOTCONN,
+ "mongo_packet_recv() sets errno to ENOTCONN if connection is NULL");
+
+ ok (mongo_packet_recv (&c) == NULL,
+ "mongo_packet_recv() fails if the FD is less than zero");
+ ok (errno == EBADF,
+ "mongo_packet_recv() sets errno to EBADF is the FD is bad");
+
+ begin_network_tests (2);
+
+ b = bson_new ();
+ bson_append_int32 (b, "getnonce", 1);
+ bson_finish (b);
+
+ p = mongo_wire_cmd_custom (42, config.db, 0, b);
+ bson_free (b);
+
+ conn = mongo_connect (config.primary_host, config.primary_port);
+ mongo_packet_send (conn, p);
+ mongo_wire_packet_free (p);
+
+ ok ((p = mongo_packet_recv (conn)) != NULL,
+ "mongo_packet_recv() works");
+ mongo_wire_packet_free (p);
+
+ close (conn->fd);
+ sleep (3);
+
+ ok (mongo_packet_recv (conn) == NULL,
+ "mongo_packet_recv() fails on a closed socket");
+
+ mongo_disconnect (conn);
+
+ end_network_tests ();
+}
+
+RUN_TEST (6, mongo_packet_recv);
diff --git a/tests/unit/mongo/client/packet_send.c b/tests/unit/mongo/client/packet_send.c
new file mode 100644
index 0000000..e501a3c
--- /dev/null
+++ b/tests/unit/mongo/client/packet_send.c
@@ -0,0 +1,75 @@
+#include "test.h"
+#include "tap.h"
+#include "mongo-wire.h"
+#include "mongo-client.h"
+
+#include <errno.h>
+#include <sys/socket.h>
+
+#include "libmongo-private.h"
+
+void
+test_mongo_packet_send (void)
+{
+ mongo_packet *p;
+ mongo_connection c, *conn;
+ mongo_packet_header h;
+ bson *b;
+
+ p = mongo_wire_cmd_kill_cursors (1, 2, (gint64)3, (gint64)4);
+ c.fd = -1;
+
+ ok (mongo_packet_send (NULL, p) == FALSE,
+ "mongo_packet_send() fails with a NULL connection");
+ ok (errno == ENOTCONN,
+ "mongo_packet_send() with a NULL connection sets errno to ENOTCONN");
+ ok (mongo_packet_send (&c, NULL) == FALSE,
+ "mongo_packet_send() fails with a NULL packet");
+ ok (errno == EINVAL,
+ "mongo_packet_send() with a NULL packet sets errno to EINVAL");
+ ok (mongo_packet_send (&c, p) == FALSE,
+ "mongo_packet_send() fails if the FD is less than zero");
+ ok (errno == EBADF,
+ "mongo_packet_send() sets errno to EBADF is the FD is bad");
+ mongo_wire_packet_free (p);
+
+ p = mongo_wire_packet_new ();
+
+ h.id = 42;
+ h.resp_to = 0;
+ h.opcode = 1;
+ h.length = sizeof (mongo_packet_header);
+ mongo_wire_packet_set_header (p, &h);
+
+ c.fd = 1;
+ ok (mongo_packet_send (&c, p) == FALSE,
+ "mongo_packet_send() fails with an unfinished packet");
+
+ mongo_wire_packet_free (p);
+
+ begin_network_tests (2);
+
+ b = bson_new ();
+ bson_append_int32 (b, "getnonce", 1);
+ bson_finish (b);
+
+ p = mongo_wire_cmd_custom (42, config.db, 0, b);
+ bson_free (b);
+
+ conn = mongo_connect (config.primary_host, config.primary_port);
+ ok (mongo_packet_send (conn, p),
+ "mongo_packet_send() works");
+
+ close (conn->fd);
+ sleep (3);
+
+ ok (mongo_packet_send (conn, p) == FALSE,
+ "mongo_packet_send() fails on a closed socket");
+ mongo_wire_packet_free (p);
+
+ mongo_disconnect (conn);
+
+ end_network_tests ();
+}
+
+RUN_TEST (9, mongo_packet_send);