1
1
package tests
2
2
3
3
import (
4
- "github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/tests/utils"
5
4
"math"
6
- "math/rand"
7
5
"testing"
8
6
9
- sdk "github.com/cosmos/cosmos-sdk/types"
10
7
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
11
8
"github.com/stretchr/testify/require"
12
9
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
13
10
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/types/pki"
14
11
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
12
+ "github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/tests/utils"
15
13
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
16
14
"google.golang.org/grpc/codes"
17
15
"google.golang.org/grpc/status"
@@ -34,12 +32,14 @@ func TestHandler_ProposeAddDaRootCert(t *testing.T) {
34
32
require .NoError (t , err )
35
33
36
34
// Check: ProposedCertificate - present
37
- proposedCertificate , _ := utils .QueryProposedCertificate (setup , testconstants .RootSubject , testconstants .RootSubjectKeyID )
35
+ proposedCertificate := utils .EnsureProposedDaRootCertificateExist (
36
+ t ,
37
+ setup ,
38
+ testconstants .RootSubject ,
39
+ testconstants .RootSubjectKeyID ,
40
+ testconstants .RootSerialNumber ,
41
+ )
38
42
require .Equal (t , proposeAddX509RootCert .Cert , proposedCertificate .PemCert )
39
- require .Equal (t , proposeAddX509RootCert .Signer , proposedCertificate .Owner )
40
- require .Equal (t , testconstants .RootSubject , proposedCertificate .Subject )
41
- require .Equal (t , testconstants .RootSubjectKeyID , proposedCertificate .SubjectKeyId )
42
- require .Equal (t , testconstants .RootSerialNumber , proposedCertificate .SerialNumber )
43
43
require .True (t , proposedCertificate .HasApprovalFrom (proposeAddX509RootCert .Signer ))
44
44
45
45
// Check: UniqueCertificate - present
@@ -129,15 +129,7 @@ func TestHandler_AddDaRootCert_TwoThirdApprovalsNeeded(t *testing.T) {
129
129
require .NoError (t , err )
130
130
131
131
// Create an array of trustee account from 1 to 50
132
- trusteeAccounts := make ([]sdk.AccAddress , 50 )
133
- for i := 0 ; i < 50 ; i ++ {
134
- trusteeAccounts [i ] = utils .GenerateAccAddress ()
135
- }
136
-
137
- totalAdditionalTrustees := rand .Intn (50 )
138
- for i := 0 ; i < totalAdditionalTrustees ; i ++ {
139
- setup .AddAccount (trusteeAccounts [i ], []dclauthtypes.AccountRole {dclauthtypes .Trustee }, 1 )
140
- }
132
+ trusteeAccounts , totalAdditionalTrustees := setup .CreateNTrusteeAccounts ()
141
133
142
134
// We have 3 Trustees in test setup.
143
135
twoThirds := int (math .Ceil (types .RootCertificateApprovalsPercent * float64 (3 + totalAdditionalTrustees )))
@@ -289,34 +281,42 @@ func TestHandler_ProposeAddX509RootCert_ForDifferentSerialNumber(t *testing.T) {
289
281
// store root certificate with different serial number
290
282
rootCertificate := utils .RootCertificate (setup .Trustee1 )
291
283
rootCertificate .SerialNumber = utils .SerialNumber
292
- setup .Keeper .SetUniqueCertificate (
293
- setup .Ctx ,
294
- utils .UniqueCertificate (rootCertificate .Subject , rootCertificate .SerialNumber ),
295
- )
296
- setup .Keeper .AddApprovedCertificate (setup .Ctx , rootCertificate )
284
+ utils .AddMokedDaCertificate (setup , rootCertificate , true )
297
285
298
286
// propose second root certificate
299
- proposeAddX509RootCert := types .NewMsgProposeAddX509RootCert (setup .Trustee1 .String (), testconstants .RootCertPem , testconstants .Info , testconstants .Vid , testconstants .CertSchemaVersion )
287
+ proposeAddX509RootCert := types .NewMsgProposeAddX509RootCert (
288
+ setup .Trustee1 .String (),
289
+ testconstants .RootCertPem ,
290
+ testconstants .Info ,
291
+ testconstants .Vid ,
292
+ testconstants .CertSchemaVersion )
300
293
_ , err := setup .Handler (setup .Ctx , proposeAddX509RootCert )
301
294
require .NoError (t , err )
302
295
303
- // check
304
- certificate , _ := utils .QueryApprovedCertificates ( setup , testconstants . RootSubject , testconstants . RootSubjectKeyID )
305
- require . True ( t , certificate . Certs [ 0 ]. IsRoot )
306
- require . Equal ( t , testconstants . RootIssuer , certificate . Certs [ 0 ]. Subject )
307
- require . Equal ( t , utils . SerialNumber , certificate . Certs [ 0 ]. SerialNumber )
308
-
309
- proposedCertificate , _ := utils . QueryProposedCertificate ( setup , testconstants .RootSubject , testconstants . RootSubjectKeyID )
310
- require . Equal ( t , testconstants . RootIssuer , proposedCertificate . Subject )
311
- require .Equal (t , testconstants . RootSerialNumber , proposedCertificate . SerialNumber )
296
+ // Check: Approved certificate exist in all indexes
297
+ approvedCertificate := utils .EnsureDaRootCertificateExist (
298
+ t ,
299
+ setup ,
300
+ testconstants . RootSubject ,
301
+ testconstants . RootSubjectKeyID ,
302
+ testconstants .RootIssuer ,
303
+ utils . SerialNumber )
304
+ require .Len (t , approvedCertificate . Certs , 1 )
312
305
313
- require .NotEqual (t , certificate .Certs [0 ].SerialNumber , proposedCertificate .SerialNumber )
306
+ // Checked proposed certificate exist
307
+ proposedCertificate := utils .EnsureProposedDaRootCertificateExist (
308
+ t ,
309
+ setup ,
310
+ testconstants .RootSubject ,
311
+ testconstants .RootSubjectKeyID ,
312
+ testconstants .RootSerialNumber )
313
+ require .True (t , proposedCertificate .HasApprovalFrom (proposeAddX509RootCert .Signer ))
314
314
}
315
315
316
- func TestHandler_AddX509RootCertsBySubjectKeyId (t * testing.T ) {
316
+ func TestHandler_AddDaRootCerts_SameSubjectButDifferentSubjectKeyId (t * testing.T ) {
317
317
setup := utils .Setup (t )
318
318
319
- // add root certificates
319
+ // add Certificate1
320
320
rootCertOptions := & utils.RootCertOptions {
321
321
PemCert : testconstants .PAACertWithSameSubjectID1 ,
322
322
Subject : testconstants .PAACertWithSameSubjectID1Subject ,
@@ -325,48 +325,82 @@ func TestHandler_AddX509RootCertsBySubjectKeyId(t *testing.T) {
325
325
Vid : testconstants .Vid ,
326
326
}
327
327
utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , rootCertOptions )
328
+
329
+ // add Certificate2
328
330
rootCertOptions .PemCert = testconstants .PAACertWithSameSubjectID2
329
331
rootCertOptions .Subject = testconstants .PAACertWithSameSubjectID2Subject
330
332
utils .ProposeAndApproveRootCertificate (setup , setup .Trustee1 , rootCertOptions )
331
333
332
- approvedCertificates , _ := utils .QueryApprovedCertificatesBySubjectKeyID (setup , testconstants .PAACertWithSameSubjectIDSubjectID )
333
- require .Equal (t , 1 , len (approvedCertificates ))
334
- require .Equal (t , 2 , len (approvedCertificates [0 ].Certs ))
335
- require .Equal (t , testconstants .PAACertWithSameSubjectIDSubjectID , approvedCertificates [0 ].SubjectKeyId )
336
- require .Equal (t , testconstants .PAACertWithSameSubjectID1Subject , approvedCertificates [0 ].Certs [0 ].Subject )
337
- require .Equal (t , testconstants .PAACertWithSameSubjectID2Subject , approvedCertificates [0 ].Certs [1 ].Subject )
334
+ // Check indexes by subject key id
335
+ approvedCertificatesBySubjectKeyId , _ := utils .QueryApprovedCertificatesBySubjectKeyID (setup , testconstants .PAACertWithSameSubjectIDSubjectID )
336
+ require .Equal (t , 1 , len (approvedCertificatesBySubjectKeyId ))
337
+ require .Equal (t , 2 , len (approvedCertificatesBySubjectKeyId [0 ].Certs ))
338
+ require .Equal (t , testconstants .PAACertWithSameSubjectIDSubjectID , approvedCertificatesBySubjectKeyId [0 ].SubjectKeyId )
339
+ require .Equal (t , testconstants .PAACertWithSameSubjectID1Subject , approvedCertificatesBySubjectKeyId [0 ].Certs [0 ].Subject )
340
+ require .Equal (t , testconstants .PAACertWithSameSubjectID2Subject , approvedCertificatesBySubjectKeyId [0 ].Certs [1 ].Subject )
341
+
342
+ allCertificatesBySubjectKeyId , _ := utils .QueryAllCertificatesBySubjectKeyID (setup , testconstants .PAACertWithSameSubjectIDSubjectID )
343
+ require .Equal (t , 1 , len (allCertificatesBySubjectKeyId ))
344
+ require .Equal (t , 2 , len (allCertificatesBySubjectKeyId [0 ].Certs ))
345
+ require .Equal (t , testconstants .PAACertWithSameSubjectIDSubjectID , allCertificatesBySubjectKeyId [0 ].SubjectKeyId )
346
+ require .Equal (t , testconstants .PAACertWithSameSubjectID1Subject , allCertificatesBySubjectKeyId [0 ].Certs [0 ].Subject )
347
+ require .Equal (t , testconstants .PAACertWithSameSubjectID2Subject , allCertificatesBySubjectKeyId [0 ].Certs [1 ].Subject )
348
+
349
+ // Check indexes by subject + subject key id
350
+ allApprovedCertificates , _ := utils .QueryAllApprovedCertificates (setup )
351
+ require .Equal (t , 2 , len (allApprovedCertificates ))
352
+
353
+ allCertificates , _ := utils .QueryAllCertificatesAll (setup )
354
+ require .Equal (t , 2 , len (allCertificates ))
355
+
356
+ // Check indexes by subject + subject key id
357
+ approvedCertificatesBySubject , _ := utils .QueryApprovedCertificatesBySubject (setup , testconstants .PAACertWithSameSubjectID1Subject )
358
+ require .Equal (t , 1 , len (approvedCertificatesBySubject .SubjectKeyIds ))
359
+
360
+ allCertificatesBySubject , _ := utils .QueryAllCertificatesBySubject (setup , testconstants .PAACertWithSameSubjectID2Subject )
361
+ require .Equal (t , 1 , len (allCertificatesBySubject .SubjectKeyIds ))
338
362
}
339
363
340
364
func TestHandler_RejectAddDaRootCert (t * testing.T ) {
341
365
setup := utils .Setup (t )
342
366
343
367
// propose x509 root certificate by account Trustee1
344
- proposeAddX509RootCert := types .NewMsgProposeAddX509RootCert (setup .Trustee1 .String (), testconstants .RootCertPem , testconstants .Info , testconstants .Vid , testconstants .CertSchemaVersion )
368
+ proposeAddX509RootCert := types .NewMsgProposeAddX509RootCert (
369
+ setup .Trustee1 .String (),
370
+ testconstants .RootCertPem ,
371
+ testconstants .Info ,
372
+ testconstants .Vid ,
373
+ testconstants .CertSchemaVersion )
345
374
_ , err := setup .Handler (setup .Ctx , proposeAddX509RootCert )
346
375
require .NoError (t , err )
347
376
348
377
// reject x509 root certificate by account Trustee2
349
- rejectAddX509RootCert := types .NewMsgRejectAddX509RootCert (setup .Trustee2 .String (), testconstants .RootSubject , testconstants .RootSubjectKeyID , testconstants .Info )
378
+ rejectAddX509RootCert := types .NewMsgRejectAddX509RootCert (
379
+ setup .Trustee2 .String (),
380
+ testconstants .RootSubject ,
381
+ testconstants .RootSubjectKeyID ,
382
+ testconstants .Info )
350
383
_ , err = setup .Handler (setup .Ctx , rejectAddX509RootCert )
351
384
require .NoError (t , err )
352
385
353
386
// certificate should be in the entity <Proposed X509 Root Certificate>, because we haven't enough reject approvals
354
- proposedCertificate , err := utils .QueryProposedCertificate (setup , testconstants .RootSubject , testconstants .RootSubjectKeyID )
355
- require .NoError (t , err )
356
-
357
- // check proposed certificate
358
- require .Equal (t , proposeAddX509RootCert .Cert , proposedCertificate .PemCert )
359
- require .Equal (t , proposeAddX509RootCert .Signer , proposedCertificate .Owner )
360
- require .Equal (t , testconstants .RootSubject , proposedCertificate .Subject )
361
- require .Equal (t , testconstants .RootSubjectKeyID , proposedCertificate .SubjectKeyId )
362
- require .Equal (t , testconstants .RootSerialNumber , proposedCertificate .SerialNumber )
387
+ proposedCertificate := utils .EnsureProposedDaRootCertificateExist (
388
+ t ,
389
+ setup ,
390
+ testconstants .RootSubject ,
391
+ testconstants .RootSubjectKeyID ,
392
+ testconstants .RootSerialNumber )
363
393
require .Equal (t , setup .Trustee1 .String (), proposedCertificate .Approvals [0 ].Address )
364
394
require .Equal (t , testconstants .Info , proposedCertificate .Approvals [0 ].Info )
365
395
require .Equal (t , setup .Trustee2 .String (), proposedCertificate .Rejects [0 ].Address )
366
396
require .Equal (t , testconstants .Info , proposedCertificate .Rejects [0 ].Info )
367
397
368
398
// reject x509 root certificate by account Trustee3
369
- rejectAddX509RootCert = types .NewMsgRejectAddX509RootCert (setup .Trustee3 .String (), testconstants .RootSubject , testconstants .RootSubjectKeyID , testconstants .Info )
399
+ rejectAddX509RootCert = types .NewMsgRejectAddX509RootCert (
400
+ setup .Trustee3 .String (),
401
+ testconstants .RootSubject ,
402
+ testconstants .RootSubjectKeyID ,
403
+ testconstants .Info )
370
404
_ , err = setup .Handler (setup .Ctx , rejectAddX509RootCert )
371
405
require .NoError (t , err )
372
406
@@ -375,16 +409,11 @@ func TestHandler_RejectAddDaRootCert(t *testing.T) {
375
409
require .Error (t , err )
376
410
377
411
// certificate should be in the entity <Rejected X509 Root Certificate>, because we have enough rejected approvals
378
- rejectedCertificates , err := utils .QueryRejectedCertificates (setup , testconstants .RootSubject , testconstants .RootSubjectKeyID )
379
- require .NoError (t , err )
380
-
381
- // check rejected certificate
382
- rejectedCertificate := rejectedCertificates .Certs [0 ]
383
- require .Equal (t , proposeAddX509RootCert .Cert , rejectedCertificate .PemCert )
384
- require .Equal (t , proposeAddX509RootCert .Signer , rejectedCertificate .Owner )
385
- require .Equal (t , testconstants .RootSubject , rejectedCertificate .Subject )
386
- require .Equal (t , testconstants .RootSubjectKeyID , rejectedCertificate .SubjectKeyId )
387
- require .Equal (t , testconstants .RootSerialNumber , rejectedCertificate .SerialNumber )
412
+ rejectedCertificate := utils .EnsureRejectedDaRootCertificateExist (
413
+ t ,
414
+ setup ,
415
+ testconstants .RootSubject ,
416
+ testconstants .RootSubjectKeyID )
388
417
require .Equal (t , setup .Trustee1 .String (), rejectedCertificate .Approvals [0 ].Address )
389
418
require .Equal (t , testconstants .Info , rejectedCertificate .Approvals [0 ].Info )
390
419
require .Equal (t , setup .Trustee2 .String (), rejectedCertificate .Rejects [0 ].Address )
0 commit comments