github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/swarm/storage/feed/binaryserializer_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-dubxcoin library.
     3  //
     4  // The go-dubxcoin library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-dubxcoin library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-dubxcoin library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package feed
    18  
    19  import (
    20  	"encoding/json"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/alexdevranger/node-1.8.27/common/hexutil"
    25  )
    26  
    27  // KV mocks a key value store
    28  type KV map[string]string
    29  
    30  func (kv KV) Get(key string) string {
    31  	return kv[key]
    32  }
    33  func (kv KV) Set(key, value string) {
    34  	kv[key] = value
    35  }
    36  
    37  func compareByteSliceToExpectedHex(t *testing.T, variableName string, actualValue []byte, expectedHex string) {
    38  	if hexutil.Encode(actualValue) != expectedHex {
    39  		t.Fatalf("%s: Expected %s to be %s, got %s", t.Name(), variableName, expectedHex, hexutil.Encode(actualValue))
    40  	}
    41  }
    42  
    43  func testBinarySerializerRecovery(t *testing.T, bin binarySerializer, expectedHex string) {
    44  	name := reflect.TypeOf(bin).Elem().Name()
    45  	serialized := make([]byte, bin.binaryLength())
    46  	if err := bin.binaryPut(serialized); err != nil {
    47  		t.Fatalf("%s.binaryPut error when trying to serialize structure: %s", name, err)
    48  	}
    49  
    50  	compareByteSliceToExpectedHex(t, name, serialized, expectedHex)
    51  
    52  	recovered := reflect.New(reflect.TypeOf(bin).Elem()).Interface().(binarySerializer)
    53  	if err := recovered.binaryGet(serialized); err != nil {
    54  		t.Fatalf("%s.binaryGet error when trying to deserialize structure: %s", name, err)
    55  	}
    56  
    57  	if !reflect.DeepEqual(bin, recovered) {
    58  		t.Fatalf("Expected that the recovered %s equals the marshalled %s", name, name)
    59  	}
    60  
    61  	serializedWrongLength := make([]byte, 1)
    62  	copy(serializedWrongLength[:], serialized)
    63  	if err := recovered.binaryGet(serializedWrongLength); err == nil {
    64  		t.Fatalf("Expected %s.binaryGet to fail since data is too small", name)
    65  	}
    66  }
    67  
    68  func testBinarySerializerLengthCheck(t *testing.T, bin binarySerializer) {
    69  	name := reflect.TypeOf(bin).Elem().Name()
    70  	// make a slice that is too small to contain the metadata
    71  	serialized := make([]byte, bin.binaryLength()-1)
    72  
    73  	if err := bin.binaryPut(serialized); err == nil {
    74  		t.Fatalf("Expected %s.binaryPut to fail, since target slice is too small", name)
    75  	}
    76  }
    77  
    78  func testValueSerializer(t *testing.T, v valueSerializer, expected KV) {
    79  	name := reflect.TypeOf(v).Elem().Name()
    80  	kv := make(KV)
    81  
    82  	v.AppendValues(kv)
    83  	if !reflect.DeepEqual(expected, kv) {
    84  		expj, _ := json.Marshal(expected)
    85  		gotj, _ := json.Marshal(kv)
    86  		t.Fatalf("Expected %s.AppendValues to return %s, got %s", name, string(expj), string(gotj))
    87  	}
    88  
    89  	recovered := reflect.New(reflect.TypeOf(v).Elem()).Interface().(valueSerializer)
    90  	err := recovered.FromValues(kv)
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	if !reflect.DeepEqual(recovered, v) {
    96  		t.Fatalf("Expected recovered %s to be the same", name)
    97  	}
    98  }