forked from edgexfoundry/edgex-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add unit tests for new functions
Relates to edgexfoundry#5038. Add unit tests on the controller and app layers. Signed-off-by: Lindsey Cheng <[email protected]>
- Loading branch information
1 parent
b41f93f
commit 48ba4ff
Showing
14 changed files
with
684 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package utils | ||
package crypto | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Copyright (C) 2024 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package interfaces | ||
|
||
import "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" | ||
|
||
type Crypto interface { | ||
Encrypt(string) (string, errors.EdgeX) | ||
Decrypt(string) ([]byte, errors.EdgeX) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// | ||
// Copyright (C) 2024 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package application | ||
|
||
import ( | ||
"testing" | ||
|
||
cryptoMocks "github.com/edgexfoundry/edgex-go/internal/pkg/utils/crypto/interfaces/mocks" | ||
"github.com/edgexfoundry/edgex-go/internal/security/proxyauth/container" | ||
"github.com/edgexfoundry/edgex-go/internal/security/proxyauth/infrastructure/interfaces/mocks" | ||
|
||
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v4/bootstrap/container" | ||
"github.com/edgexfoundry/go-mod-bootstrap/v4/di" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v4/clients/logger" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v4/common" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v4/errors" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v4/models" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func mockDic() *di.Container { | ||
return di.NewContainer(di.ServiceConstructorMap{ | ||
bootstrapContainer.LoggingClientInterfaceName: func(get di.Get) interface{} { | ||
return logger.NewMockClient() | ||
}, | ||
}) | ||
} | ||
|
||
func TestAddKey(t *testing.T) { | ||
dic := mockDic() | ||
|
||
validNewKey := "validNewKey" | ||
validIssuer := "testIssuer" | ||
validKeyData := models.KeyData{ | ||
Type: common.VerificationKeyType, | ||
Issuer: validIssuer, | ||
Key: validNewKey, | ||
} | ||
validKeyName := validKeyData.Issuer + "/" + validKeyData.Type | ||
validEncryptedKey := "encryptedValidNewKey" | ||
|
||
validUpdateKey := "validUpdateKey" | ||
updateKeyData := models.KeyData{ | ||
Type: common.SigningKeyType, | ||
Issuer: "issuer2", | ||
Key: validUpdateKey, | ||
} | ||
validUpdateKeyName := updateKeyData.Issuer + "/" + updateKeyData.Type | ||
validUpdateEncryptedKey := "encryptedValidUpdateKey" | ||
|
||
invalidKeyData := models.KeyData{ | ||
Type: "invalidKeyType", | ||
Issuer: "issuer2", | ||
Key: validUpdateKey, | ||
} | ||
|
||
encryptFailedKey := "encryptFailedKey" | ||
encryptFailedKeyData := models.KeyData{ | ||
Type: common.SigningKeyType, | ||
Issuer: "issuer3", | ||
Key: encryptFailedKey, | ||
} | ||
|
||
dbClientMock := &mocks.DBClient{} | ||
dbClientMock.On("KeyExists", validKeyName).Return(false, nil) | ||
dbClientMock.On("AddKey", validKeyName, validEncryptedKey).Return(nil) | ||
dbClientMock.On("KeyExists", validUpdateKeyName).Return(true, nil) | ||
dbClientMock.On("UpdateKey", validUpdateKeyName, validUpdateEncryptedKey).Return(nil) | ||
|
||
cryptoMock := &cryptoMocks.Crypto{} | ||
cryptoMock.On("Encrypt", validKeyData.Key).Return(validEncryptedKey, nil) | ||
cryptoMock.On("Encrypt", updateKeyData.Key).Return(validUpdateEncryptedKey, nil) | ||
cryptoMock.On("Encrypt", encryptFailedKeyData.Key).Return("", errors.NewCommonEdgeX(errors.KindServerError, "failed to encrypt the key", nil)) | ||
|
||
dic.Update(di.ServiceConstructorMap{ | ||
container.DBClientInterfaceName: func(get di.Get) interface{} { | ||
return dbClientMock | ||
}, | ||
container.CryptoInterfaceName: func(get di.Get) interface{} { | ||
return cryptoMock | ||
}, | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
keyData models.KeyData | ||
errorExpected bool | ||
errKind errors.ErrKind | ||
}{ | ||
{"Valid - Add new verification key", validKeyData, false, ""}, | ||
{"Valid - Update existing signing key", updateKeyData, false, ""}, | ||
{"Invalid - Invalid key type", invalidKeyData, true, errors.KindContractInvalid}, | ||
{"Invalid - Encryption Error", encryptFailedKeyData, true, errors.KindServerError}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := AddKey(dic, test.keyData) | ||
if test.errorExpected { | ||
require.Error(t, err) | ||
require.Equal(t, test.errKind, errors.Kind(err)) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestVerificationKeyByIssuer(t *testing.T) { | ||
dic := mockDic() | ||
|
||
validIssuer := "issuer1" | ||
validEncryptedKey := "encryptedKey" | ||
expectedKeyName := validIssuer + "/" + common.VerificationKeyType | ||
expectedKeyData := dtos.KeyData{Issuer: validIssuer, Type: common.VerificationKeyType, Key: "decryptedKey"} | ||
|
||
invalidIssuer := "invalidIssuer" | ||
invalidKeyName := invalidIssuer + "/" + common.VerificationKeyType | ||
|
||
decryptErrIssuer := "decryptErrIssuer" | ||
decryptErrKeyName := decryptErrIssuer + "/" + common.VerificationKeyType | ||
decryptErrKey := "decryptErrKey" | ||
|
||
dbClientMock := &mocks.DBClient{} | ||
dbClientMock.On("ReadKeyContent", expectedKeyName).Return(validEncryptedKey, nil) | ||
dbClientMock.On("ReadKeyContent", invalidKeyName).Return("", errors.NewCommonEdgeX(errors.KindServerError, "read key error", nil)) | ||
dbClientMock.On("ReadKeyContent", decryptErrKeyName).Return(decryptErrKey, nil) | ||
|
||
cryptoMock := &cryptoMocks.Crypto{} | ||
cryptoMock.On("Decrypt", validEncryptedKey).Return([]byte("decryptedKey"), nil) | ||
cryptoMock.On("Decrypt", decryptErrKey).Return([]byte{}, errors.NewCommonEdgeX(errors.KindServerError, "decrypt key error", nil)) | ||
|
||
dic.Update(di.ServiceConstructorMap{ | ||
container.DBClientInterfaceName: func(get di.Get) interface{} { | ||
return dbClientMock | ||
}, | ||
container.CryptoInterfaceName: func(get di.Get) interface{} { | ||
return cryptoMock | ||
}, | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
issuer string | ||
expectedKeyData dtos.KeyData | ||
errorExpected bool | ||
errKind errors.ErrKind | ||
}{ | ||
{"Valid - Valid key", validIssuer, expectedKeyData, false, ""}, | ||
{"Invalid - Empty issuer", "", dtos.KeyData{}, true, errors.KindContractInvalid}, | ||
{"Invalid - Key read error", invalidIssuer, dtos.KeyData{}, true, errors.KindServerError}, | ||
{"Invalid - Decryption error", decryptErrIssuer, dtos.KeyData{}, true, errors.KindServerError}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
result, err := VerificationKeyByIssuer(dic, test.issuer) | ||
if test.errorExpected { | ||
require.Error(t, err) | ||
require.Equal(t, test.errKind, errors.Kind(err)) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, test.expectedKeyData, result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Copyright (C) 2024 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package container | ||
|
||
import ( | ||
"github.com/edgexfoundry/edgex-go/internal/pkg/utils/crypto/interfaces" | ||
|
||
"github.com/edgexfoundry/go-mod-bootstrap/v4/di" | ||
) | ||
|
||
// CryptoInterfaceName contains the name of the interfaces.Crypto implementation in the DIC. | ||
var CryptoInterfaceName = di.TypeInstanceToName((*interfaces.Crypto)(nil)) | ||
|
||
// CryptoFrom helper function queries the DIC and returns the interfaces.Cryptor implementation. | ||
func CryptoFrom(get di.Get) interfaces.Crypto { | ||
return get(CryptoInterfaceName).(interfaces.Crypto) | ||
} |
Oops, something went wrong.