dodo/dodo

Parents 69a81838fe3b
Children 6897bb92c387
Add a simple unit test for the input stream and fix a few issues
--- a/dodo/dodoinputstream.c Sun Jul 09 01:58:53 2023 -0500
+++ b/dodo/dodoinputstream.c Sun Jul 09 02:38:13 2023 -0500
@@ -30,7 +30,7 @@
GMarkupParseContext *context;
};
-G_DEFINE_TYPE(DodoInputStream, dodo_input_stream, G_TYPE_OBJECT)
+G_DEFINE_TYPE(DodoInputStream, dodo_input_stream, DODO_TYPE_STREAM)
/******************************************************************************
* Helpers
@@ -103,7 +103,7 @@
const char *version = NULL;
const char *xml_lang = NULL;
const char *xmlns = NULL;
- const char *xmlns_server = NULL;
+ const char *xmlns_stream = NULL;
ret = g_markup_collect_attributes(
element_name, attribute_names, attribute_values, error,
@@ -113,7 +113,7 @@
G_MARKUP_COLLECT_STRING, "version", &version,
G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xml:lang", &xml_lang,
G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xmlns", &xmlns,
- G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xmlns:server", &xmlns_server,
+ G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL, "xmlns:stream", &xmlns_stream,
G_MARKUP_COLLECT_INVALID);
if(!ret) {
@@ -145,10 +145,10 @@
return;
}
- if(!dodo_str_equal(xmlns_server, DODO_STREAM_XMLNS_STREAM)) {
+ if(!dodo_str_equal(xmlns_stream, DODO_STREAM_XMLNS_STREAM)) {
g_set_error(error, DODO_DOMAIN, 0,
"got xmlns:stream='%s' but was expecting '%s'",
- xmlns_server, DODO_STREAM_XMLNS_STREAM);
+ xmlns_stream, DODO_STREAM_XMLNS_STREAM);
return;
}
}
--- a/dodo/tests/meson.build Sun Jul 09 01:58:53 2023 -0500
+++ b/dodo/tests/meson.build Sun Jul 09 02:38:13 2023 -0500
@@ -1,4 +1,5 @@
PROGRAMS = [
+ 'inputstream',
'message',
'outputstream',
'stanza',
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/dodo/tests/testinputstream.c Sun Jul 09 02:38:13 2023 -0500
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2023 Dodo Developers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+
+#include <dodo.h>
+
+/******************************************************************************
+ * Callbacks
+ *****************************************************************************/
+
+/**
+ * test_dodo_connection_timeout_cb: (skip)
+ * data: The main loop to quit.
+ *
+ * Used for callbacks for timing out the main loops when running tests. If this
+ * is called, it will assert that it was reached and cause the test to fail.
+ *
+ * Returns: G_SOURCE_REMOVE to not be called again.
+ */
+static gboolean
+test_dodo_input_stream_timeout_cb(gpointer data) {
+ g_main_loop_quit(data);
+
+ g_assert_not_reached();
+
+ return G_SOURCE_REMOVE;
+}
+
+static gboolean
+test_dodo_input_stream_quit_cb(gpointer data) {
+ g_main_loop_quit(data);
+
+ return G_SOURCE_REMOVE;
+}
+
+/******************************************************************************
+ * Tests
+ *****************************************************************************/
+static void
+test_dodo_input_stream_start(void) {
+ DodoInputStream *stream = NULL;
+ GError *error = NULL;
+ GMainLoop *loop = NULL;
+ GInputStream *input = NULL;
+ gboolean ret = FALSE;
+ const char *input_data = "" \
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<stream:stream from=\"juliet@im.example.com\" "
+ "to=\"im.example.com\" id=\"t7AMCin9zjMNwQKDnplntZPIDEI=\" "
+ "version=\"1.0\" xml:lang=\"en\" xmlns=\"jabber:client\" "
+ "xmlns:stream=\"http://etherx.jabber.org/streams\">";
+ const char *value = NULL;
+
+ stream = dodo_input_stream_new();
+
+ input = g_memory_input_stream_new_from_data(input_data, -1, NULL);
+
+ ret = dodo_input_stream_start(stream, input, &error);
+
+ g_assert_no_error(error);
+ g_assert_true(ret);
+
+ /* Create our main loop and add some timeouts and stuff. */
+ loop = g_main_loop_new(NULL, FALSE);
+ g_timeout_add(2, test_dodo_input_stream_quit_cb, loop);
+ g_timeout_add(100, test_dodo_input_stream_timeout_cb, loop);
+
+ g_main_loop_run(loop);
+
+ /* If we made it here, the quit cb got called, so verify everything. */
+ value = dodo_stream_get_from(DODO_STREAM(stream));
+ g_assert_cmpstr(value, ==, "juliet@im.example.com");
+
+ value = dodo_stream_get_to(DODO_STREAM(stream));
+ g_assert_cmpstr(value, ==, "im.example.com");
+
+ value = dodo_stream_get_id(DODO_STREAM(stream));
+ g_assert_cmpstr(value, ==, "t7AMCin9zjMNwQKDnplntZPIDEI=");
+
+ value = dodo_stream_get_version(DODO_STREAM(stream));
+ g_assert_cmpstr(value, ==, "1.0");
+
+ value = dodo_stream_get_language(DODO_STREAM(stream));
+ g_assert_cmpstr(value, ==, "en");
+
+ g_clear_object(&input);
+ g_clear_object(&stream);
+}
+
+/******************************************************************************
+ * Main
+ *****************************************************************************/
+int
+main(int argc, char *argv[]) {
+ g_test_init(&argc, &argv, NULL);
+
+ g_test_add_func("/dodo/input_stream/start",
+ test_dodo_input_stream_start);
+
+ return g_test_run();
+}
+