aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-01-24 17:38:16 -0800
committerGarrett D'Amore <garrett@damore.org>2018-02-01 16:11:38 -0800
commit3dae30ed5e543dc73fc993334ef56b9b157b9b3c (patch)
treed7e294b5d544aa18e8fc8749abfe605a05fa4bd7 /tests
parent5914e40c2ff7fcf346c90705785f3fb7650a9fdc (diff)
downloadnng-3dae30ed5e543dc73fc993334ef56b9b157b9b3c.tar.gz
nng-3dae30ed5e543dc73fc993334ef56b9b157b9b3c.tar.bz2
nng-3dae30ed5e543dc73fc993334ef56b9b157b9b3c.zip
fixes #173 Define public HTTP server API
This introduces enough of the HTTP API to support fully server applications, including creation of websocket style protocols, pluggable handlers, and so forth. We have also introduced scatter/gather I/O (rudimentary) for aios, and made other enhancements to the AIO framework. The internals of the AIOs themselves are now fully private, and we have eliminated the aio->a_addr member, with plans to remove the pipe and possibly message members as well. A few other minor issues were found and fixed as well. The HTTP API includes request, response, and connection objects, which can be used with both servers and clients. It also defines the HTTP server and handler objects, which support server applications. Support for client applications will require a client object to be exposed, and that should be happening shortly. None of this is "documented" yet, bug again, we will follow up shortly.
Diffstat (limited to 'tests')
-rw-r--r--tests/aio.c4
-rw-r--r--tests/convey.c22
-rw-r--r--tests/convey.h2
-rw-r--r--tests/httpclient.c46
-rw-r--r--tests/httpserver.c324
-rw-r--r--tests/resolv.c100
-rw-r--r--tests/trantest.h1
-rw-r--r--tests/udp.c205
-rw-r--r--tests/zt.c31
9 files changed, 404 insertions, 331 deletions
diff --git a/tests/aio.c b/tests/aio.c
index 0746fa51..fc5d4baf 100644
--- a/tests/aio.c
+++ b/tests/aio.c
@@ -1,6 +1,6 @@
//
-// Copyright 2017 Garrett D'Amore <garrett@damore.org>
-// Copyright 2017 Capitar IT Group BV <info@capitar.com>
+// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
diff --git a/tests/convey.c b/tests/convey.c
index bfe836e4..5e87779f 100644
--- a/tests/convey.c
+++ b/tests/convey.c
@@ -76,14 +76,13 @@
* a scope not expressed to user code, these rules are relaxed.
*/
-static const char *convey_sym_pass = ".";
-static const char *convey_sym_skip = "?";
-static const char *convey_sym_fail = "X";
-static const char *convey_sym_fatal = "!";
-static const char *convey_nocolor = "";
-static const char *convey_green = "";
-static const char *convey_red = "";
-static const char *convey_yellow = "";
+static const char *convey_sym_pass = ".";
+static const char *convey_sym_skip = "?";
+static const char *convey_sym_fail = "X";
+static const char *convey_nocolor = "";
+static const char *convey_green = "";
+static const char *convey_red = "";
+static const char *convey_yellow = "";
static int convey_debug = 0;
static int convey_verbose = 0;
@@ -866,10 +865,9 @@ convey_init_term(void)
(void) setlocale(LC_ALL, "");
codeset = nl_langinfo(CODESET);
if ((codeset != NULL) && (strcmp(codeset, "UTF-8") == 0)) {
- convey_sym_pass = "✔";
- convey_sym_fail = "✘";
- convey_sym_fatal = "🔥";
- convey_sym_skip = "⚠";
+ convey_sym_pass = "✔";
+ convey_sym_fail = "✘";
+ convey_sym_skip = "⚠";
}
term = getenv("TERM");
if (!isatty(fileno(stdin))) {
diff --git a/tests/convey.h b/tests/convey.h
index 59445059..9d9be0fa 100644
--- a/tests/convey.h
+++ b/tests/convey.h
@@ -185,7 +185,7 @@ extern void conveyPrintf(const char *, int, const char *, ...);
*/
#define ConveyTest(name, code) \
do { \
- int convey_rv; \
+ int convey_rv = 0; \
conveyRun(name, code, &convey_rv); \
if (convey_rv > convey_main_rv) { \
convey_main_rv = convey_rv; \
diff --git a/tests/httpclient.c b/tests/httpclient.c
index f965cf2d..f26674f6 100644
--- a/tests/httpclient.c
+++ b/tests/httpclient.c
@@ -31,52 +31,46 @@ TestMain("HTTP Client", {
Convey("Given a TCP connection to httpbin.org", {
nng_aio * aio;
- nni_aio * iaio;
nni_http_client *cli;
- nni_http * http;
- nni_url * url;
+ nng_http_conn * http;
+ nng_url * url;
So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- iaio = (nni_aio *) aio;
- So(nni_url_parse(&url, "http://httpbin.org") == 0);
+ So(nng_url_parse(&url, "http://httpbin.org/encoding/utf8") ==
+ 0);
So(nni_http_client_init(&cli, url) == 0);
- nni_http_client_connect(cli, iaio);
+ nni_http_client_connect(cli, aio);
nng_aio_wait(aio);
So(nng_aio_result(aio) == 0);
- http = nni_aio_get_output(iaio, 0);
+ http = nni_aio_get_output(aio, 0);
Reset({
nni_http_client_fini(cli);
- nni_http_fini(http);
+ nng_http_conn_close(http);
nng_aio_free(aio);
- nni_url_free(url);
+ nng_url_free(url);
});
Convey("We can initiate a message", {
- nni_http_req *req;
- nni_http_res *res;
+ nng_http_req *req;
+ nng_http_res *res;
+
So(http != NULL);
- So(nni_http_req_init(&req) == 0);
- So(nni_http_res_init(&res) == 0);
+ So(nng_http_req_alloc(&req, url) == 0);
+ So(nng_http_res_alloc(&res) == 0);
Reset({
- nni_http_close(http);
- nni_http_req_fini(req);
- nni_http_res_fini(res);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
});
- So(nni_http_req_set_method(req, "GET") == 0);
- So(nni_http_req_set_version(req, "HTTP/1.1") == 0);
- So(nni_http_req_set_uri(req, "/encoding/utf8") == 0);
- So(nni_http_req_set_header(
- req, "Host", "httpbin.org") == 0);
- nni_http_write_req(http, req, iaio);
+ nng_http_conn_write_req(http, req, aio);
nng_aio_wait(aio);
So(nng_aio_result(aio) == 0);
- nni_http_read_res(http, res, iaio);
+ nng_http_conn_read_res(http, res, aio);
nng_aio_wait(aio);
So(nng_aio_result(aio) == 0);
- So(nni_http_res_get_status(res) == 200);
+ So(nng_http_res_get_status(res) == 200);
Convey("The message contents are correct", {
uint8_t digest[20];
@@ -85,7 +79,7 @@ TestMain("HTTP Client", {
size_t sz;
nng_iov iov;
- cstr = nni_http_res_get_header(
+ cstr = nng_http_res_get_header(
res, "Content-Length");
So(cstr != NULL);
sz = atoi(cstr);
@@ -102,7 +96,7 @@ TestMain("HTTP Client", {
nng_aio_wait(aio);
So(nng_aio_result(aio) == 0);
- nni_http_read_full(http, iaio);
+ nng_http_conn_read_all(http, aio);
nng_aio_wait(aio);
So(nng_aio_result(aio) == 0);
diff --git a/tests/httpserver.c b/tests/httpserver.c
index 062f964a..1008058f 100644
--- a/tests/httpserver.c
+++ b/tests/httpserver.c
@@ -30,56 +30,43 @@ cleanup(void)
}
static int
-httpget(const char *addr, void **datap, size_t *sizep, uint16_t *statp,
- char **ctypep)
+httpdo(nng_url *url, nng_http_req *req, nng_http_res *res, void **datap,
+ size_t *sizep)
{
int rv;
- nni_aio * aio = NULL;
- nni_http_client *cli = NULL;
- nni_http * h = NULL;
- nni_http_req * req = NULL;
- nni_http_res * res = NULL;
- nni_url * url = NULL;
- size_t clen = 0;
- void * data = NULL;
- char * ctype = NULL;
+ nng_aio * aio = NULL;
+ nni_http_client *cli = NULL;
+ nng_http_conn * h = NULL;
+ size_t clen = 0;
+ void * data = NULL;
const char * ptr;
- if (((rv = nni_url_parse(&url, addr)) != 0) ||
- ((rv = nni_aio_init(&aio, NULL, NULL)) != 0) ||
- ((rv = nni_http_req_init(&req)) != 0) ||
- ((rv = nni_http_res_init(&res)) != 0) ||
+ if (((rv = nng_aio_alloc(&aio, NULL, NULL)) != 0) ||
((rv = nni_http_client_init(&cli, url)) != 0)) {
goto fail;
}
nni_http_client_connect(cli, aio);
- nni_aio_wait(aio);
+ nng_aio_wait(aio);
if ((rv = nni_aio_result(aio)) != 0) {
goto fail;
}
h = nni_aio_get_output(aio, 0);
- if (((rv = nni_http_req_set_method(req, "GET")) != 0) ||
- ((rv = nni_http_req_set_version(req, "HTTP/1.1")) != 0) ||
- ((rv = nni_http_req_set_uri(req, url->u_path)) != 0) ||
- ((rv = nni_http_req_set_header(req, "Host", url->u_host)) != 0)) {
- goto fail;
- }
- nni_http_write_req(h, req, aio);
- nni_aio_wait(aio);
- if ((rv = nni_aio_result(aio)) != 0) {
+
+ nng_http_conn_write_req(h, req, aio);
+ nng_aio_wait(aio);
+ if ((rv = nng_aio_result(aio)) != 0) {
goto fail;
}
- nni_http_read_res(h, res, aio);
- nni_aio_wait(aio);
- if ((rv = nni_aio_result(aio)) != 0) {
+ nng_http_conn_read_res(h, res, aio);
+ nng_aio_wait(aio);
+ if ((rv = nng_aio_result(aio)) != 0) {
goto fail;
}
- *statp = nni_http_res_get_status(res);
- clen = 0;
- if ((*statp == NNI_HTTP_STATUS_OK) &&
- ((ptr = nni_http_res_get_header(res, "Content-Length")) != NULL)) {
+ clen = 0;
+ if ((nng_http_res_get_status(res) == NNG_HTTP_STATUS_OK) &&
+ ((ptr = nng_http_res_get_header(res, "Content-Length")) != NULL)) {
clen = atoi(ptr);
}
@@ -88,13 +75,57 @@ httpget(const char *addr, void **datap, size_t *sizep, uint16_t *statp,
data = nni_alloc(clen);
iov.iov_buf = data;
iov.iov_len = clen;
- nni_aio_set_iov(aio, 1, &iov);
- nni_http_read_full(h, aio);
- nni_aio_wait(aio);
- if ((rv = nni_aio_result(aio)) != 0) {
+ nng_aio_set_iov(aio, 1, &iov);
+ nng_http_conn_read_all(h, aio);
+ nng_aio_wait(aio);
+ if ((rv = nng_aio_result(aio)) != 0) {
goto fail;
}
- if ((ptr = nni_http_res_get_header(res, "Content-Type")) !=
+ }
+
+ *datap = data;
+ *sizep = clen;
+
+fail:
+ if (aio != NULL) {
+ nng_aio_free(aio);
+ }
+ if (h != NULL) {
+ nng_http_conn_close(h);
+ }
+ if (cli != NULL) {
+ nni_http_client_fini(cli);
+ }
+
+ return (rv);
+}
+
+static int
+httpget(const char *addr, void **datap, size_t *sizep, uint16_t *statp,
+ char **ctypep)
+{
+ int rv;
+ nng_http_req *req = NULL;
+ nng_http_res *res = NULL;
+ nng_url * url = NULL;
+ size_t clen = 0;
+ void * data = NULL;
+ char * ctype = NULL;
+ const char * ptr;
+
+ if (((rv = nng_url_parse(&url, addr)) != 0) ||
+ ((rv = nng_http_req_alloc(&req, url)) != 0) ||
+ ((rv = nng_http_res_alloc(&res)) != 0)) {
+ goto fail;
+ }
+ if ((rv = httpdo(url, req, res, &data, &clen)) != 0) {
+ goto fail;
+ }
+
+ *statp = nng_http_res_get_status(res);
+
+ if (clen > 0) {
+ if ((ptr = nng_http_res_get_header(res, "Content-Type")) !=
NULL) {
ctype = nni_strdup(ptr);
}
@@ -114,100 +145,86 @@ fail:
if (url != NULL) {
nni_url_free(url);
}
- if (aio != NULL) {
- nni_aio_fini(aio);
- }
if (req != NULL) {
- nni_http_req_fini(req);
+ nng_http_req_free(req);
}
if (res != NULL) {
- nni_http_res_fini(res);
- }
- if (h != NULL) {
- nni_http_fini(h);
- }
- if (cli != NULL) {
- nni_http_client_fini(cli);
+ nng_http_res_free(res);
}
return (rv);
}
-TestMain("HTTP Client", {
+TestMain("HTTP Server", {
- nni_http_server * s;
- nni_http_handler *h;
+ nng_http_server * s;
+ nng_http_handler *h;
nni_init();
atexit(cleanup);
Convey("We can start an HTTP server", {
- nni_aio *aio;
+ nng_aio *aio;
char portbuf[16];
char urlstr[32];
- nni_url *url;
+ nng_url *url;
trantest_next_address(portbuf, "%u");
snprintf(
urlstr, sizeof(urlstr), "http://127.0.0.1:%s", portbuf);
- So(nni_url_parse(&url, urlstr) == 0);
- So(nni_aio_init(&aio, NULL, NULL) == 0);
+ So(nng_url_parse(&url, urlstr) == 0);
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
- So(nni_http_server_init(&s, url) == 0);
+ So(nng_http_server_hold(&s, url) == 0);
Reset({
- nni_aio_fini(aio);
- nni_http_server_fini(s);
- nni_url_free(url);
+ nng_aio_free(aio);
+ nng_http_server_release(s);
+ nng_url_free(url);
});
- So(nni_http_handler_init_static(&h, "/home.html", doc1,
+ So(nng_http_handler_alloc_static(&h, "/home.html", doc1,
strlen(doc1), "text/html") == 0);
- So(nni_http_server_add_handler(s, h) == 0);
- So(nni_http_server_start(s) == 0);
+ So(nng_http_server_add_handler(s, h) == 0);
+ So(nng_http_server_start(s) == 0);
Convey("We can connect a client to it", {
nni_http_client *cli;
- nni_http * h;
- nni_http_req * req;
- nni_http_res * res;
+ nng_http_conn * h;
+ nng_http_req * req;
+ nng_http_res * res;
So(nni_http_client_init(&cli, url) == 0);
nni_http_client_connect(cli, aio);
- nni_aio_wait(aio);
+ nng_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ So(nng_aio_result(aio) == 0);
h = nni_aio_get_output(aio, 0);
So(h != NULL);
- So(nni_http_req_init(&req) == 0);
- So(nni_http_res_init(&res) == 0);
+ So(nng_http_req_alloc(&req, url) == 0);
+ So(nng_http_res_alloc(&res) == 0);
Reset({
nni_http_client_fini(cli);
- nni_http_fini(h);
- nni_http_req_fini(req);
- nni_http_res_fini(res);
+ nng_http_conn_close(h);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
});
Convey("404 works", {
- So(nni_http_req_set_method(req, "GET") == 0);
- So(nni_http_req_set_version(req, "HTTP/1.1") ==
- 0);
- So(nni_http_req_set_uri(req, "/bogus") == 0);
- So(nni_http_req_set_header(
- req, "Host", "localhost") == 0);
- nni_http_write_req(h, req, aio);
+ So(nng_http_req_set_uri(req, "/bogus") == 0);
+ nng_http_conn_write_req(h, req, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
- nni_http_read_res(h, res, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_http_conn_read_res(h, res, aio);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
- So(nni_http_res_get_status(res) == 404);
+ So(nng_http_res_get_status(res) == 404);
});
Convey("Valid data works", {
@@ -215,46 +232,38 @@ TestMain("HTTP Client", {
const void *ptr;
nng_iov iov;
- So(nni_http_req_set_method(req, "GET") == 0);
- So(nni_http_req_set_version(req, "HTTP/1.1") ==
- 0);
- So(nni_http_req_set_uri(req, "/home.html") ==
+ So(nng_http_req_set_uri(req, "/home.html") ==
0);
- So(nni_http_req_set_header(
- req, "Host", "localhost") == 0);
- nni_http_write_req(h, req, aio);
+ nng_http_conn_write_req(h, req, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
- nni_http_read_res(h, res, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_http_conn_read_res(h, res, aio);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
- So(nni_http_res_get_status(res) == 200);
+ So(nng_http_res_get_status(res) == 200);
- ptr = nni_http_res_get_header(
+ ptr = nng_http_res_get_header(
res, "Content-Length");
So(ptr != NULL);
So(atoi(ptr) == strlen(doc1));
iov.iov_len = strlen(doc1);
iov.iov_buf = chunk;
- So(nni_aio_set_iov(aio, 1, &iov) == 0);
- nni_http_read_full(h, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
- So(nni_aio_count(aio) == strlen(doc1));
+ So(nng_aio_set_iov(aio, 1, &iov) == 0);
+ nng_http_conn_read_all(h, aio);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
+ So(nng_aio_count(aio) == strlen(doc1));
So(memcmp(chunk, doc1, strlen(doc1)) == 0);
});
-
});
});
Convey("Directory serving works", {
- nni_aio *aio;
- char portbuf[16];
char urlstr[32];
- nni_url *url;
+ nng_url *url;
char * tmpdir;
char * workdir;
char * file1;
@@ -264,9 +273,8 @@ TestMain("HTTP Client", {
char * subdir2;
trantest_next_address(urlstr, "http://127.0.0.1:%u");
- So(nni_url_parse(&url, urlstr) == 0);
- So(nni_aio_init(&aio, NULL, NULL) == 0);
- So(nni_http_server_init(&s, url) == 0);
+ So(nng_url_parse(&url, urlstr) == 0);
+ So(nng_http_server_hold(&s, url) == 0);
So((tmpdir = nni_plat_temp_dir()) != NULL);
So((workdir = nni_file_join(tmpdir, "httptest")) != NULL);
So((subdir1 = nni_file_join(workdir, "subdir1")) != NULL);
@@ -280,8 +288,7 @@ TestMain("HTTP Client", {
So(nni_file_put(file3, doc3, strlen(doc3)) == 0);
Reset({
- nni_aio_fini(aio);
- nni_http_server_fini(s);
+ nng_http_server_release(s);
nni_strfree(tmpdir);
nni_file_delete(file1);
nni_file_delete(file2);
@@ -295,12 +302,13 @@ TestMain("HTTP Client", {
nni_strfree(file3);
nni_strfree(subdir1);
nni_strfree(subdir2);
- nni_url_free(url);
+ nng_url_free(url);
});
- So(nni_http_handler_init_directory(&h, "/docs", workdir) == 0);
- So(nni_http_server_add_handler(s, h) == 0);
- So(nni_http_server_start(s) == 0);
+ So(nng_http_handler_alloc_directory(&h, "/docs", workdir) ==
+ 0);
+ So(nng_http_server_add_handler(s, h) == 0);
+ So(nng_http_server_start(s) == 0);
nng_msleep(100);
Convey("Index.html works", {
@@ -313,12 +321,12 @@ TestMain("HTTP Client", {
snprintf(fullurl, sizeof(fullurl),
"%s/docs/subdir1/index.html", urlstr);
So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
- So(stat == NNI_HTTP_STATUS_OK);
+ So(stat == NNG_HTTP_STATUS_OK);
So(size == strlen(doc1));
So(memcmp(data, doc1, size) == 0);
So(strcmp(ctype, "text/html") == 0);
nni_strfree(ctype);
- nni_free(data, size);
+ nng_free(data, size);
});
Convey("Index.htm works", {
@@ -331,12 +339,12 @@ TestMain("HTTP Client", {
snprintf(fullurl, sizeof(fullurl), "%s/docs/subdir2",
urlstr);
So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
- So(stat == NNI_HTTP_STATUS_OK);
+ So(stat == NNG_HTTP_STATUS_OK);
So(size == strlen(doc3));
So(memcmp(data, doc3, size) == 0);
So(strcmp(ctype, "text/html") == 0);
nni_strfree(ctype);
- nni_free(data, size);
+ nng_free(data, size);
});
Convey("Named file works", {
@@ -349,12 +357,12 @@ TestMain("HTTP Client", {
snprintf(fullurl, sizeof(fullurl), "%s/docs/file.txt",
urlstr);
So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
- So(stat == NNI_HTTP_STATUS_OK);
+ So(stat == NNG_HTTP_STATUS_OK);
So(size == strlen(doc2));
So(memcmp(data, doc2, size) == 0);
So(strcmp(ctype, "text/plain") == 0);
nni_strfree(ctype);
- nni_free(data, size);
+ nng_free(data, size);
});
Convey("Missing index gives 404", {
@@ -366,8 +374,76 @@ TestMain("HTTP Client", {
snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr);
So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
- So(stat == NNI_HTTP_STATUS_NOT_FOUND);
+ So(stat == NNG_HTTP_STATUS_NOT_FOUND);
So(size == 0);
});
+
+ Convey("Bad method gives 405", {
+ char fullurl[256];
+ void * data;
+ size_t size;
+ nng_http_req *req;
+ nng_http_res *res;
+ nng_url * curl;
+
+ So(nng_http_res_alloc(&res) == 0);
+ snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr);
+ So(nng_url_parse(&curl, fullurl) == 0);
+ So(nng_http_req_alloc(&req, curl) == 0);
+ So(nng_http_req_set_method(req, "POST") == 0);
+
+ So(httpdo(curl, req, res, &data, &size) == 0);
+ So(nng_http_res_get_status(res) ==
+ NNG_HTTP_STATUS_METHOD_NOT_ALLOWED);
+ So(size == 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+ Convey("Version 0.9 gives 505", {
+ char fullurl[256];
+ void * data;
+ size_t size;
+ nng_http_req *req;
+ nng_http_res *res;
+ nng_url * curl;
+
+ So(nng_http_res_alloc(&res) == 0);
+ snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr);
+ So(nng_url_parse(&curl, fullurl) == 0);
+ So(nng_http_req_alloc(&req, curl) == 0);
+ So(nng_http_req_set_version(req, "HTTP/0.9") == 0);
+
+ So(httpdo(curl, req, res, &data, &size) == 0);
+ So(nng_http_res_get_status(res) ==
+ NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP);
+ So(size == 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+ Convey("Missing Host gives 400", {
+ char fullurl[256];
+ void * data;
+ size_t size;
+ nng_http_req *req;
+ nng_http_res *res;
+ nng_url * curl;
+
+ So(nng_http_res_alloc(&res) == 0);
+ snprintf(fullurl, sizeof(fullurl), "%s/docs/", urlstr);
+ So(nng_url_parse(&curl, fullurl) == 0);
+ So(nng_http_req_alloc(&req, curl) == 0);
+ So(nng_http_req_del_header(req, "Host") == 0);
+
+ So(httpdo(curl, req, res, &data, &size) == 0);
+ So(nng_http_res_get_status(res) ==
+ NNG_HTTP_STATUS_BAD_REQUEST);
+ So(size == 0);
+ nng_http_req_free(req);
+ nng_http_res_free(res);
+ nng_url_free(curl);
+ });
+
});
})
diff --git a/tests/resolv.c b/tests/resolv.c
index d1d3b2fe..607b3b11 100644
--- a/tests/resolv.c
+++ b/tests/resolv.c
@@ -1,6 +1,6 @@
//
-// Copyright 2017 Garrett D'Amore <garrett@damore.org>
-// Copyright 2017 Capitar IT Group BV <info@capitar.com>
+// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
@@ -73,57 +73,57 @@ TestMain("Resolver", {
nni_init();
Convey("Google DNS IPv4 resolves", {
- nni_aio * aio;
+ nng_aio * aio;
const char * str;
nng_sockaddr sa;
- nni_aio_init(&aio, NULL, NULL);
- aio->a_addr = &sa;
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
+ nng_aio_set_input(aio, 0, &sa);
nni_plat_tcp_resolv("google-public-dns-a.google.com", "80",
NNG_AF_INET, 1, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
So(sa.s_un.s_in.sa_family == NNG_AF_INET);
So(sa.s_un.s_in.sa_port == ntohs(80));
str = ip4tostr(&sa.s_un.s_in.sa_addr);
So(strcmp(str, "8.8.8.8") == 0);
- nni_aio_fini(aio);
+ nng_aio_free(aio);
});
Convey("Numeric UDP resolves", {
- nni_aio * aio;
+ nng_aio * aio;
const char * str;
nng_sockaddr sa;
- nni_aio_init(&aio, NULL, NULL);
- aio->a_addr = &sa;
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
+ nng_aio_set_input(aio, 0, &sa);
nni_plat_udp_resolv("8.8.4.4", "69", NNG_AF_INET, 1, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
So(sa.s_un.s_in.sa_family == NNG_AF_INET);
So(sa.s_un.s_in.sa_port == ntohs(69));
str = ip4tostr(&sa.s_un.s_in.sa_addr);
So(strcmp(str, "8.8.4.4") == 0);
- nni_aio_fini(aio);
+ nng_aio_free(aio);
});
Convey("Numeric v4 resolves", {
- nni_aio * aio;
+ nng_aio * aio;
const char * str;
nng_sockaddr sa;
- nni_aio_init(&aio, NULL, NULL);
- aio->a_addr = &sa;
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
+ nng_aio_set_input(aio, 0, &sa);
nni_plat_tcp_resolv("8.8.4.4", "80", NNG_AF_INET, 1, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
So(sa.s_un.s_in.sa_family == NNG_AF_INET);
So(sa.s_un.s_in.sa_port == ntohs(80));
str = ip4tostr(&sa.s_un.s_in.sa_addr);
So(strcmp(str, "8.8.4.4") == 0);
- nni_aio_fini(aio);
+ nng_aio_free(aio);
});
Convey("Numeric v6 resolves", {
- nni_aio * aio;
+ nng_aio * aio;
const char * str;
nng_sockaddr sa;
@@ -135,80 +135,80 @@ TestMain("Resolver", {
ConveySkip("IPv6 missing from CI provider");
}
- nni_aio_init(&aio, NULL, NULL);
- aio->a_addr = &sa;
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
+ nng_aio_set_input(aio, 0, &sa);
nni_plat_tcp_resolv("::1", "80", NNG_AF_INET6, 1, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
So(sa.s_un.s_in6.sa_family == NNG_AF_INET6);
So(sa.s_un.s_in6.sa_port == ntohs(80));
str = ip6tostr(&sa.s_un.s_in6.sa_addr);
So(strcmp(str, "::1") == 0);
- nni_aio_fini(aio);
+ nng_aio_free(aio);
});
Convey("TCP Name service resolves", {
- nni_aio * aio;
+ nng_aio * aio;
const char * str;
nng_sockaddr sa;
- nni_aio_init(&aio, NULL, NULL);
- aio->a_addr = &sa;
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
+ nng_aio_set_input(aio, 0, &sa);
nni_plat_tcp_resolv("8.8.4.4", "http", NNG_AF_INET, 1, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
So(sa.s_un.s_in.sa_family == NNG_AF_INET);
So(sa.s_un.s_in.sa_port == ntohs(80));
str = ip4tostr(&sa.s_un.s_in.sa_addr);
So(strcmp(str, "8.8.4.4") == 0);
- nni_aio_fini(aio);
+ nng_aio_free(aio);
});
Convey("UDP Name service resolves", {
- nni_aio * aio;
+ nng_aio * aio;
const char * str;
nng_sockaddr sa;
- nni_aio_init(&aio, NULL, NULL);
- aio->a_addr = &sa;
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
+ nng_aio_set_input(aio, 0, &sa);
nni_plat_udp_resolv("8.8.4.4", "tftp", NNG_AF_INET, 1, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
So(sa.s_un.s_in.sa_family == NNG_AF_INET);
So(sa.s_un.s_in.sa_port == ntohs(69));
str = ip4tostr(&sa.s_un.s_in.sa_addr);
So(strcmp(str, "8.8.4.4") == 0);
- nni_aio_fini(aio);
+ nng_aio_free(aio);
});
Convey("Localhost IPv4 resolves", {
- nni_aio * aio;
+ nng_aio * aio;
const char * str;
nng_sockaddr sa;
- nni_aio_init(&aio, NULL, NULL);
- aio->a_addr = &sa;
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
+ nng_aio_set_input(aio, 0, &sa);
nni_plat_tcp_resolv("localhost", "80", NNG_AF_INET, 1, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
So(sa.s_un.s_in.sa_family == NNG_AF_INET);
So(sa.s_un.s_in.sa_port == ntohs(80));
So(sa.s_un.s_in.sa_addr == ntohl(0x7f000001));
str = ip4tostr(&sa.s_un.s_in.sa_addr);
So(strcmp(str, "127.0.0.1") == 0);
- nni_aio_fini(aio);
+ nng_aio_free(aio);
});
Convey("Localhost UNSPEC resolves", {
- nni_aio * aio;
+ nng_aio * aio;
const char * str;
nng_sockaddr sa;
- nni_aio_init(&aio, NULL, NULL);
- aio->a_addr = &sa;
+ So(nng_aio_alloc(&aio, NULL, NULL) == 0);
+ nng_aio_set_input(aio, 0, &sa);
nni_plat_tcp_resolv("localhost", "80", NNG_AF_UNSPEC, 1, aio);
- nni_aio_wait(aio);
- So(nni_aio_result(aio) == 0);
+ nng_aio_wait(aio);
+ So(nng_aio_result(aio) == 0);
So((sa.s_un.s_family == NNG_AF_INET) ||
(sa.s_un.s_family == NNG_AF_INET6));
switch (sa.s_un.s_family) {
@@ -224,7 +224,7 @@ TestMain("Resolver", {
So(strcmp(str, "::1") == 0);
break;
}
- nni_aio_fini(aio);
+ nng_aio_free(aio);
});
nni_fini();
diff --git a/tests/trantest.h b/tests/trantest.h
index 80f59ff9..771b761e 100644
--- a/tests/trantest.h
+++ b/tests/trantest.h
@@ -220,7 +220,6 @@ trantest_conn_refused(trantest *tt)
Convey("Connection refused works", {
nng_dialer d = 0;
- int rv = trantest_dial(tt, &d);
So(trantest_dial(tt, &d) == NNG_ECONNREFUSED);
So(d == 0);
So(trantest_dial(tt, &d) == NNG_ECONNREFUSED);
diff --git a/tests/udp.c b/tests/udp.c
index 3097476f..f10446b9 100644
--- a/tests/udp.c
+++ b/tests/udp.c
@@ -59,32 +59,34 @@ TestMain("UDP support", {
char rbuf[1024];
nng_sockaddr to;
nng_sockaddr from;
- nni_aio * aio1;
- nni_aio * aio2;
+ nng_aio * aio1;
+ nng_aio * aio2;
+ nng_iov iov1;
+ nng_iov iov2;
- nni_aio_init(&aio1, NULL, NULL);
- nni_aio_init(&aio2, NULL, NULL);
+ So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
+ So(nng_aio_alloc(&aio2, NULL, NULL) == 0);
- to = sa2;
- aio1->a_niov = 1;
- aio1->a_iov[0].iov_buf = (void *) msg;
- aio1->a_iov[0].iov_len = strlen(msg) + 1;
- aio1->a_addr = &to;
+ to = sa2;
+ iov1.iov_buf = msg;
+ iov1.iov_len = strlen(msg) + 1;
+ So(nng_aio_set_iov(aio1, 1, &iov1) == 0);
+ nng_aio_set_input(aio1, 0, &to);
- aio2->a_niov = 1;
- aio2->a_iov[0].iov_buf = (void *) rbuf;
- aio2->a_iov[0].iov_len = 1024;
- aio2->a_addr = &from;
+ iov2.iov_buf = rbuf;
+ iov2.iov_len = 1024;
+ So(nng_aio_set_iov(aio2, 1, &iov2) == 0);
+ nng_aio_set_input(aio2, 0, &from);
nni_plat_udp_recv(u2, aio2);
nni_plat_udp_send(u1, aio1);
- nni_aio_wait(aio1);
- nni_aio_wait(aio2);
+ nng_aio_wait(aio1);
+ nng_aio_wait(aio2);
- So(nni_aio_result(aio1) == 0);
- So(nni_aio_result(aio2) == 0);
+ So(nng_aio_result(aio1) == 0);
+ So(nng_aio_result(aio2) == 0);
- So(nni_aio_count(aio2) == strlen(msg) + 1);
+ So(nng_aio_count(aio2) == strlen(msg) + 1);
So(strcmp(msg, rbuf) == 0);
So(from.s_un.s_in.sa_family ==
@@ -97,23 +99,25 @@ TestMain("UDP support", {
nni_plat_udp_recv(u2, aio2);
nni_plat_udp_send(u2, aio1);
- nni_aio_fini(aio1);
- nni_aio_fini(aio2);
+ nng_aio_free(aio1);
+ nng_aio_free(aio2);
});
Convey("Sending without an address fails", {
- nni_aio *aio1;
+ nng_aio *aio1;
char * msg = "nope";
+ nng_iov iov;
- nni_aio_init(&aio1, NULL, NULL);
- aio1->a_niov = 1;
- aio1->a_iov[0].iov_buf = (void *) msg;
- aio1->a_iov[0].iov_len = strlen(msg) + 1;
+ So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
+
+ iov.iov_buf = msg;
+ iov.iov_len = strlen(msg) + 1;
+ So(nng_aio_set_iov(aio1, 1, &iov) == 0);
nni_plat_udp_send(u1, aio1);
- nni_aio_wait(aio1);
- So(nni_aio_result(aio1) == NNG_EADDRINVAL);
- nni_aio_fini(aio1);
+ nng_aio_wait(aio1);
+ So(nng_aio_result(aio1) == NNG_EADDRINVAL);
+ nng_aio_free(aio1);
});
Convey("Multiple operations work", {
@@ -125,37 +129,41 @@ TestMain("UDP support", {
nng_sockaddr to2;
nng_sockaddr from1;
nng_sockaddr from2;
- nni_aio * aio1;
- nni_aio * aio2;
- nni_aio * aio3;
- nni_aio * aio4;
-
- nni_aio_init(&aio1, NULL, NULL);
- nni_aio_init(&aio2, NULL, NULL);
- nni_aio_init(&aio3, NULL, NULL);
- nni_aio_init(&aio4, NULL, NULL);
-
- to1 = sa2;
- aio1->a_niov = 1;
- aio1->a_iov[0].iov_buf = (void *) msg1;
- aio1->a_iov[0].iov_len = strlen(msg1) + 1;
- aio1->a_addr = &to1;
-
- to2 = sa2;
- aio2->a_niov = 1;
- aio2->a_iov[0].iov_buf = (void *) msg2;
- aio2->a_iov[0].iov_len = strlen(msg2) + 1;
- aio2->a_addr = &to2;
-
- aio3->a_niov = 1;
- aio3->a_iov[0].iov_buf = (void *) rbuf1;
- aio3->a_iov[0].iov_len = 1024;
- aio3->a_addr = &from1;
-
- aio4->a_niov = 1;
- aio4->a_iov[0].iov_buf = (void *) rbuf2;
- aio4->a_iov[0].iov_len = 1024;
- aio4->a_addr = &from2;
+ nng_aio * aio1;
+ nng_aio * aio2;
+ nng_aio * aio3;
+ nng_aio * aio4;
+ nng_iov iov1;
+ nng_iov iov2;
+ nng_iov iov3;
+ nng_iov iov4;
+
+ So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
+ So(nng_aio_alloc(&aio2, NULL, NULL) == 0);
+ So(nng_aio_alloc(&aio3, NULL, NULL) == 0);
+ So(nng_aio_alloc(&aio4, NULL, NULL) == 0);
+
+ to1 = sa2;
+ iov1.iov_buf = msg1;
+ iov1.iov_len = strlen(msg1) + 1;
+ So(nng_aio_set_iov(aio1, 1, &iov1) == 0);
+ nng_aio_set_input(aio1, 0, &to1);
+
+ to2 = sa2;
+ iov2.iov_buf = msg2;
+ iov2.iov_len = strlen(msg2) + 1;
+ So(nng_aio_set_iov(aio2, 1, &iov2) == 0);
+ nng_aio_set_input(aio2, 0, &to2);
+
+ iov3.iov_buf = rbuf1;
+ iov3.iov_len = 1024;
+ So(nng_aio_set_iov(aio3, 1, &iov3) == 0);
+ nng_aio_set_input(aio3, 0, &from1);
+
+ iov4.iov_buf = rbuf2;
+ iov4.iov_len = 1024;
+ So(nng_aio_set_iov(aio4, 1, &iov4) == 0);
+ nng_aio_set_input(aio4, 0, &from2);
nni_plat_udp_recv(u2, aio4);
nni_plat_udp_recv(u2, aio3);
@@ -165,90 +173,93 @@ TestMain("UDP support", {
nng_msleep(1);
nni_plat_udp_send(u1, aio1);
- nni_aio_wait(aio2);
- nni_aio_wait(aio1);
- nni_aio_wait(aio3);
- nni_aio_wait(aio4);
+ nng_aio_wait(aio2);
+ nng_aio_wait(aio1);
+ nng_aio_wait(aio3);
+ nng_aio_wait(aio4);
- So(nni_aio_result(aio1) == 0);
- So(nni_aio_result(aio2) == 0);
- So(nni_aio_result(aio3) == 0);
- So(nni_aio_result(aio4) == 0);
+ So(nng_aio_result(aio1) == 0);
+ So(nng_aio_result(aio2) == 0);
+ So(nng_aio_result(aio3) == 0);
+ So(nng_aio_result(aio4) == 0);
So(from1.s_un.s_in.sa_family ==
sa1.s_un.s_in.sa_family);
So(from1.s_un.s_in.sa_port == sa1.s_un.s_in.sa_port);
So(from1.s_un.s_in.sa_addr == sa1.s_un.s_in.sa_addr);
- nni_aio_fini(aio1);
- nni_aio_fini(aio2);
- nni_aio_fini(aio3);
- nni_aio_fini(aio4);
+ nng_aio_free(aio1);
+ nng_aio_free(aio2);
+ nng_aio_free(aio3);
+ nng_aio_free(aio4);
});
Convey("Sending without an address fails", {
- nni_aio *aio1;
+ nng_aio *aio1;
char * msg = "nope";
+ nng_iov iov;
- nni_aio_init(&aio1, NULL, NULL);
- aio1->a_niov = 1;
- aio1->a_iov[0].iov_buf = (void *) msg;
- aio1->a_iov[0].iov_len = strlen(msg) + 1;
+ So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
+ iov.iov_buf = msg;
+ iov.iov_len = strlen(msg) + 1;
+ So(nng_aio_set_iov(aio1, 1, &iov) == 0);
nni_plat_udp_send(u1, aio1);
- nni_aio_wait(aio1);
- So(nni_aio_result(aio1) == NNG_EADDRINVAL);
- nni_aio_fini(aio1);
+ nng_aio_wait(aio1);
+ So(nng_aio_result(aio1) == NNG_EADDRINVAL);
+ nng_aio_free(aio1);
});
Convey("Sending to an IPv6 address on IPv4 fails", {
- nni_aio * aio1;
+ nng_aio * aio1;
char * msg = "nope";
nng_sockaddr sa;
int rv;
+ nng_iov iov;
sa.s_un.s_in6.sa_family = NNG_AF_INET6;
// address is for google.com
inet_ntop(AF_INET6, "2607:f8b0:4007:804::200e",
(void *) sa.s_un.s_in6.sa_addr, 16);
sa.s_un.s_in6.sa_port = 80;
- nni_aio_init(&aio1, NULL, NULL);
- aio1->a_niov = 1;
- aio1->a_iov[0].iov_buf = (void *) msg;
- aio1->a_iov[0].iov_len = strlen(msg) + 1;
- aio1->a_addr = &sa;
+ So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
+ iov.iov_buf = msg;
+ iov.iov_len = strlen(msg) + 1;
+ So(nng_aio_set_iov(aio1, 1, &iov) == 0);
+ nng_aio_set_input(aio1, 0, &sa);
nni_plat_udp_send(u1, aio1);
- nni_aio_wait(aio1);
- So((rv = nni_aio_result(aio1)) != 0);
+ nng_aio_wait(aio1);
+ So((rv = nng_aio_result(aio1)) != 0);
So(rv == NNG_EADDRINVAL || rv == NNG_ENOTSUP ||
rv == NNG_EUNREACHABLE);
- nni_aio_fini(aio1);
+ nng_aio_free(aio1);
});
Convey("Sending to an IPC address fails", {
- nni_aio * aio1;
+ nng_aio * aio1;
char * msg = "nope";
nng_sockaddr sa;
int rv;
+ nng_iov iov;
sa.s_un.s_in6.sa_family = NNG_AF_INET6;
// address is for google.com
inet_ntop(AF_INET6, "2607:f8b0:4007:804::200e",
(void *) sa.s_un.s_in6.sa_addr, 16);
sa.s_un.s_in6.sa_port = 80;
- nni_aio_init(&aio1, NULL, NULL);
- aio1->a_niov = 1;
- aio1->a_iov[0].iov_buf = (void *) msg;
- aio1->a_iov[0].iov_len = strlen(msg) + 1;
- aio1->a_addr = &sa;
+ So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
+ iov.iov_buf = msg;
+ iov.iov_len = strlen(msg) + 1;
+ So(nng_aio_set_iov(aio1, 1, &iov) == 0);
+ nng_aio_set_input(aio1, 0, &sa);
nni_plat_udp_send(u1, aio1);
- nni_aio_wait(aio1);
- So((rv = nni_aio_result(aio1)) != 0);
+ nng_aio_wait(aio1);
+ So((rv = nng_aio_result(aio1)) != 0);
So(rv == NNG_EADDRINVAL || rv == NNG_ENOTSUP ||
rv == NNG_EUNREACHABLE);
- nni_aio_fini(aio1);
+ nng_aio_free(aio1);
});
});
diff --git a/tests/zt.c b/tests/zt.c
index 8775d0b3..02e098fa 100644
--- a/tests/zt.c
+++ b/tests/zt.c
@@ -184,6 +184,17 @@ check_props(nng_msg *msg, nng_listener l, nng_dialer d)
So(mtu >= 1000 && mtu <= 10000);
});
+ Convey("Network name property works", {
+ char name[NNG_MAXADDRLEN];
+ size_t namesz;
+ int status;
+
+ namesz = sizeof(name);
+ So(nng_listener_getopt(
+ l, NNG_OPT_ZT_NETWORK_NAME, name, &namesz) == 0);
+ So(strcmp(name, "nng_test_open") == 0);
+ });
+
return (0);
}
@@ -280,20 +291,6 @@ TestMain("ZeroTier Transport", {
0);
So(node1 != 0);
- Convey("Network name & status options work", {
- char name[NNG_MAXADDRLEN];
- size_t namesz;
- int status;
-
- namesz = sizeof(name);
- nng_msleep(10000);
- So(nng_listener_getopt(l, NNG_OPT_ZT_NETWORK_NAME,
- name, &namesz) == 0);
- So(strcmp(name, "nng_test_open") == 0);
- So(nng_listener_getopt_int(
- l, NNG_OPT_ZT_NETWORK_STATUS, &status) == 0);
- So(status == nng_zt_network_status_ok);
- });
Convey("Connection refused works", {
snprintf(addr, sizeof(addr), "zt://%llx." NWID ":%u",
(unsigned long long) node1, 42u);
@@ -315,7 +312,6 @@ TestMain("ZeroTier Transport", {
uint64_t node;
port = 9944;
- // uint64_t node = 0xb000072fa6ull; // my personal host
So(nng_zt_register() == 0);
snprintf(addr1, sizeof(addr1), "zt://*." NWID ":%u", port);
@@ -324,8 +320,7 @@ TestMain("ZeroTier Transport", {
So(nng_pair_open(&s2) == 0);
Reset({
nng_close(s1);
- // This sleep allows us to ensure disconnect
- // messages work.
+ // This sleep ensures disconnect messages work.
nng_msleep(500);
nng_close(s2);
});
@@ -345,7 +340,7 @@ TestMain("ZeroTier Transport", {
So(nng_dialer_setopt(
d, NNG_OPT_ZT_HOME, path2, strlen(path2) + 1) == 0);
So(nng_dialer_start(d, 0) == 0);
- nng_msleep(2000);
+ nng_msleep(2000); // to give dialer time to start up
});
trantest_test_extended("zt://*." NWID ":%u", check_props);