Skip to content

Commit 95bfb65

Browse files
committed
Refactoring unit tests for PKI module
1 parent e46ead6 commit 95bfb65

25 files changed

+402
-224
lines changed

integration_tests/constants/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ i+oDPOUDAiAlVJQ75X1T1sR199I+v8/CA2zSm6Y5PsfvrYcUq3GCGQ==
352352
PAICertWithNumericPidVidSubjectKeyID = "AF:42:B7:09:4D:EB:D5:15:EC:6E:CF:33:B8:11:15:22:5F:32:52:88"
353353
PAICertWithNumericPidVidVid = 65521
354354
PAICertWithNumericPidVidPid = 32768
355+
PAICertWithNumericPidVidSerialNumber = "4498223361705918669"
355356

356357
PAICertWithPidVid = `
357358
-----BEGIN CERTIFICATE-----

x/pki/keeper/certificate_helpers.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func FilterCertificateList(certificates *[]*types.Certificate, predicate Certifi
8282
return result
8383
}
8484

85-
func (k msgServer) AddCertificateToGlobalCertificateIndexes(
85+
func (k Keeper) AddCertificateToGlobalCertificateIndexes(
8686
ctx sdk.Context,
8787
certificate types.Certificate,
8888
) {
@@ -94,7 +94,7 @@ func (k msgServer) AddCertificateToGlobalCertificateIndexes(
9494
k.AddAllCertificateBySubject(ctx, certificate.Subject, certificate.SubjectKeyId)
9595
}
9696

97-
func (k msgServer) RemoveCertificateFromGlobalCertificateIndexes(
97+
func (k Keeper) RemoveCertificateFromGlobalCertificateIndexes(
9898
ctx sdk.Context,
9999
subject string,
100100
subjectKeyID string,
@@ -107,7 +107,7 @@ func (k msgServer) RemoveCertificateFromGlobalCertificateIndexes(
107107
k.RemoveAllCertificateBySubject(ctx, subject, subjectKeyID)
108108
}
109109

110-
func (k msgServer) StoreDaCertificate(
110+
func (k Keeper) StoreDaCertificate(
111111
ctx sdk.Context,
112112
certificate types.Certificate,
113113
isRoot bool,
@@ -133,7 +133,7 @@ func (k msgServer) StoreDaCertificate(
133133
}
134134
}
135135

136-
func (k msgServer) RemoveDaCertificate(
136+
func (k Keeper) RemoveDaCertificate(
137137
ctx sdk.Context,
138138
subject string,
139139
subjectKeyID string,
@@ -152,7 +152,7 @@ func (k msgServer) RemoveDaCertificate(
152152
}
153153
}
154154

155-
func (k msgServer) RemoveDaCertificateBySerialNumber(
155+
func (k Keeper) RemoveDaCertificateBySerialNumber(
156156
ctx sdk.Context,
157157
subject string,
158158
subjectKeyID string,
@@ -173,7 +173,7 @@ func (k msgServer) RemoveDaCertificateBySerialNumber(
173173
}
174174
}
175175

176-
func (k msgServer) StoreNocCertificate(
176+
func (k Keeper) StoreNocCertificate(
177177
ctx sdk.Context,
178178
certificate types.Certificate,
179179
isRoot bool) {
@@ -203,7 +203,7 @@ func (k msgServer) StoreNocCertificate(
203203
}
204204
}
205205

206-
func (k msgServer) RemoveNocCertificate(
206+
func (k Keeper) RemoveNocCertificate(
207207
ctx sdk.Context,
208208
subject string,
209209
subjectKeyID string,
@@ -229,7 +229,7 @@ func (k msgServer) RemoveNocCertificate(
229229
}
230230
}
231231

232-
func (k msgServer) RemoveNocCertBySerialNumber(
232+
func (k Keeper) RemoveNocCertBySerialNumber(
233233
ctx sdk.Context,
234234
subject string,
235235
subjectKeyID string,

x/pki/tests/handler_add_noc_ica_cert_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tests
22

33
import (
4-
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/tests/utils"
54
"testing"
65

76
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
@@ -10,6 +9,7 @@ import (
109
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
1110
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/types/pki"
1211
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
12+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/tests/utils"
1313
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
1414
)
1515

x/pki/tests/handler_add_noc_root_cert_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package tests
22

33
import (
4-
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/tests/utils"
54
"testing"
65

76
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
87
"github.com/stretchr/testify/require"
98
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
109
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/types/pki"
1110
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
11+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/tests/utils"
1212
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
1313
)
1414

x/pki/tests/handler_add_paa_cert_test.go

+92-63
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package tests
22

33
import (
4-
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/tests/utils"
54
"math"
6-
"math/rand"
75
"testing"
86

9-
sdk "github.com/cosmos/cosmos-sdk/types"
107
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
118
"github.com/stretchr/testify/require"
129
testconstants "github.com/zigbee-alliance/distributed-compliance-ledger/integration_tests/constants"
1310
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/types/pki"
1411
dclauthtypes "github.com/zigbee-alliance/distributed-compliance-ledger/x/dclauth/types"
12+
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/tests/utils"
1513
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
1614
"google.golang.org/grpc/codes"
1715
"google.golang.org/grpc/status"
@@ -34,12 +32,14 @@ func TestHandler_ProposeAddDaRootCert(t *testing.T) {
3432
require.NoError(t, err)
3533

3634
// 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+
)
3842
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)
4343
require.True(t, proposedCertificate.HasApprovalFrom(proposeAddX509RootCert.Signer))
4444

4545
// Check: UniqueCertificate - present
@@ -129,15 +129,7 @@ func TestHandler_AddDaRootCert_TwoThirdApprovalsNeeded(t *testing.T) {
129129
require.NoError(t, err)
130130

131131
// 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()
141133

142134
// We have 3 Trustees in test setup.
143135
twoThirds := int(math.Ceil(types.RootCertificateApprovalsPercent * float64(3+totalAdditionalTrustees)))
@@ -289,34 +281,42 @@ func TestHandler_ProposeAddX509RootCert_ForDifferentSerialNumber(t *testing.T) {
289281
// store root certificate with different serial number
290282
rootCertificate := utils.RootCertificate(setup.Trustee1)
291283
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)
297285

298286
// 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)
300293
_, err := setup.Handler(setup.Ctx, proposeAddX509RootCert)
301294
require.NoError(t, err)
302295

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)
312305

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))
314314
}
315315

316-
func TestHandler_AddX509RootCertsBySubjectKeyId(t *testing.T) {
316+
func TestHandler_AddDaRootCerts_SameSubjectButDifferentSubjectKeyId(t *testing.T) {
317317
setup := utils.Setup(t)
318318

319-
// add root certificates
319+
// add Certificate1
320320
rootCertOptions := &utils.RootCertOptions{
321321
PemCert: testconstants.PAACertWithSameSubjectID1,
322322
Subject: testconstants.PAACertWithSameSubjectID1Subject,
@@ -325,48 +325,82 @@ func TestHandler_AddX509RootCertsBySubjectKeyId(t *testing.T) {
325325
Vid: testconstants.Vid,
326326
}
327327
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, rootCertOptions)
328+
329+
// add Certificate2
328330
rootCertOptions.PemCert = testconstants.PAACertWithSameSubjectID2
329331
rootCertOptions.Subject = testconstants.PAACertWithSameSubjectID2Subject
330332
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, rootCertOptions)
331333

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))
338362
}
339363

340364
func TestHandler_RejectAddDaRootCert(t *testing.T) {
341365
setup := utils.Setup(t)
342366

343367
// 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)
345374
_, err := setup.Handler(setup.Ctx, proposeAddX509RootCert)
346375
require.NoError(t, err)
347376

348377
// 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)
350383
_, err = setup.Handler(setup.Ctx, rejectAddX509RootCert)
351384
require.NoError(t, err)
352385

353386
// 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)
363393
require.Equal(t, setup.Trustee1.String(), proposedCertificate.Approvals[0].Address)
364394
require.Equal(t, testconstants.Info, proposedCertificate.Approvals[0].Info)
365395
require.Equal(t, setup.Trustee2.String(), proposedCertificate.Rejects[0].Address)
366396
require.Equal(t, testconstants.Info, proposedCertificate.Rejects[0].Info)
367397

368398
// 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)
370404
_, err = setup.Handler(setup.Ctx, rejectAddX509RootCert)
371405
require.NoError(t, err)
372406

@@ -375,16 +409,11 @@ func TestHandler_RejectAddDaRootCert(t *testing.T) {
375409
require.Error(t, err)
376410

377411
// 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)
388417
require.Equal(t, setup.Trustee1.String(), rejectedCertificate.Approvals[0].Address)
389418
require.Equal(t, testconstants.Info, rejectedCertificate.Approvals[0].Info)
390419
require.Equal(t, setup.Trustee2.String(), rejectedCertificate.Rejects[0].Address)

0 commit comments

Comments
 (0)