switch_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  3. * Copyright (C) 2005-2018, Anthony Minessale II <anthm@freeswitch.org>
  4. *
  5. * Version: MPL 1.1
  6. *
  7. * The contents of this file are subject to the Mozilla Public License Version
  8. * 1.1 (the "License"); you may not use this file except in compliance with
  9. * the License. You may obtain a copy of the License at
  10. * http://www.mozilla.org/MPL/
  11. *
  12. * Software distributed under the License is distributed on an "AS IS" basis,
  13. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. * for the specific language governing rights and limitations under the
  15. * License.
  16. *
  17. * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Anthony Minessale II <anthm@freeswitch.org>
  21. * Portions created by the Initial Developer are Copyright (C)
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Chris Rienzo <chris@signalwire.com>
  26. * Seven Du <dujinfang@gmail.com>
  27. *
  28. *
  29. * switch_core.c -- tests core functions
  30. *
  31. */
  32. #include <switch.h>
  33. #include <test/switch_test.h>
  34. #if defined(HAVE_OPENSSL)
  35. #include <openssl/ssl.h>
  36. #endif
  37. #define ENABLE_SNPRINTFV_TESTS 0 /* Do not turn on for CI as this requires a lot of RAM */
  38. FST_CORE_BEGIN("./conf")
  39. {
  40. FST_SUITE_BEGIN(switch_core)
  41. {
  42. FST_SETUP_BEGIN()
  43. {
  44. switch_core_set_variable("spawn_instead_of_system", "false");
  45. }
  46. FST_SETUP_END()
  47. FST_TEARDOWN_BEGIN()
  48. {
  49. }
  50. FST_TEARDOWN_END()
  51. #if ENABLE_SNPRINTFV_TESTS
  52. FST_TEST_BEGIN(test_snprintfv_1)
  53. {
  54. size_t src_buf_size = 0x100000001;
  55. char* src = calloc(1, src_buf_size);
  56. if (!src) {
  57. printf("bad allocation\n");
  58. return -1;
  59. }
  60. src[0] = '\xc0';
  61. memset(src + 1, '\x80', 0xffffffff);
  62. char dst[256];
  63. switch_snprintfv(dst, 256, "'%!q'", src);
  64. free(src);
  65. }
  66. FST_TEST_END()
  67. FST_TEST_BEGIN(test_snprintfv_2)
  68. {
  69. #define STR_LEN ((0x100000001 - 3) / 2)
  70. char* src = calloc(1, STR_LEN + 1); /* Account for NULL byte. */
  71. if (!src) { return -1; }
  72. memset(src, 'a', STR_LEN);
  73. char* dst = calloc(1, STR_LEN + 3); /* Account for extra quotes and NULL byte */
  74. if (!dst) { return -1; }
  75. switch_snprintfv(dst, 2 * STR_LEN + 3, "'%q'", src);
  76. free(src);
  77. free(dst);
  78. }
  79. FST_TEST_END()
  80. #endif
  81. FST_TEST_BEGIN(test_switch_is_number_in_range)
  82. {
  83. fst_check_int_equals(switch_is_uint_in_range("x5", 0, 10), SWITCH_FALSE);
  84. fst_check_int_equals(switch_is_uint_in_range("0", 1, 10), SWITCH_FALSE);
  85. fst_check_int_equals(switch_is_uint_in_range("-11", -10, 10), SWITCH_FALSE);
  86. fst_check_int_equals(switch_is_uint_in_range("-10", -10, 10), SWITCH_FALSE);
  87. fst_check_int_equals(switch_is_uint_in_range("-5", -10, 10), SWITCH_FALSE);
  88. fst_check_int_equals(switch_is_uint_in_range("-5", -10, 10), SWITCH_FALSE);
  89. fst_check_int_equals(switch_is_uint_in_range("5", -10, 10), SWITCH_FALSE);
  90. fst_check_int_equals(switch_is_uint_in_range("0", 0, 10), SWITCH_TRUE);
  91. fst_check_int_equals(switch_is_uint_in_range("10", 0, 10), SWITCH_TRUE);
  92. fst_check_int_equals(switch_is_uint_in_range("11", 0, 10), SWITCH_FALSE);
  93. }
  94. FST_TEST_END()
  95. FST_TEST_BEGIN(test_md5)
  96. {
  97. char digest[SWITCH_MD5_DIGEST_STRING_SIZE] = { 0 };
  98. char test_string[] = "test";
  99. switch_status_t status;
  100. status = switch_md5_string(digest, (void *)test_string, strlen(test_string));
  101. fst_check_int_equals(status, SWITCH_STATUS_SUCCESS);
  102. fst_check_string_equals(digest, "098f6bcd4621d373cade4e832627b4f6");
  103. }
  104. FST_TEST_END()
  105. FST_TEST_BEGIN(test_switch_event_add_header_leak)
  106. {
  107. switch_event_t* event;
  108. if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_CALLSTATE) == SWITCH_STATUS_SUCCESS) {
  109. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Call-State-Number[0]", "1");
  110. switch_event_fire(&event);
  111. }
  112. if (switch_event_create(&event, SWITCH_EVENT_CHANNEL_CALLSTATE) == SWITCH_STATUS_SUCCESS) {
  113. switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Channel-Call-State-Number[5000]", "12");
  114. switch_event_fire(&event);
  115. }
  116. }
  117. FST_TEST_END()
  118. FST_TEST_BEGIN(test_xml_free_attr)
  119. {
  120. switch_xml_t parent_xml = switch_xml_new("xml");
  121. switch_xml_t xml = switch_xml_add_child_d(parent_xml, "test", 1);
  122. switch_xml_set_attr(xml, "a1", "v1");
  123. switch_xml_set_attr_d(xml, "a2", "v2");
  124. switch_xml_free(parent_xml);
  125. }
  126. FST_TEST_END()
  127. FST_TEST_BEGIN(test_xml_set_attr)
  128. {
  129. switch_xml_t parent_xml = switch_xml_new("xml");
  130. switch_xml_t xml = switch_xml_add_child_d(parent_xml, "test", 1);
  131. switch_xml_set_attr_d(xml, "test1", "1");
  132. switch_xml_set_attr(xml, "a1", "v1");
  133. switch_xml_set_attr_d(xml, "a2", "v2");
  134. switch_xml_set_attr(xml, "test1", NULL);
  135. switch_xml_set_attr_d(xml, "test2", "2");
  136. switch_xml_set_attr_d(xml, "a3", "v3");
  137. switch_xml_set_attr(xml, "test2", NULL);
  138. switch_xml_set_attr(xml, "a1", NULL);
  139. switch_xml_set_attr(xml, "a2", NULL);
  140. switch_xml_set_attr(xml, "a3", NULL);
  141. switch_xml_free(parent_xml);
  142. }
  143. FST_TEST_END()
  144. #ifdef HAVE_OPENSSL
  145. FST_TEST_BEGIN(test_md5)
  146. {
  147. char *digest_name = "md5";
  148. char *digest_str = NULL;
  149. const char *str = "test data";
  150. unsigned int outputlen;
  151. switch_status_t status = switch_digest_string(digest_name, &digest_str, str, strlen(str), &outputlen);
  152. fst_check_int_equals(status, SWITCH_STATUS_SUCCESS);
  153. fst_check_string_equals(digest_str, "eb733a00c0c9d336e65691a37ab54293");
  154. switch_safe_free(digest_str);
  155. }
  156. FST_TEST_END()
  157. FST_TEST_BEGIN(test_sha256)
  158. {
  159. char *digest_name = "sha256";
  160. char *digest_str = NULL;
  161. const char *str = "test data";
  162. unsigned int outputlen;
  163. switch_status_t status = switch_digest_string(digest_name, &digest_str, str, strlen(str), &outputlen);
  164. fst_check_int_equals(status, SWITCH_STATUS_SUCCESS);
  165. fst_check_string_equals(digest_str, "916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9");
  166. switch_safe_free(digest_str);
  167. }
  168. FST_TEST_END()
  169. #endif
  170. #if OPENSSL_VERSION_NUMBER >= 0x10101000L
  171. FST_TEST_BEGIN(test_sha512_256)
  172. {
  173. char *digest_name = "sha512-256";
  174. char *digest_str = NULL;
  175. const char *str = "test data";
  176. unsigned int outputlen;
  177. switch_status_t status = switch_digest_string(digest_name, &digest_str, str, strlen(str), &outputlen);
  178. fst_check_int_equals(status, SWITCH_STATUS_SUCCESS);
  179. fst_check_string_equals(digest_str, "9fe875600168548c1954aed4f03974ce06b3e17f03a70980190da2d7ef937a43");
  180. switch_safe_free(digest_str);
  181. }
  182. FST_TEST_END()
  183. #endif
  184. #ifndef WIN32
  185. FST_TEST_BEGIN(test_fork)
  186. {
  187. switch_stream_handle_t exec_result = { 0 };
  188. SWITCH_STANDARD_STREAM(exec_result);
  189. fst_requires(switch_stream_system_fork("ip ad sh", &exec_result) == 0);
  190. fst_requires(!zstr(exec_result.data));
  191. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", (char *)exec_result.data);
  192. fst_requires(switch_stream_system_fork("ip ad sh | grep link", &exec_result) == 0);
  193. fst_requires(!zstr(exec_result.data));
  194. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", (char *)exec_result.data);
  195. switch_safe_free(exec_result.data);
  196. }
  197. FST_TEST_END()
  198. #endif
  199. FST_TEST_BEGIN(test_non_fork_exec_set)
  200. {
  201. char *var_test = switch_core_get_variable_dup("test");
  202. char *var_default_password = switch_core_get_variable_dup("default_password");
  203. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "global_getvar test: %s\n", switch_str_nil(var_test));
  204. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "global_getvar default_password: %s\n", switch_str_nil(var_default_password));
  205. fst_check_string_not_equals(var_test, "");
  206. fst_check_string_not_equals(var_default_password, "");
  207. fst_check_string_equals(var_test, var_default_password);
  208. switch_safe_free(var_test);
  209. switch_safe_free(var_default_password);
  210. }
  211. FST_TEST_END()
  212. FST_TEST_BEGIN(test_switch_sockaddr_new)
  213. {
  214. int type = SOCK_DGRAM;
  215. switch_port_t port = 12044;
  216. const char *ip = "127.0.0.1";
  217. switch_memory_pool_t *pool = NULL;
  218. switch_sockaddr_t *local_addr = NULL;
  219. switch_socket_t *sock = NULL;
  220. switch_bool_t r = SWITCH_FALSE;
  221. if (switch_core_new_memory_pool(&pool) == SWITCH_STATUS_SUCCESS) {
  222. if (switch_sockaddr_new(&local_addr, ip, port, pool) == SWITCH_STATUS_SUCCESS) {
  223. if (switch_socket_create(&sock, switch_sockaddr_get_family(local_addr), type, 0, pool) == SWITCH_STATUS_SUCCESS) {
  224. if (switch_socket_bind(sock, local_addr) == SWITCH_STATUS_SUCCESS) {
  225. r = SWITCH_TRUE;
  226. }
  227. switch_socket_close(sock);
  228. }
  229. }
  230. switch_core_destroy_memory_pool(&pool);
  231. }
  232. fst_check_int_equals(r, SWITCH_TRUE);
  233. }
  234. FST_TEST_END()
  235. FST_TEST_BEGIN(test_switch_spawn)
  236. {
  237. #ifdef __linux__
  238. int status;
  239. switch_stream_handle_t stream = { 0 };
  240. status = switch_spawn("echo CHECKING_BAD_FILE_DESCRIPTOR", SWITCH_TRUE);
  241. fst_check_int_equals(status, 0);
  242. SWITCH_STANDARD_STREAM(stream);
  243. status = switch_stream_spawn("echo DEADBEEF", SWITCH_FALSE, SWITCH_TRUE, &stream);
  244. fst_check_int_equals(status, 0);
  245. fst_check_string_equals(stream.data, "DEADBEEF\n");
  246. switch_safe_free(stream.data);
  247. SWITCH_STANDARD_STREAM(stream);
  248. status = switch_stream_spawn("echo DEADBEEF", SWITCH_FALSE, SWITCH_FALSE, &stream);
  249. fst_check_int_equals(status, 0);
  250. fst_check_string_equals(stream.data, "DEADBEEF\n");
  251. switch_safe_free(stream.data);
  252. printf("\nExpected warning check ... ");
  253. status = switch_spawn("false", SWITCH_TRUE);
  254. fct_chk_neq_int(status, 0);
  255. status = switch_spawn("false", SWITCH_FALSE);
  256. fct_chk_eq_int(status, 0);
  257. status = switch_spawn("true", SWITCH_TRUE);
  258. fct_chk_eq_int(status, 0);
  259. #endif
  260. }
  261. FST_TEST_END()
  262. FST_TEST_BEGIN(test_switch_spawn_instead_of_system)
  263. {
  264. #ifdef __linux__
  265. int status;
  266. char file_uuid[SWITCH_UUID_FORMATTED_LENGTH + 1] = { 0 };
  267. const char *filename = NULL;
  268. const char *cmd = NULL;
  269. // tell FS core to use posix_spawn() instead of popen() and friends
  270. switch_core_set_variable("spawn_instead_of_system", "true");
  271. // echo text to a file using shell redirection- this will ensure the command was executed in a shell, as expected
  272. switch_uuid_str(file_uuid, sizeof(file_uuid));
  273. filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "%s", SWITCH_GLOBAL_dirs.temp_dir, file_uuid);
  274. cmd = switch_core_sprintf(fst_pool, "echo test_switch_spawn_instead_of_system with spaces > %s", filename);
  275. status = switch_system(cmd, SWITCH_TRUE);
  276. fst_check_int_equals(status, 0);
  277. fst_xcheck(status == 0, "Expect switch_system() command to return 0");
  278. fst_xcheck(switch_file_exists(filename, fst_pool) == SWITCH_STATUS_SUCCESS, "Expect switch_system() to use shell to create file via > redirection");
  279. unlink(filename);
  280. // verify exec-set works- see conf/freeswitch.xml for test setup of shell_exec_set_test global variable
  281. fst_check_string_equals(switch_core_get_variable("shell_exec_set_test"), "usr");
  282. #endif
  283. }
  284. FST_TEST_END()
  285. FST_TEST_BEGIN(test_switch_safe_atoXX)
  286. {
  287. fst_check_int_equals(switch_safe_atoi("1", 0), 1);
  288. fst_check_int_equals(switch_safe_atoi("", 2), 0);
  289. fst_check_int_equals(switch_safe_atoi(0, 3), 3);
  290. fst_check_int_equals(switch_safe_atol("9275806", 0), 9275806);
  291. fst_check_int_equals(switch_safe_atol("", 2), 0);
  292. fst_check_int_equals(switch_safe_atol(0, 3), 3);
  293. fst_check_int_equals(switch_safe_atoll("9275806", 0), 9275806);
  294. fst_check_int_equals(switch_safe_atoll("", 2), 0);
  295. fst_check_int_equals(switch_safe_atoll(0, 3), 3);
  296. }
  297. FST_TEST_END()
  298. FST_TEST_BEGIN(test_switch_core_hash_insert_dup)
  299. {
  300. char *magicnumber = malloc(9);
  301. switch_hash_index_t *hi;
  302. switch_hash_t *hash = NULL;
  303. void *hash_val;
  304. switch_core_hash_init(&hash);
  305. fst_requires(hash);
  306. snprintf(magicnumber, 9, "%s", "DEADBEEF");
  307. switch_core_hash_insert_dup(hash, "test", (const char *)magicnumber);
  308. snprintf(magicnumber, 9, "%s", "BAADF00D");
  309. hi = switch_core_hash_first(hash);
  310. switch_core_hash_this(hi, NULL, NULL, &hash_val);
  311. fst_check_string_equals(hash_val, "DEADBEEF");
  312. switch_safe_free(hash_val);
  313. free(magicnumber);
  314. free(hi);
  315. switch_core_hash_destroy(&hash);
  316. fst_requires(hash == NULL);
  317. }
  318. FST_TEST_END()
  319. FST_TEST_BEGIN(test_switch_core_hash_insert_alloc)
  320. {
  321. char *item;
  322. switch_hash_index_t *hi;
  323. switch_hash_t *hash = NULL;
  324. void *hash_val;
  325. switch_core_hash_init(&hash);
  326. fst_requires(hash);
  327. item = switch_core_hash_insert_alloc(hash, "test", 10);
  328. fst_requires(item);
  329. snprintf(item, 9, "%s", "DEADBEEF");
  330. hi = switch_core_hash_first(hash);
  331. switch_core_hash_this(hi, NULL, NULL, &hash_val);
  332. fst_check_string_equals(hash_val, "DEADBEEF");
  333. free(hi);
  334. switch_core_hash_destroy(&hash);
  335. fst_requires(hash == NULL);
  336. free(item);
  337. }
  338. FST_TEST_END()
  339. FST_TEST_BEGIN(test_switch_core_hash_insert_pointer)
  340. {
  341. int i, sum = 0;
  342. switch_hash_index_t *hi;
  343. switch_hash_t *hash = NULL;
  344. switch_core_hash_init(&hash);
  345. fst_requires(hash);
  346. for (i = 0; i < 10; i++) {
  347. int *num = malloc(sizeof(int));
  348. *num = i;
  349. fst_check_int_equals(switch_core_hash_insert_pointer(hash, (void*)num), SWITCH_STATUS_SUCCESS);
  350. }
  351. i = 0;
  352. for (hi = switch_core_hash_first(hash); hi; hi = switch_core_hash_next(&hi)) {
  353. void *hash_val;
  354. switch_core_hash_this(hi, NULL, NULL, &hash_val);
  355. sum += *(int*)hash_val;
  356. free(hash_val);
  357. i++;
  358. }
  359. fst_check_int_equals(i, 10);
  360. fst_check_int_equals(sum, 45);
  361. switch_core_hash_destroy(&hash);
  362. fst_requires(hash == NULL);
  363. }
  364. FST_TEST_END()
  365. FST_SESSION_BEGIN(test_switch_channel_get_variable_strdup)
  366. {
  367. const char *val;
  368. switch_channel_t *channel = switch_core_session_get_channel(fst_session);
  369. fst_check(channel);
  370. switch_channel_set_variable(channel, "test_var", "test_value");
  371. fst_check(!switch_channel_get_variable_strdup(channel, "test_var_does_not_exist"));
  372. val = switch_channel_get_variable_strdup(channel, "test_var");
  373. fst_check(val);
  374. fst_check_string_equals(val, "test_value");
  375. free((char *)val);
  376. }
  377. FST_SESSION_END()
  378. FST_SESSION_BEGIN(test_switch_channel_get_variable_buf)
  379. {
  380. char buf[16] = { 0 };
  381. switch_channel_t *channel = switch_core_session_get_channel(fst_session);
  382. fst_check(channel);
  383. switch_channel_set_variable(channel, "test_var", "test_value");
  384. fst_check(switch_channel_get_variable_buf(channel, "test_var", buf, sizeof(buf)) == SWITCH_STATUS_SUCCESS);
  385. fst_check_string_equals(buf, "test_value");
  386. fst_check(switch_channel_get_variable_buf(channel, "test_var_does_not_exist", buf, sizeof(buf)) == SWITCH_STATUS_FALSE);
  387. }
  388. FST_SESSION_END()
  389. }
  390. FST_SUITE_END()
  391. }
  392. FST_CORE_END()