summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordefanor <defanor@uberspace.net>2022-01-10 17:30:43 +0300
committerdefanor <defanor@uberspace.net>2022-01-10 17:30:43 +0300
commitcfffd2ff85cc9c8656a16d951822abe9d296c219 (patch)
treed0dc254337a46953f192c095a57937427ff0e2d3
parent7893dabd6ff65a7d69ecc586e6bbab2688505239 (diff)
Add a basic test
-rw-r--r--Makefile.am2
-rw-r--r--README2
-rw-r--r--configure.ac2
-rw-r--r--tests/Makefile.am20
-rw-r--r--tests/README8
-rw-r--r--tests/send_to_self.c186
6 files changed, 217 insertions, 3 deletions
diff --git a/Makefile.am b/Makefile.am
index fcb3911..40ca628 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,5 @@
ACLOCAL_AMFLAGS = -I m4
-SUBDIRS = src
+SUBDIRS = src tests
EXTRA_DIST = examples/basic.c examples/weechat.c
info_TEXINFOS = rexmpp.texi
diff --git a/README b/README
index 5177630..6e6e27a 100644
--- a/README
+++ b/README
@@ -55,7 +55,7 @@ A rough roadmap:
[+] Abstraction of the used XML, SASL, TLS, and DNS libraries, and
optional usage of alternative ones. Though left libxml2 for now:
could reuse existing libxml2 bindings that way.
-[ ] Automated testing.
+[.] Automated testing.
- IM features:
diff --git a/configure.ac b/configure.ac
index f2052df..26d0a76 100644
--- a/configure.ac
+++ b/configure.ac
@@ -7,7 +7,7 @@ AM_INIT_AUTOMAKE([-Werror -Wall])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_SRCDIR([src/rexmpp.c])
AC_CONFIG_HEADERS([src/config.h])
-AC_CONFIG_FILES([Makefile src/Makefile rexmpp.pc Doxyfile])
+AC_CONFIG_FILES([Makefile src/Makefile tests/Makefile rexmpp.pc Doxyfile])
# Checks for programs.
AC_PROG_CC
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..7aba0b7
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,20 @@
+AM_CFLAGS = -Werror -Wall -Wextra -pedantic -std=gnu99 \
+ -Wno-pointer-sign
+
+COMMON_FLAGS = $(AM_CFLAGS) $(LIBXML_CFLAGS) \
+ $(GNUTLS_CFLAGS) $(LIBDANE_CFLAGS) $(OPENSSL_CFLAGS) \
+ $(GSASL_CFLAGS) $(UNBOUND_CFLAGS) $(CARES_CFLAGS) $(GPGME_CFLAGS) \
+ $(ICU_I18N_CFLAGS) $(LIBGCRYPT_CFLAGS) $(CURL_CFLAGS) \
+ $(NICE_CFLAGS) $(GLIB_CFLAGS) $(SRTP_CFLAGS) \
+ -I$(top_srcdir)/src
+COMMON_LIBS = $(LIBXML_LIBS) \
+ $(GNUTLS_LIBS) $(LIBDANE_LIBS) $(OPENSSL_LIBS) \
+ $(GSASL_LIBS) $(UNBOUND_LIBS) $(CARES_LIBS) $(GPGME_LIBS) $(ICU_I18N_LIBS) \
+ $(LIBGCRYPT_LIBS) $(CURL_LIBS) $(NICE_LIBS) $(GLIB_LIBS) $(SRTP_LIBS) \
+ -L$(top_builddir)/src -lrexmpp
+
+send_to_self_CFLAGS = $(COMMON_FLAGS)
+send_to_self_LDADD = $(COMMON_LIBS)
+
+check_PROGRAMS = send_to_self
+TESTS = send_to_self
diff --git a/tests/README b/tests/README
new file mode 100644
index 0000000..7e30213
--- /dev/null
+++ b/tests/README
@@ -0,0 +1,8 @@
+rexmpp tests
+
+These tests make use of an external XMPP server. Environment variables
+used by the tests:
+
+- JID and PASS: credentials.
+
+- TLS_POLICY: "require", "prefer", or "avoid".
diff --git a/tests/send_to_self.c b/tests/send_to_self.c
new file mode 100644
index 0000000..9c8c514
--- /dev/null
+++ b/tests/send_to_self.c
@@ -0,0 +1,186 @@
+/**
+ @file send_to_self.c
+ @brief A basic message sending test
+ @author defanor <defanor@uberspace.net>
+ @date 2022
+ @copyright MIT license.
+
+Connects to a server, sends a message to itself, receives it, checks
+that it's the expected message.
+
+*/
+
+#define TIMEOUT 30
+
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <syslog.h>
+#include <gsasl.h>
+#include <rexmpp.h>
+#include <rexmpp_sasl.h>
+
+enum test_stage {
+ TEST_CONNECTING,
+ TEST_MESSAGE_SENT,
+ TEST_MESSAGE_RECEIVED,
+ TEST_DONE,
+ TEST_TIMEOUT
+};
+
+enum test_stage stage = TEST_CONNECTING;
+char *jid, *pass, msg_text[256];
+
+void my_logger (rexmpp_t *s, int priority, const char *fmt, va_list args) {
+ (void)s;
+ char *priority_str = "unknown";
+ switch (priority) {
+ case LOG_EMERG: priority_str = "emerg"; break;
+ case LOG_ALERT: priority_str = "alert"; break;
+ case LOG_CRIT: priority_str = "crit"; break;
+ case LOG_ERR: priority_str = "err"; break;
+ case LOG_WARNING: priority_str = "warning"; break;
+ case LOG_NOTICE: priority_str = "notice"; break;
+ case LOG_INFO: priority_str = "info"; break;
+ case LOG_DEBUG: priority_str = "debug"; break;
+ }
+ fprintf(stdout, "[%s] ", priority_str);
+ vfprintf(stdout, fmt, args);
+ fprintf(stdout, "\n");
+}
+
+int my_sasl_property_cb (rexmpp_t *s, rexmpp_sasl_property prop) {
+ if (prop == REXMPP_SASL_PROP_PASSWORD) {
+ rexmpp_sasl_property_set (s, REXMPP_SASL_PROP_PASSWORD, pass);
+ return 0;
+ }
+ if (prop == REXMPP_SASL_PROP_AUTHID) {
+ rexmpp_sasl_property_set (s, REXMPP_SASL_PROP_AUTHID, s->initial_jid.local);
+ return 0;
+ }
+ printf("unhandled SASL property: %d\n", prop);
+ return -1;
+}
+
+int my_xml_in_cb (rexmpp_t *s, xmlNodePtr node) {
+ (void)s;
+ char *xml_buf = rexmpp_xml_serialize(node);
+ printf("recv: %s\n", xml_buf);
+ free(xml_buf);
+ if (stage == TEST_MESSAGE_SENT && rexmpp_xml_match(node, "jabber:client", "message")) {
+ xmlNodePtr body = rexmpp_xml_find_child(node, "jabber:client", "body");
+ if (body != NULL) {
+ char *txt = xmlNodeGetContent(body);
+ if (txt != NULL) {
+ if (strcmp(txt, msg_text) == 0) {
+ stage = TEST_MESSAGE_RECEIVED;
+ }
+ free(txt);
+ }
+ }
+ }
+ return 0;
+}
+
+int my_xml_out_cb (rexmpp_t *s, xmlNodePtr node) {
+ (void)s;
+ char *xml_buf = rexmpp_xml_serialize(node);
+ printf("send: %s\n", xml_buf);
+ free(xml_buf);
+ return 0;
+}
+
+int main (int argc, char **argv) {
+ (void)argc;
+ (void)argv;
+ jid = getenv("JID");
+ pass = getenv("PASS");
+ char *tls_policy = getenv("TLS_POLICY");
+
+ time_t t = time(NULL);
+ struct tm utc_time;
+ gmtime_r(&t, &utc_time);
+ strftime(msg_text, 256, "The current time is %FT%TZ", &utc_time);
+
+ rexmpp_t s;
+ rexmpp_err_t err;
+ err = rexmpp_init(&s, jid, my_logger);
+ if (err != REXMPP_SUCCESS) {
+ puts("Failed to initialise rexmpp.");
+ return -1;
+ }
+ if (tls_policy != NULL) {
+ if (strcasecmp(tls_policy, "require") == 0) {
+ s.tls_policy = REXMPP_TLS_REQUIRE;
+ } else if (strcasecmp(tls_policy, "prefer") == 0) {
+ s.tls_policy = REXMPP_TLS_PREFER;
+ } else if (strcasecmp(tls_policy, "avoid") == 0) {
+ s.tls_policy = REXMPP_TLS_AVOID;
+ }
+ }
+
+ s.sasl_property_cb = my_sasl_property_cb;
+ s.xml_in_cb = my_xml_in_cb;
+ s.xml_out_cb = my_xml_out_cb;
+
+ fd_set read_fds, write_fds;
+ int nfds;
+ struct timeval tv;
+ struct timeval *mtv;
+ int n = 0;
+
+ do {
+ err = rexmpp_run(&s, &read_fds, &write_fds);
+ if (err == REXMPP_SUCCESS) {
+ puts("done");
+ break;
+ }
+ if (err != REXMPP_E_AGAIN) {
+ printf("error: %s\n", rexmpp_strerror(err));
+ break;
+ }
+
+ if (stage == TEST_CONNECTING && s.stream_state == REXMPP_STREAM_READY) {
+ xmlNodePtr msg = rexmpp_xml_add_id(&s, xmlNewNode(NULL, "message"));
+ xmlNewNs(msg, "jabber:client", NULL);
+ xmlNewProp(msg, "to", jid);
+ xmlNewProp(msg, "type", "chat");
+ xmlNewTextChild(msg, NULL, "body", msg_text);
+ rexmpp_send(&s, msg);
+ stage = TEST_MESSAGE_SENT;
+ } else if (stage == TEST_MESSAGE_RECEIVED) {
+ rexmpp_stop(&s);
+ stage = TEST_DONE;
+ }
+
+ time_t now = time(NULL);
+ if (stage != TEST_DONE) {
+ if (stage != TEST_TIMEOUT && difftime(now, t) > TIMEOUT) {
+ puts("Timeout");
+ rexmpp_stop(&s);
+ stage = TEST_TIMEOUT;
+ } else if (stage == TEST_TIMEOUT && difftime(now, t) > TIMEOUT + 10) {
+ puts("Failed to close the stream properly, quitting");
+ rexmpp_done(&s);
+ return -1;
+ }
+ }
+
+ FD_ZERO(&read_fds);
+ FD_ZERO(&write_fds);
+ nfds = rexmpp_fds(&s, &read_fds, &write_fds);
+ tv.tv_sec = TIMEOUT;
+ tv.tv_usec = 0;
+ mtv = rexmpp_timeout(&s, (struct timeval*)&tv, (struct timeval*)&tv);
+
+ n = select(nfds, &read_fds, &write_fds, NULL, mtv);
+ if (n == -1) {
+ printf("select error: %s\n", strerror(errno));
+ break;
+ }
+ printf("stage = %u\n", stage);
+ } while (1);
+
+ rexmpp_done(&s);
+ return (stage != TEST_DONE);
+}