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

http://github.com/zpao/v8monkey · C · 375 lines · 260 code · 70 blank · 45 comment · 17 complexity · 866a7eef9c2308747b9d923e8fcfac66 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_string2.c
  39. *
  40. * Tests International 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 **vivaEspanaString,
  49. PKIX_PL_String **straussString,
  50. PKIX_PL_String **gorbachevString,
  51. PKIX_PL_String **testUTF16String,
  52. PKIX_PL_String **chineseString,
  53. PKIX_PL_String **jeanRenoString)
  54. {
  55. /* this is meant to fail - it highlights bug 0002 */
  56. unsigned char utf16String[4] = { 0xF8, 0x60,
  57. 0xFC, 0x60};
  58. unsigned char chinese[16] = { 0xe7, 0xab, 0xa0,
  59. 0xe5, 0xad, 0x90,
  60. 0xe6, 0x80, 0xa1,
  61. 0x20,
  62. 0xe4, 0xb8, 0xad,
  63. 0xe5, 0x9b, 0xbd
  64. };
  65. char* jeanReno = "Jean R\303\251no is an actor.";
  66. char* gorbachev = /* This is the name "Gorbachev" in cyrllic */
  67. "\xd0\x93\xd0\xbe\xd1\x80\xd0\xb1\xd0\xb0\xd1\x87\xd1\x91\xd0\xb2";
  68. char *vivaEspana =
  69. "¡Viva España!";
  70. char *strauss =
  71. "Strauß was born in Österreich";
  72. PKIX_TEST_STD_VARS();
  73. /* ---------------------------- */
  74. subTest("String Creation");
  75. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
  76. PKIX_ESCASCII,
  77. vivaEspana,
  78. PL_strlen(vivaEspana),
  79. vivaEspanaString,
  80. plContext));
  81. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
  82. PKIX_ESCASCII,
  83. strauss,
  84. PL_strlen(strauss),
  85. straussString,
  86. plContext));
  87. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
  88. PKIX_UTF8,
  89. gorbachev,
  90. PL_strlen(gorbachev),
  91. gorbachevString,
  92. plContext));
  93. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
  94. PKIX_UTF16,
  95. utf16String,
  96. 4,
  97. testUTF16String,
  98. plContext));
  99. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
  100. PKIX_UTF8,
  101. chinese,
  102. 16,
  103. chineseString,
  104. plContext));
  105. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
  106. PKIX_UTF8,
  107. jeanReno,
  108. PL_strlen(jeanReno),
  109. jeanRenoString,
  110. plContext));
  111. cleanup:
  112. PKIX_TEST_RETURN();
  113. }
  114. static void
  115. testGetEncoded(PKIX_PL_String *string, PKIX_UInt32 format)
  116. {
  117. void *dest = NULL;
  118. PKIX_UInt32 length;
  119. PKIX_TEST_STD_VARS();
  120. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded
  121. (string,
  122. format,
  123. &dest,
  124. &length,
  125. plContext));
  126. if (dest){
  127. (void) printf("\tResult: %s\n", (char *)dest);
  128. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  129. }
  130. cleanup:
  131. PKIX_TEST_RETURN();
  132. }
  133. static void
  134. testHTMLOutput(
  135. PKIX_PL_String *vivaEspanaString,
  136. PKIX_PL_String *straussString,
  137. PKIX_PL_String *gorbachevString,
  138. PKIX_PL_String *testUTF16String,
  139. PKIX_PL_String *chineseString,
  140. PKIX_PL_String *jeanRenoString)
  141. {
  142. void *dest = NULL;
  143. PKIX_UInt32 length;
  144. FILE *htmlFile = NULL;
  145. PKIX_TEST_STD_VARS();
  146. /* Opening a file for output */
  147. htmlFile = fopen("utf8.html", "w");
  148. if (htmlFile != plContext) {
  149. (void) fprintf(htmlFile, "<html><head>\n");
  150. (void) fprintf(htmlFile, "<meta http-equiv=\"Content-Type\"");
  151. (void) fprintf(htmlFile,
  152. "content = \"text/html; charset = UTF-8\">\n");
  153. (void) fprintf(htmlFile, "</head><body>\n");
  154. (void) fprintf(htmlFile, "<font size =\"+2\">\n");
  155. } else
  156. (void) printf("Could not open HTML file\n");
  157. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testUTF16String,
  158. PKIX_UTF8,
  159. &dest,
  160. &length,
  161. plContext));
  162. if (htmlFile != plContext) {
  163. (void) printf("%d bytes written to HTML file\n",
  164. fwrite(dest, length, 1, htmlFile));
  165. (void) fprintf(htmlFile, "<BR>\n");
  166. }
  167. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  168. dest = NULL;
  169. length = 0;
  170. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(chineseString,
  171. PKIX_UTF8,
  172. &dest,
  173. &length,
  174. plContext));
  175. if (htmlFile != plContext) {
  176. (void) printf("%d bytes written to HTML file\n",
  177. fwrite(dest, length, 1, htmlFile));
  178. (void) fprintf(htmlFile, "<BR>\n");
  179. }
  180. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  181. dest = NULL;
  182. length = 0;
  183. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(jeanRenoString,
  184. PKIX_UTF8,
  185. &dest,
  186. &length,
  187. plContext));
  188. if (htmlFile != plContext) {
  189. (void) printf("%d bytes written to HTML file\n",
  190. fwrite(dest, length, 1, htmlFile));
  191. (void) fprintf(htmlFile, "<BR>\n");
  192. }
  193. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  194. dest = NULL;
  195. length = 0;
  196. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(vivaEspanaString,
  197. PKIX_UTF8,
  198. &dest,
  199. &length,
  200. plContext));
  201. if (htmlFile != plContext) {
  202. (void) printf("%d bytes written to HTML file\n",
  203. fwrite(dest, length, 1, htmlFile));
  204. (void) fprintf(htmlFile, "<BR>\n");
  205. }
  206. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  207. dest = NULL;
  208. length = 0;
  209. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString,
  210. PKIX_UTF8,
  211. &dest,
  212. &length,
  213. plContext));
  214. if (htmlFile != plContext) {
  215. (void) printf("%d bytes written to HTML file\n",
  216. fwrite(dest, length, 1, htmlFile));
  217. (void) fprintf(htmlFile, "<BR>\n");
  218. }
  219. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  220. dest = NULL;
  221. length = 0;
  222. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString,
  223. PKIX_UTF8,
  224. &dest,
  225. &length,
  226. plContext));
  227. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  228. dest = NULL;
  229. length = 0;
  230. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(gorbachevString,
  231. PKIX_UTF8,
  232. &dest,
  233. &length,
  234. plContext));
  235. if (htmlFile != plContext) {
  236. (void) printf("%d bytes written to HTML file\n",
  237. fwrite(dest, length, 1, htmlFile));
  238. (void) fprintf(htmlFile, "<BR>\n");
  239. }
  240. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
  241. dest = NULL;
  242. length = 0;
  243. if (htmlFile != plContext) {
  244. (void) fprintf(htmlFile, "</font>\n");
  245. (void) fprintf(htmlFile, "</body></html>\n");
  246. (void) fclose(htmlFile);
  247. }
  248. cleanup:
  249. PKIX_TEST_RETURN();
  250. }
  251. static void
  252. testDestroy(
  253. PKIX_PL_String *string)
  254. {
  255. PKIX_TEST_STD_VARS();
  256. PKIX_TEST_DECREF_BC(string);
  257. cleanup:
  258. PKIX_TEST_RETURN();
  259. }
  260. int test_string2(int argc, char *argv[]) {
  261. PKIX_PL_String *vivaEspanaString, *straussString, *testUTF16String;
  262. PKIX_PL_String *chineseString, *jeanRenoString, *gorbachevString;
  263. PKIX_UInt32 actualMinorVersion;
  264. PKIX_UInt32 j = 0;
  265. PKIX_TEST_STD_VARS();
  266. startTests("Unicode Strings");
  267. PKIX_TEST_EXPECT_NO_ERROR(
  268. PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
  269. subTest("PKIX_PL_String_Create");
  270. createString(&vivaEspanaString,
  271. &straussString,
  272. &gorbachevString,
  273. &testUTF16String,
  274. &chineseString,
  275. &jeanRenoString);
  276. subTest("Converting UTF-16 to EscASCII");
  277. testGetEncoded(testUTF16String, PKIX_ESCASCII);
  278. subTest("Converting UTF-8 to EscASCII");
  279. testGetEncoded(chineseString, PKIX_ESCASCII);
  280. subTest("Converting UTF-8 to EscASCII");
  281. testGetEncoded(jeanRenoString, PKIX_ESCASCII);
  282. subTest("Converting EscASCII to UTF-16");
  283. testGetEncoded(vivaEspanaString, PKIX_UTF16);
  284. subTest("Converting UTF-8 to UTF-16");
  285. testGetEncoded(chineseString, PKIX_UTF16);
  286. subTest("Creating HTML Output File \'utf8.html\'");
  287. testHTMLOutput(vivaEspanaString,
  288. straussString,
  289. gorbachevString,
  290. testUTF16String,
  291. chineseString,
  292. jeanRenoString);
  293. subTest("Unicode Destructors");
  294. testDestroy(testUTF16String);
  295. testDestroy(chineseString);
  296. testDestroy(jeanRenoString);
  297. testDestroy(vivaEspanaString);
  298. testDestroy(straussString);
  299. testDestroy(gorbachevString);
  300. cleanup:
  301. PKIX_Shutdown(plContext);
  302. PKIX_TEST_RETURN();
  303. endTests("Unicode Strings");
  304. return (0);
  305. }