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_oid.c
6  *
7  * Test OID Types
8  *
9  */
10 
11 #include "testutil.h"
12 #include "testutil_nss.h"
13 
14 static void *plContext = NULL;
15 
16 static void
createOID(PKIX_PL_OID ** testOID,char * oidAscii,PKIX_Boolean errorHandling)17 createOID(
18     PKIX_PL_OID **testOID,
19     char *oidAscii,
20     PKIX_Boolean errorHandling)
21 {
22     PKIX_TEST_STD_VARS();
23 
24     if (errorHandling) {
25         PKIX_TEST_EXPECT_ERROR(PKIX_PL_OID_Create(oidAscii, testOID, plContext));
26     } else {
27         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create(oidAscii, testOID, plContext));
28     }
29 
30 cleanup:
31 
32     PKIX_TEST_RETURN();
33 }
34 
35 static void
testToString(PKIX_PL_OID * oid,char * expAscii)36 testToString(
37     PKIX_PL_OID *oid,
38     char *expAscii)
39 {
40     PKIX_PL_String *oidString = NULL;
41     char *temp = NULL;
42 
43     PKIX_TEST_STD_VARS();
44 
45     PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)oid,
46                                                       &oidString, plContext));
47 
48     temp = PKIX_String2ASCII(oidString, plContext);
49     if (temp == NULL) {
50         testError("PKIX_String2Ascii failed");
51         goto cleanup;
52     }
53 
54     if (PL_strcmp(temp, expAscii) != 0) {
55         (void)printf("\tOid ToString: %s %s\n", temp, expAscii);
56         testError("Output string does not match source");
57     }
58 
59     PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext));
60 
61 cleanup:
62 
63     PKIX_TEST_DECREF_AC(oidString);
64 
65     PKIX_TEST_RETURN();
66 }
67 
68 static void
testCompare(PKIX_PL_OID * oid0,PKIX_PL_OID * oid1,PKIX_PL_OID * oid2,PKIX_PL_OID * oid3)69 testCompare(
70     PKIX_PL_OID *oid0,
71     PKIX_PL_OID *oid1,
72     PKIX_PL_OID *oid2,
73     PKIX_PL_OID *oid3)
74 {
75     PKIX_Int32 cmpResult;
76     PKIX_TEST_STD_VARS();
77 
78     PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare((PKIX_PL_Object *)oid0,
79                                                      (PKIX_PL_Object *)oid1,
80                                                      &cmpResult, plContext));
81     if (cmpResult <= 0)
82         testError("Invalid Result from OID Compare");
83 
84     PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare((PKIX_PL_Object *)oid1,
85                                                      (PKIX_PL_Object *)oid0,
86                                                      &cmpResult, plContext));
87     if (cmpResult >= 0)
88         testError("Invalid Result from OID Compare");
89 
90     PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare((PKIX_PL_Object *)oid1,
91                                                      (PKIX_PL_Object *)oid2,
92                                                      &cmpResult, plContext));
93     if (cmpResult >= 0)
94         testError("Invalid Result from OID Compare");
95 
96     PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare((PKIX_PL_Object *)oid2,
97                                                      (PKIX_PL_Object *)oid1,
98                                                      &cmpResult, plContext));
99     if (cmpResult <= 0)
100         testError("Invalid Result from OID Compare");
101 
102     PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare((PKIX_PL_Object *)oid1,
103                                                      (PKIX_PL_Object *)oid3,
104                                                      &cmpResult, plContext));
105     if (cmpResult != 0)
106         testError("Invalid Result from OID Compare");
107 
108 cleanup:
109 
110     PKIX_TEST_RETURN();
111 }
112 
113 static void
testDestroy(PKIX_PL_OID * oid)114 testDestroy(
115     PKIX_PL_OID *oid)
116 {
117     PKIX_TEST_STD_VARS();
118 
119     PKIX_TEST_DECREF_BC(oid);
120 
121 cleanup:
122 
123     PKIX_TEST_RETURN();
124 }
125 
126 int
test_oid(int argc,char * argv[])127 test_oid(int argc, char *argv[])
128 {
129 
130     PKIX_PL_OID *testOID[6] = { NULL };
131     PKIX_PL_OID *badTestOID = NULL;
132     PKIX_UInt32 i, size = 6;
133     PKIX_UInt32 actualMinorVersion;
134     PKIX_UInt32 j = 0;
135 
136     char *validOID[6] = {
137         "2.11.22222.33333",
138         "1.2.3.004.5.6.7",
139         "2.11.22222.33333",
140         "1.2.3.4.5.6.7",
141         "1.2.3",
142         "2.39.3"
143     };
144 
145     char *expected[6] = {
146         "2.11.22222.33333",
147         "1.2.3.4.5.6.7",
148         "2.11.22222.33333",
149         "1.2.3.4.5.6.7",
150         "1.2.3",
151         "2.39.3"
152     };
153 
154     char *badOID[11] = {
155         "1.2.4294967299",
156         "this. is. a. bad. oid",
157         "00a1000.002b",
158         "100.-5.10",
159         "1.2..3",
160         ".1.2.3",
161         "1.2.3.",
162         "00010.1.2.3",
163         "1.000041.2.3",
164         "000000000000000000000000000000000000000010.3.2",
165         "1"
166     };
167 
168     PKIX_TEST_STD_VARS();
169 
170     startTests("OIDs");
171 
172     PKIX_TEST_EXPECT_NO_ERROR(
173         PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
174 
175     for (i = 0; i < size; i++) {
176         subTest("PKIX_PL_OID_Create");
177         createOID(&testOID[i], validOID[i], PKIX_FALSE);
178     }
179 
180     PKIX_TEST_EQ_HASH_TOSTR_DUP(testOID[0],
181                                 testOID[2],
182                                 testOID[1],
183                                 NULL,
184                                 OID,
185                                 PKIX_FALSE);
186 
187     for (i = 0; i < size; i++) {
188         subTest("PKIX_PL_OID_ToString");
189         testToString(testOID[i], expected[i]);
190     }
191 
192     subTest("PKIX_PL_OID_Compare");
193     testCompare(testOID[0], testOID[1], testOID[2], testOID[3]);
194 
195     for (i = 0; i < size; i++) {
196         subTest("PKIX_PL_OID_Destroy");
197         testDestroy(testOID[i]);
198     }
199 
200     for (i = 0; i < 11; i++) {
201         subTest("PKIX_PL_OID Error Handling");
202         createOID(&badTestOID, badOID[i], PKIX_TRUE);
203     }
204 
205 cleanup:
206 
207     PKIX_Shutdown(plContext);
208 
209     endTests("OIDs");
210 
211     return (0);
212 }
213