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