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

http://github.com/zpao/v8monkey · C · 322 lines · 212 code · 68 blank · 42 comment · 22 complexity · 7bb4eb3910cf9a42101d679b2bfb3cf9 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_object.c
  39. *
  40. * Test Object Allocation, toString, Equals, Destruction
  41. *
  42. */
  43. #include "testutil.h"
  44. #include "testutil_nss.h"
  45. static void *plContext = NULL;
  46. static PKIX_Error *
  47. destructor(
  48. /* ARGSUSED */ PKIX_PL_Object *object,
  49. /* ARGSUSED */ void *plContext)
  50. {
  51. (void) printf("\tUser defined destructor called\n");
  52. return (NULL);
  53. }
  54. static PKIX_Error*
  55. toStringCallback(
  56. PKIX_PL_Object *obj,
  57. PKIX_PL_String **pString,
  58. /* ARGSUSED */ void* plContext) {
  59. PKIX_Error *errorResult;
  60. PKIX_UInt32 type;
  61. char *format = "(addr: %x, type: %d)";
  62. PKIX_PL_String *formatString = NULL;
  63. errorResult = PKIX_PL_String_Create(
  64. PKIX_ESCASCII,
  65. format,
  66. PL_strlen(format),
  67. &formatString,
  68. plContext);
  69. if (errorResult) testError("PKIX_PL_String_Create failed");
  70. if (pString == plContext)
  71. testError("Null String");
  72. type = (unsigned int)0;
  73. (void) PKIX_PL_Object_GetType(obj, &type, plContext);
  74. errorResult = PKIX_PL_Sprintf(pString, plContext,
  75. formatString,
  76. (int)obj, type);
  77. if (errorResult) testError("PKIX_PL_Sprintf failed");
  78. errorResult = PKIX_PL_Object_DecRef((PKIX_PL_Object*)formatString,
  79. plContext);
  80. if (errorResult) testError("PKIX_PL_Object_DecRef failed");
  81. return (NULL);
  82. }
  83. static PKIX_Error *
  84. comparator(
  85. PKIX_PL_Object *first,
  86. PKIX_PL_Object *second,
  87. PKIX_Int32 *pValue,
  88. /* ARGSUSED */ void *plContext)
  89. {
  90. if (*(char *)first > *(char *)second)
  91. *pValue = 1;
  92. else if (*(char *)first < *(char *)second)
  93. *pValue = -1;
  94. else
  95. *pValue = 0;
  96. return (NULL);
  97. }
  98. static PKIX_Error *
  99. hashcodeCallback(
  100. PKIX_PL_Object *object,
  101. PKIX_UInt32 *pValue,
  102. /* ARGSUSED */ void *plContext)
  103. {
  104. *pValue = 123456789;
  105. return (NULL);
  106. }
  107. static PKIX_Error*
  108. equalsCallback(
  109. PKIX_PL_Object *first,
  110. PKIX_PL_Object *second,
  111. PKIX_Boolean *result,
  112. void* plContext) {
  113. PKIX_UInt32 firstType = 0, secondType = 0;
  114. if ((first == plContext)||(second == plContext))
  115. testError("Null Object");
  116. (void) PKIX_PL_Object_GetType(first, &firstType, plContext);
  117. (void) PKIX_PL_Object_GetType(second, &secondType, plContext);
  118. *result = (firstType == secondType)?PKIX_TRUE:PKIX_FALSE;
  119. return (NULL);
  120. }
  121. static void
  122. createObjects(
  123. PKIX_PL_Object **obj,
  124. PKIX_PL_Object **obj2,
  125. PKIX_PL_Object **obj3,
  126. PKIX_PL_Object **obj4)
  127. {
  128. PKIX_TEST_STD_VARS();
  129. #ifdef PKIX_USER_OBJECT_TYPE
  130. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType
  131. (1000, /* type */
  132. "thousand", /* description */
  133. NULL, /* destructor */
  134. NULL, /* equals */
  135. (PKIX_PL_HashcodeCallback)hashcodeCallback,
  136. NULL, /* toString */
  137. NULL, /* Comparator */
  138. NULL,
  139. plContext));
  140. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc
  141. (1000, /* type */
  142. 12, /* size */
  143. obj,
  144. plContext));
  145. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType
  146. (2000, /* type */
  147. "two thousand" /* description */,
  148. (PKIX_PL_DestructorCallback)destructor,
  149. (PKIX_PL_EqualsCallback)equalsCallback,
  150. NULL, /* hashcode */
  151. (PKIX_PL_ToStringCallback)toStringCallback,
  152. (PKIX_PL_ComparatorCallback)comparator,
  153. NULL,
  154. plContext));
  155. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc
  156. (2000, /* type */
  157. 1, /* size */
  158. obj2,
  159. plContext));
  160. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc
  161. (2000, /* type */
  162. 1, /* size */
  163. obj4,
  164. plContext));
  165. *obj3 = *obj;
  166. PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_IncRef(*obj3, plContext));
  167. cleanup:
  168. #endif /* PKIX_USER_OBJECT_TYPE */
  169. PKIX_TEST_RETURN();
  170. }
  171. static void
  172. testGetType(
  173. PKIX_PL_Object *obj,
  174. PKIX_PL_Object *obj2,
  175. PKIX_PL_Object *obj3)
  176. {
  177. PKIX_UInt32 testType;
  178. PKIX_TEST_STD_VARS();
  179. PKIX_TEST_EXPECT_NO_ERROR
  180. (PKIX_PL_Object_GetType(obj, &testType, plContext));
  181. if (testType != 1000)
  182. testError("Object 1 returned the wrong type");
  183. PKIX_TEST_EXPECT_NO_ERROR
  184. (PKIX_PL_Object_GetType(obj2, &testType, plContext));
  185. if (testType != 2000)
  186. testError("Object 2 returned the wrong type");
  187. PKIX_TEST_EXPECT_NO_ERROR
  188. (PKIX_PL_Object_GetType(obj3, &testType, plContext));
  189. if (testType != 1000)
  190. testError("Object 3 returned the wrong type");
  191. cleanup:
  192. PKIX_TEST_RETURN();
  193. }
  194. static void
  195. testCompare(
  196. PKIX_PL_Object *obj2,
  197. PKIX_PL_Object *obj4)
  198. {
  199. PKIX_Int32 cmpResult;
  200. PKIX_TEST_STD_VARS();
  201. *(char *)obj2 = 0x20;
  202. *(char *)obj4 = 0x10;
  203. PKIX_TEST_EXPECT_NO_ERROR
  204. (PKIX_PL_Object_Compare(obj2, obj4, &cmpResult, plContext));
  205. if (cmpResult <= 0) testError("Invalid Result from Object Compare");
  206. PKIX_TEST_EXPECT_NO_ERROR
  207. (PKIX_PL_Object_Compare(obj4, obj2, &cmpResult, plContext));
  208. if (cmpResult >= 0) testError("Invalid Result from Object Compare");
  209. PKIX_TEST_EXPECT_NO_ERROR
  210. (PKIX_PL_Object_Compare(obj4, obj4, &cmpResult, plContext));
  211. *(char *)obj2 = 0x10;
  212. if (cmpResult != 0) testError("Invalid Result from Object Compare");
  213. cleanup:
  214. PKIX_TEST_RETURN();
  215. }
  216. static void
  217. testDestroy(
  218. PKIX_PL_Object *obj,
  219. PKIX_PL_Object *obj2,
  220. PKIX_PL_Object *obj3,
  221. PKIX_PL_Object *obj4)
  222. {
  223. PKIX_TEST_STD_VARS();
  224. PKIX_TEST_DECREF_BC(obj);
  225. PKIX_TEST_DECREF_BC(obj2);
  226. PKIX_TEST_DECREF_BC(obj3);
  227. PKIX_TEST_DECREF_BC(obj4);
  228. cleanup:
  229. PKIX_TEST_RETURN();
  230. }
  231. int test_object(int argc, char *argv[]) {
  232. #ifdef PKIX_USER_OBJECT_TYPE
  233. PKIX_PL_Object *obj, *obj2, *obj3, *obj4;
  234. PKIX_UInt32 actualMinorVersion;
  235. PKIX_UInt32 j = 0;
  236. PKIX_TEST_STD_VARS();
  237. startTests("Objects");
  238. PKIX_TEST_EXPECT_NO_ERROR(
  239. PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
  240. subTest("PKIX_PL_Object_Create");
  241. createObjects(&obj, &obj2, &obj3, &obj4);
  242. PKIX_TEST_EQ_HASH_TOSTR_DUP(obj, obj3, obj2, NULL, Object, PKIX_FALSE);
  243. subTest("PKIX_PL_Object_GetType");
  244. testGetType(obj, obj2, obj3);
  245. subTest("PKIX_PL_Object_Compare");
  246. testCompare(obj2, obj4);
  247. subTest("PKIX_PL_Object_Destroy");
  248. testDestroy(obj, obj2, obj3, obj4);
  249. cleanup:
  250. PKIX_Shutdown(plContext);
  251. endTests("Objects");
  252. #endif /* PKIX_USER_OBJECT_TYPE */
  253. return (0);
  254. }