Clean up monitoring format/naming
[trust_router.git] / mon / tests / test_mon_req_decode.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 #include <glib.h>
10
11 #include <mon_internal.h>
12
13 /**
14  * @return reconfigure command
15  */
16 static MON_REQ *reconfigure()
17 {
18   MON_REQ *req = mon_req_new(NULL, MON_CMD_RECONFIGURE);
19   assert(req);
20   return req;
21 }
22
23 /**
24  * @return show command with no options
25  */
26 static MON_REQ *show_plain()
27 {
28   MON_REQ *req = mon_req_new(NULL, MON_CMD_SHOW);
29   assert(req);
30   return req;
31 }
32
33 /**
34  * @param opts array of option types, terminated with OPT_TYPE_UNKNOWN
35  * @return show command with the requested options, excluding the terminator
36  */
37 static MON_REQ *show_options(const MON_OPT_TYPE *opts)
38 {
39   MON_REQ *req = mon_req_new(NULL, MON_CMD_SHOW);
40   assert(req);
41
42   while (*opts != OPT_TYPE_UNKNOWN) {
43     assert(MON_SUCCESS == mon_req_add_option(req, *opts));
44     opts++;
45   }
46   return req;
47 }
48
49 /**
50  * @return show command with every option
51  */
52 static MON_REQ *show_all_options()
53 {
54   MON_OPT_TYPE opts[] = {
55       OPT_TYPE_SHOW_CONFIG_FILES,
56       OPT_TYPE_SHOW_VERSION,
57       OPT_TYPE_SHOW_UPTIME,
58       OPT_TYPE_SHOW_TID_REQ_COUNT,
59       OPT_TYPE_SHOW_TID_REQ_PENDING,
60       OPT_TYPE_SHOW_ROUTES,
61       OPT_TYPE_SHOW_COMMUNITIES,
62       OPT_TYPE_UNKNOWN // terminator
63   };
64
65   return show_options(opts);
66 }
67
68 static char *read_file(const char *filename)
69 {
70   FILE *f = fopen(filename, "r");
71   char *s = NULL;
72   size_t nn = 0;
73   ssize_t n = getline(&s, &nn, f);
74
75   fclose(f);
76
77   if( (n > 0) && (s[n-1] == '\n'))
78     s[n-1] = 0;
79
80   return s;
81 }
82
83 static int equal(MON_REQ *r1, MON_REQ *r2)
84 {
85   size_t ii;
86
87   if (r1->command != r2->command)
88     return 0;
89
90   if (mon_req_opt_count(r1) != mon_req_opt_count(r2))
91     return 0;
92
93   for (ii=0; ii < mon_req_opt_count(r1); ii++) {
94     if (mon_req_opt_index(r1, ii)->type != mon_req_opt_index(r2, ii)->type)
95       return 0;
96   }
97
98   return 1;
99 }
100
101 static int run_test(const char *filename, MON_REQ *(generator)())
102 {
103   MON_REQ *req = NULL;
104   MON_REQ *expected = NULL;
105   char *req_json_str = NULL;
106
107   expected = generator();
108   assert(expected);
109
110   req_json_str = read_file(filename);
111   assert(req_json_str);
112
113   req = mon_req_parse(NULL, req_json_str);
114   assert(req);
115   assert(equal(req, expected));
116
117   free(req_json_str);
118   mon_req_free(req);
119   mon_req_free(expected);
120
121   return 1;
122 }
123
124 int main(void)
125 {
126
127   // Test reconfigure command
128   assert(run_test("req_reconfigure.test", reconfigure));
129
130   // Test show command with no options
131   assert(run_test("req_show_no_options.test", show_plain));
132
133   // Test show command with all the options
134   assert(run_test("req_show_all_options.test", show_all_options));
135
136   return 0;
137 }