github.com/dddengyunjie/fabric-ca@v0.0.0-20190606043049-92df60ae2f0f/lib/tcert/keytree_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 tcert
    18  
    19  import (
    20  	"bytes"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/cloudflare/cfssl/log"
    27  	"github.com/hyperledger/fabric/bccsp"
    28  	"github.com/hyperledger/fabric/bccsp/factory"
    29  )
    30  
    31  func TestMain(m *testing.M) {
    32  	flag.Parse()
    33  
    34  	err := factory.InitFactories(nil)
    35  	if err != nil {
    36  		panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
    37  	}
    38  	os.Exit(m.Run())
    39  }
    40  
    41  func TestKeyTree(t *testing.T) {
    42  
    43  	log.Level = log.LevelDebug
    44  
    45  	path := []string{"A", "B", "C"}
    46  
    47  	csp := factory.GetDefault()
    48  	opts := &bccsp.AES256KeyGenOpts{Temporary: true}
    49  	rootKey, err := csp.KeyGen(opts)
    50  	if err != nil {
    51  		t.Fatalf("Failed to create root key: %s", err)
    52  	}
    53  
    54  	tree1 := NewKeyTree(csp, rootKey)
    55  	key1, err := tree1.GetKey(path)
    56  	if err != nil {
    57  		t.Fatalf("Failed to get key1: %s", err)
    58  	}
    59  
    60  	tree2 := NewKeyTree(csp, rootKey)
    61  	key2, err := tree2.GetKey(path)
    62  	if err != nil {
    63  		t.Fatalf("Failed to get key2: %s", err)
    64  	}
    65  
    66  	keyA, err := tree2.GetKey(path)
    67  	t.Logf("keyA %v", keyA)
    68  	if err != nil {
    69  		t.Fatalf("Failed to get keyA: %s", err)
    70  	}
    71  
    72  	ski1 := key1.SKI()
    73  	ski2 := key2.SKI()
    74  	if !bytes.Equal(ski1, ski2) {
    75  		t.Errorf("keys are not equal %s != %s", ski1, ski2)
    76  	}
    77  
    78  }