test_bytearray.c (5462B)
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_bytearray.c 6 * 7 * Tests ByteArray types. 8 * 9 */ 10 11 #include "testutil.h" 12 #include "testutil_nss.h" 13 14 static void *plContext = NULL; 15 16 static void 17 createByteArray( 18 PKIX_PL_ByteArray **byteArray, 19 char *bytes, 20 PKIX_UInt32 length) 21 { 22 PKIX_TEST_STD_VARS(); 23 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_ByteArray_Create((void *)bytes, 24 length, 25 byteArray, 26 plContext)); 27 cleanup: 28 PKIX_TEST_RETURN(); 29 } 30 31 static void 32 testZeroLength(void) 33 { 34 PKIX_PL_ByteArray *byteArray = NULL; 35 void *array = NULL; 36 PKIX_UInt32 length = 2; 37 38 PKIX_TEST_STD_VARS(); 39 40 createByteArray(&byteArray, NULL, 0); 41 42 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_ByteArray_GetLength(byteArray, &length, plContext)); 43 if (length != 0) { 44 testError("Length should be zero"); 45 } 46 47 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_ByteArray_GetPointer(byteArray, &array, plContext)); 48 if (array) { 49 testError("Array should be NULL"); 50 } 51 52 testToStringHelper((PKIX_PL_Object *)byteArray, "[]", plContext); 53 54 cleanup: 55 56 PKIX_TEST_DECREF_AC(byteArray); 57 58 PKIX_TEST_RETURN(); 59 } 60 61 static void 62 testToString( 63 PKIX_PL_ByteArray *byteArray, 64 char *expAscii) 65 { 66 PKIX_PL_String *string = NULL; 67 char *temp = NULL; 68 69 PKIX_TEST_STD_VARS(); 70 71 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)byteArray, 72 &string, plContext)); 73 74 temp = PKIX_String2ASCII(string, plContext); 75 if (temp == NULL) { 76 testError("PKIX_String2Ascii failed"); 77 goto cleanup; 78 } 79 80 if (PL_strcmp(temp, expAscii) != 0) { 81 (void)printf("\tByteArray ToString: %s %s\n", temp, expAscii); 82 testError("Output string does not match source"); 83 } 84 85 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext)); 86 87 cleanup: 88 89 PKIX_TEST_DECREF_AC(string); 90 91 PKIX_TEST_RETURN(); 92 } 93 94 static void 95 testGetLength( 96 PKIX_PL_ByteArray *byteArray, 97 PKIX_UInt32 expLength) 98 { 99 PKIX_UInt32 arrayLength; 100 101 PKIX_TEST_STD_VARS(); 102 103 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_ByteArray_GetLength(byteArray, &arrayLength, plContext)); 104 105 if (arrayLength != expLength) { 106 (void)printf("\tByteArray GetLength: %d %d\n", 107 arrayLength, expLength); 108 testError("Incorrect Array Length returned"); 109 } 110 111 cleanup: 112 PKIX_TEST_RETURN(); 113 } 114 115 static void 116 testGetPointer( 117 PKIX_PL_ByteArray *byteArray, 118 char *expBytes, 119 PKIX_UInt32 arrayLength) 120 { 121 char *temp = NULL; 122 PKIX_UInt32 j; 123 124 PKIX_TEST_STD_VARS(); 125 126 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_ByteArray_GetPointer(byteArray, (void **)&temp, plContext)); 127 128 for (j = 0; j < arrayLength; j++) { 129 if (temp[j] != expBytes[j]) { 130 testError("Incorrect Byte Array Contents"); 131 } 132 } 133 134 cleanup: 135 136 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext)); 137 PKIX_TEST_RETURN(); 138 } 139 140 void 141 testDestroy( 142 PKIX_PL_ByteArray *byteArray) 143 { 144 PKIX_TEST_STD_VARS(); 145 146 PKIX_TEST_DECREF_BC(byteArray); 147 148 cleanup: 149 150 PKIX_TEST_RETURN(); 151 } 152 153 int 154 test_bytearray(int argc, char *argv[]) 155 { 156 157 PKIX_PL_ByteArray *testByteArray[4]; 158 159 PKIX_UInt32 i, size = 4; 160 PKIX_UInt32 lengths[4] = { 5, 6, 1, 5 }; 161 char dArray0[5] = { 1, 2, 3, 4, 5 }; 162 unsigned char dArray1[6] = { 127, 128, 129, 254, 255, 0 }; 163 char dArray2[1] = { 100 }; 164 char dArray3[5] = { 1, 2, 3, 4, 5 }; 165 166 char *expected[4] = { 167 "[001, 002, 003, 004, 005]", 168 "[127, 128, 129, 254, 255, 000]", 169 "[100]", 170 "[001, 002, 003, 004, 005]" 171 }; 172 173 char *dummyArray[4]; 174 PKIX_UInt32 actualMinorVersion; 175 PKIX_UInt32 j = 0; 176 177 PKIX_TEST_STD_VARS(); 178 179 dummyArray[0] = dArray0; 180 dummyArray[1] = (char *)dArray1; 181 dummyArray[2] = dArray2; 182 dummyArray[3] = dArray3; 183 184 startTests("ByteArrays"); 185 186 PKIX_TEST_EXPECT_NO_ERROR( 187 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); 188 189 subTest("PKIX_PL_ByteArray_Create <zero length>"); 190 testZeroLength(); 191 192 for (i = 0; i < size; i++) { 193 subTest("PKIX_PL_ByteArray_Create"); 194 createByteArray(&testByteArray[i], dummyArray[i], lengths[i]); 195 } 196 197 PKIX_TEST_EQ_HASH_TOSTR_DUP(testByteArray[0], 198 testByteArray[3], 199 testByteArray[1], 200 "[001, 002, 003, 004, 005]", 201 ByteArray, 202 PKIX_TRUE); 203 204 for (i = 0; i < size; i++) { 205 subTest("PKIX_PL_ByteArray_ToString"); 206 testToString(testByteArray[i], expected[i]); 207 } 208 209 for (i = 0; i < size; i++) { 210 subTest("PKIX_PL_ByteArray_GetLength"); 211 testGetLength(testByteArray[i], lengths[i]); 212 } 213 214 for (i = 0; i < size; i++) { 215 subTest("PKIX_PL_ByteArray_GetPointer"); 216 testGetPointer(testByteArray[i], dummyArray[i], lengths[i]); 217 } 218 219 for (i = 0; i < size; i++) { 220 subTest("PKIX_PL_ByteArray_Destroy"); 221 testDestroy(testByteArray[i]); 222 } 223 224 cleanup: 225 226 PKIX_Shutdown(plContext); 227 228 endTests("ByteArray"); 229 230 return (0); 231 }