github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/tools/cryptogen/ca/ca_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 ca_test
    17  
    18  import (
    19  	"crypto/ecdsa"
    20  	"crypto/x509"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/hyperledger/fabric/common/tools/cryptogen/ca"
    26  	"github.com/hyperledger/fabric/common/tools/cryptogen/csp"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  const (
    31  	testCAName  = "root0"
    32  	testCA2Name = "root1"
    33  	testName    = "cert0"
    34  )
    35  
    36  var testDir = filepath.Join(os.TempDir(), "ca-test")
    37  
    38  func TestNewCA(t *testing.T) {
    39  
    40  	caDir := filepath.Join(testDir, "ca")
    41  	rootCA, err := ca.NewCA(caDir, testCAName)
    42  	assert.NoError(t, err, "Error generating CA")
    43  	assert.NotNil(t, rootCA, "Failed to return CA")
    44  	assert.NotNil(t, rootCA.Signer,
    45  		"rootCA.Signer should not be empty")
    46  	assert.IsType(t, &x509.Certificate{}, rootCA.SignCert,
    47  		"rootCA.SignCert should be type x509.Certificate")
    48  
    49  	// check to make sure the root public key was stored
    50  	pemFile := filepath.Join(caDir, testCAName+"-cert.pem")
    51  	assert.Equal(t, true, checkForFile(pemFile),
    52  		"Expected to find file "+pemFile)
    53  	cleanup(testDir)
    54  
    55  }
    56  
    57  func TestGenerateSignCertificate(t *testing.T) {
    58  
    59  	caDir := filepath.Join(testDir, "ca")
    60  	certDir := filepath.Join(testDir, "certs")
    61  	// generate private key
    62  	priv, _, err := csp.GeneratePrivateKey(certDir)
    63  	assert.NoError(t, err, "Failed to generate signed certificate")
    64  
    65  	// get EC public key
    66  	ecPubKey, err := csp.GetECPublicKey(priv)
    67  	assert.NoError(t, err, "Failed to generate signed certificate")
    68  	assert.NotNil(t, ecPubKey, "Failed to generate signed certificate")
    69  
    70  	// create our CA
    71  	rootCA, err := ca.NewCA(caDir, testCA2Name)
    72  	assert.NoError(t, err, "Error generating CA")
    73  
    74  	err = rootCA.SignCertificate(certDir, testName, ecPubKey)
    75  	assert.NoError(t, err, "Failed to generate signed certificate")
    76  
    77  	// check to make sure the signed public key was stored
    78  	pemFile := filepath.Join(certDir, testName+"-cert.pem")
    79  	assert.Equal(t, true, checkForFile(pemFile),
    80  		"Expected to find file "+pemFile)
    81  
    82  	err = rootCA.SignCertificate(certDir, "empty/CA", ecPubKey)
    83  	assert.Error(t, err, "Bad name should fail")
    84  
    85  	// use an empty CA to test error path
    86  	badCA := &ca.CA{
    87  		Name:     "badCA",
    88  		SignCert: &x509.Certificate{},
    89  	}
    90  	err = badCA.SignCertificate(certDir, testName, &ecdsa.PublicKey{})
    91  	assert.Error(t, err, "Empty CA should not be able to sign")
    92  	cleanup(testDir)
    93  
    94  }
    95  
    96  func cleanup(dir string) {
    97  	os.RemoveAll(dir)
    98  }
    99  
   100  func checkForFile(file string) bool {
   101  	if _, err := os.Stat(file); os.IsNotExist(err) {
   102  		return false
   103  	}
   104  	return true
   105  }