diff options
| -rw-r--r-- | src/nng.c | 97 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/errors.c | 33 |
3 files changed, 68 insertions, 63 deletions
@@ -360,73 +360,44 @@ nng_device(nng_socket s1, nng_socket s2) return (rv); } +static const struct { + int code; + const char *msg; +} nni_errors[] = { + // clang-format off + { 0, "Hunky dory" }, + { NNG_EINTR, "Interrupted" }, + { NNG_ENOMEM, "Out of memory" }, + { NNG_EINVAL, "Invalid argument" }, + { NNG_EBUSY, "Resource busy" }, + { NNG_ETIMEDOUT, "Timed out" }, + { NNG_ECONNREFUSED, "Connection refused" }, + { NNG_ECLOSED, "Object closed" }, + { NNG_EAGAIN, "Try again" }, + { NNG_ENOTSUP, "Not supported" }, + { NNG_EADDRINUSE, "Address in use" }, + { NNG_ESTATE, "Incorrect state" }, + { NNG_ENOENT, "Entry not found" }, + { NNG_EPROTO, "Protocol error" }, + { NNG_EUNREACHABLE, "Destination unreachable" }, + { NNG_EADDRINVAL, "Address invalid" }, + { NNG_EPERM, "Permission denied" }, + { NNG_EMSGSIZE, "Message too large" }, + { NNG_ECONNRESET, "Connection reset" }, + { NNG_ECONNABORTED, "Connection aborted" }, + { NNG_ECANCELED, "Operation canceled" }, + { 0, NULL } + // clang-format on +}; + // Misc. const char * nng_strerror(int num) { - switch (num) { - case 0: - return ("Hunky dory"); // What did you expect? - - case NNG_EINTR: - return ("Interrupted"); - - case NNG_ENOMEM: - return ("Out of memory"); - - case NNG_EINVAL: - return ("Invalid argument"); - - case NNG_EBUSY: - return ("Resource busy"); - - case NNG_ETIMEDOUT: - return ("Timed out"); - - case NNG_ECONNREFUSED: - return ("Connection refused"); - - case NNG_ECLOSED: - return ("Object closed"); - - case NNG_EAGAIN: - return ("Try again"); - - case NNG_ENOTSUP: - return ("Not supported"); - - case NNG_EADDRINUSE: - return ("Address in use"); - - case NNG_ESTATE: - return ("Incorrect state"); - - case NNG_ENOENT: - return ("Entry not found"); - - case NNG_EPROTO: - return ("Protocol error"); - - case NNG_EUNREACHABLE: - return ("Destination unreachable"); - - case NNG_EADDRINVAL: - return ("Address invalid"); - - case NNG_EPERM: - return ("Permission denied"); - - case NNG_EMSGSIZE: - return ("Message too large"); - - case NNG_ECONNRESET: - return ("Connection reset"); - - case NNG_ECONNABORTED: - return ("Connection aborted"); - - case NNG_ECANCELED: - return ("Operation canceled"); + for (int i = 0; nni_errors[i].msg != NULL; i++) { + if (nni_errors[i].code == num) { + return (nni_errors[i].msg); + } } if (num & NNG_ESYSERR) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c5244667..8234654f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -89,6 +89,7 @@ add_nng_test(tcp 5) add_nng_test(scalability 20) add_nng_test(message 5) add_nng_test(device 5) +add_nng_test(errors 2) # compatbility tests add_nng_compat_test(compat_block 5) diff --git a/tests/errors.c b/tests/errors.c new file mode 100644 index 00000000..f5e857e2 --- /dev/null +++ b/tests/errors.c @@ -0,0 +1,33 @@ +// +// Copyright 2016 Garrett D'Amore <garrett@damore.org> +// +// 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.c" +#include <errno.h> +#include <string.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); + + }); +}) |
