test_utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@digium.com>
  7. * Russell Bryant <russell@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*!
  20. * \file
  21. * \brief Unit Tests for utils API
  22. *
  23. * \author David Vossel <dvossel@digium.com>
  24. * \author Russell Bryant <russell@digium.com>
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
  32. #include "asterisk/utils.h"
  33. #include "asterisk/test.h"
  34. #include "asterisk/crypto.h"
  35. #include "asterisk/adsi.h"
  36. #include "asterisk/agi.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/module.h"
  39. AST_TEST_DEFINE(uri_encode_decode_test)
  40. {
  41. int res = AST_TEST_PASS;
  42. const char *in = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
  43. const char *expected1 = "abcdefghijklmnopurstuvwxyz%20ABCDEFGHIJKLMNOPQRSTUVWXYZ%201234567890%20~%60!%40%23%24%25%5E%26*()_-%2B%3D%7B%5B%7D%5D%7C%5C%3A%3B%22'%3C%2C%3E.%3F%2F";
  44. const char *expected2 = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%25^&*()_-+={[}]|\\:;\"'<,>.?/";
  45. char out[256] = { 0 };
  46. switch (cmd) {
  47. case TEST_INIT:
  48. info->name = "uri_encode_decode_test";
  49. info->category = "/main/utils/";
  50. info->summary = "encode and decode a hex escaped string";
  51. info->description = "encode a string, verify encoded string matches what we expect. Decode the encoded string, verify decoded string matches the original string.";
  52. return AST_TEST_NOT_RUN;
  53. case TEST_EXECUTE:
  54. break;
  55. }
  56. ast_test_status_update(test, "Input before executing ast_uri_encode:\n%s\n", in);
  57. ast_test_status_update(test, "Output expected for ast_uri_encode with enabling do_special_char: %s\n", expected1);
  58. ast_test_status_update(test, "Output expected for ast_uri_encode with out enabling do_special_char: %s\n\n", expected2);
  59. /* Test with do_special_char enabled */
  60. ast_uri_encode(in, out, sizeof(out), 1);
  61. ast_test_status_update(test, "Output after enabling do_special_char:\n%s\n", out);
  62. if (strcmp(expected1, out)) {
  63. ast_test_status_update(test, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n");
  64. res = AST_TEST_FAIL;
  65. }
  66. /* Verify uri decode matches original */
  67. ast_uri_decode(out);
  68. if (strcmp(in, out)) {
  69. ast_test_status_update(test, "Decoded string did not match original input\n");
  70. res = AST_TEST_FAIL;
  71. } else {
  72. ast_test_status_update(test, "Decoded string matched original input\n");
  73. }
  74. /* Test with do_special_char disabled */
  75. out[0] = '\0';
  76. ast_uri_encode(in, out, sizeof(out), 0);
  77. ast_test_status_update(test, "Output after disabling do_special_char: %s\n", out);
  78. if (strcmp(expected2, out)) {
  79. ast_test_status_update(test, "ENCODE DOES NOT MATCH EXPECTED, FAIL\n");
  80. res = AST_TEST_FAIL;
  81. }
  82. /* Verify uri decode matches original */
  83. ast_uri_decode(out);
  84. if (strcmp(in, out)) {
  85. ast_test_status_update(test, "Decoded string did not match original input\n");
  86. res = AST_TEST_FAIL;
  87. } else {
  88. ast_test_status_update(test, "Decoded string matched original input\n");
  89. }
  90. return res;
  91. }
  92. AST_TEST_DEFINE(quoted_escape_test)
  93. {
  94. int res = AST_TEST_PASS;
  95. const char *in = "a\"bcdefg\"hijkl\\mnopqrs tuv\twxyz";
  96. char out[256] = { 0 };
  97. char small[4] = { 0 };
  98. int i;
  99. static struct {
  100. char *buf;
  101. const size_t buflen;
  102. const char *output;
  103. } tests[] = {
  104. {0, sizeof(out),
  105. "a\\\"bcdefg\\\"hijkl\\\\mnopqrs tuv\twxyz"},
  106. {0, sizeof(small),
  107. "a\\\""},
  108. };
  109. tests[0].buf = out;
  110. tests[1].buf = small;
  111. switch (cmd) {
  112. case TEST_INIT:
  113. info->name = "quoted_escape_test";
  114. info->category = "/main/utils/";
  115. info->summary = "escape a quoted string";
  116. info->description = "Escape a string to be quoted and check the result.";
  117. return AST_TEST_NOT_RUN;
  118. case TEST_EXECUTE:
  119. break;
  120. }
  121. for (i = 0; i < ARRAY_LEN(tests); i++) {
  122. ast_escape_quoted(in, tests[i].buf, tests[i].buflen);
  123. if (strcmp(tests[i].output, tests[i].buf)) {
  124. ast_test_status_update(test, "ESCAPED DOES NOT MATCH EXPECTED, FAIL\n");
  125. ast_test_status_update(test, "original: %s\n", in);
  126. ast_test_status_update(test, "expected: %s\n", tests[i].output);
  127. ast_test_status_update(test, "result: %s\n", tests[i].buf);
  128. res = AST_TEST_FAIL;
  129. }
  130. }
  131. return res;
  132. }
  133. AST_TEST_DEFINE(md5_test)
  134. {
  135. static const struct {
  136. const char *input;
  137. const char *expected_output;
  138. } tests[] = {
  139. { "apples", "daeccf0ad3c1fc8c8015205c332f5b42" },
  140. { "bananas", "ec121ff80513ae58ed478d5c5787075b" },
  141. { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" },
  142. };
  143. enum ast_test_result_state res = AST_TEST_PASS;
  144. int i;
  145. switch (cmd) {
  146. case TEST_INIT:
  147. info->name = "md5_test";
  148. info->category = "/main/utils/";
  149. info->summary = "MD5 test";
  150. info->description =
  151. "This test exercises MD5 calculations."
  152. "";
  153. return AST_TEST_NOT_RUN;
  154. case TEST_EXECUTE:
  155. break;
  156. }
  157. ast_test_status_update(test, "Testing MD5 ...\n");
  158. for (i = 0; i < ARRAY_LEN(tests); i++) {
  159. char md5_hash[32];
  160. ast_md5_hash(md5_hash, tests[i].input);
  161. if (strcasecmp(md5_hash, tests[i].expected_output)) {
  162. ast_test_status_update(test,
  163. "input: '%s' hash: '%s' expected hash: '%s'\n",
  164. tests[i].input, md5_hash, tests[i].expected_output);
  165. res = AST_TEST_FAIL;
  166. }
  167. }
  168. return res;
  169. }
  170. AST_TEST_DEFINE(sha1_test)
  171. {
  172. static const struct {
  173. const char *input;
  174. const char *expected_output;
  175. } tests[] = {
  176. { "giraffe",
  177. "fac8f1a31d2998734d6a5253e49876b8e6a08239" },
  178. { "platypus",
  179. "1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" },
  180. { "ParastratiosphecomyiaStratiosphecomyioides",
  181. "58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" },
  182. };
  183. enum ast_test_result_state res = AST_TEST_PASS;
  184. int i;
  185. switch (cmd) {
  186. case TEST_INIT:
  187. info->name = "sha1_test";
  188. info->category = "/main/utils/";
  189. info->summary = "SHA1 test";
  190. info->description =
  191. "This test exercises SHA1 calculations."
  192. "";
  193. return AST_TEST_NOT_RUN;
  194. case TEST_EXECUTE:
  195. break;
  196. }
  197. ast_test_status_update(test, "Testing SHA1 ...\n");
  198. for (i = 0; i < ARRAY_LEN(tests); i++) {
  199. char sha1_hash[64];
  200. ast_sha1_hash(sha1_hash, tests[i].input);
  201. if (strcasecmp(sha1_hash, tests[i].expected_output)) {
  202. ast_test_status_update(test,
  203. "input: '%s' hash: '%s' expected hash: '%s'\n",
  204. tests[i].input, sha1_hash, tests[i].expected_output);
  205. res = AST_TEST_FAIL;
  206. }
  207. }
  208. return res;
  209. }
  210. AST_TEST_DEFINE(base64_test)
  211. {
  212. static const struct {
  213. const char *input;
  214. const char *decoded;
  215. } tests[] = {
  216. { "giraffe",
  217. "Z2lyYWZmZQ==" },
  218. { "platypus",
  219. "cGxhdHlwdXM=" },
  220. { "ParastratiosphecomyiaStratiosphecomyioides",
  221. "UGFyYXN0cmF0aW9zcGhlY29teWlhU3RyYXRpb3NwaGVjb215aW9pZGVz" },
  222. };
  223. int i;
  224. enum ast_test_result_state res = AST_TEST_PASS;
  225. switch (cmd) {
  226. case TEST_INIT:
  227. info->name = "base64_test";
  228. info->category = "/main/utils/";
  229. info->summary = "base64 test";
  230. info->description = "This test exercises the base64 conversions.";
  231. return AST_TEST_NOT_RUN;
  232. case TEST_EXECUTE:
  233. break;
  234. }
  235. for (i = 0; i < ARRAY_LEN(tests); i++) {
  236. char tmp[64];
  237. ast_base64encode(tmp, (unsigned char *)tests[i].input, strlen(tests[i].input), sizeof(tmp));
  238. if (strcasecmp(tmp, tests[i].decoded)) {
  239. ast_test_status_update(test,
  240. "input: '%s' base64 output: '%s' expected base64 output: '%s'\n",
  241. tests[i].input, tmp, tests[i].decoded);
  242. res = AST_TEST_FAIL;
  243. }
  244. memset(tmp, 0, sizeof(tmp));
  245. ast_base64decode((unsigned char *) tmp, tests[i].decoded, (sizeof(tmp) - 1));
  246. if (strcasecmp(tmp, tests[i].input)) {
  247. ast_test_status_update(test,
  248. "base64 input: '%s' output: '%s' expected output: '%s'\n",
  249. tests[i].decoded, tmp, tests[i].input);
  250. res = AST_TEST_FAIL;
  251. }
  252. }
  253. return res;
  254. }
  255. AST_TEST_DEFINE(crypto_loaded_test)
  256. {
  257. switch (cmd) {
  258. case TEST_INIT:
  259. info->name = "crypto_loaded_test";
  260. info->category = "/res/crypto/";
  261. info->summary = "Crypto loaded into memory";
  262. info->description = "Verifies whether the crypto functions overrode the stubs";
  263. return AST_TEST_NOT_RUN;
  264. case TEST_EXECUTE:
  265. break;
  266. }
  267. #if 0 /* Not defined on Solaris */
  268. ast_test_status_update(test,
  269. "address of __stub__ast_crypto_loaded is %p\n",
  270. __stub__ast_crypto_loaded);
  271. #ifndef HAVE_ATTRIBUTE_weak_import
  272. ast_test_status_update(test,
  273. "address of __ref__ast_crypto_loaded is %p\n",
  274. __ref__ast_crypto_loaded);
  275. #endif
  276. ast_test_status_update(test,
  277. "pointer to ast_crypto_loaded is %p\n",
  278. ast_crypto_loaded);
  279. #endif
  280. return ast_crypto_loaded() ? AST_TEST_PASS : AST_TEST_FAIL;
  281. }
  282. AST_TEST_DEFINE(adsi_loaded_test)
  283. {
  284. struct ast_channel c = { .adsicpe = AST_ADSI_AVAILABLE, };
  285. switch (cmd) {
  286. case TEST_INIT:
  287. info->name = "adsi_loaded_test";
  288. info->category = "/res/adsi/";
  289. info->summary = "ADSI loaded into memory";
  290. info->description = "Verifies whether the adsi functions overrode the stubs";
  291. return AST_TEST_NOT_RUN;
  292. case TEST_EXECUTE:
  293. break;
  294. }
  295. return ast_adsi_available(&c) ? AST_TEST_PASS : AST_TEST_FAIL;
  296. }
  297. static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, const char * const argv[])
  298. {
  299. ast_agi_send(agi->fd, chan, "200 result=0\n");
  300. return RESULT_SUCCESS;
  301. }
  302. AST_TEST_DEFINE(agi_loaded_test)
  303. {
  304. int res = AST_TEST_PASS;
  305. struct agi_command noop_command =
  306. { { "testnoop", NULL }, handle_noop, NULL, NULL, 0 };
  307. switch (cmd) {
  308. case TEST_INIT:
  309. info->name = "agi_loaded_test";
  310. info->category = "/res/agi/";
  311. info->summary = "AGI loaded into memory";
  312. info->description = "Verifies whether the agi functions overrode the stubs";
  313. return AST_TEST_NOT_RUN;
  314. case TEST_EXECUTE:
  315. break;
  316. }
  317. #if 0
  318. ast_test_status_update(test,
  319. "address of __stub__ast_agi_register is %p\n",
  320. __stub__ast_agi_register);
  321. #ifndef HAVE_ATTRIBUTE_weak_import
  322. ast_test_status_update(test,
  323. "address of __ref__ast_agi_register is %p\n",
  324. __ref__ast_agi_register);
  325. #endif
  326. ast_test_status_update(test,
  327. "pointer to ast_agi_register is %p\n",
  328. ast_agi_register);
  329. #endif
  330. if (ast_agi_register(ast_module_info->self, &noop_command) == AST_OPTIONAL_API_UNAVAILABLE) {
  331. ast_test_status_update(test, "Unable to register testnoop command, because res_agi is not loaded.\n");
  332. return AST_TEST_FAIL;
  333. }
  334. #ifndef HAVE_NULLSAFE_PRINTF
  335. /* Test for condition without actually crashing Asterisk */
  336. if (noop_command.usage == NULL) {
  337. ast_test_status_update(test, "AGI testnoop usage was not updated properly.\n");
  338. res = AST_TEST_FAIL;
  339. }
  340. if (noop_command.syntax == NULL) {
  341. ast_test_status_update(test, "AGI testnoop syntax was not updated properly.\n");
  342. res = AST_TEST_FAIL;
  343. }
  344. #endif
  345. ast_agi_unregister(ast_module_info->self, &noop_command);
  346. return res;
  347. }
  348. static int unload_module(void)
  349. {
  350. AST_TEST_UNREGISTER(uri_encode_decode_test);
  351. AST_TEST_UNREGISTER(quoted_escape_test);
  352. AST_TEST_UNREGISTER(md5_test);
  353. AST_TEST_UNREGISTER(sha1_test);
  354. AST_TEST_UNREGISTER(base64_test);
  355. AST_TEST_UNREGISTER(crypto_loaded_test);
  356. AST_TEST_UNREGISTER(adsi_loaded_test);
  357. AST_TEST_UNREGISTER(agi_loaded_test);
  358. return 0;
  359. }
  360. static int load_module(void)
  361. {
  362. AST_TEST_REGISTER(uri_encode_decode_test);
  363. AST_TEST_REGISTER(quoted_escape_test);
  364. AST_TEST_REGISTER(md5_test);
  365. AST_TEST_REGISTER(sha1_test);
  366. AST_TEST_REGISTER(base64_test);
  367. AST_TEST_REGISTER(crypto_loaded_test);
  368. AST_TEST_REGISTER(adsi_loaded_test);
  369. AST_TEST_REGISTER(agi_loaded_test);
  370. return AST_MODULE_LOAD_SUCCESS;
  371. }
  372. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Utils test module");