Skip to content

Commit bf7a652

Browse files
committed
Refactoring PKI unit tests
1 parent 28470dd commit bf7a652

19 files changed

+1433
-1498
lines changed

integration_tests/constants/constants.go

+1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ mUkpxyqvChVI1A0DTVFLJd4=
285285
-----END CERTIFICATE-----`
286286
PAACertWithNumericVidSubject = "MDAxGDAWBgNVBAMMD01hdHRlciBUZXN0IFBBQTEUMBIGCisGAQQBgqJ8AgEMBEZGRjE="
287287
PAACertWithNumericVidSubjectKeyID = "6A:FD:22:77:1F:51:1F:EC:BF:16:41:97:67:10:DC:DC:31:A1:71:7E"
288+
PAACertWithNumericVidSerialNumber = "4ea8e83182d41c1c"
288289
PAACertWithNumericVidVid int32 = 65521
289290
PAACertWithNumericVidDifferentWhitespaces = `
290291
-----BEGIN CERTIFICATE-----

integration_tests/constants/noc_constants.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,12 @@ zodhpBXZfzhHDvINejK8wzwWgf7Ds8wk3oENlmAj
165165
NocCert1CopyAuthorityKeyID = NocCert1AuthorityKeyID
166166
NocCert1CopySubjectAsText = "CN=NOC-child-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
167167

168-
NocCert2Subject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMg=="
169-
NocCert2Issuer = NocRootCert2Subject
170-
NocCert2SubjectKeyID = "87:48:A2:33:12:1F:51:5C:93:E6:90:40:4A:2C:AB:9E:D6:19:E5:AD"
171-
NocCert2SerialNumber = "361372967010167010646904372658654439710639340814"
172-
NocCert2SubjectAsText = "CN=NOC-child-2,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
168+
NocCert2Subject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMg=="
169+
NocCert2Issuer = NocRootCert2Subject
170+
NocCert2AuthorityKeyID = NocRootCert2SubjectKeyID
171+
NocCert2SubjectKeyID = "87:48:A2:33:12:1F:51:5C:93:E6:90:40:4A:2C:AB:9E:D6:19:E5:AD"
172+
NocCert2SerialNumber = "361372967010167010646904372658654439710639340814"
173+
NocCert2SubjectAsText = "CN=NOC-child-2,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
173174

174175
NocLeafCert1Subject = "MIGBMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRMwEQYDVQQDDApOT0MtbGVhZi0x"
175176
NocLeafCert1Issuer = NocCert1Subject

