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