Skip to content

Commit 7d6ceaf

Browse files
committed
Refactoring PKI unit tests
1 parent 95bfb65 commit 7d6ceaf

31 files changed

+1902
-2265
lines changed

integration_tests/constants/constants.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
2020
sdk "github.com/cosmos/cosmos-sdk/types"
2121
"github.com/cosmos/cosmos-sdk/types/module/testutil"
22-
2322
"github.com/zigbee-alliance/distributed-compliance-ledger/x/common/types"
2423
)
2524

@@ -125,7 +124,9 @@ var (
125124
ProgramType = "Some Program Type"
126125
ProgramTypeVersion = "Some Program Type Version"
127126
Transport = "Some Transport"
128-
SoftwareVersionCertificationStatus = uint32(3)
127+
SoftwareVersionCertificationStatus = uint32(
128+
3,
129+
)
129130
ParentChild1 = "parent"
130131
ParentChild2 = "child"
131132
CertificationIDOfSoftwareComponent = "some certification ID of software component"
@@ -709,13 +710,16 @@ eujhLsD51w==
709710
IntermediateCertWithSameSubjectAndSKIDSubject = "MEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQ="
710711

711712
IntermediateCertWithSameSubjectIssuer = RootCertWithSameSubjectAndSKIDSubject
713+
IntermediateCertWithSameSubjectAuthorityKeyID = RootCertWithSameSubjectAndSKIDSubjectKeyID
712714
IntermediateCertWithSameSubjectAndSKIDSubjectKeyID = "2E:13:3B:44:52:2C:30:E9:EC:FB:45:FA:5D:E5:04:0A:C1:C6:E6:B9"
713715
IntermediateCertWithSameSubjectAndSKIDIssuer = RootCertWithSameSubjectAndSKIDSubject
714716
IntermediateCertWithSameSubjectAndSKID1SerialNumber = "3"
715717
IntermediateCertWithSameSubjectAndSKID2SerialNumber = "4"
716718
LeafCertWithSameSubjectAndSKIDSubject = "MEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQ="
717719
LeafCertWithSameSubjectAndSKIDSubjectKeyID = "12:16:55:8E:5E:2A:DF:04:D7:E6:FE:D1:53:69:61:98:EF:17:2F:03"
718720
LeafCertWithSameSubjectAndSKIDSerialNumber = "5"
721+
LeafCertWithSameSubjectIssuer = IntermediateCertWithSameSubjectAndSKIDSubject
722+
LeafCertWithSameSubjectAuthorityKeyID = IntermediateCertWithSameSubjectAndSKIDSubjectKeyID
719723

720724
IntermediateIssuer = "MDQxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApzb21lLXN0YXRlMRAwDgYDVQQKDAdyb290LWNh"
721725
IntermediateAuthorityKeyID = "5A:88:0E:6C:36:53:D0:7F:B0:89:71:A3:F4:73:79:09:30:E6:2B:DB"
@@ -725,8 +729,8 @@ eujhLsD51w==
725729
IntermediateSubjectKeyIDWithoutColumns = "4E3B73F4704DC2980DDBC85A5F023BBF8625562B"
726730
IntermediateSerialNumber = "169917617234879872371588777545667947720450185023"
727731

728-
LeafIssuer = "MDwxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApzb21lLXN0YXRlMRgwFgYDVQQKDA9pbnRlcm1lZGlhdGUtY2E="
729-
LeafAuthorityKeyID = "4E:3B:73:F4:70:4D:C2:98:D:DB:C8:5A:5F:02:3B:BF:86:25:56:2B"
732+
LeafIssuer = IntermediateSubject
733+
LeafAuthorityKeyID = IntermediateSubjectKeyID
730734
LeafSubject = "MDExCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApzb21lLXN0YXRlMQ0wCwYDVQQKDARsZWFm"
731735
LeafSubjectAsText = "O=leaf,ST=some-state,C=AU"
732736
LeafSubjectKeyID = "30:F4:65:75:14:20:B2:AF:3D:14:71:17:AC:49:90:93:3E:24:A0:1F"

integration_tests/constants/noc_constants.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ zodhpBXZfzhHDvINejK8wzwWgf7Ds8wk3oENlmAj
139139
NocRootCert1CopySubjectKeyID = "44:EB:4C:62:6B:25:48:CD:A2:B3:1C:87:41:5A:08:E7:2B:B9:83:26"
140140
NocRootCert1CopySerialNumber = "460647353168152946606945669687905527879095841977"
141141
NocRootCert1CopySubjectAsText = "CN=NOC-1,OU=Testing Division,O=Example Company,L=Tashkent,ST=Some State,C=UZ"
142+
NocRootCert1CopyIssuer = "MHoxCzAJBgNVBAYTAlVaMRMwEQYDVQQIDApTb21lIFN0YXRlMREwDwYDVQQHDAhUYXNoa2VudDEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMQ4wDAYDVQQDDAVOT0MtMQ=="
142143

143144
NocRootCert2Subject = "MHoxCzAJBgNVBAYTAlVaMRMwEQYDVQQIDApTb21lIFN0YXRlMREwDwYDVQQHDAhUYXNoa2VudDEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMQ4wDAYDVQQDDAVOT0MtMg=="
144145
NocRootCert2SubjectKeyID = "CF:E6:DD:37:2B:4C:B2:B9:A9:F2:75:30:1C:AA:B1:37:1B:11:7F:1B"
@@ -157,21 +158,23 @@ zodhpBXZfzhHDvINejK8wzwWgf7Ds8wk3oENlmAj
157158
NocCert1SerialNumber = "631388393741945881054190991612463928825155142122"
158159
NocCert1SubjectAsText = "CN=NOC-child-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
159160

160-
NocCert1CopySubject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMQ=="
161-
NocCert1CopyIssuer = NocRootCert1Subject
162-
NocCert1CopySubjectKeyID = "02:72:6E:BC:BB:EF:D6:BD:8D:9B:42:AE:D4:3C:C0:55:5F:66:3A:B3"
163-
NocCert1CopySerialNumber = "169445068204646961882009388640343665944683778293"
164-
NocCert1CopySubjectAsText = "CN=NOC-child-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
161+
NocCert1CopySubject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMQ=="
162+
NocCert1CopyIssuer = NocRootCert1Subject
163+
NocCert1CopySubjectKeyID = "02:72:6E:BC:BB:EF:D6:BD:8D:9B:42:AE:D4:3C:C0:55:5F:66:3A:B3"
164+
NocCert1CopySerialNumber = "169445068204646961882009388640343665944683778293"
165+
NocCert1CopyAuthorityKeyID = NocCert1AuthorityKeyID
166+
NocCert1CopySubjectAsText = "CN=NOC-child-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
165167

166168
NocCert2Subject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMg=="
167169
NocCert2Issuer = NocRootCert2Subject
168170
NocCert2SubjectKeyID = "87:48:A2:33:12:1F:51:5C:93:E6:90:40:4A:2C:AB:9E:D6:19:E5:AD"
169171
NocCert2SerialNumber = "361372967010167010646904372658654439710639340814"
170172
NocCert2SubjectAsText = "CN=NOC-child-2,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
171173

172-
NocLeafCert1Subject = "MIGBMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRMwEQYDVQQDDApOT0MtbGVhZi0x"
173-
NocLeafCert1Issuer = NocCert1Subject
174-
NocLeafCert1SubjectKeyID = "77:1F:DB:C4:4C:B1:29:7E:3C:EB:3E:D8:2A:38:0B:63:06:07:00:01"
175-
NocLeafCert1SerialNumber = "281347277961838999749763518155363401757954575313"
176-
NocLeafCert1SubjectAsText = "CN=NOC-leaf-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
174+
NocLeafCert1Subject = "MIGBMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRMwEQYDVQQDDApOT0MtbGVhZi0x"
175+
NocLeafCert1Issuer = NocCert1Subject
176+
NocLeafCert1SubjectKeyID = "77:1F:DB:C4:4C:B1:29:7E:3C:EB:3E:D8:2A:38:0B:63:06:07:00:01"
177+
NocLeafCert1SerialNumber = "281347277961838999749763518155363401757954575313"
178+
NocLeafCert1AuthorityKeyID = NocCert1SubjectKeyID
179+
NocLeafCert1SubjectAsText = "CN=NOC-leaf-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
177180
)

