1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is the PKIX-C library.
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.
22 * Sun Microsystems, Inc.
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.
36 * ***** END LICENSE BLOCK ***** */
45 #include "testutil_nss.h"
47 static void *plContext
= NULL
;
51 PKIX_PL_BigInt
**bigInts
,
53 PKIX_Boolean errorHandling
)
55 PKIX_PL_String
*bigIntString
= NULL
;
59 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
62 PL_strlen(bigIntAscii
),
67 PKIX_TEST_EXPECT_ERROR(PKIX_PL_BigInt_Create
72 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_BigInt_Create
80 PKIX_TEST_DECREF_AC(bigIntString
);
87 PKIX_PL_BigInt
*bigInt
,
90 PKIX_PL_String
*bigIntString
= NULL
;
95 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString
96 ((PKIX_PL_Object
*)bigInt
,
97 &bigIntString
, plContext
));
99 temp
= PKIX_String2ASCII(bigIntString
, plContext
);
100 if (temp
== plContext
){
101 testError("PKIX_String2Ascii failed");
105 if (PL_strcmp(temp
, expAscii
) != 0) {
106 (void) printf("\tBigInt ToString: %s %s\n", temp
, expAscii
);
107 testError("Output string does not match source");
110 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp
, plContext
));
114 PKIX_TEST_DECREF_AC(bigIntString
);
121 PKIX_PL_BigInt
*firstBigInt
,
122 PKIX_PL_BigInt
*secondBigInt
,
123 PKIX_Int32
*cmpResult
)
125 PKIX_TEST_STD_VARS();
127 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare
128 ((PKIX_PL_Object
*)firstBigInt
,
129 (PKIX_PL_Object
*)secondBigInt
,
130 cmpResult
, plContext
));
138 PKIX_PL_BigInt
*bigInt
)
140 PKIX_TEST_STD_VARS();
142 PKIX_TEST_DECREF_BC(bigInt
);
149 int test_bigint(int argc
, char *argv
[]) {
151 PKIX_UInt32 size
= 4, badSize
= 3, i
= 0;
152 PKIX_PL_BigInt
*testBigInt
[4] = {NULL
};
153 PKIX_Int32 cmpResult
;
154 PKIX_UInt32 actualMinorVersion
;
157 char *bigIntValue
[4] =
161 "1010101010101010101010101010101010101010",
162 "1010101010101010101010101010101010101010",
165 char *badValue
[3] = {"00ff", "fff", "-ff"};
167 PKIX_TEST_STD_VARS();
169 startTests("BigInts");
171 PKIX_TEST_EXPECT_NO_ERROR(
172 PKIX_PL_NssContext_Create(0, PKIX_FALSE
, NULL
, &plContext
));
174 for (i
= 0; i
< badSize
; i
++) {
175 subTest("PKIX_PL_BigInt_Create <error_handling>");
176 createBigInt(&testBigInt
[i
], badValue
[i
], PKIX_TRUE
);
179 for (i
= 0; i
< size
; i
++) {
180 subTest("PKIX_PL_BigInt_Create");
181 createBigInt(&testBigInt
[i
], bigIntValue
[i
], PKIX_FALSE
);
184 PKIX_TEST_EQ_HASH_TOSTR_DUP
192 for (i
= 0; i
< size
; i
++) {
193 subTest("PKIX_PL_BigInt_ToString");
194 testToString(testBigInt
[i
], bigIntValue
[i
]);
197 subTest("PKIX_PL_BigInt_Compare <gt>");
198 testCompare(testBigInt
[2], testBigInt
[1], &cmpResult
);
200 testError("Invalid Result from String Compare");
203 subTest("PKIX_PL_BigInt_Compare <lt>");
204 testCompare(testBigInt
[1], testBigInt
[2], &cmpResult
);
206 testError("Invalid Result from String Compare");
209 subTest("PKIX_PL_BigInt_Compare <eq>");
210 testCompare(testBigInt
[2], testBigInt
[3], &cmpResult
);
212 testError("Invalid Result from String Compare");
215 for (i
= 0; i
< size
; i
++) {
216 subTest("PKIX_PL_BigInt_Destroy");
217 testDestroy(testBigInt
[i
]);
222 PKIX_Shutdown(plContext
);