blob: 5525ff4e50e74a75621cc9868fbcb35d66a3a0a1 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2015, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36#include "tests.h"
37
38/* Main test routine */
39int main(int argc, char *argv[])
40{
41 struct msg * acr = NULL;
42 struct avp * pi = NULL, *avp1, *avp2;
43 unsigned char * buf = NULL;
44
45 /* First, initialize the daemon modules */
46 INIT_FD();
47
48 /* Create the message object from model */
49 {
50 struct dict_object * acr_model = NULL;
51
52 /* Now find the ACR dictionary object */
53 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Accounting-Request", &acr_model, ENOENT ) );
54
55 /* Create the instance, using the templates */
56 CHECK( 0, fd_msg_new ( acr_model, 0, &acr ) );
57
58 /* Check there is no child */
59 CHECK( ENOENT, fd_msg_browse ( acr, MSG_BRW_FIRST_CHILD, NULL, NULL) );
60
61 #if 0
62 /* For debug: dump the object */
63 fd_log_debug("Dumping Accounting-Request empty message:");
64 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, acr, fd_g_config->cnf_dict, 0, 1));
65 #endif
66 }
67
68 /* Create the Proxy-Info AVP from model */
69 {
70 struct dict_object * pi_model = NULL;
71
72 /* Now find the ACR dictionary object */
73 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Proxy-Info", &pi_model, ENOENT ) );
74
75 /* Create the instance, using the templates */
76 CHECK( 0, fd_msg_avp_new ( pi_model, 0, &pi ) );
77
78 #if 0
79 /* For debug: dump the object */
80 fd_log_debug("Dumping Proxy-Info AVP");
81 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, pi, fd_g_config->cnf_dict, 0, 1));
82 fd_log_debug("Dumping dictionary model");
83 fd_log_debug("%s", fd_dict_dump_object(FD_DUMP_TEST_PARAMS, pi_model));
84 #endif
85
86 }
87
88 /* Get a reference to the current last AVP in the message */
89 {
90 int diff = 0;
91
92 CHECK( 0, fd_msg_avp_new ( NULL, 0, &avp1 ) );
93 CHECK( 0, fd_msg_avp_add ( acr, MSG_BRW_LAST_CHILD, avp1) );
94
95 CHECK( 0, fd_msg_browse ( acr, MSG_BRW_LAST_CHILD, &avp2, &diff) );
96 CHECK( 1, diff );
97 CHECK( avp1, avp2 );
98
99 /* Check that we cannot add this AVP to another object since it is already linked */
100 CHECK( EINVAL, fd_msg_avp_add( pi, MSG_BRW_LAST_CHILD, avp1) );
101 }
102
103 /* Now add the Proxy-Info AVP at the end of the message */
104 {
105 CHECK( 0, fd_msg_avp_add( acr, MSG_BRW_LAST_CHILD, pi) );
106 #if 0
107 /* For debug: dump the object */
108 fd_log_debug("Dumping Accounting-Request with Proxy-Info AVP at the end");
109 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, acr, fd_g_config->cnf_dict, 0, 1));
110 #endif
111 }
112
113 /* Check the last child is now the proxy-Info */
114 {
115 CHECK( 0, fd_msg_browse ( acr, MSG_BRW_LAST_CHILD, &avp2, NULL) );
116 CHECK( pi, avp2 );
117 }
118
119 /* Check that the avp before the proxy-info is the previous last one */
120 {
121 int diff = 0;
122 CHECK( 0, fd_msg_browse ( pi, MSG_BRW_PREV, &avp2, &diff) );
123 CHECK( avp1, avp2 );
124 CHECK( 0, diff);
125 }
126
127 /* Check that there are no AVP after the proxy-info */
128 CHECK( ENOENT, fd_msg_browse ( pi, MSG_BRW_NEXT, NULL, NULL) );
129
130 /* Test the fd_msg_free function unlinks the object properly */
131 {
132 struct dict_object * rr_model = NULL;
133
134 /* Now find the dictionary object */
135 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Route-Record", &rr_model, ENOENT ) );
136
137 /* Create the instance, using the templates */
138 CHECK( 0, fd_msg_avp_new ( rr_model, 0, &avp1 ) );
139
140 /* Add the AVP at the end of the message */
141 CHECK( 0, fd_msg_avp_add( pi, MSG_BRW_NEXT, avp1) );
142
143 /* Check the last AVP of the message is now this one */
144 CHECK( 0, fd_msg_browse ( acr, MSG_BRW_LAST_CHILD, &avp2, NULL) );
145 CHECK( avp1, avp2 );
146
147 /* Now delete it */
148 CHECK( 0, fd_msg_free( avp1 ) );
149
150 /* Check the last AVP of the message is back to pi */
151 CHECK( 0, fd_msg_browse ( acr, MSG_BRW_LAST_CHILD, &avp2, NULL) );
152 CHECK( pi, avp2 );
153
154 /* Delete the whole message */
155 CHECK( 0, fd_msg_free( acr ) );
156 }
157
158 /* Recreate the message object */
159 {
160 struct dict_object * acr_model = NULL;
161
162 /* Now find the ACR dictionary object */
163 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Accounting-Request", &acr_model, ENOENT ) );
164
165 /* Create the instance, using the templates */
166 CHECK( 0, fd_msg_new ( acr_model, 0, &acr ) );
167 }
168
169 /* Now let's create some additional Dictionary objects for the test */
170 {
171 /* The constant values used here are totally arbitrary chosen */
172 struct dict_object * vendor;
173 {
174 struct dict_vendor_data vendor_data = { 73565, "Vendor test" };
175 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_VENDOR, &vendor_data , NULL, &vendor ) );
176 }
177
178 {
179 struct dict_application_data app_data = { 73566, "Application test" };
180 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_APPLICATION, &app_data , vendor, NULL ) );
181 }
182
183 {
184 struct dict_avp_data avp_data = { 73567, 0, "AVP Test - no vendor - f32", 0, 0, AVP_TYPE_FLOAT32 };
185 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
186 }
187
188 {
189 struct dict_avp_data avp_data = { 139103, 0, "AVP Test - no vendor - f64", 0, 0, AVP_TYPE_FLOAT64 };
190 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
191 }
192
193 {
194 struct dict_object * type = NULL;
195 struct dict_type_data type_data = { AVP_TYPE_INTEGER64, "Int64 test" };
196 struct dict_avp_data avp_data = { 73568, 73565, "AVP Test - i64", AVP_FLAG_VENDOR, AVP_FLAG_VENDOR, AVP_TYPE_INTEGER64 };
197 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_TYPE, &type_data , NULL, &type ) );
198 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , type, NULL ) );
199 }
200
201 {
202 struct dict_object * type = NULL;
203 struct dict_type_data type_data = { AVP_TYPE_INTEGER32, "Enum32 test" };
204 struct dict_enumval_data val1 = { "i32 const test (val 1)", { .i32 = 1 } };
205 struct dict_enumval_data val2 = { "i32 const test (val 2)", { .i32 = 2 } };
206 struct dict_enumval_data val3 = { "i32 const test (val -5)",{ .i32 = -5 } };
207 struct dict_avp_data avp_data = { 73569, 73565, "AVP Test - enumi32", AVP_FLAG_VENDOR, AVP_FLAG_VENDOR, AVP_TYPE_INTEGER32 };
208
209 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_TYPE, &type_data , NULL, &type ) );
210 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , type, NULL ) );
211 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_ENUMVAL, &val1 , type, NULL ) );
212 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_ENUMVAL, &val2 , type, NULL ) );
213 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_ENUMVAL, &val3 , type, NULL ) );
214 }
215
216 {
217 struct dict_object * type = NULL;
218 struct dict_type_data type_data = { AVP_TYPE_OCTETSTRING, "OS test" };
219 struct dict_avp_data avp_data = { 73570, 73565, "AVP Test - os", AVP_FLAG_VENDOR, AVP_FLAG_VENDOR, AVP_TYPE_OCTETSTRING };
220 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_TYPE, &type_data , NULL, &type ) );
221 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , type, NULL ) );
222 }
223
224 {
225 struct dict_object * type = NULL;
226 struct dict_type_data type_data = { AVP_TYPE_OCTETSTRING, "OS enum test" };
227 struct dict_enumval_data val1 = { "os const test (Test)", { .os = { (unsigned char *)"Test", 4 } } };
228 struct dict_enumval_data val2 = { "os const test (waaad)", { .os = { (unsigned char *)"waaad", 5 } } };
229 struct dict_enumval_data val3 = { "os const test (waa)", { .os = { (unsigned char *)"waaad", 3 } } };
230 struct dict_avp_data avp_data = { 73571, 73565, "AVP Test - enumos", AVP_FLAG_VENDOR, AVP_FLAG_VENDOR, AVP_TYPE_OCTETSTRING };
231
232 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_TYPE, &type_data , NULL, &type ) );
233 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , type, NULL ) );
234 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_ENUMVAL, &val1 , type, NULL ) );
235 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_ENUMVAL, &val2 , type, NULL ) );
236 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_ENUMVAL, &val3 , type, NULL ) );
237 }
238
239 {
240 struct dict_object * gavp = NULL;
241 struct dict_avp_data avp_data = { 73572, 73565, "AVP Test - grouped", AVP_FLAG_VENDOR, AVP_FLAG_VENDOR, AVP_TYPE_GROUPED };
242
243 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, &gavp ) );
244
245 /* Macro to search AVP and create a rule */
246 #define ADD_RULE( _parent, _vendor, _avpname, _pos, _min, _max, _ord ) { \
247 struct dict_object * _avp = NULL; \
248 struct dict_avp_request _req = { (_vendor), 0, (_avpname) }; \
249 struct dict_rule_data _data; \
250 CHECK( 0, fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME_AND_VENDOR, &_req, &_avp, ENOENT));\
251 _data.rule_avp = _avp; \
252 _data.rule_position = (_pos); \
253 _data.rule_order = (_ord); \
254 _data.rule_min = (_min); \
255 _data.rule_max = (_max); \
256 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_RULE, &_data , (_parent), NULL ) ); \
257 }
258
259 ADD_RULE(gavp, 73565, "AVP Test - os", RULE_OPTIONAL, -1, -1, 0);
260
261 }
262
263 {
264 struct dict_object * application = NULL;
265 struct dict_object * command = NULL;
266 struct dict_cmd_data cmd_data = { 73573, "Test-Command-Request", CMD_FLAG_REQUEST, CMD_FLAG_REQUEST };
267
268 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Application test", &application, ENOENT ) );
269 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_COMMAND, &cmd_data , application, &command ) );
270 ADD_RULE(command, 0, "AVP Test - no vendor - f32", RULE_FIXED_HEAD, -1, 1, 1);
271 ADD_RULE(command, 73565, "AVP Test - i64", RULE_REQUIRED, -1, -1, 0);
272 ADD_RULE(command, 73565, "AVP Test - enumi32", RULE_OPTIONAL, -1, -1, 0);
273 ADD_RULE(command, 73565, "AVP Test - os", RULE_OPTIONAL, -1, -1, 0);
274 ADD_RULE(command, 73565, "AVP Test - enumos", RULE_OPTIONAL, -1, -1, 0);
275 ADD_RULE(command, 73565, "AVP Test - grouped", RULE_OPTIONAL, -1, -1, 0);
276 }
277
278 {
279 struct dict_object * application = NULL;
280 struct dict_object * command = NULL;
281 struct dict_cmd_data cmd_data = { 73573, "Test-Command-Answer", CMD_FLAG_REQUEST, 0 };
282
283 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Application test", &application, ENOENT ) );
284 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_COMMAND, &cmd_data , application, &command ) );
285 }
286
287 {
288 struct dict_object * gavp = NULL;
289 struct dict_avp_data avp_data = { 73574, 73565, "AVP Test - rules", AVP_FLAG_VENDOR, AVP_FLAG_VENDOR, AVP_TYPE_GROUPED };
290
291 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, &gavp ) );
292
293 ADD_RULE(gavp, 0, "AVP Test - no vendor - f32", RULE_FIXED_HEAD, 0, 1, 1);
294 ADD_RULE(gavp, 73565, "AVP Test - i64", RULE_FIXED_HEAD, -1, 1, 2);
295 ADD_RULE(gavp, 73565, "AVP Test - enumi32", RULE_FIXED_HEAD, -1, 1, 3);
296 ADD_RULE(gavp, 73565, "AVP Test - os", RULE_REQUIRED, 2, 3, 0);
297 ADD_RULE(gavp, 73565, "AVP Test - enumos", RULE_OPTIONAL, 0, 1, 0);
298 ADD_RULE(gavp, 73565, "AVP Test - grouped", RULE_FIXED_TAIL, -1, 1, 1);
299 /* ABNF :
300 < no vendor - f32 >
301 < i64 >
302 < enumi32 >
303 2*3 { os }
304 *1 [ enumos ]
305 < grouped >
306 */
307 #if 0
308 fd_log_debug("%s", fd_dict_dump_object(FD_DUMP_TEST_PARAMS, gavp));
309 #endif
310 }
311
312 {
313 struct dict_object * type = NULL;
314 struct dict_type_data type_data = { AVP_TYPE_OCTETSTRING, "OS test2", NULL, NULL, NULL, fd_dictfct_CharInOS_check, "@." };
315 struct dict_avp_data avp_data = { 73575, 73565, "AVP Test - os2", AVP_FLAG_VENDOR, AVP_FLAG_VENDOR, AVP_TYPE_OCTETSTRING };
316 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_TYPE, &type_data , NULL, &type ) );
317 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , type, NULL ) );
318 }
319
320 #if 0
321 {
322 fd_log_debug("%s", fd_dict_dump_object(FD_DUMP_TEST_PARAMS, vendor));
323 }
324 #endif
325 }
326
327 /* Now create some values and check the length is correctly handled */
328 {
329 struct dict_object * cmd_model = NULL;
330 struct msg * msg = NULL;
331 struct dict_object * avp_model = NULL;
332 struct avp * avp = NULL;
333 union avp_value value;
334
335 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Test-Command-Request", &cmd_model, ENOENT ) );
336
337 /* Check an error is trigged if the AVP has no value set */
338 {
339 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "AVP Test - no vendor - f32", &avp_model, ENOENT ) );
340
341 CHECK( 0, fd_msg_new ( cmd_model, 0, &msg ) );
342 CHECK( 0, fd_msg_avp_new ( avp_model, 0, &avp ) );
343
344 CHECK( 0, fd_msg_avp_add ( msg, MSG_BRW_FIRST_CHILD, avp ) );
345
346 CHECK( EINVAL, fd_msg_update_length ( avp ) );
347 CHECK( EINVAL, fd_msg_update_length ( msg ) );
348
349 CHECK( 0, fd_msg_free( msg ) );
350 }
351
352 /* Check the sizes are handled properly */
353 {
354 struct avp * avpi = NULL;
355 struct avp * avpch = NULL;
356 struct avp_hdr * avpdata = NULL;
357 struct msg_hdr * msgdata = NULL;
358 #define ADD_AVP( _parent, _position, _avpi, _avpvendor, _avpname) { \
359 struct dict_object * _avp = NULL; \
360 struct dict_avp_request _req = { (_avpvendor), 0, (_avpname) }; \
361 CHECK( 0, fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME_AND_VENDOR, &_req, &_avp, ENOENT));\
362 CHECK( 0, fd_msg_avp_new ( _avp, 0, &_avpi ) ); \
363 CHECK( 0, fd_msg_avp_add ( (_parent), (_position), _avpi ) ); \
364 }
365 /* Create a message with many AVP inside */
366 CHECK( 0, fd_msg_new ( cmd_model, 0, &msg ) );
367 CHECK( 0, fd_msg_hdr ( msg, &msgdata ) );
368
369 /* Avp no vendor, float32 => size = 12 */
370 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test - no vendor - f32" );
371 value.f32 = 3.1415;
372 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
373 CHECK( 0, fd_msg_update_length ( avpi ) );
374 #if 0
375 fd_log_debug("AVP no vendor, value 3.1415:");
376 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpi, fd_g_config->cnf_dict, 0, 0));
377 #endif
378 CHECK( 0, fd_msg_avp_hdr ( avpi, &avpdata ) );
379 CHECK( 12, avpdata->avp_len );
380
381 /* Check what happens when we delete the value */
382 CHECK( 0, fd_msg_avp_setvalue ( avpi, NULL ) );
383 CHECK( EINVAL, fd_msg_update_length ( avpi ) );
384 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
385
386 /* Add a vendor AVP, integer64 => size = 20 */
387 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - i64" );
388 value.i64 = 0x123456789abcdeLL;
389 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
390 CHECK( 0, fd_msg_update_length ( avpi ) );
391 #if 0
392 fd_log_debug("AVP vendor, value 0x123456789abcdeL:");
393 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpi, fd_g_config->cnf_dict, 0, 0));
394 #endif
395 CHECK( 0, fd_msg_avp_hdr ( avpi, &avpdata ) );
396 CHECK( 20, avpdata->avp_len );
397
398 /* Check the size of the message is 20 (header) + 12 + 20 = 52 */
399 CHECK( 0, fd_msg_update_length ( msg ) );
400 CHECK( 52, msgdata->msg_length );
401
402 /* Add an AVP with an enum value */
403 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - enumi32" );
404 {
405 struct dict_object * type_model = NULL;
406 struct dict_object * value_model = NULL;
407 struct dict_enumval_request request;
408
409 CHECK( 0, fd_msg_model ( avpi, &avp_model ) );
410 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_TYPE, TYPE_OF_AVP, avp_model, &type_model, ENOENT ) );
411 memset(&request, 0, sizeof(request));
412 request.type_obj = type_model;
413 request.search.enum_name = "i32 const test (val 2)";
414 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &request, &value_model, ENOENT ) );
415 CHECK( 0, fd_dict_getval ( value_model, &request.search ) );
416 CHECK( 0, fd_msg_avp_setvalue ( avpi, &request.search.enum_value ) );
417 #if 0
418 fd_log_debug("AVP enum i32, value 2 (from const):");
419 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpi, fd_g_config->cnf_dict, 0, 0));
420 #endif
421 }
422
423 /* Add an AVP with an enum value, negative */
424 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - enumi32" );
425 {
426 struct dict_object * type_model = NULL;
427 struct dict_object * value_model = NULL;
428 struct dict_enumval_request request;
429
430 CHECK( 0, fd_msg_model ( avpi, &avp_model ) );
431 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_TYPE, TYPE_OF_AVP, avp_model, &type_model, ENOENT ) );
432 memset(&request, 0, sizeof(request));
433 request.type_obj = type_model;
434 request.search.enum_name = "i32 const test (val -5)";
435 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &request, &value_model, ENOENT ) );
436 CHECK( 0, fd_dict_getval ( value_model, &request.search ) );
437 CHECK( 0, fd_msg_avp_setvalue ( avpi, &request.search.enum_value ) );
438 #if 0
439 fd_log_debug("AVP enum i32, value -5 (from const):");
440 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpi, fd_g_config->cnf_dict, 0, 0));
441 #endif
442 /* Check the size is correct ( 12 for header + 4 for value ) */
443 CHECK( 0, fd_msg_update_length ( avpi ) );
444 CHECK( 0, fd_msg_avp_hdr ( avpi, &avpdata ) );
445 CHECK( 16, avpdata->avp_len );
446 }
447
448 /* Now add a value which is not a constant into an enumerated AVP */
449 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - enumi32" );
450 value.i32 = -10;
451 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
452 CHECK( 0, fd_msg_update_length ( avpi ) );
453 #if 0
454 fd_log_debug("AVP vendor enum i32, value -10 (not const):");
455 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpi, fd_g_config->cnf_dict, 0, 0));
456 #endif
457
458 /* Add an octetstring AVP */
459 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - os" );
460 {
461 unsigned char buf[90];
462 memcpy(&buf, "This\0 is a buffer of dat\a. It is not a string so we can have any c\0ntr\0l character here...\0\0", 89);
463 value.os.data = buf;
464 value.os.len = 89;
465 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
466 memset(&buf, 0, sizeof(buf)); /* Test that the OS value is really copied */
467 CHECK( 0, fd_msg_update_length ( avpi ) );
468 #if 0
469 fd_log_debug("AVP octet string, 'This\\0 is a b...'");
470 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpi, fd_g_config->cnf_dict, 0, 0));
471 #endif
472 CHECK( 0, fd_msg_avp_hdr ( avpi, &avpdata ) );
473 CHECK( 101, avpdata->avp_len );
474 CHECK( 'T', avpdata->avp_value->os.data[0] );
475 CHECK( 'i', avpdata->avp_value->os.data[6] );
476 }
477
478 /* Check the size of the message is 20 (header) + 12 + 20 + 16 * 3 + 101 + 3 (padding) = 204 */
479 CHECK( 0, fd_msg_update_length ( msg ) );
480 CHECK( 204, msgdata->msg_length );
481
482 /* Add an octetstring from an enumerated constant */
483 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - enumos" );
484 {
485 struct dict_object * type_model = NULL;
486 struct dict_object * value_model = NULL;
487 struct dict_enumval_request request;
488
489 CHECK( 0, fd_msg_model ( avpi, &avp_model ) );
490 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_TYPE, TYPE_OF_AVP, avp_model, &type_model, ENOENT ) );
491 memset(&request, 0, sizeof(request));
492 request.type_obj = type_model;
493 request.search.enum_name = "os const test (waaad)";
494 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &request, &value_model, ENOENT ) );
495 CHECK( 0, fd_dict_getval ( value_model, &request.search ) );
496 CHECK( 0, fd_msg_avp_setvalue ( avpi, &request.search.enum_value ) );
497 #if 0
498 fd_log_debug("AVP Enumuerated OctetString (from const):");
499 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpi, fd_g_config->cnf_dict, 0, 0));
500 #endif
501 /* Check the size is correct ( 12 for header + 5 for value ) */
502 CHECK( 0, fd_msg_update_length ( avpi ) );
503 CHECK( 0, fd_msg_avp_hdr ( avpi, &avpdata ) );
504 CHECK( 17, avpdata->avp_len );
505 }
506
507 /* Add an octetstring from an enumerated constant */
508 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - enumos" );
509 {
510 struct dict_object * type_model = NULL;
511 struct dict_object * value_model = NULL;
512 struct dict_enumval_request request;
513
514 CHECK( 0, fd_msg_model ( avpi, &avp_model ) );
515 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_TYPE, TYPE_OF_AVP, avp_model, &type_model, ENOENT ) );
516 memset(&request, 0, sizeof(request));
517 request.type_obj = type_model;
518 request.search.enum_name = "os const test (waa)";
519 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_ENUMVAL, ENUMVAL_BY_STRUCT, &request, &value_model, ENOENT ) );
520 CHECK( 0, fd_dict_getval ( value_model, &request.search ) );
521 CHECK( 0, fd_msg_avp_setvalue ( avpi, &request.search.enum_value ) );
522 #if 0
523 fd_log_debug("AVP Enumuerated OctetString (from const):");
524 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpi, fd_g_config->cnf_dict, 0, 0));
525 #endif
526 /* Check the size is correct ( 12 for header + 3 for value ) */
527 CHECK( 0, fd_msg_update_length ( avpi ) );
528 CHECK( 0, fd_msg_avp_hdr ( avpi, &avpdata ) );
529 CHECK( 15, avpdata->avp_len );
530 }
531
532
533 /* Check the size of the message is 20 (header) + 12 + 20 + 16 * 3 + (101 + 3) + (17 + 3) + (15 + 1) = 240 */
534 CHECK( 0, fd_msg_update_length ( msg ) );
535 CHECK( 240, msgdata->msg_length );
536
537 /* Now test the grouped AVPs */
538 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - grouped" );
539 ADD_AVP( avpi, MSG_BRW_LAST_CHILD, avpch, 73565, "AVP Test - os" );
540 {
541 value.os.data = (unsigned char *)"12345678";
542 value.os.len = 8;
543 CHECK( 0, fd_msg_avp_setvalue ( avpch, &value ) );
544 #if 0
545 fd_log_debug("AVP octet string, '1234678'");
546 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpch, fd_g_config->cnf_dict, 0, 0));
547 #endif
548 CHECK( 0, fd_msg_update_length ( avpch ) );
549 CHECK( 0, fd_msg_avp_hdr ( avpch, &avpdata ) );
550 CHECK( 20, avpdata->avp_len );
551 }
552 ADD_AVP( avpi, MSG_BRW_LAST_CHILD, avpch, 73565, "AVP Test - os" );
553 {
554 value.os.data = (unsigned char *)"123456789";
555 value.os.len = 9;
556 CHECK( 0, fd_msg_avp_setvalue ( avpch, &value ) );
557 #if 0
558 fd_log_debug("AVP octet string, '12346789'");
559 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, avpch, fd_g_config->cnf_dict, 0, 0));
560 #endif
561 }
562
563 /* Check the size is updated recursively: (gavp hdr: 12) + (avp1: 20) + (avp2: 21 + 3) = 56 */
564 CHECK( 0, fd_msg_update_length ( avpi ) );
565 CHECK( 0, fd_msg_avp_hdr ( avpi, &avpdata ) );
566 CHECK( 56, avpdata->avp_len );
567
568 /* Add another similar grouped AVP, to have lot of padding */
569 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 73565, "AVP Test - grouped" );
570 ADD_AVP( avpi, MSG_BRW_LAST_CHILD, avpch, 73565, "AVP Test - os" );
571 {
572 value.os.data = (unsigned char *)"1";
573 value.os.len = 1;
574 CHECK( 0, fd_msg_avp_setvalue ( avpch, &value ) );
575 }
576 ADD_AVP( avpi, MSG_BRW_LAST_CHILD, avpch, 73565, "AVP Test - os" );
577 {
578 value.os.data = (unsigned char *)"1234567";
579 value.os.len = 7;
580 CHECK( 0, fd_msg_avp_setvalue ( avpch, &value ) );
581 }
582
583 /* Now check the global size of the message, if padding is correctly handled */
584 /* size = 20 (header) + 12 + 20 + 16 * 3 + (101 + 3) + (17 + 3) + (15 + 1)
585 * + ( 12 + ( 20 + 21) + 3 ) # padding for the grouped AVP = 3
586 * + ( 12 + ( (13 + 3) + 19 ) + 1 ) # and 1 for this one
587 * size = 240 + 56 + 48 = 344
588 */
589 CHECK( 0, fd_msg_update_length ( msg ) );
590 #if 0
591 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, msg, fd_g_config->cnf_dict, 0, 1));
592 #endif
593 CHECK( 344, msgdata->msg_length );
594
595 /* Set the application to the test application: 73566 */
596 msgdata->msg_appl = 73566;
597
598 /* Set the hop-by-hop ID to a random value: 0x4b44b41d */
599 msgdata->msg_hbhid = 0x4b44b41d;
600 /* Set the end-to-end ID to a random value: 0xe2ee2e1d */
601 msgdata->msg_eteid = 0xe2ee2e1d;
602 }
603
604 /* Test the msg_bufferize function */
605 {
606
607 CHECK( 0, fd_msg_bufferize( msg, &buf, NULL ) );
608
609 /* Test the first bytes */
610 CHECK( 0x01, buf[0] ); /* Version */
611 CHECK( 0x00, buf[1] ); /* Length: 344 = 0x000158 */
612 CHECK( 0x01, buf[2] );
613 CHECK( 0x58, buf[3] );
614 CHECK( 0x80, buf[4] ); /* flags: only "R" is set. */
615 CHECK( 0x01, buf[5] ); /* Command code: 73573 = 0x011F65 */
616 CHECK( 0x1F, buf[6] );
617 CHECK( 0x65, buf[7] );
618 CHECK( 0x00, buf[8] ); /* App ID: 73566 = 0x00011F5E */
619 CHECK( 0x01, buf[9] );
620 CHECK( 0x1F, buf[10] );
621 CHECK( 0x5E, buf[11] );
622 CHECK( 0x4b, buf[12] ); /* hop-by-hop id: 0x4b44b41d */
623 CHECK( 0x44, buf[13] );
624 CHECK( 0xb4, buf[14] );
625 CHECK( 0x1d, buf[15] );
626 CHECK( 0xe2, buf[16] ); /* end-to-end id: 0xe2ee2e1d */
627 CHECK( 0xee, buf[17] );
628 CHECK( 0x2e, buf[18] );
629 CHECK( 0x1d, buf[19] );
630
631 CHECK( 0x00, buf[20] ); /* First AVP (AVP Test - no vendor - f32) begin: code 73567 = 0x00011F5F */
632 CHECK( 0x01, buf[21] );
633 CHECK( 0x1F, buf[22] );
634 CHECK( 0x5F, buf[23] );
635 CHECK( 0x00, buf[24] ); /* flags: 0 */
636 CHECK( 0x00, buf[25] ); /* length: 12 = 0x00000c */
637 CHECK( 0x00, buf[26] );
638 CHECK( 0x0C, buf[27] );
639 CHECK( 0x40, buf[28] ); /* Value: 3.1415: sign = '+' => most significant bit = 0 */
640 CHECK( 0x49, buf[29] ); /* 2 <= 3.1415 < 4 => exponent = 1 => biaised (on 8 bits) = (decimal) 128 = (binary) 100 0000 0 */
641 CHECK( 0x0e, buf[30] ); /* significand = (decimal) 1.57075 = (binary) 1.100 1001 0000 1110 0101 0110 */
642 CHECK( 0x56, buf[31] ); /* total => 0100 0000 0100 1001 0000 1110 0101 0110 = (hexa) 40 49 0e 56*/
643
644 /* The other AVPs will be tested by successful parsing... */
645 }
646
647 /* Now free the message, we keep only the buffer. */
648 CHECK( 0, fd_msg_free( msg ) );
649
650 }
651
652 /* Test the parsing of buffers and messages */
653 {
654 unsigned char * buf_cpy = NULL;
655 struct msg * msg;
656
657 #define CPYBUF() { \
658 buf_cpy = malloc(344); \
659 CHECK( buf_cpy ? 1 : 0, 1); \
660 memcpy(buf_cpy, buf, 344); \
661 }
662
663 /* Test the msg_parse_buffer function */
664 {
665 CPYBUF();
666 CHECK( EBADMSG, fd_msg_parse_buffer( &buf_cpy, 340, &msg) );
667
668 CPYBUF();
669 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
670 #if 0
671 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, msg, fd_g_config->cnf_dict, 0, 1));
672 #endif
673
674 /* reinit the msg */
675 CHECK( 0, fd_msg_free ( msg ) );
676
677 }
678
679 /* Test the fd_msg_search_avp function */
680 {
681 struct dict_object * avp_model;
682 struct avp * found;
683 struct avp_hdr * avpdata = NULL;
684
685 /* Now find the ACR dictionary object */
686 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "AVP Test - no vendor - f32", &avp_model, ENOENT ) );
687
688 CPYBUF();
689 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
690
691 /* Search this AVP instance in the msg */
692 CHECK( 0, fd_msg_search_avp( msg, avp_model, &found ) );
693
694 /* Check the AVP value is 3.1415 */
695 CHECK( 0, fd_msg_avp_hdr ( found, &avpdata ) );
696 CHECK( 3.1415F, avpdata->avp_value->f32 );
697
698 /* reinit the msg */
699 CHECK( 0, fd_msg_free ( msg ) );
700
701 }
702
703 /* Test the msg_parse_dict function */
704 {
705 /* Test with an unknown command code */
706 {
707 CPYBUF();
708
709 /* Change the command-code */
710 buf_cpy[5] = 0x11;
711 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
712 CHECK( ENOTSUP, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, NULL ) );
713
714 /* reset */
715 CHECK( 0, fd_msg_free ( msg ) );
716 }
717
718 /* Test with an unknown Mandatory AVP */
719 {
720 CPYBUF();
721
722 buf_cpy[20] = 0x11; /* New AVP code = 0x11011F5F, undefined */
723 buf_cpy[24] = 0x40; /* Add the 'M' flag */
724
725 /* Check that we cannot support this message now */
726 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
727 CHECK( ENOTSUP, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, NULL ) );
728
729 /* reset */
730 CHECK( 0, fd_msg_free ( msg ) );
731 }
732
733 /* Test with an unknown optional AVP */
734 {
735 CPYBUF();
736
737 buf_cpy[20] = 0x11; /* New AVP code = 0x11011F5F, undefined */
738
739 /* Check that we can support this message now */
740 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
741 CHECK( 0, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, NULL ) );
742
743 #if 0
744 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, msg, fd_g_config->cnf_dict, 0, 1));
745 #endif
746
747 /* reset */
748 CHECK( 0, fd_msg_free ( msg ) );
749 }
750
751 /* Test with an invalid AVP (definition mismatch with the dictionary) */
752 {
753 CPYBUF();
754
755 buf_cpy[21] = 0x02; /* New AVP code = 0x00021F5F, f64 type in the dictionary */
756
757
758 /* Check that we cannot support this message now */
759 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
760 CHECK( EBADMSG, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, NULL ) );
761
762 /* reset */
763 CHECK( 0, fd_msg_free ( msg ) );
764 }
765
766 /* Test with a type verifier */
767 {
768 struct fd_pei error_info;
769 CPYBUF();
770 buf_cpy[103] = 0x67; /* Replaced AVP code = 0x00011F67, OS test2 type in the dictionary */
771
772 /* Check that we cannot support this message now */
773 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
774 CHECK( EBADMSG, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, NULL ) );
775
776 /* reset */
777 CHECK( 0, fd_msg_free ( msg ) );
778
779 CPYBUF();
780 buf_cpy[103] = 0x67; /* Replaced AVP code = 0x00011F67, OS test2 type in the dictionary */
781
782 /* Check error reporting works */
783 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
784 CHECK( EBADMSG, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, &error_info ) );
785
786 #if 1
787 fd_log_debug("Error reported: %s\n in AVP: %s", error_info.pei_message, fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, error_info.pei_avp, fd_g_config->cnf_dict, 0, 1));
788 #endif
789
790 /* reset */
791 CHECK( 0, fd_msg_free ( msg ) );
792
793 CPYBUF();
794 buf_cpy[103] = 0x67; /* Replaced AVP code = 0x00011F67, OS test2 type in the dictionary */
795 buf_cpy[130] = '@';
796 buf_cpy[140] = '.'; /* now we comply to the constraints */
797
798 /* Check that we cannot support this message now */
799 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
800 CHECK( 0, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, NULL ) );
801
802 /* reset */
803 CHECK( 0, fd_msg_free ( msg ) );
804
805
806 }
807
808 {
809 unsigned char * buftmp = NULL;
810 struct msg * error;
811 /* Check the parse or error works as expected */
812 CPYBUF();
813
814 buf_cpy[21] = 0x02; /* New AVP code = 0x00021F5F, f64 type in the dictionary */
815
816 /* Check that we cannot support this message now */
817 CHECK( 0, fd_msg_init() );
818 CHECK( 0, fd_msg_parse_buffer( &buf_cpy, 344, &msg) );
819 CHECK( EBADMSG, fd_msg_parse_or_error( &msg, &error ) );
820 CHECK( NULL, msg );
821 msg = error;
822
823 CHECK( 0, fd_msg_bufferize( msg, &buftmp, NULL ) );
824
825 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, msg, fd_g_config->cnf_dict, 0, 1));
826
827 TODO("Check the Failed-AVP is as expected");
828
829
830 /* reset */
831 CHECK( 0, fd_msg_free ( msg ) );
832 free(buftmp);
833 }
834
835
836 CHECK( 0, fd_msg_parse_buffer( &buf, 344, &msg) );
837 CHECK( 0, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, NULL ) );
838 #if 0
839 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, msg, fd_g_config->cnf_dict, 0, 1));
840 #endif
841 }
842
843 /* Now test the msg_parse_rule function */
844 {
845 struct fd_pei pei;
846
847 CHECK( 0, fd_msg_parse_rules( msg, fd_g_config->cnf_dict, &pei ) );
848
849 /* Use the "AVP Test - rules" AVP to test the rules */
850 {
851 struct avp * tavp = NULL;
852 struct avp * tempavp = NULL;
853 struct avp * childavp = NULL;
854
855 ADD_AVP( msg, MSG_BRW_LAST_CHILD, tavp, 73565, "AVP Test - rules" );
856
857 /* Create a conforming message first */
858 ADD_AVP( tavp, MSG_BRW_LAST_CHILD, childavp, 0, "AVP Test - no vendor - f32" );
859 ADD_AVP( tavp, MSG_BRW_LAST_CHILD, childavp, 73565, "AVP Test - i64" );
860 ADD_AVP( tavp, MSG_BRW_LAST_CHILD, childavp, 73565, "AVP Test - enumi32" );
861 ADD_AVP( tavp, MSG_BRW_LAST_CHILD, childavp, 73565, "AVP Test - os" );
862 ADD_AVP( tavp, MSG_BRW_LAST_CHILD, childavp, 73565, "AVP Test - os" );
863 ADD_AVP( tavp, MSG_BRW_LAST_CHILD, childavp, 73565, "AVP Test - grouped" );
864
865 /* Check the message is still conform */
866 CHECK( 0, fd_msg_parse_rules( msg, fd_g_config->cnf_dict, &pei ) );
867
868 /* The first avp is optional in fixed position, so remove it and check the message is still OK */
869 CHECK( 0, fd_msg_browse ( tavp, MSG_BRW_FIRST_CHILD, &childavp, NULL) );
870 CHECK( 0, fd_msg_free ( childavp ) );
871 CHECK( 0, fd_msg_parse_rules( msg, fd_g_config->cnf_dict, &pei ) );
872 ADD_AVP( tavp, MSG_BRW_FIRST_CHILD, childavp, 0, "AVP Test - no vendor - f32" );
873
874
875 /* Now break some rules and check it is detected */
876 #define CHECK_CONFLICT( _msg, _error, _conflictavp_name, _conflictavp_vnd ) { \
877 struct fd_pei _pei; \
878 CHECK( EBADMSG, fd_msg_parse_rules( _msg, fd_g_config->cnf_dict, &_pei ) ); \
879 if (_error) { \
880 CHECK( 0, strcmp( _error, _pei.pei_errcode ) ); \
881 } \
882 if ((_conflictavp_name) == NULL) { \
883 CHECK( NULL, _pei.pei_avp); \
884 } else { \
885 struct dict_avp_request _req = { (_conflictavp_vnd), 0, (_conflictavp_name) }; \
886 struct dict_object * _avp; \
887 struct dict_object * _conflict; \
888 CHECK( 1, (_pei.pei_avp) ? 1 : 0 ); \
889 CHECK( 0, fd_msg_model( _pei.pei_avp, &_conflict ) ); \
890 CHECK( 0, fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME_AND_VENDOR, &_req, &_avp, ENOENT)); \
891 CHECK( _avp, _conflict ); \
892 } \
893 }
894
895 /* ABNF :
896 < no vendor - f32 >
897 < i64 >
898 < enumi32 >
899 2*3 { os }
900 *1 [ enumos ]
901 < grouped >
902 */
903 {
904 /* Test the FIXED_HEAD rules positions: add another AVP before the third */
905 CHECK( 0, fd_msg_browse ( tavp, MSG_BRW_FIRST_CHILD, &tempavp, NULL) ); /* tempavp is the novendor avp */
906 CHECK( 0, fd_msg_browse ( tempavp, MSG_BRW_NEXT, &tempavp, NULL) ); /* tempavp is the i64 avp */
907 ADD_AVP( tempavp, MSG_BRW_NEXT, childavp, 73565, "AVP Test - os" );
908
909 CHECK_CONFLICT( msg, "DIAMETER_MISSING_AVP", "AVP Test - enumi32", 73565 );
910
911 /* Now remove this AVP */
912 CHECK( 0, fd_msg_free ( childavp ) );
913 }
914 {
915 /* Remove the third AVP, same rule must conflict */
916 CHECK( 0, fd_msg_browse ( tempavp, MSG_BRW_NEXT, &childavp, NULL) ); /* childavp is the enumi32 avp */
917 CHECK( 0, fd_msg_free ( childavp ) );
918
919 CHECK_CONFLICT( msg, "DIAMETER_MISSING_AVP", "AVP Test - enumi32", 73565 );
920
921 /* Add the AVP back */
922 ADD_AVP( tempavp, MSG_BRW_NEXT, childavp, 73565, "AVP Test - enumi32" );
923 }
924
925 {
926 /* Test the minimum value in the REQUIRED rule: delete one of the os AVPs */
927 CHECK( 0, fd_msg_browse ( childavp, MSG_BRW_NEXT, &tempavp, NULL) ); /* tempavp is the os avp */
928 CHECK( 0, fd_msg_free ( tempavp ) );
929
930 CHECK_CONFLICT( msg, "DIAMETER_MISSING_AVP", "AVP Test - os", 73565 ); /* The rule requires at least 2 AVP, we have only 1 */
931
932 /* Now add this AVP */
933 ADD_AVP( childavp, MSG_BRW_NEXT, tempavp, 73565, "AVP Test - os" );
934 }
935 {
936 /* Test the maximum value in the REQUIRED rule: add more of the os AVPs */
937 ADD_AVP( childavp, MSG_BRW_NEXT, tempavp, 73565, "AVP Test - os" );
938 ADD_AVP( childavp, MSG_BRW_NEXT, tempavp, 73565, "AVP Test - os" );
939
940 CHECK_CONFLICT( msg, "DIAMETER_AVP_OCCURS_TOO_MANY_TIMES", "AVP Test - os", 73565 ); /* The rule requires at most 3 AVP, we have 4 */
941
942 /* Now delete these AVP */
943 CHECK( 0, fd_msg_free ( tempavp ) );
944 CHECK( 0, fd_msg_browse ( childavp, MSG_BRW_NEXT, &tempavp, NULL) );
945 CHECK( 0, fd_msg_free ( tempavp ) );
946 }
947
948 {
949 /* Test the maximum value in the OPTIONAL rule: add 2 enumos AVPs */
950 ADD_AVP( childavp, MSG_BRW_NEXT, tempavp, 73565, "AVP Test - enumos" );
951
952 /* The message is still conform */
953 CHECK( 0, fd_msg_parse_rules( msg, fd_g_config->cnf_dict, &pei ) );
954
955 /* Now break the rule */
956 ADD_AVP( childavp, MSG_BRW_NEXT, tempavp, 73565, "AVP Test - enumos" );
957
958 CHECK_CONFLICT( msg, "DIAMETER_AVP_OCCURS_TOO_MANY_TIMES", "AVP Test - enumos", 73565 );
959
960 /* Now delete this AVP */
961 CHECK( 0, fd_msg_free ( tempavp ) );
962 }
963
964 {
965 /* Test the RULE_FIXED_TAIL rules positions: add another AVP at the end */
966 ADD_AVP( tavp, MSG_BRW_LAST_CHILD, childavp, 73565, "AVP Test - os" );
967
968 CHECK_CONFLICT( msg, "DIAMETER_MISSING_AVP", "AVP Test - grouped", 73565 );
969
970 /* Now remove this AVP */
971 CHECK( 0, fd_msg_free ( childavp ) );
972 }
973 }
974 }
975
976 /* Test the fd_msg_new_answer_from_req function */
977 {
978 struct dict_object * cmd_model = NULL;
979 struct msg * msg = NULL;
980 struct avp * pi1, *pi2, *avp;
981 char * host1="host1", * host2="host2";
982 union avp_value value;
983 struct msg_hdr * msgdata = NULL;
984
985 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Test-Command-Request", &cmd_model, ENOENT ) );
986
987 /* Test default behavior without flags */
988 {
989 /* Create a message with some AVPs inside */
990 CHECK( 0, fd_msg_new ( cmd_model, 0, &msg ) );
991 CHECK( 0, fd_msg_hdr ( msg, &msgdata ) );
992
993 /* Add a session id */
994 CHECK( 0, fd_msg_new_session( msg, (os0_t)"testmsg", strlen("testmsg") ) );
995
996 /* Create two instances of Proxy-Info */
997 ADD_AVP( msg, MSG_BRW_LAST_CHILD, pi1, 0, "Proxy-Info");
998 ADD_AVP( msg, MSG_BRW_LAST_CHILD, pi2, 0, "Proxy-Info");
999
1000 ADD_AVP( pi1, MSG_BRW_LAST_CHILD, avp, 0, "Proxy-State");
1001 value.os.data = (os0_t)"ps_pi1";
1002 value.os.len = strlen((char *)value.os.data);
1003 CHECK( 0, fd_msg_avp_setvalue ( avp, &value ) );
1004
1005 ADD_AVP( pi2, MSG_BRW_LAST_CHILD, avp, 0, "Proxy-State");
1006 value.os.data = (os0_t)"pi2_state";
1007 value.os.len = strlen((char *)value.os.data);
1008 CHECK( 0, fd_msg_avp_setvalue ( avp, &value ) );
1009
1010 ADD_AVP( pi1, MSG_BRW_FIRST_CHILD, avp, 0, "Proxy-Host");
1011 value.os.data = (os0_t)host1;
1012 value.os.len = strlen(host1);
1013 CHECK( 0, fd_msg_avp_setvalue ( avp, &value ) );
1014
1015 ADD_AVP( pi2, MSG_BRW_LAST_CHILD, avp, 0, "Proxy-Host");
1016 value.os.data = (os0_t)host2;
1017 value.os.len = strlen(host2);
1018 CHECK( 0, fd_msg_avp_setvalue ( avp, &value ) );
1019
1020 ADD_AVP( pi2, MSG_BRW_LAST_CHILD, avp, 73565, "AVP Test - i64");
1021 value.i64 = 0x123456789abcdeLL;
1022 CHECK( 0, fd_msg_avp_setvalue ( avp, &value ) );
1023
1024
1025 /* Now call the fd_msg_new_answer_from_req function */
1026 CHECK( 0, fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, &msg, 0 ) );
1027
1028 /* Check there is a Session-Id AVP */
1029 {
1030 struct session * sess;
1031 int new;
1032 CHECK( 0, fd_msg_sess_get(fd_g_config->cnf_dict, msg, &sess, &new) );
1033 CHECK( 1, sess == NULL ? 0 : 1 );
1034 CHECK( 0, new ? 1 : 0 );
1035 }
1036
1037 /* Check there are two Proxy-Info with the two hosts */
1038 {
1039 int got_h1 = 0, got_h2=0;
1040 CHECK( 0, fd_msg_browse ( msg, MSG_BRW_FIRST_CHILD, &avp, NULL) );
1041 while(avp) {
1042 struct avp_hdr * avpdata = NULL;
1043 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1044 if (avpdata->avp_code == AC_PROXY_INFO) {
1045 struct avp * iavp;
1046 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_FIRST_CHILD, &iavp, NULL) );
1047 while(iavp) {
1048 struct avp_hdr * iavpdata = NULL;
1049 CHECK( 0, fd_msg_avp_hdr ( iavp, &iavpdata ) );
1050 if (iavpdata->avp_code == AC_PROXY_HOST) {
1051 if (!memcmp(host1, iavpdata->avp_value->os.data, strlen(host1)))
1052 got_h1++;
1053 if (!memcmp(host2, iavpdata->avp_value->os.data, strlen(host2)))
1054 got_h2++;
1055 }
1056 CHECK( 0, fd_msg_browse ( iavp, MSG_BRW_NEXT, &iavp, NULL) );
1057 }
1058 }
1059
1060 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1061 }
1062
1063 CHECK(1, got_h1);
1064 CHECK(1, got_h2);
1065 }
1066
1067 /* Now test the behavior of fd_msg_rescode_set with a grouped AVP */
1068 CHECK( 0, fd_msg_rescode_set(msg, "DIAMETER_AVP_OCCURS_TOO_MANY_TIMES", NULL, pi1, 1) );
1069
1070 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, msg, fd_g_config->cnf_dict, 0, 1));
1071
1072 TODO("Check the Failed-AVP is as expected");
1073 }
1074
1075 }
1076 }
1077
1078 /* Test the msg_avp_value_interpret and msg_avp_value_encode functions. use the Address type and Host-IP-Address AVPs */
1079 {
1080 struct dict_object * cer_model = NULL;
1081 struct msg * cer = NULL;
1082
1083 struct dict_object * hia_model = NULL;
1084 struct avp *avp4, *avp6;
1085 #define TEST_IP4 "192.168.100.101"
1086 char buf4[INET_ADDRSTRLEN];
1087 #define TEST_IP6 "1111:2222:3333:4444:1234:5678:9abc:def0"
1088 char buf6[INET6_ADDRSTRLEN];
1089
1090 struct sockaddr_storage ss;
1091 struct sockaddr_in sin, *psin;
1092 struct sockaddr_in6 sin6, *psin6;
1093
1094 /* Find the CER dictionary object */
1095 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Capabilities-Exchange-Request", &cer_model, ENOENT ) );
1096
1097 /* Now find the Host-IP-Address dictionary object */
1098 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Host-IP-Address", &hia_model, ENOENT ) );
1099
1100 /* Create the msg instance */
1101 CHECK( 0, fd_msg_new ( cer_model, 0, &cer ) );
1102
1103 /* Create the avp instances */
1104 CHECK( 0, fd_msg_avp_new ( hia_model, 0, &avp4 ) );
1105 CHECK( 0, fd_msg_avp_new ( hia_model, 0, &avp6 ) );
1106
1107 /* Set the value of the IP avp */
1108 sin.sin_family = AF_INET;
1109 CHECK( 1, inet_pton( AF_INET, TEST_IP4, &sin.sin_addr.s_addr ) );
1110 CHECK( 0, fd_msg_avp_value_encode ( &sin, avp4 ) );
1111
1112 /* Set the value of the IP6 avp */
1113 sin6.sin6_family = AF_INET6;
1114 CHECK( 1, inet_pton( AF_INET6, TEST_IP6, &sin6.sin6_addr.s6_addr ) );
1115 CHECK( 0, fd_msg_avp_value_encode ( &sin6, avp6 ) );
1116
1117 /* Add these AVPs in the message */
1118 CHECK( 0, fd_msg_avp_add( cer, MSG_BRW_LAST_CHILD, avp4) );
1119 CHECK( 0, fd_msg_avp_add( cer, MSG_BRW_LAST_CHILD, avp6) );
1120
1121 /* Create the buffer for this message */
1122 CHECK( 0, fd_msg_bufferize( cer, &buf, NULL ) );
1123
1124 /* Now free the message, we keep only the buffer. */
1125 CHECK( 0, fd_msg_free( cer ) );
1126
1127 /* Check the content of the buffer is correct (skip command header) */
1128 CHECK( 0x00, buf[20] ); /* First AVP (IP4) begins: code 257 = 0x00000101 */
1129 CHECK( 0x00, buf[21] );
1130 CHECK( 0x01, buf[22] );
1131 CHECK( 0x01, buf[23] );
1132 CHECK( 0x40, buf[24] ); /* flags: M */
1133 CHECK( 0x00, buf[25] ); /* length: 8+6 = 0x00000e */
1134 CHECK( 0x00, buf[26] );
1135 CHECK( 0x0E, buf[27] );
1136 CHECK( 0x00, buf[28] ); /* Value: AddressType 1 */
1137 CHECK( 0x01, buf[29] );
1138 CHECK( 192, buf[30] ); /* 192.168.100.101 */
1139 CHECK( 168, buf[31] );
1140 CHECK( 100, buf[32] );
1141 CHECK( 101, buf[33] );
1142
1143 CHECK( 0x00, buf[34] ); /* Padding */
1144 CHECK( 0x00, buf[35] );
1145
1146 CHECK( 0x00, buf[36] ); /* Second AVP (IP6) begins: code 257 = 0x00000101 */
1147 CHECK( 0x00, buf[37] );
1148 CHECK( 0x01, buf[38] );
1149 CHECK( 0x01, buf[39] );
1150 CHECK( 0x40, buf[40] ); /* flags: M */
1151 CHECK( 0x00, buf[41] ); /* length: 8+18 = 0x00001a */
1152 CHECK( 0x00, buf[42] );
1153 CHECK( 0x1A, buf[43] );
1154 CHECK( 0x00, buf[44] ); /* Value: AddressType 2 */
1155 CHECK( 0x02, buf[45] );
1156 CHECK( 0x11, buf[46] ); /* 1111:2222:3333:4444:1234:5678:9abc:def0 */
1157 CHECK( 0x11, buf[47] );
1158 CHECK( 0x22, buf[48] );
1159 CHECK( 0x22, buf[49] );
1160 CHECK( 0x33, buf[50] );
1161 CHECK( 0x33, buf[51] );
1162 CHECK( 0x44, buf[52] );
1163 CHECK( 0x44, buf[53] );
1164 CHECK( 0x12, buf[54] );
1165 CHECK( 0x34, buf[55] );
1166 CHECK( 0x56, buf[56] );
1167 CHECK( 0x78, buf[57] );
1168 CHECK( 0x9a, buf[58] );
1169 CHECK( 0xbc, buf[59] );
1170 CHECK( 0xde, buf[60] );
1171 CHECK( 0xf0, buf[61] );
1172
1173 /* Ok, now let's recreate the message */
1174 CHECK( 0, fd_msg_parse_buffer( &buf, 64, &cer) );
1175 CHECK( 0, fd_msg_parse_dict( cer, fd_g_config->cnf_dict, NULL ) );
1176
1177 /* Get the pointers to the first and last AVP */
1178 CHECK( 0, fd_msg_browse( cer, MSG_BRW_FIRST_CHILD, &avp4, NULL) );
1179 CHECK( 0, fd_msg_browse( cer, MSG_BRW_LAST_CHILD, &avp6, NULL) );
1180
1181 /* Try and interpret the data in the AVPs */
1182 CHECK( 0, fd_msg_avp_value_interpret ( avp4, &ss ) );
1183 psin = (struct sockaddr_in *)&ss;
1184 CHECK( AF_INET, psin->sin_family );
1185 CHECK( 0, (inet_ntop( AF_INET, &psin->sin_addr.s_addr, buf4, sizeof(buf4) ) == NULL) ? errno : 0 );
1186 CHECK( 0, strcmp( buf4, TEST_IP4 ) );
1187
1188 CHECK( 0, fd_msg_avp_value_interpret ( avp6, &ss ) );
1189 psin6 = (struct sockaddr_in6 *)&ss;
1190 CHECK( AF_INET6, psin6->sin6_family );
1191 CHECK( 0, (inet_ntop( AF_INET6, &psin6->sin6_addr.s6_addr, buf6, sizeof(buf6) ) == NULL) ? errno : 0 );
1192 CHECK( 0, strcasecmp( buf6, TEST_IP6 ) );
1193
1194 /* Ok, it's done */
1195 CHECK( 0, fd_msg_free( cer ) );
1196 }
1197
1198 /* Check proper encoding / decoding for all basic types of AVP */
1199 {
1200 {
1201 struct dict_avp_data avp_data = { 91001, 0, "AVP Test 2 - os", 0, 0, AVP_TYPE_OCTETSTRING };
1202 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
1203 }
1204 {
1205 struct dict_avp_data avp_data = { 91002, 0, "AVP Test 2 - i32", 0, 0, AVP_TYPE_INTEGER32 };
1206 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
1207 }
1208 {
1209 struct dict_avp_data avp_data = { 91003, 0, "AVP Test 2 - i64", 0, 0, AVP_TYPE_INTEGER64 };
1210 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
1211 }
1212 {
1213 struct dict_avp_data avp_data = { 91004, 0, "AVP Test 2 - u32", 0, 0, AVP_TYPE_UNSIGNED32 };
1214 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
1215 }
1216 {
1217 struct dict_avp_data avp_data = { 91005, 0, "AVP Test 2 - u64", 0, 0, AVP_TYPE_UNSIGNED64 };
1218 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
1219 }
1220 {
1221 struct dict_avp_data avp_data = { 91006, 0, "AVP Test 2 - f32", 0, 0, AVP_TYPE_FLOAT32 };
1222 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
1223 }
1224 {
1225 struct dict_avp_data avp_data = { 91007, 0, "AVP Test 2 - f64", 0, 0, AVP_TYPE_FLOAT64 };
1226 CHECK( 0, fd_dict_new ( fd_g_config->cnf_dict, DICT_AVP, &avp_data , NULL, NULL ) );
1227 }
1228
1229 {
1230 struct dict_object * cmd_model = NULL;
1231 struct msg * msg = NULL;
1232 struct avp * avp = NULL;
1233 union avp_value value;
1234 struct avp * avpi = NULL;
1235 struct avp_hdr * avpdata = NULL;
1236 struct msg_hdr * msgdata = NULL;
1237
1238 CHECK( 0, fd_dict_search ( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Test-Command-Request", &cmd_model, ENOENT ) );
1239
1240 /* Create a message */
1241 CHECK( 0, fd_msg_new ( cmd_model, 0, &msg ) );
1242 CHECK( 0, fd_msg_hdr ( msg, &msgdata ) );
1243
1244 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - os" );
1245 value.os.data = (unsigned char *) "waaad";
1246 value.os.len = 6;
1247 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1248
1249 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - i32" );
1250 value.i32 = 0x123456;
1251 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1252
1253 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - i32" );
1254 value.i32 = -0x123456;
1255 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1256
1257 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - i64" );
1258 value.i64 = 0x11223344556677LL;
1259 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1260
1261 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - i64" );
1262 value.i64 = -0x11223344556677LL;
1263 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1264
1265 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - u32" );
1266 value.u32 = 0xFEDCBA98;
1267 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1268
1269 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - u64" );
1270 value.u64 = 0x123456789abcdef0LL;
1271 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1272
1273 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - f32" );
1274 value.f32 = 2097153.0F;
1275 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1276
1277 ADD_AVP( msg, MSG_BRW_LAST_CHILD, avpi, 0, "AVP Test 2 - f64" );
1278 value.f64 = -1099511627777LL;
1279 CHECK( 0, fd_msg_avp_setvalue ( avpi, &value ) );
1280
1281 /* Ok now bufferize */
1282 CHECK( 0, fd_msg_bufferize( msg, &buf, NULL ) );
1283
1284 /* Test the first bytes */
1285 CHECK( 0x01, buf[0] ); /* Version */
1286 CHECK( 0x00, buf[1] ); /* Length: 148 = 0x000094 */
1287 CHECK( 0x00, buf[2] );
1288 CHECK( 0x94, buf[3] );
1289 CHECK( 0x80, buf[4] ); /* flags: only "R" is set. */
1290 CHECK( 0x01, buf[5] ); /* Command code: 73573 = 0x011F65 */
1291 CHECK( 0x1F, buf[6] );
1292 CHECK( 0x65, buf[7] );
1293 CHECK( 0x00, buf[8] ); /* App ID */
1294 CHECK( 0x01, buf[9] );
1295 CHECK( 0x1F, buf[10] );
1296 CHECK( 0x5E, buf[11] );
1297 CHECK( 0x00, buf[12] ); /* hop-by-hop id */
1298 CHECK( 0x00, buf[13] );
1299 CHECK( 0x00, buf[14] );
1300 CHECK( 0x00, buf[15] );
1301 CHECK( 0x00, buf[16] ); /* end-to-end id */
1302 CHECK( 0x00, buf[17] );
1303 CHECK( 0x00, buf[18] );
1304 CHECK( 0x00, buf[19] );
1305
1306 CHECK( 0x00, buf[20] ); /* First AVP (AVP Test 2 - os) begin: code 91001 = 0x00016379 */
1307 CHECK( 0x01, buf[21] );
1308 CHECK( 0x63, buf[22] );
1309 CHECK( 0x79, buf[23] );
1310 CHECK( 0x00, buf[24] ); /* flags: 0 */
1311 CHECK( 0x00, buf[25] ); /* length: 14 = 0x00000e */
1312 CHECK( 0x00, buf[26] );
1313 CHECK( 0x0e, buf[27] );
1314
1315 CHECK( 0x77, buf[28] ); /* "waaad\0" + padding */
1316 CHECK( 0x61, buf[29] );
1317 CHECK( 0x61, buf[30] );
1318 CHECK( 0x61, buf[31] );
1319 CHECK( 0x64, buf[32] );
1320 CHECK( 0x00, buf[33] );
1321 CHECK( 0x00, buf[34] );
1322 CHECK( 0x00, buf[35] );
1323
1324 /* 36 ~ 43 : 2nd AVP header (size at last octet) */
1325 CHECK( 0x0c, buf[43] );
1326 CHECK( 0x00, buf[44] ); /* 0x123456 stored in integer32 in network byte order */
1327 CHECK( 0x12, buf[45] );
1328 CHECK( 0x34, buf[46] );
1329 CHECK( 0x56, buf[47] );
1330
1331 /* 48 ~ 55 : next AVP header */
1332 CHECK( 0xff, buf[56] ); /* -0x123456 stored in integer32 in network byte order. */
1333 CHECK( 0xed, buf[57] ); /* We assume that two's complement is the correct representation, although it's not clearly specified. */
1334 CHECK( 0xcb, buf[58] ); /* 00 12 34 56 inversed => FF ED CB A9 */
1335 CHECK( 0xaa, buf[59] ); /* then "+1" => FF ED CB AA */
1336
1337 /* 60 ~ 67 : next header */
1338 CHECK( 0x10, buf[67] ); /* (the size) */
1339 CHECK( 0x00, buf[68] ); /* 0x11223344556677 in network byte order */
1340 CHECK( 0x11, buf[69] );
1341 CHECK( 0x22, buf[70] );
1342 CHECK( 0x33, buf[71] );
1343 CHECK( 0x44, buf[72] );
1344 CHECK( 0x55, buf[73] );
1345 CHECK( 0x66, buf[74] );
1346 CHECK( 0x77, buf[75] );
1347
1348 /* 76 ~ 83 : next header */
1349 CHECK( 0xFF, buf[84] ); /* - 0x11223344556677 (in two's complement) */
1350 CHECK( 0xEE, buf[85] ); /* gives FF EE DD CC BB AA 99 89 */
1351 CHECK( 0xDD, buf[86] );
1352 CHECK( 0xCC, buf[87] );
1353 CHECK( 0xBB, buf[88] );
1354 CHECK( 0xAA, buf[89] );
1355 CHECK( 0x99, buf[90] );
1356 CHECK( 0x89, buf[91] );
1357
1358 /* 92 ~ 99 : next header */
1359 CHECK( 0x0c, buf[99] ); /* (the size) */
1360 CHECK( 0xFE, buf[100]); /* 0xFEDCBA98 in network byte order */
1361 CHECK( 0xDC, buf[101]);
1362 CHECK( 0xBA, buf[102]);
1363 CHECK( 0x98, buf[103]);
1364
1365 /* 104 ~ 111 : next header */
1366 CHECK( 0x10, buf[111] ); /* (the size) */
1367 CHECK( 0x12, buf[112]); /* 0x123456789abcdef0LL in network byte order */
1368 CHECK( 0x34, buf[113]);
1369 CHECK( 0x56, buf[114]);
1370 CHECK( 0x78, buf[115]);
1371 CHECK( 0x9a, buf[116]);
1372 CHECK( 0xbc, buf[117]);
1373 CHECK( 0xde, buf[118]);
1374 CHECK( 0xf0, buf[119]);
1375
1376 /* 120 ~ 127 : next header */
1377 CHECK( 0x0c, buf[127] ); /* (the size) */
1378 CHECK( 0x4a, buf[128]); /* http://en.wikipedia.org/wiki/IEEE_754-1985 to get descvription of the format */
1379 CHECK( 0x00, buf[129]); /* v = 2097153 = 2^21 + 2 ^ 0; sign : "+", 2^21 <= v < 2^22 => exponent = 21; biaised on 8 bits => 21 + 127 => 100 1010 0 */
1380 CHECK( 0x00, buf[130]); /* v = (+1) * (1 ^ 21) * ( 1 + 2^-21 ) => significand 000 0000 0000 0000 0000 0100 */
1381 CHECK( 0x04, buf[131]); /* result: 4a 00 00 04 */
1382
1383 /* 132 ~ 139 : next header */
1384 CHECK( 0x10, buf[139] ); /* (the size) */
1385 CHECK( 0xc2, buf[140]); /* -1099511627777L ( 2^40 + 1 ) in network byte order */
1386 CHECK( 0x70, buf[141]); /* sign: - => most significant bit = 1 */
1387 CHECK( 0x00, buf[142]); /* 2^40 <= v < 2^41 => biaised exponent on 11 bits: 1023 + 40: 100 0010 0111 */
1388 CHECK( 0x00, buf[143]); /* significand: 1 + 2^-40 => 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 */
1389 CHECK( 0x00, buf[144]); /* result: c2 70 00 00 00 00 10 00 */
1390 CHECK( 0x00, buf[145]);
1391 CHECK( 0x10, buf[146]);
1392 CHECK( 0x00, buf[147]);
1393
1394
1395
1396 /* Okay, now delete the message and parse the buffer, then check we obtain the same values back */
1397 #if 0
1398 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, msg, fd_g_config->cnf_dict, 0, 1));
1399 #endif
1400 CHECK( 0, fd_msg_free( msg ) );
1401
1402 CHECK( 0, fd_msg_parse_buffer( &buf, 148, &msg) );
1403 CHECK( 0, fd_msg_parse_dict( msg, fd_g_config->cnf_dict, NULL ) );
1404 #if 0
1405 fd_log_debug("%s", fd_msg_dump_treeview(FD_DUMP_TEST_PARAMS, msg, fd_g_config->cnf_dict, 0, 1));
1406 #endif
1407
1408 CHECK( 0, fd_msg_browse ( msg, MSG_BRW_FIRST_CHILD, &avp, NULL) );
1409 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1410 CHECK( 6, avpdata->avp_value->os.len );
1411 CHECK( 'w', (char)(avpdata->avp_value->os.data[0]) );
1412 CHECK( 'a', (char)(avpdata->avp_value->os.data[1]) );
1413 CHECK( 'd', (char)(avpdata->avp_value->os.data[4]) );
1414 CHECK( '\0', (char)(avpdata->avp_value->os.data[5]) );
1415
1416 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1417 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1418 CHECK( 0x123456, avpdata->avp_value->i32 );
1419
1420 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1421 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1422 CHECK( -0x123456, avpdata->avp_value->i32 );
1423
1424 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1425 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1426 CHECK( 0x11223344556677LL, avpdata->avp_value->i64 );
1427
1428 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1429 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1430 CHECK( -0x11223344556677LL, avpdata->avp_value->i64 );
1431
1432 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1433 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1434 CHECK( 0xFEDCBA98, avpdata->avp_value->u32 );
1435
1436 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1437 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1438 CHECK( 0x123456789abcdef0LL, avpdata->avp_value->u64 );
1439
1440 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1441 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1442 CHECK( 2097153.0F, avpdata->avp_value->f32 );
1443
1444 CHECK( 0, fd_msg_browse ( avp, MSG_BRW_NEXT, &avp, NULL) );
1445 CHECK( 0, fd_msg_avp_hdr ( avp, &avpdata ) );
1446 CHECK( -1099511627777LL, avpdata->avp_value->f64 );
1447
1448 CHECK( 0, fd_msg_free( msg ) );
1449 }
1450 }
1451
1452 /* That's all for the tests yet */
1453 PASSTEST();
1454}
1455