github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/storageccl/engineccl/rocksdb_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Licensed as a CockroachDB Enterprise file under the Cockroach Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //     https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
     8  
     9  package engineccl
    10  
    11  import (
    12  	"testing"
    13  
    14  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    15  	"github.com/cockroachdb/cockroach/pkg/storage"
    16  	"github.com/cockroachdb/cockroach/pkg/testutils"
    17  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    18  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    19  )
    20  
    21  func TestVerifyBatchRepr(t *testing.T) {
    22  	defer leaktest.AfterTest(t)()
    23  
    24  	keyA := storage.MVCCKey{Key: []byte("a")}
    25  	keyB := storage.MVCCKey{Key: []byte("b")}
    26  	keyC := storage.MVCCKey{Key: []byte("c")}
    27  	keyD := storage.MVCCKey{Key: []byte("d")}
    28  	keyE := storage.MVCCKey{Key: []byte("e")}
    29  
    30  	var batch storage.RocksDBBatchBuilder
    31  	key := storage.MVCCKey{Key: []byte("bb"), Timestamp: hlc.Timestamp{WallTime: 1}}
    32  	batch.Put(key, roachpb.MakeValueFromString("1").RawBytes)
    33  	data := batch.Finish()
    34  
    35  	ms, err := VerifyBatchRepr(data, keyB, keyC, 0)
    36  	if err != nil {
    37  		t.Fatalf("%+v", err)
    38  	}
    39  	if ms.KeyCount != 1 {
    40  		t.Fatalf("got %d expected 1", ms.KeyCount)
    41  	}
    42  
    43  	// Key is before the range in the request span.
    44  	if _, err := VerifyBatchRepr(data, keyD, keyE, 0); !testutils.IsError(err, "request range") {
    45  		t.Fatalf("expected request range error got: %+v", err)
    46  	}
    47  	// Key is after the range in the request span.
    48  	if _, err := VerifyBatchRepr(data, keyA, keyB, 0); !testutils.IsError(err, "request range") {
    49  		t.Fatalf("expected request range error got: %+v", err)
    50  	}
    51  
    52  	// Invalid key/value entry checksum.
    53  	{
    54  		var batch storage.RocksDBBatchBuilder
    55  		key := storage.MVCCKey{Key: []byte("bb"), Timestamp: hlc.Timestamp{WallTime: 1}}
    56  		value := roachpb.MakeValueFromString("1")
    57  		value.InitChecksum([]byte("foo"))
    58  		batch.Put(key, value.RawBytes)
    59  		data := batch.Finish()
    60  
    61  		if _, err := VerifyBatchRepr(data, keyB, keyC, 0); !testutils.IsError(err, "invalid checksum") {
    62  			t.Fatalf("expected 'invalid checksum' error got: %+v", err)
    63  		}
    64  	}
    65  }