x/pki/keeper/all_certificates.go

+14
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,17 @@ func (k Keeper) verifyCertificate(ctx sdk.Context,
179179
fmt.Sprintf("Certificate verification failed for certificate with subject=%v and subjectKeyID=%v",
180180
x509Certificate.Subject, x509Certificate.SubjectKeyID))
181181
}
182+
183+
// IsAllCertificatePresent Check if the All Certificate is present in the store.
184+
func (k Keeper) IsAllCertificatePresent(
185+
ctx sdk.Context,
186+
subject string,
187+
subjectKeyID string,
188+
) bool {
189+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesKeyPrefix))
190+
191+
return store.Has(types.AllCertificatesKey(
192+
subject,
193+
subjectKeyID,
194+
))
195+
}

x/pki/keeper/all_certificates_by_subject.go

+12
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,15 @@ func (k Keeper) GetAllAllCertificatesBySubject(ctx sdk.Context) (list []types.Al
128128

129129
return
130130
}
131+
132+
// IsCertificatesBySubjectPresent Check if the Certificate By Subject is present in the store.
133+
func (k Keeper) IsCertificatesBySubjectPresent(
134+
ctx sdk.Context,
135+
subject string,
136+
) bool {
137+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyPrefix))
138+
139+
return store.Has(types.AllCertificatesBySubjectKey(
140+
subject,
141+
))
142+
}

