diff options
| author | Garrett D'Amore <garrett@damore.org> | 2019-12-31 15:09:20 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-01-01 08:47:06 -0800 |
| commit | a693a53e2302fe9cc60c1b5d2bf59c42032b20a3 (patch) | |
| tree | e98ba805ea9adc2e5ee3e33f7dea1c32584d5c63 /tests/testutil.c | |
| parent | 0ab3403ef9407db4604cbb451c42a179ab807342 (diff) | |
| download | nng-a693a53e2302fe9cc60c1b5d2bf59c42032b20a3.tar.gz nng-a693a53e2302fe9cc60c1b5d2bf59c42032b20a3.tar.bz2 nng-a693a53e2302fe9cc60c1b5d2bf59c42032b20a3.zip | |
fixes #1081 Use after free possible in stats
fixes #1080 Desire better way to access statistics for NNG objects
We've also added a test that uses some of this, in order to verify
that the req protocol rejects invalid peers.
Diffstat (limited to 'tests/testutil.c')
| -rw-r--r-- | tests/testutil.c | 57 |
1 files changed, 53 insertions, 4 deletions
diff --git a/tests/testutil.c b/tests/testutil.c index 1c65c996..36999448 100644 --- a/tests/testutil.c +++ b/tests/testutil.c @@ -127,14 +127,63 @@ uint32_t testutil_htonl(uint32_t in) { #ifdef NNG_LITTLE_ENDIAN - in = ((in >> 24u) & 0xffu) | - ((in >> 8u) & 0xff00u) | - ((in << 8u) & 0xff0000u) | - ((in << 24u) & 0xff000000u); + in = ((in >> 24u) & 0xffu) | ((in >> 8u) & 0xff00u) | + ((in << 8u) & 0xff0000u) | ((in << 24u) & 0xff000000u); #endif return (in); } +void +testutil_scratch_addr(const char *scheme, size_t sz, char *addr) +{ + if (strcmp(scheme, "inproc") == 0) { + (void) snprintf(addr, sz, "%s://testutil%04x%04x%04x%04x", + scheme, nng_random(), nng_random(), nng_random(), + nng_random()); + return; + } + + if ((strncmp(scheme, "tcp", 3) == 0) || + (strncmp(scheme, "tls", 3) == 0)) { + (void) snprintf(addr, sz, "%s://127.0.0.1:%u", scheme, + testutil_next_port()); + return; + } + + if (strncmp(scheme, "ws", 2) == 0) { + (void) snprintf(addr, sz, + "%s://127.0.0.1:%u/testutil%04x%04x%04x%04x", scheme, + testutil_next_port(), nng_random(), nng_random(), + nng_random(), nng_random()); + return; + } + + if (strncmp(scheme, "ipc", 3) == 0) { +#ifdef _WIN32 + // Windows doesn't place IPC names in the filesystem. + (void) snprintf(addr, sz, "%s://testutil%04x%04x%04x%04x", + scheme, nng_random(), nng_random(), nng_random(), + nng_random()); +#else + char *tmpdir; + + if (((tmpdir = getenv("TMPDIR")) == NULL) && + ((tmpdir = getenv("TEMP")) == NULL) && + ((tmpdir = getenv("TMP")) == NULL)) { + tmpdir = "/tmp"; + } + + (void) snprintf(addr, sz, "%s://%s/testutil%04x%04x%04x%04x", + scheme, tmpdir, nng_random(), nng_random(), nng_random(), + nng_random()); + return; +#endif + } + + // We should not be here. + abort(); +} + // testutil_next_port returns a "next" allocation port. // Ports are chosen by starting from a random point within a // range (normally 38000-40000, but other good places to choose |
