/security/nss/cmd/libpkix/pkix_pl/system/test_string.c

http://github.com/zpao/v8monkey · C · 479 lines · 330 code · 95 blank · 54 comment · 14 complexity · 635125617e87fd9e657622e016bfc379 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the PKIX-C library.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Sun Microsystems, Inc.
  18. * Portions created by the Initial Developer are
  19. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Sun Microsystems, Inc.
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /*
  38. * test_string.c
  39. *
  40. * Tests Strings.
  41. *
  42. */
  43. #include "testutil.h"
  44. #include "testutil_nss.h"
  45. static void *plContext = NULL;
  46. static void
  47. createString(
  48. PKIX_PL_String **testString,
  49. PKIX_UInt32 format,
  50. char *stringAscii,
  51. PKIX_UInt32 length)
  52. {
  53. PKIX_TEST_STD_VARS();
  54. PKIX_TEST_EXPECT_NO_ERROR
  55. (PKIX_PL_String_Create
  56. (format, stringAscii, length, testString, plContext));
  57. cleanup:
  58. PKIX_TEST_RETURN();
  59. }
  60. static void
  61. createStringOther(
  62. PKIX_PL_String **testEscAscii,
  63. PKIX_PL_String **testUtf16,
  64. PKIX_PL_String **ampString,
  65. PKIX_PL_String **testDebugAscii,
  66. PKIX_PL_String **testNullString,
  67. PKIX_UInt32 *utf16data)
  68. {
  69. char *nullText = "Hi� there!";
  70. char *escAsciiString =
  71. "¡𐀀࿿􀀁";
  72. char *debugAsciiString =
  73. "string with
newlines and	tabs";
  74. char * utfAmp = "\x00&";
  75. PKIX_TEST_STD_VARS();
  76. createString(testEscAscii,
  77. PKIX_ESCASCII,
  78. escAsciiString,
  79. PL_strlen(escAsciiString));
  80. createString(testUtf16, PKIX_UTF16, (char *)utf16data, 12);
  81. createString(ampString, PKIX_UTF16, utfAmp, 2);
  82. createString(testDebugAscii,
  83. PKIX_ESCASCII_DEBUG,
  84. debugAsciiString,
  85. PL_strlen(debugAsciiString));
  86. createString(testNullString,
  87. PKIX_ESCASCII_DEBUG,
  88. nullText,
  89. PL_strlen(nullText));
  90. goto cleanup;
  91. cleanup:
  92. PKIX_TEST_RETURN();
  93. }
  94. static void
  95. testGetEncoded(
  96. PKIX_PL_String *testEscAscii,
  97. PKIX_PL_String *testString0,
  98. PKIX_PL_String *testDebugAscii,
  99. PKIX_PL_String *testNullString,
  100. PKIX_UInt32 *utf16data)
  101. {
  102. char *temp = NULL;
  103. void *dest = NULL;
  104. void *dest2 = NULL;
  105. char *plainText = "string with\nnewlines and\ttabs";
  106. PKIX_UInt32 length, length2, i;
  107. PKIX_TEST_STD_VARS();
  108. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testEscAscii,
  109. PKIX_UTF16,
  110. &dest,
  111. &length,
  112. plContext));
  113. for (i = 0; i < length; i++) {
  114. if (((char*)dest)[i] != ((char*)utf16data)[i]) {
  115. testError("UTF-16 Data Differs from Source");
  116. printf("%d-th char is different -%c-%c-\n", i,
  117. ((char*)dest)[i], ((char*)utf16data)[i]);
  118. }
  119. }
  120. length = 0;
  121. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  122. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testNullString,
  123. PKIX_UTF16,
  124. &dest,
  125. &length,
  126. plContext));
  127. length = 0;
  128. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  129. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testString0,
  130. PKIX_ESCASCII_DEBUG,
  131. &dest,
  132. &length,
  133. plContext));
  134. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testDebugAscii,
  135. PKIX_ESCASCII_DEBUG,
  136. &dest2,
  137. &length2,
  138. plContext));
  139. for (i = 0; (i < length) && (i < length2); i++)
  140. if (((char*)dest)[i] != ((char*)dest2)[i]) {
  141. testError("Equivalent strings are unequal");
  142. break;
  143. }
  144. length = 0;
  145. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  146. length2 = 0;
  147. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest2, plContext));
  148. temp = PKIX_String2ASCII(testDebugAscii, plContext);
  149. if (temp){
  150. if (PL_strcmp(plainText, temp) != 0)
  151. testError("Debugged ASCII does not match "
  152. "equivalent EscAscii");
  153. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext));
  154. }
  155. cleanup:
  156. PKIX_TEST_RETURN();
  157. }
  158. static void
  159. testSprintf(void)
  160. {
  161. PKIX_Int32 x = 0xCAFE;
  162. PKIX_Int32 y = -12345;
  163. PKIX_PL_String *testString = NULL;
  164. PKIX_PL_String *formatString = NULL;
  165. PKIX_PL_String *sprintfString = NULL;
  166. char *plainText = "Testing Sprintf";
  167. char *format = "%s %x %u %d";
  168. char *convertedFormat = "%s %lx %lu %ld";
  169. char *temp = NULL;
  170. char *temp2 = NULL;
  171. PKIX_TEST_STD_VARS();
  172. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
  173. PKIX_ESCASCII,
  174. plainText,
  175. PL_strlen(plainText),
  176. &testString,
  177. plContext));
  178. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
  179. PKIX_ESCASCII,
  180. format,
  181. 11,
  182. &formatString,
  183. plContext));
  184. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Sprintf(&sprintfString,
  185. plContext,
  186. formatString,
  187. testString, x, y, y));
  188. PKIX_TEST_DECREF_BC(testString);
  189. temp = PR_smprintf(convertedFormat, plainText, x, y, y);
  190. temp2 = PKIX_String2ASCII(sprintfString, plContext);
  191. if (PL_strcmp(temp, temp2) != 0)
  192. testError("Sprintf produced incorrect output");
  193. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext));
  194. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp2, plContext));
  195. PKIX_TEST_DECREF_BC(sprintfString);
  196. PKIX_TEST_DECREF_BC(formatString);
  197. cleanup:
  198. PKIX_TEST_RETURN();
  199. }
  200. static void
  201. testErrorHandling(void)
  202. {
  203. char *debugAsciiString =
  204. "string with&#x000A;newlines and&#x0009;tabs";
  205. PKIX_PL_String *testString = NULL;
  206. PKIX_TEST_STD_VARS();
  207. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  208. PKIX_ESCASCII,
  209. NULL,
  210. 50,
  211. &testString,
  212. plContext));
  213. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(PKIX_ESCASCII,
  214. "blah", 4, NULL, plContext));
  215. PKIX_TEST_EXPECT_ERROR(PKIX_PL_Sprintf(&testString, plContext, NULL));
  216. PKIX_TEST_EXPECT_ERROR
  217. (PKIX_PL_GetString(0, NULL, &testString, plContext));
  218. PKIX_TEST_EXPECT_ERROR(PKIX_PL_GetString(0, "blah", 0, plContext));
  219. /* ---------------------------- */
  220. subTest("Unicode Error Handling");
  221. /* &#x must be followed by 4 hexadecimal digits */
  222. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  223. PKIX_ESCASCII,
  224. "&#x003k;",
  225. 7,
  226. &testString,
  227. plContext));
  228. /* &#x must be followed by 4 hexadecimal digits */
  229. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  230. PKIX_ESCASCII,
  231. "abc&#x00",
  232. 8,
  233. &testString,
  234. plContext));
  235. /* &#x must be between 00010000-0010FFFF */
  236. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  237. PKIX_ESCASCII,
  238. "&#x00200101;",
  239. 11,
  240. &testString,
  241. plContext));
  242. /* &#x must be followed by 8 hexadecimal digits */
  243. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  244. PKIX_ESCASCII,
  245. "&#x001000",
  246. 10,
  247. &testString,
  248. plContext));
  249. /* &#x must be followed by 8 hexadecimal digits */
  250. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  251. PKIX_ESCASCII,
  252. "&#x0010m00;",
  253. 10,
  254. &testString,
  255. plContext));
  256. /* Byte values D800-DFFF are reserved */
  257. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  258. PKIX_ESCASCII,
  259. "&#xD800;",
  260. 7,
  261. &testString,
  262. plContext));
  263. /* Can't use &#x for regular characters */
  264. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  265. PKIX_ESCASCII,
  266. "&#x0032;",
  267. 7,
  268. &testString,
  269. plContext));
  270. /* Can't use non-printable characters */
  271. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  272. PKIX_ESCASCII,
  273. "\xA1",
  274. 1,
  275. &testString,
  276. plContext));
  277. /* Only legal \\ characters are \\, u and U */
  278. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  279. PKIX_ESCASCII,
  280. "&blah",
  281. 5,
  282. &testString,
  283. plContext));
  284. /* Surrogate pairs must be legal */
  285. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  286. PKIX_UTF16,
  287. "\xd8\x00\x0\x66",
  288. 4,
  289. &testString,
  290. plContext));
  291. /* Debugged EscASCII should not be accepted as EscASCII */
  292. PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
  293. PKIX_ESCASCII,
  294. debugAsciiString,
  295. PL_strlen(debugAsciiString),
  296. &testString,
  297. plContext));
  298. cleanup:
  299. PKIX_TEST_RETURN();
  300. }
  301. static void
  302. testDestroy(
  303. PKIX_PL_String *string)
  304. {
  305. PKIX_TEST_STD_VARS();
  306. PKIX_TEST_DECREF_BC(string);
  307. cleanup:
  308. PKIX_TEST_RETURN();
  309. }
  310. int test_string(int argc, char *argv[]) {
  311. PKIX_PL_String *testString[6] = {NULL};
  312. PKIX_PL_String *testNullString = NULL;
  313. PKIX_PL_String *testDebugAscii = NULL;
  314. PKIX_PL_String *testEscAscii = NULL;
  315. PKIX_PL_String *testUtf16 = NULL;
  316. PKIX_PL_String *ampString = NULL;
  317. unsigned char utf16Data[] = {0x00, 0xA1, 0xD8, 0x00,
  318. 0xDC, 0x00, 0x0F, 0xFF,
  319. 0xDB, 0xC0, 0xDC, 0x01};
  320. PKIX_UInt32 i, size = 6;
  321. char *plainText[6] = {
  322. "string with\nnewlines and\ttabs",
  323. "Not an escaped char: &amp;#x0012;",
  324. "Encode &amp; with &amp;amp; in ASCII",
  325. "&#x00A1;",
  326. "&amp;",
  327. "string with\nnewlines and\ttabs"
  328. };
  329. PKIX_UInt32 actualMinorVersion;
  330. PKIX_UInt32 j = 0;
  331. PKIX_TEST_STD_VARS();
  332. startTests("Strings");
  333. PKIX_TEST_EXPECT_NO_ERROR(
  334. PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
  335. subTest("PKIX_PL_String_Create <ascii format>");
  336. for (i = 0; i < size; i++) {
  337. testString[i] = NULL;
  338. createString
  339. (&testString[i],
  340. PKIX_ESCASCII,
  341. plainText[i],
  342. PL_strlen(plainText[i]));
  343. }
  344. subTest("PKIX_PL_String_Create <other formats>");
  345. createStringOther
  346. (&testEscAscii,
  347. &testUtf16,
  348. &ampString,
  349. &testDebugAscii,
  350. &testNullString,
  351. (PKIX_UInt32 *)utf16Data);
  352. PKIX_TEST_EQ_HASH_TOSTR_DUP
  353. (testString[0],
  354. testString[5],
  355. testString[1],
  356. plainText[0],
  357. String,
  358. PKIX_TRUE);
  359. subTest("PKIX_PL_String_GetEncoded");
  360. testGetEncoded
  361. (testEscAscii,
  362. testString[0],
  363. testDebugAscii,
  364. testNullString,
  365. (PKIX_UInt32 *)utf16Data);
  366. subTest("PKIX_PL_Sprintf");
  367. testSprintf();
  368. subTest("PKIX_PL_String_Create <error_handling>");
  369. testErrorHandling();
  370. subTest("PKIX_PL_String_Destroy");
  371. for (i = 0; i < size; i++) {
  372. testDestroy(testString[i]);
  373. }
  374. testDestroy(testEscAscii);
  375. testDestroy(testUtf16);
  376. testDestroy(ampString);
  377. testDestroy(testDebugAscii);
  378. testDestroy(testNullString);
  379. cleanup:
  380. PKIX_Shutdown(plContext);
  381. PKIX_TEST_RETURN();
  382. endTests("String");
  383. return (0);
  384. }