Skip to content

Commit

Permalink
Improved test coverage for bccsp/utils
Browse files Browse the repository at this point in the history
This change-set improves the test coverage
of the bccsp/utils package

Change-Id: Iccef021c18e227abd14b7e7aa88928d11aff0903
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed Apr 22, 2017
1 parent 95d13d2 commit 75db97d
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 7 deletions.
31 changes: 31 additions & 0 deletions bccsp/utils/errs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright IBM Corp. 2017 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.
*/

package utils

import (
"errors"
"testing"

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

func TestErrToString(t *testing.T) {
assert.Equal(t, ErrToString(errors.New("error")), "error")

assert.Equal(t, ErrToString(nil), "<clean>")

}
2 changes: 1 addition & 1 deletion bccsp/utils/io.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. 2017 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.
Expand Down
74 changes: 74 additions & 0 deletions bccsp/utils/io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright IBM Corp. 2017 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.
*/

package utils

import (
"os"
"path/filepath"
"testing"

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

func TestDirExists(t *testing.T) {
r, err := DirExists("")
assert.False(t, r)
assert.NoError(t, err)

r, err = DirExists(os.TempDir())
assert.NoError(t, err)
assert.Equal(t, true, r)

r, err = DirExists(filepath.Join(os.TempDir(), "7rhf90239vhev90"))
assert.NoError(t, err)
assert.Equal(t, false, r)
}

func TestDirMissingOrEmpty(t *testing.T) {
r, err := DirMissingOrEmpty("")
assert.NoError(t, err)
assert.True(t, r)

r, err = DirMissingOrEmpty(filepath.Join(os.TempDir(), "7rhf90239vhev90"))
assert.NoError(t, err)
assert.Equal(t, true, r)
}

func TestDirEmpty(t *testing.T) {
_, err := DirEmpty("")
assert.Error(t, err)

path := filepath.Join(os.TempDir(), "7rhf90239vhev90")
defer os.Remove(path)
os.Mkdir(path, os.ModePerm)

r, err := DirEmpty(path)
assert.NoError(t, err)
assert.Equal(t, true, r)

r, err = DirEmpty(os.TempDir())
assert.NoError(t, err)
assert.Equal(t, false, r)

r, err = DirMissingOrEmpty(os.TempDir())
assert.NoError(t, err)
assert.Equal(t, false, r)

r, err = DirMissingOrEmpty(path)
assert.NoError(t, err)
assert.Equal(t, true, r)
}
28 changes: 22 additions & 6 deletions bccsp/utils/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func PrivateKeyToPEM(privateKey interface{}, pwd []byte) ([]byte, error) {
if len(pwd) != 0 {
return PrivateKeyToEncryptedPEM(privateKey, pwd)
}
if privateKey == nil {
return nil, errors.New("Invalid key. It must be different from nil.")
}

switch k := privateKey.(type) {
case *ecdsa.PrivateKey:
Expand Down Expand Up @@ -131,7 +134,6 @@ func PrivateKeyToPEM(privateKey interface{}, pwd []byte) ([]byte, error) {
if k == nil {
return nil, errors.New("Invalid rsa private key. It must be different from nil.")
}

raw := x509.MarshalPKCS1PrivateKey(k)

return pem.EncodeToMemory(
Expand All @@ -147,12 +149,15 @@ func PrivateKeyToPEM(privateKey interface{}, pwd []byte) ([]byte, error) {

// PrivateKeyToEncryptedPEM converts a private key to an encrypted PEM
func PrivateKeyToEncryptedPEM(privateKey interface{}, pwd []byte) ([]byte, error) {
if privateKey == nil {
return nil, errors.New("Invalid private key. It must be different from nil.")
}

switch k := privateKey.(type) {
case *ecdsa.PrivateKey:
if k == nil {
return nil, errors.New("Invalid ecdsa private key. It must be different from nil.")
}

raw, err := x509.MarshalECPrivateKey(k)

if err != nil {
Expand Down Expand Up @@ -295,12 +300,15 @@ func PublicKeyToPEM(publicKey interface{}, pwd []byte) ([]byte, error) {
return PublicKeyToEncryptedPEM(publicKey, pwd)
}

if publicKey == nil {
return nil, errors.New("Invalid public key. It must be different from nil.")
}

switch k := publicKey.(type) {
case *ecdsa.PublicKey:
if k == nil {
return nil, errors.New("Invalid ecdsa public key. It must be different from nil.")
}

PubASN1, err := x509.MarshalPKIXPublicKey(k)
if err != nil {
return nil, err
Expand All @@ -316,7 +324,6 @@ func PublicKeyToPEM(publicKey interface{}, pwd []byte) ([]byte, error) {
if k == nil {
return nil, errors.New("Invalid rsa public key. It must be different from nil.")
}

PubASN1, err := x509.MarshalPKIXPublicKey(k)
if err != nil {
return nil, err
Expand All @@ -336,12 +343,15 @@ func PublicKeyToPEM(publicKey interface{}, pwd []byte) ([]byte, error) {

// PublicKeyToDER marshals a public key to the der format
func PublicKeyToDER(publicKey interface{}) ([]byte, error) {
if publicKey == nil {
return nil, errors.New("Invalid public key. It must be different from nil.")
}

switch k := publicKey.(type) {
case *ecdsa.PublicKey:
if k == nil {
return nil, errors.New("Invalid ecdsa public key. It must be different from nil.")
}

PubASN1, err := x509.MarshalPKIXPublicKey(k)
if err != nil {
return nil, err
Expand All @@ -356,13 +366,19 @@ func PublicKeyToDER(publicKey interface{}) ([]byte, error) {

// PublicKeyToEncryptedPEM converts a public key to encrypted pem
func PublicKeyToEncryptedPEM(publicKey interface{}, pwd []byte) ([]byte, error) {
if publicKey == nil {
return nil, errors.New("Invalid public key. It must be different from nil.")
}
if len(pwd) == 0 {
return nil, errors.New("Invalid password. It must be different from nil.")
}

switch k := publicKey.(type) {
case *ecdsa.PublicKey:
if k == nil {
return nil, errors.New("Invalid ecdsa public key. It must be different from nil.")
}
raw, err := x509.MarshalPKIXPublicKey(k)

if err != nil {
return nil, err
}
Expand Down
97 changes: 97 additions & 0 deletions bccsp/utils/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"encoding/pem"
Expand Down Expand Up @@ -271,4 +272,100 @@ func TestECDSAKeys(t *testing.T) {
if err == nil {
t.Fatal("PEMtoPublicKey should fail on nil PEM and wrong password")
}

// Public Key DER format
der, err = PublicKeyToDER(&key.PublicKey)
assert.NoError(t, err)
keyFromDER, err = DERToPublicKey(der)
assert.NoError(t, err)
ecdsaPkFromPEM = keyFromDER.(*ecdsa.PublicKey)
// TODO: check the curve
if key.X.Cmp(ecdsaPkFromPEM.X) != 0 {
t.Fatal("Failed converting PEM to private key. Invalid X coordinate.")
}
if key.Y.Cmp(ecdsaPkFromPEM.Y) != 0 {
t.Fatal("Failed converting PEM to private key. Invalid Y coordinate.")
}
}

func TestAESKey(t *testing.T) {
k := []byte{0, 1, 2, 3, 4, 5}
pem := AEStoPEM(k)

k2, err := PEMtoAES(pem, nil)
assert.NoError(t, err)
assert.Equal(t, k, k2)

pem, err = AEStoEncryptedPEM(k, k)
assert.NoError(t, err)

k2, err = PEMtoAES(pem, k)
assert.NoError(t, err)
assert.Equal(t, k, k2)
}

func TestDERToPublicKey(t *testing.T) {
_, err := DERToPublicKey(nil)
assert.Error(t, err)
}

func TestNil(t *testing.T) {
_, err := PrivateKeyToEncryptedPEM(nil, nil)
assert.Error(t, err)

_, err = PEMtoAES(nil, nil)
assert.Error(t, err)

_, err = AEStoEncryptedPEM(nil, nil)
assert.Error(t, err)

_, err = PublicKeyToPEM(nil, nil)
assert.Error(t, err)
_, err = PublicKeyToPEM(nil, []byte("hello world"))
assert.Error(t, err)

_, err = PublicKeyToPEM("hello world", nil)
assert.Error(t, err)
_, err = PublicKeyToPEM("hello world", []byte("hello world"))
assert.Error(t, err)

_, err = PublicKeyToDER(nil)
assert.Error(t, err)
_, err = PublicKeyToDER("hello world")
assert.Error(t, err)

_, err = PublicKeyToEncryptedPEM(nil, nil)
assert.Error(t, err)
_, err = PublicKeyToEncryptedPEM("hello world", nil)
assert.Error(t, err)
_, err = PublicKeyToEncryptedPEM("hello world", []byte("Hello world"))
assert.Error(t, err)

}

func TestPrivateKeyToPEM(t *testing.T) {
_, err := PrivateKeyToPEM(nil, nil)
assert.Error(t, err)

_, err = PrivateKeyToPEM("hello world", nil)
assert.Error(t, err)

key, err := rsa.GenerateKey(rand.Reader, 1024)
assert.NoError(t, err)
pem, err := PrivateKeyToPEM(key, nil)
assert.NoError(t, err)
assert.NotNil(t, pem)
key2, err := PEMtoPrivateKey(pem, nil)
assert.NoError(t, err)
assert.NotNil(t, key2)
assert.Equal(t, key.D, key2.(*rsa.PrivateKey).D)

pem, err = PublicKeyToPEM(&key.PublicKey, nil)
assert.NoError(t, err)
assert.NotNil(t, pem)
key3, err := PEMtoPublicKey(pem, nil)
assert.NoError(t, err)
assert.NotNil(t, key2)
assert.Equal(t, key.PublicKey.E, key3.(*rsa.PublicKey).E)
assert.Equal(t, key.PublicKey.N, key3.(*rsa.PublicKey).N)
}
29 changes: 29 additions & 0 deletions bccsp/utils/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright IBM Corp. 2017 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.
*/

package utils

import (
"testing"

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

func TestClone(t *testing.T) {
src := []byte{0, 1, 2, 3, 4}
clone := Clone(src)
assert.Equal(t, src, clone)
}
Loading

0 comments on commit 75db97d

Please sign in to comment.