github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/sbft/backend/backend_test.go (about) 1 /* 2 Copyright Digital Asset Holdings, LLC 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 backend 18 19 import ( 20 "crypto/ecdsa" 21 "crypto/elliptic" 22 crand "crypto/rand" 23 "crypto/rsa" 24 "reflect" 25 "testing" 26 27 "github.com/golang/protobuf/proto" 28 "github.com/hyperledger/fabric/orderer/common/filter" 29 "github.com/hyperledger/fabric/orderer/mocks/multichain" 30 mc "github.com/hyperledger/fabric/orderer/multichain" 31 "github.com/hyperledger/fabric/orderer/sbft/simplebft" 32 cb "github.com/hyperledger/fabric/protos/common" 33 ) 34 35 func TestSignAndVerifyRsa(t *testing.T) { 36 data := []byte{1, 1, 1, 1, 1} 37 privateKey, err := rsa.GenerateKey(crand.Reader, 1024) 38 if err != nil { 39 panic("RSA failed to generate private key in test.") 40 } 41 s := Sign(privateKey, data) 42 if s == nil { 43 t.Error("Nil signature was generated.") 44 } 45 46 publicKey := privateKey.Public() 47 err = CheckSig(publicKey, data, s) 48 if err != nil { 49 t.Errorf("Signature check failed: %s", err) 50 } 51 } 52 53 func TestSignAndVerifyEcdsa(t *testing.T) { 54 data := []byte{1, 1, 1, 1, 1} 55 privateKey, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader) 56 if err != nil { 57 panic("ECDSA failed to generate private key in test.") 58 } 59 s := Sign(privateKey, data) 60 if s == nil { 61 t.Error("Nil signature was generated.") 62 } 63 64 publicKey := privateKey.Public() 65 err = CheckSig(publicKey, data, s) 66 if err != nil { 67 t.Errorf("Signature check failed: %s", err) 68 } 69 } 70 71 func TestLedgerReadWrite(t *testing.T) { 72 testChainID1 := "testID1" 73 testChainID2 := "testID2" 74 testChainID3 := "testID2" 75 b := Backend{supports: map[string]mc.ConsenterSupport{}, lastBatches: map[string]*simplebft.Batch{}} 76 77 b.supports[testChainID1] = &multichain.ConsenterSupport{Batches: make(chan []*cb.Envelope, 10)} 78 b.supports[testChainID2] = &multichain.ConsenterSupport{Batches: make(chan []*cb.Envelope, 10)} 79 b.supports[testChainID3] = &multichain.ConsenterSupport{Batches: make(chan []*cb.Envelope, 10)} 80 81 header := []byte("header") 82 e1 := &cb.Envelope{Payload: []byte("data1")} 83 e2 := &cb.Envelope{Payload: []byte("data2")} 84 ebytes1, _ := proto.Marshal(e1) 85 ebytes2, _ := proto.Marshal(e2) 86 data := [][]byte{ebytes1, ebytes2} 87 sgns := make(map[uint64][]byte) 88 sgns[uint64(1)] = []byte("sgn1") 89 sgns[uint64(22)] = []byte("sgn22") 90 batch := simplebft.Batch{Header: header, Payloads: data, Signatures: sgns} 91 92 b.Deliver(testChainID1, &batch, []filter.Committer{}) 93 batch1 := b.LastBatch(testChainID1) 94 batch2 := b.LastBatch(testChainID2) 95 b.Deliver(testChainID3, &batch, []filter.Committer{}) 96 batch3 := b.LastBatch(testChainID3) 97 98 if !reflect.DeepEqual(batch, *batch1) { 99 t.Errorf("The wrong batch was returned by LastBatch(chainID1) after Deliver: %v (original was: %v)", batch1, &batch) 100 } 101 if !reflect.DeepEqual(batch, *batch3) { 102 t.Errorf("The wrong batch was returned by LastBatch(chainID3) after Deliver: %v (original was: %v)", batch3, &batch) 103 } 104 if batch2 != nil { 105 t.Error("The second chain should be empty.") 106 } 107 } 108 109 func TestEncoderEncodesDecodesSgnsWithoutPanic(t *testing.T) { 110 defer func() { 111 if r := recover(); r != nil { 112 t.Errorf("Encoding/decoding failed for valid signatures, code panicked.") 113 } 114 }() 115 sgns1 := make(map[uint64][]byte) 116 e1 := encodeSignatures(sgns1) 117 118 sgns2 := make(map[uint64][]byte) 119 sgns2[uint64(1)] = []byte("sgn1") 120 e2 := encodeSignatures(sgns2) 121 122 sgns3 := make(map[uint64][]byte) 123 sgns3[uint64(22)] = []byte("sgn22") 124 sgns3[uint64(143)] = []byte("sgn22") 125 sgns3[uint64(200)] = []byte("sgn200") 126 e3 := encodeSignatures(sgns3) 127 128 rsgns1 := decodeSignatures(e1) 129 rsgns2 := decodeSignatures(e2) 130 rsgns3 := decodeSignatures(e3) 131 132 if !reflect.DeepEqual(sgns1, rsgns1) { 133 t.Errorf("Decoding error: %v (original: %v). (1)", rsgns1, sgns1) 134 } 135 if !reflect.DeepEqual(sgns2, rsgns2) { 136 t.Errorf("Decoding error: %v (original: %v). (2)", rsgns2, sgns2) 137 } 138 if !reflect.DeepEqual(sgns3, rsgns3) { 139 t.Errorf("Decoding error: %v (original: %v). (3)", rsgns3, sgns3) 140 } 141 }