Skip to content

Commit 7b61f2f

Browse files
committed
Refactoring PKI unit tests
1 parent 42b837e commit 7b61f2f

29 files changed

+4120
-3170
lines changed

types/pki/keys.go

-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ func KeyPrefix(p string) []byte {
2121
return []byte(p)
2222
}
2323

24-
const (
25-
ApprovedRootCertificatesKeyPrefix = "ApprovedRootCertificates/value/"
26-
RevokedRootCertificatesKeyPrefix = "RevokedRootCertificates/value/"
27-
)
28-
2924
var (
3025
ApprovedRootCertificatesKey = []byte{0}
3126
RevokedRootCertificatesKey = []byte{0}

x/pki/client/cli/query_approved_root_certificates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func CmdShowApprovedRootCertificates() *cobra.Command {
2424
return cli.QueryWithProofList(
2525
clientCtx,
2626
pkitypes.StoreKey,
27-
pkitypes.ApprovedRootCertificatesKeyPrefix,
27+
types.ApprovedRootCertificatesKeyPrefix,
2828
pkitypes.ApprovedRootCertificatesKey,
2929
&res,
3030
)

x/pki/client/cli/query_revoked_root_certificates.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func CmdShowRevokedRootCertificates() *cobra.Command {
2424
return cli.QueryWithProofList(
2525
clientCtx,
2626
pkitypes.StoreKey,
27-
pkitypes.RevokedRootCertificatesKeyPrefix,
27+
types.RevokedRootCertificatesKeyPrefix,
2828
pkitypes.RevokedRootCertificatesKey,
2929
&res,
3030
)

x/pki/keeper/approved_root_certificates.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99

1010
// SetApprovedRootCertificates set approvedRootCertificates in the store.
1111
func (k Keeper) SetApprovedRootCertificates(ctx sdk.Context, approvedRootCertificates types.ApprovedRootCertificates) {
12-
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.ApprovedRootCertificatesKeyPrefix))
12+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ApprovedRootCertificatesKeyPrefix))
1313
b := k.cdc.MustMarshal(&approvedRootCertificates)
1414
store.Set(pkitypes.ApprovedRootCertificatesKey, b)
1515
}
1616

1717
// GetApprovedRootCertificates returns approvedRootCertificates.
1818
func (k Keeper) GetApprovedRootCertificates(ctx sdk.Context) (val types.ApprovedRootCertificates, found bool) {
19-
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.ApprovedRootCertificatesKeyPrefix))
19+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ApprovedRootCertificatesKeyPrefix))
2020

2121
b := store.Get(pkitypes.ApprovedRootCertificatesKey)
2222
if b == nil {
@@ -30,7 +30,7 @@ func (k Keeper) GetApprovedRootCertificates(ctx sdk.Context) (val types.Approved
3030

3131
// RemoveApprovedRootCertificates removes approvedRootCertificates from the store.
3232
func (k Keeper) RemoveApprovedRootCertificates(ctx sdk.Context) {
33-
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.ApprovedRootCertificatesKeyPrefix))
33+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ApprovedRootCertificatesKeyPrefix))
3434
store.Delete(pkitypes.ApprovedRootCertificatesKey)
3535
}
3636

x/pki/keeper/msg_server_approve_revoke_x_509_root_cert.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func (k msgServer) ApproveRevokeX509RootCert(goCtx context.Context, msg *types.M
1818
if err != nil {
1919
return nil, errors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid Address: (%s)", err)
2020
}
21+
2122
if !k.dclauthKeeper.HasRole(ctx, signerAddr, types.RootCertificateApprovalRole) {
2223
return nil, errors.Wrapf(sdkerrors.ErrUnauthorized,
2324
"MsgApproveRevokeX509RootCert transaction should be signed by "+

x/pki/keeper/msg_server_propose_revoke_x_509_root_cert.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (k msgServer) ProposeRevokeX509RootCert(goCtx context.Context, msg *types.M
1919
if err != nil {
2020
return nil, errors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid Address: (%s)", err)
2121
}
22+
2223
if !k.dclauthKeeper.HasRole(ctx, signerAddr, types.RootCertificateApprovalRole) {
2324
return nil, errors.Wrapf(sdkerrors.ErrUnauthorized,
2425
"MsgProposeRevokeX509RootCert transaction should be signed by "+

x/pki/keeper/revoked_root_certificates.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99

1010
// SetRevokedRootCertificates set revokedRootCertificates in the store.
1111
func (k Keeper) SetRevokedRootCertificates(ctx sdk.Context, revokedRootCertificates types.RevokedRootCertificates) {
12-
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.RevokedRootCertificatesKeyPrefix))
12+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.RevokedRootCertificatesKeyPrefix))
1313
b := k.cdc.MustMarshal(&revokedRootCertificates)
1414
store.Set(pkitypes.RevokedRootCertificatesKey, b)
1515
}
1616

1717
// GetRevokedRootCertificates returns revokedRootCertificates.
1818
func (k Keeper) GetRevokedRootCertificates(ctx sdk.Context) (val types.RevokedRootCertificates, found bool) {
19-
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.RevokedRootCertificatesKeyPrefix))
19+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.RevokedRootCertificatesKeyPrefix))
2020

