@@ -17,11 +17,11 @@ import (
17
17
func TestHandler_AddDaIntermediateCert (t * testing.T ) {
18
18
setup := utils .Setup (t )
19
19
20
- // add DA root certificate
20
+ // Add DA root certificate
21
21
rootCertificate := utils .RootDaCertificate (setup .Trustee1 )
22
22
utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , rootCertificate )
23
23
24
- // add DA PAI certificate
24
+ // Add DA PAI certificate
25
25
testIntermediateCertificate := utils .IntermediateDaCertificate (setup .Vendor1 )
26
26
utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate )
27
27
@@ -41,6 +41,10 @@ func TestHandler_AddDaIntermediateCert(t *testing.T) {
41
41
{Key : types .ApprovedRootCertificatesKeyPrefix },
42
42
{Key : types .ProposedCertificateKeyPrefix },
43
43
{Key : types .RejectedCertificateKeyPrefix },
44
+ {Key : types .NocCertificatesKeyPrefix },
45
+ {Key : types .NocCertificatesBySubjectKeyPrefix },
46
+ {Key : types .NocCertificatesBySubjectKeyIDKeyPrefix },
47
+ {Key : types .NocCertificatesByVidAndSkidKeyPrefix },
44
48
},
45
49
}
46
50
utils .CheckCertificateStateIndexes (t , setup , testIntermediateCertificate , indexes )
@@ -49,13 +53,14 @@ func TestHandler_AddDaIntermediateCert(t *testing.T) {
49
53
func TestHandler_AddDaIntermediateCert_VidScoped (t * testing.T ) {
50
54
setup := utils .Setup (t )
51
55
56
+ // Add vendor
52
57
accAddress := setup .CreateVendorAccount (testconstants .PAACertWithNumericVidVid )
53
58
54
- // store root certificate
59
+ // Store root certificate
55
60
testRootCertificate := utils .RootDaCertificateWithNumericVid (setup .Trustee1 )
56
61
utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , testRootCertificate )
57
62
58
- // add intermediate certificate
63
+ // Add intermediate certificate
59
64
testIntermediateCertificate := utils .IntermediateDaCertificateWithNumericPidVid (accAddress )
60
65
utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate )
61
66
@@ -83,28 +88,28 @@ func TestHandler_AddDaIntermediateCert_VidScoped(t *testing.T) {
83
88
func TestHandler_AddDaIntermediateCert_SameSubjectAndSkid_DifferentSerialNumber (t * testing.T ) {
84
89
setup := utils .Setup (t )
85
90
86
- // store root certificate
87
- rootCertificate := utils .RootDaCertificate (setup .Trustee1 )
88
- utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , rootCertificate )
91
+ // Add vendor account
92
+ vendorAccAddress := setup .CreateVendorAccount (testconstants .RootCertWithVidVid )
89
93
90
- // store intermediate certificate with different serial number
91
- intermediateCertificate := utils .IntermediateDaCertificate (setup .Vendor1 )
92
- intermediateCertificate .SerialNumber = utils .SerialNumber
93
- utils .AddMokedDaCertificate (setup , intermediateCertificate )
94
+ // add root certificate
95
+ rootCert := utils .RootDaCertificateWithSameSubjectAndSKID1 (setup .Trustee1 )
96
+ utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , rootCert )
94
97
95
- // store intermediate certificate second time
96
- testIntermediateCertificate1 := utils .IntermediateDaCertificate ( setup . Vendor1 )
98
+ // Add intermediate certificates
99
+ testIntermediateCertificate1 := utils .IntermediateDaCertificateWithSameSubjectAndSKID1 ( vendorAccAddress )
97
100
utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate1 )
98
101
99
- // query All approved certificate
102
+ testIntermediateCertificate2 := utils .IntermediateDaCertificateWithSameSubjectAndSKID2 (vendorAccAddress )
103
+ utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate2 )
104
+
105
+ // check count of certificates
100
106
allApprovedCertificates , _ := utils .QueryAllApprovedCertificates (setup )
101
107
require .Equal (t , 2 , len (allApprovedCertificates )) // root + intermediate
102
108
103
- // query All certificate
104
109
allCertificates , _ := utils .QueryAllCertificatesAll (setup )
105
110
require .Equal (t , 2 , len (allCertificates )) // root + intermediate
106
111
107
- // Check indexes for certificate1
112
+ // Check state indexes for intermediate certificates
108
113
indexes := utils.TestIndexes {
109
114
Present : []utils.TestIndex {
110
115
{Key : types .UniqueCertificateKeyPrefix },
@@ -123,36 +128,34 @@ func TestHandler_AddDaIntermediateCert_SameSubjectAndSkid_DifferentSerialNumber(
123
128
},
124
129
}
125
130
resolvedCertificates := utils .CheckCertificateStateIndexes (t , setup , testIntermediateCertificate1 , indexes )
131
+ utils .CheckCertificateStateIndexes (t , setup , testIntermediateCertificate2 , indexes )
126
132
127
- // additional checks
128
- require .Equal (t , resolvedCertificates .ApprovedCertificates .Certs [0 ].SerialNumber , intermediateCertificate .SerialNumber )
129
- require .Equal (t , resolvedCertificates .ApprovedCertificates .Certs [1 ].SerialNumber , testIntermediateCertificate1 .SerialNumber )
133
+ // additional checks - serial numbers
134
+ require .Equal (t , resolvedCertificates .ApprovedCertificates .Certs [0 ].SerialNumber , testIntermediateCertificate1 .SerialNumber )
135
+ require .Equal (t , resolvedCertificates .ApprovedCertificates .Certs [1 ].SerialNumber , testIntermediateCertificate2 .SerialNumber )
130
136
require .NotEqual (
131
137
t ,
132
138
resolvedCertificates .ApprovedCertificates .Certs [0 ].SerialNumber ,
133
139
resolvedCertificates .ApprovedCertificates .Certs [1 ].SerialNumber ,
134
140
)
135
-
136
- // Check indexes for certificate2
137
- utils .CheckCertificateStateIndexes (t , setup , intermediateCertificate , indexes )
138
141
}
139
142
140
143
func TestHandler_AddDaIntermediateCert_ForTree (t * testing.T ) {
141
144
setup := utils .Setup (t )
142
145
143
- // add root x509 certificate
146
+ // add root certificate
144
147
testRootCertificate := utils .RootDaCertificate (setup .Trustee1 )
145
148
utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , testRootCertificate )
146
149
147
- // add intermediate x509 certificate
150
+ // add intermediate certificate
148
151
testIntermediateCertificate := utils .IntermediateDaCertificate (setup .Vendor1 )
149
152
utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate )
150
153
151
- // add leaf x509 certificate
154
+ // add leaf certificate
152
155
testLeafCertificate := utils .LeafCertificate (setup .Vendor1 )
153
156
utils .AddDaIntermediateCertificate (setup , testLeafCertificate )
154
157
155
- // Check indexes for root
158
+ // Check state indexes - root
156
159
indexes := utils.TestIndexes {
157
160
Present : []utils.TestIndex {
158
161
{Key : types .UniqueCertificateKeyPrefix },
@@ -168,7 +171,7 @@ func TestHandler_AddDaIntermediateCert_ForTree(t *testing.T) {
168
171
}
169
172
utils .CheckCertificateStateIndexes (t , setup , testRootCertificate , indexes )
170
173
171
- // Check indexes for intermediate
174
+ // Check state indexes - intermediate
172
175
indexes = utils.TestIndexes {
173
176
Present : []utils.TestIndex {
174
177
{Key : types .UniqueCertificateKeyPrefix },
@@ -186,49 +189,49 @@ func TestHandler_AddDaIntermediateCert_ForTree(t *testing.T) {
186
189
}
187
190
utils .CheckCertificateStateIndexes (t , setup , testIntermediateCertificate , indexes )
188
191
189
- // Check indexes for leaf
192
+ // Check state indexes - leaf (all the same as for intermediate)
190
193
utils .CheckCertificateStateIndexes (t , setup , testLeafCertificate , indexes )
191
194
}
192
195
193
196
func TestHandler_AddDaIntermediateCert_ByNotOwnerButSameVendor (t * testing.T ) {
194
197
setup := utils .Setup (t )
195
198
196
199
// add two vendors with the same VID
197
- vendorAccAddress1 := setup .CreateVendorAccount (testconstants .Vid )
198
- vendorAccAddress2 := setup .CreateVendorAccount (testconstants .Vid )
200
+ vendorAccAddress1 := setup .CreateVendorAccount (testconstants .RootCertWithVidVid )
201
+ vendorAccAddress2 := setup .CreateVendorAccount (testconstants .RootCertWithVidVid )
199
202
200
- // store root certificate
201
- testRootCertificate := utils .RootDaCertificate (setup .Trustee1 )
202
- utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , testRootCertificate )
203
+ // add root certificate
204
+ rootCert := utils .RootDaCertificateWithSameSubjectAndSKID1 (setup .Trustee1 )
205
+ utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , rootCert )
203
206
204
- // Store an intermediate certificate with the first vendor account as the owner
205
- intermediateCertificate := utils .IntermediateDaCertificate (vendorAccAddress1 )
206
- intermediateCertificate .SerialNumber = utils .SerialNumber
207
- utils .AddMokedDaCertificate (setup , intermediateCertificate )
207
+ // Add first intermediate certificates by vendor1
208
+ testIntermediateCertificate1 := utils .IntermediateDaCertificateWithSameSubjectAndSKID1 (vendorAccAddress1 )
209
+ utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate1 )
208
210
209
- // add an intermediate certificate with the same subject and SKID by second vendor account
210
- testIntermediateCertificate := utils .IntermediateDaCertificate (vendorAccAddress2 )
211
- utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate )
211
+ // Add second intermediate certificates by vendor2
212
+ testIntermediateCertificate2 := utils .IntermediateDaCertificateWithSameSubjectAndSKID2 (vendorAccAddress2 )
213
+ utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate2 )
212
214
213
215
// Check state indexes
214
216
indexes := utils.TestIndexes {
215
217
Present : []utils.TestIndex {
216
218
{Key : types .UniqueCertificateKeyPrefix },
217
219
{Key : types .AllCertificatesKeyPrefix , Count : 2 },
218
- {Key : types .AllCertificatesBySubjectKeyPrefix },
220
+ {Key : types .AllCertificatesBySubjectKeyPrefix , Count : 1 },
219
221
{Key : types .AllCertificatesBySubjectKeyIDKeyPrefix , Count : 2 },
220
222
{Key : types .ApprovedCertificatesKeyPrefix , Count : 2 },
221
- {Key : types .ApprovedCertificatesBySubjectKeyPrefix },
223
+ {Key : types .ApprovedCertificatesBySubjectKeyPrefix , Count : 1 },
222
224
{Key : types .ApprovedCertificatesBySubjectKeyIDKeyPrefix , Count : 2 },
223
- {Key : types .ChildCertificatesKeyPrefix , Count : 1 },
225
+ {Key : types .ChildCertificatesKeyPrefix , Count : 1 }, // by serial number
224
226
},
225
227
Missing : []utils.TestIndex {
226
228
{Key : types .ApprovedRootCertificatesKeyPrefix },
227
229
{Key : types .ProposedCertificateKeyPrefix },
228
230
{Key : types .RejectedCertificateKeyPrefix },
229
231
},
230
232
}
231
- utils .CheckCertificateStateIndexes (t , setup , testIntermediateCertificate , indexes )
233
+ utils .CheckCertificateStateIndexes (t , setup , testIntermediateCertificate1 , indexes )
234
+ utils .CheckCertificateStateIndexes (t , setup , testIntermediateCertificate2 , indexes )
232
235
}
233
236
234
237
func TestHandler_AddDaIntermediateCert_VIDScopedRoot (t * testing.T ) {
@@ -240,7 +243,7 @@ func TestHandler_AddDaIntermediateCert_VIDScopedRoot(t *testing.T) {
240
243
rootCert := utils .RootDaCertificateWithNumericVid (setup .Trustee1 )
241
244
utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , rootCert )
242
245
243
- // add x509 certificate
246
+ // add certificate
244
247
testIntermediateCertificate := utils .IntermediateDaCertificateWithNumericPidVid (accAddress )
245
248
utils .AddDaIntermediateCertificate (setup , testIntermediateCertificate )
246
249
@@ -297,20 +300,22 @@ func TestHandler_AddDaIntermediateCert_NonVIDScopedRoot(t *testing.T) {
297
300
for _ , tc := range cases {
298
301
t .Run (tc .name , func (t * testing.T ) {
299
302
setup := utils .Setup (t )
303
+
300
304
// store root certificate
301
305
utils .ProposeAndApproveRootCertificateByOptions (setup , setup .Trustee1 , tc .rootCertOptions )
302
306
303
307
// add vendor account
304
308
setup .AddAccount (accAddress , []dclauthtypes.AccountRole {dclauthtypes .Vendor }, tc .accountVid )
305
309
306
- // add x509 certificate
310
+ // add certificate
307
311
addX509Cert := types .NewMsgAddX509Cert (accAddress .String (), tc .childCert , testconstants .CertSchemaVersion )
308
312
_ , err := setup .Handler (setup .Ctx , addX509Cert )
309
313
require .NoError (setup .T , err )
310
314
311
315
// query certificate
312
316
certs , _ := utils .QueryAllApprovedCertificates (setup )
313
317
require .Equal (t , 2 , len (certs ))
318
+
314
319
intermediateCerts , _ := utils .QueryApprovedCertificates (setup , tc .childCertSubject , tc .childCertSubjectKeyID )
315
320
require .Equal (t , 1 , len (intermediateCerts .Certs ))
316
321
require .Equal (t , tc .childCertSubject , intermediateCerts .Certs [0 ].Subject )
@@ -333,7 +338,7 @@ func TestHandler_AddDaIntermediateCert_ForInvalidCertificate(t *testing.T) {
333
338
func TestHandler_AddDaIntermediateCert_ForRootCertificate (t * testing.T ) {
334
339
setup := utils .Setup (t )
335
340
336
- // add root certificate as leaf x509 certificate
341
+ // add root certificate as leaf
337
342
addX509Cert := types .NewMsgAddX509Cert (setup .Vendor1 .String (), testconstants .RootCertPem , testconstants .CertSchemaVersion )
338
343
_ , err := setup .Handler (setup .Ctx , addX509Cert )
339
344
require .ErrorIs (t , err , pkitypes .ErrNonRootCertificateSelfSigned )
@@ -347,7 +352,11 @@ func TestHandler_AddDaIntermediateCert_ForDuplicate(t *testing.T) {
347
352
setup .Keeper .AddAllCertificate (setup .Ctx , rootCertificate )
348
353
349
354
// store intermediate certificate
350
- addX509Cert := types .NewMsgAddX509Cert (setup .Vendor1 .String (), testconstants .IntermediateCertPem , testconstants .CertSchemaVersion )
355
+ testIntermediateCertificate := utils .IntermediateDaCertificate (setup .Vendor1 )
356
+ addX509Cert := types .NewMsgAddX509Cert (
357
+ setup .Vendor1 .String (),
358
+ testIntermediateCertificate .PemCert ,
359
+ testconstants .CertSchemaVersion )
351
360
_ , err := setup .Handler (setup .Ctx , addX509Cert )
352
361
require .NoError (t , err )
353
362
@@ -359,15 +368,19 @@ func TestHandler_AddDaIntermediateCert_ForDuplicate(t *testing.T) {
359
368
func TestHandler_AddDaIntermediateCert_RootIsNoc (t * testing.T ) {
360
369
setup := utils .Setup (t )
361
370
362
- accAddress := setup .CreateVendorAccount (testconstants .IntermediateCertWithVid1Vid )
363
-
364
371
// Add NOC root certificate
365
- addNocX509RootCert := types .NewMsgAddNocX509RootCert (accAddress .String (), testconstants .RootCertPem , testconstants .CertSchemaVersion )
372
+ addNocX509RootCert := types .NewMsgAddNocX509RootCert (
373
+ setup .Vendor1 .String (),
374
+ testconstants .RootCertPem ,
375
+ testconstants .CertSchemaVersion )
366
376
_ , err := setup .Handler (setup .Ctx , addNocX509RootCert )
367
377
require .NoError (t , err )
368
378
369
- // add x509 certificate
370
- addX509Cert := types .NewMsgAddX509Cert (accAddress .String (), testconstants .IntermediateCertPem , testconstants .CertSchemaVersion )
379
+ // add intermediate certificate
380
+ addX509Cert := types .NewMsgAddX509Cert (
381
+ setup .Vendor1 .String (),
382
+ testconstants .IntermediateCertPem ,
383
+ testconstants .CertSchemaVersion )
371
384
_ , err = setup .Handler (setup .Ctx , addX509Cert )
372
385
require .ErrorIs (t , err , pkitypes .ErrInappropriateCertificateType )
373
386
}
@@ -450,6 +463,7 @@ func TestHandler_AddDaIntermediateCert_VIDScopedRoot_NegativeCases(t *testing.T)
450
463
for _ , tc := range cases {
451
464
t .Run (tc .name , func (t * testing.T ) {
452
465
setup := utils .Setup (t )
466
+
453
467
// store root certificate
454
468
utils .ProposeAndApproveRootCertificateByOptions (setup , setup .Trustee1 , tc .rootCertOptions )
455
469
0 commit comments