diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-09-29 15:27:08 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-09-29 20:00:36 -0700 |
| commit | 869d0eeb20657cd6d2e87d8c4836b086c6be448d (patch) | |
| tree | 4731bf410cf36fb3442861575807dbb83400a3b3 /tests | |
| parent | 82d17f6365a95a500c32ae3a4ad40ff6fb609f3e (diff) | |
| download | nng-869d0eeb20657cd6d2e87d8c4836b086c6be448d.tar.gz nng-869d0eeb20657cd6d2e87d8c4836b086c6be448d.tar.bz2 nng-869d0eeb20657cd6d2e87d8c4836b086c6be448d.zip | |
Windows UDP support.
This implements the basic UDP functionality for Windows (required
for ZeroTier for example). We have also introduced a UDP test suite
to validate that this actually works. While here a few Windows
compilation warnings / nits were fixed.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | tests/trantest.h | 2 | ||||
| -rw-r--r-- | tests/udp.c | 100 |
3 files changed, 102 insertions, 2 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fdfd6055..70c9de3a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -127,7 +127,7 @@ add_nng_test(message 5) add_nng_test(device 5) add_nng_test(errors 2) add_nng_test(pair1 5) - +add_nng_test(udp 5) if (NNG_HAVE_ZEROTIER) add_nng_test(zt 60) endif() diff --git a/tests/trantest.h b/tests/trantest.h index b6843ea6..4b4d0335 100644 --- a/tests/trantest.h +++ b/tests/trantest.h @@ -183,7 +183,7 @@ trantest_send_recv_large(trantest *tt) size = 1024 * 128; // bigger than any transport segment So((data = nng_alloc(size)) != NULL); - for (int i = 0; i < size; i++) { + for (int i = 0; (size_t) i < size; i++) { data[i] = nni_random() & 0xff; } diff --git a/tests/udp.c b/tests/udp.c new file mode 100644 index 00000000..7f681ae1 --- /dev/null +++ b/tests/udp.c @@ -0,0 +1,100 @@ +// +// Copyright 2017 Garrett D'Amore <garrett@damore.org> +// Copyright 2017 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 "convey.h" +#include "trantest.h" + +#ifndef _WIN32 +#include <arpa/inet.h> +#endif + +// Basic UDP tests. +#include "core/nng_impl.h" + +TestMain("UDP support", { + + nni_init(); + atexit(nng_fini); + + trantest_port = trantest_port ? trantest_port : 5555; + + Convey("We can start a pair of UDP ports", { + nng_sockaddr sa1; + nng_sockaddr sa2; + uint16_t p1; + uint16_t p2; + nni_plat_udp *u1; + nni_plat_udp *u2; + uint32_t loopback; + + loopback = htonl(0x7f000001); // 127.0.0.1 + + sa1.s_un.s_in.sa_family = NNG_AF_INET; + sa1.s_un.s_in.sa_addr = loopback; + sa1.s_un.s_in.sa_port = trantest_port++; + + sa2.s_un.s_in.sa_family = NNG_AF_INET; + sa2.s_un.s_in.sa_addr = loopback; + sa2.s_un.s_in.sa_port = trantest_port++; + + p1 = sa1.s_un.s_in.sa_port; + p2 = sa2.s_un.s_in.sa_port; + + So(nni_plat_udp_open(&u1, &sa1) == 0); + So(nni_plat_udp_open(&u2, &sa2) == 0); + Reset({ + nni_plat_udp_close(u1); + nni_plat_udp_close(u2); + }); + + Convey("They can talk to each other", { + char msg[] = "hello"; + char rbuf[1024]; + nng_sockaddr to; + nng_sockaddr from; + nni_aio * aio1; + nni_aio * aio2; + + nni_aio_init(&aio1, NULL, NULL); + nni_aio_init(&aio2, NULL, NULL); + + to = sa2; + aio1->a_niov = 1; + aio1->a_iov[0].iov_buf = msg; + aio1->a_iov[0].iov_len = strlen(msg) + 1; + aio1->a_addr = &to; + + aio2->a_niov = 1; + aio2->a_iov[0].iov_buf = rbuf; + aio2->a_iov[0].iov_len = 1024; + aio2->a_addr = &from; + + nni_plat_udp_recv(u2, aio2); + nni_plat_udp_send(u1, aio1); + nni_aio_wait(aio1); + nni_aio_wait(aio2); + + So(nni_aio_result(aio1) == 0); + So(nni_aio_result(aio2) == 0); + + So(nni_aio_count(aio2) == strlen(msg) + 1); + So(strcmp(msg, rbuf) == 0); + + So(from.s_un.s_in.sa_family == + sa1.s_un.s_in.sa_family); + So(from.s_un.s_in.sa_port == sa1.s_un.s_in.sa_port); + So(from.s_un.s_in.sa_addr == sa1.s_un.s_in.sa_addr); + + nni_aio_fini(aio1); + nni_aio_fini(aio2); + }); + }); + +}); |
