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