diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-11-16 20:44:29 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-11-18 21:05:16 -0800 |
| commit | b826bfc171d90f8bde7bd672c0ac14201b8b2742 (patch) | |
| tree | 5c416487f24104e6305a797af31c5e8b1aab99d1 /tests | |
| parent | cda4885676f009e2e7f2ad5e6c52743efc8b8924 (diff) | |
| download | nng-b826bfc171d90f8bde7bd672c0ac14201b8b2742.tar.gz nng-b826bfc171d90f8bde7bd672c0ac14201b8b2742.tar.bz2 nng-b826bfc171d90f8bde7bd672c0ac14201b8b2742.zip | |
Work for test refactoring.
There are a few major areas in this change.
* CMake options are now located in a common cmake/NNGOptions.cmake
file. This should make it easier for folks to figure out what
the options are, and how they are used.
* Tests are now scoped with their directory name, which should
avoid possible name collisions with test names.
* A number of tests have been either moved or incorporated into
the newer testutil/acutest framework. We are moving away from
my old c-convey framework to something easier to debug.
* We use CMake directories a bit more extensively leading to a much
cleaner CMake structure. It's not complete, but a big step in the
right direction, and a preview of future work.
* Tests are now run with verbose flags, so we get more test results
in the CI/CD logs.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/CMakeLists.txt | 13 | ||||
| -rw-r--r-- | tests/aio.c | 245 | ||||
| -rw-r--r-- | tests/bufsz.c | 116 | ||||
| -rw-r--r-- | tests/bug1247.c | 39 | ||||
| -rw-r--r-- | tests/errors.c | 33 | ||||
| -rw-r--r-- | tests/id.c | 275 | ||||
| -rw-r--r-- | tests/platform.c | 181 | ||||
| -rw-r--r-- | tests/reconnect.c | 167 | ||||
| -rw-r--r-- | tests/resolv.c | 199 | ||||
| -rw-r--r-- | tests/set_recvmaxsize.c | 48 | ||||
| -rw-r--r-- | tests/sock.c | 666 | ||||
| -rw-r--r-- | tests/tcp.c | 202 | ||||
| -rw-r--r-- | tests/url.c | 479 |
13 files changed, 1 insertions, 2662 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index aa7260ac..a662d495 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -124,19 +124,7 @@ else () endmacro(add_nng_test3) endif () -nng_test(aio) -nng_test(bufsz) -nng_test(bug1247) -nng_test(id) -nng_test(platform) -nng_test(reconnect) -nng_test(resolv) -nng_test(sock) -nng_test(url) - - add_nng_test(device 5) -add_nng_test(errors 2) add_nng_test(files 5) add_nng_test1(httpclient 60 NNG_SUPP_HTTP) add_nng_test1(httpserver 30 NNG_SUPP_HTTP) @@ -152,7 +140,6 @@ add_nng_test(options 5) add_nng_test(pipe 5) add_nng_test(pollfd 5) add_nng_test(scalability 20 ON) -add_nng_test(set_recvmaxsize 2) add_nng_test1(stats 5 NNG_ENABLE_STATS) add_nng_test(synch 5) add_nng_test(tls 60) diff --git a/tests/aio.c b/tests/aio.c deleted file mode 100644 index 0fe665b1..00000000 --- a/tests/aio.c +++ /dev/null @@ -1,245 +0,0 @@ -// -// Copyright 2019 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 -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include <string.h> - -#include <nng/nng.h> -#include <nng/protocol/pair1/pair.h> -#include <nng/supplemental/util/platform.h> - -#include "acutest.h" -#include "testutil.h" - -void -cbdone(void *p) -{ - (*(int *) p)++; -} - -void -sleepdone(void *arg) -{ - *(nng_time *) arg = nng_clock(); -} - -void -cancelfn(nng_aio *aio, void *arg, int rv) -{ - *(int *) arg = rv; - nng_aio_finish(aio, rv); -} - -void -test_sleep(void) -{ - nng_time start = 0; - nng_time end = 0; - nng_aio *saio; - - TEST_NNG_PASS(nng_aio_alloc(&saio, sleepdone, &end)); - start = nng_clock(); - nng_sleep_aio(200, saio); - nng_aio_wait(saio); - TEST_NNG_PASS(nng_aio_result(saio)); - TEST_CHECK(end != 0); - TEST_CHECK((end - start) >= 200); - TEST_CHECK((end - start) <= 1000); - TEST_CHECK((nng_clock() - start) >= 200); - TEST_CHECK((nng_clock() - start) <= 1000); - nng_aio_free(saio); -} - -void -test_sleep_timeout(void) -{ - nng_time start = 0; - nng_time end = 0; - nng_aio *saio; - - TEST_CHECK(nng_aio_alloc(&saio, sleepdone, &end) == 0); - nng_aio_set_timeout(saio, 100); - start = nng_clock(); - nng_sleep_aio(2000, saio); - nng_aio_wait(saio); - TEST_NNG_FAIL(nng_aio_result(saio), NNG_ETIMEDOUT); - TEST_CHECK(end != 0); - TEST_CHECK((end - start) >= 100); - TEST_CHECK((end - start) <= 1000); - TEST_CHECK((nng_clock() - start) >= 100); - TEST_CHECK((nng_clock() - start) <= 1000); - nng_aio_free(saio); -} - -void -test_insane_niov(void) -{ - nng_aio *aio; - nng_iov iov; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - TEST_NNG_FAIL(nng_aio_set_iov(aio, 1024, &iov), NNG_EINVAL); - nng_aio_free(aio); -} - -void -test_provider_cancel(void) -{ - nng_aio *aio; - int rv = 0; - // We fake an empty provider that does not do anything. - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - TEST_CHECK(nng_aio_begin(aio) == true); - nng_aio_defer(aio, cancelfn, &rv); - nng_aio_cancel(aio); - nng_aio_wait(aio); - TEST_CHECK(rv == NNG_ECANCELED); - nng_aio_free(aio); -} - -void -test_consumer_cancel(void) -{ - nng_aio * a; - nng_socket s1; - int done = 0; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_aio_alloc(&a, cbdone, &done) == 0); - - nng_aio_set_timeout(a, NNG_DURATION_INFINITE); - nng_recv_aio(s1, a); - nng_aio_cancel(a); - nng_aio_wait(a); - TEST_CHECK(done == 1); - TEST_CHECK(nng_aio_result(a) == NNG_ECANCELED); - - nng_aio_free(a); - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_traffic(void) -{ - nng_socket s1; - nng_socket s2; - nng_aio * txaio; - nng_aio * rxaio; - int txdone = 0; - int rxdone = 0; - nng_msg * m; - char * addr = "inproc://traffic"; - - TEST_NNG_PASS(nng_pair1_open(&s1)); - TEST_NNG_PASS(nng_pair1_open(&s2)); - - TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0)); - TEST_NNG_PASS(nng_dial(s2, addr, NULL, 0)); - - TEST_NNG_PASS(nng_aio_alloc(&rxaio, cbdone, &rxdone)); - TEST_NNG_PASS(nng_aio_alloc(&txaio, cbdone, &txdone)); - - nng_aio_set_timeout(rxaio, 1000); - nng_aio_set_timeout(txaio, 1000); - - TEST_NNG_PASS(nng_msg_alloc(&m, 0)); - TEST_NNG_PASS(nng_msg_append(m, "hello", strlen("hello"))); - - nng_recv_aio(s2, rxaio); - - nng_aio_set_msg(txaio, m); - nng_send_aio(s1, txaio); - - nng_aio_wait(txaio); - nng_aio_wait(rxaio); - - TEST_NNG_PASS(nng_aio_result(rxaio)); - TEST_NNG_PASS(nng_aio_result(txaio)); - - TEST_CHECK((m = nng_aio_get_msg(rxaio)) != NULL); - TEST_CHECK(nng_msg_len(m) == strlen("hello")); - TEST_CHECK(memcmp(nng_msg_body(m), "hello", strlen("hello")) == 0); - - nng_msg_free(m); - - TEST_CHECK(rxdone == 1); - TEST_CHECK(txdone == 1); - - nng_aio_free(rxaio); - nng_aio_free(txaio); - TEST_NNG_PASS(nng_close(s1)); - TEST_NNG_PASS(nng_close(s2)); -} - -void -test_explicit_timeout(void) -{ - nng_socket s; - nng_aio * a; - int done = 0; - - TEST_NNG_PASS(nng_pair1_open(&s)); - TEST_NNG_PASS(nng_aio_alloc(&a, cbdone, &done)); - nng_aio_set_timeout(a, 40); - nng_recv_aio(s, a); - nng_aio_wait(a); - TEST_CHECK(done == 1); - TEST_NNG_FAIL(nng_aio_result(a), NNG_ETIMEDOUT); - nng_aio_free(a); - TEST_NNG_PASS(nng_close(s)); -} - -void -test_inherited_timeout(void) -{ - nng_socket s; - nng_aio * a; - int done = 0; - - TEST_NNG_PASS(nng_pair1_open(&s)); - TEST_NNG_PASS(nng_aio_alloc(&a, cbdone, &done)); - TEST_NNG_PASS(nng_setopt_ms(s, NNG_OPT_RECVTIMEO, 40)); - nng_recv_aio(s, a); - nng_aio_wait(a); - TEST_CHECK(done == 1); - TEST_NNG_FAIL(nng_aio_result(a), NNG_ETIMEDOUT); - nng_aio_free(a); - TEST_NNG_PASS(nng_close(s)); -} - -void -test_zero_timeout(void) -{ - nng_socket s; - nng_aio * a; - int done = 0; - - TEST_NNG_PASS(nng_pair1_open(&s)); - TEST_NNG_PASS(nng_aio_alloc(&a, cbdone, &done)); - nng_aio_set_timeout(a, NNG_DURATION_ZERO); - nng_recv_aio(s, a); - nng_aio_wait(a); - TEST_CHECK(done == 1); - TEST_NNG_FAIL(nng_aio_result(a), NNG_ETIMEDOUT); - nng_aio_free(a); - TEST_NNG_PASS(nng_close(s)); -} - -TEST_LIST = { - { "sleep", test_sleep }, - { "sleep timeout", test_sleep_timeout }, - { "insane niov", test_insane_niov }, - { "provider cancel", test_provider_cancel }, - { "consumer cancel", test_consumer_cancel }, - { "traffic", test_traffic }, - { "explicit timeout", test_explicit_timeout }, - { "inherited timeout", test_inherited_timeout }, - { "zero timeout", test_zero_timeout }, - { NULL, NULL }, -};
\ No newline at end of file diff --git a/tests/bufsz.c b/tests/bufsz.c deleted file mode 100644 index 4f62905d..00000000 --- a/tests/bufsz.c +++ /dev/null @@ -1,116 +0,0 @@ -// -// 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 -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - - -#include <nng/nng.h> -#include <nng/protocol/pair1/pair.h> -#include <nng/supplemental/util/platform.h> - -#include <nng/compat/nanomsg/nn.h> - -#include "acutest.h" - -void -test_buffer_options(void) -{ - nng_socket s1; - int val; - size_t sz; - char * opt; - - char *cases[] = { - NNG_OPT_RECVBUF, - NNG_OPT_SENDBUF, - NULL, - }; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - for (int i = 0; (opt = cases[i]) != NULL; i++) { - - TEST_CASE(opt); - - // Can't receive a size into zero bytes. - sz = 0; - TEST_CHECK(nng_getopt(s1, opt, &val, &sz) == NNG_EINVAL); - - // Can set a valid size - TEST_CHECK(nng_setopt_int(s1, opt, 1234) == 0); - TEST_CHECK(nng_getopt_int(s1, opt, &val) == 0); - TEST_CHECK(val == 1234); - - val = 0; - sz = sizeof(val); - TEST_CHECK(nng_getopt(s1, opt, &val, &sz) == 0); - TEST_CHECK(val == 1234); - TEST_CHECK(sz == sizeof(val)); - - // Can't set a negative size - TEST_CHECK(nng_setopt_int(s1, opt, -5) == NNG_EINVAL); - - // Can't pass a buf too small for size - sz = sizeof(val) - 1; - val = 1; - TEST_CHECK(nng_setopt(s1, opt, &val, sz) == NNG_EINVAL); - // Buffer sizes are limited to sane levels - TEST_CHECK(nng_setopt_int(s1, opt, 0x100000) == NNG_EINVAL); - } - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_buffer_legacy(void) -{ - nng_socket s1; - char * opt; - - char *cases[] = { - NNG_OPT_RECVBUF, - NNG_OPT_SENDBUF, - NULL, - }; - int legacy[] = { - NN_RCVBUF, - NN_SNDBUF, - }; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - for (int i = 0; (opt = cases[i]) != NULL; i++) { - int cnt; - int os = (int) s1.id; - size_t sz; - int nnopt = legacy[i]; - - TEST_CASE(opt); - - sz = sizeof(cnt); - TEST_CHECK(nng_setopt_int(s1, opt, 10) == 0); - TEST_CHECK( - nn_getsockopt(os, NN_SOL_SOCKET, nnopt, &cnt, &sz) == 0); - TEST_CHECK(cnt == 10240); // 1k multiple - - cnt = 1; - TEST_CHECK( - nn_setsockopt(os, NN_SOL_SOCKET, nnopt, &cnt, sz) == 0); - TEST_CHECK(nn_getsockopt(os, NN_SOL_SOCKET, nnopt, &cnt, &sz) == 0); - TEST_CHECK(cnt == 1024); // round up! - TEST_CHECK(nng_getopt_int(s1, opt, &cnt) == 0); - TEST_CHECK(cnt == 1); - - TEST_CHECK(nn_setsockopt(os, NN_SOL_SOCKET, nnopt, &cnt, 100) == -1); - TEST_CHECK(nn_errno() == EINVAL); - } - TEST_CHECK(nng_close(s1) == 0); -} - -TEST_LIST = { - { "buffer options", test_buffer_options }, - { "buffer legacy", test_buffer_legacy }, - { NULL, NULL }, -}; diff --git a/tests/bug1247.c b/tests/bug1247.c deleted file mode 100644 index 6f418f53..00000000 --- a/tests/bug1247.c +++ /dev/null @@ -1,39 +0,0 @@ -// -// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> -// -// This software is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include <string.h> - -#include <nng/nng.h> -#include <nng/protocol/bus0/bus.h> - -#include "acutest.h" -#include "testutil.h" - -void -test_bug1247(void) -{ - nng_socket bus1, bus2; - char addr[64]; - - testutil_scratch_addr("tcp", sizeof(addr), addr); - - TEST_NNG_PASS(nng_bus0_open(&bus1)); - TEST_NNG_PASS(nng_bus0_open(&bus2)); - - TEST_NNG_PASS(nng_listen(bus1, addr, NULL, 0)); - TEST_NNG_FAIL(nng_listen(bus2, addr, NULL, 0), NNG_EADDRINUSE); - - TEST_NNG_PASS(nng_close(bus2)); - TEST_NNG_PASS(nng_close(bus1)); -} - -TEST_LIST = { - { "bug1247", test_bug1247 }, - { NULL, NULL }, -}; diff --git a/tests/errors.c b/tests/errors.c deleted file mode 100644 index 0cade6a4..00000000 --- a/tests/errors.c +++ /dev/null @@ -1,33 +0,0 @@ -// -// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> -// -// This software is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include <errno.h> -#include <string.h> - -#include <nng/nng.h> - -#include "convey.h" - -TestMain("Error messages work", { - Convey("Known errors work", { - So(strcmp(nng_strerror(NNG_ECLOSED), "Object closed") == 0); - So(strcmp(nng_strerror(NNG_ETIMEDOUT), "Timed out") == 0); - }); - Convey("We always get a valid error", { - for (unsigned i = 1; i < 0x1000000; i = i * 2 + 100) { - So(nng_strerror(i) != NULL); - } - }); - Convey("System errors work", { - So(strcmp(nng_strerror(NNG_ESYSERR + ENOENT), - strerror(ENOENT)) == 0); - So(strcmp(nng_strerror(NNG_ESYSERR + EINVAL), - strerror(EINVAL)) == 0); - }); -}) diff --git a/tests/id.c b/tests/id.c deleted file mode 100644 index e8944547..00000000 --- a/tests/id.c +++ /dev/null @@ -1,275 +0,0 @@ -// -// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> -// -// This software is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include "acutest.h" -#include "testutil.h" - -#include "core/idhash.h" - -void -test_basic(void) -{ - nni_id_map m; - char * five = "five"; - char * four = "four"; - - nni_id_map_init(&m, 0, 0, false); - - // insert it - TEST_NNG_PASS(nni_id_set(&m, 5, five)); - // retrieve it - TEST_CHECK(nni_id_get(&m, 5) == five); - - // change it - TEST_NNG_PASS(nni_id_set(&m, 5, four)); - TEST_CHECK(nni_id_get(&m, 5) == four); - - // delete - TEST_NNG_PASS(nni_id_remove(&m, 5)); - - nni_id_map_fini(&m); -} - -void -test_random(void) -{ - int i; - uint32_t id; - for (i = 0; i < 2; i++) { - nni_id_map m; - nni_id_map_init(&m, 0, 0, true); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &id)); - nni_id_map_fini(&m); - TEST_CHECK(id != 0); - if (id != 1) { - break; - } - // one chance in 4 billion, but try again - } - - TEST_CHECK(id != 1); - TEST_CHECK(i < 2); -} - -void -test_collision(void) -{ - nni_id_map m; - char * five = "five"; - char * four = "four"; - - nni_id_map_init(&m, 0, 0, false); - - // Carefully crafted -- 13 % 8 == 5. - TEST_NNG_PASS(nni_id_set(&m, 5, five)); - TEST_NNG_PASS(nni_id_set(&m, 13, four)); - TEST_CHECK(nni_id_get(&m, 5) == five); - TEST_CHECK(nni_id_get(&m, 13) == four); - - // Delete the intermediate - TEST_NNG_PASS(nni_id_remove(&m, 5)); - TEST_CHECK(nni_id_get(&m, 13) == four); - - nni_id_map_fini(&m); -} - -void -test_empty(void) -{ - nni_id_map m; - nni_id_map_init(&m, 0, 0, false); - - TEST_CHECK(nni_id_get(&m, 42) == NULL); - TEST_NNG_FAIL(nni_id_remove(&m, 42), NNG_ENOENT); - TEST_NNG_FAIL(nni_id_remove(&m, 1), NNG_ENOENT); - nni_id_map_fini(&m); -} - -void -test_not_found(void) -{ - nni_id_map m; - uint32_t id; - nni_id_map_init(&m, 0, 0, false); - - TEST_NNG_PASS(nni_id_alloc(&m, &id, &id)); - TEST_NNG_FAIL(nni_id_remove(&m, 42), NNG_ENOENT); - TEST_NNG_FAIL(nni_id_remove(&m, 2), NNG_ENOENT); - TEST_NNG_PASS(nni_id_remove(&m, id)); - nni_id_map_fini(&m); -} - -void -test_resize(void) -{ - nni_id_map m; - int rv; - int i; - int expect[1024]; - - for (i = 0; i < 1024; i++) { - expect[i] = i; - } - - nni_id_map_init(&m, 0, 0, false); - - for (i = 0; i < 1024; i++) { - if ((rv = nni_id_set(&m, i, &expect[i])) != 0) { - TEST_NNG_PASS(rv); - } - } - - for (i = 0; i < 1024; i++) { - if ((rv = nni_id_remove(&m, i)) != 0) { - TEST_NNG_PASS(rv); - } - } - nni_id_map_fini(&m); -} - -void -test_dynamic(void) -{ - nni_id_map m; - int expect[5]; - uint32_t id; - - nni_id_map_init(&m, 10, 13, false); - - // We can fill the table. - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[0])); - TEST_CHECK(id == 10); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[1])); - TEST_CHECK(id == 11); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[2])); - TEST_CHECK(id == 12); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[3])); - TEST_CHECK(id == 13); - - // Adding another fails. - TEST_NNG_FAIL(nni_id_alloc(&m, &id, &expect[4]), NNG_ENOMEM); - - // Delete one. - TEST_NNG_PASS(nni_id_remove(&m, 11)); - - // And now we can allocate one. - TEST_NNG_PASS(nni_id_alloc(&m, &id, &expect[4])); - TEST_CHECK(id == 11); - nni_id_map_fini(&m); -} - -void -test_set_out_of_range(void) -{ - nni_id_map m; - int x; - uint32_t id; - - nni_id_map_init(&m, 10, 13, false); - - // We can insert outside the range forcibly. - TEST_NNG_PASS(nni_id_set(&m, 1, &x)); - TEST_NNG_PASS(nni_id_set(&m, 100, &x)); - TEST_NNG_PASS(nni_id_alloc(&m, &id, &x)); - TEST_CHECK(id == 10); - nni_id_map_fini(&m); -} - -#define STRESS_LOAD 50000 -#define NUM_VALUES 1000 - -void -test_stress(void) -{ - void * values[NUM_VALUES]; - nni_id_map m; - size_t i; - int rv; - void * x; - int v; - - nni_id_map_init(&m, 0, 0, false); - for (i = 0; i < NUM_VALUES; i++) { - values[i] = NULL; - } - - for (i = 0; i < STRESS_LOAD; i++) { - v = rand() % NUM_VALUES; // Keep it constrained - - switch (rand() & 3) { - case 0: - x = &values[rand() % NUM_VALUES]; - values[v] = x; - if ((rv = nni_id_set(&m, v, x)) != 0) { - TEST_NNG_PASS(rv); - goto out; - } - break; - - case 1: - rv = nni_id_remove(&m, v); - if (values[v] == NULL) { - if (rv != NNG_ENOENT) { - TEST_NNG_FAIL(rv, NNG_ENOENT); - goto out; - } - } else { - values[v] = NULL; - if (rv != 0) { - TEST_NNG_PASS(rv); - goto out; - } - } - break; - case 2: - x = nni_id_get(&m, v); - if (x != values[v]) { - TEST_CHECK(x == values[v]); - goto out; - } - break; - } - } -out: - TEST_CHECK(i == STRESS_LOAD); - - // Post stress check. - for (i = 0; i < NUM_VALUES; i++) { - x = nni_id_get(&m, i); - if (x != values[i]) { - TEST_CHECK(x == values[i]); - break; - } - - // We only use the test macros if we know they are going - // to fail. Otherwise there will be too many errors reported. - rv = nni_id_remove(&m, i); - if ((x == NULL) && (rv != NNG_ENOENT)) { - TEST_NNG_FAIL(rv, NNG_ENOENT); - } else if ((x != NULL) && (rv != 0)) { - TEST_NNG_PASS(rv); - } - } - TEST_CHECK(i == NUM_VALUES); - - nni_id_map_fini(&m); -} - -TEST_LIST = { - { "basic", test_basic }, - { "random", test_random }, - { "collision", test_collision }, - { "empty", test_empty }, - { "not found", test_not_found }, - { "resize", test_resize }, - { "dynamic", test_dynamic }, - { "set out of range", test_set_out_of_range }, - { "stress", test_stress }, - { NULL, NULL }, -}; diff --git a/tests/platform.c b/tests/platform.c deleted file mode 100644 index 228baf5e..00000000 --- a/tests/platform.c +++ /dev/null @@ -1,181 +0,0 @@ -// -// Copyright 2020 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 -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include "testutil.h" - -#include <nng/nng.h> -#include <nng/supplemental/util/platform.h> - -#include "acutest.h" - -struct addarg { - int cnt; - nng_mtx *mx; - nng_cv * cv; -}; - -void -add(void *arg) -{ - struct addarg *aa = arg; - - nng_mtx_lock(aa->mx); - aa->cnt++; - nng_cv_wake(aa->cv); - nng_mtx_unlock(aa->mx); -} - -void -test_sleep(void) -{ - uint64_t start, end; - start = testutil_clock(); - nng_msleep(100); - end = testutil_clock(); - TEST_CHECK((end - start) >= 100); -#ifdef __has_feature -#if !__has_feature(thread_sanitizer) && !__has_feature(memory_sanitizer) - TEST_CHECK((end - start) <= 500); -#endif -#endif -} - -void -test_clock(void) -{ - uint64_t mstart; - uint64_t msend; - nng_time usend; - nng_time usnow; - - mstart = testutil_clock(); - usnow = nng_clock(); - nng_msleep(200); - usend = nng_clock(); - msend = testutil_clock(); - - TEST_CHECK(usend > usnow); - TEST_CHECK(msend > mstart); - -#ifdef __has_feature -#if !__has_feature(thread_sanitizer) && !__has_feature(memory_sanitizer) - uint64_t usdelta; - uint64_t msdelta; - usdelta = usend - usnow; - msdelta = msend - mstart; - TEST_CHECK(usdelta >= 200); - TEST_CHECK(usdelta < 500); // increased tolerance for CIs - if (msdelta > usdelta) { - TEST_CHECK((msdelta - usdelta) < 50); - } else { - TEST_CHECK((usdelta - msdelta) < 50); - } -#endif -#endif -} - -void -test_mutex(void) -{ - nng_mtx *mx, *mx2; - - TEST_CHECK(nng_mtx_alloc(&mx) == 0); - nng_mtx_lock(mx); - nng_mtx_unlock(mx); - - nng_mtx_lock(mx); - nng_mtx_unlock(mx); - nng_mtx_free(mx); - - // Verify that the mutexes are not always the same! - TEST_CHECK(nng_mtx_alloc(&mx) == 0); - TEST_CHECK(nng_mtx_alloc(&mx2) == 0); - TEST_CHECK(mx != mx2); - nng_mtx_free(mx); - nng_mtx_free(mx2); -} - -void -test_thread(void) -{ - nng_thread * thr; - int rv; - struct addarg aa; - - TEST_CHECK(nng_mtx_alloc(&aa.mx) == 0); - TEST_CHECK(nng_cv_alloc(&aa.cv, aa.mx) == 0); - aa.cnt = 0; - - TEST_CHECK((rv = nng_thread_create(&thr, add, &aa)) == 0); - nng_thread_destroy(thr); - TEST_CHECK(aa.cnt == 1); - - nng_cv_free(aa.cv); - nng_mtx_free(aa.mx); -} - -void -test_condvar(void) -{ - nng_thread * thr; - int rv; - struct addarg aa; - - TEST_CHECK(nng_mtx_alloc(&aa.mx) == 0); - TEST_CHECK(nng_cv_alloc(&aa.cv, aa.mx) == 0); - aa.cnt = 0; - - TEST_CHECK((rv = nng_thread_create(&thr, add, &aa)) == 0); - - nng_mtx_lock(aa.mx); - while (aa.cnt == 0) { - nng_cv_wait(aa.cv); - } - nng_mtx_unlock(aa.mx); - nng_thread_destroy(thr); - TEST_CHECK(aa.cnt == 1); - - nng_cv_free(aa.cv); - nng_mtx_free(aa.mx); -} - -void -test_random(void) -{ - int same = 0; - uint32_t values[1000]; - - for (int i = 0; i < 1000; i++) { - values[i] = nng_random(); - } - for (int i = 0; i < 1000; i++) { - for (int j = 0; j < i; j++) { - if (values[j] == values[i]) { - same++; - } - } - } - - // 1% reproduction is *highly* unlikely. - // There are 4 billion possible options, we are only looking at - // 1000 of them. In general, it would be an extreme outlier - // to see more than 2 repeats, unless you RNG is biased. - TEST_CHECK_(same < 5, "fewer than 5 in 1000 repeats: %d", same); -} - -TEST_LIST = { - { "sleep", test_sleep }, - { "clock", test_clock }, - { "mutex", test_mutex }, - { "thread", test_thread }, - { "condvar", test_condvar }, - { "random", test_random }, - { NULL, NULL }, -}; diff --git a/tests/reconnect.c b/tests/reconnect.c deleted file mode 100644 index 317f1bec..00000000 --- a/tests/reconnect.c +++ /dev/null @@ -1,167 +0,0 @@ -// -// Copyright 2020 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 -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include <string.h> - -#include <nng/nng.h> -#include <nng/protocol/pipeline0/pull.h> -#include <nng/protocol/pipeline0/push.h> -#include <nng/supplemental/util/platform.h> - -#include "acutest.h" -#include "testutil.h" - -void -test_dial_before_listen(void) -{ - nng_socket push; - nng_socket pull; - char addr[64]; - - testutil_scratch_addr("inproc", sizeof(addr), addr); - - TEST_NNG_PASS(nng_push0_open(&push)); - TEST_NNG_PASS(nng_pull0_open(&pull)); - - TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMINT, 10)); - TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMAXT, 10)); - - TEST_NNG_PASS(nng_dial(pull, addr, NULL, NNG_FLAG_NONBLOCK)); - testutil_sleep(100); - TEST_NNG_PASS(nng_listen(push, addr, NULL, 0)); - - TEST_NNG_SEND_STR(push, "hello"); - TEST_NNG_RECV_STR(pull, "hello"); - - TEST_NNG_PASS(nng_close(push)); - TEST_NNG_PASS(nng_close(pull)); -} - -void -test_reconnect(void) -{ - nng_socket push; - nng_socket pull; - nng_listener l; - char addr[64]; - - testutil_scratch_addr("inproc", sizeof(addr), addr); - - TEST_NNG_PASS(nng_push0_open(&push)); - TEST_NNG_PASS(nng_pull0_open(&pull)); - - TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMINT, 10)); - TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMAXT, 10)); - - TEST_NNG_PASS(nng_dial(pull, addr, NULL, NNG_FLAG_NONBLOCK)); - testutil_sleep(100); - TEST_NNG_PASS(nng_listen(push, addr, &l, 0)); - - TEST_NNG_SEND_STR(push, "hello"); - TEST_NNG_RECV_STR(pull, "hello"); - - // Close the listener - TEST_NNG_PASS(nng_listener_close(l)); - - TEST_NNG_PASS(nng_listen(push, addr, &l, 0)); - TEST_NNG_SEND_STR(push, "again"); - TEST_NNG_RECV_STR(pull, "again"); - - TEST_NNG_PASS(nng_close(push)); - TEST_NNG_PASS(nng_close(pull)); -} - -void -test_reconnect_pipe(void) -{ - nng_socket push; - nng_socket pull; - nng_listener l; - nng_msg * msg; - char addr[64]; - - testutil_scratch_addr("inproc", sizeof(addr), addr); - - TEST_NNG_PASS(nng_push0_open(&push)); - TEST_NNG_PASS(nng_pull0_open(&pull)); - - TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMINT, 10)); - TEST_NNG_PASS(nng_setopt_ms(pull, NNG_OPT_RECONNMAXT, 10)); - - TEST_NNG_PASS(nng_dial(pull, addr, NULL, NNG_FLAG_NONBLOCK)); - testutil_sleep(100); - TEST_NNG_PASS(nng_listen(push, addr, &l, 0)); - - TEST_NNG_SEND_STR(push, "hello"); - - TEST_NNG_PASS(nng_recvmsg(pull, &msg, 0)); - TEST_CHECK(msg != NULL); - TEST_CHECK(nng_msg_len(msg) == 6); - TEST_CHECK(strcmp(nng_msg_body(msg), "hello") == 0); - nng_pipe_close(nng_msg_get_pipe(msg)); - nng_msg_free(msg); - - // We have to wait a bit, because while we closed the pipe on the - // receiver, the receiver might not have got the update. If we - // send too soon, then the message gets routed to the sender pipe - // that is about to close. - testutil_sleep(100); - - // Reconnect should happen more ore less immediately. - TEST_NNG_SEND_STR(push, "again"); - TEST_NNG_RECV_STR(pull, "again"); - - TEST_NNG_PASS(nng_close(push)); - TEST_NNG_PASS(nng_close(pull)); -} - -void -test_reconnect_back_off_zero(void) -{ - nng_socket push; - nng_socket pull; - nng_time start; - char addr[64]; - testutil_scratch_addr("inproc", sizeof(addr), addr); - - TEST_NNG_PASS(nng_push0_open(&push)); - TEST_NNG_PASS(nng_pull0_open(&pull)); - - // redial every 10 ms. - TEST_NNG_PASS(nng_setopt_ms(push, NNG_OPT_RECONNMAXT, 0)); - TEST_NNG_PASS(nng_setopt_ms(push, NNG_OPT_RECONNMINT, 10)); - TEST_NNG_PASS(nng_dial(push, addr, NULL, NNG_FLAG_NONBLOCK)); - - // Start up the dialer first. It should keep retrying every 10 ms. - - // Wait 500 milliseconds. This gives a chance for an exponential - // back-off to increase to a longer time. It should by this point - // be well over 100 and probably closer to 200 ms. - nng_msleep(500); - - start = nng_clock(); - TEST_NNG_PASS(nng_listen(pull, addr, NULL, 0)); - - TEST_NNG_SEND_STR(push, "hello"); - TEST_NNG_RECV_STR(pull, "hello"); - - TEST_CHECK(nng_clock() - start < 100); - - TEST_NNG_PASS(nng_close(push)); - TEST_NNG_PASS(nng_close(pull)); -} - -TEST_LIST = { - { "dial before listen", test_dial_before_listen }, - { "reconnect", test_reconnect }, - { "reconnect back-off zero", test_reconnect_back_off_zero }, - { "reconnect pipe", test_reconnect_pipe }, - { NULL, NULL }, -};
\ No newline at end of file diff --git a/tests/resolv.c b/tests/resolv.c deleted file mode 100644 index 43168cdb..00000000 --- a/tests/resolv.c +++ /dev/null @@ -1,199 +0,0 @@ -// -// Copyright 2020 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 -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include "testutil.h" - -#include <string.h> - -#include "core/nng_impl.h" -#include "stubs.h" - -#include "acutest.h" - -#ifndef _WIN32 -#include <arpa/inet.h> // for htons, htonl -#endif - -uint8_t v6loop[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; - -void -test_google_dns(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip("google-public-dns-a.google.com", "80", NNG_AF_INET, - true, &sa, aio); - nng_aio_wait(aio); - TEST_NNG_PASS(nng_aio_result(aio)); - TEST_CHECK(sa.s_in.sa_family == NNG_AF_INET); - TEST_CHECK(sa.s_in.sa_port == ntohs(80)); - TEST_CHECK(sa.s_in.sa_addr == 0x08080808); // aka 8.8.8.8 - nng_aio_free(aio); -} - -void -test_numeric_addr(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip("8.8.4.4", "69", NNG_AF_INET, true, &sa, aio); - nng_aio_wait(aio); - TEST_NNG_PASS(nng_aio_result(aio)); - TEST_CHECK(sa.s_in.sa_family == NNG_AF_INET); - TEST_CHECK(sa.s_in.sa_port == ntohs(69)); - TEST_CHECK(sa.s_in.sa_addr == ntohl(0x08080404)); // 8.8.4.4. - nng_aio_free(aio); -} - -void -test_numeric_v6(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - // Travis CI has moved some of their services to host that - // apparently don't support IPv6 at all. This is very sad. - // CircleCI 2.0 is in the same boat. (Amazon to blame.) - if ((getenv("TRAVIS") != NULL) || (getenv("CIRCLECI") != NULL)) { - return; // skip this one. - } - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip("::1", "80", NNG_AF_INET6, true, &sa, aio); - nng_aio_wait(aio); - TEST_NNG_PASS(nng_aio_result(aio)); - TEST_CHECK(sa.s_in6.sa_family == NNG_AF_INET6); - TEST_CHECK(sa.s_in6.sa_port == ntohs(80)); - TEST_CHECK(memcmp(sa.s_in6.sa_addr, v6loop, 16) == 0); - nng_aio_free(aio); -} - -void -test_service_names(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip("8.8.4.4", "http", NNG_AF_INET, true, &sa, aio); - nng_aio_wait(aio); - TEST_NNG_PASS(nng_aio_result(aio)); - TEST_CHECK(sa.s_in.sa_port == ntohs(80)); - TEST_CHECK(sa.s_in.sa_addr = ntohl(0x08080404)); - nng_aio_free(aio); -} - -void -test_localhost_v4(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip("localhost", "80", NNG_AF_INET, true, &sa, aio); - nng_aio_wait(aio); - TEST_NNG_PASS(nng_aio_result(aio)); - TEST_CHECK(sa.s_in.sa_family == NNG_AF_INET); - TEST_CHECK(sa.s_in.sa_port == ntohs(80)); - TEST_CHECK(sa.s_in.sa_addr == ntohl(0x7f000001)); - nng_aio_free(aio); -} - -void -test_localhost_unspec(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip("localhost", "80", NNG_AF_UNSPEC, true, &sa, aio); - nng_aio_wait(aio); - TEST_NNG_PASS(nng_aio_result(aio)); - TEST_CHECK( - (sa.s_family == NNG_AF_INET) || (sa.s_family == NNG_AF_INET6)); - switch (sa.s_family) { - case NNG_AF_INET: - TEST_CHECK(sa.s_in.sa_port == ntohs(80)); - TEST_CHECK(sa.s_in.sa_addr == ntohl(0x7f000001)); - break; - case NNG_AF_INET6: - TEST_CHECK(sa.s_in6.sa_port == ntohs(80)); - TEST_CHECK(memcmp(sa.s_in6.sa_addr, v6loop, 16) == 0); - break; - } - nng_aio_free(aio); -} - -void -test_null_passive(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip(NULL, "80", NNG_AF_INET, true, &sa, aio); - nng_aio_wait(aio); - TEST_NNG_PASS(nng_aio_result(aio)); - TEST_CHECK(sa.s_in.sa_family == NNG_AF_INET); - TEST_CHECK(sa.s_in.sa_port == ntohs(80)); - TEST_CHECK(sa.s_in.sa_addr == 0); // INADDR_ANY - nng_aio_free(aio); -} - -void -test_null_not_passive(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip(NULL, "80", NNG_AF_INET, false, &sa, aio); - nng_aio_wait(aio); - // We can either get NNG_EADDRINVAL, or a loopback address. - // Most systems do the former, but Linux does the latter. - if (nng_aio_result(aio) == 0) { - TEST_CHECK(sa.s_family == NNG_AF_INET); - TEST_CHECK(sa.s_in.sa_addr == htonl(0x7f000001)); - TEST_CHECK(sa.s_in.sa_port == htons(80)); - } else { - TEST_NNG_FAIL(nng_aio_result(aio), NNG_EADDRINVAL); - } - nng_aio_free(aio); -} - -void -test_bad_port_number(void) -{ - nng_aio * aio; - nng_sockaddr sa; - - TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL)); - nni_resolv_ip("1.1.1.1", "1000000", NNG_AF_INET, true, &sa, aio); - nng_aio_wait(aio); - TEST_NNG_FAIL(nng_aio_result(aio), NNG_EADDRINVAL); - nng_aio_free(aio); -} - -TEST_LIST = { - { "resolve google dns", test_google_dns }, - { "resolve numeric addr", test_numeric_addr }, - { "resolve numeric v6", test_numeric_v6 }, - { "resolve service names", test_service_names }, - { "resolve localhost v4", test_localhost_v4 }, - { "resolve localhost unspec", test_localhost_unspec }, - { "resolve null passive", test_null_passive }, - { "resolve null not passive", test_null_not_passive }, - { "resolve bad port number", test_bad_port_number }, - { NULL, NULL }, -}; diff --git a/tests/set_recvmaxsize.c b/tests/set_recvmaxsize.c deleted file mode 100644 index 70d9d700..00000000 --- a/tests/set_recvmaxsize.c +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2018 Cody Piersall <cody.piersall@gmail.com> -// -// This software is supplied under the terms of the MIT License, a -// copy of which should be located in the distribution where this -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include "convey.h" - -#include <nng/nng.h> -#include <nng/protocol/pair1/pair.h> - -#define SNDBUFSIZE 150 -#define RCVBUFSIZE 200 - -const char *addrs[] = { - "tcp://127.0.0.1:43895", - "ws://127.0.0.1:43897", -}; - -TestMain("recvmaxsize", { - // we don't actually care what the content of the message is. - char msg[SNDBUFSIZE]; - char rcvbuf[RCVBUFSIZE]; - size_t rcvsize = RCVBUFSIZE; - nng_socket s0; - nng_socket s1; - nng_listener l; - int numproto = sizeof addrs / sizeof *addrs; - Convey("recvmaxsize can be set after listening", { - for (int i = 0; i < numproto; i++) { - const char *addr = addrs[i]; - So(nng_pair1_open(&s0) == 0); - So(nng_setopt_ms(s0, NNG_OPT_RECVTIMEO, 100) == 0); - So(nng_setopt_size(s0, NNG_OPT_RECVMAXSZ, 200) == 0); - So(nng_listen(s0, addr, &l, 0) == 0); - So(nng_setopt_size(s0, NNG_OPT_RECVMAXSZ, 100) == 0); - - So(nng_pair1_open(&s1) == 0); - So(nng_dial(s1, addr, NULL, 0) == 0); - So(nng_send(s1, msg, 150, 0) == 0); - So(nng_recv(s0, rcvbuf, &rcvsize, 0) == NNG_ETIMEDOUT); - So(nng_close(s0) == 0); - So(nng_close(s1) == 0); - } - }); -}) diff --git a/tests/sock.c b/tests/sock.c deleted file mode 100644 index 7b19f378..00000000 --- a/tests/sock.c +++ /dev/null @@ -1,666 +0,0 @@ -// -// Copyright 2020 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 -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - -#include <string.h> - -#include <nng/nng.h> -#include <nng/protocol/pair1/pair.h> -#include <nng/supplemental/util/platform.h> - -#include "acutest.h" -#include "testutil.h" - -void -test_recv_timeout(void) -{ - nng_socket s1; - uint64_t now; - nng_msg * msg = NULL; - - TEST_CHECK(nng_pair_open(&s1) == 0); - TEST_CHECK(nng_setopt_ms(s1, NNG_OPT_RECVTIMEO, 10) == 0); - now = testutil_clock(); - TEST_CHECK(nng_recvmsg(s1, &msg, 0) == NNG_ETIMEDOUT); - TEST_CHECK(msg == NULL); - TEST_CHECK(testutil_clock() >= (now + 9)); - TEST_CHECK(testutil_clock() < (now + 500)); - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_recv_nonblock(void) -{ - nng_socket s1; - uint64_t now; - nng_msg * msg = NULL; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_setopt_ms(s1, NNG_OPT_RECVTIMEO, 10) == 0); - now = testutil_clock(); - TEST_CHECK(nng_recvmsg(s1, &msg, NNG_FLAG_NONBLOCK) == NNG_EAGAIN); - TEST_CHECK(msg == NULL); - TEST_CHECK(testutil_clock() < (now + 500)); - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_send_timeout(void) -{ - nng_socket s1; - uint64_t now; - nng_msg * msg; - - TEST_CHECK(nng_msg_alloc(&msg, 0) == 0); - TEST_CHECK(nng_pair_open(&s1) == 0); - TEST_CHECK(nng_setopt_ms(s1, NNG_OPT_SENDTIMEO, 100) == 0); - now = testutil_clock(); - TEST_CHECK(nng_sendmsg(s1, msg, 0) == NNG_ETIMEDOUT); - TEST_CHECK(testutil_clock() >= (now + 9)); - TEST_CHECK(testutil_clock() < (now + 500)); - nng_msg_free(msg); - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_send_nonblock(void) -{ - nng_socket s1; - uint64_t now; - nng_msg * msg; - - TEST_CHECK(nng_msg_alloc(&msg, 0) == 0); - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_setopt_ms(s1, NNG_OPT_SENDTIMEO, 500) == 0); - now = testutil_clock(); - TEST_CHECK(nng_sendmsg(s1, msg, NNG_FLAG_NONBLOCK) == NNG_EAGAIN); - TEST_CHECK(testutil_clock() < (now + 100)); - TEST_CHECK(nng_close(s1) == 0); - nng_msg_free(msg); -} - -void -test_readonly_options(void) -{ - nng_socket s1; - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_setopt_int(s1, NNG_OPT_RECVFD, 0) == NNG_EREADONLY); - TEST_CHECK(nng_setopt_int(s1, NNG_OPT_SENDFD, 0) == NNG_EREADONLY); - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_socket_base(void) -{ - nng_socket s1 = NNG_SOCKET_INITIALIZER; - - TEST_CHECK(nng_socket_id(s1) < 0); - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_socket_id(s1) > 0); - - // Cannot set bogus options - TEST_CHECK(nng_setopt_bool(s1, "BAD_OPT", false) == NNG_ENOTSUP); - - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_socket_name(void) -{ - nng_socket s1; - char name[128]; // 64 is max - char * str; - long id; - char * end; - size_t sz; - - sz = sizeof(name); - TEST_CHECK(nng_pair_open(&s1) == 0); - TEST_CHECK(nng_getopt(s1, NNG_OPT_SOCKNAME, name, &sz) == 0); - TEST_CHECK(sz > 0 && sz < 64); - TEST_CHECK(sz == strlen(name) + 1); - id = strtol(name, &end, 10); - TEST_CHECK(id == (long) s1.id); - TEST_CHECK(end != NULL && *end == '\0'); - - TEST_CHECK(nng_setopt(s1, NNG_OPT_SOCKNAME, "hello", 6) == 0); - sz = sizeof(name); - TEST_CHECK(nng_getopt(s1, NNG_OPT_SOCKNAME, name, &sz) == 0); - TEST_CHECK(sz == 6); - TEST_CHECK(strcmp(name, "hello") == 0); - - memset(name, 'A', 64); - name[64] = '\0'; - - // strings must be NULL terminated - TEST_CHECK(nng_setopt(s1, NNG_OPT_SOCKNAME, name, 5) == NNG_EINVAL); - - TEST_CHECK(nng_getopt_string(s1, NNG_OPT_SOCKNAME, &str) == 0); - TEST_ASSERT(str != NULL); - TEST_CHECK(strlen(str) == 5); - TEST_CHECK(strcmp(str, "hello") == 0); - nng_strfree(str); - - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_socket_name_oversize(void) -{ - nng_socket s1; - char name[256]; // 64 is max - size_t sz = sizeof(name); - - memset(name, 'A', sz); - TEST_CHECK(nng_pair_open(&s1) == 0); - - TEST_CHECK(nng_setopt(s1, NNG_OPT_SOCKNAME, name, sz) == NNG_EINVAL); - name[sz - 1] = '\0'; - TEST_CHECK(nng_setopt(s1, NNG_OPT_SOCKNAME, name, sz) == NNG_EINVAL); - - strcpy(name, "hello"); - TEST_CHECK(nng_setopt(s1, NNG_OPT_SOCKNAME, name, sz) == 0); - sz = sizeof(name); - memset(name, 'B', sz); - TEST_CHECK(nng_getopt(s1, NNG_OPT_SOCKNAME, name, &sz) == 0); - TEST_CHECK(sz == 6); - TEST_CHECK(strcmp(name, "hello") == 0); - - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_send_recv(void) -{ - nng_socket s1; - nng_socket s2; - int len; - size_t sz; - nng_duration to = 3000; // 3 seconds - char * buf; - char * a = "inproc://t1"; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_pair1_open(&s2) == 0); - - TEST_CHECK(nng_setopt_int(s1, NNG_OPT_RECVBUF, 1) == 0); - TEST_CHECK(nng_getopt_int(s1, NNG_OPT_RECVBUF, &len) == 0); - TEST_CHECK(len == 1); - - TEST_CHECK(nng_setopt_int(s1, NNG_OPT_SENDBUF, 1) == 0); - TEST_CHECK(nng_setopt_int(s2, NNG_OPT_SENDBUF, 1) == 0); - - TEST_CHECK(nng_setopt_ms(s1, NNG_OPT_SENDTIMEO, to) == 0); - TEST_CHECK(nng_setopt_ms(s1, NNG_OPT_RECVTIMEO, to) == 0); - TEST_CHECK(nng_setopt_ms(s2, NNG_OPT_SENDTIMEO, to) == 0); - TEST_CHECK(nng_setopt_ms(s2, NNG_OPT_RECVTIMEO, to) == 0); - - TEST_CHECK(nng_listen(s1, a, NULL, 0) == 0); - TEST_CHECK(nng_dial(s2, a, NULL, 0) == 0); - - TEST_CHECK(nng_send(s1, "abc", 4, 0) == 0); - TEST_CHECK(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC) == 0); - TEST_CHECK(buf != NULL); - TEST_CHECK(sz == 4); - TEST_CHECK(memcmp(buf, "abc", 4) == 0); - nng_free(buf, sz); - - TEST_CHECK(nng_close(s1) == 0); - TEST_CHECK(nng_close(s2) == 0); -} - -void -test_send_recv_zero_length(void) -{ - nng_socket s1; - nng_socket s2; - int len; - size_t sz; - nng_duration to = 3000; // 3 seconds - char * buf; - char * a = "inproc://send-recv-zero-length"; - - TEST_NNG_PASS(nng_pair1_open(&s1)); - TEST_NNG_PASS(nng_pair1_open(&s2)); - - TEST_NNG_PASS(nng_setopt_int(s1, NNG_OPT_RECVBUF, 1)); - TEST_NNG_PASS(nng_getopt_int(s1, NNG_OPT_RECVBUF, &len)); - TEST_CHECK(len == 1); - - TEST_NNG_PASS(nng_setopt_int(s1, NNG_OPT_SENDBUF, 1)); - TEST_NNG_PASS(nng_setopt_int(s2, NNG_OPT_SENDBUF, 1)); - - TEST_NNG_PASS(nng_setopt_ms(s1, NNG_OPT_SENDTIMEO, to)); - TEST_NNG_PASS(nng_setopt_ms(s1, NNG_OPT_RECVTIMEO, to)); - TEST_NNG_PASS(nng_setopt_ms(s2, NNG_OPT_SENDTIMEO, to)); - TEST_NNG_PASS(nng_setopt_ms(s2, NNG_OPT_RECVTIMEO, to)); - - TEST_NNG_PASS(nng_listen(s1, a, NULL, 0)); - TEST_NNG_PASS(nng_dial(s2, a, NULL, 0)); - - TEST_NNG_PASS(nng_send(s1, "", 0, 0)); - TEST_NNG_PASS(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC)); - TEST_CHECK(buf == NULL); - TEST_CHECK(sz == 0); - nng_free(buf, sz); - - TEST_NNG_PASS(nng_close(s1)); - TEST_NNG_PASS(nng_close(s2)); -} - -void -test_connection_refused(void) -{ - nng_socket s1; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_dial(s1, "inproc://no", NULL, 0) == NNG_ECONNREFUSED); - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_late_connection(void) -{ - char * buf; - size_t sz; - nng_socket s1; - nng_socket s2; - char * a = "inproc://asy"; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_pair1_open(&s2) == 0); - - TEST_CHECK(nng_setopt_ms(s1, NNG_OPT_RECONNMINT, 10) == 0); - TEST_CHECK(nng_setopt_ms(s1, NNG_OPT_RECONNMAXT, 10) == 0); - - TEST_CHECK(nng_dial(s1, a, NULL, NNG_FLAG_NONBLOCK) == 0); - TEST_CHECK(nng_listen(s2, a, NULL, 0) == 0); - nng_msleep(100); - TEST_CHECK(nng_send(s1, "abc", 4, 0) == 0); - TEST_CHECK(nng_recv(s2, &buf, &sz, NNG_FLAG_ALLOC) == 0); - TEST_CHECK(sz == 4); - TEST_CHECK(memcmp(buf, "abc", 4) == 0); - nng_free(buf, sz); - - TEST_CHECK(nng_close(s1) == 0); - TEST_CHECK(nng_close(s2) == 0); -} - -void -test_address_busy(void) -{ - char * a = "inproc://eaddrinuse"; - nng_listener l = NNG_LISTENER_INITIALIZER; - nng_dialer d = NNG_DIALER_INITIALIZER; - nng_socket s1; - nng_socket s2; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_pair1_open(&s2) == 0); - - TEST_CHECK(nng_listener_id(l) < 0); - TEST_CHECK(nng_listen(s1, a, &l, 0) == 0); - TEST_CHECK(nng_listener_id(l) > 0); - - // Cannot start another one. - TEST_CHECK(nng_listen(s1, a, NULL, 0) == NNG_EADDRINUSE); - - // We can't restart it -- it's already running - TEST_CHECK(nng_listener_start(l, 0) == NNG_ESTATE); - - // We can connect to it. - TEST_CHECK(nng_dialer_id(d) < 0); - TEST_CHECK(nng_dial(s2, a, &d, 0) == 0); - TEST_CHECK(nng_dialer_id(d) > 0); - - TEST_CHECK(nng_close(s1) == 0); - TEST_CHECK(nng_close(s2) == 0); -} - -void -test_endpoint_types(void) -{ - nng_socket s1; - nng_dialer d = NNG_DIALER_INITIALIZER; - nng_listener l = NNG_LISTENER_INITIALIZER; - nng_dialer d2; - nng_listener l2; - char * a = "inproc://mumble..."; - bool b; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - - TEST_CHECK(nng_dialer_id(d) < 0); - TEST_CHECK(nng_dialer_create(&d, s1, a) == 0); - TEST_CHECK(nng_dialer_id(d) > 0); - - // Forge a listener - l2.id = nng_dialer_id(d); - TEST_CHECK( - nng_listener_getopt_bool(l2, NNG_OPT_RAW, &b) == NNG_ENOENT); - TEST_CHECK(nng_listener_close(l2) == NNG_ENOENT); - TEST_CHECK(nng_dialer_close(d) == 0); - - TEST_CHECK(nng_listener_id(l) < 0); - TEST_CHECK(nng_listener_create(&l, s1, a) == 0); - TEST_CHECK(nng_listener_id(l) > 0); - - // Forge a dialer - d2.id = nng_listener_id(l); - TEST_CHECK(nng_dialer_getopt_bool(d2, NNG_OPT_RAW, &b) == NNG_ENOENT); - TEST_CHECK(nng_dialer_close(d2) == NNG_ENOENT); - TEST_CHECK(nng_listener_close(l) == 0); - - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_bad_url(void) -{ - nng_socket s1; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - TEST_CHECK(nng_dial(s1, "bogus://1", NULL, 0) == NNG_ENOTSUP); - TEST_CHECK(nng_listen(s1, "bogus://2", NULL, 0) == NNG_ENOTSUP); - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_url_option(void) -{ - nng_socket s1; - char url[NNG_MAXADDRLEN]; - nng_listener l; - nng_dialer d; - size_t sz; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - - // Listener - TEST_CHECK(nng_listener_create(&l, s1, "inproc://url1") == 0); - memset(url, 0, sizeof(url)); - sz = sizeof(url); - TEST_CHECK(nng_listener_getopt(l, NNG_OPT_URL, url, &sz) == 0); - TEST_CHECK(strcmp(url, "inproc://url1") == 0); - TEST_CHECK( - nng_listener_setopt(l, NNG_OPT_URL, url, sz) == NNG_EREADONLY); - sz = sizeof(url); - - // Dialer - TEST_CHECK(nng_dialer_create(&d, s1, "inproc://url2") == 0); - TEST_CHECK(nng_dialer_getopt(d, NNG_OPT_URL, url, &sz) == 0); - TEST_CHECK(strcmp(url, "inproc://url2") == 0); - TEST_CHECK( - nng_dialer_setopt(d, NNG_OPT_URL, url, sz) == NNG_EREADONLY); - - nng_close(s1); -} - -void -test_listener_options(void) -{ - nng_socket s1; - nng_listener l; - size_t sz; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - - // Create a listener with the specified options - TEST_CHECK(nng_setopt_size(s1, NNG_OPT_RECVMAXSZ, 543) == 0); - TEST_CHECK(nng_listener_create(&l, s1, "inproc://listener_opts") == 0); - TEST_CHECK(nng_listener_getopt_size(l, NNG_OPT_RECVMAXSZ, &sz) == 0); - TEST_CHECK(sz == 543); - - // Verify endpoint overrides - TEST_CHECK(nng_listener_setopt_size(l, NNG_OPT_RECVMAXSZ, 678) == 0); - TEST_CHECK(nng_listener_getopt_size(l, NNG_OPT_RECVMAXSZ, &sz) == 0); - TEST_CHECK(sz == 678); - TEST_CHECK(nng_getopt_size(s1, NNG_OPT_RECVMAXSZ, &sz) == 0); - TEST_CHECK(sz == 543); - - // And socket overrides again - TEST_CHECK(nng_setopt_size(s1, NNG_OPT_RECVMAXSZ, 911) == 0); - TEST_CHECK(nng_listener_getopt_size(l, NNG_OPT_RECVMAXSZ, &sz) == 0); - TEST_CHECK(sz == 911); - - // Cannot set invalid options - TEST_CHECK(nng_listener_setopt_size(l, "BAD_OPT", 1) == NNG_ENOTSUP); - TEST_CHECK(nng_listener_setopt_bool(l, NNG_OPT_RECVMAXSZ, true) == - NNG_EBADTYPE); - TEST_CHECK( - nng_listener_setopt(l, NNG_OPT_RECVMAXSZ, &sz, 1) == NNG_EINVAL); - - // Cannot set inappropriate options - TEST_CHECK(nng_listener_setopt_string(l, NNG_OPT_SOCKNAME, "1") == - NNG_ENOTSUP); - TEST_CHECK( - nng_listener_setopt_bool(l, NNG_OPT_RAW, true) == NNG_ENOTSUP); - TEST_CHECK( - nng_listener_setopt_ms(l, NNG_OPT_RECONNMINT, 1) == NNG_ENOTSUP); - TEST_CHECK(nng_listener_setopt_string(l, NNG_OPT_SOCKNAME, "bogus") == - NNG_ENOTSUP); - - // Read only options - TEST_CHECK(nng_listener_setopt_string( - l, NNG_OPT_URL, "inproc://junk") == NNG_EREADONLY); - - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_dialer_options(void) -{ - nng_socket s1; - nng_dialer d; - size_t sz; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - - // Create a listener with the specified options - TEST_CHECK(nng_setopt_size(s1, NNG_OPT_RECVMAXSZ, 543) == 0); - TEST_CHECK(nng_dialer_create(&d, s1, "inproc://dialer_opts") == 0); - TEST_CHECK(nng_dialer_getopt_size(d, NNG_OPT_RECVMAXSZ, &sz) == 0); - TEST_CHECK(sz == 543); - - // Verify endpoint overrides - TEST_CHECK(nng_dialer_setopt_size(d, NNG_OPT_RECVMAXSZ, 678) == 0); - TEST_CHECK(nng_dialer_getopt_size(d, NNG_OPT_RECVMAXSZ, &sz) == 0); - TEST_CHECK(sz == 678); - TEST_CHECK(nng_getopt_size(s1, NNG_OPT_RECVMAXSZ, &sz) == 0); - TEST_CHECK(sz == 543); - - // And socket overrides again - TEST_CHECK(nng_setopt_size(s1, NNG_OPT_RECVMAXSZ, 911) == 0); - TEST_CHECK(nng_dialer_getopt_size(d, NNG_OPT_RECVMAXSZ, &sz) == 0); - TEST_CHECK(sz == 911); - - // Cannot set invalid options - TEST_CHECK(nng_dialer_setopt_size(d, "BAD_OPT", 1) == NNG_ENOTSUP); - TEST_CHECK(nng_dialer_setopt_bool(d, NNG_OPT_RECVMAXSZ, true) == - NNG_EBADTYPE); - TEST_CHECK( - nng_dialer_setopt(d, NNG_OPT_RECVMAXSZ, &sz, 1) == NNG_EINVAL); - - // Cannot set inappropriate options - TEST_CHECK( - nng_dialer_setopt_string(d, NNG_OPT_SOCKNAME, "1") == NNG_ENOTSUP); - TEST_CHECK( - nng_dialer_setopt_bool(d, NNG_OPT_RAW, true) == NNG_ENOTSUP); - TEST_CHECK( - nng_dialer_setopt_ms(d, NNG_OPT_SENDTIMEO, 1) == NNG_ENOTSUP); - TEST_CHECK(nng_dialer_setopt_string(d, NNG_OPT_SOCKNAME, "bogus") == - NNG_ENOTSUP); - - // Read only options - TEST_CHECK(nng_dialer_setopt_string(d, NNG_OPT_URL, "inproc://junk") == - NNG_EREADONLY); - - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_endpoint_absent_options(void) -{ - size_t s; - int i; - nng_duration t; - bool b; - nng_dialer d; - nng_listener l; - d.id = 1999; - l.id = 1999; - - TEST_CHECK( - nng_dialer_setopt_size(d, NNG_OPT_RECVMAXSZ, 10) == NNG_ENOENT); - TEST_CHECK( - nng_listener_setopt_size(l, NNG_OPT_RECVMAXSZ, 10) == NNG_ENOENT); - - TEST_CHECK(nng_dialer_getopt_bool(d, NNG_OPT_RAW, &b) == NNG_ENOENT); - TEST_CHECK(nng_listener_getopt_bool(l, NNG_OPT_RAW, &b) == NNG_ENOENT); - - TEST_CHECK( - nng_dialer_getopt_size(d, NNG_OPT_RECVMAXSZ, &s) == NNG_ENOENT); - TEST_CHECK( - nng_listener_getopt_size(l, NNG_OPT_RECVMAXSZ, &s) == NNG_ENOENT); - - TEST_CHECK(nng_dialer_getopt_int(d, NNG_OPT_RAW, &i) == NNG_ENOENT); - TEST_CHECK(nng_listener_getopt_int(l, NNG_OPT_RAW, &i) == NNG_ENOENT); - - TEST_CHECK( - nng_dialer_getopt_ms(d, NNG_OPT_RECVTIMEO, &t) == NNG_ENOENT); - TEST_CHECK( - nng_listener_getopt_ms(l, NNG_OPT_SENDTIMEO, &t) == NNG_ENOENT); -} - -void -test_timeout_options(void) -{ - nng_socket s1; - nng_duration to; - size_t sz; - - char *cases[] = { - NNG_OPT_RECVTIMEO, - NNG_OPT_SENDTIMEO, - NNG_OPT_RECONNMAXT, - NNG_OPT_RECONNMINT, - NULL, - }; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - for (int i = 0; cases[i] != NULL; i++) { - bool b; - TEST_CASE(cases[i]); - - // Can't receive a duration into zero bytes. - sz = 0; - TEST_CHECK(nng_getopt(s1, cases[i], &to, &sz) == NNG_EINVAL); - - // Type mismatches - TEST_CHECK(nng_getopt_bool(s1, cases[i], &b) == NNG_EBADTYPE); - sz = 1; - TEST_CHECK(nng_getopt(s1, cases[i], &b, &sz) == NNG_EINVAL); - - // Can set a valid duration - TEST_CHECK(nng_setopt_ms(s1, cases[i], 1234) == 0); - TEST_CHECK(nng_getopt_ms(s1, cases[i], &to) == 0); - TEST_CHECK(to == 1234); - - to = 0; - sz = sizeof(to); - TEST_CHECK(nng_getopt(s1, cases[i], &to, &sz) == 0); - TEST_CHECK(to == 1234); - TEST_CHECK(sz == sizeof(to)); - - // Can't set a negative duration - TEST_CHECK(nng_setopt_ms(s1, cases[i], -5) == NNG_EINVAL); - - // Can't pass a buf too small for duration - sz = sizeof(to) - 1; - to = 1; - TEST_CHECK(nng_setopt(s1, cases[i], &to, sz) == NNG_EINVAL); - } - TEST_CHECK(nng_close(s1) == 0); -} - -void -test_size_options(void) -{ - nng_socket s1; - size_t val; - size_t sz; - char * opt; - - char *cases[] = { - NNG_OPT_RECVMAXSZ, - NULL, - }; - - TEST_CHECK(nng_pair1_open(&s1) == 0); - for (int i = 0; (opt = cases[i]) != NULL; i++) { - TEST_CASE(opt); - - // Can't receive a size into zero bytes. - sz = 0; - TEST_CHECK(nng_getopt(s1, opt, &val, &sz) == NNG_EINVAL); - - // Can set a valid duration - TEST_CHECK(nng_setopt_size(s1, opt, 1234) == 0); - TEST_CHECK(nng_getopt_size(s1, opt, &val) == 0); - TEST_CHECK(val == 1234); - - val = 0; - sz = sizeof(val); - TEST_CHECK(nng_getopt(s1, opt, &val, &sz) == 0); - TEST_CHECK(val == 1234); - TEST_CHECK(sz == sizeof(val)); - - // Can't pass a buf too small for size - sz = sizeof(val) - 1; - val = 1; - TEST_CHECK(nng_setopt(s1, opt, &val, sz) == NNG_EINVAL); - - // We limit the limit to 4GB. Clear it if you want to - // ship more than 4GB at a time. -#if defined(_WIN64) || defined(_LP64) - val = 0x10000u; - val <<= 30u; - TEST_CHECK(nng_setopt_size(s1, opt, val) == NNG_EINVAL); - TEST_CHECK(nng_getopt_size(s1, opt, &val) == 0); - TEST_CHECK(val == 1234); -#endif - } - TEST_CHECK(nng_close(s1) == 0); -} - -TEST_LIST = { - { "recv timeout", test_recv_timeout }, - { "recv non-block", test_recv_nonblock }, - { "send timeout", test_send_timeout }, - { "send non-block", test_send_nonblock }, - { "read only options", test_readonly_options }, - { "socket base", test_socket_base }, - { "socket name", test_socket_name }, - { "socket name oversize", test_socket_name_oversize }, - { "send recv", test_send_recv }, - { "send recv zero length", test_send_recv_zero_length }, - { "connection refused", test_connection_refused }, - { "late connection", test_late_connection }, - { "address busy", test_address_busy }, - { "bad url", test_bad_url }, - { "url option", test_url_option }, - { "listener options", test_listener_options }, - { "dialer options", test_dialer_options }, - { "timeout options", test_timeout_options }, - { "size options", test_size_options }, - { "endpoint absent options", test_endpoint_absent_options }, - { "endpoint types", test_endpoint_types }, - - { NULL, NULL }, -}; diff --git a/tests/tcp.c b/tests/tcp.c index 1288af01..eecc148a 100644 --- a/tests/tcp.c +++ b/tests/tcp.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // Copyright 2018 Devolutions <info@devolutions.net> // @@ -63,205 +63,5 @@ check_props_v4(nng_msg *msg) TestMain("TCP Transport", { trantest_test_extended("tcp://127.0.0.1:%u", check_props_v4); - Convey("We cannot connect to wild cards", { - nng_socket s; - char addr[NNG_MAXADDRLEN]; - - So(nng_pair_open(&s) == 0); - Reset({ nng_close(s); }); - trantest_next_address(addr, "tcp://*:%u"); - So(nng_dial(s, addr, NULL, 0) == NNG_EADDRINVAL); - }); - - Convey("We can bind to wild card", { - nng_socket s1; - nng_socket s2; - char addr[NNG_MAXADDRLEN]; - - So(nng_pair_open(&s1) == 0); - So(nng_pair_open(&s2) == 0); - Reset({ - nng_close(s2); - nng_close(s1); - }); - trantest_next_address(addr, "tcp4://*:%u"); - So(nng_listen(s1, addr, NULL, 0) == 0); - // reset port back one - trantest_prev_address(addr, "tcp://127.0.0.1:%u"); - So(nng_dial(s2, addr, NULL, 0) == 0); - }); - - Convey("We can bind to port zero", { - nng_socket s1; - nng_socket s2; - nng_sockaddr sa; - nng_listener l; - char * addr; - - So(nng_pair_open(&s1) == 0); - So(nng_pair_open(&s2) == 0); - Reset({ - nng_close(s2); - nng_close(s1); - }); - So(nng_listen(s1, "tcp://127.0.0.1:0", &l, 0) == 0); - So(nng_listener_getopt_string(l, NNG_OPT_URL, &addr) == 0); - So(memcmp(addr, "tcp://", 6) == 0); - So(nng_listener_getopt_sockaddr(l, NNG_OPT_LOCADDR, &sa) == 0); - So(sa.s_in.sa_family == NNG_AF_INET); - So(sa.s_in.sa_port != 0); - So(sa.s_in.sa_addr = htonl(0x7f000001)); - So(nng_dial(s2, addr, NULL, 0) == 0); - nng_strfree(addr); - }); - - Convey("We can use local interface to connect", { - nng_socket s1; - nng_socket s2; - char addr[NNG_MAXADDRLEN]; - - So(nng_pair_open(&s1) == 0); - So(nng_pair_open(&s2) == 0); - Reset({ - nng_close(s2); - nng_close(s1); - }); - trantest_next_address(addr, "tcp://127.0.0.1:%u"); - So(nng_listen(s1, addr, NULL, 0) == 0); - // reset port back one - trantest_prev_address(addr, "tcp://127.0.0.1;127.0.0.1:%u"); - So(nng_dial(s2, addr, NULL, 0) == 0); - }); - - Convey("Botched local interfaces fail resonably", { - nng_socket s1; - - So(nng_pair_open(&s1) == 0); - Reset({ nng_close(s1); }); - So(nng_dial(s1, "tcp://1x.2;127.0.0.1:80", NULL, 0) == - NNG_EADDRINVAL); - }); - - Convey("Can't specify address that isn't ours", { - nng_socket s1; - - So(nng_pair_open(&s1) == 0); - Reset({ nng_close(s1); }); - So(nng_dial(s1, "tcp://8.8.8.8;127.0.0.1:80", NULL, 0) == - NNG_EADDRINVAL); - }); - - Convey("Malformed TCP addresses do not panic", { - nng_socket s1; - - So(nng_pair_open(&s1) == 0); - Reset({ nng_close(s1); }); - So(nng_dial(s1, "tcp://127.0.0.1", NULL, 0) == NNG_EADDRINVAL); - So(nng_dial(s1, "tcp://127.0.0.1.32", NULL, 0) == - NNG_EADDRINVAL); - So(nng_dial(s1, "tcp://127.0.x.1.32", NULL, 0) == - NNG_EADDRINVAL); - So(nng_listen(s1, "tcp://127.0.0.1.32", NULL, 0) == - NNG_EADDRINVAL); - So(nng_listen(s1, "tcp://127.0.x.1.32", NULL, 0) == - NNG_EADDRINVAL); - }); - - Convey("No delay option", { - nng_socket s; - nng_dialer d; - nng_listener l; - bool v; - int x; - - So(nng_pair_open(&s) == 0); - Reset({ nng_close(s); }); - So(nng_getopt_bool(s, NNG_OPT_TCP_NODELAY, &v) == 0); - So(v == true); - So(nng_dialer_create(&d, s, "tcp://127.0.0.1:4999") == 0); - So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_NODELAY, &v) == 0); - So(v == true); - So(nng_dialer_setopt_bool(d, NNG_OPT_TCP_NODELAY, false) == 0); - So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_NODELAY, &v) == 0); - So(v == false); - So(nng_dialer_getopt_int(d, NNG_OPT_TCP_NODELAY, &x) == - NNG_EBADTYPE); - x = 0; - So(nng_dialer_setopt_int(d, NNG_OPT_TCP_NODELAY, x) == - NNG_EBADTYPE); - // This assumes sizeof (bool) != sizeof (int) - So(nng_dialer_setopt(d, NNG_OPT_TCP_NODELAY, &x, sizeof(x)) == - NNG_EINVAL); - - So(nng_listener_create(&l, s, "tcp://127.0.0.1:4999") == 0); - So(nng_listener_getopt_bool(l, NNG_OPT_TCP_NODELAY, &v) == 0); - So(v == true); - x = 0; - So(nng_listener_setopt_int(l, NNG_OPT_TCP_NODELAY, x) == - NNG_EBADTYPE); - // This assumes sizeof (bool) != sizeof (int) - So(nng_listener_setopt( - l, NNG_OPT_TCP_NODELAY, &x, sizeof(x)) == NNG_EINVAL); - - nng_dialer_close(d); - nng_listener_close(l); - - // Make sure socket wide defaults apply. - So(nng_setopt_bool(s, NNG_OPT_TCP_NODELAY, true) == 0); - v = false; - So(nng_getopt_bool(s, NNG_OPT_TCP_NODELAY, &v) == 0); - So(v == true); - So(nng_setopt_bool(s, NNG_OPT_TCP_NODELAY, false) == 0); - So(nng_dialer_create(&d, s, "tcp://127.0.0.1:4999") == 0); - So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_NODELAY, &v) == 0); - So(v == false); - }); - - Convey("Keepalive option", { - nng_socket s; - nng_dialer d; - nng_listener l; - bool v; - int x; - - So(nng_pair_open(&s) == 0); - Reset({ nng_close(s); }); - So(nng_getopt_bool(s, NNG_OPT_TCP_KEEPALIVE, &v) == 0); - So(v == false); - So(nng_dialer_create(&d, s, "tcp://127.0.0.1:4999") == 0); - So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_KEEPALIVE, &v) == 0); - So(v == false); - So(nng_dialer_setopt_bool(d, NNG_OPT_TCP_KEEPALIVE, true) == - 0); - So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_KEEPALIVE, &v) == 0); - So(v == true); - So(nng_dialer_getopt_int(d, NNG_OPT_TCP_KEEPALIVE, &x) == - NNG_EBADTYPE); - x = 1; - So(nng_dialer_setopt_int(d, NNG_OPT_TCP_KEEPALIVE, x) == - NNG_EBADTYPE); - - So(nng_listener_create(&l, s, "tcp://127.0.0.1:4999") == 0); - So(nng_listener_getopt_bool(l, NNG_OPT_TCP_KEEPALIVE, &v) == - 0); - So(v == false); - x = 1; - So(nng_listener_setopt_int(l, NNG_OPT_TCP_KEEPALIVE, x) == - NNG_EBADTYPE); - - nng_dialer_close(d); - nng_listener_close(l); - - // Make sure socket wide defaults apply. - So(nng_setopt_bool(s, NNG_OPT_TCP_KEEPALIVE, false) == 0); - v = true; - So(nng_getopt_bool(s, NNG_OPT_TCP_KEEPALIVE, &v) == 0); - So(v == false); - So(nng_setopt_bool(s, NNG_OPT_TCP_KEEPALIVE, true) == 0); - So(nng_dialer_create(&d, s, "tcp://127.0.0.1:4999") == 0); - So(nng_dialer_getopt_bool(d, NNG_OPT_TCP_KEEPALIVE, &v) == 0); - So(v == true); - }); - nng_fini(); }) diff --git a/tests/url.c b/tests/url.c deleted file mode 100644 index 847b7df3..00000000 --- a/tests/url.c +++ /dev/null @@ -1,479 +0,0 @@ -// -// 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 -// file was obtained (LICENSE.txt). A copy of the license may also be -// found online at https://opensource.org/licenses/MIT. -// - - -#include "acutest.h" - -#include <string.h> - -#include <nng/nng.h> - -#include "core/url.h" - -#include "testutil.h" - -void -test_url_host(void) -{ - nng_url *url; - - TEST_NNG_PASS(nng_url_parse(&url, "http://www.google.com")); - TEST_ASSERT(url != NULL); - TEST_CHECK(strcmp(url->u_scheme, "http") == 0); - TEST_CHECK(strcmp(url->u_host, "www.google.com") == 0); - TEST_CHECK(strcmp(url->u_hostname, "www.google.com") == 0); - TEST_CHECK(strcmp(url->u_port, "80") == 0); - TEST_CHECK(strcmp(url->u_path, "") == 0); - TEST_CHECK(strcmp(url->u_requri, "") == 0); - TEST_CHECK(url->u_query == NULL); - TEST_CHECK(url->u_fragment == NULL); - TEST_CHECK(url->u_userinfo == NULL); - nng_url_free(url); -} - -void -test_url_host_port(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "http://www.google.com:1234")); - TEST_ASSERT(url != NULL); - TEST_CHECK(strcmp(url->u_scheme, "http") == 0); - TEST_CHECK(strcmp(url->u_host, "www.google.com:1234") == 0); - TEST_CHECK(strcmp(url->u_hostname, "www.google.com") == 0); - TEST_CHECK(strcmp(url->u_port, "1234") == 0); - TEST_CHECK(strcmp(url->u_path, "") == 0); - TEST_CHECK(strcmp(url->u_requri, "") == 0); - TEST_CHECK(url->u_query == NULL); - TEST_CHECK(url->u_fragment == NULL); - TEST_CHECK(url->u_userinfo == NULL); - nng_url_free(url); -} - -void -test_url_host_port_path(void) -{ - nng_url *url; - - TEST_NNG_PASS( - nng_url_parse(&url, "http://www.google.com:1234/somewhere")); - TEST_ASSERT(url != NULL); - TEST_CHECK(strcmp(url->u_scheme, "http") == 0); - TEST_CHECK(strcmp(url->u_host, "www.google.com:1234") == 0); - TEST_CHECK(strcmp(url->u_hostname, "www.google.com") == 0); - TEST_CHECK(strcmp(url->u_port, "1234") == 0); - TEST_CHECK(strcmp(url->u_path, "/somewhere") == 0); - TEST_CHECK(strcmp(url->u_requri, "/somewhere") == 0); - TEST_CHECK(url->u_userinfo == NULL); - TEST_CHECK(url->u_query == NULL); - TEST_CHECK(url->u_fragment == NULL); - nng_url_free(url); -} - -void -test_url_user_info(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse( - &url, "http://garrett@www.google.com:1234/somewhere")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_userinfo, "garrett"); - TEST_STREQUAL(url->u_host, "www.google.com:1234"); - TEST_STREQUAL(url->u_hostname, "www.google.com"); - TEST_STREQUAL(url->u_port, "1234"); - TEST_STREQUAL(url->u_path, "/somewhere"); - TEST_STREQUAL(url->u_requri, "/somewhere"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_fragment); - nng_url_free(url); -} - -void -test_url_path_query_param(void) -{ - nng_url *url; - TEST_NNG_PASS( - nng_url_parse(&url, "http://www.google.com/somewhere?result=yes")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_host, "www.google.com"); - TEST_STREQUAL(url->u_hostname, "www.google.com"); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_path, "/somewhere"); - TEST_STREQUAL(url->u_query, "result=yes"); - TEST_STREQUAL(url->u_requri, "/somewhere?result=yes"); - TEST_NULL(url->u_userinfo); - TEST_NULL(url->u_fragment); - nng_url_free(url); -} - -void -test_url_query_param_anchor(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, - "http://www.google.com/" - "somewhere?result=yes#chapter1")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_host, "www.google.com"); - TEST_STREQUAL(url->u_hostname, "www.google.com"); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_path, "/somewhere"); - TEST_STREQUAL(url->u_query, "result=yes"); - TEST_STREQUAL(url->u_fragment, "chapter1"); - TEST_STREQUAL(url->u_requri, "/somewhere?result=yes#chapter1"); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_path_anchor(void) -{ - nng_url *url; - TEST_NNG_PASS( - nng_url_parse(&url, "http://www.google.com/somewhere#chapter2")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_host, "www.google.com"); - TEST_STREQUAL(url->u_hostname, "www.google.com"); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_path, "/somewhere"); - TEST_STREQUAL(url->u_fragment, "chapter2"); - TEST_STREQUAL(url->u_requri, "/somewhere#chapter2"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_anchor(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "http://www.google.com#chapter3")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_host, "www.google.com"); - TEST_STREQUAL(url->u_hostname, "www.google.com"); - TEST_STREQUAL(url->u_path, ""); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_fragment, "chapter3"); - TEST_STREQUAL(url->u_requri, "#chapter3"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_query_param(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "http://www.google.com?color=red")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_host, "www.google.com"); - TEST_STREQUAL(url->u_hostname, "www.google.com"); - TEST_STREQUAL(url->u_path, ""); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_query, "color=red"); - TEST_STREQUAL(url->u_requri, "?color=red"); - TEST_ASSERT(url != NULL); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_v6_host(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "http://[::1]")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_host, "[::1]"); - TEST_STREQUAL(url->u_hostname, "::1"); - TEST_STREQUAL(url->u_path, ""); - TEST_STREQUAL(url->u_port, "80"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_fragment); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_v6_host_port(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "http://[::1]:29")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_host, "[::1]:29"); - TEST_STREQUAL(url->u_hostname, "::1"); - TEST_STREQUAL(url->u_path, ""); - TEST_STREQUAL(url->u_port, "29"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_fragment); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_v6_host_port_path(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "http://[::1]:29/bottles")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_host, "[::1]:29"); - TEST_STREQUAL(url->u_hostname, "::1"); - TEST_STREQUAL(url->u_path, "/bottles"); - TEST_STREQUAL(url->u_port, "29"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_fragment); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_tcp_port(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "tcp://:9876/")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "tcp"); - TEST_STREQUAL(url->u_host, ":9876"); - TEST_STREQUAL(url->u_hostname, ""); - TEST_STREQUAL(url->u_path, "/"); - TEST_STREQUAL(url->u_port, "9876"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_fragment); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_bare_ws(void) -{ - nng_url *url; - - TEST_NNG_PASS(nng_url_parse(&url, "ws://")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "ws"); - TEST_STREQUAL(url->u_host, ""); - TEST_STREQUAL(url->u_hostname, ""); - TEST_STREQUAL(url->u_path, ""); - TEST_STREQUAL(url->u_port, "80"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_fragment); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_ws_wildcard(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "ws://*:12345/foobar")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "ws"); - TEST_STREQUAL(url->u_host, ":12345"); - TEST_STREQUAL(url->u_hostname, ""); - TEST_STREQUAL(url->u_path, "/foobar"); - TEST_STREQUAL(url->u_port, "12345"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_fragment); - TEST_NULL(url->u_userinfo); - nng_url_free(url); -} - -void -test_url_ssh(void) -{ - nng_url *url; - TEST_NNG_PASS(nng_url_parse(&url, "ssh://user@host.example.com")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "ssh"); - TEST_STREQUAL(url->u_host, "host.example.com"); - TEST_STREQUAL(url->u_hostname, "host.example.com"); - TEST_STREQUAL(url->u_path, ""); - TEST_STREQUAL(url->u_port, "22"); - TEST_NULL(url->u_query); - TEST_NULL(url->u_fragment); - TEST_STREQUAL(url->u_userinfo, "user"); - nng_url_free(url); -} - -void -test_url_bad_scheme(void) -{ - nng_url *url = NULL; - TEST_NNG_FAIL(nng_url_parse(&url, "www.google.com"), NNG_EINVAL); - TEST_NULL(url); - TEST_NNG_FAIL(nng_url_parse(&url, "http:www.google.com"), NNG_EINVAL); - TEST_NULL(url); -} - -void -test_url_bad_ipv6(void) -{ - nng_url *url = NULL; - TEST_NNG_FAIL(nng_url_parse(&url, "http://[::1"), NNG_EINVAL); - TEST_NULL(url); - TEST_NNG_FAIL(nng_url_parse(&url, "http://[::1]bogus"), NNG_EINVAL); - TEST_NULL(url); -} - -void -test_url_canonify(void) -{ - nng_url *url = NULL; - TEST_NNG_PASS(nng_url_parse( - &url, "hTTp://www.EXAMPLE.com/bogus/.%2e/%7egarrett")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_hostname, "www.example.com"); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_path, "/~garrett"); - nng_url_free(url); -} - -void -test_url_path_resolve(void) -{ - nng_url *url = NULL; - TEST_NNG_PASS( - nng_url_parse(&url, "http://www.x.com//abc/def/./x/..///./../y")); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_hostname, "www.x.com"); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_path, "/abc/y"); - nng_url_free(url); -} - -void -test_url_query_info_pass(void) -{ - nng_url *url = NULL; - TEST_NNG_PASS( - nng_url_parse(&url, "http://www.x.com/?/abc/def/./x/.././../y")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_hostname, "www.x.com"); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_path, "/"); - TEST_STREQUAL(url->u_query, "/abc/def/./x/.././../y"); - nng_url_free(url); -} - -void -test_url_bad_utf8(void) -{ - nng_url *url = NULL; - TEST_NNG_FAIL(nng_url_parse(&url, "http://x.com/x%80x"), NNG_EINVAL); - TEST_NULL(url); - TEST_NNG_FAIL(nng_url_parse(&url, "http://x.com/x%c0%81"), NNG_EINVAL); - TEST_NULL(url); -} - -void -test_url_good_utf8(void) -{ - nng_url *url = NULL; - TEST_NNG_PASS(nng_url_parse(&url, "http://www.x.com/%c2%a2_cents")); - TEST_ASSERT(url != NULL); - TEST_STREQUAL(url->u_scheme, "http"); - TEST_STREQUAL(url->u_hostname, "www.x.com"); - TEST_STREQUAL(url->u_port, "80"); - TEST_STREQUAL(url->u_path, "/\xc2\xa2_cents"); - nng_url_free(url); -} - -void -test_url_decode(void) -{ - uint8_t out[16]; - size_t len; - - out[3] = 'x'; - len = nni_url_decode(out, "abc", 3); - TEST_CHECK(len == 3); - TEST_CHECK(memcmp(out, "abc", 3) == 0); - TEST_CHECK(out[3] == 'x'); - - len = nni_url_decode(out, "x%00y", 3); // embedded NULL - TEST_CHECK(len == 3); - TEST_CHECK(memcmp(out, "x\x00y", 3) == 0); - TEST_CHECK(out[3] == 'x'); - - len = nni_url_decode(out, "%3987", 3); - TEST_CHECK(len == 3); - TEST_CHECK(memcmp(out, "987", 3) == 0); - TEST_CHECK(out[3] == 'x'); - - len = nni_url_decode(out, "78%39", 3); - TEST_CHECK(len == 3); - TEST_CHECK(memcmp(out, "789", 3) == 0); - TEST_CHECK(out[3] == 'x'); - - len = nni_url_decode(out, "", 5); - TEST_CHECK(len == 0); - TEST_CHECK(memcmp(out, "789", 3) == 0); - TEST_CHECK(out[3] == 'x'); - - len = nni_url_decode(out, "be", 2); - TEST_CHECK(len == 2); - TEST_CHECK(memcmp(out, "be9", 3) == 0); - TEST_CHECK(out[3] == 'x'); - - len = nni_url_decode(out, "78%39", 2); - TEST_CHECK(len == (size_t) -1); - - len = nni_url_decode(out, "", 2); - TEST_CHECK(len == 0); - - len = nni_url_decode(out, "78%", 5); - TEST_CHECK(len == (size_t) -1); - - len = nni_url_decode(out, "78%xy", 5); - TEST_CHECK(len == (size_t) -1); - - len = nni_url_decode(out, "78%1$", 5); - TEST_CHECK(len == (size_t) -1); - - len = nni_url_decode(out, "%%20", 5); - TEST_CHECK(len == (size_t) -1); -} - -TEST_LIST = { - { "url host", test_url_host }, - { "url host port", test_url_host_port }, - { "url host port path", test_url_host_port_path }, - { "url user info", test_url_user_info }, - { "url path query param", test_url_path_query_param }, - { "url query param anchor", test_url_query_param_anchor }, - { "url path anchor", test_url_path_anchor }, - { "url anchor", test_url_anchor }, - { "url query param", test_url_query_param }, - { "url v6 host", test_url_v6_host }, - { "url v6 host port", test_url_v6_host_port }, - { "url v6 host port path", test_url_v6_host_port_path }, - { "url tcp port", test_url_tcp_port }, - { "url bare ws", test_url_bare_ws }, - { "url ws wildcard", test_url_ws_wildcard }, - { "url ssh", test_url_ssh }, - { "url bad scheme", test_url_bad_scheme }, - { "url bad v6", test_url_bad_ipv6 }, - { "url canonify", test_url_canonify }, - { "url path resolve", test_url_path_resolve }, - { "url query info pass", test_url_query_info_pass }, - { "url bad utf8", test_url_bad_utf8 }, - { "url good utf8", test_url_good_utf8 }, - { "url decode", test_url_decode }, - { NULL, NULL }, -};
\ No newline at end of file |
