test_voicemail_api.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Matt Jordan
  5. *
  6. * Matt Jordan <mjordan@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 Skeleton Test
  21. *
  22. * \author\verbatim Matt Jordan <mjordan@digium.com> \endverbatim
  23. *
  24. * Tests for the publicly exposed Voicemail API
  25. * \ingroup tests
  26. */
  27. /*** MODULEINFO
  28. <depend>TEST_FRAMEWORK</depend>
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <sys/stat.h>
  34. #include "asterisk/utils.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/test.h"
  37. #include "asterisk/paths.h"
  38. #include "asterisk/channel.h"
  39. #include "asterisk/app.h"
  40. #include "asterisk/format_cache.h"
  41. /*!
  42. * \internal
  43. * \brief Permissions to set on the voicemail directories we create
  44. *
  45. * \note taken from app_voicemail
  46. */
  47. #define VOICEMAIL_DIR_MODE 0777
  48. /*!
  49. * \internal
  50. * \brief Permissions to set on the voicemail files we create
  51. *
  52. * \note taken from app_voicemail
  53. */
  54. #define VOICEMAIL_FILE_MODE 0666
  55. /*!
  56. * \internal
  57. * \brief The number of mock snapshot objects we use for tests
  58. */
  59. #define TOTAL_SNAPSHOTS 4
  60. /*!
  61. * \internal
  62. * \brief Create and populate the mock message objects and create the
  63. * envelope files on the file system
  64. */
  65. #define VM_API_TEST_SETUP do { \
  66. if (!ast_vm_is_registered()) { \
  67. ast_test_status_update(test, "No voicemail provider registered.\n"); \
  68. return AST_TEST_FAIL; \
  69. } else if (test_vm_api_test_setup()) { \
  70. VM_API_TEST_CLEANUP; \
  71. ast_test_status_update(test, "Failed to set up necessary mock objects for voicemail API test\n"); \
  72. return AST_TEST_FAIL; \
  73. } else { \
  74. int i = 0; \
  75. for (; i < TOTAL_SNAPSHOTS; i++) { \
  76. ast_test_status_update(test, "Created message in %s/%s with ID %s\n", \
  77. test_snapshots[i]->exten, test_snapshots[i]->folder_name, test_snapshots[i]->msg_id); \
  78. } \
  79. } } while (0)
  80. /*!
  81. * \internal
  82. * \brief Safely cleanup after a test run.
  83. *
  84. * \note This should be called both when a test fails and when it passes
  85. */
  86. #define VM_API_TEST_CLEANUP test_vm_api_test_teardown()
  87. /*!
  88. * \internal
  89. * \brief Safely cleanup a snapshot and a test run.
  90. *
  91. * \note It assumes that the mailbox snapshot object is test_mbox_snapshot
  92. */
  93. #define VM_API_SNAPSHOT_TEST_CLEANUP \
  94. if (test_mbox_snapshot) { \
  95. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot); \
  96. } \
  97. VM_API_TEST_CLEANUP; \
  98. /*!
  99. * \internal
  100. * \brief Verify the expected result from two string values obtained
  101. * from a mailbox snapshot.
  102. *
  103. * \note It assumes the mailbox snapshot object is test_mbox_snapshot
  104. */
  105. #define VM_API_STRING_FIELD_VERIFY(expected, actual) do { \
  106. if (strcmp((expected), (actual))) { \
  107. ast_test_status_update(test, "Test failed for parameter %s: Expected [%s], Actual [%s]\n", #actual, expected, actual); \
  108. VM_API_SNAPSHOT_TEST_CLEANUP; \
  109. return AST_TEST_FAIL; \
  110. } } while (0)
  111. /*!
  112. * \internal
  113. * \brief Verify the expected result from two integer values.
  114. *
  115. * \note It assumes the mailbox snapshot object is test_mbox_snapshot
  116. */
  117. #define VM_API_INT_VERIFY(expected, actual) do { \
  118. if ((expected) != (actual)) { \
  119. ast_test_status_update(test, "Test failed for parameter %s: Expected [%d], Actual [%d]\n", #actual, (int)expected, (int)actual); \
  120. VM_API_SNAPSHOT_TEST_CLEANUP; \
  121. return AST_TEST_FAIL; \
  122. } } while (0)
  123. /*!
  124. * \internal
  125. * \brief Verify that a mailbox snapshot contains the expected message
  126. * snapshot, in the correct position, with the expected values.
  127. *
  128. * \note It assumes the mailbox snapshot object is test_mbox_snapshot
  129. */
  130. #define VM_API_SNAPSHOT_MSG_VERIFY(expected, actual, expected_folder, expected_index) do { \
  131. struct ast_vm_msg_snapshot *msg; \
  132. int found = 0; \
  133. int counter = 0; \
  134. AST_LIST_TRAVERSE(&((actual)->snapshots[get_folder_by_name(expected_folder)]), msg, msg) { \
  135. if (!(strcmp(msg->msg_id, (expected)->msg_id))) { \
  136. ast_test_status_update(test, "Found message %s in snapshot\n", msg->msg_id); \
  137. found = 1; \
  138. if ((expected_index) != counter) { \
  139. ast_test_status_update(test, "Expected message %s at index %d; Actual [%d]\n", \
  140. (expected)->msg_id, (expected_index), counter); \
  141. VM_API_SNAPSHOT_TEST_CLEANUP; \
  142. return AST_TEST_FAIL; \
  143. } \
  144. VM_API_STRING_FIELD_VERIFY((expected)->callerid, msg->callerid); \
  145. VM_API_STRING_FIELD_VERIFY((expected)->callerchan, msg->callerchan); \
  146. VM_API_STRING_FIELD_VERIFY((expected)->exten, msg->exten); \
  147. VM_API_STRING_FIELD_VERIFY((expected)->origdate, msg->origdate); \
  148. VM_API_STRING_FIELD_VERIFY((expected)->origtime, msg->origtime); \
  149. VM_API_STRING_FIELD_VERIFY((expected)->duration, msg->duration); \
  150. VM_API_STRING_FIELD_VERIFY((expected)->folder_name, msg->folder_name); \
  151. VM_API_STRING_FIELD_VERIFY((expected)->flag, msg->flag); \
  152. VM_API_INT_VERIFY((expected)->msg_number, msg->msg_number); \
  153. break; \
  154. } \
  155. ++counter; \
  156. } \
  157. if (!found) { \
  158. ast_test_status_update(test, "Test failed for message snapshot %s: not found in mailbox snapshot\n", (expected)->msg_id); \
  159. VM_API_SNAPSHOT_TEST_CLEANUP; \
  160. return AST_TEST_FAIL; \
  161. } } while (0)
  162. /*!
  163. * \internal
  164. * \brief Create a message snapshot, failing the test if the snapshot could not be created.
  165. *
  166. * \note This requires having a snapshot named test_mbox_snapshot.
  167. */
  168. #define VM_API_SNAPSHOT_CREATE(mailbox, context, folder, desc, sort, old_and_inbox) do { \
  169. if (!(test_mbox_snapshot = ast_vm_mailbox_snapshot_create( \
  170. (mailbox), (context), (folder), (desc), (sort), (old_and_inbox)))) { \
  171. ast_test_status_update(test, "Failed to create voicemail mailbox snapshot\n"); \
  172. VM_API_TEST_CLEANUP; \
  173. return AST_TEST_FAIL; \
  174. } } while (0)
  175. /*!
  176. * \internal
  177. * \brief Create a message snapshot, failing the test if the snapshot could be created.
  178. *
  179. * \note This is used to test off nominal conditions.
  180. * \note This requires having a snapshot named test_mbox_snapshot.
  181. */
  182. #define VM_API_SNAPSHOT_OFF_NOMINAL_TEST(mailbox, context, folder, desc, sort, old_and_inbox) do { \
  183. if ((test_mbox_snapshot = ast_vm_mailbox_snapshot_create( \
  184. (mailbox), (context), (folder), (desc), (sort), (old_and_inbox)))) { \
  185. ast_test_status_update(test, "Created mailbox snapshot when none was expected\n"); \
  186. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot); \
  187. VM_API_TEST_CLEANUP; \
  188. return AST_TEST_FAIL; \
  189. } } while (0)
  190. /*!
  191. * \internal
  192. * \brief Move a voicemail message, failing the test if the message could not be moved
  193. */
  194. #define VM_API_MOVE_MESSAGE(mailbox, context, number_of_messages, source, message_numbers_in, dest) do { \
  195. if (ast_vm_msg_move((mailbox), (context), (number_of_messages), (source), (message_numbers_in), (dest))) { \
  196. ast_test_status_update(test, "Failed to move message %s@%s from %s to %s\n", \
  197. (mailbox) ? (mailbox): "(NULL)", (context) ? (context) : "(NULL)", (source) ? (source) : "(NULL)", (dest) ? (dest) : "(NULL)"); \
  198. VM_API_TEST_CLEANUP; \
  199. return AST_TEST_FAIL; \
  200. } } while (0)
  201. /*!
  202. * \internal
  203. * \brief Attempt to move a voicemail message, failing the test if the message could be moved
  204. */
  205. #define VM_API_MOVE_MESSAGE_OFF_NOMINAL(mailbox, context, number_of_messages, source, message_numbers_in, dest) do { \
  206. if (!ast_vm_msg_move((mailbox), (context), (number_of_messages), (source), (message_numbers_in), (dest))) { \
  207. ast_test_status_update(test, "Succeeded to move message %s@%s from %s to %s when we really shouldn't\n", \
  208. (mailbox) ? (mailbox): "(NULL)", (context) ? (context) : "(NULL)", (source) ? (source) : "(NULL)", (dest) ? (dest) : "(NULL)"); \
  209. VM_API_TEST_CLEANUP; \
  210. return AST_TEST_FAIL; \
  211. } } while (0)
  212. /*!
  213. * \internal
  214. * \brief Remove a message, failing the test if the method failed or if the message is still present.
  215. */
  216. #define VM_API_REMOVE_MESSAGE(mailbox, context, number_of_messages, folder, message_numbers_in) do { \
  217. if (ast_vm_msg_remove((mailbox), (context), (number_of_messages), (folder), (message_numbers_in))) { \
  218. ast_test_status_update(test, "Failed to remove message from mailbox %s@%s, folder %s", \
  219. (mailbox) ? (mailbox): "(NULL)", (context) ? (context) : "(NULL)", (folder) ? (folder) : "(NULL)"); \
  220. VM_API_TEST_CLEANUP; \
  221. return AST_TEST_FAIL; \
  222. } \
  223. VM_API_SNAPSHOT_CREATE((mailbox), (context), (folder), 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0); \
  224. VM_API_INT_VERIFY(0, test_mbox_snapshot->total_msg_num); \
  225. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot); \
  226. } while (0)
  227. /*!
  228. * \internal
  229. * \brief Remove a message, failing the test if the method succeeds
  230. */
  231. #define VM_API_REMOVE_MESSAGE_OFF_NOMINAL(mailbox, context, number_of_messages, folder, message_numbers_in) do { \
  232. if (!ast_vm_msg_remove((mailbox), (context), (number_of_messages), (folder), (message_numbers_in))) { \
  233. ast_test_status_update(test, "Succeeded in removing message from mailbox %s@%s, folder %s, when expected result was failure\n", \
  234. (mailbox) ? (mailbox): "(NULL)", (context) ? (context) : "(NULL)", (folder) ? (folder) : "(NULL)"); \
  235. VM_API_TEST_CLEANUP; \
  236. return AST_TEST_FAIL; \
  237. } } while (0)
  238. /*!
  239. * \internal
  240. * \brief Forward a message, failing the test if the message could not be forwarded
  241. */
  242. # define VM_API_FORWARD_MESSAGE(from_mailbox, from_context, from_folder, to_mailbox, to_context, to_folder, number_of_messages, message_numbers_in, delete_old) do { \
  243. if (ast_vm_msg_forward((from_mailbox), (from_context), (from_folder), (to_mailbox), (to_context), (to_folder), (number_of_messages), (message_numbers_in), (delete_old))) { \
  244. ast_test_status_update(test, "Failed to forward message from %s@%s [%s] to %s@%s [%s]\n", \
  245. (from_mailbox) ? (from_mailbox) : "(NULL)", (from_context) ? (from_context) : "(NULL)", (from_folder) ? (from_folder) : "(NULL)", \
  246. (to_mailbox) ? (to_mailbox) : "(NULL)", (to_context) ? (to_context) : "(NULL)", (to_folder) ? (to_folder) : "(NULL)"); \
  247. VM_API_TEST_CLEANUP; \
  248. return AST_TEST_FAIL; \
  249. } } while (0)
  250. /*!
  251. * \internal
  252. * \brief Forward a message, failing the test if the message was successfully forwarded
  253. */
  254. #define VM_API_FORWARD_MESSAGE_OFF_NOMINAL(from_mailbox, from_context, from_folder, to_mailbox, to_context, to_folder, number_of_messages, message_numbers_in, delete_old) do { \
  255. if (!ast_vm_msg_forward((from_mailbox), (from_context), (from_folder), (to_mailbox), (to_context), (to_folder), (number_of_messages), (message_numbers_in), (delete_old))) { \
  256. ast_test_status_update(test, "Succeeded in forwarding message from %s@%s [%s] to %s@%s [%s] when expected result was fail\n", \
  257. (from_mailbox) ? (from_mailbox) : "(NULL)", (from_context) ? (from_context) : "(NULL)", (from_folder) ? (from_folder) : "(NULL)", \
  258. (to_mailbox) ? (to_mailbox) : "(NULL)", (to_context) ? (to_context) : "(NULL)", (to_folder) ? (to_folder) : "(NULL)"); \
  259. VM_API_TEST_CLEANUP; \
  260. return AST_TEST_FAIL; \
  261. } } while (0)
  262. /*!
  263. * \internal .
  264. * \brief Playback a message on a channel or callback function .
  265. * .
  266. * \note The channel name must be test_channel.
  267. * \note Fail the test if the message could not be played.
  268. */
  269. #define VM_API_PLAYBACK_MESSAGE(channel, mailbox, context, folder, message, callback_fn) do { \
  270. if (ast_vm_msg_play((channel), (mailbox), (context), (folder), (message), (callback_fn))) { \
  271. ast_test_status_update(test, "Failed nominal playback message test\n"); \
  272. ast_hangup(test_channel); \
  273. VM_API_TEST_CLEANUP; \
  274. return AST_TEST_FAIL; \
  275. } } while (0)
  276. /*!
  277. * \internal
  278. * \brief Playback a message on a channel or callback function.
  279. *
  280. * \note The channel name must be test_channel.
  281. * \note Fail the test if the message is successfully played
  282. */
  283. #define VM_API_PLAYBACK_MESSAGE_OFF_NOMINAL(channel, mailbox, context, folder, message, callback_fn) do { \
  284. if (!ast_vm_msg_play((channel), (mailbox), (context), (folder), (message), (callback_fn))) { \
  285. ast_test_status_update(test, "Succeeded in playing back of message when expected result was to fail\n"); \
  286. ast_hangup(test_channel); \
  287. VM_API_TEST_CLEANUP; \
  288. return AST_TEST_FAIL; \
  289. } } while (0)
  290. /*!
  291. * \internal
  292. * \brief Possible names of folders.
  293. *
  294. * \note Taken from app_voicemail
  295. */
  296. static const char * const mailbox_folders[] = {
  297. "INBOX",
  298. "Old",
  299. "Work",
  300. "Family",
  301. "Friends",
  302. "Cust1",
  303. "Cust2",
  304. "Cust3",
  305. "Cust4",
  306. "Cust5",
  307. "Deleted",
  308. "Urgent",
  309. };
  310. /*!
  311. * \internal
  312. * \brief Message snapshots representing the messages that are used by the various tests
  313. */
  314. static struct ast_vm_msg_snapshot *test_snapshots[TOTAL_SNAPSHOTS];
  315. /*!
  316. * \internal
  317. * \brief Tracks whether or not we entered into the message playback callback function
  318. */
  319. static int global_entered_playback_callback = 0;
  320. /*!
  321. * \internal
  322. * \brief Get a folder index by its name
  323. */
  324. static int get_folder_by_name(const char *folder)
  325. {
  326. size_t i;
  327. for (i = 0; i < ARRAY_LEN(mailbox_folders); i++) {
  328. if (strcasecmp(folder, mailbox_folders[i]) == 0) {
  329. return i;
  330. }
  331. }
  332. return -1;
  333. }
  334. /*!
  335. * \internal
  336. * \brief Get a mock snapshot object
  337. *
  338. * \param context The mailbox context
  339. * \param exten The mailbox extension
  340. * \param callerid The caller ID of the person leaving the message
  341. *
  342. * \returns an ast_vm_msg_snapshot object on success
  343. * \returns NULL on error
  344. */
  345. static struct ast_vm_msg_snapshot *test_vm_api_create_mock_snapshot(const char *context, const char *exten, const char *callerid)
  346. {
  347. char msg_id_hash[AST_MAX_CONTEXT + AST_MAX_EXTENSION + sizeof(callerid) + 1];
  348. char msg_id_buf[256];
  349. struct ast_vm_msg_snapshot *snapshot;
  350. snprintf(msg_id_hash, sizeof(msg_id_hash), "%s%s%s", exten, context, callerid);
  351. snprintf(msg_id_buf, sizeof(msg_id_buf), "%ld-%d", (long)time(NULL), ast_str_hash(msg_id_hash));
  352. if ((snapshot = ast_calloc(1, sizeof(*snapshot)))) {
  353. if (ast_string_field_init(snapshot, 128)) {
  354. ast_free(snapshot);
  355. return NULL;
  356. }
  357. ast_string_field_set(snapshot, msg_id, msg_id_buf);
  358. ast_string_field_set(snapshot, exten, exten);
  359. ast_string_field_set(snapshot, callerid, callerid);
  360. }
  361. return snapshot;
  362. }
  363. /*!
  364. * \internal
  365. * \brief Destroy a mock snapshot object
  366. */
  367. static void test_vm_api_destroy_mock_snapshot(struct ast_vm_msg_snapshot *snapshot)
  368. {
  369. if (snapshot) {
  370. ast_string_field_free_memory(snapshot);
  371. ast_free(snapshot);
  372. }
  373. }
  374. /*!
  375. * \internal
  376. * \brief Make a voicemail mailbox folder based on the values provided in a message snapshot
  377. *
  378. * \param snapshot The snapshot containing the information to create the folder from
  379. *
  380. * \returns 0 on success
  381. * \returns 1 on failure
  382. */
  383. static int test_vm_api_create_voicemail_folder(const char *folder_path)
  384. {
  385. mode_t mode = VOICEMAIL_DIR_MODE;
  386. int res;
  387. if ((res = ast_mkdir(folder_path, mode))) {
  388. ast_log(AST_LOG_ERROR, "ast_mkdir '%s' failed: %s\n", folder_path, strerror(res));
  389. return 1;
  390. }
  391. return 0;
  392. }
  393. /*!
  394. * \internal
  395. * \brief Create the voicemail files specified by a snapshot
  396. *
  397. * \param context The context of the mailbox
  398. * \param mailbox The actual mailbox
  399. * \param snapshot The message snapshot object containing the relevant envelope data
  400. *
  401. * \note This will symbolic link the sound file 'beep.gsm' to act as the 'sound' portion of the voicemail.
  402. * Certain actions in app_voicemail will fail if an actual sound file does not exist
  403. *
  404. * \returns 0 on success
  405. * \returns 1 on any failure
  406. */
  407. static int test_vm_api_create_voicemail_files(const char *context, const char *mailbox, struct ast_vm_msg_snapshot *snapshot)
  408. {
  409. FILE *msg_file;
  410. char folder_path[PATH_MAX];
  411. char msg_path[PATH_MAX];
  412. char snd_path[PATH_MAX];
  413. char beep_path[PATH_MAX];
  414. /* Note that we create both the text and a dummy sound file here. Without
  415. * the sound file, a number of the voicemail operations 'silently' fail, as it
  416. * does not believe that an actual voicemail exists
  417. */
  418. snprintf(folder_path, sizeof(folder_path), "%s/voicemail/%s/%s/%s",
  419. ast_config_AST_SPOOL_DIR, context, mailbox, snapshot->folder_name);
  420. snprintf(msg_path, sizeof(msg_path), "%s/msg%04u.txt",
  421. folder_path, snapshot->msg_number);
  422. snprintf(snd_path, sizeof(snd_path), "%s/msg%04u.gsm",
  423. folder_path, snapshot->msg_number);
  424. snprintf(beep_path, sizeof(beep_path), "%s/sounds/en/beep.gsm", ast_config_AST_DATA_DIR);
  425. if (test_vm_api_create_voicemail_folder(folder_path)) {
  426. return 1;
  427. }
  428. if (ast_lock_path(folder_path) == AST_LOCK_FAILURE) {
  429. ast_log(AST_LOG_ERROR, "Unable to lock directory %s\n", folder_path);
  430. return 1;
  431. }
  432. if (symlink(beep_path, snd_path)) {
  433. ast_unlock_path(folder_path);
  434. ast_log(AST_LOG_ERROR, "Failed to create a symbolic link from %s to %s: %s\n",
  435. beep_path, snd_path, strerror(errno));
  436. return 1;
  437. }
  438. if (!(msg_file = fopen(msg_path, "w"))) {
  439. /* Attempt to remove the sound file */
  440. unlink(snd_path);
  441. ast_unlock_path(folder_path);
  442. ast_log(AST_LOG_ERROR, "Failed to open %s for writing\n", msg_path);
  443. return 1;
  444. }
  445. fprintf(msg_file, ";\n; Message Information file\n;\n"
  446. "[message]\n"
  447. "origmailbox=%s\n"
  448. "context=%s\n"
  449. "macrocontext=%s\n"
  450. "exten=%s\n"
  451. "rdnis=%s\n"
  452. "priority=%d\n"
  453. "callerchan=%s\n"
  454. "callerid=%s\n"
  455. "origdate=%s\n"
  456. "origtime=%s\n"
  457. "category=%s\n"
  458. "msg_id=%s\n"
  459. "flag=%s\n"
  460. "duration=%s\n",
  461. mailbox,
  462. context,
  463. "",
  464. snapshot->exten,
  465. "unknown",
  466. 1,
  467. snapshot->callerchan,
  468. snapshot->callerid,
  469. snapshot->origdate,
  470. snapshot->origtime,
  471. "",
  472. snapshot->msg_id,
  473. snapshot->flag,
  474. snapshot->duration);
  475. fclose(msg_file);
  476. if (chmod(msg_path, VOICEMAIL_FILE_MODE) < 0) {
  477. ast_unlock_path(folder_path);
  478. ast_log(AST_LOG_ERROR, "Couldn't set permissions on voicemail text file %s: %s", msg_path, strerror(errno));
  479. return 1;
  480. }
  481. ast_unlock_path(folder_path);
  482. return 0;
  483. }
  484. /*!
  485. * \internal
  486. * \brief Destroy the voicemail on the file system associated with a snapshot
  487. *
  488. * \param snapshot The snapshot describing the voicemail
  489. */
  490. static void test_vm_api_remove_voicemail(struct ast_vm_msg_snapshot *snapshot)
  491. {
  492. char msg_path[PATH_MAX];
  493. char snd_path[PATH_MAX];
  494. char folder_path[PATH_MAX];
  495. if (!snapshot) {
  496. return;
  497. }
  498. snprintf(folder_path, sizeof(folder_path), "%s/voicemail/%s/%s/%s",
  499. ast_config_AST_SPOOL_DIR, "default", snapshot->exten, snapshot->folder_name);
  500. snprintf(msg_path, sizeof(msg_path), "%s/msg%04u.txt",
  501. folder_path, snapshot->msg_number);
  502. snprintf(snd_path, sizeof(snd_path), "%s/msg%04u.gsm",
  503. folder_path, snapshot->msg_number);
  504. unlink(msg_path);
  505. unlink(snd_path);
  506. return;
  507. }
  508. /*!
  509. * \internal
  510. * \brief Destroy the voicemails associated with a mailbox snapshot
  511. *
  512. * \param mailbox The actual mailbox name
  513. * \param mailbox_snapshot The mailbox snapshot containing the voicemails to destroy
  514. *
  515. * \note It is necessary to specify not just the snapshot, but the mailbox itself. The
  516. * message snapshots contained in the snapshot may have originated from a different mailbox
  517. * then the one we're destroying, which means that we can't determine the files to delete
  518. * without knowing the actual mailbox they exist in.
  519. */
  520. static void test_vm_api_destroy_mailbox_voicemails(const char *mailbox, struct ast_vm_mailbox_snapshot *mailbox_snapshot)
  521. {
  522. struct ast_vm_msg_snapshot *msg;
  523. int i;
  524. for (i = 0; i < 12; ++i) {
  525. AST_LIST_TRAVERSE(&mailbox_snapshot->snapshots[i], msg, msg) {
  526. ast_string_field_set(msg, exten, mailbox);
  527. test_vm_api_remove_voicemail(msg);
  528. }
  529. }
  530. }
  531. /*!
  532. * \internal
  533. * \brief Use snapshots to remove all messages in the mailboxes
  534. */
  535. static void test_vm_api_remove_all_messages(void)
  536. {
  537. struct ast_vm_mailbox_snapshot *mailbox_snapshot;
  538. /* Take a snapshot of each mailbox and remove the contents. Note that we need to use
  539. * snapshots of the mailboxes in addition to our tracked test snapshots, as there's a good chance
  540. * we've created copies of the snapshots */
  541. if ((mailbox_snapshot = ast_vm_mailbox_snapshot_create("test_vm_api_1234", "default", NULL, 0, AST_VM_SNAPSHOT_SORT_BY_ID, 0))) {
  542. test_vm_api_destroy_mailbox_voicemails("test_vm_api_1234", mailbox_snapshot);
  543. mailbox_snapshot = ast_vm_mailbox_snapshot_destroy(mailbox_snapshot);
  544. } else {
  545. ast_log(AST_LOG_WARNING, "Failed to create mailbox snapshot - could not remove test messages for test_vm_api_1234\n");
  546. }
  547. if ((mailbox_snapshot = ast_vm_mailbox_snapshot_create("test_vm_api_2345", "default", NULL, 0, AST_VM_SNAPSHOT_SORT_BY_ID, 0))) {
  548. test_vm_api_destroy_mailbox_voicemails("test_vm_api_2345", mailbox_snapshot);
  549. mailbox_snapshot = ast_vm_mailbox_snapshot_destroy(mailbox_snapshot);
  550. } else {
  551. ast_log(AST_LOG_WARNING, "Failed to create mailbox snapshot - could not remove test messages for test_vm_api_2345\n");
  552. }
  553. }
  554. /*!
  555. * \internal
  556. * \brief Set up the necessary voicemails for a unit test run
  557. *
  558. * \details
  559. * This creates 4 voicemails, stores them on the file system, and creates snapshot objects
  560. * representing them for expected/actual value comparisons in the array test_snapshots.
  561. *
  562. * test_snapshots[0] => in test_vm_1234@default, folder INBOX, message 0
  563. * test_snapshots[1] => in test_vm_1234@default, folder Old, message 0
  564. * test_snapshots[2] => in test_vm_2345@default, folder INBOX, message 0
  565. * test_snapshots[3] => in test_vm_2345@default, folder Old, message 1
  566. *
  567. * \returns 0 on success
  568. * \returns 1 on failure
  569. */
  570. static int test_vm_api_test_setup(void)
  571. {
  572. int i, res = 0;
  573. struct ast_vm_msg_snapshot *msg_one = NULL;
  574. struct ast_vm_msg_snapshot *msg_two = NULL;
  575. struct ast_vm_msg_snapshot *msg_three = NULL;
  576. struct ast_vm_msg_snapshot *msg_four = NULL;
  577. /* Make the four sample voicemails */
  578. if ( !((msg_one = test_vm_api_create_mock_snapshot("default", "test_vm_api_1234", "\"Phil\" <2000>")))
  579. || !((msg_two = test_vm_api_create_mock_snapshot("default", "test_vm_api_1234", "\"Noel\" <8000>")))
  580. || !((msg_three = test_vm_api_create_mock_snapshot("default", "test_vm_api_2345", "\"Phil\" <2000>")))
  581. || !((msg_four = test_vm_api_create_mock_snapshot("default", "test_vm_api_2345", "\"Bill\" <3000>")))) {
  582. ast_log(AST_LOG_ERROR, "Failed to create mock snapshots for test\n");
  583. test_vm_api_destroy_mock_snapshot(msg_one);
  584. test_vm_api_destroy_mock_snapshot(msg_two);
  585. test_vm_api_destroy_mock_snapshot(msg_three);
  586. test_vm_api_destroy_mock_snapshot(msg_four);
  587. return 1;
  588. }
  589. /* Create the voicemail users */
  590. if (ast_vm_test_create_user("default", "test_vm_api_1234")
  591. || ast_vm_test_create_user("default", "test_vm_api_2345")) {
  592. ast_log(AST_LOG_ERROR, "Failed to create test voicemail users\n");
  593. test_vm_api_destroy_mock_snapshot(msg_one);
  594. test_vm_api_destroy_mock_snapshot(msg_two);
  595. test_vm_api_destroy_mock_snapshot(msg_three);
  596. test_vm_api_destroy_mock_snapshot(msg_four);
  597. /* Note that the cleanup macro will ensure that any test user that
  598. * was successfully created is removed
  599. */
  600. return 1;
  601. }
  602. /* Now that the users exist from the perspective of the voicemail
  603. * application, attempt to remove any existing voicemails
  604. */
  605. test_vm_api_remove_all_messages();
  606. /* Set the basic properties on each */
  607. ast_string_field_set(msg_one, callerchan, "SIP/2000-00000000");
  608. ast_string_field_set(msg_one, origdate, "Mon Mar 19 04:14:21 PM UTC 2012");
  609. ast_string_field_set(msg_one, origtime, "1332173661");
  610. ast_string_field_set(msg_one, duration, "8");
  611. ast_string_field_set(msg_one, folder_name, "Old");
  612. msg_one->msg_number = 0;
  613. test_snapshots[0] = msg_one;
  614. ast_string_field_set(msg_two, callerchan, "SIP/8000-00000001");
  615. ast_string_field_set(msg_two, origdate, "Mon Mar 19 06:16:13 PM UTC 2012");
  616. ast_string_field_set(msg_two, origtime, "1332180973");
  617. ast_string_field_set(msg_two, duration, "24");
  618. ast_string_field_set(msg_two, folder_name, "INBOX");
  619. msg_two->msg_number = 0;
  620. test_snapshots[1] = msg_two;
  621. ast_string_field_set(msg_three, callerchan, "IAX/2000-000000a3");
  622. ast_string_field_set(msg_three, origdate, "Thu Mar 22 23:13:03 PM UTC 2012");
  623. ast_string_field_set(msg_three, origtime, "1332181251");
  624. ast_string_field_set(msg_three, duration, "25");
  625. ast_string_field_set(msg_three, folder_name, "INBOX");
  626. msg_three->msg_number = 0;
  627. test_snapshots[2] = msg_three;
  628. ast_string_field_set(msg_four, callerchan, "DAHDI/3000-00000010");
  629. ast_string_field_set(msg_four, origdate, "Fri Mar 23 03:01:03 AM UTC 2012");
  630. ast_string_field_set(msg_four, origtime, "1332181362");
  631. ast_string_field_set(msg_four, duration, "13");
  632. ast_string_field_set(msg_four, folder_name, "INBOX");
  633. msg_three->msg_number = 1;
  634. test_snapshots[3] = msg_four;
  635. /* Store the messages */
  636. for (i = 0; i < TOTAL_SNAPSHOTS; ++i) {
  637. if (test_vm_api_create_voicemail_files("default", test_snapshots[i]->exten, test_snapshots[i])) {
  638. /* On a failure, the test_vm_api_test_teardown method will remove and
  639. * unlink any created files. Since we failed to create the file, clean
  640. * up the object here instead */
  641. ast_log(AST_LOG_ERROR, "Failed to store voicemail %s/%s\n",
  642. "default", test_snapshots[i]->exten);
  643. test_vm_api_destroy_mock_snapshot(test_snapshots[i]);
  644. test_snapshots[i] = NULL;
  645. res = 1;
  646. }
  647. }
  648. return res;
  649. }
  650. static void test_vm_api_test_teardown(void)
  651. {
  652. int i;
  653. /* Remove our test message snapshots */
  654. for (i = 0; i < TOTAL_SNAPSHOTS; ++i) {
  655. test_vm_api_remove_voicemail(test_snapshots[i]);
  656. test_vm_api_destroy_mock_snapshot(test_snapshots[i]);
  657. test_snapshots[i] = NULL;
  658. }
  659. test_vm_api_remove_all_messages();
  660. /* Remove the test users */
  661. ast_vm_test_destroy_user("default", "test_vm_api_1234");
  662. ast_vm_test_destroy_user("default", "test_vm_api_2345");
  663. }
  664. /*!
  665. * \internal
  666. * \brief Update the test snapshots with a new mailbox snapshot
  667. *
  668. * \param mailbox_snapshot The new mailbox shapshot to update the test snapshots with
  669. */
  670. static void test_vm_api_update_test_snapshots(struct ast_vm_mailbox_snapshot *mailbox_snapshot)
  671. {
  672. int i, j;
  673. struct ast_vm_msg_snapshot *msg;
  674. for (i = 0; i < TOTAL_SNAPSHOTS; ++i) {
  675. for (j = 0; j < 12; ++j) {
  676. AST_LIST_TRAVERSE(&mailbox_snapshot->snapshots[j], msg, msg) {
  677. if (!strcmp(msg->msg_id, test_snapshots[i]->msg_id)) {
  678. ast_string_field_set(test_snapshots[i], callerid, msg->callerid);
  679. ast_string_field_set(test_snapshots[i], callerchan, msg->callerchan);
  680. ast_string_field_set(test_snapshots[i], exten, msg->exten);
  681. ast_string_field_set(test_snapshots[i], origdate, msg->origdate);
  682. ast_string_field_set(test_snapshots[i], origtime, msg->origtime);
  683. ast_string_field_set(test_snapshots[i], duration, msg->duration);
  684. ast_string_field_set(test_snapshots[i], folder_name, msg->folder_name);
  685. ast_string_field_set(test_snapshots[i], flag, msg->flag);
  686. test_snapshots[i]->msg_number = msg->msg_number;
  687. }
  688. }
  689. }
  690. }
  691. }
  692. /*!
  693. * \internal
  694. * \brief A callback function for message playback
  695. *
  696. * \param chan The channel the file would be played back on
  697. * \param file The file to play back
  698. * \param duration The duration of the file
  699. *
  700. * \note This sets global_entered_playback_callback to 1 if the parameters
  701. * passed to the callback are minimally valid
  702. */
  703. static void message_playback_callback_fn(struct ast_channel *chan, const char *file, int duration)
  704. {
  705. if ((chan) && !ast_strlen_zero(file) && duration > 0) {
  706. global_entered_playback_callback = 1;
  707. } else {
  708. ast_log(AST_LOG_WARNING, "Entered into message playback callback function with invalid parameters\n");
  709. }
  710. }
  711. /*!
  712. * \internal
  713. * \brief Dummy channel write function for mock_channel_tech
  714. */
  715. static int test_vm_api_mock_channel_write(struct ast_channel *chan, struct ast_frame *frame)
  716. {
  717. return 0;
  718. }
  719. /*!
  720. * \internal
  721. * \brief Dummy channel read function for mock_channel_tech
  722. */
  723. static struct ast_frame *test_vm_api_mock_channel_read(struct ast_channel *chan)
  724. {
  725. return &ast_null_frame;
  726. }
  727. /*!
  728. * \internal
  729. * \brief A dummy channel technology
  730. */
  731. static const struct ast_channel_tech mock_channel_tech = {
  732. .write = test_vm_api_mock_channel_write,
  733. .read = test_vm_api_mock_channel_read,
  734. };
  735. /*!
  736. * \internal
  737. * \brief Create a dummy channel suitable for 'playing back' gsm sound files on
  738. *
  739. * \returns a channel on success
  740. * \returns NULL on failure
  741. */
  742. static struct ast_channel *test_vm_api_create_mock_channel(void)
  743. {
  744. struct ast_channel *mock_channel;
  745. struct ast_format_cap *native_formats;
  746. if (!(mock_channel = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, "TestChannel"))) {
  747. return NULL;
  748. }
  749. ast_channel_set_writeformat(mock_channel, ast_format_gsm);
  750. native_formats = ast_channel_nativeformats(mock_channel);
  751. ast_format_cap_append(native_formats, ast_channel_writeformat(mock_channel), 0);
  752. ast_channel_set_rawwriteformat(mock_channel, ast_format_gsm);
  753. ast_channel_set_readformat(mock_channel, ast_format_gsm);
  754. ast_channel_set_rawreadformat(mock_channel, ast_format_gsm);
  755. ast_channel_tech_set(mock_channel, &mock_channel_tech);
  756. ast_channel_unlock(mock_channel);
  757. return mock_channel;
  758. }
  759. AST_TEST_DEFINE(voicemail_api_nominal_snapshot)
  760. {
  761. struct ast_vm_mailbox_snapshot *test_mbox_snapshot = NULL;
  762. switch (cmd) {
  763. case TEST_INIT:
  764. info->name = "nominal_snapshot";
  765. info->category = "/main/voicemail_api/";
  766. info->summary = "Nominal mailbox snapshot tests";
  767. info->description =
  768. "Test retrieving mailbox snapshots";
  769. return AST_TEST_NOT_RUN;
  770. case TEST_EXECUTE:
  771. break;
  772. }
  773. VM_API_TEST_SETUP;
  774. ast_test_status_update(test, "Test retrieving message 1 from INBOX of test_vm_1234\n");
  775. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  776. VM_API_INT_VERIFY(1, test_mbox_snapshot->total_msg_num);
  777. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[1], test_mbox_snapshot, "INBOX", 0);
  778. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  779. ast_test_status_update(test, "Test retrieving message 0 from Old of test_vm_1234\n");
  780. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "Old", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  781. VM_API_INT_VERIFY(1, test_mbox_snapshot->total_msg_num);
  782. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[0], test_mbox_snapshot, "Old", 0);
  783. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  784. ast_test_status_update(test, "Test retrieving message 0, 1 from Old and INBOX of test_vm_1234 ordered by time\n");
  785. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 1);
  786. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  787. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[0], test_mbox_snapshot, "INBOX", 0);
  788. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[1], test_mbox_snapshot, "INBOX", 1);
  789. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  790. ast_test_status_update(test, "Test retrieving message 1, 0 from Old and INBOX of test_vm_1234 ordered by time desc\n");
  791. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 1, AST_VM_SNAPSHOT_SORT_BY_TIME, 1);
  792. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  793. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[1], test_mbox_snapshot, "INBOX", 0);
  794. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[0], test_mbox_snapshot, "INBOX", 1);
  795. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  796. ast_test_status_update(test, "Test retrieving message 0, 1 from Old and INBOX of test_vm_1234 ordered by id\n");
  797. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_ID, 1);
  798. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  799. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[1], test_mbox_snapshot, "INBOX", 0);
  800. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[0], test_mbox_snapshot, "INBOX", 1);
  801. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  802. ast_test_status_update(test, "Test retrieving message 1, 0 from Old and INBOX of test_vm_1234 ordered by id desc\n");
  803. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 1, AST_VM_SNAPSHOT_SORT_BY_ID, 1);
  804. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  805. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[0], test_mbox_snapshot, "INBOX", 0);
  806. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[1], test_mbox_snapshot, "INBOX", 1);
  807. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  808. ast_test_status_update(test, "Test retrieving message 0, 1 from all folders of test_vm_1234 ordered by id\n");
  809. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", NULL, 0, AST_VM_SNAPSHOT_SORT_BY_ID, 0);
  810. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  811. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[0], test_mbox_snapshot, "Old", 0);
  812. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[1], test_mbox_snapshot, "INBOX", 0);
  813. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  814. ast_test_status_update(test, "Test retrieving message 0, 1 from all folders of test_vm_1234 ordered by time\n");
  815. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", NULL, 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  816. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  817. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[0], test_mbox_snapshot, "Old", 0);
  818. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[1], test_mbox_snapshot, "INBOX", 0);
  819. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  820. ast_test_status_update(test, "Test retrieving message 0, 1 from all folders of test_vm_1234, default context ordered by time\n");
  821. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", NULL, NULL, 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  822. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  823. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[0], test_mbox_snapshot, "Old", 0);
  824. VM_API_SNAPSHOT_MSG_VERIFY(test_snapshots[1], test_mbox_snapshot, "INBOX", 0);
  825. ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  826. VM_API_TEST_CLEANUP;
  827. return AST_TEST_PASS;
  828. }
  829. AST_TEST_DEFINE(voicemail_api_off_nominal_snapshot)
  830. {
  831. struct ast_vm_mailbox_snapshot *test_mbox_snapshot = NULL;
  832. switch (cmd) {
  833. case TEST_INIT:
  834. info->name = "off_nominal_snapshot";
  835. info->category = "/main/voicemail_api/";
  836. info->summary = "Off nominal mailbox snapshot tests";
  837. info->description =
  838. "Test off nominal requests for mailbox snapshots. This includes"
  839. " testing the following:\n"
  840. " * Access to non-exisstent mailbox\n"
  841. " * Access to NULL mailbox\n"
  842. " * Access to non-existent context\n"
  843. " * Access to non-existent folder\n"
  844. " * Access to NULL folder\n"
  845. " * Invalid sort identifier";
  846. return AST_TEST_NOT_RUN;
  847. case TEST_EXECUTE:
  848. break;
  849. }
  850. VM_API_TEST_SETUP;
  851. ast_test_status_update(test, "Test access to non-existent mailbox test_vm_api_3456\n");
  852. VM_API_SNAPSHOT_OFF_NOMINAL_TEST("test_vm_api_3456", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  853. ast_test_status_update(test, "Test access to null mailbox\n");
  854. VM_API_SNAPSHOT_OFF_NOMINAL_TEST(NULL, "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  855. ast_test_status_update(test, "Test access non-existent context test_vm_api_defunct\n");
  856. VM_API_SNAPSHOT_OFF_NOMINAL_TEST("test_vm_api_1234", "test_vm_api_defunct", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  857. ast_test_status_update(test, "Test non-existent folder test_vm_api_platypus\n");
  858. VM_API_SNAPSHOT_OFF_NOMINAL_TEST("test_vm_api_1234", "default", "test_vm_api_platypus", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  859. VM_API_TEST_CLEANUP;
  860. return AST_TEST_PASS;
  861. }
  862. AST_TEST_DEFINE(voicemail_api_nominal_move)
  863. {
  864. struct ast_vm_mailbox_snapshot *test_mbox_snapshot = NULL;
  865. const char *inbox_msg_id;
  866. const char *old_msg_id;
  867. const char *multi_msg_ids[2];
  868. switch (cmd) {
  869. case TEST_INIT:
  870. info->name = "nominal_move";
  871. info->category = "/main/voicemail_api/";
  872. info->summary = "Nominal move voicemail tests";
  873. info->description =
  874. "Test nominal requests to move a voicemail to a different"
  875. " folder. This includes moving messages given a context,"
  876. " given a NULL context, and moving multiple messages";
  877. return AST_TEST_NOT_RUN;
  878. case TEST_EXECUTE:
  879. break;
  880. }
  881. VM_API_TEST_SETUP;
  882. old_msg_id = test_snapshots[0]->msg_id;
  883. inbox_msg_id = test_snapshots[1]->msg_id;
  884. multi_msg_ids[0] = test_snapshots[2]->msg_id;
  885. multi_msg_ids[1] = test_snapshots[3]->msg_id;
  886. ast_test_status_update(test, "Test move of test_vm_api_1234 message from INBOX to Family\n");
  887. VM_API_MOVE_MESSAGE("test_vm_api_1234", "default", 1, "INBOX", &inbox_msg_id, "Family");
  888. ast_test_status_update(test, "Test move of test_vm_api_1234 message from Old to Family\n");
  889. VM_API_MOVE_MESSAGE("test_vm_api_1234", NULL, 1, "Old", &old_msg_id, "Family");
  890. /* Take a snapshot and update the test snapshots for verification */
  891. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "Family", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  892. test_vm_api_update_test_snapshots(test_mbox_snapshot);
  893. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  894. VM_API_STRING_FIELD_VERIFY("Family", test_snapshots[0]->folder_name);
  895. VM_API_STRING_FIELD_VERIFY("Family", test_snapshots[1]->folder_name);
  896. VM_API_INT_VERIFY(0, test_snapshots[1]->msg_number);
  897. VM_API_INT_VERIFY(1, test_snapshots[0]->msg_number);
  898. /* Move both of the 2345 messages to Family */
  899. ast_test_status_update(test, "Test move of test_vm_api_2345 messages from Inbox to Family\n");
  900. VM_API_MOVE_MESSAGE("test_vm_api_2345", "default", 2, "INBOX", multi_msg_ids, "Family");
  901. /* Take a snapshot and update the test snapshots for verification */
  902. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "Family", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  903. test_vm_api_update_test_snapshots(test_mbox_snapshot);
  904. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  905. VM_API_STRING_FIELD_VERIFY("Family", test_snapshots[2]->folder_name);
  906. VM_API_STRING_FIELD_VERIFY("Family", test_snapshots[3]->folder_name);
  907. ast_test_status_update(test, "Test move of test_vm_api_2345 message from Family to INBOX\n");
  908. VM_API_MOVE_MESSAGE("test_vm_api_2345", "default", 2, "Family", multi_msg_ids, "INBOX");
  909. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  910. test_vm_api_update_test_snapshots(test_mbox_snapshot);
  911. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  912. VM_API_STRING_FIELD_VERIFY("INBOX", test_snapshots[2]->folder_name);
  913. VM_API_STRING_FIELD_VERIFY("INBOX", test_snapshots[3]->folder_name);
  914. VM_API_TEST_CLEANUP;
  915. return AST_TEST_PASS;
  916. }
  917. AST_TEST_DEFINE(voicemail_api_off_nominal_move)
  918. {
  919. const char *inbox_msg_id;
  920. const char *multi_msg_ids[4];
  921. switch (cmd) {
  922. case TEST_INIT:
  923. info->name = "off_nominal_move";
  924. info->category = "/main/voicemail_api/";
  925. info->summary = "Off nominal mailbox message move tests";
  926. info->description =
  927. "Test nominal requests to move a voicemail to a different"
  928. " folder. This includes testing the following:\n"
  929. " * Moving to a non-existent mailbox\n"
  930. " * Moving to a NULL mailbox\n"
  931. " * Moving to a non-existent context\n"
  932. " * Moving to/from non-existent folder\n"
  933. " * Moving to/from NULL folder\n"
  934. " * Invalid message identifier(s)";
  935. return AST_TEST_NOT_RUN;
  936. case TEST_EXECUTE:
  937. break;
  938. }
  939. VM_API_TEST_SETUP;
  940. inbox_msg_id = test_snapshots[1]->msg_id;
  941. multi_msg_ids[0] = test_snapshots[0]->msg_id;
  942. multi_msg_ids[1] = test_snapshots[1]->msg_id;
  943. multi_msg_ids[2] = test_snapshots[2]->msg_id;
  944. multi_msg_ids[3] = test_snapshots[3]->msg_id;
  945. ast_test_status_update(test, "Test move attempt for invalid mailbox test_vm_3456\n");
  946. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_3456", "default", 1, "INBOX", &inbox_msg_id, "Family");
  947. VM_API_MOVE_MESSAGE_OFF_NOMINAL(NULL, "default", 1, "INBOX", &inbox_msg_id, "Family");
  948. ast_test_status_update(test, "Test move attempt for invalid context test_vm_api_defunct\n");
  949. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "test_vm_api_defunct", 1, "INBOX", &inbox_msg_id, "Family");
  950. ast_test_status_update(test, "Test move attempt to invalid folder\n");
  951. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, "INBOX", &inbox_msg_id, "SPAMALOT");
  952. ast_test_status_update(test, "Test move attempt from invalid folder\n");
  953. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, "MEATINACAN", &inbox_msg_id, "Family");
  954. ast_test_status_update(test, "Test move attempt to NULL folder\n");
  955. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, "INBOX", &inbox_msg_id, NULL);
  956. ast_test_status_update(test, "Test move attempt from NULL folder\n");
  957. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, NULL, &inbox_msg_id, "Family");
  958. ast_test_status_update(test, "Test move attempt with non-existent message number\n");
  959. inbox_msg_id = "6";
  960. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, "INBOX", &inbox_msg_id, "Family");
  961. ast_test_status_update(test, "Test move attempt with invalid message number\n");
  962. inbox_msg_id = "";
  963. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, "INBOX", &inbox_msg_id, "Family");
  964. ast_test_status_update(test, "Test move attempt with 0 number of messages\n");
  965. inbox_msg_id = test_snapshots[1]->msg_id;
  966. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 0, "INBOX", &inbox_msg_id, "Family");
  967. ast_test_status_update(test, "Test move attempt with invalid number of messages\n");
  968. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", -30, "INBOX", &inbox_msg_id, "Family");
  969. ast_test_status_update(test, "Test move attempt with non-existent multiple messages, where some messages exist\n");
  970. VM_API_MOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 4, "INBOX", multi_msg_ids, "Family");
  971. VM_API_TEST_CLEANUP;
  972. return AST_TEST_PASS;
  973. }
  974. AST_TEST_DEFINE(voicemail_api_nominal_remove)
  975. {
  976. struct ast_vm_mailbox_snapshot *test_mbox_snapshot = NULL;
  977. const char *inbox_msg_id;
  978. const char *old_msg_id;
  979. const char *multi_msg_ids[2];
  980. switch (cmd) {
  981. case TEST_INIT:
  982. info->name = "nominal_remove";
  983. info->category = "/main/voicemail_api/";
  984. info->summary = "Nominal mailbox remove message tests";
  985. info->description =
  986. "Tests removing messages from voicemail folders. Includes"
  987. " both removing messages one at a time, and in a set";
  988. return AST_TEST_NOT_RUN;
  989. case TEST_EXECUTE:
  990. break;
  991. }
  992. VM_API_TEST_SETUP;
  993. old_msg_id = test_snapshots[0]->msg_id;
  994. inbox_msg_id = test_snapshots[1]->msg_id;
  995. multi_msg_ids[0] = test_snapshots[2]->msg_id;
  996. multi_msg_ids[1] = test_snapshots[3]->msg_id;
  997. ast_test_status_update(test, "Test removing a single message from INBOX\n");
  998. VM_API_REMOVE_MESSAGE("test_vm_api_1234", "default", 1, "INBOX", &inbox_msg_id);
  999. ast_test_status_update(test, "Test removing a single message from Old\n");
  1000. VM_API_REMOVE_MESSAGE("test_vm_api_1234", "default", 1, "Old", &old_msg_id);
  1001. ast_test_status_update(test, "Test removing multiple messages from INBOX\n");
  1002. VM_API_REMOVE_MESSAGE("test_vm_api_2345", "default", 2, "INBOX", multi_msg_ids);
  1003. VM_API_TEST_CLEANUP;
  1004. return AST_TEST_PASS;
  1005. }
  1006. AST_TEST_DEFINE(voicemail_api_off_nominal_remove)
  1007. {
  1008. const char *inbox_msg_id;
  1009. const char *multi_msg_ids[2];
  1010. const char *empty_msg_ids[] = { };
  1011. switch (cmd) {
  1012. case TEST_INIT:
  1013. info->name = "off_nominal_remove";
  1014. info->category = "/main/voicemail_api/";
  1015. info->summary = "Off nominal mailbox message removal tests";
  1016. info->description =
  1017. "Test off nominal requests for removing messages from "
  1018. "a mailbox. This includes:\n"
  1019. " * Removing messages with an invalid mailbox\n"
  1020. " * Removing messages from a NULL mailbox\n"
  1021. " * Removing messages from an invalid context\n"
  1022. " * Removing messages from an invalid folder\n"
  1023. " * Removing messages from a NULL folder\n"
  1024. " * Removing messages with bad identifiers";
  1025. return AST_TEST_NOT_RUN;
  1026. case TEST_EXECUTE:
  1027. break;
  1028. }
  1029. VM_API_TEST_SETUP;
  1030. inbox_msg_id = test_snapshots[1]->msg_id;
  1031. multi_msg_ids[0] = test_snapshots[2]->msg_id;
  1032. multi_msg_ids[1] = test_snapshots[3]->msg_id;
  1033. ast_test_status_update(test, "Test removing a single message with an invalid mailbox\n");
  1034. VM_API_REMOVE_MESSAGE_OFF_NOMINAL("test_vm_api_3456", "default", 1, "INBOX", &inbox_msg_id);
  1035. ast_test_status_update(test, "Test removing a single message with a NULL mailbox\n");
  1036. VM_API_REMOVE_MESSAGE_OFF_NOMINAL(NULL, "default", 1, "INBOX", &inbox_msg_id);
  1037. ast_test_status_update(test, "Test removing a single message with an invalid context\n");
  1038. VM_API_REMOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "defunct", 1, "INBOX", &inbox_msg_id);
  1039. ast_test_status_update(test, "Test removing a single message with an invalid folder\n");
  1040. VM_API_REMOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, "SPAMINACAN", &inbox_msg_id);
  1041. ast_test_status_update(test, "Test removing a single message with a NULL folder\n");
  1042. VM_API_REMOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, NULL, &inbox_msg_id);
  1043. ast_test_status_update(test, "Test removing a single message with an invalid message number\n");
  1044. inbox_msg_id = "POOPOO";
  1045. VM_API_REMOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 1, "INBOX", &inbox_msg_id);
  1046. ast_test_status_update(test, "Test removing multiple messages with a single invalid message number\n");
  1047. multi_msg_ids[1] = "POOPOO";
  1048. VM_API_REMOVE_MESSAGE_OFF_NOMINAL("test_vm_api_2345", "default", 2, "INBOX", multi_msg_ids);
  1049. ast_test_status_update(test, "Test removing no messages with no message numbers\n");
  1050. VM_API_REMOVE_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", 0, "INBOX", empty_msg_ids);
  1051. ast_test_status_update(test, "Test removing multiple messages with an invalid size specifier\n");
  1052. VM_API_REMOVE_MESSAGE_OFF_NOMINAL("test_vm_api_2345", "default", -30, "INBOX", multi_msg_ids);
  1053. VM_API_TEST_CLEANUP;
  1054. return AST_TEST_PASS;
  1055. }
  1056. AST_TEST_DEFINE(voicemail_api_nominal_forward)
  1057. {
  1058. struct ast_vm_mailbox_snapshot *test_mbox_snapshot = NULL;
  1059. const char *inbox_msg_id;
  1060. const char *multi_msg_ids[2];
  1061. switch (cmd) {
  1062. case TEST_INIT:
  1063. info->name = "nominal_forward";
  1064. info->category = "/main/voicemail_api/";
  1065. info->summary = "Nominal message forward tests";
  1066. info->description =
  1067. "Tests the nominal cases of forwarding messages"
  1068. " between mailboxes";
  1069. return AST_TEST_NOT_RUN;
  1070. case TEST_EXECUTE:
  1071. break;
  1072. }
  1073. VM_API_TEST_SETUP;
  1074. inbox_msg_id = test_snapshots[1]->msg_id;
  1075. multi_msg_ids[0] = test_snapshots[2]->msg_id;
  1076. multi_msg_ids[1] = test_snapshots[3]->msg_id;
  1077. ast_test_status_update(test, "Test forwarding message 0 from test_vm_api_1234 INBOX to test_vm_api_2345 INBOX\n");
  1078. VM_API_FORWARD_MESSAGE("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", "default", "INBOX", 1, &inbox_msg_id, 0);
  1079. /* Make sure we didn't delete the message */
  1080. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1081. VM_API_INT_VERIFY(1, test_mbox_snapshot->total_msg_num);
  1082. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1083. /* We should now have a total of 3 messages in test_vm_api_2345 INBOX */
  1084. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1085. VM_API_INT_VERIFY(3, test_mbox_snapshot->total_msg_num);
  1086. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1087. ast_test_status_update(test, "Test forwarding message 0 from test_vm_api_1234 INBOX with default context to test_vm_api_2345 INBOX\n");
  1088. VM_API_FORWARD_MESSAGE("test_vm_api_1234", NULL, "INBOX", "test_vm_api_2345", "default", "INBOX", 1, &inbox_msg_id, 0);
  1089. /* Make sure we didn't delete the message */
  1090. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1091. VM_API_INT_VERIFY(1, test_mbox_snapshot->total_msg_num);
  1092. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1093. /* We should now have a total of 4 messages in test_vm_api_2345 INBOX */
  1094. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1095. VM_API_INT_VERIFY(4, test_mbox_snapshot->total_msg_num);
  1096. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1097. ast_test_status_update(test, "Test forwarding message 0 from test_vm_api_1234 INBOX to test_vm_api_2345 INBOX with default context\n");
  1098. VM_API_FORWARD_MESSAGE("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", NULL, "INBOX", 1, &inbox_msg_id, 0);
  1099. /* Make sure we didn't delete the message */
  1100. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1101. VM_API_INT_VERIFY(1, test_mbox_snapshot->total_msg_num);
  1102. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1103. /* We should now have a total of 5 messages in test_vm_api_2345 INBOX */
  1104. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1105. VM_API_INT_VERIFY(5, test_mbox_snapshot->total_msg_num);
  1106. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1107. ast_test_status_update(test, "Test forwarding message 0 from test_vm_api_1234 INBOX to test_vm_api_2345 INBOX, deleting original\n");
  1108. VM_API_FORWARD_MESSAGE("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", NULL, "INBOX", 1, &inbox_msg_id, 1);
  1109. /* Make sure we deleted the message */
  1110. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1111. VM_API_INT_VERIFY(0, test_mbox_snapshot->total_msg_num);
  1112. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1113. /* We should now have a total of 6 messages in test_vm_api_2345 INBOX */
  1114. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1115. VM_API_INT_VERIFY(6, test_mbox_snapshot->total_msg_num);
  1116. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1117. ast_test_status_update(test, "Test forwarding 2 messages from test_vm_api_2345 INBOX to test_vm_api_1234 INBOX");
  1118. VM_API_FORWARD_MESSAGE("test_vm_api_2345", "default", "INBOX", "test_vm_api_1234", "default", "INBOX", 2, multi_msg_ids, 0);
  1119. /* Make sure we didn't delete the messages */
  1120. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1121. VM_API_INT_VERIFY(6, test_mbox_snapshot->total_msg_num);
  1122. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1123. /* We should now have a total of 2 messages in test_vm_api_1234 INBOX */
  1124. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1125. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  1126. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1127. ast_test_status_update(test, "Test forwarding 2 messages from test_vm_api_2345 INBOX to test_vm_api_1234 Family, deleting original\n");
  1128. VM_API_FORWARD_MESSAGE("test_vm_api_2345", "default", "INBOX", "test_vm_api_1234", "default", "Family", 2, multi_msg_ids, 1);
  1129. /* Make sure we deleted the messages */
  1130. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "INBOX", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1131. VM_API_INT_VERIFY(4, test_mbox_snapshot->total_msg_num);
  1132. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1133. /* We should now have a total of 2 messages in test_vm_api_1234 Family */
  1134. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "Family", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1135. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  1136. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1137. VM_API_TEST_CLEANUP;
  1138. return AST_TEST_PASS;
  1139. }
  1140. AST_TEST_DEFINE(voicemail_api_off_nominal_forward)
  1141. {
  1142. const char *inbox_msg_id;
  1143. const char *multi_msg_ids[4];
  1144. const char *empty_msg_ids[] = { };
  1145. switch (cmd) {
  1146. case TEST_INIT:
  1147. info->name = "off_nominal_forward";
  1148. info->category = "/main/voicemail_api/";
  1149. info->summary = "Off nominal message forwarding tests";
  1150. info->description =
  1151. "Test off nominal forwarding of messages. This includes:\n"
  1152. " * Invalid/NULL from mailbox\n"
  1153. " * Invalid from context\n"
  1154. " * Invalid/NULL from folder\n"
  1155. " * Invalid/NULL to mailbox\n"
  1156. " * Invalid to context\n"
  1157. " * Invalid/NULL to folder\n"
  1158. " * Invalid message numbers\n"
  1159. " * Invalid number of messages";
  1160. return AST_TEST_NOT_RUN;
  1161. case TEST_EXECUTE:
  1162. break;
  1163. }
  1164. VM_API_TEST_SETUP;
  1165. inbox_msg_id = test_snapshots[1]->msg_id;
  1166. multi_msg_ids[0] = test_snapshots[0]->msg_id;
  1167. multi_msg_ids[1] = test_snapshots[1]->msg_id;
  1168. multi_msg_ids[2] = test_snapshots[2]->msg_id;
  1169. multi_msg_ids[3] = test_snapshots[3]->msg_id;
  1170. ast_test_status_update(test, "Test forwarding from an invalid mailbox\n");
  1171. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_3456", "default", "INBOX", "test_vm_api_2345", "default", "INBOX", 1, &inbox_msg_id, 0);
  1172. ast_test_status_update(test, "Test forwarding from a NULL mailbox\n");
  1173. VM_API_FORWARD_MESSAGE_OFF_NOMINAL(NULL, "default", "INBOX", "test_vm_api_2345", "default", "INBOX", 1, &inbox_msg_id, 0);
  1174. ast_test_status_update(test, "Test forwarding from an invalid context\n");
  1175. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "defunct", "INBOX", "test_vm_api_2345", "default", "INBOX", 1, &inbox_msg_id, 0);
  1176. ast_test_status_update(test, "Test forwarding from an invalid folder\n");
  1177. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "POTTEDMEAT", "test_vm_api_2345", "default", "INBOX", 1, &inbox_msg_id, 0);
  1178. ast_test_status_update(test, "Test forwarding from a NULL folder\n");
  1179. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", NULL, "test_vm_api_2345", "default", "INBOX", 1, &inbox_msg_id, 0);
  1180. ast_test_status_update(test, "Test forwarding to an invalid mailbox\n");
  1181. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "INBOX", "test_vm_api_3456", "default", "INBOX", 1, &inbox_msg_id, 0);
  1182. ast_test_status_update(test, "Test forwarding to a NULL mailbox\n");
  1183. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "INBOX", NULL, "default", "INBOX", 1, &inbox_msg_id, 0);
  1184. ast_test_status_update(test, "Test forwarding to an invalid context\n");
  1185. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", "defunct", "INBOX", 1, &inbox_msg_id, 0);
  1186. ast_test_status_update(test, "Test forwarding to an invalid folder\n");
  1187. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", "default", "POTTEDMEAT", 1, &inbox_msg_id, 0);
  1188. ast_test_status_update(test, "Test forwarding to a NULL folder\n");
  1189. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", "default", NULL, 1, &inbox_msg_id, 0);
  1190. ast_test_status_update(test, "Test forwarding when no messages are select\n");
  1191. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", "default", "INBOX", 0, empty_msg_ids, 0);
  1192. ast_test_status_update(test, "Test forwarding a message that doesn't exist\n");
  1193. inbox_msg_id = "POOPOO";
  1194. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", "default", "INBOX", 1, &inbox_msg_id, 0);
  1195. ast_test_status_update(test, "Test forwarding multiple messages, where some messages don't exist\n");
  1196. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_2345", "default", "INBOX", "test_vm_api_1234", "default", "INBOX", 4, multi_msg_ids, 0);
  1197. ast_test_status_update(test, "Test forwarding a message with an invalid size specifier\n");
  1198. VM_API_FORWARD_MESSAGE_OFF_NOMINAL("test_vm_api_1234", "default", "INBOX", "test_vm_api_2345", "default", "INBOX", -30, &inbox_msg_id, 0);
  1199. VM_API_TEST_CLEANUP;
  1200. return AST_TEST_PASS;
  1201. }
  1202. AST_TEST_DEFINE(voicemail_api_nominal_msg_playback)
  1203. {
  1204. struct ast_vm_mailbox_snapshot *test_mbox_snapshot = NULL;
  1205. struct ast_channel *test_channel;
  1206. const char *message_id_1234;
  1207. const char *message_id_2345[2];
  1208. switch (cmd) {
  1209. case TEST_INIT:
  1210. info->name = "nominal_msg_playback";
  1211. info->category = "/main/voicemail_api/";
  1212. info->summary = "Nominal message playback";
  1213. info->description =
  1214. "Tests playing back a message on a provided"
  1215. " channel or callback function";
  1216. return AST_TEST_NOT_RUN;
  1217. case TEST_EXECUTE:
  1218. break;
  1219. }
  1220. VM_API_TEST_SETUP;
  1221. message_id_1234 = test_snapshots[1]->msg_id;
  1222. message_id_2345[0] = test_snapshots[2]->msg_id;
  1223. message_id_2345[1] = test_snapshots[3]->msg_id;
  1224. if (!(test_channel = test_vm_api_create_mock_channel())) {
  1225. ast_log(AST_LOG_ERROR, "Failed to create mock channel for testing\n");
  1226. VM_API_TEST_CLEANUP;
  1227. return AST_TEST_FAIL;
  1228. }
  1229. ast_test_status_update(test, "Playing back message from test_vm_api_1234 to mock channel\n");
  1230. VM_API_PLAYBACK_MESSAGE(test_channel, "test_vm_api_1234", "default", "INBOX", message_id_1234, NULL);
  1231. ast_test_status_update(test, "Playing back message from test_vm_api_2345 to callback function\n");
  1232. VM_API_PLAYBACK_MESSAGE(test_channel, "test_vm_api_2345", "default", "INBOX", message_id_2345[0], &message_playback_callback_fn);
  1233. VM_API_INT_VERIFY(1, global_entered_playback_callback);
  1234. global_entered_playback_callback = 0;
  1235. ast_test_status_update(test, "Playing back message from test_vm_api_2345 to callback function with default context\n");
  1236. VM_API_PLAYBACK_MESSAGE(test_channel, "test_vm_api_2345", NULL, "INBOX", message_id_2345[1], &message_playback_callback_fn);
  1237. VM_API_INT_VERIFY(1, global_entered_playback_callback);
  1238. global_entered_playback_callback = 0;
  1239. VM_API_SNAPSHOT_CREATE("test_vm_api_1234", "default", "Old", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1240. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  1241. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1242. VM_API_SNAPSHOT_CREATE("test_vm_api_2345", "default", "Old", 0, AST_VM_SNAPSHOT_SORT_BY_TIME, 0);
  1243. VM_API_INT_VERIFY(2, test_mbox_snapshot->total_msg_num);
  1244. test_mbox_snapshot = ast_vm_mailbox_snapshot_destroy(test_mbox_snapshot);
  1245. ast_hangup(test_channel);
  1246. VM_API_TEST_CLEANUP;
  1247. return AST_TEST_PASS;
  1248. }
  1249. AST_TEST_DEFINE(voicemail_api_off_nominal_msg_playback)
  1250. {
  1251. struct ast_channel *test_channel;
  1252. const char *msg_id;
  1253. const char *invalid_msg_id = "POOPOO";
  1254. switch (cmd) {
  1255. case TEST_INIT:
  1256. info->name = "off_nominal_msg_playback";
  1257. info->category = "/main/voicemail_api/";
  1258. info->summary = "Off nominal message playback";
  1259. info->description =
  1260. "Tests off nominal conditions in playing back a "
  1261. "message. This includes:\n"
  1262. " * Invalid/NULL mailbox\n"
  1263. " * Invalid context\n"
  1264. " * Invalid/NULL folder\n"
  1265. " * Invalid message identifiers";
  1266. return AST_TEST_NOT_RUN;
  1267. case TEST_EXECUTE:
  1268. break;
  1269. }
  1270. VM_API_TEST_SETUP;
  1271. msg_id = test_snapshots[0]->msg_id;
  1272. if (!(test_channel = test_vm_api_create_mock_channel())) {
  1273. ast_log(AST_LOG_ERROR, "Failed to create mock channel for testing\n");
  1274. VM_API_TEST_CLEANUP;
  1275. return AST_TEST_FAIL;
  1276. }
  1277. ast_test_status_update(test, "Playing back message from invalid mailbox\n");
  1278. VM_API_PLAYBACK_MESSAGE_OFF_NOMINAL(test_channel, "test_vm_api_3456", "default", "INBOX", msg_id, NULL);
  1279. ast_test_status_update(test, "Playing back message from NULL mailbox\n");
  1280. VM_API_PLAYBACK_MESSAGE_OFF_NOMINAL(test_channel, NULL, "default", "INBOX", msg_id, NULL);
  1281. ast_test_status_update(test, "Playing back message from invalid context\n");
  1282. VM_API_PLAYBACK_MESSAGE_OFF_NOMINAL(test_channel, "test_vm_api_1234", "defunct", "INBOX", msg_id, NULL);
  1283. ast_test_status_update(test, "Playing back message from invalid folder\n");
  1284. VM_API_PLAYBACK_MESSAGE_OFF_NOMINAL(test_channel, "test_vm_api_1234", "default", "BACON", msg_id, NULL);
  1285. ast_test_status_update(test, "Playing back message from NULL folder\n");
  1286. VM_API_PLAYBACK_MESSAGE_OFF_NOMINAL(test_channel, "test_vm_api_1234", "default", NULL, msg_id, NULL);
  1287. ast_test_status_update(test, "Playing back message with invalid message specifier\n");
  1288. VM_API_PLAYBACK_MESSAGE_OFF_NOMINAL(test_channel, "test_vm_api_1234", "default", "INBOX", invalid_msg_id, NULL);
  1289. ast_test_status_update(test, "Playing back message with NULL message specifier\n");
  1290. VM_API_PLAYBACK_MESSAGE_OFF_NOMINAL(test_channel, "test_vm_api_1234", "default", "INBOX", NULL, NULL);
  1291. ast_hangup(test_channel);
  1292. VM_API_TEST_CLEANUP;
  1293. return AST_TEST_PASS;
  1294. }
  1295. static int unload_module(void)
  1296. {
  1297. /* Snapshot tests */
  1298. AST_TEST_UNREGISTER(voicemail_api_nominal_snapshot);
  1299. AST_TEST_UNREGISTER(voicemail_api_off_nominal_snapshot);
  1300. /* Move Tests */
  1301. AST_TEST_UNREGISTER(voicemail_api_nominal_move);
  1302. AST_TEST_UNREGISTER(voicemail_api_off_nominal_move);
  1303. /* Remove Tests */
  1304. AST_TEST_UNREGISTER(voicemail_api_nominal_remove);
  1305. AST_TEST_UNREGISTER(voicemail_api_off_nominal_remove);
  1306. /* Forward Tests */
  1307. AST_TEST_UNREGISTER(voicemail_api_nominal_forward);
  1308. AST_TEST_UNREGISTER(voicemail_api_off_nominal_forward);
  1309. /* Message Playback Tests */
  1310. AST_TEST_UNREGISTER(voicemail_api_nominal_msg_playback);
  1311. AST_TEST_UNREGISTER(voicemail_api_off_nominal_msg_playback);
  1312. return 0;
  1313. }
  1314. static int load_module(void)
  1315. {
  1316. /* Snapshot tests */
  1317. AST_TEST_REGISTER(voicemail_api_nominal_snapshot);
  1318. AST_TEST_REGISTER(voicemail_api_off_nominal_snapshot);
  1319. /* Move Tests */
  1320. AST_TEST_REGISTER(voicemail_api_nominal_move);
  1321. AST_TEST_REGISTER(voicemail_api_off_nominal_move);
  1322. /* Remove Tests */
  1323. AST_TEST_REGISTER(voicemail_api_nominal_remove);
  1324. AST_TEST_REGISTER(voicemail_api_off_nominal_remove);
  1325. /* Forward Tests */
  1326. AST_TEST_REGISTER(voicemail_api_nominal_forward);
  1327. AST_TEST_REGISTER(voicemail_api_off_nominal_forward);
  1328. /* Message Playback Tests */
  1329. AST_TEST_REGISTER(voicemail_api_nominal_msg_playback);
  1330. AST_TEST_REGISTER(voicemail_api_off_nominal_msg_playback);
  1331. return AST_MODULE_LOAD_SUCCESS;
  1332. }
  1333. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Core Voicemail API Tests");