test_voicemail_api.c 60 KB

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