2121
b := store.Get(pkitypes.RevokedRootCertificatesKey)
2222
if b == nil {
@@ -30,7 +30,7 @@ func (k Keeper) GetRevokedRootCertificates(ctx sdk.Context) (val types.RevokedRo
3030

3131
// RemoveRevokedRootCertificates removes revokedRootCertificates from the store.
3232
func (k Keeper) RemoveRevokedRootCertificates(ctx sdk.Context) {
33-
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.RevokedRootCertificatesKeyPrefix))
33+
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.RevokedRootCertificatesKeyPrefix))
3434
store.Delete(pkitypes.RevokedRootCertificatesKey)
3535
}
3636

x/pki/tests/handler_add_noc_ica_cert_test.go

+57-10
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ func TestHandler_AddNocIntermediateCert(t *testing.T) {
5252
utils.CheckCertificateStateIndexes(t, setup, icaCertificate, indexes)
5353
}
5454

55-
// Extra cases
56-
5755
func TestHandler_AddNocIntermediateCert_SameSubjectAndSkid_DifferentSerialNumber(t *testing.T) {
5856
setup := utils.Setup(t)
5957

@@ -96,18 +94,68 @@ func TestHandler_AddNocIntermediateCert_SameSubjectAndSkid_DifferentSerialNumber
9694
utils.CheckCertificateStateIndexes(t, setup, icaCertificate, indexes)
9795
}
9896

97+
func TestHandler_AddNocIntermediateCert_ByNotOwnerButSameVendor(t *testing.T) {
98+
setup := utils.Setup(t)
99+
100+
// add two vendors with the same VID
101+
vendorAccAddress1 := setup.CreateVendorAccount(testconstants.Vid)
102+
vendorAccAddress2 := setup.CreateVendorAccount(testconstants.Vid)
103+
104+
// add NOC root certificate
105+
rootCertificate := utils.RootNocCertificate1(vendorAccAddress1)
106+
utils.AddNocRootCertificate(setup, rootCertificate)
107+
108+
// add the new NOC certificate by first vendor
109+
icaCertificate := utils.IntermediateNocCertificate1(vendorAccAddress1)
110+
utils.AddNocIntermediateCertificate(setup, icaCertificate)
111+
112+
// add the new NOC certificate by second vendor
113+
icaCertificate2 := utils.IntermediateNocCertificate1Copy(vendorAccAddress2)
114+
utils.AddNocIntermediateCertificate(setup, icaCertificate2)
115+
116+
// Check state indexes
117+
indexes := utils.TestIndexes{
118+
Present: []utils.TestIndex{
119+
{Key: types.AllCertificatesKeyPrefix, Count: 2},
120+
{Key: types.AllCertificatesBySubjectKeyPrefix},
121+
{Key: types.AllCertificatesBySubjectKeyIDKeyPrefix, Count: 2},
122+
{Key: types.NocCertificatesKeyPrefix, Count: 2},
123+
{Key: types.NocCertificatesBySubjectKeyPrefix},
124+
{Key: types.NocCertificatesBySubjectKeyIDKeyPrefix, Count: 2},
125+
{Key: types.NocCertificatesByVidAndSkidKeyPrefix, Count: 2},
126+
{Key: types.NocRootCertificatesKeyPrefix, Count: 1}, // we create root certificate as well but ica should not be there
127+
{Key: types.NocIcaCertificatesKeyPrefix, Count: 2},
128+
{Key: types.UniqueCertificateKeyPrefix},
129+
{Key: types.ChildCertificatesKeyPrefix},
130+
},
131+
Missing: []utils.TestIndex{
132+
{Key: types.ProposedCertificateKeyPrefix},
133+
{Key: types.ApprovedCertificatesKeyPrefix},
134+
{Key: types.ApprovedCertificatesBySubjectKeyPrefix},
135+
{Key: types.ApprovedCertificatesBySubjectKeyIDKeyPrefix},
136+
{Key: types.ApprovedRootCertificatesKeyPrefix},
137+
},
138+
}
139+
utils.CheckCertificateStateIndexes(t, setup, icaCertificate, indexes)
140+
utils.CheckCertificateStateIndexes(t, setup, icaCertificate2, indexes)
141+
}
142+
99143
// Error cases
100144

