Import from firefox-3.0b1 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / pkix_pl / system / test_bigint.c
blob236a1f2954cf242aac256122486f10502334a88c
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
12 * License.
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.
21 * Contributor(s):
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 ***** */
38 * test_bigint.c
40 * Tests BigInt Types
44 #include "testutil.h"
45 #include "testutil_nss.h"
47 static void *plContext = NULL;
49 static void
50 createBigInt(
51 PKIX_PL_BigInt **bigInts,
52 char *bigIntAscii,
53 PKIX_Boolean errorHandling)
55 PKIX_PL_String *bigIntString = NULL;
57 PKIX_TEST_STD_VARS();
59 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create
60 (PKIX_ESCASCII,
61 bigIntAscii,
62 PL_strlen(bigIntAscii),
63 &bigIntString,
64 plContext));
66 if (errorHandling){
67 PKIX_TEST_EXPECT_ERROR(PKIX_PL_BigInt_Create
68 (bigIntString,
69 bigInts,
70 plContext));
71 } else {
72 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_BigInt_Create
73 (bigIntString,
74 bigInts,
75 plContext));
78 cleanup:
80 PKIX_TEST_DECREF_AC(bigIntString);
82 PKIX_TEST_RETURN();
85 static void
86 testToString(
87 PKIX_PL_BigInt *bigInt,
88 char *expAscii)
90 PKIX_PL_String *bigIntString = NULL;
91 char *temp = NULL;
93 PKIX_TEST_STD_VARS();
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");
102 goto cleanup;
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));
112 cleanup:
114 PKIX_TEST_DECREF_AC(bigIntString);
116 PKIX_TEST_RETURN();
119 static void
120 testCompare(
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));
131 cleanup:
133 PKIX_TEST_RETURN();
136 static void
137 testDestroy(
138 PKIX_PL_BigInt *bigInt)
140 PKIX_TEST_STD_VARS();
142 PKIX_TEST_DECREF_BC(bigInt);
144 cleanup:
146 PKIX_TEST_RETURN();
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;
155 PKIX_UInt32 j = 0;
157 char *bigIntValue[4] =
159 "03",
160 "ff",
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
185 (testBigInt[2],
186 testBigInt[3],
187 testBigInt[1],
188 bigIntValue[2],
189 BigInt,
190 PKIX_TRUE);
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);
199 if (cmpResult <= 0){
200 testError("Invalid Result from String Compare");
203 subTest("PKIX_PL_BigInt_Compare <lt>");
204 testCompare(testBigInt[1], testBigInt[2], &cmpResult);
205 if (cmpResult >= 0){
206 testError("Invalid Result from String Compare");
209 subTest("PKIX_PL_BigInt_Compare <eq>");
210 testCompare(testBigInt[2], testBigInt[3], &cmpResult);
211 if (cmpResult != 0){
212 testError("Invalid Result from String Compare");
215 for (i = 0; i < size; i++) {
216 subTest("PKIX_PL_BigInt_Destroy");
217 testDestroy(testBigInt[i]);
220 cleanup:
222 PKIX_Shutdown(plContext);
224 endTests("BigInt");
226 return (0);