Skip to content

Commit 75db97d

Browse files
committed
Improved test coverage for bccsp/utils
This change-set improves the test coverage of the bccsp/utils package Change-Id: Iccef021c18e227abd14b7e7aa88928d11aff0903 Signed-off-by: Angelo De Caro <[email protected]>
1 parent 95d13d2 commit 75db97d

File tree

7 files changed

+355
-7
lines changed

7 files changed

+355
-7
lines changed

bccsp/utils/errs_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestErrToString(t *testing.T) {
27+
assert.Equal(t, ErrToString(errors.New("error")), "error")
28+
29+
assert.Equal(t, ErrToString(nil), "<clean>")
30+
31+
}

bccsp/utils/io.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. 2017 All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

bccsp/utils/io_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestDirExists(t *testing.T) {
28+
r, err := DirExists("")
29+
assert.False(t, r)
30+
assert.NoError(t, err)
31+
32+
r, err = DirExists(os.TempDir())
33+
assert.NoError(t, err)
34+
assert.Equal(t, true, r)
35+
36+
r, err = DirExists(filepath.Join(os.TempDir(), "7rhf90239vhev90"))
37+
assert.NoError(t, err)
38+
assert.Equal(t, false, r)
39+
}
40+
41+
func TestDirMissingOrEmpty(t *testing.T) {
42+
r, err := DirMissingOrEmpty("")
43+
assert.NoError(t, err)
44+
assert.True(t, r)
45+
46+
r, err = DirMissingOrEmpty(filepath.Join(os.TempDir(), "7rhf90239vhev90"))
47+
assert.NoError(t, err)
48+
assert.Equal(t, true, r)
49+
}
50+
51+
func TestDirEmpty(t *testing.T) {
52+
_, err := DirEmpty("")
53+
assert.Error(t, err)
54+
55+
path := filepath.Join(os.TempDir(), "7rhf90239vhev90")
56+
defer os.Remove(path)
57+
os.Mkdir(path, os.ModePerm)
58+
59+
r, err := DirEmpty(path)
60+
assert.NoError(t, err)
61+
assert.Equal(t, true, r)
62+
63+
r, err = DirEmpty(os.TempDir())
64+
assert.NoError(t, err)
65+
assert.Equal(t, false, r)
66+
67+
r, err = DirMissingOrEmpty(os.TempDir())
68+
assert.NoError(t, err)
69+
assert.Equal(t, false, r)
70+
71+
r, err = DirMissingOrEmpty(path)
72+
assert.NoError(t, err)
73+
assert.Equal(t, true, r)
74+
}

