test_aoc.c 21 KB

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