x/pki/keeper/all_certificates_by_subject_key_id.go

+12
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,15 @@ func (k Keeper) _removeAllCertificatesFromSubjectKeyIDState(ctx sdk.Context, sub
145145
k.SetAllCertificatesBySubjectKeyID(ctx, certs)
146146
}
147147
}
148+
149+
// IsCertificatesBySubjectKeyIdPresent Check if the Certificate By Subject Key ID is present in the store.
150+
func (k Keeper) IsCertificatesBySubjectKeyIdPresent(
151+
ctx sdk.Context,
152+
subjectKeyID string,
153+
) bool {
154+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix))
155+
156+
return store.Has(types.AllCertificatesBySubjectKeyIDKey(
157+
subjectKeyID,
158+
))
159+
}

x/pki/keeper/approved_certificates.go

+14
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,17 @@ func (k Keeper) AddApprovedCertificate(ctx sdk.Context, approvedCertificate type
128128
approvedCertificates.SubjectKeyId,
129129
), b)
130130
}
131+
132+
// IsApprovedCertificatesPresent Check if the Approved Certificate is present in the store.
133+
func (k Keeper) IsApprovedCertificatesPresent(
134+
ctx sdk.Context,
135+
subject string,
136+
subjectKeyID string,
137+
) bool {
138+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ApprovedCertificatesKeyPrefix))
139+
140+
return store.Has(types.ApprovedCertificatesKey(
141+
subject,
142+
subjectKeyID,
143+
))
144+
}

x/pki/keeper/approved_certificates_by_subject.go

+12
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,15 @@ func (k Keeper) RemoveApprovedCertificateBySubject(ctx sdk.Context, subject stri
103103
k.RemoveApprovedCertificatesBySubject(ctx, subject)
104104
}
105105
}
106+
107+
// IsApprovedCertificatesBySubjectPresent Check if the Approved Certificate By Subject is present in the store.
108+
func (k Keeper) IsApprovedCertificatesBySubjectPresent(
109+
ctx sdk.Context,
110+
subject string,
111+
) bool {
112+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ApprovedCertificatesBySubjectKeyPrefix))
113+
114+
return store.Has(types.ApprovedCertificatesBySubjectKey(
115+
subject,
116+
))
117+
}

