blob: fe788f127c6b3a5f7a57bd1e7472fb78e0df53dc [file] [log] [blame]
Ole Troan793be462020-12-04 13:15:30 +01001/*
2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <assert.h>
18#include <vlibapi/api.h>
19#include "vat2/test/vat2_test.api_types.h"
20#include "vat2/test/vat2_test.api_tojson.h"
21#include "vat2/test/vat2_test.api_fromjson.h"
22
23typedef cJSON *(* tojson_fn_t)(void *);
24typedef void *(* fromjson_fn_t)(cJSON *o, int *len);
25
26static void
27test (tojson_fn_t tojson, fromjson_fn_t fromjson, cJSON *o, bool should_fail)
28{
29 // convert JSON object to API
30 int len = 0;
31 void *mp = (fromjson)(o, &len);
32 assert(mp);
33
34 // convert API to JSON
35 cJSON *o2 = (tojson)(mp);
36 assert(o2);
37
38 if (should_fail)
39 assert(!cJSON_Compare(o, o2, 1));
40 else
41 assert(cJSON_Compare(o, o2, 1));
42 char *s2 = cJSON_Print(o2);
43 assert(s2);
44
45 char *in = cJSON_Print(o);
46 printf("%s\n%s\n", in, s2);
47
48 free(in);
49 free(mp);
50 cJSON_Delete(o2);
51 free(s2);
52}
53
54struct msgs {
55 char *name;
56 tojson_fn_t tojson;
57 fromjson_fn_t fromjson;
58};
59struct tests {
60 char *s;
61 bool should_fail;
62};
63
64uword *function_by_name_tojson;
65uword *function_by_name_fromjson;
66static void
67register_functions(struct msgs msgs[], int n)
68{
69 int i;
70 function_by_name_tojson = hash_create_string (0, sizeof (uword));
71 function_by_name_fromjson = hash_create_string (0, sizeof (uword));
72 for (i = 0; i < n; i++) {
73 hash_set_mem(function_by_name_tojson, msgs[i].name, msgs[i].tojson);
74 hash_set_mem(function_by_name_fromjson, msgs[i].name, msgs[i].fromjson);
75 }
76}
77
78static void
79runtest (char *s, bool should_fail)
80{
81 cJSON *o = cJSON_Parse(s);
82 assert(o);
83 char *name = cJSON_GetStringValue(cJSON_GetObjectItem(o, "_msgname"));
84 assert(name);
85
86 uword *p = hash_get_mem(function_by_name_tojson, name);
87 assert(p);
88 tojson_fn_t tojson = (tojson_fn_t)p[0];
89
90 p = hash_get_mem(function_by_name_fromjson, name);
91 assert(p);
92 fromjson_fn_t fromjson = (fromjson_fn_t)p[0];
93
94 test(tojson, fromjson, o, should_fail);
95 cJSON_Delete(o);
96}
97
98struct msgs msgs[] = {
99{
100 .name = "test_prefix",
101 .tojson = (tojson_fn_t)vl_api_test_prefix_t_tojson,
102 .fromjson = (fromjson_fn_t)vl_api_test_prefix_t_fromjson,
103},
104{
105 .name = "test_enum",
106 .tojson = (tojson_fn_t)vl_api_test_enum_t_tojson,
107 .fromjson = (fromjson_fn_t)vl_api_test_enum_t_fromjson,
108},
109};
110
111struct tests tests[] = {
112 {.s = "{\"_msgname\": \"test_prefix\", \"pref\": \"2001:db8::/64\"}"},
113 {.s = "{\"_msgname\": \"test_prefix\", \"pref\": \"192.168.10.0/24\"}"},
114 {.s = "{\"_msgname\": \"test_enum\", \"flags\": [\"RED\", \"BLUE\"]}"},
115 {.s = "{\"_msgname\": \"test_enum\", \"flags\": [\"BLACK\", \"BLUE\"]}",
116 .should_fail = 1},
117};
118
119int main (int argc, char **argv)
120{
121 clib_mem_init (0, 64 << 20);
122 int n = sizeof(msgs)/sizeof(msgs[0]);
123 register_functions(msgs, n);
124
125 int i;
126 n = sizeof(tests)/sizeof(tests[0]);
127 for (i = 0; i < n; i++) {
128 runtest(tests[i].s, tests[i].should_fail);
129 }
130}