parking_tests.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@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. /*! \file
  19. *
  20. * \brief Call Parking Unit Tests
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. */
  24. #include "asterisk.h"
  25. #include "res_parking.h"
  26. #include "asterisk/utils.h"
  27. #include "asterisk/module.h"
  28. #include "asterisk/astobj2.h"
  29. #include "asterisk/test.h"
  30. #include "asterisk/stringfields.h"
  31. #include "asterisk/time.h"
  32. #include "asterisk/causes.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/format_cache.h"
  35. #if defined(TEST_FRAMEWORK)
  36. #define TEST_CATEGORY "/res/parking/"
  37. #define CHANNEL_TECH_NAME "ParkingTestChannel"
  38. static const struct ast_party_caller alice_callerid = {
  39. .id.name.str = "Alice",
  40. .id.name.valid = 1,
  41. .id.number.str = "100",
  42. .id.number.valid = 1,
  43. };
  44. static int parking_test_write(struct ast_channel *chan, struct ast_frame *frame)
  45. {
  46. return 0;
  47. }
  48. static struct ast_frame *parking_test_read(struct ast_channel *chan)
  49. {
  50. return &ast_null_frame;
  51. }
  52. static const struct ast_channel_tech parking_test_tech = {
  53. .type = CHANNEL_TECH_NAME,
  54. .description = "Parking unit test technology",
  55. .write = parking_test_write,
  56. .read = parking_test_read,
  57. };
  58. /*! \brief Set ulaw format on the channel */
  59. static int set_test_formats(struct ast_channel *chan)
  60. {
  61. struct ast_format_cap *caps;
  62. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  63. if (!caps) {
  64. return -1;
  65. }
  66. ast_format_cap_append(caps, ast_format_ulaw, 0);
  67. ast_channel_nativeformats_set(chan, caps);
  68. ast_channel_set_writeformat(chan, ast_format_ulaw);
  69. ast_channel_set_rawwriteformat(chan, ast_format_ulaw);
  70. ast_channel_set_readformat(chan, ast_format_ulaw);
  71. ast_channel_set_rawreadformat(chan, ast_format_ulaw);
  72. ao2_ref(caps, -1);
  73. return 0;
  74. }
  75. /*! \brief Create a \ref test_cdr_chan_tech for Alice */
  76. static struct ast_channel *create_alice_channel(void)
  77. {
  78. struct ast_channel *alice = ast_channel_alloc(0, AST_STATE_DOWN,
  79. "100", "Alice", "100", "100", "default", NULL, NULL, 0,
  80. CHANNEL_TECH_NAME "/Alice");
  81. if (!alice) {
  82. return NULL;
  83. }
  84. if (set_test_formats(alice)) {
  85. ast_channel_unlock(alice);
  86. ast_channel_release(alice);
  87. return NULL;
  88. }
  89. ast_channel_tech_set(alice, &parking_test_tech);
  90. ast_channel_set_caller(alice, &alice_callerid, NULL);
  91. ast_channel_unlock(alice);
  92. return alice;
  93. }
  94. /*! \brief Hang up a test channel safely */
  95. static struct ast_channel *hangup_channel(struct ast_channel *chan, int hangup_cause)
  96. {
  97. ast_channel_hangupcause_set(chan, hangup_cause);
  98. ast_hangup(chan);
  99. return NULL;
  100. }
  101. static void safe_channel_release(struct ast_channel *chan)
  102. {
  103. if (!chan) {
  104. return;
  105. }
  106. ast_channel_release(chan);
  107. }
  108. static void do_sleep(struct timespec *to_sleep)
  109. {
  110. while ((nanosleep(to_sleep, to_sleep) == -1) && (errno == EINTR)) {
  111. }
  112. }
  113. #define TEST_LOT_NAME "unit_tests_res_parking_test_lot"
  114. static struct parking_lot *generate_test_parking_lot(const char *name, int low_space, int high_space, const char *park_exten, const char *park_context, struct ast_test *test)
  115. {
  116. RAII_VAR(struct parking_lot_cfg *, test_cfg, NULL, ao2_cleanup);
  117. struct parking_lot *test_lot;
  118. test_cfg = parking_lot_cfg_create(name);
  119. if (!test_cfg) {
  120. return NULL;
  121. }
  122. test_cfg->parking_start = low_space;
  123. test_cfg->parking_stop = high_space;
  124. test_cfg->parkingtime = 10;
  125. test_cfg->comebackdialtime = 10;
  126. test_cfg->parkfindnext = 1;
  127. test_cfg->parkext_exclusive = 1;
  128. ast_string_field_set(test_cfg, parkext, park_exten);
  129. ast_string_field_set(test_cfg, parking_con, park_context);
  130. ast_string_field_set(test_cfg, comebackcontext, "unit_test_res_parking_create_lot_comeback");
  131. if (parking_lot_cfg_create_extensions(test_cfg)) {
  132. ast_test_status_update(test, "Extensions for parking lot '%s' could not be registered. Extension Creation failed.\n", name);
  133. return NULL;
  134. }
  135. test_lot = parking_lot_build_or_update(test_cfg, 1);
  136. if (!test_lot) {
  137. return NULL;
  138. }
  139. return test_lot;
  140. }
  141. static int dispose_test_lot(struct parking_lot *test_lot, int expect_destruction)
  142. {
  143. RAII_VAR(struct parking_lot *, found_lot, NULL, ao2_cleanup);
  144. test_lot->mode = PARKINGLOT_DISABLED;
  145. parking_lot_remove_if_unused(test_lot);
  146. found_lot = parking_lot_find_by_name(test_lot->name);
  147. if ((expect_destruction && !found_lot) || (!expect_destruction && found_lot)) {
  148. return 0;
  149. }
  150. return -1;
  151. }
  152. AST_TEST_DEFINE(create_lot)
  153. {
  154. RAII_VAR(struct parking_lot *, test_lot, NULL, ao2_cleanup);
  155. RAII_VAR(struct parking_lot *, found_copy, NULL, ao2_cleanup);
  156. switch (cmd) {
  157. case TEST_INIT:
  158. info->name = "create_lot";
  159. info->category = TEST_CATEGORY;
  160. info->summary = "Parking lot creation";
  161. info->description =
  162. "Creates a parking lot and then disposes of it.";
  163. return AST_TEST_NOT_RUN;
  164. case TEST_EXECUTE:
  165. break;
  166. }
  167. ast_test_status_update(test, "Creating test parking lot '%s'\n", TEST_LOT_NAME);
  168. test_lot = generate_test_parking_lot(TEST_LOT_NAME, 701, 703, NULL, "unit_test_res_parking_create_lot_con", test);
  169. if (!test_lot) {
  170. ast_test_status_update(test, "Failed to create test parking lot. Test Failed\n");
  171. return AST_TEST_FAIL;
  172. }
  173. ast_test_status_update(test, "Successfully created parking lot. Retrieving test parking lot from container.\n");
  174. found_copy = parking_lot_find_by_name(TEST_LOT_NAME);
  175. if (!found_copy) {
  176. ast_test_status_update(test, "Failed to find parking lot in the parking lot container. Test failed.\n");
  177. dispose_test_lot(test_lot, 1);
  178. return AST_TEST_FAIL;
  179. }
  180. ast_test_status_update(test, "Successfully retrieved parking lot. Removing test parking lot from container.\n");
  181. if (dispose_test_lot(found_copy, 1)) {
  182. ast_test_status_update(test, "Found parking lot in container after attempted removal. Test failed.\n");
  183. }
  184. ast_test_status_update(test, "Parking lot was successfully removed from the container. Test complete.\n");
  185. return AST_TEST_PASS;
  186. }
  187. AST_TEST_DEFINE(park_call)
  188. {
  189. RAII_VAR(struct parking_lot *, test_lot, NULL, ao2_cleanup);
  190. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  191. RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup);
  192. struct timespec to_sleep = {1, 0};
  193. switch (cmd) {
  194. case TEST_INIT:
  195. info->name = "park_channel";
  196. info->category = TEST_CATEGORY;
  197. info->summary = "Park a Channel";
  198. info->description =
  199. "Creates a parking lot, parks a channel in it, then removes it from the parking lot bridge.";
  200. return AST_TEST_NOT_RUN;
  201. case TEST_EXECUTE:
  202. break;
  203. }
  204. ast_test_status_update(test, "Creating test parking lot '%s'\n", TEST_LOT_NAME);
  205. test_lot = generate_test_parking_lot(TEST_LOT_NAME, 701, 703, NULL, "unit_test_res_parking_create_lot_con", test);
  206. if (!test_lot) {
  207. ast_test_status_update(test, "Failed to create test parking lot. Test failed.\n");
  208. return AST_TEST_FAIL;
  209. }
  210. chan_alice = create_alice_channel();
  211. if (!chan_alice) {
  212. ast_test_status_update(test, "Failed to create test channel to park. Test failed.\n");
  213. dispose_test_lot(test_lot, 1);
  214. return AST_TEST_FAIL;
  215. }
  216. ast_channel_state_set(chan_alice, AST_STATE_UP);
  217. pbx_builtin_setvar_helper(chan_alice, "BLINDTRANSFER", ast_channel_name(chan_alice));
  218. parking_bridge = park_application_setup(chan_alice, chan_alice, TEST_LOT_NAME, NULL);
  219. if (!parking_bridge) {
  220. ast_test_status_update(test, "Failed to get the parking bridge for '%s'. Test failed.\n", TEST_LOT_NAME);
  221. dispose_test_lot(test_lot, 1);
  222. return AST_TEST_FAIL;
  223. }
  224. if (ast_bridge_impart(parking_bridge, chan_alice, NULL, NULL,
  225. AST_BRIDGE_IMPART_CHAN_DEPARTABLE)) {
  226. ast_test_status_update(test, "Failed to impart alice into parking lot. Test failed.\n");
  227. dispose_test_lot(test_lot, 1);
  228. return AST_TEST_FAIL;
  229. }
  230. do_sleep(&to_sleep);
  231. ast_bridge_depart(chan_alice);
  232. chan_alice = hangup_channel(chan_alice, AST_CAUSE_NORMAL);
  233. if (dispose_test_lot(test_lot, 1)) {
  234. ast_test_status_update(test, "Found parking lot in container after attempted removal. Test failed.\n");
  235. return AST_TEST_FAIL;
  236. }
  237. return AST_TEST_PASS;
  238. }
  239. static int parked_users_match(const struct parked_user *actual, const struct parked_user *expected, struct ast_test *test)
  240. {
  241. if (expected->parking_space != actual->parking_space) {
  242. ast_test_status_update(test, "parking_space expected: %d - got: %d\n", expected->parking_space, actual->parking_space);
  243. return 0;
  244. }
  245. if (strcmp(expected->parker_dial_string, actual->parker_dial_string)) {
  246. ast_test_status_update(test, "parker_dial_string expected: %s - got: %s\n", expected->parker_dial_string, actual->parker_dial_string);
  247. return 0;
  248. }
  249. if (expected->time_limit != actual->time_limit) {
  250. ast_test_status_update(test, "time_limit expected: %u - got: %u\n", expected->time_limit, actual->time_limit);
  251. return 0;
  252. }
  253. if (expected->resolution != actual->resolution) {
  254. ast_test_status_update(test, "resolution expected: %u - got: %u\n", expected->resolution, actual->resolution);
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. static int parking_lot_cfgs_match(const struct parking_lot_cfg *actual, const struct parking_lot_cfg *expected, struct ast_test *test)
  260. {
  261. if (expected->parking_start != actual->parking_start) {
  262. ast_test_status_update(test, "parking_start expected: %d - got: %d\n", expected->parking_start, actual->parking_start);
  263. return 0;
  264. }
  265. if (expected->parking_stop != actual->parking_stop) {
  266. ast_test_status_update(test, "parking_stop expected: %d - got: %d\n", expected->parking_stop, actual->parking_stop);
  267. return 0;
  268. }
  269. if (expected->parkingtime != actual->parkingtime) {
  270. ast_test_status_update(test, "parkingtime expected: %u - got: %u\n", expected->parkingtime, actual->parkingtime);
  271. return 0;
  272. }
  273. if (expected->comebackdialtime != actual->comebackdialtime) {
  274. ast_test_status_update(test, "comebackdialtime expected: %u - got: %u\n", expected->comebackdialtime, actual->comebackdialtime);
  275. return 0;
  276. }
  277. if (expected->parkfindnext != actual->parkfindnext) {
  278. ast_test_status_update(test, "parkfindnext expected: %u - got: %u\n", expected->parkfindnext, actual->parkfindnext);
  279. return 0;
  280. }
  281. if (expected->parkext_exclusive != actual->parkext_exclusive) {
  282. ast_test_status_update(test, "parkext_exclusive expected: %u - got: %u\n", expected->parkext_exclusive, actual->parkext_exclusive);
  283. return 0;
  284. }
  285. if (strcmp(expected->parkext, actual->parkext)) {
  286. ast_test_status_update(test, "parkext expected: %s - got: %s\n", expected->parkext, actual->parkext);
  287. return 0;
  288. }
  289. if (strcmp(expected->parking_con, actual->parking_con)) {
  290. ast_test_status_update(test, "parking_con expected: %s - got: %s\n", expected->parking_con, actual->parking_con);
  291. return 0;
  292. }
  293. if (strcmp(expected->comebackcontext, actual->comebackcontext)) {
  294. ast_test_status_update(test, "comebackcontext expected: %s - got: %s\n", expected->comebackcontext, actual->comebackcontext);
  295. return 0;
  296. }
  297. return 1;
  298. }
  299. AST_TEST_DEFINE(retrieve_call)
  300. {
  301. RAII_VAR(struct parking_lot *, test_lot, NULL, ao2_cleanup);
  302. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  303. RAII_VAR(struct ast_bridge *, parking_bridge, NULL, ao2_cleanup);
  304. RAII_VAR(struct parked_user *, retrieved_user, NULL, ao2_cleanup);
  305. struct timespec to_sleep = {1, 0};
  306. int failure = 0;
  307. static const struct parked_user expected_user = {
  308. .parking_space = 701,
  309. .parker_dial_string = "ParkingTestChannel/Alice",
  310. .time_limit = 10,
  311. .resolution = PARK_ANSWERED,
  312. };
  313. switch (cmd) {
  314. case TEST_INIT:
  315. info->name = "park_retrieve";
  316. info->category = TEST_CATEGORY;
  317. info->summary = "Retrieve a parked channel";
  318. info->description =
  319. "Creates a parking lot, parks a channel in it, then removes it from the parking lot bridge.";
  320. return AST_TEST_NOT_RUN;
  321. case TEST_EXECUTE:
  322. break;
  323. }
  324. ast_test_status_update(test, "Creating test parking lot '%s'\n", TEST_LOT_NAME);
  325. test_lot = generate_test_parking_lot(TEST_LOT_NAME, 701, 703, NULL, "unit_test_res_parking_create_lot_con", test);
  326. if (!test_lot) {
  327. ast_test_status_update(test, "Failed to create test parking lot. Test failed.\n");
  328. return AST_TEST_FAIL;
  329. }
  330. chan_alice = create_alice_channel();
  331. if (!chan_alice) {
  332. ast_test_status_update(test, "Failed to create test channel to park. Test failed.\n");
  333. dispose_test_lot(test_lot, 1);
  334. return AST_TEST_FAIL;
  335. }
  336. ast_channel_state_set(chan_alice, AST_STATE_UP);
  337. pbx_builtin_setvar_helper(chan_alice, "BLINDTRANSFER", ast_channel_name(chan_alice));
  338. parking_bridge = park_application_setup(chan_alice, chan_alice, TEST_LOT_NAME, NULL);
  339. if (!parking_bridge) {
  340. ast_test_status_update(test, "Failed to get the parking bridge for '%s'. Test failed.\n", TEST_LOT_NAME);
  341. dispose_test_lot(test_lot, 1);
  342. return AST_TEST_FAIL;
  343. }
  344. if (ast_bridge_impart(parking_bridge, chan_alice, NULL, NULL,
  345. AST_BRIDGE_IMPART_CHAN_DEPARTABLE)) {
  346. ast_test_status_update(test, "Failed to impart alice into parking lot. Test failed.\n");
  347. dispose_test_lot(test_lot, 1);
  348. return AST_TEST_FAIL;
  349. }
  350. do_sleep(&to_sleep);
  351. retrieved_user = parking_lot_retrieve_parked_user(test_lot, 701);
  352. if (!retrieved_user) {
  353. ast_test_status_update(test, "Failed to retrieve the parked user from the expected parking space. Test failed.\n");
  354. failure = 1;
  355. goto test_cleanup;
  356. }
  357. ast_test_status_update(test, "Successfully retrieved parked user from the parking lot. Validating user data.\n");
  358. if (!parked_users_match(retrieved_user, &expected_user, test)) {
  359. ast_test_status_update(test, "Parked user validation failed\n");
  360. failure = 1;
  361. goto test_cleanup;
  362. }
  363. if (retrieved_user->chan != chan_alice) {
  364. ast_test_status_update(test, "The retrieved parked channel didn't match the expected channel. Test failed.\n");
  365. failure = 1;
  366. goto test_cleanup;
  367. }
  368. test_cleanup:
  369. ast_bridge_depart(chan_alice);
  370. chan_alice = hangup_channel(chan_alice, AST_CAUSE_NORMAL);
  371. if (dispose_test_lot(test_lot, 1)) {
  372. ast_test_status_update(test, "Found parking lot in container after attempted removal. Test failed.\n");
  373. failure = 1;
  374. }
  375. return failure ? AST_TEST_FAIL : AST_TEST_PASS;
  376. }
  377. static int check_retrieve_call_extensions(struct ast_test *test, int expected)
  378. {
  379. struct ast_exten *check;
  380. struct pbx_find_info find_info = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
  381. int extens;
  382. char search_buffer[4];
  383. /* Check the parking extensions */
  384. check = pbx_find_extension(NULL, NULL, &find_info, "unit_test_res_parking_create_lot_con", "700", 1, NULL, NULL, E_MATCH);
  385. if (check ? !expected : expected) {
  386. /* extension isn't present when it should be or is present when it shouldn't be. Automatic failure. */
  387. ast_test_status_update(test, "An extension '700' was %s when it %s have been. Test failed.\n",
  388. expected ? "not present" : "present",
  389. expected ? "should" : "should not");
  390. return -1;
  391. } else if (check && expected) {
  392. if (strcmp(ast_get_extension_app(check), "Park")) {
  393. ast_test_status_update(test, "An extension '700' has the wrong application associated with it. Got '%s' expected 'Park'.\n",
  394. ast_get_extension_app(check));
  395. return -1;
  396. }
  397. }
  398. /* Check the parking space extensions 701-703 */
  399. for (extens = 701; extens <= 703; extens++) {
  400. sprintf(search_buffer, "%d", extens);
  401. find_info.stacklen = 0; /* reset for pbx_find_extension */
  402. check = pbx_find_extension(NULL, NULL, &find_info, "unit_test_res_parking_create_lot_con", search_buffer, 1, NULL, NULL, E_MATCH);
  403. if (check ? !expected : expected) {
  404. /* extension isn't present when it should be or is present when it shouldn't be. Automatic failure. */
  405. ast_test_status_update(test, "An extension '%s' was %s when it %s have been. Test failed.\n",
  406. search_buffer,
  407. expected ? "not present" : "present",
  408. expected ? "should" : "should not");
  409. return -1;
  410. } else if (check && expected) {
  411. if (strcmp(ast_get_extension_app(check), "ParkedCall")) {
  412. ast_test_status_update(test, "An extension '%s' has the wrong application associated with it. Got '%s', expected 'ParkedCall'.\n",
  413. search_buffer,
  414. ast_get_extension_app(check));
  415. return -1;
  416. }
  417. }
  418. }
  419. return 0;
  420. }
  421. AST_TEST_DEFINE(park_extensions)
  422. {
  423. RAII_VAR(struct parking_lot *, test_lot, NULL, ao2_cleanup);
  424. switch (cmd) {
  425. case TEST_INIT:
  426. info->name = "park_extensions";
  427. info->category = TEST_CATEGORY;
  428. info->summary = "Parking lot extension creation tests";
  429. info->description =
  430. "Creates parking lots and checks that they registered the expected extensions, then removes them.";
  431. return AST_TEST_NOT_RUN;
  432. case TEST_EXECUTE:
  433. break;
  434. }
  435. test_lot = generate_test_parking_lot(TEST_LOT_NAME, 701, 703, "700", "unit_test_res_parking_create_lot_con", test);
  436. if (!test_lot) {
  437. ast_test_status_update(test, "Failed to create test parking lot. Test Failed.\n");
  438. return AST_TEST_FAIL;
  439. }
  440. if (check_retrieve_call_extensions(test, 1)) {
  441. dispose_test_lot(test_lot, 1);
  442. return AST_TEST_FAIL;
  443. }
  444. ast_test_status_update(test, "Extensions for the test parking lot were verified. Cleaning up and verifying their removal.\n");
  445. if (dispose_test_lot(test_lot, 1)) {
  446. ast_test_status_update(test, "Found parking lot in container after attempted removal. Test failed.\n");
  447. return AST_TEST_FAIL;
  448. }
  449. ao2_cleanup(test_lot);
  450. test_lot = NULL;
  451. if (check_retrieve_call_extensions(test, 0)) {
  452. ast_log(LOG_ERROR, "Test 'park_extensions' failed to clean up after itself properly.\n");
  453. return AST_TEST_FAIL;
  454. }
  455. ast_test_status_update(test, "Extensions for the test parking lot verified as removed. Test completed successfully.\n");
  456. return AST_TEST_PASS;
  457. }
  458. AST_TEST_DEFINE(extension_conflicts)
  459. {
  460. RAII_VAR(struct parking_lot *, base_lot, NULL, ao2_cleanup);
  461. RAII_VAR(struct parking_lot *, expect_fail1, NULL, ao2_cleanup); /* Failure due to overlapping parkexten */
  462. RAII_VAR(struct parking_lot *, expect_fail2, NULL, ao2_cleanup); /* Failure due to overlapping spaces */
  463. RAII_VAR(struct parking_lot *, expect_fail3, NULL, ao2_cleanup); /* parkexten overlaps parking spaces */
  464. RAII_VAR(struct parking_lot *, expect_fail4, NULL, ao2_cleanup); /* parking spaces overlap parkexten */
  465. RAII_VAR(struct parking_lot *, expect_success1, NULL, ao2_cleanup); /* Success due to being in a different context */
  466. RAII_VAR(struct parking_lot *, expect_success2, NULL, ao2_cleanup); /* Success due to not having overlapping extensions */
  467. RAII_VAR(struct parking_lot *, expect_success3, NULL, ao2_cleanup); /* Range of parking spaces differs by one above */
  468. RAII_VAR(struct parking_lot *, expect_success4, NULL, ao2_cleanup); /* Range of parking spaces differs by one below */
  469. char *cur_lot_name;
  470. int failed = 0;
  471. switch (cmd) {
  472. case TEST_INIT:
  473. info->name = "extension_conflicts";
  474. info->category = TEST_CATEGORY;
  475. info->summary = "Tests the addition of parking lot extensions to make sure conflicts are detected";
  476. info->description =
  477. "Creates parking lots with overlapping extensions to test for conflicts";
  478. return AST_TEST_NOT_RUN;
  479. case TEST_EXECUTE:
  480. break;
  481. }
  482. ast_test_status_update(test, "Creating the base lot. This should pass.\n");
  483. base_lot = generate_test_parking_lot(TEST_LOT_NAME, 701, 703, "700", "unit_test_res_parking_create_lot_con", test);
  484. if (!base_lot) {
  485. ast_test_status_update(test, "Failed to create the base parking lot. Test failed.\n");
  486. failed = 1;
  487. goto cleanup;
  488. }
  489. cur_lot_name = "unit_tests_res_parking_test_lot_fail1";
  490. ast_test_status_update(test, "Creating a test lot which will overlap.\n");
  491. expect_fail1 = generate_test_parking_lot(cur_lot_name,
  492. 801, 803, "700", "unit_test_res_parking_create_lot_con", /* The parkexten overlaps the parkexten of the base */
  493. test);
  494. if (expect_fail1) {
  495. ast_test_status_update(test, "%s was successfully created when it was expected to fail. Test failed.\n", cur_lot_name);
  496. failed = 1;
  497. goto cleanup;
  498. }
  499. cur_lot_name = "unit_tests_res_parking_test_lot_fail2";
  500. expect_fail2 = generate_test_parking_lot(cur_lot_name,
  501. 702, 705, "800", "unit_test_res_parking_create_lot_con", /* The range overlaps the range of the base */
  502. test);
  503. if (expect_fail2) {
  504. ast_test_status_update(test, "%s was successfully created when it was expected to fail. Test failed.\n", cur_lot_name);
  505. failed = 1;
  506. goto cleanup;
  507. }
  508. cur_lot_name = "unit_tests_res_parking_test_lot_fail3";
  509. expect_fail3 = generate_test_parking_lot(cur_lot_name,
  510. 698, 700, "testfail3", "unit_test_res_parking_create_lot_con", /* The range overlaps the parkexten of the base */
  511. test);
  512. if (expect_fail3) {
  513. ast_test_status_update(test, "%s was successfully created when it was expected to fail. Test failed.\n", cur_lot_name);
  514. failed = 1;
  515. goto cleanup;
  516. }
  517. cur_lot_name = "unit_tests_res_parking_test_lot_fail4";
  518. expect_fail4 = generate_test_parking_lot(cur_lot_name,
  519. 704, 706, "703", "unit_test_res_parking_create_lot_con", /* The parkexten overlaps the range of the base */
  520. test);
  521. if (expect_fail4) {
  522. ast_test_status_update(test, "%s was successfully created when it was expected to fail. Test failed.\n", cur_lot_name);
  523. failed = 1;
  524. goto cleanup;
  525. }
  526. cur_lot_name = "unit_tests_res_parking_test_lot_success1";
  527. expect_success1 = generate_test_parking_lot(cur_lot_name,
  528. 701, 703, "700", "unit_test_res_parking_create_lot_con_2", /* no overlap due to different context */
  529. test);
  530. if (!expect_success1) {
  531. ast_test_status_update(test, "%s failed to be created. Success was expected. Test failed.\n", cur_lot_name);
  532. failed = 1;
  533. goto cleanup;
  534. }
  535. cur_lot_name = "unit_tests_res_parking_test_lot_success2";
  536. expect_success2 = generate_test_parking_lot(cur_lot_name,
  537. 601, 605, "600", "unit_test_res_parking_create_lot_con", /* no overlap due to different extensions and ranges */
  538. test);
  539. if (!expect_success2) {
  540. ast_test_status_update(test, "%s failed to be created. Success was expected. Test failed.\n", cur_lot_name);
  541. failed = 1;
  542. goto cleanup;
  543. }
  544. cur_lot_name = "unit_tests_res_parking_test_lot_success3";
  545. expect_success3 = generate_test_parking_lot(cur_lot_name,
  546. 704, 706, "testsuccess3", "unit_test_res_parking_create_lot_con", /* no overlap because the parking spaces start 1 above existing ranges */
  547. test);
  548. if (!expect_success3) {
  549. ast_test_status_update(test, "%s failed to be created. Success was expected. Test failed.\n", cur_lot_name);
  550. failed = 1;
  551. goto cleanup;
  552. }
  553. cur_lot_name = "unit_tests_res_parking_test_lot_success4";
  554. expect_success4 = generate_test_parking_lot(cur_lot_name,
  555. 697, 699, "testsuccess4", "unit_test_res_parking_create_lot_con", /* no overlap because the parking spaces end 1 below existing ranges */
  556. test);
  557. if (!expect_success4) {
  558. failed = 1;
  559. goto cleanup;
  560. }
  561. cleanup:
  562. if (base_lot && dispose_test_lot(base_lot, 1)) {
  563. ast_test_status_update(test, "Found base parking lot in container after attempted removal. Test failed.\n");
  564. failed = 1;
  565. }
  566. if (expect_fail1) {
  567. dispose_test_lot(expect_fail1, 1);
  568. failed = 1;
  569. }
  570. if (expect_fail2) {
  571. dispose_test_lot(expect_fail2, 1);
  572. failed = 1;
  573. }
  574. if (expect_fail3) {
  575. dispose_test_lot(expect_fail3, 1);
  576. failed = 1;
  577. }
  578. if (expect_fail4) {
  579. dispose_test_lot(expect_fail4, 1);
  580. failed = 1;
  581. }
  582. if (expect_success1 && dispose_test_lot(expect_success1, 1)) {
  583. ast_test_status_update(test, "Found expect_success1 parking lot in container after attempted removal. Test failed.\n");
  584. failed = 1;
  585. }
  586. if (expect_success2 && dispose_test_lot(expect_success2, 1)) {
  587. ast_test_status_update(test, "Found expect_success2 parking lot in container after attempted removal. Test failed.\n");
  588. failed = 1;
  589. }
  590. if (expect_success3 && dispose_test_lot(expect_success3, 1)) {
  591. ast_test_status_update(test, "Found expect_success3 parking lot in container after attempted removal. Test failed.\n");
  592. failed = 1;
  593. }
  594. if (expect_success4 && dispose_test_lot(expect_success4, 1)) {
  595. ast_test_status_update(test, "Found expect_success4 parking lot in container after attempted removal. Test failed.\n");
  596. }
  597. return failed ? AST_TEST_FAIL : AST_TEST_PASS;
  598. }
  599. AST_TEST_DEFINE(dynamic_parking_variables)
  600. {
  601. RAII_VAR(struct parking_lot *, template_lot, NULL, ao2_cleanup);
  602. RAII_VAR(struct parking_lot *, dynamic_lot, NULL, ao2_cleanup);
  603. RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
  604. RAII_VAR(struct parking_lot_cfg *, expected_cfg, NULL, ao2_cleanup);
  605. int failed = 0;
  606. switch (cmd) {
  607. case TEST_INIT:
  608. info->name = "dynamic_parking_variables";
  609. info->category = TEST_CATEGORY;
  610. info->summary = "Tests whether dynamic parking lot creation respects channel variables";
  611. info->description =
  612. "Creates a template parking lot, creates a channel, sets dynamic parking variables, and then creates a parking lot for that channel";
  613. return AST_TEST_NOT_RUN;
  614. case TEST_EXECUTE:
  615. break;
  616. }
  617. ast_test_status_update(test, "Creating expected configuration for dynamic parking lot\n");
  618. expected_cfg = parking_lot_cfg_create("unit_tests_res_parking_test_lot_dynamic");
  619. if (!expected_cfg) {
  620. ast_test_status_update(test, "Failed to create expected configuration. Test failed.\n");
  621. return AST_TEST_FAIL;
  622. }
  623. expected_cfg->parking_start = 751;
  624. expected_cfg->parking_stop = 760;
  625. expected_cfg->parkingtime = 10;
  626. expected_cfg->comebackdialtime = 10;
  627. expected_cfg->parkfindnext = 1;
  628. expected_cfg->parkext_exclusive = 1;
  629. ast_string_field_set(expected_cfg, parkext, "750");
  630. ast_string_field_set(expected_cfg, parking_con, "unit_test_res_parking_create_lot_dynamic");
  631. ast_string_field_set(expected_cfg, comebackcontext, "unit_test_res_parking_create_lot_comeback");
  632. ast_test_status_update(test, "Creating template lot\n");
  633. template_lot = generate_test_parking_lot(TEST_LOT_NAME, 701, 703, "700", "unit_test_res_parking_create_lot_con", test);
  634. if (!template_lot) {
  635. ast_test_status_update(test, "Failed to generate template lot. Test failed.\n");
  636. return AST_TEST_FAIL;
  637. }
  638. ast_test_status_update(test, "Creating Alice channel to test dynamic parking lot creation.\n");
  639. chan_alice = create_alice_channel();
  640. if (!chan_alice) {
  641. ast_test_status_update(test, "Failed to create Alice channel. Test failed.\n");
  642. failed = 1;
  643. goto cleanup;
  644. }
  645. ast_test_status_update(test, "Setting Dynamic Parking channel variables on Alice.\n");
  646. pbx_builtin_setvar_helper(chan_alice, "PARKINGDYNAMIC", TEST_LOT_NAME);
  647. pbx_builtin_setvar_helper(chan_alice, "PARKINGLOT", "unit_test_res_parking_create_lot_dynamic");
  648. pbx_builtin_setvar_helper(chan_alice, "PARKINGDYNCONTEXT", "unit_test_res_parking_create_lot_dynamic");
  649. pbx_builtin_setvar_helper(chan_alice, "PARKINGDYNEXTEN", "750");
  650. pbx_builtin_setvar_helper(chan_alice, "PARKINGDYNPOS", "751-760");
  651. ast_test_status_update(test, "Generating dynamic parking lot based on Alice's channel variables.\n");
  652. dynamic_lot = parking_create_dynamic_lot_forced("unit_tests_res_parking_test_lot_dynamic", chan_alice);
  653. if (!dynamic_lot) {
  654. ast_test_status_update(test, "Failed to create dynamic parking lot. Test failed.\n");
  655. failed = 1;
  656. goto cleanup;
  657. }
  658. /* Check stats */
  659. if (!parking_lot_cfgs_match(dynamic_lot->cfg, expected_cfg, test)) {
  660. ast_test_status_update(test, "Dynamic parking lot configuration did not match Expectations.\n");
  661. failed = 1;
  662. goto cleanup;
  663. }
  664. ast_test_status_update(test, "Dynamic parking lot created successfully and matches expectations. Test passed.\n");
  665. cleanup:
  666. if (template_lot && dispose_test_lot(template_lot, 1)) {
  667. ast_test_status_update(test, "Found template parking lot in container after attempted removal. Test failed.\n");
  668. failed = 1;
  669. }
  670. if (dynamic_lot && dispose_test_lot(dynamic_lot, 1)) {
  671. ast_test_status_update(test, "Found dynamic parking lot in container after attempted removal. Test failed.\n");
  672. failed = 1;
  673. }
  674. return failed ? AST_TEST_FAIL : AST_TEST_PASS;
  675. }
  676. #endif /* TEST_FRAMEWORK */
  677. void unload_parking_tests(void)
  678. {
  679. /* NOOP without test framework */
  680. #if defined(TEST_FRAMEWORK)
  681. AST_TEST_UNREGISTER(create_lot);
  682. AST_TEST_UNREGISTER(park_call);
  683. AST_TEST_UNREGISTER(retrieve_call);
  684. AST_TEST_UNREGISTER(park_extensions);
  685. AST_TEST_UNREGISTER(extension_conflicts);
  686. AST_TEST_UNREGISTER(dynamic_parking_variables);
  687. #endif
  688. }
  689. int load_parking_tests(void)
  690. {
  691. int res = 0;
  692. /* NOOP without test framework */
  693. #if defined(TEST_FRAMEWORK)
  694. res |= AST_TEST_REGISTER(create_lot);
  695. res |= AST_TEST_REGISTER(park_call);
  696. res |= AST_TEST_REGISTER(retrieve_call);
  697. res |= AST_TEST_REGISTER(park_extensions);
  698. res |= AST_TEST_REGISTER(extension_conflicts);
  699. res |= AST_TEST_REGISTER(dynamic_parking_variables);
  700. #endif
  701. return res;
  702. }