test_store.c (5153B)
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_certstore.c 6 * 7 * Test CertStore Type 8 * 9 */ 10 11 #include "testutil.h" 12 #include "testutil_nss.h" 13 14 static void *plContext = NULL; 15 16 static PKIX_Error * 17 testCRLCallback( 18 PKIX_CertStore *store, 19 PKIX_CRLSelector *selector, 20 void **pNBIOContext, 21 PKIX_List **pCrls, /* list of PKIX_PL_Crl */ 22 void *plContext) 23 { 24 return (0); 25 } 26 27 static PKIX_Error * 28 testCRLContinue( 29 PKIX_CertStore *store, 30 PKIX_CRLSelector *selector, 31 void **pNBIOContext, 32 PKIX_List **pCrls, /* list of PKIX_PL_Crl */ 33 void *plContext) 34 { 35 return (0); 36 } 37 38 static PKIX_Error * 39 testCertCallback( 40 PKIX_CertStore *store, 41 PKIX_CertSelector *selector, 42 void **pNBIOContext, 43 PKIX_List **pCerts, /* list of PKIX_PL_Cert */ 44 void *plContext) 45 { 46 return (0); 47 } 48 49 static PKIX_Error * 50 testCertContinue( 51 PKIX_CertStore *store, 52 PKIX_CertSelector *selector, 53 void **pNBIOContext, 54 PKIX_List **pCerts, /* list of PKIX_PL_Cert */ 55 void *plContext) 56 { 57 return (0); 58 } 59 60 static char * 61 catDirName(char *platform, char *dir, void *plContext) 62 { 63 char *pathName = NULL; 64 PKIX_UInt32 dirLen; 65 PKIX_UInt32 platformLen; 66 67 PKIX_TEST_STD_VARS(); 68 69 dirLen = PL_strlen(dir); 70 platformLen = PL_strlen(platform); 71 72 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc(platformLen + 73 dirLen + 74 2, 75 (void **)&pathName, plContext)); 76 77 PL_strcpy(pathName, platform); 78 PL_strcat(pathName, "/"); 79 PL_strcat(pathName, dir); 80 81 cleanup: 82 83 PKIX_TEST_RETURN(); 84 85 return (pathName); 86 } 87 88 static void 89 testCertStore(char *crlDir) 90 { 91 PKIX_PL_String *dirString = NULL; 92 PKIX_CertStore *certStore = NULL; 93 PKIX_PL_Object *getCertStoreContext = NULL; 94 PKIX_CertStore_CertCallback certCallback = NULL; 95 PKIX_CertStore_CRLCallback crlCallback = NULL; 96 97 PKIX_TEST_STD_VARS(); 98 99 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(PKIX_ESCASCII, 100 crlDir, 101 0, 102 &dirString, 103 plContext)); 104 105 subTest("PKIX_CertStore_Create"); 106 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_Create(testCertCallback, 107 testCRLCallback, 108 testCertContinue, 109 testCRLContinue, 110 NULL, /* trustCallback */ 111 (PKIX_PL_Object *)dirString, 112 PKIX_TRUE, /* cacheFlag */ 113 PKIX_TRUE, /* local */ 114 &certStore, 115 plContext)); 116 117 subTest("PKIX_CertStore_GetCertCallback"); 118 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCertCallback(certStore, &certCallback, plContext)); 119 120 if (certCallback != testCertCallback) { 121 testError("PKIX_CertStore_GetCertCallback unexpected mismatch"); 122 } 123 124 subTest("PKIX_CertStore_GetCRLCallback"); 125 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCRLCallback(certStore, &crlCallback, plContext)); 126 127 if (crlCallback != testCRLCallback) { 128 testError("PKIX_CertStore_GetCRLCallback unexpected mismatch"); 129 } 130 131 subTest("PKIX_CertStore_GetCertStoreContext"); 132 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCertStoreContext(certStore, &getCertStoreContext, plContext)); 133 134 if ((PKIX_PL_Object *)dirString != getCertStoreContext) { 135 testError("PKIX_CertStore_GetCertStoreContext unexpected mismatch"); 136 } 137 138 cleanup: 139 140 PKIX_TEST_DECREF_AC(dirString); 141 PKIX_TEST_DECREF_AC(certStore); 142 PKIX_TEST_DECREF_AC(getCertStoreContext); 143 144 PKIX_TEST_RETURN(); 145 } 146 147 static void 148 printUsage(char *pName) 149 { 150 printf("\nUSAGE: %s testName <data-dir> <platform-dir>\n\n", pName); 151 } 152 153 /* Functional tests for CertStore public functions */ 154 155 int 156 test_store(int argc, char *argv[]) 157 { 158 159 char *platformDir = NULL; 160 char *dataDir = NULL; 161 char *combinedDir = NULL; 162 PKIX_UInt32 actualMinorVersion; 163 PKIX_UInt32 j = 0; 164 165 PKIX_TEST_STD_VARS(); 166 167 PKIX_TEST_EXPECT_NO_ERROR( 168 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); 169 170 if (argc < (3 + j)) { 171 printUsage(argv[0]); 172 return (0); 173 } 174 175 startTests(argv[1 + j]); 176 177 dataDir = argv[2 + j]; 178 platformDir = argv[3 + j]; 179 combinedDir = catDirName(platformDir, dataDir, plContext); 180 181 testCertStore(combinedDir); 182 183 cleanup: 184 185 pkixTestErrorResult = PKIX_PL_Free(combinedDir, plContext); 186 187 PKIX_Shutdown(plContext); 188 189 PKIX_TEST_RETURN(); 190 191 endTests("CertStore"); 192 193 return (0); 194 }