Skip to content

Commit 28470dd

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

24 files changed

+546
-976
lines changed

x/pki/keeper/all_certificates.go

-14
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,3 @@ 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,15 +128,3 @@ 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/approved_certificates.go

-14
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,3 @@ 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,15 +103,3 @@ 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_root_certificates.go

-12
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,3 @@ 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

+14-44
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ import (
1818
func TestHandler_AddNocIntermediateCert(t *testing.T) {
1919
setup := utils.Setup(t)
2020

21-
accAddress := setup.CreateVendorAccount(testconstants.Vid)
22-
2321
// add NOC root certificate
2422
rootCertificate := utils.CreateTestNocRoot1Cert()
25-
utils.AddNocRootCertificate(setup, accAddress, rootCertificate.PEM)
23+
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate.PEM)
2624

2725
// add NOC ICA certificate
2826
icaCertificate := utils.CreateTestNocIca1Cert()
29-
utils.AddNocIntermediateCertificate(setup, accAddress, icaCertificate.PEM)
27+
utils.AddNocIntermediateCertificate(setup, setup.Vendor1, icaCertificate.PEM)
3028

3129
// Check indexes
3230
indexes := utils.TestIndexes{
@@ -38,7 +36,7 @@ func TestHandler_AddNocIntermediateCert(t *testing.T) {
3836
{Key: types.NocCertificatesBySubjectKeyPrefix},
3937
{Key: types.NocCertificatesBySubjectKeyIDKeyPrefix},
4038
{Key: types.NocCertificatesByVidAndSkidKeyPrefix},
41-
{Key: types.NocRootCertificatesKeyPrefix, Count: 1}, // we create root certificate as well but ica should not be there
39+
{Key: types.NocRootCertificatesKeyPrefix, Count: 1}, // we created root certificate with same vid
4240
{Key: types.NocIcaCertificatesKeyPrefix},
4341
{Key: types.UniqueCertificateKeyPrefix},
4442
{Key: types.ChildCertificatesKeyPrefix},
@@ -59,25 +57,18 @@ func TestHandler_AddNocIntermediateCert(t *testing.T) {
5957
func TestHandler_AddNocIntermediateCert_SameSubjectAndSkid_DifferentSerialNumber(t *testing.T) {
6058
setup := utils.Setup(t)
6159

62-
accAddress := setup.CreateVendorAccount(testconstants.Vid)
63-
6460
// add NOC root certificate
6561
rootCertificate := utils.CreateTestNocRoot1Cert()
66-
utils.AddNocRootCertificate(setup, accAddress, rootCertificate.PEM)
62+
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate.PEM)
6763

68-
// Store the NOC certificate
69-
icaCertificate := utils.CreateTestNocIca1Cert()
70-
intermediateCertificate := utils.NocIntermediateCertificate(accAddress)
64+
// Store the NOC certificate with different serial number
65+
intermediateCertificate := utils.NocIntermediateCertificate(setup.Vendor1)
7166
intermediateCertificate.SerialNumber = testconstants.TestSerialNumber
7267
utils.AddMokedNocCertificate(setup, intermediateCertificate, false)
7368

7469
// add the new NOC certificate
75-
addNocX509Cert := types.NewMsgAddNocX509IcaCert(
76-
accAddress.String(),
77-
icaCertificate.PEM,
78-
testconstants.CertSchemaVersion)
79-
_, err := setup.Handler(setup.Ctx, addNocX509Cert)
80-
require.NoError(t, err)
70+
icaCertificate := utils.CreateTestNocIca1Cert()
71+
utils.AddNocIntermediateCertificate(setup, setup.Vendor1, icaCertificate.PEM)
8172

8273
// query noc certificate by Subject and SKID
8374
indexes := utils.TestIndexes{
@@ -119,15 +110,10 @@ func TestHandler_AddNocX509Cert_SenderNotVendor(t *testing.T) {
119110
func TestHandler_AddNocX509Cert_Root_VID_Does_Not_Equal_To_AccountVID(t *testing.T) {
120111
setup := utils.Setup(t)
121112

122-
accAddress := utils.GenerateAccAddress()
123-
vid := testconstants.Vid
124-
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)
125-
126113
// add NOC root certificate
127-
utils.AddNocRootCertificate(setup, accAddress, testconstants.NocRootCert1)
114+
utils.AddNocRootCertificate(setup, setup.Vendor1, testconstants.NocRootCert1)
128115

129-
newAccAddress := utils.GenerateAccAddress()
130-
setup.AddAccount(newAccAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, 1111)
116+
newAccAddress := setup.CreateVendorAccount(1111)
131117

132118
// try to add NOC certificate
133119
nocX509Cert := types.NewMsgAddNocX509IcaCert(newAccAddress.String(), testconstants.NocCert1, testconstants.CertSchemaVersion)
@@ -138,36 +124,24 @@ func TestHandler_AddNocX509Cert_Root_VID_Does_Not_Equal_To_AccountVID(t *testing
138124
func TestHandler_AddNocX509Cert_ForInvalidCertificate(t *testing.T) {
139125
setup := utils.Setup(t)
140126

141-
accAddress := utils.GenerateAccAddress()
142-
vid := testconstants.Vid
143-
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)
144-
145127
// add x509 certificate
146-
addX509Cert := types.NewMsgAddNocX509IcaCert(accAddress.String(), testconstants.StubCertPem, testconstants.CertSchemaVersion)
128+
addX509Cert := types.NewMsgAddNocX509IcaCert(setup.Vendor1.String(), testconstants.StubCertPem, testconstants.CertSchemaVersion)
147129
_, err := setup.Handler(setup.Ctx, addX509Cert)
148130
require.ErrorIs(t, err, pkitypes.ErrInvalidCertificate)
149131
}
150132

151133
func TestHandler_AddXNoc509Cert_ForNocRootCertificate(t *testing.T) {
152134
setup := utils.Setup(t)
153135

154-
accAddress := utils.GenerateAccAddress()
155-
vid := testconstants.Vid
156-
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)
157-
158136
// try to add root certificate x509 certificate
159-
addX509Cert := types.NewMsgAddX509Cert(accAddress.String(), testconstants.NocRootCert1, testconstants.CertSchemaVersion)
137+
addX509Cert := types.NewMsgAddX509Cert(setup.Vendor1.String(), testconstants.NocRootCert1, testconstants.CertSchemaVersion)
160138
_, err := setup.Handler(setup.Ctx, addX509Cert)
161139
require.ErrorIs(t, err, pkitypes.ErrNonRootCertificateSelfSigned)
162140
}
163141

164142
func TestHandler_AddXNoc509Cert_ForRootNonNocCertificate(t *testing.T) {
165143
setup := utils.Setup(t)
166144

167-
accAddress := utils.GenerateAccAddress()
168-
vid := testconstants.Vid
169-
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)
170-
171145
// store root certificate
172146
rootCertOptions := &utils.RootCertOptions{
173147
PemCert: testconstants.RootCertWithVid,
@@ -179,20 +153,16 @@ func TestHandler_AddXNoc509Cert_ForRootNonNocCertificate(t *testing.T) {
179153
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, rootCertOptions)
180154

181155
// try to add root certificate x509 certificate
182-
addX509Cert := types.NewMsgAddNocX509IcaCert(accAddress.String(), testconstants.IntermediateCertWithVid1, testconstants.CertSchemaVersion)
156+
addX509Cert := types.NewMsgAddNocX509IcaCert(setup.Vendor1.String(), testconstants.IntermediateCertWithVid1, testconstants.CertSchemaVersion)
183157
_, err := setup.Handler(setup.Ctx, addX509Cert)
184158
require.ErrorIs(t, err, pkitypes.ErrInappropriateCertificateType)
185159
}
186160

187161
func TestHandler_AddXNoc509Cert_WhenNocRootCertIsAbsent(t *testing.T) {
188162
setup := utils.Setup(t)
189163

190-
accAddress := utils.GenerateAccAddress()
191-
vid := testconstants.Vid
192-
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)
193-
194164
// add the new NOC certificate
195-
addNocX509Cert := types.NewMsgAddNocX509IcaCert(accAddress.String(), testconstants.NocCert1, testconstants.CertSchemaVersion)
165+
addNocX509Cert := types.NewMsgAddNocX509IcaCert(setup.Vendor1.String(), testconstants.NocCert1, testconstants.CertSchemaVersion)
196166
_, err := setup.Handler(setup.Ctx, addNocX509Cert)
197167

198168
require.ErrorIs(t, err, pkitypes.ErrCertificateDoesNotExist)

x/pki/tests/handler_add_noc_root_cert_test.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ import (
1717
func TestHandler_AddNocRootCert(t *testing.T) {
1818
setup := utils.Setup(t)
1919

20-
accAddress := setup.CreateVendorAccount(testconstants.Vid)
21-
2220
// add NOC root certificate
2321
rootCertificate := utils.CreateTestNocRoot1Cert()
24-
utils.AddNocRootCertificate(setup, accAddress, rootCertificate.PEM)
22+
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate.PEM)
2523

2624
// Check indexes
2725
indexes := utils.TestIndexes{
@@ -53,16 +51,14 @@ func TestHandler_AddNocRootCert(t *testing.T) {
5351
func TestHandler_AddNocRootCert_SameSubjectAndSkid_DifferentSerialNumber(t *testing.T) {
5452
setup := utils.Setup(t)
5553

56-
accAddress := setup.CreateVendorAccount(testconstants.Vid)
57-
5854
// Store the NOC root certificate
5955
rootCertificate1 := utils.CreateTestNocRoot1Cert()
60-
utils.AddNocRootCertificate(setup, accAddress, rootCertificate1.PEM)
56+
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate1.PEM)
6157

6258
// add the new NOC root certificate
6359
rootCertificate2 := utils.CreateTestNocRoot2Cert()
6460
addNocX509RootCert := types.NewMsgAddNocX509RootCert(
65-
accAddress.String(),
61+
setup.Vendor1.String(),
6662
rootCertificate2.PEM,
6763
testconstants.CertSchemaVersion)
6864
_, err := setup.Handler(setup.Ctx, addNocX509RootCert)

0 commit comments

Comments
 (0)