Clean up monitoring format/naming
[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 *reconfigure(MON_RESP_CODE code, const char *message)
15 {
16   MON_REQ *req = NULL;
17   MON_RESP *resp = NULL;
18   json_t *resp_json = NULL;
19   char *result = NULL;
20
21   req = mon_req_new(NULL, MON_CMD_RECONFIGURE);
22   assert(req);
23
24   resp = mon_resp_new(NULL, code, message, NULL);
25   assert(resp);
26
27   resp_json = mon_resp_encode(resp);
28   assert(resp_json);
29
30   result = json_dumps(resp_json, JSON_DUMP_OPTS);
31   assert(result);
32
33   json_decref(resp_json);
34   mon_resp_free(resp);
35   mon_req_free(req);
36   return result;
37 }
38
39 static char *reconfigure_success()
40 {
41   return reconfigure(MON_RESP_SUCCESS, "success");
42 }
43
44 static char *reconfigure_error()
45 {
46   return reconfigure(MON_RESP_ERROR, "error");
47 }
48
49 static char *show_success()
50 {
51   MON_REQ *req = NULL;
52   MON_RESP *resp = NULL;
53   json_t *resp_json = NULL;
54   json_t *payload = NULL;
55   char *result = NULL;
56
57   req = mon_req_new(NULL, MON_CMD_SHOW);
58   // Only need the command to be set in req, don't actually need the options
59   assert(req);
60
61   payload = json_object();
62   assert(payload);
63   assert(! json_object_set_new(payload,
64                                mon_opt_type_to_string(OPT_TYPE_SHOW_VERSION),
65                                json_string("1.2.3-4")));
66   assert(! json_object_set_new(payload,
67                                mon_opt_type_to_string(OPT_TYPE_SHOW_CONFIG_FILES),
68                                json_integer(1234567890)));
69   assert(! json_object_set_new(payload,
70                                mon_opt_type_to_string(OPT_TYPE_SHOW_CONFIG_FILES),
71                                json_integer(86400)));
72   assert(! json_object_set_new(payload,
73                                mon_opt_type_to_string(OPT_TYPE_SHOW_TID_REQ_PENDING),
74                                json_integer(13)));
75   assert(! json_object_set_new(payload,
76                                mon_opt_type_to_string(OPT_TYPE_SHOW_TID_REQ_COUNT),
77                                json_integer(1432)));
78
79   resp = mon_resp_new(NULL, MON_RESP_SUCCESS, "success", payload);
80   assert(resp);
81
82   resp_json = mon_resp_encode(resp);
83   assert(resp_json);
84
85   result = json_dumps(resp_json, JSON_DUMP_OPTS);
86   assert(result);
87
88   json_decref(resp_json);
89   mon_resp_free(resp);
90   mon_req_free(req);
91   return result;
92 }
93
94 static char *read_file(const char *filename)
95 {
96   FILE *f = fopen(filename, "r");
97   char *s = NULL;
98   size_t nn = 0;
99   ssize_t n = getline(&s, &nn, f);
100   fclose(f);
101
102   if( (n > 0) && (s[n-1] == '\n'))
103     s[n-1] = 0;
104
105   return s;
106 }
107
108 int run_test(const char *filename, char *(generator)())
109 {
110   char *s = NULL;
111   char *expected = NULL;
112
113   // Test reconfigure command
114   s = generator();
115   expected = read_file(filename);
116   assert(expected);
117   assert(strcmp(expected, s) == 0);
118   free(s);
119   free(expected);
120
121   return 1;
122 }
123
124 int main(void)
125 {
126   assert(run_test("resp_reconfigure_success.test", reconfigure_success));
127   assert(run_test("resp_reconfigure_error.test", reconfigure_error));
128   assert(run_test("resp_show_success.test", show_success));
129   return 0;
130 }