x/pki/keeper/approved_certificates_by_subject_key_id.go

+12
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,15 @@ func (k Keeper) _removeApprovedCertificatesFromSubjectKeyIDState(ctx sdk.Context
145145
k.SetApprovedCertificatesBySubjectKeyID(ctx, certs)
146146
}
147147
}
148+
149+
// IsApprovedCertificatesBySubjectKeyIdPresent Check if the Approved Certificate By Subject Key ID is present in the store.
150+
func (k Keeper) IsApprovedCertificatesBySubjectKeyIdPresent(
151+
ctx sdk.Context,
152+
subjectKeyID string,
153+
) bool {
154+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ApprovedCertificatesBySubjectKeyIDKeyPrefix))
155+
156+
return store.Has(types.ApprovedCertificatesBySubjectKeyIDKey(
157+
subjectKeyID,
158+
))
159+
}

x/pki/keeper/approved_root_certificates.go

+12
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,15 @@ func (k Keeper) RemoveApprovedRootCertificate(
8585

8686
k.SetApprovedRootCertificates(ctx, rootCertificates)
8787
}
88+
89+
// IsApprovedRootCertificatePresent Check if the Approved Root Certificate is present in the store.
90+
func (k Keeper) IsApprovedRootCertificatePresent(
91+
ctx sdk.Context,
92+
subjectKeyID string,
93+
) bool {
94+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.ApprovedRootCertificatesKeyPrefix))
95+
96+
return store.Has(types.ApprovedCertificatesBySubjectKeyIDKey(
97+
subjectKeyID,
98+
))
99+
}

x/pki/tests/handler_add_noc_ica_cert_test.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,29 @@ func TestHandler_AddNocIntermediateCert(t *testing.T) {
2424
utils.AddNocRootCertificate(setup, accAddress, testconstants.NocRootCert1)
2525

2626
// add NOC ICA certificate
27+
icaCertificate := utils.CreateTestNocIca1Cert()
2728
utils.AddNocIntermediateCertificate(setup, accAddress, testconstants.NocCert1)
2829

29-
// Check: Noc + All + UniqueCertificate
30-
utils.EnsureNocIntermediateCertificateExist(
31-
t,
32-
setup,
33-
testconstants.NocCert1Subject,
34-
testconstants.NocCert1SubjectKeyID,
35-
testconstants.NocCert1Issuer,
36-
testconstants.NocCert1SerialNumber,
37-
testconstants.Vid,
38-
false,
39-
)
40-
41-
// ChildCertificates: check that child certificates of issuer contains certificate identifier
42-
utils.EnsureChildCertificateExist(
43-
t,
44-
setup,
45-
testconstants.NocRootCert1Subject,
46-
testconstants.NocRootCert1SubjectKeyID,
47-
testconstants.NocCert1Subject,
48-
testconstants.NocCert1SubjectKeyID,
49-
)
30+
// Check indexes
31+
indexes := []utils.TestIndex{
32+
{Key: types.AllCertificatesKeyPrefix, Exist: true},
33+
{Key: types.AllCertificatesBySubjectKeyPrefix, Exist: true},
34+
{Key: types.AllCertificatesBySubjectKeyIDKeyPrefix, Exist: true},
35+
{Key: types.NocCertificatesKeyPrefix, Exist: true},
36+
{Key: types.NocCertificatesBySubjectKeyPrefix, Exist: true},
37+
{Key: types.NocCertificatesBySubjectKeyIDKeyPrefix, Exist: true},
38+
{Key: types.NocCertificatesByVidAndSkidKeyPrefix, Exist: true},
39+
{Key: types.NocRootCertificatesKeyPrefix, Exist: true, Count: 1}, // we create root certificate as well but ica should not get there
40+
{Key: types.NocIcaCertificatesKeyPrefix, Exist: true},
41+
{Key: types.UniqueCertificateKeyPrefix, Exist: true},
42+
{Key: types.ChildCertificatesKeyPrefix, Exist: true},
43+
{Key: types.ProposedCertificateKeyPrefix, Exist: false},
44+
{Key: types.ApprovedCertificatesKeyPrefix, Exist: false},
45+
{Key: types.ApprovedCertificatesBySubjectKeyPrefix, Exist: false},
46+
{Key: types.ApprovedCertificatesBySubjectKeyIDKeyPrefix, Exist: false},
47+
{Key: types.ApprovedRootCertificatesKeyPrefix, Exist: false},
48+
}
49+
utils.CheckCertificateStateIndexes(t, setup, icaCertificate, indexes)
5050
}
5151

