test_utils.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. #include <sys/stat.h>
  40. AST_TEST_DEFINE(uri_encode_decode_test)
  41. {
  42. int res = AST_TEST_PASS;
  43. const char *in = "abcdefghijklmnopurstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890 ~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
  44. char out[256] = { 0 };
  45. char small[4] = { 0 };
  46. const struct ast_flags none = {0};
  47. int i = 0;
  48. static struct {
  49. const char *spec_str;
  50. struct ast_flags spec;
  51. char *buf;
  52. size_t buflen;
  53. const char *input;
  54. const char *output;
  55. const char *decoded_output;
  56. } tests[5];
  57. #define INIT_ENCODE_TEST(s, buffer, in, out, dec_out) do { \
  58. if (i < ARRAY_LEN(tests)) { \
  59. tests[i].spec_str = #s; \
  60. tests[i].spec = s; \
  61. tests[i].buf = buffer; \
  62. tests[i].buflen = sizeof(buffer); \
  63. tests[i].input = in; \
  64. tests[i].output = out; \
  65. tests[i].decoded_output = dec_out; \
  66. i++; \
  67. } else { \
  68. ast_test_status_update(test, "error: 'tests' array too small\n"); \
  69. res = AST_TEST_FAIL; \
  70. } \
  71. } while (0)
  72. INIT_ENCODE_TEST(ast_uri_http, out, in,
  73. "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);
  74. INIT_ENCODE_TEST(ast_uri_http_legacy, out, in,
  75. "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);
  76. INIT_ENCODE_TEST(ast_uri_sip_user, out, in,
  77. "abcdefghijklmnopurstuvwxyz%20ABCDEFGHIJKLMNOPQRSTUVWXYZ%201234567890%20~%60!%40%23$%25%5E&*()_-+=%7B%5B%7D%5D%7C%5C%3A;%22'%3C,%3E.?/", in);
  78. INIT_ENCODE_TEST(none, small, in, "%61", "a");
  79. INIT_ENCODE_TEST(ast_uri_http, small, in, "abc", "abc");
  80. switch (cmd) {
  81. case TEST_INIT:
  82. info->name = "uri_encode_decode_test";
  83. info->category = "/main/utils/";
  84. info->summary = "encode and decode a hex escaped string";
  85. info->description = "encode a string, verify encoded string matches what we expect. Decode the encoded string, verify decoded string matches the original string.";
  86. return AST_TEST_NOT_RUN;
  87. case TEST_EXECUTE:
  88. break;
  89. }
  90. for (i = 0; i < ARRAY_LEN(tests); i++) {
  91. ast_uri_encode(tests[i].input, tests[i].buf, tests[i].buflen, tests[i].spec);
  92. if (strcmp(tests[i].output, tests[i].buf)) {
  93. ast_test_status_update(test, "encoding with %s did not match expected output, FAIL\n", tests[i].spec_str);
  94. ast_test_status_update(test, "original: %s\n", tests[i].input);
  95. ast_test_status_update(test, "expected: %s\n", tests[i].output);
  96. ast_test_status_update(test, "result: %s\n", tests[i].buf);
  97. res = AST_TEST_FAIL;
  98. continue;
  99. }
  100. ast_uri_decode(tests[i].buf, tests[i].spec);
  101. if (strcmp(tests[i].decoded_output, tests[i].buf)) {
  102. ast_test_status_update(test, "decoding with %s did not match the original input (or expected decoded output)\n", tests[i].spec_str);
  103. ast_test_status_update(test, "original: %s\n", tests[i].input);
  104. ast_test_status_update(test, "expected: %s\n", tests[i].decoded_output);
  105. ast_test_status_update(test, "decoded: %s\n", tests[i].buf);
  106. res = AST_TEST_FAIL;
  107. }
  108. }
  109. return res;
  110. }
  111. AST_TEST_DEFINE(quoted_escape_test)
  112. {
  113. int res = AST_TEST_PASS;
  114. const char *in = "a\"bcdefg\"hijkl\\mnopqrs tuv\twxyz";
  115. char out[256] = { 0 };
  116. char small[4] = { 0 };
  117. int i;
  118. static struct {
  119. char *buf;
  120. const size_t buflen;
  121. const char *output;
  122. } tests[] = {
  123. {0, sizeof(out),
  124. "a\\\"bcdefg\\\"hijkl\\\\mnopqrs tuv\twxyz"},
  125. {0, sizeof(small),
  126. "a\\\""},
  127. };
  128. tests[0].buf = out;
  129. tests[1].buf = small;
  130. switch (cmd) {
  131. case TEST_INIT:
  132. info->name = "quoted_escape_test";
  133. info->category = "/main/utils/";
  134. info->summary = "escape a quoted string";
  135. info->description = "Escape a string to be quoted and check the result.";
  136. return AST_TEST_NOT_RUN;
  137. case TEST_EXECUTE:
  138. break;
  139. }
  140. for (i = 0; i < ARRAY_LEN(tests); i++) {
  141. ast_escape_quoted(in, tests[i].buf, tests[i].buflen);
  142. if (strcmp(tests[i].output, tests[i].buf)) {
  143. ast_test_status_update(test, "ESCAPED DOES NOT MATCH EXPECTED, FAIL\n");
  144. ast_test_status_update(test, "original: %s\n", in);
  145. ast_test_status_update(test, "expected: %s\n", tests[i].output);
  146. ast_test_status_update(test, "result: %s\n", tests[i].buf);
  147. res = AST_TEST_FAIL;
  148. }
  149. }
  150. return res;
  151. }
  152. AST_TEST_DEFINE(md5_test)
  153. {
  154. static const struct {
  155. const char *input;
  156. const char *expected_output;
  157. } tests[] = {
  158. { "apples", "daeccf0ad3c1fc8c8015205c332f5b42" },
  159. { "bananas", "ec121ff80513ae58ed478d5c5787075b" },
  160. { "reallylongstringaboutgoatcheese", "0a2d9280d37e2e37545cfef6e7e4e890" },
  161. };
  162. enum ast_test_result_state res = AST_TEST_PASS;
  163. int i;
  164. switch (cmd) {
  165. case TEST_INIT:
  166. info->name = "md5_test";
  167. info->category = "/main/utils/";
  168. info->summary = "MD5 test";
  169. info->description =
  170. "This test exercises MD5 calculations."
  171. "";
  172. return AST_TEST_NOT_RUN;
  173. case TEST_EXECUTE:
  174. break;
  175. }
  176. ast_test_status_update(test, "Testing MD5 ...\n");
  177. for (i = 0; i < ARRAY_LEN(tests); i++) {
  178. char md5_hash[33];
  179. ast_md5_hash(md5_hash, tests[i].input);
  180. if (strcasecmp(md5_hash, tests[i].expected_output)) {
  181. ast_test_status_update(test,
  182. "input: '%s' hash: '%s' expected hash: '%s'\n",
  183. tests[i].input, md5_hash, tests[i].expected_output);
  184. res = AST_TEST_FAIL;
  185. }
  186. }
  187. return res;
  188. }
  189. AST_TEST_DEFINE(sha1_test)
  190. {
  191. static const struct {
  192. const char *input;
  193. const char *expected_output;
  194. } tests[] = {
  195. { "giraffe",
  196. "fac8f1a31d2998734d6a5253e49876b8e6a08239" },
  197. { "platypus",
  198. "1dfb21b7a4d35e90d943e3a16107ccbfabd064d5" },
  199. { "ParastratiosphecomyiaStratiosphecomyioides",
  200. "58af4e8438676f2bd3c4d8df9e00ee7fe06945bb" },
  201. };
  202. enum ast_test_result_state res = AST_TEST_PASS;
  203. int i;
  204. switch (cmd) {
  205. case TEST_INIT:
  206. info->name = "sha1_test";
  207. info->category = "/main/utils/";
  208. info->summary = "SHA1 test";
  209. info->description =
  210. "This test exercises SHA1 calculations."
  211. "";
  212. return AST_TEST_NOT_RUN;
  213. case TEST_EXECUTE:
  214. break;
  215. }
  216. ast_test_status_update(test, "Testing SHA1 ...\n");
  217. for (i = 0; i < ARRAY_LEN(tests); i++) {
  218. char sha1_hash[64];
  219. ast_sha1_hash(sha1_hash, tests[i].input);
  220. if (strcasecmp(sha1_hash, tests[i].expected_output)) {
  221. ast_test_status_update(test,
  222. "input: '%s' hash: '%s' expected hash: '%s'\n",
  223. tests[i].input, sha1_hash, tests[i].expected_output);
  224. res = AST_TEST_FAIL;
  225. }
  226. }
  227. return res;
  228. }
  229. AST_TEST_DEFINE(base64_test)
  230. {
  231. static const struct {
  232. const char *input;
  233. const char *decoded;
  234. } tests[] = {
  235. { "giraffe",
  236. "Z2lyYWZmZQ==" },
  237. { "platypus",
  238. "cGxhdHlwdXM=" },
  239. { "ParastratiosphecomyiaStratiosphecomyioides",
  240. "UGFyYXN0cmF0aW9zcGhlY29teWlhU3RyYXRpb3NwaGVjb215aW9pZGVz" },
  241. };
  242. int i;
  243. enum ast_test_result_state res = AST_TEST_PASS;
  244. switch (cmd) {
  245. case TEST_INIT:
  246. info->name = "base64_test";
  247. info->category = "/main/utils/";
  248. info->summary = "base64 test";
  249. info->description = "This test exercises the base64 conversions.";
  250. return AST_TEST_NOT_RUN;
  251. case TEST_EXECUTE:
  252. break;
  253. }
  254. for (i = 0; i < ARRAY_LEN(tests); i++) {
  255. char tmp[64];
  256. ast_base64encode(tmp, (unsigned char *)tests[i].input, strlen(tests[i].input), sizeof(tmp));
  257. if (strcasecmp(tmp, tests[i].decoded)) {
  258. ast_test_status_update(test,
  259. "input: '%s' base64 output: '%s' expected base64 output: '%s'\n",
  260. tests[i].input, tmp, tests[i].decoded);
  261. res = AST_TEST_FAIL;
  262. }
  263. memset(tmp, 0, sizeof(tmp));
  264. ast_base64decode((unsigned char *) tmp, tests[i].decoded, (sizeof(tmp) - 1));
  265. if (strcasecmp(tmp, tests[i].input)) {
  266. ast_test_status_update(test,
  267. "base64 input: '%s' output: '%s' expected output: '%s'\n",
  268. tests[i].decoded, tmp, tests[i].input);
  269. res = AST_TEST_FAIL;
  270. }
  271. }
  272. return res;
  273. }
  274. AST_TEST_DEFINE(crypto_loaded_test)
  275. {
  276. switch (cmd) {
  277. case TEST_INIT:
  278. info->name = "crypto_loaded_test";
  279. info->category = "/res/crypto/";
  280. info->summary = "Crypto loaded into memory";
  281. info->description = "Verifies whether the crypto functions overrode the stubs";
  282. return AST_TEST_NOT_RUN;
  283. case TEST_EXECUTE:
  284. break;
  285. }
  286. #if 0 /* Not defined on Solaris */
  287. ast_test_status_update(test,
  288. "address of __stub__ast_crypto_loaded is %p\n",
  289. __stub__ast_crypto_loaded);
  290. #ifndef HAVE_ATTRIBUTE_weak_import
  291. ast_test_status_update(test,
  292. "address of __ref__ast_crypto_loaded is %p\n",
  293. __ref__ast_crypto_loaded);
  294. #endif
  295. ast_test_status_update(test,
  296. "pointer to ast_crypto_loaded is %p\n",
  297. ast_crypto_loaded);
  298. #endif
  299. return ast_crypto_loaded() ? AST_TEST_PASS : AST_TEST_FAIL;
  300. }
  301. AST_TEST_DEFINE(adsi_loaded_test)
  302. {
  303. struct ast_channel *c;
  304. int res;
  305. switch (cmd) {
  306. case TEST_INIT:
  307. info->name = "adsi_loaded_test";
  308. info->category = "/res/adsi/";
  309. info->summary = "ADSI loaded into memory";
  310. info->description = "Verifies whether the adsi functions overrode the stubs";
  311. return AST_TEST_NOT_RUN;
  312. case TEST_EXECUTE:
  313. break;
  314. }
  315. if (!(c = ast_dummy_channel_alloc())) {
  316. return AST_TEST_FAIL;
  317. }
  318. ast_channel_adsicpe_set(c, AST_ADSI_AVAILABLE);
  319. res = ast_adsi_available(c) ? AST_TEST_PASS : AST_TEST_FAIL;
  320. c = ast_channel_unref(c);
  321. return res;
  322. }
  323. static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, const char * const argv[])
  324. {
  325. ast_agi_send(agi->fd, chan, "200 result=0\n");
  326. return RESULT_SUCCESS;
  327. }
  328. AST_TEST_DEFINE(agi_loaded_test)
  329. {
  330. int res = AST_TEST_PASS;
  331. struct agi_command noop_command =
  332. { { "testnoop", NULL }, handle_noop, NULL, NULL, 0 };
  333. switch (cmd) {
  334. case TEST_INIT:
  335. info->name = "agi_loaded_test";
  336. info->category = "/res/agi/";
  337. info->summary = "AGI loaded into memory";
  338. info->description = "Verifies whether the agi functions overrode the stubs";
  339. return AST_TEST_NOT_RUN;
  340. case TEST_EXECUTE:
  341. break;
  342. }
  343. #if 0
  344. ast_test_status_update(test,
  345. "address of __stub__ast_agi_register is %p\n",
  346. __stub__ast_agi_register);
  347. #ifndef HAVE_ATTRIBUTE_weak_import
  348. ast_test_status_update(test,
  349. "address of __ref__ast_agi_register is %p\n",
  350. __ref__ast_agi_register);
  351. #endif
  352. ast_test_status_update(test,
  353. "pointer to ast_agi_register is %p\n",
  354. ast_agi_register);
  355. #endif
  356. if (ast_agi_register(ast_module_info->self, &noop_command) == AST_OPTIONAL_API_UNAVAILABLE) {
  357. ast_test_status_update(test, "Unable to register testnoop command, because res_agi is not loaded.\n");
  358. return AST_TEST_FAIL;
  359. }
  360. #ifndef HAVE_NULLSAFE_PRINTF
  361. /* Test for condition without actually crashing Asterisk */
  362. if (noop_command.usage == NULL) {
  363. ast_test_status_update(test, "AGI testnoop usage was not updated properly.\n");
  364. res = AST_TEST_FAIL;
  365. }
  366. if (noop_command.syntax == NULL) {
  367. ast_test_status_update(test, "AGI testnoop syntax was not updated properly.\n");
  368. res = AST_TEST_FAIL;
  369. }
  370. #endif
  371. ast_agi_unregister(ast_module_info->self, &noop_command);
  372. return res;
  373. }
  374. AST_TEST_DEFINE(safe_mkdir_test)
  375. {
  376. char base_path[] = "/tmp/safe_mkdir.XXXXXX";
  377. char path[80] = {};
  378. int res;
  379. struct stat actual;
  380. switch (cmd) {
  381. case TEST_INIT:
  382. info->name = __func__;
  383. info->category = "/main/utils/";
  384. info->summary = "Safe mkdir test";
  385. info->description =
  386. "This test ensures that ast_safe_mkdir does what it is "
  387. "supposed to";
  388. return AST_TEST_NOT_RUN;
  389. case TEST_EXECUTE:
  390. break;
  391. }
  392. if (mkdtemp(base_path) == NULL) {
  393. ast_test_status_update(test, "Failed to create tmpdir for test\n");
  394. return AST_TEST_FAIL;
  395. }
  396. snprintf(path, sizeof(path), "%s/should_work", base_path);
  397. res = ast_safe_mkdir(base_path, path, 0777);
  398. ast_test_validate(test, 0 == res);
  399. res = stat(path, &actual);
  400. ast_test_validate(test, 0 == res);
  401. ast_test_validate(test, S_ISDIR(actual.st_mode));
  402. snprintf(path, sizeof(path), "%s/should/also/work", base_path);
  403. res = ast_safe_mkdir(base_path, path, 0777);
  404. ast_test_validate(test, 0 == res);
  405. res = stat(path, &actual);
  406. ast_test_validate(test, 0 == res);
  407. ast_test_validate(test, S_ISDIR(actual.st_mode));
  408. snprintf(path, sizeof(path), "%s/even/this/../should/work", base_path);
  409. res = ast_safe_mkdir(base_path, path, 0777);
  410. ast_test_validate(test, 0 == res);
  411. snprintf(path, sizeof(path), "%s/even/should/work", base_path);
  412. res = stat(path, &actual);
  413. ast_test_validate(test, 0 == res);
  414. ast_test_validate(test, S_ISDIR(actual.st_mode));
  415. snprintf(path, sizeof(path),
  416. "%s/surprisingly/this/should//////////////////work", base_path);
  417. res = ast_safe_mkdir(base_path, path, 0777);
  418. ast_test_validate(test, 0 == res);
  419. snprintf(path, sizeof(path),
  420. "%s/surprisingly/this/should/work", base_path);
  421. res = stat(path, &actual);
  422. ast_test_validate(test, 0 == res);
  423. ast_test_validate(test, S_ISDIR(actual.st_mode));
  424. snprintf(path, sizeof(path), "/should_not_work");
  425. res = ast_safe_mkdir(base_path, path, 0777);
  426. ast_test_validate(test, 0 != res);
  427. ast_test_validate(test, EPERM == errno);
  428. res = stat(path, &actual);
  429. ast_test_validate(test, 0 != res);
  430. ast_test_validate(test, ENOENT == errno);
  431. snprintf(path, sizeof(path), "%s/../nor_should_this", base_path);
  432. res = ast_safe_mkdir(base_path, path, 0777);
  433. ast_test_validate(test, 0 != res);
  434. ast_test_validate(test, EPERM == errno);
  435. strncpy(path, "/tmp/nor_should_this", sizeof(path));
  436. res = stat(path, &actual);
  437. ast_test_validate(test, 0 != res);
  438. ast_test_validate(test, ENOENT == errno);
  439. snprintf(path, sizeof(path),
  440. "%s/this/especially/should/not/../../../../../work", base_path);
  441. res = ast_safe_mkdir(base_path, path, 0777);
  442. ast_test_validate(test, 0 != res);
  443. ast_test_validate(test, EPERM == errno);
  444. strncpy(path, "/tmp/work", sizeof(path));
  445. res = stat(path, &actual);
  446. ast_test_validate(test, 0 != res);
  447. ast_test_validate(test, ENOENT == errno);
  448. return AST_TEST_PASS;
  449. }
  450. AST_TEST_DEFINE(crypt_test)
  451. {
  452. RAII_VAR(char *, password_crypted, NULL, ast_free);
  453. RAII_VAR(char *, blank_crypted, NULL, ast_free);
  454. const char *password = "Passw0rd";
  455. const char *not_a_password = "not-a-password";
  456. switch (cmd) {
  457. case TEST_INIT:
  458. info->name = "crypt_test";
  459. info->category = "/main/utils/";
  460. info->summary = "Test ast_crypt wrappers";
  461. info->description = "Verifies that the ast_crypt wrappers work as expected.";
  462. return AST_TEST_NOT_RUN;
  463. case TEST_EXECUTE:
  464. break;
  465. }
  466. password_crypted = ast_crypt_encrypt(password);
  467. ast_test_validate(test, NULL != password_crypted);
  468. ast_test_validate(test, 0 != strcmp(password, password_crypted));
  469. ast_test_validate(test, ast_crypt_validate(password, password_crypted));
  470. ast_test_validate(test,
  471. !ast_crypt_validate(not_a_password, password_crypted));
  472. blank_crypted = ast_crypt_encrypt("");
  473. ast_test_validate(test, NULL != blank_crypted);
  474. ast_test_validate(test, 0 != strcmp(blank_crypted, ""));
  475. ast_test_validate(test, ast_crypt_validate("", blank_crypted));
  476. ast_test_validate(test,
  477. !ast_crypt_validate(not_a_password, blank_crypted));
  478. return AST_TEST_PASS;
  479. }
  480. struct quote_set {
  481. char *input;
  482. char *output;
  483. };
  484. AST_TEST_DEFINE(quote_mutation)
  485. {
  486. char escaped[64];
  487. static const struct quote_set escape_sets[] = {
  488. {"\"string\"", "\\\"string\\\""},
  489. {"\"string", "\\\"string"},
  490. {"string\"", "string\\\""},
  491. {"string", "string"},
  492. {"str\"ing", "str\\\"ing"},
  493. {"\"", "\\\""},
  494. {"\\\"", "\\\\\\\""},
  495. };
  496. int i;
  497. switch (cmd) {
  498. case TEST_INIT:
  499. info->name = "quote_mutation";
  500. info->category = "/main/utils/";
  501. info->summary = "Test mutation of quotes in strings";
  502. info->description =
  503. "This tests escaping and unescaping of quotes in strings to "
  504. "verify that the original string is recovered.";
  505. return AST_TEST_NOT_RUN;
  506. case TEST_EXECUTE:
  507. break;
  508. }
  509. for (i = 0; i < ARRAY_LEN(escape_sets); i++) {
  510. ast_escape_quoted(escape_sets[i].input, escaped, sizeof(escaped));
  511. if (strcmp(escaped, escape_sets[i].output)) {
  512. ast_test_status_update(test,
  513. "Expected escaped string '%s' instead of '%s'\n",
  514. escape_sets[i].output, escaped);
  515. return AST_TEST_FAIL;
  516. }
  517. ast_unescape_quoted(escaped);
  518. if (strcmp(escaped, escape_sets[i].input)) {
  519. ast_test_status_update(test,
  520. "Expected unescaped string '%s' instead of '%s'\n",
  521. escape_sets[i].input, escaped);
  522. return AST_TEST_FAIL;
  523. }
  524. }
  525. return AST_TEST_PASS;
  526. }
  527. AST_TEST_DEFINE(quote_unescaping)
  528. {
  529. static const struct quote_set escape_sets[] = {
  530. {"\"string\"", "\"string\""},
  531. {"\\\"string\"", "\"string\""},
  532. {"\"string\\\"", "\"string\""},
  533. {"str\\ing", "string"},
  534. {"string\\", "string"},
  535. {"\\string", "string"},
  536. };
  537. int i;
  538. switch (cmd) {
  539. case TEST_INIT:
  540. info->name = "quote_unescaping";
  541. info->category = "/main/utils/";
  542. info->summary = "Test unescaping of off-nominal strings";
  543. info->description =
  544. "This tests unescaping of strings which contain a mix of "
  545. "escaped and unescaped sequences.";
  546. return AST_TEST_NOT_RUN;
  547. case TEST_EXECUTE:
  548. break;
  549. }
  550. for (i = 0; i < ARRAY_LEN(escape_sets); i++) {
  551. RAII_VAR(char *, escaped, ast_strdup(escape_sets[i].input), ast_free);
  552. ast_unescape_quoted(escaped);
  553. if (strcmp(escaped, escape_sets[i].output)) {
  554. ast_test_status_update(test,
  555. "Expected unescaped string '%s' instead of '%s'\n",
  556. escape_sets[i].output, escaped);
  557. return AST_TEST_FAIL;
  558. }
  559. }
  560. return AST_TEST_PASS;
  561. }
  562. static int unload_module(void)
  563. {
  564. AST_TEST_UNREGISTER(uri_encode_decode_test);
  565. AST_TEST_UNREGISTER(quoted_escape_test);
  566. AST_TEST_UNREGISTER(md5_test);
  567. AST_TEST_UNREGISTER(sha1_test);
  568. AST_TEST_UNREGISTER(base64_test);
  569. AST_TEST_UNREGISTER(crypto_loaded_test);
  570. AST_TEST_UNREGISTER(adsi_loaded_test);
  571. AST_TEST_UNREGISTER(agi_loaded_test);
  572. AST_TEST_UNREGISTER(safe_mkdir_test);
  573. AST_TEST_UNREGISTER(crypt_test);
  574. AST_TEST_UNREGISTER(quote_mutation);
  575. AST_TEST_UNREGISTER(quote_unescaping);
  576. return 0;
  577. }
  578. static int load_module(void)
  579. {
  580. AST_TEST_REGISTER(uri_encode_decode_test);
  581. AST_TEST_REGISTER(quoted_escape_test);
  582. AST_TEST_REGISTER(md5_test);
  583. AST_TEST_REGISTER(sha1_test);
  584. AST_TEST_REGISTER(base64_test);
  585. AST_TEST_REGISTER(crypto_loaded_test);
  586. AST_TEST_REGISTER(adsi_loaded_test);
  587. AST_TEST_REGISTER(agi_loaded_test);
  588. AST_TEST_REGISTER(safe_mkdir_test);
  589. AST_TEST_REGISTER(crypt_test);
  590. AST_TEST_REGISTER(quote_mutation);
  591. AST_TEST_REGISTER(quote_unescaping);
  592. return AST_MODULE_LOAD_SUCCESS;
  593. }
  594. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Utils test module");