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
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 /*
5 * test_bigint.c
6 *
7 * Tests BigInt Types
8 *
9 */
10
11 #include "testutil.h"
12 #include "testutil_nss.h"
13
14 static void *plContext = NULL;
15
16 static void
createBigInt(PKIX_PL_BigInt ** bigInts,char * bigIntAscii,PKIX_Boolean errorHandling)17 createBigInt(
18 PKIX_PL_BigInt **bigInts,
19 char *bigIntAscii,
20 PKIX_Boolean errorHandling)
21 {
22 PKIX_PL_String *bigIntString = NULL;
23
24 PKIX_TEST_STD_VARS();
25
26 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(PKIX_ESCASCII,
27 bigIntAscii,
28 PL_strlen(bigIntAscii),
29 &bigIntString,
30 plContext));
31
32 if (errorHandling) {
33 PKIX_TEST_EXPECT_ERROR(PKIX_PL_BigInt_Create(bigIntString,
34 bigInts,
35 plContext));
36 } else {
37 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_BigInt_Create(bigIntString,
38 bigInts,
39 plContext));
40 }
41
42 cleanup:
43
44 PKIX_TEST_DECREF_AC(bigIntString);
45
46 PKIX_TEST_RETURN();
47 }
48
49 static void
testToString(PKIX_PL_BigInt * bigInt,char * expAscii)50 testToString(
51 PKIX_PL_BigInt *bigInt,
52 char *expAscii)
53 {
54 PKIX_PL_String *bigIntString = NULL;
55 char *temp = NULL;
56
57 PKIX_TEST_STD_VARS();
58
59 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)bigInt,
60 &bigIntString, plContext));
61
62 temp = PKIX_String2ASCII(bigIntString, plContext);
63 if (temp == plContext) {
64 testError("PKIX_String2Ascii failed");
65 goto cleanup;
66 }
67
68 if (PL_strcmp(temp, expAscii) != 0) {
69 (void)printf("\tBigInt ToString: %s %s\n", temp, expAscii);
70 testError("Output string does not match source");
71 }
72
73 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext));
74
75 cleanup:
76
77 PKIX_TEST_DECREF_AC(bigIntString);
78
79 PKIX_TEST_RETURN();
80 }
81
82 static void
testCompare(PKIX_PL_BigInt * firstBigInt,PKIX_PL_BigInt * secondBigInt,PKIX_Int32 * cmpResult)83 testCompare(
84 PKIX_PL_BigInt *firstBigInt,
85 PKIX_PL_BigInt *secondBigInt,
86 PKIX_Int32 *cmpResult)
87 {
88 PKIX_TEST_STD_VARS();
89
90 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare((PKIX_PL_Object *)firstBigInt,
91 (PKIX_PL_Object *)secondBigInt,
92 cmpResult, plContext));
93 cleanup:
94
95 PKIX_TEST_RETURN();
96 }
97
98 static void
testDestroy(PKIX_PL_BigInt * bigInt)99 testDestroy(
100 PKIX_PL_BigInt *bigInt)
101 {
102 PKIX_TEST_STD_VARS();
103
104 PKIX_TEST_DECREF_BC(bigInt);
105
106 cleanup:
107
108 PKIX_TEST_RETURN();
109 }
110
111 int
test_bigint(int argc,char * argv[])112 test_bigint(int argc, char *argv[])
113 {
114
115 PKIX_UInt32 size = 4, badSize = 3, i = 0;
116 PKIX_PL_BigInt *testBigInt[4] = { NULL };
117 PKIX_Int32 cmpResult;
118 PKIX_UInt32 actualMinorVersion;
119 PKIX_UInt32 j = 0;
120
121 char *bigIntValue[4] =
122 {
123 "03",
124 "ff",
125 "1010101010101010101010101010101010101010",
126 "1010101010101010101010101010101010101010",
127 };
128
129 char *badValue[3] = { "00ff", "fff", "-ff" };
130
131 PKIX_TEST_STD_VARS();
132
133 startTests("BigInts");
134
135 PKIX_TEST_EXPECT_NO_ERROR(
136 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
137
138 for (i = 0; i < badSize; i++) {
139 subTest("PKIX_PL_BigInt_Create <error_handling>");
140 createBigInt(&testBigInt[i], badValue[i], PKIX_TRUE);
141 }
142
143 for (i = 0; i < size; i++) {
144 subTest("PKIX_PL_BigInt_Create");
145 createBigInt(&testBigInt[i], bigIntValue[i], PKIX_FALSE);
146 }
147
148 PKIX_TEST_EQ_HASH_TOSTR_DUP(testBigInt[2],
149 testBigInt[3],
150 testBigInt[1],
151 bigIntValue[2],
152 BigInt,
153 PKIX_TRUE);
154
155 for (i = 0; i < size; i++) {
156 subTest("PKIX_PL_BigInt_ToString");
157 testToString(testBigInt[i], bigIntValue[i]);
158 }
159
160 subTest("PKIX_PL_BigInt_Compare <gt>");
161 testCompare(testBigInt[2], testBigInt[1], &cmpResult);
162 if (cmpResult <= 0) {
163 testError("Invalid Result from String Compare");
164 }
165
166 subTest("PKIX_PL_BigInt_Compare <lt>");
167 testCompare(testBigInt[1], testBigInt[2], &cmpResult);
168 if (cmpResult >= 0) {
169 testError("Invalid Result from String Compare");
170 }
171
172 subTest("PKIX_PL_BigInt_Compare <eq>");
173 testCompare(testBigInt[2], testBigInt[3], &cmpResult);
174 if (cmpResult != 0) {
175 testError("Invalid Result from String Compare");
176 }
177
178 for (i = 0; i < size; i++) {
179 subTest("PKIX_PL_BigInt_Destroy");
180 testDestroy(testBigInt[i]);
181 }
182
183 cleanup:
184
185 PKIX_Shutdown(plContext);
186
187 endTests("BigInt");
188
189 return (0);
190 }
191