github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/scc/qscc/query_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 qscc 17 18 import ( 19 "fmt" 20 "os" 21 "testing" 22 23 "github.com/spf13/viper" 24 25 "strings" 26 27 "github.com/hyperledger/fabric/common/policies" 28 "github.com/hyperledger/fabric/core/chaincode/shim" 29 "github.com/hyperledger/fabric/core/peer" 30 "github.com/hyperledger/fabric/core/policy" 31 peer2 "github.com/hyperledger/fabric/protos/peer" 32 "github.com/hyperledger/fabric/protos/utils" 33 "github.com/stretchr/testify/assert" 34 ) 35 36 func TestInit(t *testing.T) { 37 viper.Set("peer.fileSystemPath", "/var/hyperledger/test1/") 38 defer os.RemoveAll("/var/hyperledger/test1/") 39 peer.MockInitialize() 40 peer.MockCreateChain("mytestchainid1") 41 42 e := new(LedgerQuerier) 43 stub := shim.NewMockStub("LedgerQuerier", e) 44 45 if res := stub.MockInit("1", nil); res.Status != shim.OK { 46 fmt.Println("Init failed", string(res.Message)) 47 t.FailNow() 48 } 49 } 50 51 func TestQueryGetChainInfo(t *testing.T) { 52 viper.Set("peer.fileSystemPath", "/var/hyperledger/test2/") 53 defer os.RemoveAll("/var/hyperledger/test2/") 54 peer.MockInitialize() 55 peer.MockCreateChain("mytestchainid2") 56 57 e := new(LedgerQuerier) 58 stub := shim.NewMockStub("LedgerQuerier", e) 59 60 if res := stub.MockInit("1", nil); res.Status != shim.OK { 61 fmt.Println("Init failed", string(res.Message)) 62 t.FailNow() 63 } 64 65 args := [][]byte{[]byte(GetChainInfo), []byte("mytestchainid2")} 66 if res := stub.MockInvoke("2", args); res.Status != shim.OK { 67 t.Fatalf("qscc GetChainInfo failed with err: %s", res.Message) 68 } 69 } 70 71 func TestQueryGetTransactionByID(t *testing.T) { 72 viper.Set("peer.fileSystemPath", "/var/hyperledger/test3/") 73 defer os.RemoveAll("/var/hyperledger/test3/") 74 peer.MockInitialize() 75 peer.MockCreateChain("mytestchainid3") 76 77 e := new(LedgerQuerier) 78 stub := shim.NewMockStub("LedgerQuerier", e) 79 80 if res := stub.MockInit("1", nil); res.Status != shim.OK { 81 fmt.Println("Init failed", string(res.Message)) 82 t.FailNow() 83 } 84 85 args := [][]byte{[]byte(GetTransactionByID), []byte("mytestchainid3"), []byte("1")} 86 if res := stub.MockInvoke("2", args); res.Status == shim.OK { 87 t.Fatal("qscc getTransactionByID should have failed with invalid txid: 2") 88 } 89 } 90 91 func TestQueryWithWrongParameters(t *testing.T) { 92 viper.Set("peer.fileSystemPath", "/var/hyperledger/test4/") 93 defer os.RemoveAll("/var/hyperledger/test4/") 94 peer.MockInitialize() 95 peer.MockCreateChain("mytestchainid4") 96 97 e := new(LedgerQuerier) 98 stub := shim.NewMockStub("LedgerQuerier", e) 99 100 if res := stub.MockInit("1", nil); res.Status != shim.OK { 101 fmt.Println("Init failed", string(res.Message)) 102 t.FailNow() 103 } 104 105 // Test with wrong number of parameters 106 args := [][]byte{[]byte(GetTransactionByID), []byte("mytestchainid4")} 107 if res := stub.MockInvoke("2", args); res.Status == shim.OK { 108 t.Fatal("qscc getTransactionByID should have failed with invalid txid: 2") 109 } 110 } 111 112 func TestQueryGetBlockByNumber(t *testing.T) { 113 //t.Skip() 114 viper.Set("peer.fileSystemPath", "/var/hyperledger/test5/") 115 defer os.RemoveAll("/var/hyperledger/test5/") 116 peer.MockInitialize() 117 peer.MockCreateChain("mytestchainid5") 118 119 e := new(LedgerQuerier) 120 stub := shim.NewMockStub("LedgerQuerier", e) 121 122 if res := stub.MockInit("1", nil); res.Status != shim.OK { 123 fmt.Println("Init failed", string(res.Message)) 124 t.FailNow() 125 } 126 // block number 0 (genesis block) would already be present in the ledger 127 args := [][]byte{[]byte(GetBlockByNumber), []byte("mytestchainid5"), []byte("1")} 128 if res := stub.MockInvoke("2", args); res.Status == shim.OK { 129 t.Fatal("qscc GetBlockByNumber should have failed with invalid number: 1") 130 } 131 } 132 133 func TestQueryGetBlockByHash(t *testing.T) { 134 viper.Set("peer.fileSystemPath", "/var/hyperledger/test6/") 135 defer os.RemoveAll("/var/hyperledger/test6/") 136 peer.MockInitialize() 137 peer.MockCreateChain("mytestchainid6") 138 139 e := new(LedgerQuerier) 140 stub := shim.NewMockStub("LedgerQuerier", e) 141 142 if res := stub.MockInit("1", nil); res.Status != shim.OK { 143 fmt.Println("Init failed", string(res.Message)) 144 t.FailNow() 145 } 146 147 args := [][]byte{[]byte(GetBlockByHash), []byte("mytestchainid6"), []byte("0")} 148 if res := stub.MockInvoke("2", args); res.Status == shim.OK { 149 t.Fatal("qscc GetBlockByHash should have failed with invalid hash: 0") 150 } 151 } 152 153 func TestQueryGetBlockByTxID(t *testing.T) { 154 viper.Set("peer.fileSystemPath", "/var/hyperledger/test8/") 155 defer os.RemoveAll("/var/hyperledger/test8/") 156 peer.MockInitialize() 157 peer.MockCreateChain("mytestchainid8") 158 159 e := new(LedgerQuerier) 160 stub := shim.NewMockStub("LedgerQuerier", e) 161 162 txID := "" 163 164 if res := stub.MockInit("1", nil); res.Status != shim.OK { 165 fmt.Println("Init failed", string(res.Message)) 166 t.FailNow() 167 } 168 169 args := [][]byte{[]byte(GetBlockByTxID), []byte("mytestchainid8"), []byte(txID)} 170 if res := stub.MockInvoke("2", args); res.Status == shim.OK { 171 t.Fatalf("qscc GetBlockByTxID should have failed with invalid txID: %s", txID) 172 } 173 } 174 175 func TestFailingAccessControl(t *testing.T) { 176 viper.Set("peer.fileSystemPath", "/var/hyperledger/test9/") 177 defer os.RemoveAll("/var/hyperledger/test9/") 178 peer.MockInitialize() 179 peer.MockCreateChain("mytestchainid9") 180 181 e := new(LedgerQuerier) 182 // Init the policy checker to have a failure 183 policyManagerGetter := &policy.MockChannelPolicyManagerGetter{ 184 Managers: map[string]policies.Manager{ 185 "mytestchainid9": &policy.MockChannelPolicyManager{MockPolicy: &policy.MockPolicy{Deserializer: &policy.MockIdentityDeserializer{[]byte("Alice"), []byte("msg1")}}}, 186 }, 187 } 188 189 e.policyChecker = policy.NewPolicyChecker( 190 policyManagerGetter, 191 &policy.MockIdentityDeserializer{[]byte("Alice"), []byte("msg1")}, 192 &policy.MockMSPPrincipalGetter{Principal: []byte("Alice")}, 193 ) 194 195 stub := shim.NewMockStub("LedgerQuerier", e) 196 197 args := [][]byte{[]byte(GetChainInfo), []byte("mytestchainid9")} 198 sProp, _ := utils.MockSignedEndorserProposalOrPanic("mytestchainid9", &peer2.ChaincodeSpec{}, []byte("Alice"), []byte("msg1")) 199 policyManagerGetter.Managers["mytestchainid9"].(*policy.MockChannelPolicyManager).MockPolicy.(*policy.MockPolicy).Deserializer.(*policy.MockIdentityDeserializer).Msg = sProp.ProposalBytes 200 sProp.Signature = sProp.ProposalBytes 201 if res := stub.MockInvokeWithSignedProposal("2", args, sProp); res.Status != shim.OK { 202 t.Fatalf("qscc GetChainInfo failed with err: %s", res.Message) 203 } 204 205 sProp, _ = utils.MockSignedEndorserProposalOrPanic("mytestchainid9", &peer2.ChaincodeSpec{}, []byte("Bob"), []byte("msg2")) 206 res := stub.MockInvokeWithSignedProposal("3", args, sProp) 207 if res.Status == shim.OK { 208 t.Fatalf("qscc GetChainInfo must fail: %s", res.Message) 209 } 210 assert.True(t, strings.HasPrefix(res.Message, "Authorization request failed")) 211 }