Skip to content

Commit

Permalink
[FAB-9312] Resolve metalinter warnings
Browse files Browse the repository at this point in the history
- fix core pkg

Change-Id: I6edffe958c80df8c51a22943e8161ec0bdcd89cb
Signed-off-by: Firas Qutishat <firas.qutishat@securekey.com>
  • Loading branch information
fqutishat committed Apr 5, 2018
1 parent 0a27dbc commit b396088
Show file tree
Hide file tree
Showing 16 changed files with 194 additions and 85 deletions.
7 changes: 4 additions & 3 deletions pkg/core/config/config.go
Expand Up @@ -90,8 +90,10 @@ func initFromReader(in io.Reader, configType string, opts ...Option) (core.Confi
// read config from bytes array, but must set ConfigType
// for viper to properly unmarshal the bytes array
backend.configViper.SetConfigType(configType)
backend.configViper.MergeConfig(in)

err = backend.configViper.MergeConfig(in)
if err != nil {
return nil, err
}
setLogLevel(backend)

return backend, nil
Expand Down Expand Up @@ -148,7 +150,6 @@ func setLogLevel(backend core.ConfigBackend) {
loggingLevelString, _ := backend.Lookup("client.logging.level")
logLevel := logging.INFO
if loggingLevelString != nil {
const logModule = "fabsdk" // TODO: allow more flexability in setting levels for different modules
var err error
logLevel, err = logging.LogLevel(loggingLevelString.(string))
if err != nil {
Expand Down
32 changes: 12 additions & 20 deletions pkg/core/config/config_test.go
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/hyperledger/fabric-sdk-go/pkg/util/pathvar"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand All @@ -32,22 +31,22 @@ const (

func TestFromRawSuccess(t *testing.T) {
// get a config byte for testing
cBytes, err := loadConfigBytesFromFile(t, configTestFilePath)
cBytes, _ := loadConfigBytesFromFile(t, configTestFilePath)

// test init config from bytes
_, err = FromRaw(cBytes, configType)()
_, err := FromRaw(cBytes, configType)()
if err != nil {
t.Fatalf("Failed to initialize config from bytes array. Error: %s", err)
}
}

func TestFromReaderSuccess(t *testing.T) {
// get a config byte for testing
cBytes, err := loadConfigBytesFromFile(t, configTestFilePath)
cBytes, _ := loadConfigBytesFromFile(t, configTestFilePath)
buf := bytes.NewBuffer(cBytes)

// test init config from bytes
_, err = FromReader(buf, configType)()
_, err := FromReader(buf, configType)()
if err != nil {
t.Fatalf("Failed to initialize config from bytes array. Error: %s", err)
}
Expand All @@ -65,7 +64,7 @@ func loadConfigBytesFromFile(t *testing.T, filePath string) ([]byte, error) {
t.Fatalf("Failed to read config file stat. Error: %s", err)
}
s := fi.Size()
cBytes := make([]byte, s, s)
cBytes := make([]byte, s)
n, err := f.Read(cBytes)
if err != nil {
t.Fatalf("Failed to read test config for bytes array testing. Error: %s", err)
Expand Down Expand Up @@ -101,14 +100,14 @@ func TestInitConfigWithCmdRoot(t *testing.T) {

logger.Infof("fileLoc right before calling InitConfigWithCmdRoot is %s", fileLoc)

configBackend, err := FromFile(fileLoc, WithEnvPrefix(cmdRoot))()
configBackend1, err := FromFile(fileLoc, WithEnvPrefix(cmdRoot))()
if err != nil {
t.Fatalf("Failed to initialize config backend with cmd root. Error: %s", err)
}

config := cryptosuite.ConfigFromBackend(configBackend)
config := cryptosuite.ConfigFromBackend(configBackend1)

secAlg, ok := configBackend.Lookup("client.BCCSP.security.hashAlgorithm")
secAlg, ok := configBackend1.Lookup("client.BCCSP.security.hashAlgorithm")
if !ok {
t.Fatal("supposed to get valid value")
}
Expand Down Expand Up @@ -158,7 +157,7 @@ func TestMultipleVipers(t *testing.T) {
t.Fatalf("Expected testValue before config initialization got: %s", testValue1)
}
// initialize go sdk
configBackend, err := FromFile(configTestFilePath)()
configBackend1, err := FromFile(configTestFilePath)()
if err != nil {
t.Log(err.Error())
}
Expand All @@ -169,7 +168,7 @@ func TestMultipleVipers(t *testing.T) {
t.Fatalf("Expected testvalue after config initialization")
}
// Make sure Go SDK config is unaffected
testValue3, ok := configBackend.Lookup("client.BCCSP.security.softVerify")
testValue3, ok := configBackend1.Lookup("client.BCCSP.security.softVerify")
if !ok {
t.Fatalf("Expected valid value")
}
Expand Down Expand Up @@ -210,12 +209,12 @@ func TestEnvironmentVariablesSpecificCmdRoot(t *testing.T) {
t.Log(err.Error())
}

configBackend, err := FromFile(configTestFilePath, WithEnvPrefix("test_root"))()
configBackend1, err := FromFile(configTestFilePath, WithEnvPrefix("test_root"))()
if err != nil {
t.Log(err.Error())
}

value, ok := configBackend.Lookup("env.test")
value, ok := configBackend1.Lookup("env.test")
if value != "456" || !ok {
t.Fatalf("Expected environment variable value but got: %s", testValue)
}
Expand Down Expand Up @@ -243,13 +242,6 @@ func teardown() {
configBackend = nil
}

func crossCheckWithViperConfig(expected string, actual string, message string, t *testing.T) {
expected = pathvar.Subst(expected)
if actual != expected {
t.Fatalf(message)
}
}

func TestNewGoodOpt(t *testing.T) {
_, err := FromFile("../../../test/fixtures/config/config_test.yaml", goodOpt())()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/cryptosuite/bccsp/pkcs11/cryptosuiteimpl_test.go
Expand Up @@ -169,7 +169,7 @@ func verifyHashFn(t *testing.T, c core.CryptoSuite) {
t.Fatalf("Not supposed to get error, but got: %v", err)
}

if bytes.Compare(a, e[:]) != 0 {
if !bytes.Equal(a, e[:]) {
t.Fatalf("Expected SHA 256 hash function")
}
}
2 changes: 1 addition & 1 deletion pkg/core/cryptosuite/bccsp/sw/cryptosuiteimpl_test.go
Expand Up @@ -86,7 +86,7 @@ func verifyHashFn(t *testing.T, c core.CryptoSuite) {
t.Fatalf("Not supposed to get error, but got: %v", err)
}

if bytes.Compare(a, e[:]) != 0 {
if !bytes.Equal(a, e[:]) {
t.Fatalf("Expected SHA 256 hash function")
}
}
3 changes: 0 additions & 3 deletions pkg/core/cryptosuite/bccsp/wrapper/cryptosuiteimpl.go
Expand Up @@ -10,12 +10,9 @@ import (
"hash"

"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
)

var logger = logging.NewLogger("fabsdk/core")

//NewCryptoSuite returns cryptosuite adaptor for given bccsp.BCCSP implementation
func NewCryptoSuite(bccsp bccsp.BCCSP) core.CryptoSuite {
return &CryptoSuite{
Expand Down
1 change: 0 additions & 1 deletion pkg/core/cryptosuite/bccsp/wrapper/mocksuite_test.go
Expand Up @@ -46,7 +46,6 @@ func getOptsByConfig(c core.CryptoSuiteConfig) *bccspSw.SwOpts {
},
Ephemeral: c.Ephemeral(),
}
logger.Debug("Initialized mock cryptosuite")

return opts
}
112 changes: 99 additions & 13 deletions pkg/core/cryptosuite/cryptoconfig_test.go
Expand Up @@ -17,16 +17,15 @@ import (

const configTestFilePath = "../config/testdata/config_test.yaml"

func TestCAConfig(t *testing.T) {
func TestCAConfigKeyStorePath(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

config := ConfigFromBackend(customBackend)
cryptoConfig := config.(*Config)
cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test KeyStore Path
val, ok := customBackend.Lookup("client.credentialStore.cryptoStore.path")
Expand All @@ -37,79 +36,166 @@ func TestCAConfig(t *testing.T) {
if path.Join(val.(string), "keystore") != cryptoConfig.KeyStorePath() {
t.Fatalf("Incorrect keystore path ")
}
}

func TestCAConfigBCCSPSecurityEnabled(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test BCCSP security is enabled
val, ok = customBackend.Lookup("client.BCCSP.security.enabled")
val, ok := customBackend.Lookup("client.BCCSP.security.enabled")
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(bool) != cryptoConfig.IsSecurityEnabled() {
t.Fatalf("Incorrect BCCSP Security enabled flag")
}
}

func TestCAConfigSecurityAlgorithm(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test SecurityAlgorithm
val, ok = customBackend.Lookup("client.BCCSP.security.hashAlgorithm")
val, ok := customBackend.Lookup("client.BCCSP.security.hashAlgorithm")
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(string) != cryptoConfig.SecurityAlgorithm() {
t.Fatalf("Incorrect BCCSP Security Hash algorithm")
}
}

func TestCAConfigSecurityLevel(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test Security Level
val, ok = customBackend.Lookup("client.BCCSP.security.level")
val, ok := customBackend.Lookup("client.BCCSP.security.level")
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(int) != cryptoConfig.SecurityLevel() {
t.Fatalf("Incorrect BCCSP Security Level")
}
}

func TestCAConfigSecurityProvider(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test SecurityProvider provider
val, ok = customBackend.Lookup("client.BCCSP.security.default.provider")
val, ok := customBackend.Lookup("client.BCCSP.security.default.provider")
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(string) != cryptoConfig.SecurityProvider() {
t.Fatalf("Incorrect BCCSP SecurityProvider provider")
}
}

func TestCAConfigEphemeralFlag(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test Ephemeral flag
val, ok = customBackend.Lookup("client.BCCSP.security.ephemeral")
val, ok := customBackend.Lookup("client.BCCSP.security.ephemeral")
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(bool) != cryptoConfig.Ephemeral() {
t.Fatalf("Incorrect BCCSP Ephemeral flag")
}
}

func TestCAConfigSoftVerifyFlag(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test SoftVerify flag
val, ok = customBackend.Lookup("client.BCCSP.security.softVerify")
val, ok := customBackend.Lookup("client.BCCSP.security.softVerify")
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(bool) != cryptoConfig.SoftVerify() {
t.Fatalf("Incorrect BCCSP Ephemeral flag")
}
}

func TestCAConfigSecurityProviderPin(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test SecurityProviderPin
val, ok = customBackend.Lookup("client.BCCSP.security.pin")
val, ok := customBackend.Lookup("client.BCCSP.security.pin")
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(string) != cryptoConfig.SecurityProviderPin() {
t.Fatalf("Incorrect BCCSP SecurityProviderPin flag")
}
}

// Test SecurityProviderPin
val, ok = customBackend.Lookup("client.BCCSP.security.label")
func TestCAConfigSecurityProviderLabel(t *testing.T) {
backend, err := config.FromFile(configTestFilePath)()
if err != nil {
t.Fatal("Failed to get config backend")
}

customBackend := getCustomBackend(backend)

cryptoConfig := ConfigFromBackend(customBackend).(*Config)

// Test SecurityProviderLabel
val, ok := customBackend.Lookup("client.BCCSP.security.label")
if !ok || val == nil {
t.Fatal("expected valid value")
}
if val.(string) != cryptoConfig.SecurityProviderLabel() {
t.Fatalf("Incorrect BCCSP SecurityProviderPin flag")
}

}

//getCustomBackend returns custom backend to override config values and to avoid using new config file for test scenarios
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/logging/metadata/callerInfo.go
Expand Up @@ -37,10 +37,10 @@ func (l *CallerInfo) HideCallerInfo(module string, level api.Level) {
//IsCallerInfoEnabled returns if callerinfo enabled for given module and level
func (l *CallerInfo) IsCallerInfoEnabled(module string, level api.Level) bool {
showcaller, exists := l.showcaller[callerInfoKey{module, level}]
if exists == false {
if !exists {
//If no callerinfo setting exists, then look for default
showcaller, exists = l.showcaller[callerInfoKey{"", level}]
if exists == false {
if !exists {
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/logging/metadata/level.go
Expand Up @@ -16,10 +16,10 @@ type ModuleLevels struct {
// GetLevel returns the log level for the given module.
func (l *ModuleLevels) GetLevel(module string) api.Level {
level, exists := l.levels[module]
if exists == false {
if !exists {
level, exists = l.levels[""]
// no configuration exists, default to info
if exists == false {
if !exists {
level = api.INFO
}
}
Expand Down

0 comments on commit b396088

Please sign in to comment.