PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 44#include "testutil.h"
 45#include "testutil_nss.h"
 46
 47static void *plContext = NULL;
 48
 49static PKIX_Error *
 50destructor(
 51        /* ARGSUSED */ PKIX_PL_Object *object,
 52        /* ARGSUSED */ void *plContext)
 53{
 54        (void) printf("\tUser defined destructor called\n");
 55        return (NULL);
 56}
 57
 58static PKIX_Error*
 59toStringCallback(
 60        PKIX_PL_Object *obj,
 61        PKIX_PL_String **pString,
 62        /* ARGSUSED */ void* plContext) {
 63
 64        PKIX_Error *errorResult;
 65        PKIX_UInt32 type;
 66        char *format = "(addr: %x, type: %d)";
 67        PKIX_PL_String *formatString = NULL;
 68
 69        errorResult = PKIX_PL_String_Create(
 70                                        PKIX_ESCASCII,
 71                                        format,
 72                                        PL_strlen(format),
 73                                        &formatString,
 74                                        plContext);
 75        if (errorResult) testError("PKIX_PL_String_Create failed");
 76
 77        if (pString == plContext)
 78                testError("Null String");
 79
 80        type = (unsigned int)0;
 81
 82        (void) PKIX_PL_Object_GetType(obj, &type, plContext);
 83
 84        errorResult = PKIX_PL_Sprintf(pString, plContext,
 85                                    formatString,
 86                                    (int)obj, type);
 87        if (errorResult) testError("PKIX_PL_Sprintf failed");
 88
 89
 90        errorResult = PKIX_PL_Object_DecRef((PKIX_PL_Object*)formatString,
 91                                        plContext);
 92        if (errorResult) testError("PKIX_PL_Object_DecRef failed");
 93
 94        return (NULL);
 95}
 96
 97static PKIX_Error *
 98comparator(
 99        PKIX_PL_Object *first,
100        PKIX_PL_Object *second,
101        PKIX_Int32 *pValue,
102        /* ARGSUSED */ void *plContext)
103{
104        if (*(char *)first > *(char *)second)
105                *pValue = 1;
106        else if (*(char *)first < *(char *)second)
107                *pValue = -1;
108        else
109                *pValue = 0;
110        return (NULL);
111}
112
113
114static PKIX_Error *
115hashcodeCallback(
116        PKIX_PL_Object *object,
117        PKIX_UInt32 *pValue,
118        /* ARGSUSED */ void *plContext)
119{
120        *pValue = 123456789;
121        return (NULL);
122}
123
124static PKIX_Error*
125equalsCallback(
126        PKIX_PL_Object *first,
127        PKIX_PL_Object *second,
128        PKIX_Boolean *result,
129        void* plContext) {
130
131        PKIX_UInt32 firstType = 0, secondType = 0;
132
133        if ((first == plContext)||(second == plContext))
134                testError("Null Object");
135
136        (void) PKIX_PL_Object_GetType(first, &firstType, plContext);
137
138        (void) PKIX_PL_Object_GetType(second, &secondType, plContext);
139
140        *result = (firstType == secondType)?PKIX_TRUE:PKIX_FALSE;
141
142        return (NULL);
143}
144
145static void
146createObjects(
147        PKIX_PL_Object **obj,
148        PKIX_PL_Object **obj2,
149        PKIX_PL_Object **obj3,
150        PKIX_PL_Object **obj4)
151{
152        PKIX_TEST_STD_VARS();
153
154#ifdef PKIX_USER_OBJECT_TYPE
155        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType
156                    (1000, /* type */
157                    "thousand", /* description */
158                    NULL, /* destructor */
159                    NULL, /* equals */
160                    (PKIX_PL_HashcodeCallback)hashcodeCallback,
161                    NULL,  /* toString */
162                    NULL,  /* Comparator */
163                    NULL,
164                    plContext));
165
166        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc
167                    (1000, /* type */
168                    12, /* size */
169                    obj,
170                    plContext));
171
172        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType
173                    (2000, /* type */
174                    "two thousand" /* description */,
175                    (PKIX_PL_DestructorCallback)destructor,
176                    (PKIX_PL_EqualsCallback)equalsCallback,
177                    NULL, /* hashcode */
178                    (PKIX_PL_ToStringCallback)toStringCallback,
179                    (PKIX_PL_ComparatorCallback)comparator,
180                    NULL,
181                    plContext));
182
183        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc
184                    (2000, /* type */
185                    1, /* size */
186                    obj2,
187                    plContext));
188
189        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc
190                    (2000, /* type */
191                    1, /* size */
192                    obj4,
193                    plContext));
194
195        *obj3 = *obj;
196        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_IncRef(*obj3, plContext));
197
198cleanup:
199#endif /* PKIX_USER_OBJECT_TYPE */
200        PKIX_TEST_RETURN();
201}
202
203
204static void
205testGetType(
206            PKIX_PL_Object *obj,
207            PKIX_PL_Object *obj2,
208            PKIX_PL_Object *obj3)
209{
210        PKIX_UInt32 testType;
211
212        PKIX_TEST_STD_VARS();
213
214        PKIX_TEST_EXPECT_NO_ERROR
215                (PKIX_PL_Object_GetType(obj, &testType, plContext));
216
217        if (testType != 1000)
218                testError("Object 1 returned the wrong type");
219
220        PKIX_TEST_EXPECT_NO_ERROR
221                (PKIX_PL_Object_GetType(obj2, &testType, plContext));
222        if (testType != 2000)
223                testError("Object 2 returned the wrong type");
224
225        PKIX_TEST_EXPECT_NO_ERROR
226                (PKIX_PL_Object_GetType(obj3, &testType, plContext));
227        if (testType != 1000)
228                testError("Object 3 returned the wrong type");
229
230cleanup:
231
232        PKIX_TEST_RETURN();
233}
234
235static void
236testCompare(
237            PKIX_PL_Object *obj2,
238            PKIX_PL_Object *obj4)
239{
240        PKIX_Int32 cmpResult;
241        PKIX_TEST_STD_VARS();
242
243        *(char *)obj2 = 0x20;
244        *(char *)obj4 = 0x10;
245        PKIX_TEST_EXPECT_NO_ERROR
246                (PKIX_PL_Object_Compare(obj2, obj4, &cmpResult, plContext));
247        if (cmpResult <= 0) testError("Invalid Result from Object Compare");
248
249        PKIX_TEST_EXPECT_NO_ERROR
250                (PKIX_PL_Object_Compare(obj4, obj2, &cmpResult, plContext));
251        if (cmpResult >= 0) testError("Invalid Result from Object Compare");
252
253        PKIX_TEST_EXPECT_NO_ERROR
254                (PKIX_PL_Object_Compare(obj4, obj4, &cmpResult, plContext));
255
256        *(char *)obj2 = 0x10;
257        if (cmpResult != 0) testError("Invalid Result from Object Compare");
258
259
260cleanup:
261
262        PKIX_TEST_RETURN();
263}
264
265
266static void
267testDestroy(
268            PKIX_PL_Object *obj,
269            PKIX_PL_Object *obj2,
270            PKIX_PL_Object *obj3,
271            PKIX_PL_Object *obj4)
272{
273        PKIX_TEST_STD_VARS();
274
275        PKIX_TEST_DECREF_BC(obj);
276        PKIX_TEST_DECREF_BC(obj2);
277        PKIX_TEST_DECREF_BC(obj3);
278        PKIX_TEST_DECREF_BC(obj4);
279
280cleanup:
281
282        PKIX_TEST_RETURN();
283}
284
285int test_object(int argc, char *argv[]) {
286
287#ifdef PKIX_USER_OBJECT_TYPE
288        PKIX_PL_Object *obj, *obj2, *obj3, *obj4;
289        PKIX_UInt32 actualMinorVersion;
290        PKIX_UInt32 j = 0;
291
292        PKIX_TEST_STD_VARS();
293
294        startTests("Objects");
295
296        PKIX_TEST_EXPECT_NO_ERROR(
297            PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
298
299        subTest("PKIX_PL_Object_Create");
300        createObjects(&obj, &obj2, &obj3, &obj4);
301
302        PKIX_TEST_EQ_HASH_TOSTR_DUP(obj, obj3, obj2, NULL, Object, PKIX_FALSE);
303
304        subTest("PKIX_PL_Object_GetType");
305        testGetType(obj, obj2, obj3);
306
307        subTest("PKIX_PL_Object_Compare");
308        testCompare(obj2, obj4);
309
310        subTest("PKIX_PL_Object_Destroy");
311        testDestroy(obj, obj2, obj3, obj4);
312
313
314cleanup:
315
316        PKIX_Shutdown(plContext);
317
318        endTests("Objects");
319#endif /* PKIX_USER_OBJECT_TYPE */
320        return (0);
321
322}