test_utils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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>extended</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. char out[256] = { 0 };
  44. char small[4] = { 0 };
  45. const struct ast_flags none = {0};
  46. int i = 0;
  47. static struct {
  48. const char *spec_str;
  49. struct ast_flags spec;
  50. char *buf;
  51. size_t buflen;
  52. const char *input;
  53. const char *output;
  54. const char *decoded_output;
  55. } tests[5];
  56. #define INIT_ENCODE_TEST(s, buffer, in, out, dec_out) do { \
  57. if (i < ARRAY_LEN(tests)) { \
  58. tests[i].spec_str = #s; \
  59. tests[i].spec = s; \
  60. tests[i].buf = buffer; \
  61. tests[i].buflen = sizeof(buffer); \
  62. tests[i].input = in; \
  63. tests[i].output = out; \
  64. tests[i].decoded_output = dec_out; \
  65. i++; \
  66. } else { \
  67. ast_test_status_update(test, "error: 'tests' array too small\n"); \
  68. res = AST_TEST_FAIL; \
  69. } \
  70. } while (0)
  71. INIT_ENCODE_TEST(ast_uri_http, out, in,
  72. "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", in);
  73. INIT_ENCODE_TEST(ast_uri_http_legacy, out, in,
  74. "abcdefghijklmnopurstuvwxyz+ABCDEFGHIJKLMNOPQRSTUVWXYZ+1234567890+~%60!%40%23%24%25%5E%26*()_-%2B%3D%7B%5B%7D%5D%7C%5C%3A%3B%22'%3C%2C%3E.%3F%2F", in);
  75. INIT_ENCODE_TEST(ast_uri_sip_user, out, in,
  76. "abcdefghijklmnopurstuvwxyz%20ABCDEFGHIJKLMNOPQRSTUVWXYZ%201234567890%20~%60!%40%23$%25%5E&*()_-+=%7B%5B%7D%5D%7C%5C%3A;%22'%3C,%3E.?/", in);
  77. INIT_ENCODE_TEST(none, small, in, "%61", "a");
  78. INIT_ENCODE_TEST(ast_uri_http, small, in, "abc", "abc");
  79. switch (cmd) {
  80. case TEST_INIT:
  81. info->name = "uri_encode_decode_test";
  82. info->category = "/main/utils/";
  83. info->summary = "encode and decode a hex escaped string";
  84. info->description = "encode a string, verify encoded string matches what we expect. Decode the encoded string, verify decoded string matches the original string.";
  85. return AST_TEST_NOT_RUN;
  86. case TEST_EXECUTE:
  87. break;
  88. }
  89. for (i = 0; i < ARRAY_LEN(tests); i++) {
  90. ast_uri_encode(tests[i].input, tests[i].buf, tests[i].buflen, tests[i].spec);
  91. if (strcmp(tests[i].output, tests[i].buf)) {
  92. ast_test_status_update(test, "encoding with %s did not match expected output, FAIL\n", tests[i].spec_str);
  93. ast_test_status_update(test, "original: %s\n", tests[i].input);
  94. ast_test_status_update(test, "expected: %s\n", tests[i].output);
  95. ast_test_status_update(test, "result: %s\n", tests[i].buf);
  96. res = AST_TEST_FAIL;
  97. continue;
  98. }
  99. ast_uri_decode(tests[i].buf, tests[i].spec);
  100. if (strcmp(tests[i].decoded_output, tests[i].buf)) {
  101. ast_test_status_update(test, "decoding with %s did not match the original input (or expected decoded output)\n", tests[i].spec_str);
  102. ast_test_status_update(test, "original: %s\n", tests[i].input);
  103. ast_test_status_update(test, "expected: %s\n", tests[i].decoded_output);
  104. ast_test_status_update(test, "decoded: %s\n", tests[i].buf);
  105. res = AST_TEST_FAIL;
  106. }
  107. }
  108. return res;
  109. }
  110. AST_TEST_DEFINE(quoted_escape_test)
  111. {
  112. int res = AST_TEST_PASS;
  113. const char *in = "a\"bcdefg\"hijkl\\mnopqrs tuv\twxyz";
  114. char out[256] = { 0 };
  115. char small[4] = { 0 };
  116. int i;
  117. static struct {
  118. char *buf;
  119. const size_t buflen;
  120. const char *output;
  121. } tests[] = {
  122. {0, sizeof(out),
  123. "a\\\"bcdefg\\\"hijkl\\\\mnopqrs tuv\twxyz"},
  124. {0, sizeof(small),
  125. "a\\\""},
  126. };
  127. tests[0].buf = out;
  128. tests[1].buf = small;
  129. switch (cmd) {
  130. case TEST_INIT:
  131. info->name = "quoted_escape_test";
  132. info->category = "/main/utils/";
  133. info->summary = "escape a quoted string";
  134. info->description = "Escape a string to be quoted and check the result.";
  135. return AST_TEST_NOT_RUN;
  136. case TEST_EXECUTE:
  137. break;
  138. }
  139. for (i = 0; i < ARRAY_LEN(tests); i++) {
  140. ast_escape_quoted(in, tests[i].buf, tests[i].buflen);
  141. if (strcmp(tests[i].output, tests[i].buf)) {
  142. ast_test_status_update(test, "ESCAPED DOES NOT MATCH EXPECTED, FAIL\n");
  143. ast_test_status_update(test, "original: %s\n", in);
  144. ast_test_status_update(test, "expected: %s\n", tests[i].output);
  145. ast_test_status_update(test, "result: %s\n", tests[i].buf);
  146. res = AST_TEST_FAIL;
  147. }
  148. }
  149. return res;
  150. }
  151. AST_TEST_DEFINE(md5_test)
  152. {
  153. static const struct {
  154. const char *input;
  155. const char *expected_output;
  156. } tests[] = {
  157. { "apples", "daeccf0ad3c1fc8c8015205c332f5b42" },
  158. { "bananas", "ec121ff80513ae58ed478d5c5787075b" },
  159. { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" },
  160. };
  161. enum ast_test_result_state res = AST_TEST_PASS;
  162. int i;
  163. switch (cmd) {
  164. case TEST_INIT:
  165. info->name = "md5_test";
  166. info->category = "/main/utils/";
  167. info->summary = "MD5 test";
  168. info->description =
  169. "This test exercises MD5 calculations."
  170. "";
  171. return AST_TEST_NOT_RUN;
  172. case TEST_EXECUTE:
  173. break;
  174. }
  175. ast_test_status_update(test, "Testing MD5 ...\n");
  176. for (i = 0; i < ARRAY_LEN(tests); i++) {
  177. char md5_hash[32];
  178. ast_md5_hash(md5_hash, tests[i].input);
  179. if (strcasecmp(md5_hash, tests[i].expected_output)) {
  180. ast_test_status_update(test,
  181. "input: '%s' hash: '%s' expected hash: '%s'\n",
  182. tests[i].input, md5_hash, tests[i].expected_output);
  183. res = AST_TEST_FAIL;
  184. }
  185. }
  186. return res;
  187. }
  188. AST_TEST_DEFINE(sha1_test)
  189. {
  190. static const struct {
  191. const char *input;
  192. const char *expected_output;
  193. } tests[] = {
  194. { "giraffe",
  195. "fac8f1a31d2998734d6a5253e49876b8e6a08239" },
  196. { "platypus",
  197. "1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" },
  198. { "ParastratiosphecomyiaStratiosphecomyioides",
  199. "58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" },
  200. };
  201. enum ast_test_result_state res = AST_TEST_PASS;
  202. int i;
  203. switch (cmd) {
  204. case TEST_INIT:
  205. info->name = "sha1_test";
  206. info->category = "/main/utils/";
  207. info->summary = "SHA1 test";
  208. info->description =
  209. "This test exercises SHA1 calculations."
  210. "";
  211. return AST_TEST_NOT_RUN;
  212. case TEST_EXECUTE:
  213. break;
  214. }
  215. ast_test_status_update(test, "Testing SHA1 ...\n");
  216. for (i = 0; i < ARRAY_LEN(tests); i++) {
  217. char sha1_hash[64];
  218. ast_sha1_hash(sha1_hash, tests[i].input);
  219. if (strcasecmp(sha1_hash, tests[i].expected_output)) {
  220. ast_test_status_update(test,
  221. "input: '%s' hash: '%s' expected hash: '%s'\n",
  222. tests[i].input, sha1_hash, tests[i].expected_output);
  223. res = AST_TEST_FAIL;
  224. }
  225. }
  226. return res;
  227. }
  228. AST_TEST_DEFINE(base64_test)
  229. {
  230. static const struct {
  231. const char *input;
  232. const char *decoded;
  233. } tests[] = {
  234. { "giraffe",
  235. "Z2lyYWZmZQ==" },
  236. { "platypus",
  237. "cGxhdHlwdXM=" },
  238. { "ParastratiosphecomyiaStratiosphecomyioides",
  239. "UGFyYXN0cmF0aW9zcGhlY29teWlhU3RyYXRpb3NwaGVjb215aW9pZGVz" },
  240. };
  241. int i;
  242. enum ast_test_result_state res = AST_TEST_PASS;
  243. switch (cmd) {
  244. case TEST_INIT:
  245. info->name = "base64_test";
  246. info->category = "/main/utils/";
  247. info->summary = "base64 test";
  248. info->description = "This test exercises the base64 conversions.";
  249. return AST_TEST_NOT_RUN;
  250. case TEST_EXECUTE:
  251. break;
  252. }
  253. for (i = 0; i < ARRAY_LEN(tests); i++) {
  254. char tmp[64];
  255. ast_base64encode(tmp, (unsigned char *)tests[i].input, strlen(tests[i].input), sizeof(tmp));
  256. if (strcasecmp(tmp, tests[i].decoded)) {
  257. ast_test_status_update(test,
  258. "input: '%s' base64 output: '%s' expected base64 output: '%s'\n",
  259. tests[i].input, tmp, tests[i].decoded);
  260. res = AST_TEST_FAIL;
  261. }
  262. memset(tmp, 0, sizeof(tmp));
  263. ast_base64decode((unsigned char *) tmp, tests[i].decoded, (sizeof(tmp) - 1));
  264. if (strcasecmp(tmp, tests[i].input)) {
  265. ast_test_status_update(test,
  266. "base64 input: '%s' output: '%s' expected output: '%s'\n",
  267. tests[i].decoded, tmp, tests[i].input);
  268. res = AST_TEST_FAIL;
  269. }
  270. }
  271. return res;
  272. }
  273. AST_TEST_DEFINE(crypto_loaded_test)
  274. {
  275. switch (cmd) {
  276. case TEST_INIT:
  277. info->name = "crypto_loaded_test";
  278. info->category = "/res/crypto/";
  279. info->summary = "Crypto loaded into memory";
  280. info->description = "Verifies whether the crypto functions overrode the stubs";
  281. return AST_TEST_NOT_RUN;
  282. case TEST_EXECUTE:
  283. break;
  284. }
  285. #if 0 /* Not defined on Solaris */
  286. ast_test_status_update(test,
  287. "address of __stub__ast_crypto_loaded is %p\n",
  288. __stub__ast_crypto_loaded);
  289. #ifndef HAVE_ATTRIBUTE_weak_import
  290. ast_test_status_update(test,
  291. "address of __ref__ast_crypto_loaded is %p\n",
  292. __ref__ast_crypto_loaded);
  293. #endif
  294. ast_test_status_update(test,
  295. "pointer to ast_crypto_loaded is %p\n",
  296. ast_crypto_loaded);
  297. #endif
  298. return ast_crypto_loaded() ? AST_TEST_PASS : AST_TEST_FAIL;
  299. }
  300. AST_TEST_DEFINE(adsi_loaded_test)
  301. {
  302. struct ast_channel c = { .adsicpe = AST_ADSI_AVAILABLE, };
  303. switch (cmd) {
  304. case TEST_INIT:
  305. info->name = "adsi_loaded_test";
  306. info->category = "/res/adsi/";
  307. info->summary = "ADSI loaded into memory";
  308. info->description = "Verifies whether the adsi functions overrode the stubs";
  309. return AST_TEST_NOT_RUN;
  310. case TEST_EXECUTE:
  311. break;
  312. }
  313. return ast_adsi_available(&c) ? AST_TEST_PASS : AST_TEST_FAIL;
  314. }
  315. static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, const char * const argv[])
  316. {
  317. ast_agi_send(agi->fd, chan, "200 result=0\n");
  318. return RESULT_SUCCESS;
  319. }
  320. AST_TEST_DEFINE(agi_loaded_test)
  321. {
  322. int res = AST_TEST_PASS;
  323. struct agi_command noop_command =
  324. { { "testnoop", NULL }, handle_noop, NULL, NULL, 0 };
  325. switch (cmd) {
  326. case TEST_INIT:
  327. info->name = "agi_loaded_test";
  328. info->category = "/res/agi/";
  329. info->summary = "AGI loaded into memory";
  330. info->description = "Verifies whether the agi functions overrode the stubs";
  331. return AST_TEST_NOT_RUN;
  332. case TEST_EXECUTE:
  333. break;
  334. }
  335. #if 0
  336. ast_test_status_update(test,
  337. "address of __stub__ast_agi_register is %p\n",
  338. __stub__ast_agi_register);
  339. #ifndef HAVE_ATTRIBUTE_weak_import
  340. ast_test_status_update(test,
  341. "address of __ref__ast_agi_register is %p\n",
  342. __ref__ast_agi_register);
  343. #endif
  344. ast_test_status_update(test,
  345. "pointer to ast_agi_register is %p\n",
  346. ast_agi_register);
  347. #endif
  348. if (ast_agi_register(ast_module_info->self, &noop_command) == AST_OPTIONAL_API_UNAVAILABLE) {
  349. ast_test_status_update(test, "Unable to register testnoop command, because res_agi is not loaded.\n");
  350. return AST_TEST_FAIL;
  351. }
  352. #ifndef HAVE_NULLSAFE_PRINTF
  353. /* Test for condition without actually crashing Asterisk */
  354. if (noop_command.usage == NULL) {
  355. ast_test_status_update(test, "AGI testnoop usage was not updated properly.\n");
  356. res = AST_TEST_FAIL;
  357. }
  358. if (noop_command.syntax == NULL) {
  359. ast_test_status_update(test, "AGI testnoop syntax was not updated properly.\n");
  360. res = AST_TEST_FAIL;
  361. }
  362. #endif
  363. ast_agi_unregister(ast_module_info->self, &noop_command);
  364. return res;
  365. }
  366. static int unload_module(void)
  367. {
  368. AST_TEST_UNREGISTER(uri_encode_decode_test);
  369. AST_TEST_UNREGISTER(quoted_escape_test);
  370. AST_TEST_UNREGISTER(md5_test);
  371. AST_TEST_UNREGISTER(sha1_test);
  372. AST_TEST_UNREGISTER(base64_test);
  373. AST_TEST_UNREGISTER(crypto_loaded_test);
  374. AST_TEST_UNREGISTER(adsi_loaded_test);
  375. AST_TEST_UNREGISTER(agi_loaded_test);
  376. return 0;
  377. }
  378. static int load_module(void)
  379. {
  380. AST_TEST_REGISTER(uri_encode_decode_test);
  381. AST_TEST_REGISTER(quoted_escape_test);
  382. AST_TEST_REGISTER(md5_test);
  383. AST_TEST_REGISTER(sha1_test);
  384. AST_TEST_REGISTER(base64_test);
  385. AST_TEST_REGISTER(crypto_loaded_test);
  386. AST_TEST_REGISTER(adsi_loaded_test);
  387. AST_TEST_REGISTER(agi_loaded_test);
  388. return AST_MODULE_LOAD_SUCCESS;
  389. }
  390. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Utils test module");