101-
func TestHandler_AddNocX509Cert_SenderNotVendor(t *testing.T) {
145+
func TestHandler_AddNocIntermediateCert_SenderNotVendor(t *testing.T) {
102146
setup := utils.Setup(t)
103147

148+
// add NOC root certificate
149+
rootCertificate := utils.RootNocCertificate1(setup.Vendor1)
150+
utils.AddNocRootCertificate(setup, rootCertificate)
151+
104152
addNocX509Cert := types.NewMsgAddNocX509IcaCert(setup.Trustee1.String(), testconstants.NocCert1, testconstants.CertSchemaVersion)
105153
_, err := setup.Handler(setup.Ctx, addNocX509Cert)
106154

107155
require.ErrorIs(t, err, sdkerrors.ErrUnauthorized)
108156
}
109157

110-
func TestHandler_AddNocX509Cert_Root_VID_Does_Not_Equal_To_AccountVID(t *testing.T) {
158+
func TestHandler_AddNocIntermediateCert_Root_VID_Does_Not_Equal_To_AccountVID(t *testing.T) {
111159
setup := utils.Setup(t)
112160

113161
// add NOC root certificate
@@ -122,7 +170,7 @@ func TestHandler_AddNocX509Cert_Root_VID_Does_Not_Equal_To_AccountVID(t *testing
122170
require.ErrorIs(t, err, pkitypes.ErrCertVidNotEqualAccountVid)
123171
}
124172

125-
func TestHandler_AddNocX509Cert_ForInvalidCertificate(t *testing.T) {
173+
func TestHandler_AddNocIntermediateCert_ForInvalidCertificate(t *testing.T) {
126174
setup := utils.Setup(t)
127175

128176
// add x509 certificate
@@ -131,7 +179,7 @@ func TestHandler_AddNocX509Cert_ForInvalidCertificate(t *testing.T) {
131179
require.ErrorIs(t, err, pkitypes.ErrInvalidCertificate)
132180
}
133181

134-
func TestHandler_AddXNoc509Cert_ForNocRootCertificate(t *testing.T) {
182+
func TestHandler_AddNocIntermediateCert_ForNocRootCertificate(t *testing.T) {
135183
setup := utils.Setup(t)
136184

137185
// try to add root certificate x509 certificate
@@ -140,11 +188,10 @@ func TestHandler_AddXNoc509Cert_ForNocRootCertificate(t *testing.T) {
140188
require.ErrorIs(t, err, pkitypes.ErrNonRootCertificateSelfSigned)
141189
}
142190

143-
func TestHandler_AddXNoc509Cert_ForRootNonNocCertificate(t *testing.T) {
191+
func TestHandler_AddNocIntermediateCert_ForRootNonNocCertificate(t *testing.T) {
144192
setup := utils.Setup(t)
145193

146194
// store root certificate
147-
148195
rootCert := utils.RootDaCertificateWithVid(setup.Trustee1)
149196
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, rootCert)
150197

@@ -154,7 +201,7 @@ func TestHandler_AddXNoc509Cert_ForRootNonNocCertificate(t *testing.T) {
154201
require.ErrorIs(t, err, pkitypes.ErrInappropriateCertificateType)
155202
}
156203

157-
func TestHandler_AddXNoc509Cert_WhenNocRootCertIsAbsent(t *testing.T) {
204+
func TestHandler_AddNocIntermediateCert_WhenNocRootCertIsAbsent(t *testing.T) {
158205
setup := utils.Setup(t)
159206

160207
// add the new NOC certificate
@@ -164,7 +211,7 @@ func TestHandler_AddXNoc509Cert_WhenNocRootCertIsAbsent(t *testing.T) {
164211
require.ErrorIs(t, err, pkitypes.ErrCertificateDoesNotExist)
165212
}
166213

167-
func TestHandler_AddNocX509Cert_CertificateExist(t *testing.T) {
214+
func TestHandler_AddNocIntermediateCert_CertificateExist(t *testing.T) {
168215
accAddress := utils.GenerateAccAddress()
169216

170217
cases := []struct {

x/pki/tests/handler_add_noc_root_cert_test.go

+47-6
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ func TestHandler_AddNocRootCert(t *testing.T) {
4646
utils.CheckCertificateStateIndexes(t, setup, rootCertificate, indexes)
4747
}
4848

49-
// Extra cases
50-
5149
func TestHandler_AddNocRootCert_SameSubjectAndSkid_DifferentSerialNumber(t *testing.T) {
5250
setup := utils.Setup(t)
5351

@@ -84,19 +82,62 @@ func TestHandler_AddNocRootCert_SameSubjectAndSkid_DifferentSerialNumber(t *test
8482
utils.CheckCertificateStateIndexes(t, setup, rootCertificate2, indexes)
8583
}
8684

85+
func TestHandler_AddNocRootCert_ByNotOwnerButSameVendor(t *testing.T) {
86+
setup := utils.Setup(t)
87+
88+
// add two vendors with the same VID
89+
vendorAccAddress1 := setup.CreateVendorAccount(testconstants.Vid)
90+
vendorAccAddress2 := setup.CreateVendorAccount(testconstants.Vid)
91+
92+
// add NOC root certificate
93+
rootCertificate1 := utils.RootNocCertificate1(vendorAccAddress1)
94+
utils.AddNocRootCertificate(setup, rootCertificate1)
95+
96+
// add NOC root certificate
97+
rootCertificate2 := utils.RootNocCertificate1Copy(vendorAccAddress2)
98+
utils.AddNocRootCertificate(setup, rootCertificate2)
99+
100+
// Check state indexes
101+
indexes := utils.TestIndexes{
102+
Present: []utils.TestIndex{
103+
{Key: types.AllCertificatesKeyPrefix, Count: 2},
104+
{Key: types.AllCertificatesBySubjectKeyPrefix},
105+
{Key: types.AllCertificatesBySubjectKeyIDKeyPrefix, Count: 2},
106+
{Key: types.NocCertificatesKeyPrefix, Count: 2},
107+
{Key: types.NocCertificatesBySubjectKeyPrefix},
108+
{Key: types.NocCertificatesBySubjectKeyIDKeyPrefix, Count: 2},
109+
{Key: types.NocRootCertificatesKeyPrefix, Count: 2},
110+
{Key: types.UniqueCertificateKeyPrefix},
111+
},
112+
Missing: []utils.TestIndex{
113+
{Key: types.NocIcaCertificatesKeyPrefix},
114+
{Key: types.ProposedCertificateKeyPrefix},
115+
{Key: types.ApprovedCertificatesKeyPrefix},
116+
{Key: types.ApprovedCertificatesBySubjectKeyPrefix},
117+
{Key: types.ApprovedCertificatesBySubjectKeyIDKeyPrefix},
118+
{Key: types.ApprovedRootCertificatesKeyPrefix},
119+
},
120+
}
121+
utils.CheckCertificateStateIndexes(t, setup, rootCertificate1, indexes)
122+
utils.CheckCertificateStateIndexes(t, setup, rootCertificate2, indexes)
123+
}
124+
87125
// Error cases
88126

89-
func TestHandler_AddNocX509RootCert_SenderNotVendor(t *testing.T) {
127+
func TestHandler_AddNocRootCert_SenderNotVendor(t *testing.T) {
90128
setup := utils.Setup(t)
91129

92-
addNocX509RootCert := types.NewMsgAddNocX509RootCert(setup.Trustee1.String(), testconstants.RootCertPem, testconstants.CertSchemaVersion)
130+
addNocX509RootCert := types.NewMsgAddNocX509RootCert(
131+
setup.Trustee1.String(),
132+
testconstants.RootCertPem,
133+
testconstants.CertSchemaVersion)
93134
_, err := setup.Handler(setup.Ctx, addNocX509RootCert)
94135

95136
require.Error(t, err)
96137
require.True(t, sdkerrors.ErrUnauthorized.Is(err))
97138
}
98139

99-
func TestHandler_AddNocX509RootCert_InvalidCertificate(t *testing.T) {
140+
func TestHandler_AddNocRootCert_InvalidCertificate(t *testing.T) {
100141
accAddress := utils.GenerateAccAddress()
101142

102143
cases := []struct {
@@ -141,7 +182,7 @@ func TestHandler_AddNocX509RootCert_InvalidCertificate(t *testing.T) {
141182
}
142183
}
143184

144-
func TestHandler_AddNocX509RootCert_CertificateExist(t *testing.T) {
185+
func TestHandler_AddNocRootCert_CertificateExist(t *testing.T) {
145186
accAddress := utils.GenerateAccAddress()
146187

147188
cases := []struct {

0 commit comments

Comments
 (0)