summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2024-11-15 14:38:55 -0500
committerJonathan Bradley <jcb@pikum.xyz>2024-11-15 15:19:41 -0500
commitf23b4ce2dd648174a5df0e259faf7209c5d4c653 (patch)
tree84ef5f33475cbc513118afb3500e3bb59f55bdc1 /test
parent3beea930f372e85ff23092a70eb70619675a1371 (diff)
pkev: test for exit() via longjmp
Diffstat (limited to 'test')
-rw-r--r--test/pkev.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/test/pkev.cpp b/test/pkev.cpp
index a1a3036..652f020 100644
--- a/test/pkev.cpp
+++ b/test/pkev.cpp
@@ -3,10 +3,27 @@
#include "../pkmacros.h"
+#include <csetjmp>
#include <cstdio>
#include <future>
#include <thread>
+static bool expected_exit = false;
+static bool caught = false;
+static jmp_buf jmp_env;
+// https://stackoverflow.com/questions/64190847/how-to-catch-a-call-to-exit-for-unit-testing
+// stub function
+void
+exit(int code)
+{
+ if (expected_exit) {
+ caught = true;
+ longjmp(jmp_env, 1);
+ } else {
+ _exit(code);
+ }
+}
+
struct ev {
pk_ev_mgr_id_T evmgr;
pk_ev_id_T evid;
@@ -160,5 +177,90 @@ int main(int argc, char *argv[])
if (any_false == true) exit(1);
}
+ // overload cbs and evs (enforce uint8_t limits)
+ do
+ {
+ int r;
+ const uint64_t cb_count = 256;
+ const uint64_t ev_count = 1;
+ struct ev evs[ev_count] = {{0}};
+ const pk_ev_mgr_id_T evmgr = test_setup();
+
+ r = setjmp(jmp_env);
+ if (r == 1) {
+ if (expected_exit == true && caught == true) {
+ expected_exit = false;
+ caught = false;
+ pk_ev_teardown();
+ PK_LOGV_INF("%s: successfully caught err.\n", __FILE__);
+ fflush(stdout);
+ fflush(stderr);
+ break;
+ } else {
+ goto uncaught_err;
+ }
+ }
+
+ for (i = 0; i < ev_count; ++i) {
+ evs[i].evmgr = evmgr;
+ evs[i].evid = pk_ev_register_ev(evmgr, NULL);
+ for (ii = 0; ii < cb_count; ++ii) {
+ caught = false;
+ expected_exit = true;
+ pk_ev_register_cb(evmgr, evs[i].evid, &stress_cb, NULL);
+ expected_exit = false;
+ caught = false;
+ }
+ }
+ goto uncaught_err;
+ }
+ while(false);
+
+ // overload cbs and evs (enforce uint8_t limits)
+ do
+ {
+ int r;
+ const uint64_t cb_count = 1;
+ const uint64_t ev_count = 256;
+ struct ev evs[ev_count] = {{0}};
+ const pk_ev_mgr_id_T evmgr = test_setup();
+
+ r = setjmp(jmp_env);
+ if (r == 1) {
+ if (expected_exit == true && caught == true) {
+ expected_exit = false;
+ caught = false;
+ pk_ev_teardown();
+ PK_LOGV_INF("%s: successfully caught err.\n", __FILE__);
+ fflush(stdout);
+ fflush(stderr);
+ break;
+ } else {
+ goto uncaught_err;
+ }
+ }
+
+ for (i = 0; i < ev_count; ++i) {
+ evs[i].evmgr = evmgr;
+ caught = false;
+ expected_exit = true;
+ evs[i].evid = pk_ev_register_ev(evmgr, NULL);
+ expected_exit = false;
+ caught = false;
+ for (ii = 0; ii < cb_count; ++ii) {
+ pk_ev_register_cb(evmgr, evs[i].evid, &stress_cb, NULL);
+ }
+ }
+
+ goto uncaught_err;
+
+ }
+ while(false);
+
return 0;
+uncaught_err:
+ PK_LOGV_ERR("%s: failed to catch err.\n", __FILE__);
+ fflush(stdout);
+ fflush(stderr);
+ return 1;
}