parking_tests.c 28 KB

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