Skip to content

Commit

Permalink
Refactoring PKI unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Artemkaaas committed Dec 2, 2024
1 parent 6feaeda commit 310d5c3
Show file tree
Hide file tree
Showing 24 changed files with 546 additions and 976 deletions.
14 changes: 0 additions & 14 deletions x/pki/keeper/all_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,3 @@ func (k Keeper) verifyCertificate(ctx sdk.Context,
fmt.Sprintf("Certificate verification failed for certificate with subject=%v and subjectKeyID=%v",
x509Certificate.Subject, x509Certificate.SubjectKeyID))
}

// IsAllCertificatePresent Check if the All Certificate is present in the store.
func (k Keeper) IsAllCertificatePresent(
ctx sdk.Context,
subject string,
subjectKeyID string,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesKeyPrefix))

return store.Has(types.AllCertificatesKey(
subject,
subjectKeyID,
))
}
12 changes: 0 additions & 12 deletions x/pki/keeper/all_certificates_by_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,3 @@ func (k Keeper) GetAllAllCertificatesBySubject(ctx sdk.Context) (list []types.Al

return
}

// IsCertificatesBySubjectPresent Check if the Certificate By Subject is present in the store.
func (k Keeper) IsCertificatesBySubjectPresent(
ctx sdk.Context,
subject string,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyPrefix))

return store.Has(types.AllCertificatesBySubjectKey(
subject,
))
}
14 changes: 0 additions & 14 deletions x/pki/keeper/approved_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,3 @@ func (k Keeper) AddApprovedCertificate(ctx sdk.Context, approvedCertificate type
approvedCertificates.SubjectKeyId,
), b)
}

// IsApprovedCertificatesPresent Check if the Approved Certificate is present in the store.
func (k Keeper) IsApprovedCertificatesPresent(
ctx sdk.Context,
subject string,
subjectKeyID string,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ApprovedCertificatesKeyPrefix))

return store.Has(types.ApprovedCertificatesKey(
subject,
subjectKeyID,
))
}
12 changes: 0 additions & 12 deletions x/pki/keeper/approved_certificates_by_subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,3 @@ func (k Keeper) RemoveApprovedCertificateBySubject(ctx sdk.Context, subject stri
k.RemoveApprovedCertificatesBySubject(ctx, subject)
}
}

// IsApprovedCertificatesBySubjectPresent Check if the Approved Certificate By Subject is present in the store.
func (k Keeper) IsApprovedCertificatesBySubjectPresent(
ctx sdk.Context,
subject string,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.ApprovedCertificatesBySubjectKeyPrefix))

return store.Has(types.ApprovedCertificatesBySubjectKey(
subject,
))
}
12 changes: 0 additions & 12 deletions x/pki/keeper/approved_root_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,3 @@ func (k Keeper) RemoveApprovedRootCertificate(

k.SetApprovedRootCertificates(ctx, rootCertificates)
}

// IsApprovedRootCertificatePresent Check if the Approved Root Certificate is present in the store.
func (k Keeper) IsApprovedRootCertificatePresent(
ctx sdk.Context,
subjectKeyID string,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.ApprovedRootCertificatesKeyPrefix))

return store.Has(types.ApprovedCertificatesBySubjectKeyIDKey(
subjectKeyID,
))
}
58 changes: 14 additions & 44 deletions x/pki/tests/handler_add_noc_ica_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ import (
func TestHandler_AddNocIntermediateCert(t *testing.T) {
setup := utils.Setup(t)

accAddress := setup.CreateVendorAccount(testconstants.Vid)

// add NOC root certificate
rootCertificate := utils.CreateTestNocRoot1Cert()
utils.AddNocRootCertificate(setup, accAddress, rootCertificate.PEM)
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate.PEM)

// add NOC ICA certificate
icaCertificate := utils.CreateTestNocIca1Cert()
utils.AddNocIntermediateCertificate(setup, accAddress, icaCertificate.PEM)
utils.AddNocIntermediateCertificate(setup, setup.Vendor1, icaCertificate.PEM)

// Check indexes
indexes := utils.TestIndexes{
Expand All @@ -38,7 +36,7 @@ func TestHandler_AddNocIntermediateCert(t *testing.T) {
{Key: types.NocCertificatesBySubjectKeyPrefix},
{Key: types.NocCertificatesBySubjectKeyIDKeyPrefix},
{Key: types.NocCertificatesByVidAndSkidKeyPrefix},
{Key: types.NocRootCertificatesKeyPrefix, Count: 1}, // we create root certificate as well but ica should not be there
{Key: types.NocRootCertificatesKeyPrefix, Count: 1}, // we created root certificate with same vid
{Key: types.NocIcaCertificatesKeyPrefix},
{Key: types.UniqueCertificateKeyPrefix},
{Key: types.ChildCertificatesKeyPrefix},
Expand All @@ -59,25 +57,18 @@ func TestHandler_AddNocIntermediateCert(t *testing.T) {
func TestHandler_AddNocIntermediateCert_SameSubjectAndSkid_DifferentSerialNumber(t *testing.T) {
setup := utils.Setup(t)

accAddress := setup.CreateVendorAccount(testconstants.Vid)

// add NOC root certificate
rootCertificate := utils.CreateTestNocRoot1Cert()
utils.AddNocRootCertificate(setup, accAddress, rootCertificate.PEM)
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate.PEM)

// Store the NOC certificate
icaCertificate := utils.CreateTestNocIca1Cert()
intermediateCertificate := utils.NocIntermediateCertificate(accAddress)
// Store the NOC certificate with different serial number
intermediateCertificate := utils.NocIntermediateCertificate(setup.Vendor1)
intermediateCertificate.SerialNumber = testconstants.TestSerialNumber
utils.AddMokedNocCertificate(setup, intermediateCertificate, false)

// add the new NOC certificate
addNocX509Cert := types.NewMsgAddNocX509IcaCert(
accAddress.String(),
icaCertificate.PEM,
testconstants.CertSchemaVersion)
_, err := setup.Handler(setup.Ctx, addNocX509Cert)
require.NoError(t, err)
icaCertificate := utils.CreateTestNocIca1Cert()
utils.AddNocRootCertificate(setup, setup.Vendor1, icaCertificate.PEM)

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

accAddress := utils.GenerateAccAddress()
vid := testconstants.Vid
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)

// add NOC root certificate
utils.AddNocRootCertificate(setup, accAddress, testconstants.NocRootCert1)
utils.AddNocRootCertificate(setup, setup.Vendor1, testconstants.NocRootCert1)

newAccAddress := utils.GenerateAccAddress()
setup.AddAccount(newAccAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, 1111)
newAccAddress := setup.CreateVendorAccount(1111)

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

accAddress := utils.GenerateAccAddress()
vid := testconstants.Vid
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)

// add x509 certificate
addX509Cert := types.NewMsgAddNocX509IcaCert(accAddress.String(), testconstants.StubCertPem, testconstants.CertSchemaVersion)
addX509Cert := types.NewMsgAddNocX509IcaCert(setup.Vendor1.String(), testconstants.StubCertPem, testconstants.CertSchemaVersion)
_, err := setup.Handler(setup.Ctx, addX509Cert)
require.ErrorIs(t, err, pkitypes.ErrInvalidCertificate)
}

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

accAddress := utils.GenerateAccAddress()
vid := testconstants.Vid
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)

