diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-11-21 22:11:21 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-11-23 22:20:12 -0800 |
| commit | d1218d7309475193b53911667911c4f59a1a7752 (patch) | |
| tree | 6ea796998fb60d2cb8afa704faa77fe7fddd644c /tests/testutil.h | |
| parent | b826bfc171d90f8bde7bd672c0ac14201b8b2742 (diff) | |
| download | nng-d1218d7309475193b53911667911c4f59a1a7752.tar.gz nng-d1218d7309475193b53911667911c4f59a1a7752.tar.bz2 nng-d1218d7309475193b53911667911c4f59a1a7752.zip | |
New NUTS test framework (NNG Unit Test Support).
This is based on testutil/acutest, but is cleaner and fixes some
short-comings. We will be adding more support for additional
common paradigms to better facilitate transport tests.
While here we added some more test cases, and fixed a possible
symbol collision in the the stats framework (due to Linux use
of a macro definition of "si_value" in a standard OS header).
Test coverage may regress slightly as we are no longer using
some of the legacy APIs.
Diffstat (limited to 'tests/testutil.h')
| -rw-r--r-- | tests/testutil.h | 129 |
1 files changed, 0 insertions, 129 deletions
diff --git a/tests/testutil.h b/tests/testutil.h deleted file mode 100644 index 63979cff..00000000 --- a/tests/testutil.h +++ /dev/null @@ -1,129 +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. -// - -#ifndef TESTUTIL_H -#define TESTUTIL_H - -#include <stdbool.h> -#include <stdint.h> - -// The following headers are provided for test code convenience. -#include <nng/nng.h> -#include <nng/supplemental/util/platform.h> - -#ifdef __cplusplus -extern "C" { -#endif - -// testutil_clock returns the current time in milliseconds. -// The reference clock may be any point in the past (typically since -// the program started running.) -extern uint64_t testutil_clock(void); - -// testutil_pollfd tests if the given file descriptor polls as readable. -extern bool testutil_pollfd(int); - -// testutil_htons is just htons portably. -extern uint16_t testutil_htons(uint16_t); - -// testutil_htonl is just htonl portably. -extern uint32_t testutil_htonl(uint32_t); - -// testutil_sleep sleeps the specified number of msec -extern void testutil_sleep(int); - -// testutil_next_port returns a new port number (presumably unique) -extern uint16_t testutil_next_port(void); - -// testutil_scratch_addr makes a scratch address for the given scheme. -// The address buffer must be supplied, and the size should be at least -// 64 bytes to ensure no truncation occurs. -extern void testutil_scratch_addr(const char *, size_t, char *); - -// testutil_marry connects two sockets using inproc. It uses socket -// pipe hooks to ensure that it does not return before both sockets -// are fully connected. -extern int testutil_marry(nng_socket, nng_socket); - -// testutil_marry_ex is like testutil_marry, but returns the pipes that -// were connected, and includes an optional URL. The pipe pointers and the -// URL may be NULL if not needed. -extern int testutil_marry_ex( - nng_socket, nng_socket, const char *, nng_pipe *, nng_pipe *); - -// testutil_stream_send_start and testutil_stream_recv_start are used -// to initiate transfers asynchronously. They return a token which can -// be used with testutil_stream_send_wait and testutil_stream_recv_wait. -// Those wait functions will return the result of operation (0 on -// success, an NNG error number otherwise.) -extern void *testutil_stream_send_start(nng_stream *, void *, size_t); -extern void *testutil_stream_recv_start(nng_stream *, void *, size_t); -extern int testutil_stream_send_wait(void *); -extern int testutil_stream_recv_wait(void *); - -// These are TLS certificates. The client and server are signed with the -// root. The server uses CN 127.0.0.1. Other details are bogus, but -// designed to prevent accidental use elsewhere. -extern const char *testutil_server_key; -extern const char *testutil_server_crt; -extern const char *testutil_client_key; -extern const char *testutil_client_crt; - -// TEST_NNG_PASS tests for NNG success. It reports the failure if it -// did not. -#define TEST_NNG_PASS(cond) \ - do { \ - int result_ = (cond); \ - TEST_CHECK_(result_ == 0, "%s succeeds", #cond); \ - TEST_MSG("%s: expected success, got %s (%d)", #cond, \ - nng_strerror(result_), result_); \ - } while (0) - -#define TEST_NNG_FAIL(cond, expect) \ - do { \ - int result_ = (cond); \ - TEST_CHECK_(result_ == expect, "%s fails with %s", #cond, \ - nng_strerror(expect)); \ - TEST_MSG("%s: expected %s (%d), got %s (%d)", #cond, \ - nng_strerror(expect), expect, nng_strerror(result_), \ - result_); \ - } while (0) - -#define TEST_NNG_SEND_STR(sock, string) \ - TEST_NNG_PASS(nng_send(sock, string, strlen(string) + 1, 0)) - -#define TEST_NNG_RECV_STR(sock, string) \ - do { \ - char buf_[64]; \ - size_t sz_ = sizeof(buf_); \ - int rv_ = nng_recv(sock, &buf_, &sz_, 0); \ - TEST_CHECK_( \ - rv_ == 0, "nng_recv (%d %s)", rv_, nng_strerror(rv_)); \ - TEST_CHECK_(sz_ == strlen(string) + 1, "length %d want %d", \ - sz_, strlen(string) + 1); \ - buf_[sizeof(buf_) - 1] = '\0'; \ - TEST_CHECK_( \ - strcmp(string, buf_) == 0, "%s == %s", string, buf_); \ - } while (0) - -#define TEST_STREQUAL(s1, s2) \ - do { \ - TEST_CHECK_(strcmp(s1, s2) == 0, "%s == %s", s1, s2); \ - } while (0) - -#define TEST_NULL(x) \ - do { \ - TEST_CHECK_((x) == NULL, "%p == NULL", x); \ - } while (0) - -#ifdef __cplusplus -}; -#endif - -#endif // TESTUTIL_H |
