github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/ledger/blkstorage/fsblkstorage/fs_blockstore_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package fsblkstorage 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/hyperledger/fabric/common/ledger/testutil" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 ) 17 18 func TestWrongBlockNumber(t *testing.T) { 19 env := newTestEnv(t, NewConf(testPath(), 0)) 20 defer env.Cleanup() 21 22 provider := env.provider 23 store, _ := provider.OpenBlockStore("testLedger") 24 defer store.Shutdown() 25 26 blocks := testutil.ConstructTestBlocks(t, 5) 27 for i := 0; i < 3; i++ { 28 err := store.AddBlock(blocks[i]) 29 assert.NoError(t, err) 30 } 31 err := store.AddBlock(blocks[4]) 32 assert.Error(t, err, "Error shold have been thrown when adding block number 4 while block number 3 is expected") 33 } 34 35 func TestTxIDIndexErrorPropagations(t *testing.T) { 36 env := newTestEnv(t, NewConf(testPath(), 0)) 37 defer env.Cleanup() 38 39 provider := env.provider 40 store, _ := provider.OpenBlockStore("testLedger") 41 defer store.Shutdown() 42 blocks := testutil.ConstructTestBlocks(t, 3) 43 for i := 0; i < 3; i++ { 44 err := store.AddBlock(blocks[i]) 45 assert.NoError(t, err) 46 } 47 48 fsStore := store.(*fsBlockStore) 49 index := fsStore.fileMgr.db 50 51 txIDBasedFunctions := []func() error{ 52 func() error { 53 _, err := fsStore.RetrieveTxByID("junkTxID") 54 return err 55 }, 56 func() error { 57 _, err := fsStore.RetrieveBlockByTxID("junkTxID") 58 return err 59 }, 60 func() error { 61 _, err := fsStore.RetrieveTxValidationCodeByTxID("junkTxID") 62 return err 63 }, 64 } 65 66 index.Put( 67 constructTxIDKey("junkTxID", 5, 4), 68 []byte("junkValue"), 69 false, 70 ) 71 expectedErrMsg := fmt.Sprintf("unexpected error while unmarshaling bytes [%#v] into TxIDIndexValProto:", []byte("junkValue")) 72 for _, f := range txIDBasedFunctions { 73 err := f() 74 require.Error(t, err) 75 require.Contains(t, err.Error(), expectedErrMsg) 76 } 77 78 env.provider.leveldbProvider.Close() 79 expectedErrMsg = "error while trying to retrieve transaction info by TXID [junkTxID]:" 80 for _, f := range txIDBasedFunctions { 81 err := f() 82 require.Error(t, err) 83 require.Contains(t, err.Error(), expectedErrMsg) 84 } 85 }