2
0

switch_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. FST_CORE_BEGIN("./conf")
  38. {
  39. FST_SUITE_BEGIN(switch_core)
  40. {
  41. FST_SETUP_BEGIN()
  42. {
  43. switch_core_set_variable("spawn_instead_of_system", "false");
  44. }
  45. FST_SETUP_END()
  46. FST_TEARDOWN_BEGIN()
  47. {
  48. }
  49. FST_TEARDOWN_END()
  50. FST_TEST_BEGIN(test_xml_free_attr)
  51. {
  52. switch_xml_t parent_xml = switch_xml_new("xml");
  53. switch_xml_t xml = switch_xml_add_child_d(parent_xml, "test", 1);
  54. switch_xml_set_attr(xml, "a1", "v1");
  55. switch_xml_set_attr_d(xml, "a2", "v2");
  56. switch_xml_free(parent_xml);
  57. }
  58. FST_TEST_END()
  59. FST_TEST_BEGIN(test_xml_set_attr)
  60. {
  61. switch_xml_t parent_xml = switch_xml_new("xml");
  62. switch_xml_t xml = switch_xml_add_child_d(parent_xml, "test", 1);
  63. switch_xml_set_attr_d(xml, "test1", "1");
  64. switch_xml_set_attr(xml, "a1", "v1");
  65. switch_xml_set_attr_d(xml, "a2", "v2");
  66. switch_xml_set_attr(xml, "test1", NULL);
  67. switch_xml_set_attr_d(xml, "test2", "2");
  68. switch_xml_set_attr_d(xml, "a3", "v3");
  69. switch_xml_set_attr(xml, "test2", NULL);
  70. switch_xml_set_attr(xml, "a1", NULL);
  71. switch_xml_set_attr(xml, "a2", NULL);
  72. switch_xml_set_attr(xml, "a3", NULL);
  73. switch_xml_free(parent_xml);
  74. }
  75. FST_TEST_END()
  76. #ifdef HAVE_OPENSSL
  77. FST_TEST_BEGIN(test_md5)
  78. {
  79. char *digest_name = "md5";
  80. char *digest_str = NULL;
  81. const char *str = "test data";
  82. unsigned int outputlen;
  83. switch_status_t status = switch_digest_string(digest_name, &digest_str, str, strlen(str), &outputlen);
  84. fst_check_int_equals(status, SWITCH_STATUS_SUCCESS);
  85. fst_check_string_equals(digest_str, "eb733a00c0c9d336e65691a37ab54293");
  86. switch_safe_free(digest_str);
  87. }
  88. FST_TEST_END()
  89. FST_TEST_BEGIN(test_sha256)
  90. {
  91. char *digest_name = "sha256";
  92. char *digest_str = NULL;
  93. const char *str = "test data";
  94. unsigned int outputlen;
  95. switch_status_t status = switch_digest_string(digest_name, &digest_str, str, strlen(str), &outputlen);
  96. fst_check_int_equals(status, SWITCH_STATUS_SUCCESS);
  97. fst_check_string_equals(digest_str, "916f0027a575074ce72a331777c3478d6513f786a591bd892da1a577bf2335f9");
  98. switch_safe_free(digest_str);
  99. }
  100. FST_TEST_END()
  101. #endif
  102. #if OPENSSL_VERSION_NUMBER >= 0x10101000L
  103. FST_TEST_BEGIN(test_sha512_256)
  104. {
  105. char *digest_name = "sha512-256";
  106. char *digest_str = NULL;
  107. const char *str = "test data";
  108. unsigned int outputlen;
  109. switch_status_t status = switch_digest_string(digest_name, &digest_str, str, strlen(str), &outputlen);
  110. fst_check_int_equals(status, SWITCH_STATUS_SUCCESS);
  111. fst_check_string_equals(digest_str, "9fe875600168548c1954aed4f03974ce06b3e17f03a70980190da2d7ef937a43");
  112. switch_safe_free(digest_str);
  113. }
  114. FST_TEST_END()
  115. #endif
  116. #ifndef WIN32
  117. FST_TEST_BEGIN(test_fork)
  118. {
  119. switch_stream_handle_t exec_result = { 0 };
  120. SWITCH_STANDARD_STREAM(exec_result);
  121. fst_requires(switch_stream_system_fork("ip ad sh", &exec_result) == 0);
  122. fst_requires(!zstr(exec_result.data));
  123. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", (char *)exec_result.data);
  124. fst_requires(switch_stream_system_fork("ip ad sh | grep link", &exec_result) == 0);
  125. fst_requires(!zstr(exec_result.data));
  126. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", (char *)exec_result.data);
  127. switch_safe_free(exec_result.data);
  128. }
  129. FST_TEST_END()
  130. #endif
  131. FST_TEST_BEGIN(test_non_fork_exec_set)
  132. {
  133. char *var_test = switch_core_get_variable_dup("test");
  134. char *var_default_password = switch_core_get_variable_dup("default_password");
  135. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "global_getvar test: %s\n", switch_str_nil(var_test));
  136. switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "global_getvar default_password: %s\n", switch_str_nil(var_default_password));
  137. fst_check_string_not_equals(var_test, "");
  138. fst_check_string_not_equals(var_default_password, "");
  139. fst_check_string_equals(var_test, var_default_password);
  140. switch_safe_free(var_test);
  141. switch_safe_free(var_default_password);
  142. }
  143. FST_TEST_END()
  144. FST_TEST_BEGIN(test_switch_sockaddr_new)
  145. {
  146. int type = SOCK_DGRAM;
  147. switch_port_t port = 12044;
  148. const char *ip = "127.0.0.1";
  149. switch_memory_pool_t *pool = NULL;
  150. switch_sockaddr_t *local_addr = NULL;
  151. switch_socket_t *sock = NULL;
  152. switch_bool_t r = SWITCH_FALSE;
  153. if (switch_core_new_memory_pool(&pool) == SWITCH_STATUS_SUCCESS) {
  154. if (switch_sockaddr_new(&local_addr, ip, port, pool) == SWITCH_STATUS_SUCCESS) {
  155. if (switch_socket_create(&sock, switch_sockaddr_get_family(local_addr), type, 0, pool) == SWITCH_STATUS_SUCCESS) {
  156. if (switch_socket_bind(sock, local_addr) == SWITCH_STATUS_SUCCESS) {
  157. r = SWITCH_TRUE;
  158. }
  159. switch_socket_close(sock);
  160. }
  161. }
  162. switch_core_destroy_memory_pool(&pool);
  163. }
  164. fst_check_int_equals(r, SWITCH_TRUE);
  165. }
  166. FST_TEST_END()
  167. FST_TEST_BEGIN(test_switch_spawn)
  168. {
  169. #ifdef __linux__
  170. int status;
  171. switch_stream_handle_t stream = { 0 };
  172. status = switch_spawn("echo CHECKING_BAD_FILE_DESCRIPTOR", SWITCH_TRUE);
  173. fst_check_int_equals(status, 0);
  174. SWITCH_STANDARD_STREAM(stream);
  175. status = switch_stream_spawn("echo DEADBEEF", SWITCH_FALSE, SWITCH_TRUE, &stream);
  176. fst_check_int_equals(status, 0);
  177. fst_check_string_equals(stream.data, "DEADBEEF\n");
  178. switch_safe_free(stream.data);
  179. SWITCH_STANDARD_STREAM(stream);
  180. status = switch_stream_spawn("echo DEADBEEF", SWITCH_FALSE, SWITCH_FALSE, &stream);
  181. fst_check_int_equals(status, 0);
  182. fst_check_string_equals(stream.data, "DEADBEEF\n");
  183. switch_safe_free(stream.data);
  184. printf("\nExpected warning check ... ");
  185. status = switch_spawn("false", SWITCH_TRUE);
  186. fct_chk_neq_int(status, 0);
  187. status = switch_spawn("false", SWITCH_FALSE);
  188. fct_chk_eq_int(status, 0);
  189. status = switch_spawn("true", SWITCH_TRUE);
  190. fct_chk_eq_int(status, 0);
  191. #endif
  192. }
  193. FST_TEST_END()
  194. FST_TEST_BEGIN(test_switch_spawn_instead_of_system)
  195. {
  196. #ifdef __linux__
  197. int status;
  198. char file_uuid[SWITCH_UUID_FORMATTED_LENGTH + 1] = { 0 };
  199. const char *filename = NULL;
  200. const char *cmd = NULL;
  201. // tell FS core to use posix_spawn() instead of popen() and friends
  202. switch_core_set_variable("spawn_instead_of_system", "true");
  203. // echo text to a file using shell redirection- this will ensure the command was executed in a shell, as expected
  204. switch_uuid_str(file_uuid, sizeof(file_uuid));
  205. filename = switch_core_sprintf(fst_pool, "%s" SWITCH_PATH_SEPARATOR "%s", SWITCH_GLOBAL_dirs.temp_dir, file_uuid);
  206. cmd = switch_core_sprintf(fst_pool, "echo test_switch_spawn_instead_of_system with spaces > %s", filename);
  207. status = switch_system(cmd, SWITCH_TRUE);
  208. fst_check_int_equals(status, 0);
  209. fst_xcheck(status == 0, "Expect switch_system() command to return 0");
  210. fst_xcheck(switch_file_exists(filename, fst_pool) == SWITCH_STATUS_SUCCESS, "Expect switch_system() to use shell to create file via > redirection");
  211. unlink(filename);
  212. // verify exec-set works- see conf/freeswitch.xml for test setup of shell_exec_set_test global variable
  213. fst_check_string_equals(switch_core_get_variable("shell_exec_set_test"), "usr");
  214. #endif
  215. }
  216. FST_TEST_END()
  217. FST_TEST_BEGIN(test_switch_safe_atoXX)
  218. {
  219. fst_check_int_equals(switch_safe_atoi("1", 0), 1);
  220. fst_check_int_equals(switch_safe_atoi("", 2), 0);
  221. fst_check_int_equals(switch_safe_atoi(0, 3), 3);
  222. fst_check_int_equals(switch_safe_atol("9275806", 0), 9275806);
  223. fst_check_int_equals(switch_safe_atol("", 2), 0);
  224. fst_check_int_equals(switch_safe_atol(0, 3), 3);
  225. fst_check_int_equals(switch_safe_atoll("9275806", 0), 9275806);
  226. fst_check_int_equals(switch_safe_atoll("", 2), 0);
  227. fst_check_int_equals(switch_safe_atoll(0, 3), 3);
  228. }
  229. FST_TEST_END()
  230. FST_TEST_BEGIN(test_switch_core_hash_insert_dup)
  231. {
  232. char *magicnumber = malloc(9);
  233. switch_hash_index_t *hi;
  234. switch_hash_t *hash = NULL;
  235. void *hash_val;
  236. switch_core_hash_init(&hash);
  237. fst_requires(hash);
  238. snprintf(magicnumber, 9, "%s", "DEADBEEF");
  239. switch_core_hash_insert_dup(hash, "test", (const char *)magicnumber);
  240. snprintf(magicnumber, 9, "%s", "BAADF00D");
  241. hi = switch_core_hash_first(hash);
  242. switch_core_hash_this(hi, NULL, NULL, &hash_val);
  243. fst_check_string_equals(hash_val, "DEADBEEF");
  244. switch_safe_free(hash_val);
  245. free(magicnumber);
  246. free(hi);
  247. switch_core_hash_destroy(&hash);
  248. fst_requires(hash == NULL);
  249. }
  250. FST_TEST_END()
  251. FST_TEST_BEGIN(test_switch_core_hash_insert_alloc)
  252. {
  253. char *item;
  254. switch_hash_index_t *hi;
  255. switch_hash_t *hash = NULL;
  256. void *hash_val;
  257. switch_core_hash_init(&hash);
  258. fst_requires(hash);
  259. item = switch_core_hash_insert_alloc(hash, "test", 10);
  260. fst_requires(item);
  261. snprintf(item, 9, "%s", "DEADBEEF");
  262. hi = switch_core_hash_first(hash);
  263. switch_core_hash_this(hi, NULL, NULL, &hash_val);
  264. fst_check_string_equals(hash_val, "DEADBEEF");
  265. free(hi);
  266. switch_core_hash_destroy(&hash);
  267. fst_requires(hash == NULL);
  268. free(item);
  269. }
  270. FST_TEST_END()
  271. FST_TEST_BEGIN(test_switch_core_hash_insert_pointer)
  272. {
  273. int i, sum = 0;
  274. switch_hash_index_t *hi;
  275. switch_hash_t *hash = NULL;
  276. switch_core_hash_init(&hash);
  277. fst_requires(hash);
  278. for (i = 0; i < 10; i++) {
  279. int *num = malloc(sizeof(int));
  280. *num = i;
  281. fst_check_int_equals(switch_core_hash_insert_pointer(hash, (void*)num), SWITCH_STATUS_SUCCESS);
  282. }
  283. i = 0;
  284. for (hi = switch_core_hash_first(hash); hi; hi = switch_core_hash_next(&hi)) {
  285. void *hash_val;
  286. switch_core_hash_this(hi, NULL, NULL, &hash_val);
  287. sum += *(int*)hash_val;
  288. free(hash_val);
  289. i++;
  290. }
  291. fst_check_int_equals(i, 10);
  292. fst_check_int_equals(sum, 45);
  293. switch_core_hash_destroy(&hash);
  294. fst_requires(hash == NULL);
  295. }
  296. FST_TEST_END()
  297. }
  298. FST_SUITE_END()
  299. }
  300. FST_CORE_END()