And the automerge property.
[asterisk/asterisk.git] / tests / test_aoc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * David Vossel <dvossel@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*!
20  * \file
21  * \brief Generic AOC encode decode tests
22  *
23  * \author David Vossel <dvossel@digium.com>
24  *
25  * \ingroup tests
26  */
27
28 /*** MODULEINFO
29         <depend>TEST_FRAMEWORK</depend>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/utils.h"
37 #include "asterisk/module.h"
38 #include "asterisk/test.h"
39 #include "asterisk/aoc.h"
40
41
42 AST_TEST_DEFINE(aoc_event_generation_test)
43 {
44         int res = AST_TEST_PASS;
45         struct ast_aoc_decoded *decoded = NULL;
46         struct ast_str *msg = NULL;
47
48         switch (cmd) {
49         case TEST_INIT:
50                 info->name = "aoc_event_test";
51                 info->category = "main/aoc/";
52                 info->summary = "Advice of Charge event generation test";
53                 info->description =
54                         "Creates AOC messages, verify event string matches expected results";
55                 return AST_TEST_NOT_RUN;
56         case TEST_EXECUTE:
57                 break;
58         }
59
60         if (!(msg = ast_str_create(1024))) {
61                 goto cleanup_aoc_event_test;
62         }
63
64         /* ---- TEST 1, AOC-D event generation */
65         if (!(decoded = ast_aoc_create(AST_AOC_D, AST_AOC_CHARGE_CURRENCY, 0))) {
66
67                 ast_test_status_update(test, "failed to create AOC-D message for event generation.\n");
68                 res = AST_TEST_FAIL;
69                 goto cleanup_aoc_event_test;
70         }
71         /* Add billing id information */
72         ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_CREDIT_CARD);
73
74         /* Set currency information, verify results */
75         if ((ast_aoc_set_currency_info(decoded, 100, AST_AOC_MULT_ONE, "usd")) ||
76                 (ast_aoc_set_total_type(decoded, AST_AOC_SUBTOTAL))) {
77
78                 ast_test_status_update(test, "failed to set currency info in AOC-D msg\n");
79                 res = AST_TEST_FAIL;
80                 goto cleanup_aoc_event_test;
81         }
82         if (ast_aoc_decoded2str(decoded, &msg)) {
83
84                 ast_test_status_update(test, "failed to generate AOC-D msg string.\n");
85                 res = AST_TEST_FAIL;
86                 goto cleanup_aoc_event_test;
87         }
88         if (strncmp(ast_str_buffer(msg),
89                 "AOC-D\r\n"
90                 "Type: Currency\r\n"
91                 "BillingID: CreditCard\r\n"
92                 "TypeOfCharging: SubTotal\r\n"
93                 "Currency: usd\r\n"
94                 "Currency/Amount/Cost: 100\r\n"
95                 "Currency/Amount/Multiplier: 1\r\n",
96                 strlen(ast_str_buffer(msg)))) {
97
98                 ast_test_status_update(test, "AOC-D msg event did not match expected results\n");
99                 res = AST_TEST_FAIL;
100                 goto cleanup_aoc_event_test;
101         }
102         decoded = ast_aoc_destroy_decoded(decoded);
103         ast_str_reset(msg);
104
105
106         /* ---- TEST 2, AOC-S event generation */
107         if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
108                 ast_test_status_update(test, "failed to create AOC-S message for event generation.\n");
109
110                 res = AST_TEST_FAIL;
111                 goto cleanup_aoc_event_test;
112         }
113
114         ast_aoc_s_add_rate_flat(decoded,
115                 AST_AOC_CHARGED_ITEM_BASIC_COMMUNICATION,
116                 123,
117                 AST_AOC_MULT_TEN,
118                 "pineapple");
119
120         ast_aoc_s_add_rate_volume(decoded,
121                 AST_AOC_CHARGED_ITEM_CALL_ATTEMPT,
122                 AST_AOC_VOLUME_UNIT_SEGMENT,
123                 937,
124                 AST_AOC_MULT_ONEHUNDREDTH,
125                 "oranges");
126
127         ast_aoc_s_add_rate_duration(decoded,
128                 AST_AOC_CHARGED_ITEM_CALL_ATTEMPT,
129                 937,
130                 AST_AOC_MULT_ONETHOUSANDTH,
131                 "bananas",
132                 848,
133                 AST_AOC_TIME_SCALE_TENTH_SECOND,
134                 949,
135                 AST_AOC_TIME_SCALE_HOUR,
136                 1);
137
138         ast_aoc_s_add_rate_duration(decoded,
139                 AST_AOC_CHARGED_ITEM_USER_USER_INFO,
140                 937,
141                 AST_AOC_MULT_THOUSAND,
142                 "bananas",
143                 1111,
144                 AST_AOC_TIME_SCALE_SECOND,
145                 2222,
146                 AST_AOC_TIME_SCALE_DAY,
147                 0);
148
149         if (ast_aoc_decoded2str(decoded, &msg)) {
150
151                 ast_test_status_update(test, "failed to generate AOC-D msg string.\n");
152                 res = AST_TEST_FAIL;
153                 goto cleanup_aoc_event_test;
154         }
155
156
157         if (strncmp(ast_str_buffer(msg),
158                 "AOC-S\r\n"
159                 "NumberRates: 4\r\n"
160                 "Rate(0)/Chargeable: BasicCommunication\r\n"
161                 "Rate(0)/Type: Flat\r\n"
162                 "Rate(0)/Flat/Currency: pineapple\r\n"
163                 "Rate(0)/Flat/Amount/Cost: 123\r\n"
164                 "Rate(0)/Flat/Amount/Multiplier: 10\r\n"
165                 "Rate(1)/Chargeable: CallAttempt\r\n"
166                 "Rate(1)/Type: Volume\r\n"
167                 "Rate(1)/Volume/Currency: oranges\r\n"
168                 "Rate(1)/Volume/Amount/Cost: 937\r\n"
169                 "Rate(1)/Volume/Amount/Multiplier: 1/100\r\n"
170                 "Rate(1)/Volume/Unit: Segment\r\n"
171                 "Rate(2)/Chargeable: CallAttempt\r\n"
172                 "Rate(2)/Type: Duration\r\n"
173                 "Rate(2)/Duration/Currency: bananas\r\n"
174                 "Rate(2)/Duration/Amount/Cost: 937\r\n"
175                 "Rate(2)/Duration/Amount/Multiplier: 1/1000\r\n"
176                 "Rate(2)/Duration/ChargingType: StepFunction\r\n"
177                 "Rate(2)/Duration/Time/Length: 848\r\n"
178                 "Rate(2)/Duration/Time/Scale: OneTenthSecond\r\n"
179                 "Rate(2)/Duration/Granularity/Length: 949\r\n"
180                 "Rate(2)/Duration/Granularity/Scale: Hour\r\n"
181                 "Rate(3)/Chargeable: UserUserInfo\r\n"
182                 "Rate(3)/Type: Duration\r\n"
183                 "Rate(3)/Duration/Currency: bananas\r\n"
184                 "Rate(3)/Duration/Amount/Cost: 937\r\n"
185                 "Rate(3)/Duration/Amount/Multiplier: 1000\r\n"
186                 "Rate(3)/Duration/ChargingType: ContinuousCharging\r\n"
187                 "Rate(3)/Duration/Time/Length: 1111\r\n"
188                 "Rate(3)/Duration/Time/Scale: Second\r\n"
189                 "Rate(3)/Duration/Granularity/Length: 2222\r\n"
190                 "Rate(3)/Duration/Granularity/Scale: Day\r\n",
191                 strlen(ast_str_buffer(msg)))) {
192
193                 ast_test_status_update(test, "AOC-S msg event did not match expected results\n");
194                 res = AST_TEST_FAIL;
195                 goto cleanup_aoc_event_test;
196         }
197         decoded = ast_aoc_destroy_decoded(decoded);
198         ast_str_reset(msg);
199
200         /* ---- TEST 3, AOC-E event generation with various charging association information*/
201         if (!(decoded = ast_aoc_create(AST_AOC_E, AST_AOC_CHARGE_UNIT, 0))) {
202                 ast_test_status_update(test, "failed to create AOC-E message for event generation.\n");
203
204                 res = AST_TEST_FAIL;
205                 goto cleanup_aoc_event_test;
206         }
207         if ((ast_aoc_add_unit_entry(decoded, 1, 111, 1, 1)) ||
208                 (!ast_aoc_add_unit_entry(decoded, 0, 2222, 0, 2)) || /* we expect this to fail, and it should not be added to entry list */
209                 (ast_aoc_add_unit_entry(decoded, 1, 3333, 0, 3)) ||
210                 (ast_aoc_add_unit_entry(decoded, 0, 44444, 1, 4))) {
211
212                 ast_test_status_update(test, "failed to set unit info for AOC-E message\n");
213                 res = AST_TEST_FAIL;
214                 goto cleanup_aoc_event_test;
215         }
216
217         if (ast_aoc_decoded2str(decoded, &msg)) {
218                 ast_test_status_update(test, "failed to generate AOC-E msg string.\n");
219                 res = AST_TEST_FAIL;
220                 goto cleanup_aoc_event_test;
221         }
222
223         if (strncmp(ast_str_buffer(msg),
224                 "AOC-E\r\n"
225                 "Type: Units\r\n"
226                 "BillingID: NotAvailable\r\n"
227                 "Units/NumberItems: 3\r\n"
228                 "Units/Item(0)/NumberOf: 111\r\n"
229                 "Units/Item(0)/TypeOf: 1\r\n"
230                 "Units/Item(1)/NumberOf: 3333\r\n"
231                 "Units/Item(2)/TypeOf: 4\r\n",
232                 strlen(ast_str_buffer(msg)))) {
233
234                 ast_test_status_update(test, "AOC-E msg event did not match expected results, with no charging association info\n");
235                 res = AST_TEST_FAIL;
236                 goto cleanup_aoc_event_test;
237         }
238
239         /* add AOC-E charging association number info */
240         if (ast_aoc_set_association_number(decoded, "555-555-5555", 16)) {
241                         ast_test_status_update(test, "failed to set the charging association number info correctly, 3\n");
242                         res = AST_TEST_FAIL;
243                         goto cleanup_aoc_event_test;
244         }
245
246         ast_str_reset(msg);
247         if (ast_aoc_decoded2str(decoded, &msg)) {
248                 ast_test_status_update(test, "failed to generate AOC-E msg string.\n");
249                 res = AST_TEST_FAIL;
250                 goto cleanup_aoc_event_test;
251         }
252
253         if (strncmp(ast_str_buffer(msg),
254                 "AOC-E\r\n"
255                 "ChargingAssociation/Number: 555-555-5555\r\n"
256                 "ChargingAssociation/Number/Plan: 16\r\n"
257                 "Type: Units\r\n"
258                 "BillingID: NotAvailable\r\n"
259                 "Units/NumberItems: 3\r\n"
260                 "Units/Item(0)/NumberOf: 111\r\n"
261                 "Units/Item(0)/TypeOf: 1\r\n"
262                 "Units/Item(1)/NumberOf: 3333\r\n"
263                 "Units/Item(2)/TypeOf: 4\r\n",
264                 strlen(ast_str_buffer(msg)))) {
265
266                 ast_test_status_update(test, "AOC-E msg event did not match expected results, with charging association number\n");
267                 res = AST_TEST_FAIL;
268                 goto cleanup_aoc_event_test;
269         }
270
271         /* add AOC-E charging association id info */
272         if (ast_aoc_set_association_id(decoded, 2222)) {
273                         ast_test_status_update(test, "failed to set the charging association number info correctly, 3\n");
274                         res = AST_TEST_FAIL;
275                         goto cleanup_aoc_event_test;
276         }
277
278         ast_str_reset(msg);
279         if (ast_aoc_decoded2str(decoded, &msg)) {
280                 ast_test_status_update(test, "failed to generate AOC-E msg string.\n");
281                 res = AST_TEST_FAIL;
282                 goto cleanup_aoc_event_test;
283         }
284
285         if (strncmp(ast_str_buffer(msg),
286                 "AOC-E\r\n"
287                 "ChargingAssociation/ID: 2222\r\n"
288                 "Type: Units\r\n"
289                 "BillingID: NotAvailable\r\n"
290                 "Units/NumberItems: 3\r\n"
291                 "Units/Item(0)/NumberOf: 111\r\n"
292                 "Units/Item(0)/TypeOf: 1\r\n"
293                 "Units/Item(1)/NumberOf: 3333\r\n"
294                 "Units/Item(2)/TypeOf: 4\r\n",
295                 strlen(ast_str_buffer(msg)))) {
296
297                 ast_test_status_update(test, "AOC-E msg event did not match expected results with charging association id.\n");
298                 res = AST_TEST_FAIL;
299                 goto cleanup_aoc_event_test;
300         }
301
302
303 cleanup_aoc_event_test:
304
305         decoded = ast_aoc_destroy_decoded(decoded);
306         ast_free(msg);
307         return res;
308 }
309
310 AST_TEST_DEFINE(aoc_encode_decode_test)
311 {
312         int res = AST_TEST_PASS;
313         struct ast_aoc_decoded *decoded;
314
315         switch (cmd) {
316         case TEST_INIT:
317                 info->name = "aoc_encode_decode_test";
318                 info->category = "main/aoc/";
319                 info->summary = "Advice of Charge encode and decode test";
320                 info->description =
321                         "This tests the Advice of Charge encode and decode routines.";
322                 return AST_TEST_NOT_RUN;
323         case TEST_EXECUTE:
324                 break;
325         }
326
327         /* ---- Test 1 ---- create AOC-D message, encode message, and decode message once again. */
328         /* create AOC-D message */
329         if (!(decoded = ast_aoc_create(AST_AOC_D, AST_AOC_CHARGE_CURRENCY, 0)) ||
330                 (ast_aoc_get_msg_type(decoded) != AST_AOC_D) ||
331                 (ast_aoc_get_charge_type(decoded) != AST_AOC_CHARGE_CURRENCY)) {
332
333                 ast_test_status_update(test, "Test 1: failed to create AOC-D message\n");
334                 res = AST_TEST_FAIL;
335                 goto cleanup_aoc_test;
336         }
337
338         /* Add billing id information */
339         if ((ast_aoc_set_billing_id(decoded, AST_AOC_BILLING_NORMAL) ||
340                 (ast_aoc_get_billing_id(decoded) != AST_AOC_BILLING_NORMAL))) {
341
342                 ast_test_status_update(test, "TEST 1, could not set billing id correctly\n");
343                 res = AST_TEST_FAIL;
344                 goto cleanup_aoc_test;
345
346         }
347
348         /* Set currency information, verify results*/
349         if ((ast_aoc_set_currency_info(decoded, 100, AST_AOC_MULT_ONE, "usd")) ||
350                 (ast_aoc_set_total_type(decoded, AST_AOC_SUBTOTAL)) ||
351                 (ast_aoc_get_total_type(decoded) != AST_AOC_SUBTOTAL) ||
352                 (ast_aoc_get_currency_amount(decoded) != 100) ||
353                 (ast_aoc_get_currency_multiplier(decoded) != AST_AOC_MULT_ONE) ||
354                 (strcmp(ast_aoc_get_currency_name(decoded), "usd"))) {
355
356                 ast_test_status_update(test, "Test 1: failed to set currency info\n");
357                 res = AST_TEST_FAIL;
358                 goto cleanup_aoc_test;
359         }
360
361         /* Set a currency name larger than 10 characters which is the the maximum
362          * length allowed by the ETSI aoc standard.  The name is expected to truncate
363          * to 10 characters. */
364         if ((ast_aoc_set_currency_info(decoded, 100, AST_AOC_MULT_ONE, "12345678901234567890")) ||
365                 (ast_aoc_get_currency_amount(decoded) != 100) ||
366                 (ast_aoc_get_currency_multiplier(decoded) != AST_AOC_MULT_ONE) ||
367                 (strcmp(ast_aoc_get_currency_name(decoded), "1234567890"))) {
368
369                 ast_test_status_update(test, "Test 1: failed to set currency info currency name exceeding limit\n");
370                 res = AST_TEST_FAIL;
371                 goto cleanup_aoc_test;
372         }
373
374         /* Encode the message */
375         if (ast_aoc_test_encode_decode_match(decoded)) {
376                 ast_test_status_update(test, "Test1: encode decode routine did not match expected results \n");
377                 res = AST_TEST_FAIL;
378                 goto cleanup_aoc_test;
379         }
380         /* cleanup decoded msg */
381         decoded = ast_aoc_destroy_decoded(decoded);
382
383         /* ---- Test 2 ---- create AOC-E message with charge type == unit */
384         /* create AOC-E message */
385         if (!(decoded = ast_aoc_create(AST_AOC_E, AST_AOC_CHARGE_UNIT, 0)) ||
386                 (ast_aoc_get_msg_type(decoded) != AST_AOC_E) ||
387                 (ast_aoc_get_charge_type(decoded) != AST_AOC_CHARGE_UNIT)) {
388
389                 ast_test_status_update(test, "Test 2: failed to create AOC-E message\n");
390                 res = AST_TEST_FAIL;
391                 goto cleanup_aoc_test;
392         }
393
394         /* Set unit information, verify results*/
395         if ((ast_aoc_add_unit_entry(decoded, 1, 1, 1, 2)) ||
396                 (!ast_aoc_add_unit_entry(decoded, 0, 123, 0, 123)) || /* this entry should fail since either number nor type are present */
397                 (ast_aoc_add_unit_entry(decoded, 0, 2, 1, 3)) ||
398                 (ast_aoc_add_unit_entry(decoded, 1, 3, 0, 4))) {
399
400                 ast_test_status_update(test, "Test 2: failed to set unit info\n");
401                 res = AST_TEST_FAIL;
402                 goto cleanup_aoc_test;
403         }
404
405         /* verify unit list is correct */
406         if (ast_aoc_get_unit_count(decoded) == 3) {
407                 int i;
408                 const struct ast_aoc_unit_entry *unit;
409                 for (i = 0; i < 3; i++) {
410                         if (!(unit = ast_aoc_get_unit_info(decoded, i)) ||
411                                 ((unit->valid_amount) && (unit->amount != (i+1))) ||
412                                 ((unit->valid_type) && (unit->type != (i+2)))) {
413                                 ast_test_status_update(test, "TEST 2, invalid unit entry result, got %d,%d, expected %d,%d\n",
414                                         unit->amount,
415                                         unit->type,
416                                         i+1,
417                                         i+2);
418                                 res = AST_TEST_FAIL;
419                                 goto cleanup_aoc_test;
420                         }
421                 }
422         } else {
423                 ast_test_status_update(test, "TEST 2, invalid unit list entry count \n");
424                 res = AST_TEST_FAIL;
425                 goto cleanup_aoc_test;
426         }
427
428
429         /* Test charging association information */
430         {
431                 const struct ast_aoc_charging_association *ca;
432                 if ((ast_aoc_set_association_id(decoded, 1234)) ||
433                    (!(ca = ast_aoc_get_association_info(decoded)))) {
434                         ast_test_status_update(test, "TEST 2, could not set charging association id info correctly\n");
435                         res = AST_TEST_FAIL;
436                         goto cleanup_aoc_test;
437                 }
438
439                 if ((ca->charging_type != AST_AOC_CHARGING_ASSOCIATION_ID) || (ca->charge.id != 1234)) {
440                         ast_test_status_update(test, "TEST 2, could not get charging association id info correctly, 2\n");
441                 }
442
443                 if ((ast_aoc_set_association_number(decoded, "1234", 16)) ||
444                    (!(ca = ast_aoc_get_association_info(decoded)))) {
445                         ast_test_status_update(test, "TEST 2, could not set charging association number info correctly, 3\n");
446                         res = AST_TEST_FAIL;
447                         goto cleanup_aoc_test;
448                 }
449
450                 if ((ca->charging_type != AST_AOC_CHARGING_ASSOCIATION_NUMBER) ||
451                         (ca->charge.number.plan != 16) ||
452                         (strcmp(ca->charge.number.number, "1234"))) {
453                         ast_test_status_update(test, "TEST 2, could not get charging association number info correctly\n");
454                 }
455         }
456
457         /* Test every billing id possiblity */
458         {
459                 int billid[9] = {
460                         AST_AOC_BILLING_NA,
461                         AST_AOC_BILLING_NORMAL,
462                         AST_AOC_BILLING_REVERSE_CHARGE,
463                         AST_AOC_BILLING_CREDIT_CARD,
464                         AST_AOC_BILLING_CALL_FWD_UNCONDITIONAL,
465                         AST_AOC_BILLING_CALL_FWD_BUSY,
466                         AST_AOC_BILLING_CALL_FWD_NO_REPLY,
467                         AST_AOC_BILLING_CALL_DEFLECTION,
468                         AST_AOC_BILLING_CALL_TRANSFER,
469                 };
470                 int i;
471
472                 /* these should fail */
473                 if (!(ast_aoc_set_billing_id(decoded, (AST_AOC_BILLING_NA - 1))) ||
474                         !(ast_aoc_set_billing_id(decoded, (AST_AOC_BILLING_CALL_TRANSFER + 1)))) {
475
476                                 ast_test_status_update(test, "TEST 2, setting invalid billing id should fail\n");
477                                 res = AST_TEST_FAIL;
478                                 goto cleanup_aoc_test;
479                 }
480
481                 for (i = 0; i < ARRAY_LEN(billid); i++) {
482                         if ((ast_aoc_set_billing_id(decoded, billid[i]) ||
483                                 (ast_aoc_get_billing_id(decoded) != billid[i]))) {
484
485                                 ast_test_status_update(test, "TEST 2, could not set billing id correctly, iteration #%d\n", i);
486                                 res = AST_TEST_FAIL;
487                                 goto cleanup_aoc_test;
488                         }
489                 }
490         }
491         /* Encode the message */
492         if (ast_aoc_test_encode_decode_match(decoded)) {
493                 ast_test_status_update(test, "Test2: encode decode routine did not match expected results \n");
494                 res = AST_TEST_FAIL;
495                 goto cleanup_aoc_test;
496         }
497         /* cleanup decoded msg */
498         decoded = ast_aoc_destroy_decoded(decoded);
499
500         /* ---- Test 3 ---- create AOC-Request. test all possible combinations */
501         {
502                 int request[7] = { /* all possible request combinations */
503                         AST_AOC_REQUEST_S,
504                         AST_AOC_REQUEST_D,
505                         AST_AOC_REQUEST_E,
506                         (AST_AOC_REQUEST_S | AST_AOC_REQUEST_D),
507                         (AST_AOC_REQUEST_S | AST_AOC_REQUEST_E),
508                         (AST_AOC_REQUEST_D | AST_AOC_REQUEST_E),
509                         (AST_AOC_REQUEST_D | AST_AOC_REQUEST_E | AST_AOC_REQUEST_S)
510                 };
511                 int i;
512
513                 for (i = 0; i < ARRAY_LEN(request); i++) {
514                         if (!(decoded = ast_aoc_create(AST_AOC_REQUEST, 0, request[i])) ||
515                                 (ast_aoc_get_msg_type(decoded) != AST_AOC_REQUEST) ||
516                                 (ast_aoc_get_termination_request(decoded)) ||
517                                 (ast_aoc_get_request(decoded) != request[i])) {
518
519                                 ast_test_status_update(test, "Test 3: failed to create AOC-Request message, iteration #%d\n", i);
520                                 res = AST_TEST_FAIL;
521                                 goto cleanup_aoc_test;
522                         }
523
524                         /* Encode the message */
525                         if (ast_aoc_test_encode_decode_match(decoded)) {
526                                 ast_test_status_update(test, "Test3: encode decode routine did not match expected results, iteration #%d\n", i);
527                                 res = AST_TEST_FAIL;
528                                 goto cleanup_aoc_test;
529                         }
530                         /* cleanup decoded msg */
531                         decoded = ast_aoc_destroy_decoded(decoded);
532                 }
533
534
535                 /* Test termination Request Type */
536                 if (!(decoded = ast_aoc_create(AST_AOC_REQUEST, 0, AST_AOC_REQUEST_E)) ||
537                         (ast_aoc_set_termination_request(decoded)) ||
538                         (!ast_aoc_get_termination_request(decoded)) ||
539                         (ast_aoc_get_msg_type(decoded) != AST_AOC_REQUEST) ||
540                         (ast_aoc_get_request(decoded) != AST_AOC_REQUEST_E)) {
541
542                         ast_test_status_update(test, "Test 3: failed to create AOC-Request message with Termination Request set\n");
543                         res = AST_TEST_FAIL;
544                         goto cleanup_aoc_test;
545                 }
546
547                 /* Encode the message */
548                 if (ast_aoc_test_encode_decode_match(decoded)) {
549                         ast_test_status_update(test, "Test3: encode decode routine did not match expected results with termination request set\n");
550                         res = AST_TEST_FAIL;
551                         goto cleanup_aoc_test;
552                 }
553                 /* cleanup decoded msg */
554                 decoded = ast_aoc_destroy_decoded(decoded);
555         }
556
557         /* ---- Test 4 ----  Make stuff blow up */
558         if ((decoded = ast_aoc_create(AST_AOC_D, 1234567, 0))) {
559
560                 ast_test_status_update(test, "Test 4: aoc-d creation with no valid charge type should fail\n");
561                 res = AST_TEST_FAIL;
562                 goto cleanup_aoc_test;
563         }
564         if ((decoded = ast_aoc_create(AST_AOC_REQUEST, 0, 0))) {
565
566                 ast_test_status_update(test, "Test 4: aoc request creation with no data should have failed\n");
567                 res = AST_TEST_FAIL;
568                 goto cleanup_aoc_test;
569         }
570         if ((decoded = ast_aoc_create(AST_AOC_REQUEST, -12345678, -23456789))) {
571
572                 ast_test_status_update(test, "Test 4: aoc request creation with random data should have failed\n");
573                 res = AST_TEST_FAIL;
574                 goto cleanup_aoc_test;
575         }
576
577         /* ---- Test 5 ---- create AOC-E message with charge type == FREE and charge type == NA */
578         /* create AOC-E message */
579         if (!(decoded = ast_aoc_create(AST_AOC_E, AST_AOC_CHARGE_FREE, 0)) ||
580                 (ast_aoc_get_msg_type(decoded) != AST_AOC_E) ||
581                 (ast_aoc_get_charge_type(decoded) != AST_AOC_CHARGE_FREE)) {
582
583                 ast_test_status_update(test, "Test 5: failed to create AOC-E message, charge type Free\n");
584                 res = AST_TEST_FAIL;
585                 goto cleanup_aoc_test;
586         }
587         if (ast_aoc_test_encode_decode_match(decoded)) {
588                 ast_test_status_update(test, "Test5: encode decode routine did not match expected results, charge type Free\n");
589                 res = AST_TEST_FAIL;
590                 goto cleanup_aoc_test;
591         }
592         /* cleanup decoded msg */
593         decoded = ast_aoc_destroy_decoded(decoded);
594
595         /* create AOC-E message */
596         if (!(decoded = ast_aoc_create(AST_AOC_E, AST_AOC_CHARGE_NA, 0)) ||
597                 (ast_aoc_get_msg_type(decoded) != AST_AOC_E) ||
598                 (ast_aoc_get_charge_type(decoded) != AST_AOC_CHARGE_NA)) {
599
600                 ast_test_status_update(test, "Test 5: failed to create AOC-E message, charge type NA\n");
601                 res = AST_TEST_FAIL;
602                 goto cleanup_aoc_test;
603         }
604         if (ast_aoc_test_encode_decode_match(decoded)) {
605                 ast_test_status_update(test, "Test5: encode decode routine did not match expected results, charge type NA.\n");
606                 res = AST_TEST_FAIL;
607                 goto cleanup_aoc_test;
608         }
609         /* cleanup decoded msg */
610         decoded = ast_aoc_destroy_decoded(decoded);
611
612
613 /* ---- TEST 6, AOC-S encode decode */
614         if (!(decoded = ast_aoc_create(AST_AOC_S, 0, 0))) {
615                 ast_test_status_update(test, "failed to create AOC-S message for encode decode testing.\n");
616
617                 res = AST_TEST_FAIL;
618                 goto cleanup_aoc_test;
619         }
620
621         ast_aoc_s_add_rate_duration(decoded,
622                 AST_AOC_CHARGED_ITEM_SUPPLEMENTARY_SERVICE,
623                 937,
624                 AST_AOC_MULT_THOUSAND,
625                 "jkasdf",
626                 235328,
627                 AST_AOC_TIME_SCALE_SECOND,
628                 905423,
629                 AST_AOC_TIME_SCALE_DAY,
630                 1);
631
632         ast_aoc_s_add_rate_flat(decoded,
633                 AST_AOC_CHARGED_ITEM_CALL_SETUP,
634                 1337,
635                 AST_AOC_MULT_ONEHUNDREDTH,
636                 "MONEYS");
637
638         ast_aoc_s_add_rate_volume(decoded,
639                 AST_AOC_CHARGED_ITEM_CALL_ATTEMPT,
640                 AST_AOC_VOLUME_UNIT_SEGMENT,
641                 5555,
642                 AST_AOC_MULT_ONEHUNDREDTH,
643                 "pounds");
644
645         ast_aoc_s_add_rate_duration(decoded,
646                 AST_AOC_CHARGED_ITEM_CALL_ATTEMPT,
647                 78923,
648                 AST_AOC_MULT_ONETHOUSANDTH,
649                 "SNAP",
650                 9354,
651                 AST_AOC_TIME_SCALE_HUNDREDTH_SECOND,
652                 234933,
653                 AST_AOC_TIME_SCALE_SECOND,
654                 0);
655
656         ast_aoc_s_add_rate_free(decoded, AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT, 1);
657         ast_aoc_s_add_rate_free(decoded, AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT, 0);
658         ast_aoc_s_add_rate_na(decoded, AST_AOC_CHARGED_ITEM_SPECIAL_ARRANGEMENT);
659
660         if (ast_aoc_test_encode_decode_match(decoded)) {
661                 ast_test_status_update(test, "Test6: encode decode routine for AOC-S did not match expected results\n");
662                 res = AST_TEST_FAIL;
663                 goto cleanup_aoc_test;
664         }
665         /* cleanup decoded msg */
666         decoded = ast_aoc_destroy_decoded(decoded);
667
668
669
670 cleanup_aoc_test:
671
672         decoded = ast_aoc_destroy_decoded(decoded);
673         return res;
674 }
675
676 static int unload_module(void)
677 {
678         AST_TEST_UNREGISTER(aoc_encode_decode_test);
679         AST_TEST_UNREGISTER(aoc_event_generation_test);
680         return 0;
681 }
682
683 static int load_module(void)
684 {
685         AST_TEST_REGISTER(aoc_encode_decode_test);
686         AST_TEST_REGISTER(aoc_event_generation_test);
687         return AST_MODULE_LOAD_SUCCESS;
688 }
689
690 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AOC unit tests");