Return separate counts of TID reqs that succeed and result in error
[trust_router.git] / mon / tests / test_mon_resp_encode.c
1 //
2 // Created by jlr on 4/9/18.
3 //
4
5 #include <talloc.h>
6 #include <jansson.h>
7 #include <assert.h>
8 #include <string.h>
9
10 #include <mon_internal.h>
11
12 #define JSON_DUMP_OPTS 0
13
14 static char *show_success()
15 {
16   MON_REQ *req = NULL;
17   MON_RESP *resp = NULL;
18   json_t *resp_json = NULL;
19   json_t *payload = NULL;
20   char *result = NULL;
21
22   req = mon_req_new(NULL, MON_CMD_SHOW);
23   // Only need the command to be set in req, don't actually need the options
24   assert(req);
25
26   payload = json_object();
27   assert(payload);
28   assert(! json_object_set_new(payload,
29                                mon_opt_type_to_string(OPT_TYPE_SHOW_VERSION),
30                                json_string("1.2.3-4")));
31   assert(! json_object_set_new(payload,
32                                mon_opt_type_to_string(OPT_TYPE_SHOW_CONFIG_FILES),
33                                json_integer(1234567890)));
34   assert(! json_object_set_new(payload,
35                                mon_opt_type_to_string(OPT_TYPE_SHOW_CONFIG_FILES),
36                                json_integer(86400)));
37   assert(! json_object_set_new(payload,
38                                mon_opt_type_to_string(OPT_TYPE_SHOW_TID_REQS_PENDING),
39                                json_integer(13)));
40   assert(! json_object_set_new(payload,
41                                mon_opt_type_to_string(OPT_TYPE_SHOW_TID_REQS_PROCESSED),
42                                json_integer(1432)));
43
44   resp = mon_resp_new(NULL, MON_RESP_SUCCESS, "success", payload);
45   assert(resp);
46
47   resp_json = mon_resp_encode(resp);
48   assert(resp_json);
49
50   result = json_dumps(resp_json, JSON_DUMP_OPTS);
51   assert(result);
52
53   json_decref(resp_json);
54   mon_resp_free(resp);
55   mon_req_free(req);
56   return result;
57 }
58
59 static char *read_file(const char *filename)
60 {
61   FILE *f = fopen(filename, "r");
62   char *s = NULL;
63   size_t nn = 0;
64   ssize_t n = getline(&s, &nn, f);
65   fclose(f);
66
67   if( (n > 0) && (s[n-1] == '\n'))
68     s[n-1] = 0;
69
70   return s;
71 }
72
73 int run_test(const char *filename, char *(generator)())
74 {
75   char *s = NULL;
76   char *expected = NULL;
77
78   // Test reconfigure command
79   s = generator();
80   expected = read_file(filename);
81   assert(expected);
82   assert(strcmp(expected, s) == 0);
83   free(s);
84   free(expected);
85
86   return 1;
87 }
88
89 int main(void)
90 {
91   assert(run_test("resp_show_success.test", show_success));
92   return 0;
93 }