5252
// Extra cases

x/pki/tests/handler_add_noc_root_cert_test.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,28 @@ func TestHandler_AddNocRootCert(t *testing.T) {
2020
accAddress := setup.CreateVendorAccount(testconstants.Vid)
2121

2222
// add NOC root certificate
23+
rootCertificate := utils.CreateTestNocRoot1Cert()
2324
utils.AddNocRootCertificate(setup, accAddress, testconstants.NocRootCert1)
2425

25-
// Check: Noc + All + UniqueCertificate
26-
utils.EnsureNocRootCertificateExist(
27-
t,
28-
setup,
29-
testconstants.NocRootCert1Subject,
30-
testconstants.NocRootCert1SubjectKeyID,
31-
testconstants.NocCert1Issuer,
32-
testconstants.NocRootCert1SerialNumber,
33-
testconstants.Vid)
26+
// Check indexes
27+
indexes := []utils.TestIndex{
28+
{Key: types.AllCertificatesKeyPrefix, Exist: true},
29+
{Key: types.AllCertificatesBySubjectKeyPrefix, Exist: true},
30+
{Key: types.AllCertificatesBySubjectKeyIDKeyPrefix, Exist: true},
31+
{Key: types.NocCertificatesKeyPrefix, Exist: true},
32+
{Key: types.NocCertificatesBySubjectKeyPrefix, Exist: true},
33+
{Key: types.NocCertificatesBySubjectKeyIDKeyPrefix, Exist: true},
34+
{Key: types.NocCertificatesByVidAndSkidKeyPrefix, Exist: true},
35+
{Key: types.NocRootCertificatesKeyPrefix, Exist: true},
36+
{Key: types.NocIcaCertificatesKeyPrefix, Exist: false},
37+
{Key: types.UniqueCertificateKeyPrefix, Exist: true},
38+
{Key: types.ProposedCertificateKeyPrefix, Exist: false},
39+
{Key: types.ApprovedCertificatesKeyPrefix, Exist: false},
40+
{Key: types.ApprovedCertificatesBySubjectKeyPrefix, Exist: false},
41+
{Key: types.ApprovedCertificatesBySubjectKeyIDKeyPrefix, Exist: false},
42+
{Key: types.ApprovedRootCertificatesKeyPrefix, Exist: false},
43+
}
44+
utils.CheckCertificateStateIndexes(t, setup, rootCertificate, indexes)
3445
}
3546

3647
// Extra cases
@@ -91,7 +102,7 @@ func TestHandler_AddNocX509RootCert_Renew(t *testing.T) {
91102
require.Equal(t, &newNocCertificate, nocCertificatesBySubjectKeyID[0].Certs[0])
92103

93104
// query noc root certificate by VID
94-
nocRootCertificates, err := utils.QueryNocRootCertificates(setup, testconstants.Vid)
105+
nocRootCertificates, err := utils.QueryNocRootCertificatesByVid(setup, testconstants.Vid)
95106
require.NoError(t, err)
96107
require.Equal(t, len(nocRootCertificates.Certs), 2)
97108
require.Equal(t, &newNocCertificate, nocRootCertificates.Certs[1])

0 commit comments

Comments
 (0)