/security/nss/cmd/libpkix/testutil/testutil.h

http://github.com/zpao/v8monkey · C Header · 337 lines · 166 code · 36 blank · 135 comment · 22 complexity · 84e70484c8393d4207779038b14f11ff 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. * testutil.h
  39. *
  40. * Utility functions for handling test errors
  41. *
  42. */
  43. #ifndef _TESTUTIL_H
  44. #define _TESTUTIL_H
  45. #include "pkix.h"
  46. #include "plstr.h"
  47. #include "prprf.h"
  48. #include "prlong.h"
  49. #include "pkix_pl_common.h"
  50. #include "secutil.h"
  51. #include <stdio.h>
  52. #include <ctype.h>
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /*
  57. * In order to have a consistent format for displaying test information,
  58. * all tests are REQUIRED to use the functions provided by this library
  59. * (libtestutil.a) for displaying their information.
  60. *
  61. * A test using this library begins with a call to startTests with the test
  62. * name as the arg (which is used only for formatting). Before the first
  63. * subtest, a call to subTest should be made with the subtest name as the arg
  64. * (again, for formatting). If the subTest is successful, then no action
  65. * is needed. However, if the subTest is not successful, then a call
  66. * to testError should be made with a descriptive error message as the arg.
  67. * Note that a subTest MUST NOT call testError more than once.
  68. * Finally, a call to endTests is made with the test name as the arg (for
  69. * formatting). Note that most of these macros assume that a variable named
  70. * "plContext" of type (void *) has been defined by the test. As such, it
  71. * is essential that the test satisfy this condition.
  72. */
  73. /*
  74. * PKIX_TEST_STD_VARS should be called at the beginning of every function
  75. * that uses PKIX_TEST_RETURN (e.g. subTests), but it should be called only
  76. * AFTER declaring local variables (so we don't get compiler warnings about
  77. * declarations after statements). PKIX_TEST_STD_VARS declares and initializes
  78. * several variables needed by the other test macros.
  79. */
  80. #define PKIX_TEST_STD_VARS() \
  81. PKIX_Error *pkixTestErrorResult = NULL; \
  82. char *pkixTestErrorMsg = NULL;
  83. /*
  84. * PKIX_TEST_EXPECT_NO_ERROR should be used to wrap a standard PKIX function
  85. * call (one which returns a pointer to PKIX_Error) that is expected to return
  86. * NULL (i.e. to succeed). If "pkixTestErrorResult" is not NULL,
  87. * "goto cleanup" is executed, where a testError call is made if there were
  88. * unexpected results. This macro MUST NOT be called after the "cleanup" label.
  89. *
  90. * Example Usage: PKIX_TEST_EXPECT_NO_ERROR(pkixFunc_expected_to_succeed(...));
  91. */
  92. #define PKIX_TEST_EXPECT_NO_ERROR(func) \
  93. do { \
  94. pkixTestErrorResult = (func); \
  95. if (pkixTestErrorResult) { \
  96. goto cleanup; \
  97. } \
  98. } while (0)
  99. /*
  100. * PKIX_TEST_EXPECT_ERROR should be used to wrap a standard PKIX function call
  101. * (one which returns a pointer to PKIX_Error) that is expected to return
  102. * a non-NULL value (i.e. to fail). If "pkixTestErrorResult" is NULL,
  103. * "pkixTestErrorMsg" is set to a standard string and "goto cleanup"
  104. * is executed, where a testError call is made if there were unexpected
  105. * results. This macro MUST NOT be called after the "cleanup" label.
  106. *
  107. * Example Usage: PKIX_TEST_EXPECT_ERROR(pkixFunc_expected_to_fail(...));
  108. */
  109. #define PKIX_TEST_EXPECT_ERROR(func) \
  110. do { \
  111. pkixTestErrorResult = (func); \
  112. if (!pkixTestErrorResult){ \
  113. pkixTestErrorMsg = \
  114. "Should have thrown an error here."; \
  115. goto cleanup; \
  116. } \
  117. PKIX_TEST_DECREF_BC(pkixTestErrorResult); \
  118. } while (0)
  119. /*
  120. * PKIX_TEST_DECREF_BC is a convenience macro which should only be called
  121. * BEFORE the "cleanup" label ("BC"). If the input parameter is non-NULL, it
  122. * DecRefs the input parameter and wraps the function with
  123. * PKIX_TEST_EXPECT_NO_ERROR, which executes "goto cleanup" upon error.
  124. * This macro MUST NOT be called after the "cleanup" label.
  125. */
  126. #define PKIX_TEST_DECREF_BC(obj) \
  127. do { \
  128. if (obj){ \
  129. PKIX_TEST_EXPECT_NO_ERROR \
  130. (PKIX_PL_Object_DecRef \
  131. ((PKIX_PL_Object*)(obj), plContext)); \
  132. obj = NULL; \
  133. } \
  134. } while (0)
  135. /*
  136. * PKIX_TEST_DECREF_AC is a convenience macro which should only be called
  137. * AFTER the "cleanup" label ("AC"). If the input parameter is non-NULL, it
  138. * DecRefs the input parameter. A pkixTestTempResult variable is used to prevent
  139. * incorrectly overwriting pkixTestErrorResult with NULL.
  140. * In the case DecRef succeeds, pkixTestTempResult will be NULL, and we won't
  141. * overwrite a previously set pkixTestErrorResult (if any). If DecRef fails,
  142. * then we do want to overwrite a previously set pkixTestErrorResult since a
  143. * DecRef failure is fatal and may be indicative of memory corruption.
  144. */
  145. #define PKIX_TEST_DECREF_AC(obj) \
  146. do { \
  147. if (obj){ \
  148. PKIX_Error *pkixTestTempResult = NULL; \
  149. pkixTestTempResult = \
  150. PKIX_PL_Object_DecRef \
  151. ((PKIX_PL_Object*)(obj), plContext); \
  152. if (pkixTestTempResult) \
  153. pkixTestErrorResult = pkixTestTempResult; \
  154. obj = NULL; \
  155. } \
  156. } while (0)
  157. /*
  158. * PKIX_TEST_RETURN must always be AFTER the "cleanup" label. It does nothing
  159. * if everything went as expected. However, if there were unexpected results,
  160. * PKIX_TEST_RETURN calls testError, which displays a standard failure message
  161. * and increments the number of subtests that have failed. In the case
  162. * of an unexpected error, testError is called using the error's description
  163. * as an input and the error is DecRef'd. In the case of unexpected success
  164. * testError is called with a standard string.
  165. */
  166. #define PKIX_TEST_RETURN() \
  167. { \
  168. if (pkixTestErrorMsg){ \
  169. testError(pkixTestErrorMsg); \
  170. } else if (pkixTestErrorResult){ \
  171. pkixTestErrorMsg = \
  172. PKIX_Error2ASCII \
  173. (pkixTestErrorResult, plContext); \
  174. if (pkixTestErrorMsg) { \
  175. testError(pkixTestErrorMsg); \
  176. PKIX_PL_Free \
  177. ((PKIX_PL_Object *)pkixTestErrorMsg, \
  178. plContext); \
  179. } else { \
  180. testError("PKIX_Error2ASCII Failed"); \
  181. } \
  182. if (pkixTestErrorResult != PKIX_ALLOC_ERROR()){ \
  183. PKIX_PL_Object_DecRef \
  184. ((PKIX_PL_Object*)pkixTestErrorResult, \
  185. plContext); \
  186. pkixTestErrorResult = NULL; \
  187. } \
  188. } \
  189. }
  190. /*
  191. * PKIX_TEST_EQ_HASH_TOSTR_DUP is a convenience macro which executes the
  192. * standard set of operations that test the Equals, Hashcode, ToString, and
  193. * Duplicate functions of an object type. The goodObj, equalObj, and diffObj
  194. * are as the names suggest. The expAscii parameter is the expected result of
  195. * calling ToString on the goodObj. If expAscii is NULL, then ToString will
  196. * not be called on the goodObj. The checkDuplicate parameter is treated as
  197. * a Boolean to indicate whether the Duplicate function should be tested. If
  198. * checkDuplicate is NULL, then Duplicate will not be called on the goodObj.
  199. * The type is the name of the function's family. For example, if the type is
  200. * Cert, this macro will call PKIX_PL_Cert_Equals, PKIX_PL_Cert_Hashcode, and
  201. * PKIX_PL_Cert_ToString.
  202. *
  203. * Note: If goodObj uses the default Equals and Hashcode functions, then
  204. * for goodObj and equalObj to be equal, they must have the same pointer value.
  205. */
  206. #define PKIX_TEST_EQ_HASH_TOSTR_DUP(goodObj, equalObj, diffObj, \
  207. expAscii, type, checkDuplicate) \
  208. do { \
  209. subTest("PKIX_PL_" #type "_Equals <match>"); \
  210. testEqualsHelper \
  211. ((PKIX_PL_Object *)(goodObj), \
  212. (PKIX_PL_Object *)(equalObj), \
  213. PKIX_TRUE, \
  214. plContext); \
  215. subTest("PKIX_PL_" #type "_Hashcode <match>"); \
  216. testHashcodeHelper \
  217. ((PKIX_PL_Object *)(goodObj), \
  218. (PKIX_PL_Object *)(equalObj), \
  219. PKIX_TRUE, \
  220. plContext); \
  221. subTest("PKIX_PL_" #type "_Equals <non-match>"); \
  222. testEqualsHelper \
  223. ((PKIX_PL_Object *)(goodObj), \
  224. (PKIX_PL_Object *)(diffObj), \
  225. PKIX_FALSE, \
  226. plContext); \
  227. subTest("PKIX_PL_" #type "_Hashcode <non-match>"); \
  228. testHashcodeHelper \
  229. ((PKIX_PL_Object *)(goodObj), \
  230. (PKIX_PL_Object *)(diffObj), \
  231. PKIX_FALSE, \
  232. plContext); \
  233. if (expAscii){ \
  234. subTest("PKIX_PL_" #type "_ToString"); \
  235. testToStringHelper \
  236. ((PKIX_PL_Object *)(goodObj), \
  237. (expAscii), \
  238. plContext); } \
  239. if (checkDuplicate){ \
  240. subTest("PKIX_PL_" #type "_Duplicate"); \
  241. testDuplicateHelper \
  242. ((PKIX_PL_Object *)goodObj, plContext); } \
  243. } while (0)
  244. /*
  245. * PKIX_TEST_DECREF_BC is a convenience macro which should only be called
  246. * BEFORE the "cleanup" label ("BC"). If the input parameter is non-NULL, it
  247. * DecRefs the input parameter and wraps the function with
  248. * PKIX_TEST_EXPECT_NO_ERROR, which executes "goto cleanup" upon error.
  249. * This macro MUST NOT be called after the "cleanup" label.
  250. */
  251. #define PKIX_TEST_ABORT_ON_NULL(obj) \
  252. do { \
  253. if (!obj){ \
  254. goto cleanup; \
  255. } \
  256. } while (0)
  257. #define PKIX_TEST_ARENAS_ARG(arena) \
  258. (arena? \
  259. (PORT_Strcmp(arena, "arenas") ? PKIX_FALSE : (j++, PKIX_TRUE)): \
  260. PKIX_FALSE)
  261. #define PKIX_TEST_ERROR_RECEIVED (pkixTestErrorMsg || pkixTestErrorResult)
  262. /* see source file for function documentation */
  263. void startTests(char *testName);
  264. void endTests(char *testName);
  265. void subTest(char *subTestName);
  266. void testError(char *msg);
  267. extern PKIX_Error *
  268. _ErrorCheck(PKIX_Error *errorResult);
  269. extern PKIX_Error *
  270. _OutputError(PKIX_Error *errorResult);
  271. char* PKIX_String2ASCII(PKIX_PL_String *string, void *plContext);
  272. char* PKIX_Error2ASCII(PKIX_Error *error, void *plContext);
  273. char* PKIX_Object2ASCII(PKIX_PL_Object *object);
  274. char *PKIX_Cert2ASCII(PKIX_PL_Cert *cert);
  275. void
  276. testHashcodeHelper(
  277. PKIX_PL_Object *goodObject,
  278. PKIX_PL_Object *otherObject,
  279. PKIX_Boolean match,
  280. void *plContext);
  281. void
  282. testToStringHelper(
  283. PKIX_PL_Object *goodObject,
  284. char *expected,
  285. void *plContext);
  286. void
  287. testEqualsHelper(
  288. PKIX_PL_Object *goodObject,
  289. PKIX_PL_Object *otherObject,
  290. PKIX_Boolean match,
  291. void *plContext);
  292. void
  293. testDuplicateHelper(
  294. PKIX_PL_Object *object,
  295. void *plContext);
  296. void
  297. testErrorUndo(char *msg);
  298. #ifdef __cplusplus
  299. }
  300. #endif
  301. #endif /* TESTUTIL_H */