x/pki/tests/handler_add_noc_ica_cert_test.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestHandler_AddNocIntermediateCert(t *testing.T) {
2626
icaCertificate := utils.CreateTestNocIca1Cert()
2727
utils.AddNocIntermediateCertificate(setup, setup.Vendor1, icaCertificate.PEM)
2828

29-
// Check indexes
29+
// Check state indexes
3030
indexes := utils.TestIndexes{
3131
Present: []utils.TestIndex{
3232
{Key: types.AllCertificatesKeyPrefix},
@@ -70,7 +70,7 @@ func TestHandler_AddNocIntermediateCert_SameSubjectAndSkid_DifferentSerialNumber
7070
icaCertificate := utils.CreateTestNocIca1Cert()
7171
utils.AddNocIntermediateCertificate(setup, setup.Vendor1, icaCertificate.PEM)
7272

73-
// query noc certificate by Subject and SKID
73+
// Check state indexes
7474
indexes := utils.TestIndexes{
7575
Present: []utils.TestIndex{
7676
{Key: types.AllCertificatesKeyPrefix, Count: 2},
@@ -143,14 +143,9 @@ func TestHandler_AddXNoc509Cert_ForRootNonNocCertificate(t *testing.T) {
143143
setup := utils.Setup(t)
144144

145145
// store root certificate
146-
rootCertOptions := &utils.RootCertOptions{
147-
PemCert: testconstants.RootCertWithVid,
148-
Info: testconstants.Info,
149-
Subject: testconstants.RootCertWithVidSubject,
150-
SubjectKeyID: testconstants.RootCertWithVidSubjectKeyID,
151-
Vid: testconstants.RootCertWithVidVid,
152-
}
153-
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, rootCertOptions)
146+
147+
rootCert := utils.CreateTestRootCertWithVid()
148+
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, &rootCert)
154149

155150
// try to add root certificate x509 certificate
156151
addX509Cert := types.NewMsgAddNocX509IcaCert(setup.Vendor1.String(), testconstants.IntermediateCertWithVid1, testconstants.CertSchemaVersion)

x/pki/tests/handler_add_noc_root_cert_test.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestHandler_AddNocRootCert(t *testing.T) {
2121
rootCertificate := utils.CreateTestNocRoot1Cert()
2222
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate.PEM)
2323

24-
// Check indexes
24+
// Check state indexes
2525
indexes := utils.TestIndexes{
2626
Present: []utils.TestIndex{
2727
{Key: types.AllCertificatesKeyPrefix},
@@ -57,14 +57,9 @@ func TestHandler_AddNocRootCert_SameSubjectAndSkid_DifferentSerialNumber(t *test
5757

5858
// add the new NOC root certificate
5959
rootCertificate2 := utils.CreateTestNocRoot2Cert()
60-
addNocX509RootCert := types.NewMsgAddNocX509RootCert(
61-
setup.Vendor1.String(),
62-
rootCertificate2.PEM,
63-
testconstants.CertSchemaVersion)
64-
_, err := setup.Handler(setup.Ctx, addNocX509RootCert)
65-
require.NoError(t, err)
60+
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate2.PEM)
6661

67-
// check indexes
62+
// Check state indexes
6863
indexes := utils.TestIndexes{
6964
Present: []utils.TestIndex{
7065
{Key: types.AllCertificatesKeyPrefix, Count: 2},

x/pki/tests/handler_add_paa_cert_test.go

+35-44
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestHandler_ProposeAddDaRootCert(t *testing.T) {
2525
// propose DA root certificate
2626
proposeAddX509RootCert := utils.ProposeDaRootCertificate(setup, setup.Trustee1, rootCertificate.PEM)
2727

28-
// Check indexes
28+
// Check state indexes
2929
indexes := utils.TestIndexes{
3030
Present: []utils.TestIndex{
3131
{Key: types.ProposedCertificateKeyPrefix},
@@ -52,15 +52,14 @@ func TestHandler_ProposeAddDaRootCert(t *testing.T) {
5252
func TestHandler_AddDaRootCert(t *testing.T) {
5353
setup := utils.Setup(t)
5454

55-
rootCertificate := utils.CreateTestRootCert()
56-
5755
// propose add x509 root certificate by trustee
56+
rootCertificate := utils.CreateTestRootCert()
5857
utils.ProposeDaRootCertificate(setup, setup.Trustee1, rootCertificate.PEM)
5958

6059
// approve by second trustee
6160
utils.ApproveDaRootCertificate(setup, setup.Trustee2, rootCertificate.Subject, rootCertificate.SubjectKeyID)
6261

63-
// Check indexes
62+
// Check state indexes
6463
indexes := utils.TestIndexes{
6564
Present: []utils.TestIndex{
6665
{Key: types.UniqueCertificateKeyPrefix},
@@ -83,9 +82,8 @@ func TestHandler_AddDaRootCert(t *testing.T) {
8382
func TestHandler_AddDaRootCert_TwoThirdApprovalsNeeded(t *testing.T) {
8483
setup := utils.Setup(t)
8584

86-
rootCertificate := utils.CreateTestRootCert()
87-
8885
// propose x509 root certificate by account without trustee role
86+
rootCertificate := utils.CreateTestRootCert()
8987
utils.ProposeDaRootCertificate(setup, setup.Trustee1, rootCertificate.PEM)
9088

9189
// Create an array of trustee account from 1 to 50
@@ -98,7 +96,7 @@ func TestHandler_AddDaRootCert_TwoThirdApprovalsNeeded(t *testing.T) {
9896
for i := 1; i < twoThirds-1; i++ {
9997
utils.ApproveDaRootCertificate(setup, trusteeAccounts[i], rootCertificate.Subject, rootCertificate.SubjectKeyID)
10098

101-
// Check indexes
99+
// Check state indexes
102100
indexes := utils.TestIndexes{
103101
Present: []utils.TestIndex{
104102
{Key: types.UniqueCertificateKeyPrefix},
@@ -121,7 +119,7 @@ func TestHandler_AddDaRootCert_TwoThirdApprovalsNeeded(t *testing.T) {
121119
// One more approval will move this to approved state from pending
122120
utils.ApproveDaRootCertificate(setup, setup.Trustee2, rootCertificate.Subject, rootCertificate.SubjectKeyID)
123121

124-
// Check indexes
122+
// Check state indexes
125123
indexes := utils.TestIndexes{
126124
Present: []utils.TestIndex{
127125
{Key: types.UniqueCertificateKeyPrefix},
@@ -140,7 +138,7 @@ func TestHandler_AddDaRootCert_TwoThirdApprovalsNeeded(t *testing.T) {
140138
}
141139
resolvedCertificates := utils.CheckCertificateStateIndexes(t, setup, rootCertificate, indexes)
142140

143-
// Additional check: Check all approvals are present
141+
// Additional checks
144142
for i := 1; i < twoThirds-1; i++ {
145143
require.Equal(t, resolvedCertificates.ApprovedCertificates.Certs[0].HasApprovalFrom(trusteeAccounts[i].String()), true)
146144
}
@@ -151,8 +149,6 @@ func TestHandler_AddDaRootCert_TwoThirdApprovalsNeeded(t *testing.T) {
151149
func TestHandler_AddDaRootCert_FourApprovalsAreNeeded_FiveTrustees(t *testing.T) {
152150
setup := utils.Setup(t)
153151

154-
rootCertificate := utils.CreateTestRootCert()
155-
156152
// we have 5 trustees: 1 approval comes from propose => we need 3 more approvals
157153

158154
// store 4th trustee
@@ -164,6 +160,7 @@ func TestHandler_AddDaRootCert_FourApprovalsAreNeeded_FiveTrustees(t *testing.T)
164160
setup.AddAccount(fifthTrustee, []dclauthtypes.AccountRole{dclauthtypes.Trustee}, 1)
165161

166162
// propose x509 root certificate by account Trustee1
163+
rootCertificate := utils.CreateTestRootCert()
167164
utils.ProposeDaRootCertificate(setup, setup.Trustee1, rootCertificate.PEM)
168165

169166
// approve x509 root certificate by account Trustee2
@@ -175,19 +172,29 @@ func TestHandler_AddDaRootCert_FourApprovalsAreNeeded_FiveTrustees(t *testing.T)
175172
// reject x509 root certificate by account Trustee4
176173
utils.RejectDaRootCertificate(setup, fourthTrustee, rootCertificate.Subject, rootCertificate.SubjectKeyID)
177174

178-
// Check: ProposedCertificate - present because we haven't enough approvals
175+
// Check state indexes - certificate is in proposed state
179176
indexes := utils.TestIndexes{
180177
Present: []utils.TestIndex{
181178
{Key: types.ProposedCertificateKeyPrefix},
179+
{Key: types.UniqueCertificateKeyPrefix},
180+
},
181+
Missing: []utils.TestIndex{
182+
{Key: types.RejectedCertificateKeyPrefix},
183+
{Key: types.AllCertificatesKeyPrefix},
184+
{Key: types.AllCertificatesBySubjectKeyPrefix},
185+
{Key: types.AllCertificatesBySubjectKeyIDKeyPrefix},
186+
{Key: types.ApprovedCertificatesKeyPrefix},
187+
{Key: types.ApprovedCertificatesBySubjectKeyPrefix},
188+
{Key: types.ApprovedCertificatesBySubjectKeyIDKeyPrefix},
189+
{Key: types.ApprovedRootCertificatesKeyPrefix},
182190
},
183-
Missing: []utils.TestIndex{},
184191
}
185192
utils.CheckCertificateStateIndexes(t, setup, rootCertificate, indexes)
186193

187194
// approve x509 root certificate by account Trustee5
188195
utils.ApproveDaRootCertificate(setup, fifthTrustee, rootCertificate.Subject, rootCertificate.SubjectKeyID)
189196

190-
// Check indexes
197+
// Check state indexes
191198
indexes = utils.TestIndexes{
192199
Present: []utils.TestIndex{
193200
{Key: types.UniqueCertificateKeyPrefix},
@@ -221,7 +228,7 @@ func TestHandler_ProposeAddX509RootCert_ForDifferentSerialNumber(t *testing.T) {
221228
testRootCertificate := utils.CreateTestRootCert()
222229
utils.ProposeDaRootCertificate(setup, setup.Trustee1, testRootCertificate.PEM)
223230

224-
// Check indexes
231+
// Check state indexes
225232
indexes := utils.TestIndexes{
226233
Present: []utils.TestIndex{
227234
{Key: types.ProposedCertificateKeyPrefix}, // we have both: Proposed and Approved
@@ -247,28 +254,13 @@ func TestHandler_ProposeAddX509RootCert_ForDifferentSerialNumber(t *testing.T) {
247254
func TestHandler_AddDaRootCerts_SameSubjectKeyIdButDifferentSubject(t *testing.T) {
248255
setup := utils.Setup(t)
249256

250-
testRootCertificate := utils.CreateTestRootCertWithSameSubject()
251-
testRootCertificate2 := utils.CreateTestRootCertWithSameSubject2()
252-
253257
// add Certificate1
254-
rootCertOptions := &utils.RootCertOptions{
255-
PemCert: testRootCertificate.PEM,
256-
Subject: testRootCertificate.Subject,
257-
SubjectKeyID: testRootCertificate.SubjectKeyID,
258-
Info: testconstants.Info,
259-
Vid: testconstants.Vid,
260-
}
261-
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, rootCertOptions)
258+
testRootCertificate := utils.CreateTestRootCertWithSameSubject()
259+
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, &testRootCertificate)
262260

263261
// add Certificate2
264-
rootCert2Options := &utils.RootCertOptions{
265-
PemCert: testRootCertificate2.PEM,
266-
Subject: testRootCertificate2.Subject,
267-
SubjectKeyID: testRootCertificate2.SubjectKeyID,
268-
Info: testconstants.Info,
269-
Vid: testconstants.Vid,
270-
}
271-
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, rootCert2Options)
262+
testRootCertificate2 := utils.CreateTestRootCertWithSameSubject2()
263+
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, &testRootCertificate2)
272264

273265
// Check indexes by subject + subject key id
274266
allApprovedCertificates, _ := utils.QueryAllApprovedCertificates(setup)
@@ -277,7 +269,7 @@ func TestHandler_AddDaRootCerts_SameSubjectKeyIdButDifferentSubject(t *testing.T
277269
allCertificates, _ := utils.QueryAllCertificatesAll(setup)
278270
require.Equal(t, 2, len(allCertificates))
279271

280-
// Check indexes
272+
// Check state indexes
281273
indexes := utils.TestIndexes{
282274
Present: []utils.TestIndex{
283275
{Key: types.UniqueCertificateKeyPrefix},
@@ -308,9 +300,8 @@ func TestHandler_AddDaRootCerts_SameSubjectKeyIdButDifferentSubject(t *testing.T
308300
func TestHandler_RejectAddDaRootCert(t *testing.T) {
309301
setup := utils.Setup(t)
310302

311-
testRootCertificate := utils.CreateTestRootCert()
312-
313303
// propose x509 root certificate by account Trustee1
304+
testRootCertificate := utils.CreateTestRootCert()
314305
utils.ProposeDaRootCertificate(setup, setup.Trustee1, testRootCertificate.PEM)
315306

316307
// reject x509 root certificate by account Trustee2
@@ -377,9 +368,8 @@ func TestHandler_RejectAddDaRootCert(t *testing.T) {
377368
func TestHandler_ApproveX509RootCertAndRejectX509RootCert_FromTheSameTrustee(t *testing.T) {
378369
setup := utils.Setup(t)
379370

380-
rootCertificate := utils.CreateTestRootCert()
381-
382371
// propose add x509 root certificate
372+
rootCertificate := utils.CreateTestRootCert()
383373
utils.ProposeDaRootCertificate(setup, setup.Trustee1, rootCertificate.PEM)
384374

385375
for _, role := range []dclauthtypes.AccountRole{
@@ -413,9 +403,8 @@ func TestHandler_ApproveX509RootCertAndRejectX509RootCert_FromTheSameTrustee(t *
413403
func TestHandler_RejectX509RootCertAndApproveX509RootCert_FromTheSameTrustee(t *testing.T) {
414404
setup := utils.Setup(t)
415405

416-
rootCertificate := utils.CreateTestRootCert()
417-
418406
// propose add x509 root certificate
407+
rootCertificate := utils.CreateTestRootCert()
419408
utils.ProposeDaRootCertificate(setup, setup.Trustee1, rootCertificate.PEM)
420409

421410
for _, role := range []dclauthtypes.AccountRole{
@@ -467,6 +456,7 @@ func TestHandler_RejectX509RootCert_TwoRejectApprovalsAreNeeded_FiveTrustees(t *
467456
// reject x509 root certificate by account Trustee2
468457
utils.RejectDaRootCertificate(setup, setup.Trustee2, rootCertificate.Subject, rootCertificate.SubjectKeyID)
469458

459+
// Check state indexes
470460
indexes := utils.TestIndexes{
471461
Present: []utils.TestIndex{
472462
{Key: types.ProposedCertificateKeyPrefix},
@@ -488,6 +478,7 @@ func TestHandler_RejectX509RootCert_TwoRejectApprovalsAreNeeded_FiveTrustees(t *
488478
// reject x509 root certificate by account Trustee3
489479
utils.RejectDaRootCertificate(setup, setup.Trustee3, rootCertificate.Subject, rootCertificate.SubjectKeyID)
490480

481+
// Check state indexes
491482
indexes = utils.TestIndexes{
492483
Present: []utils.TestIndex{
493484
{Key: types.RejectedCertificateKeyPrefix}, // certificate is rejected now
@@ -517,7 +508,7 @@ func TestHandler_ProposeAddAndRejectX509RootCert_ByTrustee(t *testing.T) {
517508
// reject x509 root certificate
518509
utils.RejectDaRootCertificate(setup, setup.Trustee1, rootCertificate.Subject, rootCertificate.SubjectKeyID)
519510

520-
// check state indexes
511+
// Check state indexes
521512
indexes := utils.TestIndexes{
522513
Present: []utils.TestIndex{},
523514
Missing: []utils.TestIndex{
@@ -546,7 +537,7 @@ func TestHandler_ProposeAddAndRejectX509RootCert_ByAnotherTrustee(t *testing.T)
546537
// reject x509 root certificate
547538
utils.RejectDaRootCertificate(setup, setup.Trustee2, rootCertificate.Subject, rootCertificate.SubjectKeyID)
548539

549-
// check state indexes
540+
// Check state indexes
550541
indexes := utils.TestIndexes{
551542
Present: []utils.TestIndex{
552543
{Key: types.UniqueCertificateKeyPrefix},
@@ -582,7 +573,7 @@ func TestHandler_ProposeAddAndRejectX509RootCertWithApproval_ByTrustee(t *testin
582573
// reject x509 root certificate
583574
utils.RejectDaRootCertificate(setup, setup.Trustee1, rootCertificate.Subject, rootCertificate.SubjectKeyID)
584575

585-
// check state indexes
576+
// Check state indexes
586577
indexes := utils.TestIndexes{
587578
Present: []utils.TestIndex{
588579
{Key: types.UniqueCertificateKeyPrefix},

0 commit comments

Comments
 (0)