bccsp/utils/keys.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ func PrivateKeyToPEM(privateKey interface{}, pwd []byte) ([]byte, error) {
8282
if len(pwd) != 0 {
8383
return PrivateKeyToEncryptedPEM(privateKey, pwd)
8484
}
85+
if privateKey == nil {
86+
return nil, errors.New("Invalid key. It must be different from nil.")
87+
}
8588

8689
switch k := privateKey.(type) {
8790
case *ecdsa.PrivateKey:
@@ -131,7 +134,6 @@ func PrivateKeyToPEM(privateKey interface{}, pwd []byte) ([]byte, error) {
131134
if k == nil {
132135
return nil, errors.New("Invalid rsa private key. It must be different from nil.")
133136
}
134-
135137
raw := x509.MarshalPKCS1PrivateKey(k)
136138

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

148150
// PrivateKeyToEncryptedPEM converts a private key to an encrypted PEM
149151
func PrivateKeyToEncryptedPEM(privateKey interface{}, pwd []byte) ([]byte, error) {
152+
if privateKey == nil {
153+
return nil, errors.New("Invalid private key. It must be different from nil.")
154+
}
155+
150156
switch k := privateKey.(type) {
151157
case *ecdsa.PrivateKey:
152158
if k == nil {
153159
return nil, errors.New("Invalid ecdsa private key. It must be different from nil.")
154160
}
155-
156161
raw, err := x509.MarshalECPrivateKey(k)
157162

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

303+
if publicKey == nil {
304+
return nil, errors.New("Invalid public key. It must be different from nil.")
305+
}
306+
298307
switch k := publicKey.(type) {
299308
case *ecdsa.PublicKey:
300309
if k == nil {
301310
return nil, errors.New("Invalid ecdsa public key. It must be different from nil.")
302311
}
303-
304312
PubASN1, err := x509.MarshalPKIXPublicKey(k)
305313
if err != nil {
306314
return nil, err
@@ -316,7 +324,6 @@ func PublicKeyToPEM(publicKey interface{}, pwd []byte) ([]byte, error) {
316324
if k == nil {
317325
return nil, errors.New("Invalid rsa public key. It must be different from nil.")
318326
}
319-
320327
PubASN1, err := x509.MarshalPKIXPublicKey(k)
321328
if err != nil {
322329
return nil, err
@@ -336,12 +343,15 @@ func PublicKeyToPEM(publicKey interface{}, pwd []byte) ([]byte, error) {
336343

337344
// PublicKeyToDER marshals a public key to the der format
338345
func PublicKeyToDER(publicKey interface{}) ([]byte, error) {
346+
if publicKey == nil {
347+
return nil, errors.New("Invalid public key. It must be different from nil.")
348+
}
349+
339350
switch k := publicKey.(type) {
340351
case *ecdsa.PublicKey:
341352
if k == nil {
342353
return nil, errors.New("Invalid ecdsa public key. It must be different from nil.")
343354
}
344-
345355
PubASN1, err := x509.MarshalPKIXPublicKey(k)
346356
if err != nil {
347357
return nil, err
@@ -356,13 +366,19 @@ func PublicKeyToDER(publicKey interface{}) ([]byte, error) {
356366

357367
// PublicKeyToEncryptedPEM converts a public key to encrypted pem
358368
func PublicKeyToEncryptedPEM(publicKey interface{}, pwd []byte) ([]byte, error) {
369+
if publicKey == nil {
370+
return nil, errors.New("Invalid public key. It must be different from nil.")
371+
}
372+
if len(pwd) == 0 {
373+
return nil, errors.New("Invalid password. It must be different from nil.")
374+
}
375+
359376
switch k := publicKey.(type) {
360377
case *ecdsa.PublicKey:
361378
if k == nil {
362379
return nil, errors.New("Invalid ecdsa public key. It must be different from nil.")
363380
}
364381
raw, err := x509.MarshalPKIXPublicKey(k)
365-
366382
if err != nil {
367383
return nil, err
368384
}

bccsp/utils/keys_test.go

+97
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/ecdsa"
55
"crypto/elliptic"
66
"crypto/rand"
7+
"crypto/rsa"
78
"crypto/x509"
89
"encoding/asn1"
910
"encoding/pem"
@@ -271,4 +272,100 @@ func TestECDSAKeys(t *testing.T) {
271272
if err == nil {
272273
t.Fatal("PEMtoPublicKey should fail on nil PEM and wrong password")
273274
}
275+
276+
// Public Key DER format
277+
der, err = PublicKeyToDER(&key.PublicKey)
278+
assert.NoError(t, err)
279+
keyFromDER, err = DERToPublicKey(der)
280+
assert.NoError(t, err)
281+
ecdsaPkFromPEM = keyFromDER.(*ecdsa.PublicKey)
282+
// TODO: check the curve
283+
if key.X.Cmp(ecdsaPkFromPEM.X) != 0 {
284+
t.Fatal("Failed converting PEM to private key. Invalid X coordinate.")
285+
}
286+
if key.Y.Cmp(ecdsaPkFromPEM.Y) != 0 {
287+
t.Fatal("Failed converting PEM to private key. Invalid Y coordinate.")
288+
}
289+
}
290+
291+
func TestAESKey(t *testing.T) {
292+
k := []byte{0, 1, 2, 3, 4, 5}
293+
pem := AEStoPEM(k)
294+
295+
k2, err := PEMtoAES(pem, nil)
296+
assert.NoError(t, err)
297+
assert.Equal(t, k, k2)
298+
299+
pem, err = AEStoEncryptedPEM(k, k)
300+
assert.NoError(t, err)
301+
302+
k2, err = PEMtoAES(pem, k)
303+
assert.NoError(t, err)
304+
assert.Equal(t, k, k2)
305+
}
306+
307+
func TestDERToPublicKey(t *testing.T) {
308+
_, err := DERToPublicKey(nil)
309+
assert.Error(t, err)
310+
}
311+
312+
func TestNil(t *testing.T) {
313+
_, err := PrivateKeyToEncryptedPEM(nil, nil)
314+
assert.Error(t, err)
315+
316+
_, err = PEMtoAES(nil, nil)
317+
assert.Error(t, err)
318+
319+
_, err = AEStoEncryptedPEM(nil, nil)
320+
assert.Error(t, err)
321+
322+
_, err = PublicKeyToPEM(nil, nil)
323+
assert.Error(t, err)
324+
_, err = PublicKeyToPEM(nil, []byte("hello world"))
325+
assert.Error(t, err)
326+
327+
_, err = PublicKeyToPEM("hello world", nil)
328+
assert.Error(t, err)
329+
_, err = PublicKeyToPEM("hello world", []byte("hello world"))
330+
assert.Error(t, err)
331+
332+
_, err = PublicKeyToDER(nil)
333+
assert.Error(t, err)
334+
_, err = PublicKeyToDER("hello world")
335+
assert.Error(t, err)
336+
337+
_, err = PublicKeyToEncryptedPEM(nil, nil)
338+
assert.Error(t, err)
339+
_, err = PublicKeyToEncryptedPEM("hello world", nil)
340+
assert.Error(t, err)
341+
_, err = PublicKeyToEncryptedPEM("hello world", []byte("Hello world"))
342+
assert.Error(t, err)
343+
344+
}
345+
346+
func TestPrivateKeyToPEM(t *testing.T) {
347+
_, err := PrivateKeyToPEM(nil, nil)
348+
assert.Error(t, err)
349+
350+
_, err = PrivateKeyToPEM("hello world", nil)
351+
assert.Error(t, err)
352+
353+
key, err := rsa.GenerateKey(rand.Reader, 1024)
354+
assert.NoError(t, err)
355+
pem, err := PrivateKeyToPEM(key, nil)
356+
assert.NoError(t, err)
357+
assert.NotNil(t, pem)
358+
key2, err := PEMtoPrivateKey(pem, nil)
359+
assert.NoError(t, err)
360+
assert.NotNil(t, key2)
361+
assert.Equal(t, key.D, key2.(*rsa.PrivateKey).D)
362+
363+
pem, err = PublicKeyToPEM(&key.PublicKey, nil)
364+
assert.NoError(t, err)
365+
assert.NotNil(t, pem)
366+
key3, err := PEMtoPublicKey(pem, nil)
367+
assert.NoError(t, err)
368+
assert.NotNil(t, key2)
369+
assert.Equal(t, key.PublicKey.E, key3.(*rsa.PublicKey).E)
370+
assert.Equal(t, key.PublicKey.N, key3.(*rsa.PublicKey).N)
274371
}

bccsp/utils/slice_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestClone(t *testing.T) {
26+
src := []byte{0, 1, 2, 3, 4}
27+
clone := Clone(src)
28+
assert.Equal(t, src, clone)
29+
}

0 commit comments

Comments
 (0)