Skip to content

Commit 76cace6

Browse files
committed
Added design for unit test
1 parent 8f1f9f6 commit 76cace6

19 files changed

+807
-174
lines changed

integration_tests/constants/noc_constants.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ BAMCA0kAMEYCIQDzsjB569j1SsltNIP8CMTD4kRsTulqSp+O7JbQdWyzPAIhAODV
129129
zodhpBXZfzhHDvINejK8wzwWgf7Ds8wk3oENlmAj
130130
-----END CERTIFICATE-----`
131131

132+
NocRootCert1Issuer = "MHoxCzAJBgNVBAYTAlVaMRMwEQYDVQQIDApTb21lIFN0YXRlMREwDwYDVQQHDAhUYXNoa2VudDEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMQ4wDAYDVQQDDAVOT0MtMQ=="
132133
NocRootCert1Subject = "MHoxCzAJBgNVBAYTAlVaMRMwEQYDVQQIDApTb21lIFN0YXRlMREwDwYDVQQHDAhUYXNoa2VudDEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMQ4wDAYDVQQDDAVOT0MtMQ=="
133134
NocRootCert1SubjectKeyID = "44:EB:4C:62:6B:25:48:CD:A2:B3:1C:87:41:5A:08:E7:2B:B9:83:26"
134135
NocRootCert1SerialNumber = "47211865327720222621302679792296833381734533449"
@@ -149,11 +150,12 @@ zodhpBXZfzhHDvINejK8wzwWgf7Ds8wk3oENlmAj
149150
NocRootCert3SerialNumber = "38457288443253426021793906708335409501754677187"
150151
NocRootCert3SubjectAsText = "CN=NOC-3,O=Internet Widgits Pty Ltd,ST=Some-State,C=AU"
151152

152-
NocCert1Subject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMQ=="
153-
NocCert1Issuer = NocRootCert1Subject
154-
NocCert1SubjectKeyID = "02:72:6E:BC:BB:EF:D6:BD:8D:9B:42:AE:D4:3C:C0:55:5F:66:3A:B3"
155-
NocCert1SerialNumber = "631388393741945881054190991612463928825155142122"
156-
NocCert1SubjectAsText = "CN=NOC-child-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
153+
NocCert1Subject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMQ=="
154+
NocCert1Issuer = NocRootCert1Subject
155+
NocCert1AuthorityKeyID = NocRootCert1SubjectKeyID
156+
NocCert1SubjectKeyID = "02:72:6E:BC:BB:EF:D6:BD:8D:9B:42:AE:D4:3C:C0:55:5F:66:3A:B3"
157+
NocCert1SerialNumber = "631388393741945881054190991612463928825155142122"
158+
NocCert1SubjectAsText = "CN=NOC-child-1,OU=Testing Division,O=Example Company,L=Some State,ST=Some State,C=UZ"
157159

158160
NocCert1CopySubject = "MIGCMQswCQYDVQQGEwJVWjETMBEGA1UECAwKU29tZSBTdGF0ZTETMBEGA1UEBwwKU29tZSBTdGF0ZTEYMBYGA1UECgwPRXhhbXBsZSBDb21wYW55MRkwFwYDVQQLDBBUZXN0aW5nIERpdmlzaW9uMRQwEgYDVQQDDAtOT0MtY2hpbGQtMQ=="
159161
NocCert1CopyIssuer = NocRootCert1Subject

x/pki/keeper/child_certificates.go

+14
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,17 @@ func (k msgServer) RemoveChildCertificate(
178178
k.RemoveChildCertificates(ctx, issuer, authorityKeyID)
179179
}
180180
}
181+
182+
// IsChildCertificatePresent Check if the Child Certificate is present in the store.
183+
func (k Keeper) IsChildCertificatePresent(
184+
ctx sdk.Context,
185+
issuer string,
186+
authorityKeyID string,
187+
) bool {
188+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ChildCertificatesKeyPrefix))
189+
190+
return store.Has(types.ChildCertificatesKey(
191+
issuer,
192+
authorityKeyID,
193+
))
194+
}

x/pki/keeper/grpc_query_revoked_certificates.go

+14
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,17 @@ func (k Keeper) RevokedCertificates(c context.Context, req *types.QueryGetRevoke
5757

5858
return &types.QueryGetRevokedCertificatesResponse{RevokedCertificates: val}, nil
5959
}
60+
61+
// IsRevokedCertificatePresent Check if the Revoked Certificate is present in the store.
62+
func (k Keeper) IsRevokedCertificatePresent(
63+
ctx sdk.Context,
64+
subject string,
65+
subjectKeyID string,
66+
) bool {
67+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.RevokedCertificatesKeyPrefix))
68+
69+
return store.Has(types.RevokedCertificatesKey(
70+
subject,
71+
subjectKeyID,
72+
))
73+
}

x/pki/keeper/msg_server_remove_noc_x_509_ica_cert.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (k msgServer) RemoveNocX509IcaCert(goCtx context.Context, msg *types.MsgRem
5959
SubjectKeyId: msg.SubjectKeyId,
6060
}
6161

62-
if msg.SerialNumber != "" {
62+
if msg.SerialNumber != "" { //nolint:nestif
6363
certBySerialNumber, found := FindCertificateInList(msg.SerialNumber, &certificates)
6464
if !found {
6565
return nil, pkitypes.NewErrCertificateBySerialNumberDoesNotExist(msg.Subject, msg.SubjectKeyId, msg.SerialNumber)
@@ -80,6 +80,12 @@ func (k msgServer) RemoveNocX509IcaCert(goCtx context.Context, msg *types.MsgRem
8080
certBySerialNumber.Issuer,
8181
false,
8282
)
83+
if len(icaCerts.Certs) == 0 {
84+
k.RemoveChildCertificate(ctx, certBySerialNumber.Issuer, certBySerialNumber.AuthorityKeyId, types.CertificateIdentifier{
85+
Subject: icaCerts.Subject,
86+
SubjectKeyId: icaCerts.SubjectKeyId,
87+
})
88+
}
8389
}
8490

8591
if foundRevoked {
@@ -91,6 +97,8 @@ func (k msgServer) RemoveNocX509IcaCert(goCtx context.Context, msg *types.MsgRem
9197
k.RemoveRevokedNocIcaCertificates(ctx, certID.Subject, certID.SubjectKeyId)
9298
// remove from noc certificates map
9399
k.RemoveNocCertificate(ctx, cert.Subject, cert.SubjectKeyId, accountVid, false)
100+
// Remove certificate identifier from issuer's ChildCertificates record
101+
k.RemoveChildCertificate(ctx, certificates[0].Issuer, certificates[0].AuthorityKeyId, certID)
94102
// remove from subject with serialNumber map
95103
for _, cert := range certificates {
96104
k.RemoveUniqueCertificate(ctx, cert.Issuer, cert.SerialNumber)

x/pki/keeper/msg_server_remove_x_509_cert.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (k msgServer) RemoveX509Cert(goCtx context.Context, msg *types.MsgRemoveX50
4444
return nil, err
4545
}
4646

47-
if msg.SerialNumber != "" {
47+
if msg.SerialNumber != "" { //nolint:nestif
4848
certBySerialNumber, found := FindCertificateInList(msg.SerialNumber, &certificates)
4949
if !found {
5050
return nil, pkitypes.NewErrCertificateBySerialNumberDoesNotExist(msg.Subject, msg.SubjectKeyId, msg.SerialNumber)
@@ -63,16 +63,28 @@ func (k msgServer) RemoveX509Cert(goCtx context.Context, msg *types.MsgRemoveX50
6363
certBySerialNumber.Issuer,
6464
false,
6565
)
66+
if len(aprCerts.Certs) == 0 {
67+
k.RemoveChildCertificate(ctx, certBySerialNumber.Issuer, certBySerialNumber.AuthorityKeyId, types.CertificateIdentifier{
68+
Subject: aprCerts.Subject,
69+
SubjectKeyId: aprCerts.SubjectKeyId,
70+
})
71+
}
6672
}
6773
if foundRevoked {
6874
RemoveCertFromList(certBySerialNumber.Issuer, certBySerialNumber.SerialNumber, &revCerts.Certs)
6975
k.removeOrUpdateRevokedX509Cert(ctx, msg.Subject, msg.SubjectKeyId, &revCerts)
7076
}
7177
} else {
72-
// remove from noc certificates map
78+
certIdentifier := types.CertificateIdentifier{
79+
Subject: msg.Subject,
80+
SubjectKeyId: msg.SubjectKeyId,
81+
}
82+
// remove from da certificates map
7383
k.RemoveDaCertificate(ctx, msg.Subject, msg.SubjectKeyId, false)
7484
// remove from revoked list
7585
k.RemoveRevokedCertificates(ctx, msg.Subject, msg.SubjectKeyId)
86+
// Remove certificate identifier from issuer's ChildCertificates record
87+
k.RemoveChildCertificate(ctx, certificates[0].Issuer, certificates[0].AuthorityKeyId, certIdentifier)
7688
// remove from subject with serialNumber map
7789
for _, cert := range certificates {
7890
k.RemoveUniqueCertificate(ctx, cert.Issuer, cert.SerialNumber)

x/pki/keeper/proposed_certificate.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ func (k Keeper) GetAllProposedCertificate(ctx sdk.Context) (list []types.Propose
6767
return
6868
}
6969

70-
// Check if the Proposed Certificate record associated with a
71-
// Subject/SubjectKeyID combination is present in the store.
70+
// IsProposedCertificatePresent Check if the Proposed Certificate record associated with a Subject/SubjectKeyID combination is present in the store.
7271
func (k Keeper) IsProposedCertificatePresent(
7372
ctx sdk.Context,
7473
subject string,

x/pki/keeper/revoked_noc_ica_certificates.go

+14
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,17 @@ func (k Keeper) GetAllRevokedNocIcaCertificates(ctx sdk.Context) (list []types.R
9797

9898
return
9999
}
100+
101+
// IsRevokedNocIcaCertificatePresent Check if the Revoked Noc ICA Certificate record associated with a Subject/SubjectKeyID combination is present in the store.
102+
func (k Keeper) IsRevokedNocIcaCertificatePresent(
103+
ctx sdk.Context,
104+
subject string,
105+
subjectKeyID string,
106+
) bool {
107+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.RevokedNocIcaCertificatesKeyPrefix))
108+
109+
return store.Has(types.RevokedNocIcaCertificatesKey(
110+
subject,
111+
subjectKeyID,
112+
))
113+
}

x/pki/keeper/revoked_noc_root_certificates.go

+14
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,17 @@ func (k Keeper) GetAllRevokedNocRootCertificates(ctx sdk.Context) (list []types.
9797

9898
return
9999
}
100+
101+
// IsRevokedNocRootCertificatePresent Check if the Revoked Noc Root Certificate record associated with a Subject/SubjectKeyID combination is present in the store.
102+
func (k Keeper) IsRevokedNocRootCertificatePresent(
103+
ctx sdk.Context,
104+
subject string,
105+
subjectKeyID string,
106+
) bool {
107+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.RevokedNocRootCertificatesKeyPrefix))
108+
109+
return store.Has(types.RevokedNocRootCertificatesKey(
110+
subject,
111+
subjectKeyID,
112+
))
113+
}

x/pki/tests/handler_add_noc_ica_cert_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestHandler_AddNocX509Cert_AddNewIca(t *testing.T) {
2727
// add NOC ICA certificate
2828
addNocIcaCertificate(setup, accAddress, testconstants.NocCert1)
2929

30+
// Check: Noc + All + UniqueCertificate
3031
ensureNocIcaCertificateExist(
3132
t,
3233
setup,

x/pki/tests/handler_add_noc_root_cert_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestHandler_AddNocX509Cert_AddNewRoot(t *testing.T) {
2323
// add NOC root certificate
2424
addNocRootCertificate(setup, accAddress, testconstants.NocRootCert1)
2525

26+
// Check: Noc + All + UniqueCertificate
2627
ensureNocRootCertificateExist(
2728
t,
2829
setup,

x/pki/tests/handler_remove_noc_ica_cert_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,63 @@ import (
1515

1616
// Main
1717

18+
func TestHandler_RemoveNocX509IcaCert(t *testing.T) {
19+
setup := Setup(t)
20+
21+
// Add vendor account
22+
vid := testconstants.Vid
23+
vendorAccAddress := GenerateAccAddress()
24+
setup.AddAccount(vendorAccAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)
25+
26+
// add NOC root certificate
27+
addNocRootCertificate(setup, vendorAccAddress, testconstants.NocRootCert1)
28+
29+
// add intermediate certificate
30+
addNocIcaCertificate(setup, vendorAccAddress, testconstants.NocCert1)
31+
32+
// remove intermediate certificate
33+
removeIcaCert := types.NewMsgRemoveNocX509IcaCert(
34+
vendorAccAddress.String(),
35+
testconstants.NocCert1Subject,
36+
testconstants.NocCert1SubjectKeyID,
37+
"",
38+
)
39+
_, err := setup.Handler(setup.Ctx, removeIcaCert)
40+
require.NoError(t, err)
41+
42+
// Check: Noc - missing
43+
ensureCertificateNotPresentInNocCertificateIndexes(
44+
t,
45+
setup,
46+
testconstants.NocCert1Subject,
47+
testconstants.NocCert1SubjectKeyID,
48+
testconstants.Vid,
49+
false,
50+
false,
51+
)
52+
53+
// Check: All - missing
54+
ensureCertificateNotPresentInGlobalCertificateIndexes(
55+
t,
56+
setup,
57+
testconstants.NocCert1Subject,
58+
testconstants.NocCert1SubjectKeyID,
59+
false,
60+
)
61+
62+
// Check: UniqueCertificate - missing
63+
found := setup.Keeper.IsUniqueCertificatePresent(setup.Ctx, testconstants.NocCert1Issuer, testconstants.NocCert1SerialNumber)
64+
require.False(t, found)
65+
66+
// Check: RevokedCertificates (ica) - missing
67+
found = setup.Keeper.IsRevokedNocIcaCertificatePresent(setup.Ctx, testconstants.NocCert1Subject, testconstants.NocCert1SubjectKeyID)
68+
require.False(t, found)
69+
70+
// Check: child certificate - missing
71+
found = setup.Keeper.IsChildCertificatePresent(setup.Ctx, testconstants.NocCert1Issuer, testconstants.NocCert1AuthorityKeyID)
72+
require.False(t, found)
73+
}
74+
1875
func TestHandler_RemoveNocX509IcaCert_BySubjectAndSKID(t *testing.T) {
1976
setup := Setup(t)
2077

x/pki/tests/handler_remove_noc_root_cert_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,56 @@ import (
1515

1616
// Main
1717

18+
func TestHandler_RemoveNocX509RootCert(t *testing.T) {
19+
setup := Setup(t)
20+
21+
// Add vendor account
22+
vid := testconstants.Vid
23+
vendorAccAddress := GenerateAccAddress()
24+
setup.AddAccount(vendorAccAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)
25+
26+
// add NOC root certificates
27+
addNocRootCertificate(setup, vendorAccAddress, testconstants.NocRootCert1)
28+
29+
// remove noc root certificate
30+
removeIcaCert := types.NewMsgRemoveNocX509RootCert(
31+
vendorAccAddress.String(),
32+
testconstants.NocRootCert1Subject,
33+
testconstants.NocRootCert1SubjectKeyID,
34+
"",
35+
)
36+
_, err := setup.Handler(setup.Ctx, removeIcaCert)
37+
require.NoError(t, err)
38+
39+
// Check: Noc - missing
40+
ensureCertificateNotPresentInNocCertificateIndexes(
41+
t,
42+
setup,
43+
testconstants.NocRootCert1Subject,
44+
testconstants.NocRootCert1SubjectKeyID,
45+
testconstants.Vid,
46+
true,
47+
false,
48+
)
49+
50+
// Check: All - missing
51+
ensureCertificateNotPresentInGlobalCertificateIndexes(
52+
t,
53+
setup,
54+
testconstants.NocRootCert1Subject,
55+
testconstants.NocRootCert1SubjectKeyID,
56+
false,
57+
)
58+
59+
// Check: UniqueCertificate - missing
60+
found := setup.Keeper.IsUniqueCertificatePresent(setup.Ctx, testconstants.NocRootCert1Issuer, testconstants.NocRootCert1SerialNumber)
61+
require.False(t, found)
62+
63+
// Check: RevokedCertificates (root) - missing
64+
found = setup.Keeper.IsRevokedNocRootCertificatePresent(setup.Ctx, testconstants.NocRootCert1Subject, testconstants.NocRootCert1SubjectKeyID)
65+
require.False(t, found)
66+
}
67+
1868
func TestHandler_RemoveNocX509RootCert_BySubjectAndSKID(t *testing.T) {
1969
setup := Setup(t)
2070

0 commit comments

Comments
 (0)