test_bigint.c (4875B)
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 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 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 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 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 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 "03", 123 "ff", 124 "1010101010101010101010101010101010101010", 125 "1010101010101010101010101010101010101010", 126 }; 127 128 char *badValue[3] = { "00ff", "fff", "-ff" }; 129 130 PKIX_TEST_STD_VARS(); 131 132 startTests("BigInts"); 133 134 PKIX_TEST_EXPECT_NO_ERROR( 135 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); 136 137 for (i = 0; i < badSize; i++) { 138 subTest("PKIX_PL_BigInt_Create <error_handling>"); 139 createBigInt(&testBigInt[i], badValue[i], PKIX_TRUE); 140 } 141 142 for (i = 0; i < size; i++) { 143 subTest("PKIX_PL_BigInt_Create"); 144 createBigInt(&testBigInt[i], bigIntValue[i], PKIX_FALSE); 145 } 146 147 PKIX_TEST_EQ_HASH_TOSTR_DUP(testBigInt[2], 148 testBigInt[3], 149 testBigInt[1], 150 bigIntValue[2], 151 BigInt, 152 PKIX_TRUE); 153 154 for (i = 0; i < size; i++) { 155 subTest("PKIX_PL_BigInt_ToString"); 156 testToString(testBigInt[i], bigIntValue[i]); 157 } 158 159 subTest("PKIX_PL_BigInt_Compare <gt>"); 160 testCompare(testBigInt[2], testBigInt[1], &cmpResult); 161 if (cmpResult <= 0) { 162 testError("Invalid Result from String Compare"); 163 } 164 165 subTest("PKIX_PL_BigInt_Compare <lt>"); 166 testCompare(testBigInt[1], testBigInt[2], &cmpResult); 167 if (cmpResult >= 0) { 168 testError("Invalid Result from String Compare"); 169 } 170 171 subTest("PKIX_PL_BigInt_Compare <eq>"); 172 testCompare(testBigInt[2], testBigInt[3], &cmpResult); 173 if (cmpResult != 0) { 174 testError("Invalid Result from String Compare"); 175 } 176 177 for (i = 0; i < size; i++) { 178 subTest("PKIX_PL_BigInt_Destroy"); 179 testDestroy(testBigInt[i]); 180 } 181 182 cleanup: 183 184 PKIX_Shutdown(plContext); 185 186 endTests("BigInt"); 187 188 return (0); 189 }