aboutsummaryrefslogtreecommitdiff
path: root/minimal-examples/api-tests
diff options
context:
space:
mode:
authorAndy Green <andy@warmcat.com>2019-06-26 14:32:46 +0100
committerAndy Green <andy@warmcat.com>2019-06-29 21:08:36 +0100
commita7e1bac4acb959ee9ffe38d5588eb24847bbe16c (patch)
tree0d9be6b7d11bb45673e24a6408f66269b0f41c92 /minimal-examples/api-tests
parent604a718e929954234b075fcdc4ff9667041d7db8 (diff)
downloadlibwebsockets-a7e1bac4acb959ee9ffe38d5588eb24847bbe16c.tar.gz
unit test sequencer
Diffstat (limited to 'minimal-examples/api-tests')
-rw-r--r--minimal-examples/api-tests/api-test-lws_sequencer/main.c2
-rw-r--r--minimal-examples/api-tests/api-test-smtp_client/CMakeLists.txt2
-rw-r--r--minimal-examples/api-tests/api-test-smtp_client/README.md26
-rw-r--r--minimal-examples/api-tests/api-test-smtp_client/main.c236
4 files changed, 189 insertions, 77 deletions
diff --git a/minimal-examples/api-tests/api-test-lws_sequencer/main.c b/minimal-examples/api-tests/api-test-lws_sequencer/main.c
index cbb3209d..2ec07e8b 100644
--- a/minimal-examples/api-tests/api-test-lws_sequencer/main.c
+++ b/minimal-examples/api-tests/api-test-lws_sequencer/main.c
@@ -370,7 +370,7 @@ main(int argc, const char **argv)
*/
seq = lws_sequencer_create(context, 0, sizeof(struct myseq),
- (void **)&s, sequencer_cb);
+ (void **)&s, sequencer_cb, "seq");
if (!seq) {
lwsl_err("%s: unable to create sequencer\n", __func__);
goto bail1;
diff --git a/minimal-examples/api-tests/api-test-smtp_client/CMakeLists.txt b/minimal-examples/api-tests/api-test-smtp_client/CMakeLists.txt
index 43f42469..4c8e671f 100644
--- a/minimal-examples/api-tests/api-test-smtp_client/CMakeLists.txt
+++ b/minimal-examples/api-tests/api-test-smtp_client/CMakeLists.txt
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 2.8)
include(CheckCSourceCompiles)
-set(SAMP lws-api-test-smtp_client)
+set(SAMP lws-unit-tests-smtp-client)
set(SRCS main.c)
# If we are being built as part of lws, confirm current build config supports
diff --git a/minimal-examples/api-tests/api-test-smtp_client/README.md b/minimal-examples/api-tests/api-test-smtp_client/README.md
index a3b3d01f..4c2052d9 100644
--- a/minimal-examples/api-tests/api-test-smtp_client/README.md
+++ b/minimal-examples/api-tests/api-test-smtp_client/README.md
@@ -1,6 +1,11 @@
# lws api test smtp client
-Demonstrates how to send email through your local MTA
+Performs unit tests on the lws SMTP client abstract protocol
+implementation.
+
+The first test "sends mail to a server" (actually is prompted by
+test vectors that look like a server) and the second test
+confirm it can handle rejection by the "server" cleanly.
## build
@@ -19,11 +24,18 @@ Commandline option|Meaning
```
- $ ./lws-api-test-smtp_client -r andy@warmcat.com
-[2019/04/17 05:12:06:5293] USER: LWS API selftest: SMTP client
-[2019/04/17 05:12:06:5635] NOTICE: LGSSMTP_IDLE: connecting to 127.0.0.1:25
-[2019/04/17 05:12:06:6238] NOTICE: email_sent_or_failed: sent OK
-[2019/04/17 05:12:06:6394] USER: Completed: PASS
-
+ $ ./lws-api-test-smtp_client
+[2019/06/28 21:56:41:0711] USER: LWS API selftest: SMTP client unit tests
+[2019/06/28 21:56:41:1114] NOTICE: test_sequencer_cb: test-seq: created
+[2019/06/28 21:56:41:1259] NOTICE: unit_test_sequencer_cb: unit-test-seq: created
+[2019/06/28 21:56:41:1272] NOTICE: lws_atcut_client_conn: smtp: test 'sending': start
+[2019/06/28 21:56:41:1441] NOTICE: unit_test_sequencer_cb: unit-test-seq: created
+[2019/06/28 21:56:41:1442] NOTICE: lws_atcut_client_conn: smtp: test 'rejected': start
+[2019/06/28 21:56:41:1453] NOTICE: lws_smtp_client_abs_rx: bad response from server: 500 (state 4) 500 Service Unavailable
+[2019/06/28 21:56:41:1467] USER: test_sequencer_cb: sequence completed OK
+[2019/06/28 21:56:41:1474] USER: main: 2 tests 0 fail
+[2019/06/28 21:56:41:1476] USER: test 0: PASS
+[2019/06/28 21:56:41:1478] USER: test 1: PASS
+[2019/06/28 21:56:41:1480] USER: Completed: PASS
```
diff --git a/minimal-examples/api-tests/api-test-smtp_client/main.c b/minimal-examples/api-tests/api-test-smtp_client/main.c
index d7a1d2d3..4b64334b 100644
--- a/minimal-examples/api-tests/api-test-smtp_client/main.c
+++ b/minimal-examples/api-tests/api-test-smtp_client/main.c
@@ -1,77 +1,190 @@
/*
- * lws-api-test-smtp_client
+ * lws-unit-tests-smtp-client
*
* Written in 2010-2019 by Andy Green <andy@warmcat.com>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
+ *
+ * This performs unit tests for the SMTP client abstract protocol
*/
#include <libwebsockets.h>
#include <signal.h>
-static int interrupted, result = 1;
-static const char *recip;
+static int interrupted, results[10], count_tests, count_passes;
-static void
-sigint_handler(int sig)
+static int
+email_sent_or_failed(struct lws_smtp_email *email, void *buf, size_t len)
{
- interrupted = 1;
+ free(email);
+
+ return 0;
}
+/*
+ * The test helper calls this on the instance it created to prepare it for
+ * the test. In our case, we need to queue up a test email to send on the
+ * smtp client abstract protocol.
+ */
+
static int
-email_sent_or_failed(struct lws_smtp_email *email, void *buf, size_t len)
+smtp_test_instance_init(lws_abs_t *instance)
{
- /* you could examine email->data here */
- if (buf)
- lwsl_notice("%s: %.*s\n", __func__, (int)len, (const char *)buf);
- else
- lwsl_notice("%s:\n", __func__);
+ lws_smtp_email_t *email = (lws_smtp_email_t *)
+ malloc(sizeof(*email) + 2048);
+
+ if (!email)
+ return 1;
- /* destroy any allocations in email */
+ /* attach an email to it */
- free((char *)email->payload);
+ memset(email, 0, sizeof(*email));
+ email->data = NULL /* email specific user data */;
+ email->email_from = "noreply@warmcat.com";
+ email->email_to = "andy@warmcat.com";
+ email->payload = (void *)&email[1];
- result = 0;
- interrupted = 1;
+ lws_snprintf((char *)email->payload, 2048,
+ "From: noreply@example.com\n"
+ "To: %s\n"
+ "Subject: Test email for lws smtp-client\n"
+ "\n"
+ "Hello this was an api test for lws smtp-client\n"
+ "\r\n.\r\n", "andy@warmcat.com");
+ email->done = email_sent_or_failed;
+
+ if (lws_smtp_client_add_email(instance, email)) {
+ lwsl_err("%s: failed to add email\n", __func__);
+ return 1;
+ }
return 0;
}
/*
- * We're going to bind to the raw-skt transport, so tell that what we want it
- * to connect to
+ * from https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
+ *
+ * test vector sent to protocol
+ * test vector received from protocol
*/
-static const lws_token_map_t smtp_raw_skt_transport_tokens[] = {
- {
- .u = { .value = "127.0.0.1" },
- .name_index = LTMI_PEER_V_DNS_ADDRESS,
- }, {
- .u = { .lvalue = 25 },
- .name_index = LTMI_PEER_LV_PORT,
- }, {
- }
+static lws_unit_test_packet_t test_send1[] = {
+ {
+ "220 smtp.example.com ESMTP Postfix",
+ smtp_test_instance_init, 34, LWS_AUT_EXPECT_RX
+ }, {
+ "HELO lws-test-client\x0a",
+ NULL, 21, LWS_AUT_EXPECT_TX
+ }, {
+ "250 smtp.example.com, I am glad to meet you",
+ NULL, 43, LWS_AUT_EXPECT_RX
+ }, {
+ "MAIL FROM: <noreply@warmcat.com>\x0a",
+ NULL, 33, LWS_AUT_EXPECT_TX
+ }, {
+ "250 Ok",
+ NULL, 6, LWS_AUT_EXPECT_RX
+ }, {
+ "RCPT TO: <andy@warmcat.com>\x0a",
+ NULL, 28, LWS_AUT_EXPECT_TX
+ }, {
+ "250 Ok",
+ NULL, 6, LWS_AUT_EXPECT_RX
+ }, {
+ "DATA\x0a",
+ NULL, 5, LWS_AUT_EXPECT_TX
+ }, {
+ "354 End data with <CR><LF>.<CR><LF>\x0a",
+ NULL, 35, LWS_AUT_EXPECT_RX
+ }, {
+ "From: noreply@example.com\n"
+ "To: andy@warmcat.com\n"
+ "Subject: Test email for lws smtp-client\n"
+ "\n"
+ "Hello this was an api test for lws smtp-client\n"
+ "\r\n.\r\n",
+ NULL, 27 + 21 + 39 + 1 + 47 + 5, LWS_AUT_EXPECT_TX
+ }, {
+ "250 Ok: queued as 12345\x0a",
+ NULL, 23, LWS_AUT_EXPECT_RX
+ }, {
+ "quit\x0a",
+ NULL, 5, LWS_AUT_EXPECT_TX
+ }, {
+ "221 Bye\x0a",
+ NULL, 7, LWS_AUT_EXPECT_RX |
+ LWS_AUT_EXPECT_LOCAL_CLOSE |
+ LWS_AUT_EXPECT_DO_REMOTE_CLOSE |
+ LWS_AUT_EXPECT_TEST_END
+ }, { /* sentinel */
+
+ }
};
+
+static lws_unit_test_packet_t test_send2[] = {
+ {
+ "220 smtp.example.com ESMTP Postfix",
+ smtp_test_instance_init, 34, LWS_AUT_EXPECT_RX
+ }, {
+ "HELO lws-test-client\x0a",
+ NULL, 21, LWS_AUT_EXPECT_TX
+ }, {
+ "250 smtp.example.com, I am glad to meet you",
+ NULL, 43, LWS_AUT_EXPECT_RX
+ }, {
+ "MAIL FROM: <noreply@warmcat.com>\x0a",
+ NULL, 33, LWS_AUT_EXPECT_TX
+ }, {
+ "500 Service Unavailable",
+ NULL, 23, LWS_AUT_EXPECT_RX |
+ LWS_AUT_EXPECT_DO_REMOTE_CLOSE |
+ LWS_AUT_EXPECT_TEST_END
+ }, { /* sentinel */
+
+ }
+};
+
+static lws_unit_test_t tests[] = {
+ { "sending", test_send1, 3 },
+ { "rejected", test_send2, 3 },
+ { } /* sentinel */
+};
+
+static void
+sigint_handler(int sig)
+{
+ interrupted = 1;
+}
+
+/*
+ * set the HELO our SMTP client will use
+ */
+
static const lws_token_map_t smtp_protocol_tokens[] = {
{
.u = { .value = "lws-test-client" },
.name_index = LTMI_PSMTP_V_HELO,
- }, {
+ }, { /* sentinel */
}
};
+void
+tests_completion_cb(const void *cb_user)
+{
+ interrupted = 1;
+}
int main(int argc, const char **argv)
{
int n = 1, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE;
struct lws_context_creation_info info;
+ lws_test_sequencer_args_t args;
struct lws_context *context;
- lws_abs_t abs, *instance;
- lws_smtp_email_t email;
struct lws_vhost *vh;
+ lws_abs_t abs, *instance;
const char *p;
/* the normal lws init */
@@ -81,15 +194,8 @@ int main(int argc, const char **argv)
if ((p = lws_cmdline_option(argc, argv, "-d")))
logs = atoi(p);
- p = lws_cmdline_option(argc, argv, "-r");
- if (!p) {
- lwsl_err("-r <recipient email> is required\n");
- return 1;
- }
- recip = p;
-
lws_set_log_level(logs, NULL);
- lwsl_user("LWS API selftest: SMTP client\n");
+ lwsl_user("LWS API selftest: SMTP client unit tests\n");
memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
info.port = CONTEXT_PORT_NO_LISTEN;
@@ -107,9 +213,7 @@ int main(int argc, const char **argv)
goto bail1;
}
- /*
- * create an smtp client that's hooked up to real sockets
- */
+ /* create the smtp client */
memset(&abs, 0, sizeof(abs));
abs.vh = vh;
@@ -124,39 +228,28 @@ int main(int argc, const char **argv)
/* select the transport and bind its tokens */
- abs.at = lws_abs_transport_get_by_name("raw_skt");
+ abs.at = lws_abs_transport_get_by_name("unit_test");
if (!abs.at)
goto bail1;
- abs.at_tokens = smtp_raw_skt_transport_tokens;
-
instance = lws_abs_bind_and_create_instance(&abs);
if (!instance)
goto bail1;
- /* attach an email to it */
-
- memset(&email, 0, sizeof(email));
- email.data = NULL /* email specific user data */;
- email.email_from = "andy@warmcat.com";
- email.email_to = recip;
- email.payload = malloc(2048);
- if (!email.payload) {
- goto bail1;
- }
+ /* configure the test sequencer */
- lws_snprintf((char *)email.payload, 2048,
- "From: noreply@example.com\n"
- "To: %s\n"
- "Subject: Test email for lws smtp-client\n"
- "\n"
- "Hello this was an api test for lws smtp-client\n"
- "\r\n.\r\n", recip);
- email.done = email_sent_or_failed;
+ args.abs = &abs;
+ args.tests = tests;
+ args.results = results;
+ args.results_max = LWS_ARRAY_SIZE(results);
+ args.count_tests = &count_tests;
+ args.count_passes = &count_passes;
+ args.cb = tests_completion_cb;
+ args.cb_user = NULL;
- if (lws_smtp_client_add_email(instance, &email)) {
- lwsl_err("%s: failed to add email\n", __func__);
- goto bail;
+ if (lws_abs_unit_test_sequencer(&args)) {
+ lwsl_err("%s: failed to create test sequencer\n", __func__);
+ goto bail1;
}
/* the usual lws event loop */
@@ -164,12 +257,19 @@ int main(int argc, const char **argv)
while (n >= 0 && !interrupted)
n = lws_service(context, 1000);
-bail:
+ /* describe the overall test results */
+
+ lwsl_user("%s: %d tests %d fail\n", __func__, count_tests,
+ count_tests - count_passes);
+ for (n = 0; n < count_tests; n++)
+ lwsl_user(" test %d: %s\n", n,
+ lws_unit_test_result_name(results[n]));
bail1:
- lwsl_user("Completed: %s\n", result ? "FAIL" : "PASS");
+ lwsl_user("Completed: %s\n",
+ !count_tests || count_passes != count_tests ? "FAIL" : "PASS");
lws_context_destroy(context);
- return result;
+ return !count_tests || count_passes != count_tests;
}