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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
23 typedef cJSON *(* tojson_fn_t)(void *);
24 typedef void *(* fromjson_fn_t)(cJSON *o, int *len);
27 test (tojson_fn_t tojson, fromjson_fn_t fromjson, cJSON *o, bool should_fail)
29 // convert JSON object to API
31 void *mp = (fromjson)(o, &len);
34 // convert API to JSON
35 cJSON *o2 = (tojson)(mp);
39 assert(!cJSON_Compare(o, o2, 1));
41 assert(cJSON_Compare(o, o2, 1));
42 char *s2 = cJSON_Print(o2);
45 char *in = cJSON_Print(o);
46 printf("%s\n%s\n", in, s2);
57 fromjson_fn_t fromjson;
64 uword *function_by_name_tojson;
65 uword *function_by_name_fromjson;
67 register_functions(struct msgs msgs[], int n)
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);
79 runtest (char *s, bool should_fail)
81 cJSON *o = cJSON_Parse(s);
83 char *name = cJSON_GetStringValue(cJSON_GetObjectItem(o, "_msgname"));
86 uword *p = hash_get_mem(function_by_name_tojson, name);
87 printf ("Message name: %s\n", name);
89 tojson_fn_t tojson = (tojson_fn_t)p[0];
91 p = hash_get_mem(function_by_name_fromjson, name);
93 fromjson_fn_t fromjson = (fromjson_fn_t)p[0];
95 test(tojson, fromjson, o, should_fail);
99 struct msgs msgs[] = {
101 .name = "test_prefix",
102 .tojson = (tojson_fn_t) vl_api_test_prefix_t_tojson,
103 .fromjson = (fromjson_fn_t) vl_api_test_prefix_t_fromjson,
107 .tojson = (tojson_fn_t) vl_api_test_enum_t_tojson,
108 .fromjson = (fromjson_fn_t) vl_api_test_enum_t_fromjson,
111 .name = "test_string",
112 .tojson = (tojson_fn_t) vl_api_test_string_t_tojson,
113 .fromjson = (fromjson_fn_t) vl_api_test_string_t_fromjson,
116 .name = "test_string2",
117 .tojson = (tojson_fn_t) vl_api_test_string2_t_tojson,
118 .fromjson = (fromjson_fn_t) vl_api_test_string2_t_fromjson,
122 .tojson = (tojson_fn_t) vl_api_test_vla_t_tojson,
123 .fromjson = (fromjson_fn_t) vl_api_test_vla_t_fromjson,
127 .tojson = (tojson_fn_t) vl_api_test_vla2_t_tojson,
128 .fromjson = (fromjson_fn_t) vl_api_test_vla2_t_fromjson,
132 .tojson = (tojson_fn_t) vl_api_test_vla3_t_tojson,
133 .fromjson = (fromjson_fn_t) vl_api_test_vla3_t_fromjson,
137 .tojson = (tojson_fn_t) vl_api_test_vla4_t_tojson,
138 .fromjson = (fromjson_fn_t) vl_api_test_vla4_t_fromjson,
142 .tojson = (tojson_fn_t) vl_api_test_vla5_t_tojson,
143 .fromjson = (fromjson_fn_t) vl_api_test_vla5_t_fromjson,
146 .name = "test_addresses",
147 .tojson = (tojson_fn_t) vl_api_test_addresses_t_tojson,
148 .fromjson = (fromjson_fn_t) vl_api_test_addresses_t_fromjson,
151 .name = "test_addresses2",
152 .tojson = (tojson_fn_t) vl_api_test_addresses2_t_tojson,
153 .fromjson = (fromjson_fn_t) vl_api_test_addresses2_t_fromjson,
156 .name = "test_addresses3",
157 .tojson = (tojson_fn_t) vl_api_test_addresses3_t_tojson,
158 .fromjson = (fromjson_fn_t) vl_api_test_addresses3_t_fromjson,
161 .name = "test_empty",
162 .tojson = (tojson_fn_t) vl_api_test_empty_t_tojson,
163 .fromjson = (fromjson_fn_t) vl_api_test_empty_t_fromjson,
166 .name = "test_interface",
167 .tojson = (tojson_fn_t) vl_api_test_interface_t_tojson,
168 .fromjson = (fromjson_fn_t) vl_api_test_interface_t_fromjson,
172 struct tests tests[] = {
173 { .s = "{\"_msgname\": \"test_prefix\", \"pref\": \"2001:db8::/64\"}" },
174 { .s = "{\"_msgname\": \"test_prefix\", \"pref\": \"192.168.10.0/24\"}" },
175 { .s = "{\"_msgname\": \"test_enum\", \"flags\": [\"RED\", \"BLUE\"]}" },
176 { .s = "{\"_msgname\": \"test_enum\", \"flags\": [\"BLACK\", \"BLUE\"]}",
178 { .s = "{\"_msgname\": \"test_string\", \"str\": {\"str\": \"Test string "
181 "{\"_msgname\": \"test_string2\", \"str\": \"Test string toplevel\"}" },
182 { .s = "{\"_msgname\": \"test_vla\", \"count\": 5, \"vla\": [1,2,3,4,5]}" },
183 { .s = "{\"_msgname\": \"test_vla2\", \"count\": 5, \"vla\": "
184 "\"0xaabbccddee\"}" },
185 { .s = "{\"_msgname\": \"test_vla3\", \"count\": 2, \"vla\": [{\"data\": 1} "
186 ", {\"data\": 2} ] }" },
187 { .s = "{\"_msgname\": \"test_vla4\", \"data\": { \"count\": 5, \"vla\": "
189 { .s = "{\"_msgname\": \"test_vla5\", \"data\": { \"count\": 5, \"vla\": "
190 "\"0xaabbccddee\" }}" },
191 { .s = "{\"_msgname\": \"test_addresses\", \"a\": \"1.2.3.4\" }" },
192 { .s = "{\"_msgname\": \"test_addresses\", \"a\": \"2001:db8::23\" }" },
193 { .s = "{\"_msgname\": \"test_addresses2\", \"a\": [\"2001:db8::23\", "
194 "\"2001:db8::23\"] }" },
195 { .s = "{\"_msgname\": \"test_addresses3\", \"n\": 2, \"a\": "
196 "[\"2001:db8::23\", \"2001:db8::23\"] }" },
197 { .s = "{\"_msgname\": \"test_empty\"}" },
198 { .s = "{\"_msgname\": \"test_interface\", \"sw_if_index\": 100 }" },
201 int main (int argc, char **argv)
203 clib_mem_init (0, 64 << 20);
204 int n = sizeof(msgs)/sizeof(msgs[0]);
205 register_functions(msgs, n);
208 n = sizeof(tests)/sizeof(tests[0]);
209 for (i = 0; i < n; i++) {
210 runtest(tests[i].s, tests[i].should_fail);