1
1
/*
2
- Copyright IBM Corp. 2016 All Rights Reserved.
2
+ Copyright IBM Corp. All Rights Reserved.
3
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.
4
+ SPDX-License-Identifier: Apache-2.0
15
5
*/
16
6
17
7
package historyleveldb
@@ -22,6 +12,7 @@ import (
22
12
"testing"
23
13
24
14
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
15
+ "github.com/hyperledger/fabric/common/flogging"
25
16
"github.com/hyperledger/fabric/common/ledger/testutil"
26
17
util2 "github.com/hyperledger/fabric/common/util"
27
18
"github.com/hyperledger/fabric/core/ledger"
@@ -34,6 +25,8 @@ import (
34
25
35
26
func TestMain (m * testing.M ) {
36
27
viper .Set ("peer.fileSystemPath" , "/tmp/fabric/ledgertests/kvledger/history/historydb/historyleveldb" )
28
+ flogging .SetModuleLevel ("leveldbhelper" , "debug" )
29
+ flogging .SetModuleLevel ("historyleveldb" , "debug" )
37
30
os .Exit (m .Run ())
38
31
}
39
32
@@ -266,3 +259,81 @@ func TestGenesisBlockNoError(t *testing.T) {
266
259
err = env .testHistoryDB .Commit (block )
267
260
testutil .AssertNoError (t , err , "" )
268
261
}
262
+
263
+ // TestHistoryWithKeyContainingNilBytes tests historydb when keys contains nil bytes (FAB-11244) -
264
+ // which happens to be used as a separator in the composite keys that is formed for the entries in the historydb
265
+ func TestHistoryWithKeyContainingNilBytes (t * testing.T ) {
266
+ env := newTestHistoryEnv (t )
267
+ defer env .cleanup ()
268
+ provider := env .testBlockStorageEnv .provider
269
+ ledger1id := "ledger1"
270
+ store1 , err := provider .OpenBlockStore (ledger1id )
271
+ testutil .AssertNoError (t , err , "Error upon provider.OpenBlockStore()" )
272
+ defer store1 .Shutdown ()
273
+
274
+ bg , gb := testutil .NewBlockGenerator (t , ledger1id , false )
275
+ testutil .AssertNoError (t , store1 .AddBlock (gb ), "" )
276
+ testutil .AssertNoError (t , env .testHistoryDB .Commit (gb ), "" )
277
+
278
+ //block1
279
+ txid := util2 .GenerateUUID ()
280
+ simulator , _ := env .txmgr .NewTxSimulator (txid )
281
+ simulator .SetState ("ns1" , "key" , []byte ("value1" )) // add a key <key> that contains no nil byte
282
+ simulator .Done ()
283
+ simRes , _ := simulator .GetTxSimulationResults ()
284
+ pubSimResBytes , _ := simRes .GetPubSimulationBytes ()
285
+ block1 := bg .NextBlock ([][]byte {pubSimResBytes })
286
+ err = store1 .AddBlock (block1 )
287
+ testutil .AssertNoError (t , err , "" )
288
+ err = env .testHistoryDB .Commit (block1 )
289
+ testutil .AssertNoError (t , err , "" )
290
+
291
+ //block2 tran1
292
+ simulationResults := [][]byte {}
293
+ txid = util2 .GenerateUUID ()
294
+ simulator , _ = env .txmgr .NewTxSimulator (txid )
295
+ simulator .SetState ("ns1" , "key" , []byte ("value2" )) // add another value for the key <key>
296
+ simulator .Done ()
297
+ simRes , _ = simulator .GetTxSimulationResults ()
298
+ pubSimResBytes , _ = simRes .GetPubSimulationBytes ()
299
+ simulationResults = append (simulationResults , pubSimResBytes )
300
+
301
+ //block2 tran2
302
+ txid2 := util2 .GenerateUUID ()
303
+ simulator2 , _ := env .txmgr .NewTxSimulator (txid2 )
304
+ // add another key <key\x00\x01\x01\x15> that contains a nil byte - such that when a range query is formed using old format, this key falls in the range
305
+ simulator2 .SetState ("ns1" , "key\x00 \x01 \x01 \x15 " , []byte ("dummyVal1" ))
306
+ simulator2 .SetState ("ns1" , "\x00 key\x00 \x01 \x01 \x15 " , []byte ("dummyVal2" ))
307
+ simulator2 .Done ()
308
+ simRes2 , _ := simulator2 .GetTxSimulationResults ()
309
+ pubSimResBytes2 , _ := simRes2 .GetPubSimulationBytes ()
310
+ simulationResults = append (simulationResults , pubSimResBytes2 )
311
+ block2 := bg .NextBlock (simulationResults )
312
+ err = store1 .AddBlock (block2 )
313
+ testutil .AssertNoError (t , err , "" )
314
+ err = env .testHistoryDB .Commit (block2 )
315
+ testutil .AssertNoError (t , err , "" )
316
+
317
+ qhistory , err := env .testHistoryDB .NewHistoryQueryExecutor (store1 )
318
+ testutil .AssertNoError (t , err , "Error upon NewHistoryQueryExecutor" )
319
+ testutilVerifyResults (t , qhistory , "ns1" , "key" , []string {"value1" , "value2" })
320
+ testutilVerifyResults (t , qhistory , "ns1" , "key\x00 \x01 \x01 \x15 " , []string {"dummyVal1" })
321
+ testutilVerifyResults (t , qhistory , "ns1" , "\x00 key\x00 \x01 \x01 \x15 " , []string {"dummyVal2" })
322
+ }
323
+
324
+ func testutilVerifyResults (t * testing.T , hqe ledger.HistoryQueryExecutor , ns , key string , expectedVals []string ) {
325
+ itr , err := hqe .GetHistoryForKey (ns , key )
326
+ testutil .AssertNoError (t , err , "Error upon GetHistoryForKey()" )
327
+ retrievedVals := []string {}
328
+ for {
329
+ kmod , _ := itr .Next ()
330
+ if kmod == nil {
331
+ break
332
+ }
333
+ txid := kmod .(* queryresult.KeyModification ).TxId
334
+ retrievedValue := string (kmod .(* queryresult.KeyModification ).Value )
335
+ retrievedVals = append (retrievedVals , retrievedValue )
336
+ t .Logf ("Retrieved history record at TxId=%s with value %s" , txid , retrievedValue )
337
+ }
338
+ testutil .AssertEquals (t , retrievedVals , expectedVals )
339
+ }
0 commit comments