Skip to content

Commit

Permalink
[FAB-4335] Remove checks on mock/util function
Browse files Browse the repository at this point in the history
External unit tests that call `GenerateMockPublicPrivateKeyPairPEM`
shouldn't have to check for the error returned by this function.
This should be checked once by the
`TestGenerateMockPublicPrivateKeyPairPEM` test.

This changeset addresses this.

Change-Id: Ia70f707f4943647ef614533686b99105213e855d
Signed-off-by: Kostas Christidis <kostas@christidis.io>
  • Loading branch information
kchristidis committed Jun 2, 2017
1 parent a690a05 commit 56d06f7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 45 deletions.
10 changes: 2 additions & 8 deletions common/viperutil/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ func TestPEMBlocksFromFile(t *testing.T) {
numberOfCertificates := 3
var pems []byte
for i := 0; i < numberOfCertificates; i++ {
publicKeyCert, _, err := util.GenerateMockPublicPrivateKeyPairPEM(true)
if err != nil {
t.Fatalf("Enable to generate a signer certificate: %v", err)
}
publicKeyCert, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true)
pems = append(pems, publicKeyCert...)
}

Expand Down Expand Up @@ -259,10 +256,7 @@ func TestPEMBlocksFromFileEnv(t *testing.T) {
numberOfCertificates := 3
var pems []byte
for i := 0; i < numberOfCertificates; i++ {
publicKeyCert, _, err := util.GenerateMockPublicPrivateKeyPairPEM(true)
if err != nil {
t.Fatalf("Enable to generate a signer certificate: %v", err)
}
publicKeyCert, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true)
pems = append(pems, publicKeyCert...)
}

Expand Down
30 changes: 6 additions & 24 deletions orderer/kafka/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,8 @@ func TestNewBrokerConfig(t *testing.T) {
}

func TestTLSConfigEnabled(t *testing.T) {
publicKey, privateKey, err := util.GenerateMockPublicPrivateKeyPairPEM(false)
if err != nil {
t.Fatalf("Enable to generate a public/private key pair: %v", err)
}
caPublicKey, _, err := util.GenerateMockPublicPrivateKeyPairPEM(true)
if err != nil {
t.Fatalf("Enable to generate a signer certificate: %v", err)
}
publicKey, privateKey, _ := util.GenerateMockPublicPrivateKeyPairPEM(false)
caPublicKey, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true)

config := newBrokerConfig(testConf.Kafka.Version, 0, config.TLS{
Enabled: true,
Expand All @@ -137,14 +131,8 @@ func TestTLSConfigEnabled(t *testing.T) {
}

func TestTLSConfigDisabled(t *testing.T) {
publicKey, privateKey, err := util.GenerateMockPublicPrivateKeyPairPEM(false)
if err != nil {
t.Fatalf("Enable to generate a public/private key pair: %v", err)
}
caPublicKey, _, err := util.GenerateMockPublicPrivateKeyPairPEM(true)
if err != nil {
t.Fatalf("Enable to generate a signer certificate: %v", err)
}
publicKey, privateKey, _ := util.GenerateMockPublicPrivateKeyPairPEM(false)
caPublicKey, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true)

config := newBrokerConfig(testConf.Kafka.Version, 0, config.TLS{
Enabled: false,
Expand All @@ -159,14 +147,8 @@ func TestTLSConfigDisabled(t *testing.T) {
}

func TestTLSConfigBadCert(t *testing.T) {
publicKey, privateKey, err := util.GenerateMockPublicPrivateKeyPairPEM(false)
if err != nil {
t.Fatalf("Enable to generate a public/private key pair: %v", err)
}
caPublicKey, _, err := util.GenerateMockPublicPrivateKeyPairPEM(true)
if err != nil {
t.Fatalf("Enable to generate a signer certificate: %v", err)
}
publicKey, privateKey, _ := util.GenerateMockPublicPrivateKeyPairPEM(false)
caPublicKey, _, _ := util.GenerateMockPublicPrivateKeyPairPEM(true)

t.Run("BadPrivateKey", func(t *testing.T) {
assert.Panics(t, func() {
Expand Down
17 changes: 4 additions & 13 deletions orderer/mocks/util/util.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package util
Expand All @@ -25,7 +15,8 @@ import (
"math/big"
)

// GenerateMockPublicPrivateKeyPairPEM returns public/private key pair encoded as PEM strings
// GenerateMockPublicPrivateKeyPairPEM returns public/private key pair encoded
// as PEM strings.
func GenerateMockPublicPrivateKeyPairPEM(isCA bool) (string, string, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions orderer/mocks/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGenerateMockPublicPrivateKeyPairPEM(t *testing.T) {
_, _, err := GenerateMockPublicPrivateKeyPairPEM(false)
assert.NoError(t, err, "Unable to generate a public/private key pair: %v", err)
}

func TestGenerateMockPublicPrivateKeyPairPEMWhenCASet(t *testing.T) {
_, _, err := GenerateMockPublicPrivateKeyPairPEM(true)
assert.NoError(t, err, "Unable to generate a signer certificate: %v", err)
}

0 comments on commit 56d06f7

Please sign in to comment.