X-Git-Url: http://www.project-moonshot.org/gitweb/?p=trust_router.git;a=blobdiff_plain;f=mon%2Ftests%2Ftest_mon_resp_encode.c;fp=mon%2Ftests%2Ftest_mon_resp_encode.c;h=3c1dd831bdd2a21dfee59563e060fe80ffeb8178;hp=0000000000000000000000000000000000000000;hb=6f65c9cce86719147d0b4dcc9823b25443c2d185;hpb=eaa1a8ceed54fbfadc2638cf383aaa12ab446a57 diff --git a/mon/tests/test_mon_resp_encode.c b/mon/tests/test_mon_resp_encode.c new file mode 100644 index 0000000..3c1dd83 --- /dev/null +++ b/mon/tests/test_mon_resp_encode.c @@ -0,0 +1,93 @@ +// +// Created by jlr on 4/9/18. +// + +#include +#include +#include +#include + +#include + +#define JSON_DUMP_OPTS 0 + +static char *show_success() +{ + MON_REQ *req = NULL; + MON_RESP *resp = NULL; + json_t *resp_json = NULL; + json_t *payload = NULL; + char *result = NULL; + + req = mon_req_new(NULL, MON_CMD_SHOW); + // Only need the command to be set in req, don't actually need the options + assert(req); + + payload = json_object(); + assert(payload); + assert(! json_object_set_new(payload, + mon_opt_type_to_string(OPT_TYPE_SHOW_VERSION), + json_string("1.2.3-4"))); + assert(! json_object_set_new(payload, + mon_opt_type_to_string(OPT_TYPE_SHOW_CONFIG_FILES), + json_integer(1234567890))); + assert(! json_object_set_new(payload, + mon_opt_type_to_string(OPT_TYPE_SHOW_CONFIG_FILES), + json_integer(86400))); + assert(! json_object_set_new(payload, + mon_opt_type_to_string(OPT_TYPE_SHOW_TID_REQS_PENDING), + json_integer(13))); + assert(! json_object_set_new(payload, + mon_opt_type_to_string(OPT_TYPE_SHOW_TID_REQS_PROCESSED), + json_integer(1432))); + + resp = mon_resp_new(NULL, MON_RESP_SUCCESS, "success", payload); + assert(resp); + + resp_json = mon_resp_encode(resp); + assert(resp_json); + + result = json_dumps(resp_json, JSON_DUMP_OPTS); + assert(result); + + json_decref(resp_json); + mon_resp_free(resp); + mon_req_free(req); + return result; +} + +static char *read_file(const char *filename) +{ + FILE *f = fopen(filename, "r"); + char *s = NULL; + size_t nn = 0; + ssize_t n = getline(&s, &nn, f); + fclose(f); + + if( (n > 0) && (s[n-1] == '\n')) + s[n-1] = 0; + + return s; +} + +int run_test(const char *filename, char *(generator)()) +{ + char *s = NULL; + char *expected = NULL; + + // Test reconfigure command + s = generator(); + expected = read_file(filename); + assert(expected); + assert(strcmp(expected, s) == 0); + free(s); + free(expected); + + return 1; +} + +int main(void) +{ + assert(run_test("resp_show_success.test", show_success)); + return 0; +}