github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/tools/cryptogen/csp/csp_test.go (about)

     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  package csp_test
    17  
    18  import (
    19  	"crypto/ecdsa"
    20  	"encoding/hex"
    21  	"errors"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"github.com/hyperledger/fabric/bccsp"
    27  	"github.com/hyperledger/fabric/common/tools/cryptogen/csp"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  // mock implementation of bccsp.Key interface
    32  type mockKey struct {
    33  	pubKeyErr error
    34  	bytesErr  error
    35  	pubKey    bccsp.Key
    36  }
    37  
    38  func (mk *mockKey) Bytes() ([]byte, error) {
    39  	if mk.bytesErr != nil {
    40  		return nil, mk.bytesErr
    41  	}
    42  	return []byte{1, 2, 3, 4}, nil
    43  }
    44  
    45  func (mk *mockKey) PublicKey() (bccsp.Key, error) {
    46  	if mk.pubKeyErr != nil {
    47  		return nil, mk.pubKeyErr
    48  	}
    49  	return mk.pubKey, nil
    50  }
    51  
    52  func (mk *mockKey) SKI() []byte { return []byte{1, 2, 3, 4} }
    53  
    54  func (mk *mockKey) Symmetric() bool { return false }
    55  
    56  func (mk *mockKey) Private() bool { return false }
    57  
    58  var testDir = filepath.Join(os.TempDir(), "csp-test")
    59  
    60  func TestGeneratePrivateKey(t *testing.T) {
    61  
    62  	priv, signer, err := csp.GeneratePrivateKey(testDir)
    63  	assert.NoError(t, err, "Failed to generate private key")
    64  	assert.NotNil(t, priv, "Should have returned a bccsp.Key")
    65  	assert.Equal(t, true, priv.Private(), "Failed to return private key")
    66  	assert.NotNil(t, signer, "Should have returned a crypto.Signer")
    67  	pkFile := filepath.Join(testDir, hex.EncodeToString(priv.SKI())+"_sk")
    68  	t.Log(pkFile)
    69  	assert.Equal(t, true, checkForFile(pkFile),
    70  		"Expected to find private key file")
    71  	cleanup(testDir)
    72  
    73  }
    74  
    75  func TestGetECPublicKey(t *testing.T) {
    76  
    77  	priv, _, err := csp.GeneratePrivateKey(testDir)
    78  	assert.NoError(t, err, "Failed to generate private key")
    79  
    80  	ecPubKey, err := csp.GetECPublicKey(priv)
    81  	assert.NoError(t, err, "Failed to get public key from private key")
    82  	assert.IsType(t, &ecdsa.PublicKey{}, ecPubKey,
    83  		"Failed to return an ecdsa.PublicKey")
    84  
    85  	// force errors using mockKey
    86  	priv = &mockKey{
    87  		pubKeyErr: nil,
    88  		bytesErr:  nil,
    89  		pubKey:    &mockKey{},
    90  	}
    91  	_, err = csp.GetECPublicKey(priv)
    92  	assert.Error(t, err, "Expected an error with a invalid pubKey bytes")
    93  	priv = &mockKey{
    94  		pubKeyErr: nil,
    95  		bytesErr:  nil,
    96  		pubKey: &mockKey{
    97  			bytesErr: errors.New("bytesErr"),
    98  		},
    99  	}
   100  	_, err = csp.GetECPublicKey(priv)
   101  	assert.EqualError(t, err, "bytesErr", "Expected bytesErr")
   102  	priv = &mockKey{
   103  		pubKeyErr: errors.New("pubKeyErr"),
   104  		bytesErr:  nil,
   105  		pubKey:    &mockKey{},
   106  	}
   107  	_, err = csp.GetECPublicKey(priv)
   108  	assert.EqualError(t, err, "pubKeyErr", "Expected pubKeyErr")
   109  
   110  	cleanup(testDir)
   111  }
   112  
   113  func cleanup(dir string) {
   114  	os.RemoveAll(dir)
   115  }
   116  
   117  func checkForFile(file string) bool {
   118  	if _, err := os.Stat(file); os.IsNotExist(err) {
   119  		return false
   120  	}
   121  	return true
   122  }