OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 /* | 4 /* |
5 * test_object.c | 5 * test_object.c |
6 * | 6 * |
7 * Test Object Allocation, toString, Equals, Destruction | 7 * Test Object Allocation, toString, Equals, Destruction |
8 * | 8 * |
9 */ | 9 */ |
10 | 10 |
11 #include "testutil.h" | 11 #include "testutil.h" |
12 #include "testutil_nss.h" | 12 #include "testutil_nss.h" |
13 | 13 |
14 static void *plContext = NULL; | 14 static void *plContext = NULL; |
15 | 15 |
16 static PKIX_Error * | 16 static PKIX_Error *destructor( |
17 destructor( | 17 /* ARGSUSED */ PKIX_PL_Object *object, |
18 /* ARGSUSED */ PKIX_PL_Object *object, | 18 /* ARGSUSED */ void *plContext) { |
19 /* ARGSUSED */ void *plContext) | 19 (void)printf("\tUser defined destructor called\n"); |
20 { | 20 return (NULL); |
21 (void) printf("\tUser defined destructor called\n"); | 21 } |
22 return (NULL); | 22 |
23 } | 23 static PKIX_Error *toStringCallback(PKIX_PL_Object *obj, |
24 | 24 PKIX_PL_String **pString, |
25 static PKIX_Error* | 25 /* ARGSUSED */ void *plContext) { |
26 toStringCallback( | 26 |
27 PKIX_PL_Object *obj, | 27 PKIX_Error *errorResult; |
28 PKIX_PL_String **pString, | 28 PKIX_UInt32 type; |
29 /* ARGSUSED */ void* plContext) { | 29 char *format = "(addr: %x, type: %d)"; |
30 | 30 PKIX_PL_String *formatString = NULL; |
31 PKIX_Error *errorResult; | 31 |
32 PKIX_UInt32 type; | 32 errorResult = PKIX_PL_String_Create(PKIX_ESCASCII, format, PL_strlen(format), |
33 char *format = "(addr: %x, type: %d)"; | 33 &formatString, plContext); |
34 PKIX_PL_String *formatString = NULL; | 34 if (errorResult) testError("PKIX_PL_String_Create failed"); |
35 | 35 |
36 errorResult = PKIX_PL_String_Create( | 36 if (pString == plContext) testError("Null String"); |
37 PKIX_ESCASCII, | 37 |
38 format, | 38 type = (unsigned int)0; |
39 PL_strlen(format), | 39 |
40 &formatString, | 40 (void)PKIX_PL_Object_GetType(obj, &type, plContext); |
41 plContext); | 41 |
42 if (errorResult) testError("PKIX_PL_String_Create failed"); | 42 errorResult = |
43 | 43 PKIX_PL_Sprintf(pString, plContext, formatString, (int)obj, type); |
44 if (pString == plContext) | 44 if (errorResult) testError("PKIX_PL_Sprintf failed"); |
45 testError("Null String"); | 45 |
46 | 46 errorResult = |
47 type = (unsigned int)0; | 47 PKIX_PL_Object_DecRef((PKIX_PL_Object *)formatString, plContext); |
48 | 48 if (errorResult) testError("PKIX_PL_Object_DecRef failed"); |
49 (void) PKIX_PL_Object_GetType(obj, &type, plContext); | 49 |
50 | 50 return (NULL); |
51 errorResult = PKIX_PL_Sprintf(pString, plContext, | 51 } |
52 formatString, | 52 |
53 (int)obj, type); | 53 static PKIX_Error *comparator(PKIX_PL_Object *first, PKIX_PL_Object *second, |
54 if (errorResult) testError("PKIX_PL_Sprintf failed"); | 54 PKIX_Int32 *pValue, |
55 | 55 /* ARGSUSED */ void *plContext) { |
56 | 56 if (*(char *)first > *(char *)second) |
57 errorResult = PKIX_PL_Object_DecRef((PKIX_PL_Object*)formatString, | 57 *pValue = 1; |
58 plContext); | 58 else if (*(char *)first < *(char *)second) |
59 if (errorResult) testError("PKIX_PL_Object_DecRef failed"); | 59 *pValue = -1; |
60 | 60 else |
61 return (NULL); | 61 *pValue = 0; |
62 } | 62 return (NULL); |
63 | 63 } |
64 static PKIX_Error * | 64 |
65 comparator( | 65 static PKIX_Error *hashcodeCallback(PKIX_PL_Object *object, PKIX_UInt32 *pValue, |
66 PKIX_PL_Object *first, | 66 /* ARGSUSED */ void *plContext) { |
67 PKIX_PL_Object *second, | 67 *pValue = 123456789; |
68 PKIX_Int32 *pValue, | 68 return (NULL); |
69 /* ARGSUSED */ void *plContext) | 69 } |
70 { | 70 |
71 if (*(char *)first > *(char *)second) | 71 static PKIX_Error *equalsCallback(PKIX_PL_Object *first, PKIX_PL_Object *second, |
72 *pValue = 1; | 72 PKIX_Boolean *result, void *plContext) { |
73 else if (*(char *)first < *(char *)second) | 73 |
74 *pValue = -1; | 74 PKIX_UInt32 firstType = 0, secondType = 0; |
75 else | 75 |
76 *pValue = 0; | 76 if ((first == plContext) || (second == plContext)) testError("Null Object"); |
77 return (NULL); | 77 |
78 } | 78 (void)PKIX_PL_Object_GetType(first, &firstType, plContext); |
79 | 79 |
80 | 80 (void)PKIX_PL_Object_GetType(second, &secondType, plContext); |
81 static PKIX_Error * | 81 |
82 hashcodeCallback( | 82 *result = (firstType == secondType) ? PKIX_TRUE : PKIX_FALSE; |
83 PKIX_PL_Object *object, | 83 |
84 PKIX_UInt32 *pValue, | 84 return (NULL); |
85 /* ARGSUSED */ void *plContext) | 85 } |
86 { | 86 |
87 *pValue = 123456789; | 87 static void createObjects(PKIX_PL_Object **obj, PKIX_PL_Object **obj2, |
88 return (NULL); | 88 PKIX_PL_Object **obj3, PKIX_PL_Object **obj4) { |
89 } | 89 PKIX_TEST_STD_VARS(); |
90 | |
91 static PKIX_Error* | |
92 equalsCallback( | |
93 PKIX_PL_Object *first, | |
94 PKIX_PL_Object *second, | |
95 PKIX_Boolean *result, | |
96 void* plContext) { | |
97 | |
98 PKIX_UInt32 firstType = 0, secondType = 0; | |
99 | |
100 if ((first == plContext)||(second == plContext)) | |
101 testError("Null Object"); | |
102 | |
103 (void) PKIX_PL_Object_GetType(first, &firstType, plContext); | |
104 | |
105 (void) PKIX_PL_Object_GetType(second, &secondType, plContext); | |
106 | |
107 *result = (firstType == secondType)?PKIX_TRUE:PKIX_FALSE; | |
108 | |
109 return (NULL); | |
110 } | |
111 | |
112 static void | |
113 createObjects( | |
114 PKIX_PL_Object **obj, | |
115 PKIX_PL_Object **obj2, | |
116 PKIX_PL_Object **obj3, | |
117 PKIX_PL_Object **obj4) | |
118 { | |
119 PKIX_TEST_STD_VARS(); | |
120 | 90 |
121 #ifdef PKIX_USER_OBJECT_TYPE | 91 #ifdef PKIX_USER_OBJECT_TYPE |
122 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType | 92 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType( |
123 (1000, /* type */ | 93 1000, /* type */ |
124 "thousand", /* description */ | 94 "thousand", /* description */ |
125 NULL, /* destructor */ | 95 NULL, /* destructor */ |
126 NULL, /* equals */ | 96 NULL, /* equals */ |
127 (PKIX_PL_HashcodeCallback)hashcodeCallback, | 97 (PKIX_PL_HashcodeCallback)hashcodeCallback, NULL, /* toString */ |
128 NULL, /* toString */ | 98 NULL, /* Comparator */ |
129 NULL, /* Comparator */ | 99 NULL, plContext)); |
130 NULL, | 100 |
131 plContext)); | 101 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc(1000, /* type */ |
132 | 102 12, /* size */ |
133 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc | 103 obj, plContext)); |
134 (1000, /* type */ | 104 |
135 12, /* size */ | 105 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType( |
136 obj, | 106 2000, /* type */ |
137 plContext)); | 107 "two thousand" /* description */, (PKIX_PL_DestructorCallback)destructor, |
138 | 108 (PKIX_PL_EqualsCallback)equalsCallback, NULL, /* hashcode */ |
139 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType | 109 (PKIX_PL_ToStringCallback)toStringCallback, |
140 (2000, /* type */ | 110 (PKIX_PL_ComparatorCallback)comparator, NULL, plContext)); |
141 "two thousand" /* description */, | 111 |
142 (PKIX_PL_DestructorCallback)destructor, | 112 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc(2000, /* type */ |
143 (PKIX_PL_EqualsCallback)equalsCallback, | 113 1, /* size */ |
144 NULL, /* hashcode */ | 114 obj2, plContext)); |
145 (PKIX_PL_ToStringCallback)toStringCallback, | 115 |
146 (PKIX_PL_ComparatorCallback)comparator, | 116 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc(2000, /* type */ |
147 NULL, | 117 1, /* size */ |
148 plContext)); | 118 obj4, plContext)); |
149 | 119 |
150 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc | 120 *obj3 = *obj; |
151 (2000, /* type */ | 121 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_IncRef(*obj3, plContext)); |
152 1, /* size */ | |
153 obj2, | |
154 plContext)); | |
155 | |
156 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc | |
157 (2000, /* type */ | |
158 1, /* size */ | |
159 obj4, | |
160 plContext)); | |
161 | |
162 *obj3 = *obj; | |
163 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_IncRef(*obj3, plContext)); | |
164 | 122 |
165 cleanup: | 123 cleanup: |
166 #endif /* PKIX_USER_OBJECT_TYPE */ | 124 #endif /* PKIX_USER_OBJECT_TYPE */ |
167 PKIX_TEST_RETURN(); | 125 PKIX_TEST_RETURN(); |
168 } | 126 } |
169 | 127 |
170 | 128 static void testGetType(PKIX_PL_Object *obj, PKIX_PL_Object *obj2, |
171 static void | 129 PKIX_PL_Object *obj3) { |
172 testGetType( | 130 PKIX_UInt32 testType; |
173 PKIX_PL_Object *obj, | 131 |
174 PKIX_PL_Object *obj2, | 132 PKIX_TEST_STD_VARS(); |
175 PKIX_PL_Object *obj3) | 133 |
176 { | 134 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_GetType(obj, &testType, plContext)); |
177 PKIX_UInt32 testType; | 135 |
178 | 136 if (testType != 1000) testError("Object 1 returned the wrong type"); |
179 PKIX_TEST_STD_VARS(); | 137 |
180 | 138 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_GetType(obj2, &testType, plContext)); |
181 PKIX_TEST_EXPECT_NO_ERROR | 139 if (testType != 2000) testError("Object 2 returned the wrong type"); |
182 (PKIX_PL_Object_GetType(obj, &testType, plContext)); | 140 |
183 | 141 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_GetType(obj3, &testType, plContext)); |
184 if (testType != 1000) | 142 if (testType != 1000) testError("Object 3 returned the wrong type"); |
185 testError("Object 1 returned the wrong type"); | 143 |
186 | 144 cleanup: |
187 PKIX_TEST_EXPECT_NO_ERROR | 145 |
188 (PKIX_PL_Object_GetType(obj2, &testType, plContext)); | 146 PKIX_TEST_RETURN(); |
189 if (testType != 2000) | 147 } |
190 testError("Object 2 returned the wrong type"); | 148 |
191 | 149 static void testCompare(PKIX_PL_Object *obj2, PKIX_PL_Object *obj4) { |
192 PKIX_TEST_EXPECT_NO_ERROR | 150 PKIX_Int32 cmpResult; |
193 (PKIX_PL_Object_GetType(obj3, &testType, plContext)); | 151 PKIX_TEST_STD_VARS(); |
194 if (testType != 1000) | 152 |
195 testError("Object 3 returned the wrong type"); | 153 *(char *)obj2 = 0x20; |
196 | 154 *(char *)obj4 = 0x10; |
197 cleanup: | 155 PKIX_TEST_EXPECT_NO_ERROR( |
198 | 156 PKIX_PL_Object_Compare(obj2, obj4, &cmpResult, plContext)); |
199 PKIX_TEST_RETURN(); | 157 if (cmpResult <= 0) testError("Invalid Result from Object Compare"); |
200 } | 158 |
201 | 159 PKIX_TEST_EXPECT_NO_ERROR( |
202 static void | 160 PKIX_PL_Object_Compare(obj4, obj2, &cmpResult, plContext)); |
203 testCompare( | 161 if (cmpResult >= 0) testError("Invalid Result from Object Compare"); |
204 PKIX_PL_Object *obj2, | 162 |
205 PKIX_PL_Object *obj4) | 163 PKIX_TEST_EXPECT_NO_ERROR( |
206 { | 164 PKIX_PL_Object_Compare(obj4, obj4, &cmpResult, plContext)); |
207 PKIX_Int32 cmpResult; | 165 |
208 PKIX_TEST_STD_VARS(); | 166 *(char *)obj2 = 0x10; |
209 | 167 if (cmpResult != 0) testError("Invalid Result from Object Compare"); |
210 *(char *)obj2 = 0x20; | 168 |
211 *(char *)obj4 = 0x10; | 169 cleanup: |
212 PKIX_TEST_EXPECT_NO_ERROR | 170 |
213 (PKIX_PL_Object_Compare(obj2, obj4, &cmpResult, plContext)); | 171 PKIX_TEST_RETURN(); |
214 if (cmpResult <= 0) testError("Invalid Result from Object Compare"); | 172 } |
215 | 173 |
216 PKIX_TEST_EXPECT_NO_ERROR | 174 static void testDestroy(PKIX_PL_Object *obj, PKIX_PL_Object *obj2, |
217 (PKIX_PL_Object_Compare(obj4, obj2, &cmpResult, plContext)); | 175 PKIX_PL_Object *obj3, PKIX_PL_Object *obj4) { |
218 if (cmpResult >= 0) testError("Invalid Result from Object Compare"); | 176 PKIX_TEST_STD_VARS(); |
219 | 177 |
220 PKIX_TEST_EXPECT_NO_ERROR | 178 PKIX_TEST_DECREF_BC(obj); |
221 (PKIX_PL_Object_Compare(obj4, obj4, &cmpResult, plContext)); | 179 PKIX_TEST_DECREF_BC(obj2); |
222 | 180 PKIX_TEST_DECREF_BC(obj3); |
223 *(char *)obj2 = 0x10; | 181 PKIX_TEST_DECREF_BC(obj4); |
224 if (cmpResult != 0) testError("Invalid Result from Object Compare"); | 182 |
225 | 183 cleanup: |
226 | 184 |
227 cleanup: | 185 PKIX_TEST_RETURN(); |
228 | |
229 PKIX_TEST_RETURN(); | |
230 } | |
231 | |
232 | |
233 static void | |
234 testDestroy( | |
235 PKIX_PL_Object *obj, | |
236 PKIX_PL_Object *obj2, | |
237 PKIX_PL_Object *obj3, | |
238 PKIX_PL_Object *obj4) | |
239 { | |
240 PKIX_TEST_STD_VARS(); | |
241 | |
242 PKIX_TEST_DECREF_BC(obj); | |
243 PKIX_TEST_DECREF_BC(obj2); | |
244 PKIX_TEST_DECREF_BC(obj3); | |
245 PKIX_TEST_DECREF_BC(obj4); | |
246 | |
247 cleanup: | |
248 | |
249 PKIX_TEST_RETURN(); | |
250 } | 186 } |
251 | 187 |
252 int test_object(int argc, char *argv[]) { | 188 int test_object(int argc, char *argv[]) { |
253 | 189 |
254 #ifdef PKIX_USER_OBJECT_TYPE | 190 #ifdef PKIX_USER_OBJECT_TYPE |
255 PKIX_PL_Object *obj, *obj2, *obj3, *obj4; | 191 PKIX_PL_Object *obj, *obj2, *obj3, *obj4; |
256 PKIX_UInt32 actualMinorVersion; | 192 PKIX_UInt32 actualMinorVersion; |
257 PKIX_UInt32 j = 0; | 193 PKIX_UInt32 j = 0; |
258 | 194 |
259 PKIX_TEST_STD_VARS(); | 195 PKIX_TEST_STD_VARS(); |
260 | 196 |
261 startTests("Objects"); | 197 startTests("Objects"); |
262 | 198 |
263 PKIX_TEST_EXPECT_NO_ERROR( | 199 PKIX_TEST_EXPECT_NO_ERROR( |
264 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); | 200 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); |
265 | 201 |
266 subTest("PKIX_PL_Object_Create"); | 202 subTest("PKIX_PL_Object_Create"); |
267 createObjects(&obj, &obj2, &obj3, &obj4); | 203 createObjects(&obj, &obj2, &obj3, &obj4); |
268 | 204 |
269 PKIX_TEST_EQ_HASH_TOSTR_DUP(obj, obj3, obj2, NULL, Object, PKIX_FALSE); | 205 PKIX_TEST_EQ_HASH_TOSTR_DUP(obj, obj3, obj2, NULL, Object, PKIX_FALSE); |
270 | 206 |
271 subTest("PKIX_PL_Object_GetType"); | 207 subTest("PKIX_PL_Object_GetType"); |
272 testGetType(obj, obj2, obj3); | 208 testGetType(obj, obj2, obj3); |
273 | 209 |
274 subTest("PKIX_PL_Object_Compare"); | 210 subTest("PKIX_PL_Object_Compare"); |
275 testCompare(obj2, obj4); | 211 testCompare(obj2, obj4); |
276 | 212 |
277 subTest("PKIX_PL_Object_Destroy"); | 213 subTest("PKIX_PL_Object_Destroy"); |
278 testDestroy(obj, obj2, obj3, obj4); | 214 testDestroy(obj, obj2, obj3, obj4); |
279 | 215 |
280 | 216 cleanup: |
281 cleanup: | 217 |
282 | 218 PKIX_Shutdown(plContext); |
283 PKIX_Shutdown(plContext); | 219 |
284 | 220 endTests("Objects"); |
285 endTests("Objects"); | |
286 #endif /* PKIX_USER_OBJECT_TYPE */ | 221 #endif /* PKIX_USER_OBJECT_TYPE */ |
287 return (0); | 222 return (0); |
288 | 223 } |
289 } | |
OLD | NEW |