diff options
| author | Garrett D'Amore <garrett@damore.org> | 2019-12-21 10:20:55 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2019-12-24 00:34:29 -0800 |
| commit | 3f7561417bec08226bcfeb107d94be0dbf71b09e (patch) | |
| tree | 409901d7929df5eeb7295ab971b34c2e1040f507 /tests/bufsz.c | |
| parent | 9e7a4aff25139703bbc375b6dda263d6d42341a8 (diff) | |
| download | nng-3f7561417bec08226bcfeb107d94be0dbf71b09e.tar.gz nng-3f7561417bec08226bcfeb107d94be0dbf71b09e.tar.bz2 nng-3f7561417bec08226bcfeb107d94be0dbf71b09e.zip | |
fixes #1032 Figure out Darwin bustedness
fixes #1035 Convey is awkward -- consider acutest.h
This represents a rather large effort towards cleaning up our
testing and optional configuration infrastructure.
A separate test library is built by default, which is static, and
includes some useful utilities design to make it easier to write
shorter and more robust (not timing dependent) tests. This also means
that we can cover pretty nearly all the tests (protocols etc.) in
every case, even if the shipped image will be minimized.
Subsystems which are optional can now use a few new macros to configure
what they need see nng_sources_if, nng_headers_if, and nng_defines_if.
This goes a long way to making the distributed CMakefiles a lot simpler.
Additionally, tests for different parts of the tree can now be located
outside of the tests/ tree, so that they can be placed next to the code
that they are testing.
Beyond the enabling work, the work has only begun, but these changes
have resolved the most often failing tests for Darwin in the cloud.
Diffstat (limited to 'tests/bufsz.c')
| -rw-r--r-- | tests/bufsz.c | 190 |
1 files changed, 101 insertions, 89 deletions
diff --git a/tests/bufsz.c b/tests/bufsz.c index da6a14cb..4f62905d 100644 --- a/tests/bufsz.c +++ b/tests/bufsz.c @@ -8,97 +8,109 @@ // found online at https://opensource.org/licenses/MIT. // -#include <string.h> - -#include <nng/compat/nanomsg/nn.h> #include <nng/nng.h> -#include <nng/protocol/pubsub0/sub.h> #include <nng/protocol/pair1/pair.h> #include <nng/supplemental/util/platform.h> -#include "trantest.h" -#include "convey.h" -#include "stubs.h" - -#define SECONDS(x) ((x) *1000) - -TestMain("Buffer Options", { - - atexit(nng_fini); - - Convey("We are able to open a PAIR socket", { - nng_socket s1; - - So(nng_pair_open(&s1) == 0); - - Reset({ nng_close(s1); }); - - Convey("Set/Get Recv Buf Option", { - int cnt; - So(nng_setopt_int(s1, NNG_OPT_RECVBUF, 10) == 0); - So(nng_getopt_int(s1, NNG_OPT_RECVBUF, &cnt) == 0); - So(cnt == 10); - So(nng_setopt_size(s1, NNG_OPT_RECVBUF, 42) == - NNG_EBADTYPE); - - }); - Convey("Set/Get Send Buf Option", { - int cnt; - So(nng_setopt_int(s1, NNG_OPT_SENDBUF, 10) == 0); - So(nng_getopt_int(s1, NNG_OPT_SENDBUF, &cnt) == 0); - So(cnt == 10); - So(nng_setopt_size(s1, NNG_OPT_SENDBUF, 42) == - NNG_EBADTYPE); - - }); - - // NOTE: We are going to use the compat mode, but - // this assumes that the socket is the same between compat - // and current mode. This is true, but normal applications - // MUST NOT assume this. We only do so for testing. - Convey("Legacy Recv Buf Option", { - int cnt; - int os = (int) s1.id; - size_t sz = sizeof(cnt); - So(nng_setopt_int(s1, NNG_OPT_RECVBUF, 10) == 0); - So(nn_getsockopt( - os, NN_SOL_SOCKET, NN_RCVBUF, &cnt, &sz) == 0); - So(cnt == 10240); - cnt = 1; - So(nn_setsockopt( - os, NN_SOL_SOCKET, NN_RCVBUF, &cnt, sz) == 0); - So(nn_getsockopt( - os, NN_SOL_SOCKET, NN_RCVBUF, &cnt, &sz) == 0); - So(cnt == 1024); // round up! - So(nng_getopt_int(s1, NNG_OPT_RECVBUF, &cnt) == 0); - So(cnt == 1); - - So(nn_setsockopt( - os, NN_SOL_SOCKET, NN_RCVBUF, &cnt, 100) == -1); - So(nn_errno() == EINVAL); - }); - Convey("Legacy Send Buf Option", { - int cnt; - int os = (int) s1.id; - size_t sz = sizeof(cnt); - So(nng_setopt_int(s1, NNG_OPT_SENDBUF, 10) == 0); - So(nn_getsockopt( - os, NN_SOL_SOCKET, NN_SNDBUF, &cnt, &sz) == 0); - So(cnt == 10240); - cnt = 1; - So(nn_setsockopt( - os, NN_SOL_SOCKET, NN_SNDBUF, &cnt, sz) == 0); - So(nn_getsockopt( - os, NN_SOL_SOCKET, NN_SNDBUF, &cnt, &sz) == 0); - So(cnt == 1024); // round up! - So(nng_getopt_int(s1, NNG_OPT_SENDBUF, &cnt) == 0); - So(cnt == 1); - - So(nn_setsockopt( - os, NN_SOL_SOCKET, NN_SNDBUF, &cnt, 100) == -1); - So(nn_errno() == EINVAL); - }); - - }); -}) +#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 }, +}; |