// try to add root certificate x509 certificate
addX509Cert := types.NewMsgAddX509Cert(accAddress.String(), testconstants.NocRootCert1, testconstants.CertSchemaVersion)
addX509Cert := types.NewMsgAddX509Cert(setup.Vendor1.String(), testconstants.NocRootCert1, testconstants.CertSchemaVersion)
_, err := setup.Handler(setup.Ctx, addX509Cert)
require.ErrorIs(t, err, pkitypes.ErrNonRootCertificateSelfSigned)
}

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

accAddress := utils.GenerateAccAddress()
vid := testconstants.Vid
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)

// store root certificate
rootCertOptions := &utils.RootCertOptions{
PemCert: testconstants.RootCertWithVid,
Expand All @@ -179,20 +153,16 @@ func TestHandler_AddXNoc509Cert_ForRootNonNocCertificate(t *testing.T) {
utils.ProposeAndApproveRootCertificate(setup, setup.Trustee1, rootCertOptions)

// try to add root certificate x509 certificate
addX509Cert := types.NewMsgAddNocX509IcaCert(accAddress.String(), testconstants.IntermediateCertWithVid1, testconstants.CertSchemaVersion)
addX509Cert := types.NewMsgAddNocX509IcaCert(setup.Vendor1.String(), testconstants.IntermediateCertWithVid1, testconstants.CertSchemaVersion)
_, err := setup.Handler(setup.Ctx, addX509Cert)
require.ErrorIs(t, err, pkitypes.ErrInappropriateCertificateType)
}

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

accAddress := utils.GenerateAccAddress()
vid := testconstants.Vid
setup.AddAccount(accAddress, []dclauthtypes.AccountRole{dclauthtypes.Vendor}, vid)

// add the new NOC certificate
addNocX509Cert := types.NewMsgAddNocX509IcaCert(accAddress.String(), testconstants.NocCert1, testconstants.CertSchemaVersion)
addNocX509Cert := types.NewMsgAddNocX509IcaCert(setup.Vendor1.String(), testconstants.NocCert1, testconstants.CertSchemaVersion)
_, err := setup.Handler(setup.Ctx, addNocX509Cert)

require.ErrorIs(t, err, pkitypes.ErrCertificateDoesNotExist)
Expand Down
10 changes: 3 additions & 7 deletions x/pki/tests/handler_add_noc_root_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ import (
func TestHandler_AddNocRootCert(t *testing.T) {
setup := utils.Setup(t)

accAddress := setup.CreateVendorAccount(testconstants.Vid)

// add NOC root certificate
rootCertificate := utils.CreateTestNocRoot1Cert()
utils.AddNocRootCertificate(setup, accAddress, rootCertificate.PEM)
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate.PEM)

// Check indexes
indexes := utils.TestIndexes{
Expand Down Expand Up @@ -53,16 +51,14 @@ func TestHandler_AddNocRootCert(t *testing.T) {
func TestHandler_AddNocRootCert_SameSubjectAndSkid_DifferentSerialNumber(t *testing.T) {
setup := utils.Setup(t)

accAddress := setup.CreateVendorAccount(testconstants.Vid)

// Store the NOC root certificate
rootCertificate1 := utils.CreateTestNocRoot1Cert()
utils.AddNocRootCertificate(setup, accAddress, rootCertificate1.PEM)
utils.AddNocRootCertificate(setup, setup.Vendor1, rootCertificate1.PEM)

// add the new NOC root certificate
rootCertificate2 := utils.CreateTestNocRoot2Cert()
addNocX509RootCert := types.NewMsgAddNocX509RootCert(
accAddress.String(),
setup.Vendor1.String(),
rootCertificate2.PEM,
testconstants.CertSchemaVersion)
_, err := setup.Handler(setup.Ctx, addNocX509RootCert)
Expand Down
Loading

0 comments on commit 310d5c3

Please sign in to comment.