github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/ledger/util/util_test.go (about)

     1  /*
     2  Copyright IBM Corp. 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 util
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestBasicEncodingDecoding(t *testing.T) {
    29  	for i := 0; i < 10000; i++ {
    30  		value := EncodeOrderPreservingVarUint64(uint64(i))
    31  		nextValue := EncodeOrderPreservingVarUint64(uint64(i + 1))
    32  		if !(bytes.Compare(value, nextValue) < 0) {
    33  			t.Fatalf("A smaller integer should result into smaller bytes. Encoded bytes for [%d] is [%x] and for [%d] is [%x]",
    34  				i, i+1, value, nextValue)
    35  		}
    36  		decodedValue, _, err := DecodeOrderPreservingVarUint64(value)
    37  		assert.NoError(t, err, "Error via calling DecodeOrderPreservingVarUint64")
    38  		if decodedValue != uint64(i) {
    39  			t.Fatalf("Value not same after decoding. Original value = [%d], decode value = [%d]", i, decodedValue)
    40  		}
    41  	}
    42  }
    43  
    44  func TestDecodingAppendedValues(t *testing.T) {
    45  	appendedValues := []byte{}
    46  	for i := 0; i < 1000; i++ {
    47  		appendedValues = append(appendedValues, EncodeOrderPreservingVarUint64(uint64(i))...)
    48  	}
    49  
    50  	len := 0
    51  	value := uint64(0)
    52  	var err error
    53  	for i := 0; i < 1000; i++ {
    54  		appendedValues = appendedValues[len:]
    55  		value, len, err = DecodeOrderPreservingVarUint64(appendedValues)
    56  		assert.NoError(t, err, "Error via calling DecodeOrderPreservingVarUint64")
    57  		if value != uint64(i) {
    58  			t.Fatalf("expected value = [%d], decode value = [%d]", i, value)
    59  		}
    60  	}
    61  }
    62  
    63  func TestDecodingBadInputBytes(t *testing.T) {
    64  	// error case when num consumed bytes > 1
    65  	sizeBytes := proto.EncodeVarint(uint64(1000))
    66  	_, _, err := DecodeOrderPreservingVarUint64(sizeBytes)
    67  	assert.Equal(t, fmt.Sprintf("number of consumed bytes from DecodeVarint is invalid, expected 1, but got %d", len(sizeBytes)), err.Error())
    68  
    69  	// error case when decoding invalid bytes - trim off last byte
    70  	invalidSizeBytes := sizeBytes[0 : len(sizeBytes)-1]
    71  	_, _, err = DecodeOrderPreservingVarUint64(invalidSizeBytes)
    72  	assert.Equal(t, "number of consumed bytes from DecodeVarint is invalid, expected 1, but got 0", err.Error())
    73  
    74  	// error case when size is more than available bytes
    75  	inputBytes := proto.EncodeVarint(uint64(8))
    76  	_, _, err = DecodeOrderPreservingVarUint64(inputBytes)
    77  	assert.Equal(t, "decoded size (8) from DecodeVarint is more than available bytes (0)", err.Error())
    78  
    79  	// error case when size is greater than 8
    80  	bigSizeBytes := proto.EncodeVarint(uint64(12))
    81  	_, _, err = DecodeOrderPreservingVarUint64(bigSizeBytes)
    82  	assert.Equal(t, "decoded size from DecodeVarint is invalid, expected <=8, but got 12", err.Error())
    83  }