Skip to content

Commit

Permalink
Verify errno handling works; use table driven approach.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Aug 11, 2017
1 parent 19e0363 commit c552065
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 63 deletions.
97 changes: 34 additions & 63 deletions src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions tests/errors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright 2016 Garrett D'Amore <[email protected]>
//
// 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);

});
})

0 comments on commit c552065

Please sign in to comment.