github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/storage/mru/metadata_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  package mru
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/FusionFoundation/efsn/common/hexutil"
    22  )
    23  
    24  func compareByteSliceToExpectedHex(t *testing.T, variableName string, actualValue []byte, expectedHex string) {
    25  	if hexutil.Encode(actualValue) != expectedHex {
    26  		t.Fatalf("%s: Expected %s to be %s, got %s", t.Name(), variableName, expectedHex, hexutil.Encode(actualValue))
    27  	}
    28  }
    29  
    30  func getTestMetadata() *ResourceMetadata {
    31  	return &ResourceMetadata{
    32  		Name: "world news report, every hour, on the hour",
    33  		StartTime: Timestamp{
    34  			Time: 1528880400,
    35  		},
    36  		Frequency: 3600,
    37  		Owner:     newCharlieSigner().Address(),
    38  	}
    39  }
    40  
    41  func TestMetadataSerializerDeserializer(t *testing.T) {
    42  	metadata := *getTestMetadata()
    43  
    44  	rootAddr, metaHash, chunkData, err := metadata.serializeAndHash() // creates hashes and marshals, in one go
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	const expectedRootAddr = "0xfb0ed7efa696bdb0b54cd75554cc3117ffc891454317df7dd6fefad978e2f2fb"
    49  	const expectedMetaHash = "0xf74a10ce8f26ffc8bfaa07c3031a34b2c61f517955e7deb1592daccf96c69cf0"
    50  	const expectedChunkData = "0x00004f0010dd205b00000000100e0000000000002a776f726c64206e657773207265706f72742c20657665727920686f75722c206f6e2074686520686f7572876a8936a7cd0b79ef0735ad0896c1afe278781c"
    51  
    52  	compareByteSliceToExpectedHex(t, "rootAddr", rootAddr, expectedRootAddr)
    53  	compareByteSliceToExpectedHex(t, "metaHash", metaHash, expectedMetaHash)
    54  	compareByteSliceToExpectedHex(t, "chunkData", chunkData, expectedChunkData)
    55  
    56  	recoveredMetadata := ResourceMetadata{}
    57  	recoveredMetadata.binaryGet(chunkData)
    58  
    59  	if recoveredMetadata != metadata {
    60  		t.Fatalf("Expected that the recovered metadata equals the marshalled metadata")
    61  	}
    62  
    63  	// we are going to mess with the data, so create a backup to go back to it for the next test
    64  	backup := make([]byte, len(chunkData))
    65  	copy(backup, chunkData)
    66  
    67  	chunkData = []byte{1, 2, 3}
    68  	if err := recoveredMetadata.binaryGet(chunkData); err == nil {
    69  		t.Fatal("Expected binaryGet to fail since chunk is too small")
    70  	}
    71  
    72  	// restore backup
    73  	chunkData = make([]byte, len(backup))
    74  	copy(chunkData, backup)
    75  
    76  	// mess with the prefix so it is not zero
    77  	chunkData[0] = 7
    78  	chunkData[1] = 9
    79  
    80  	if err := recoveredMetadata.binaryGet(chunkData); err == nil {
    81  		t.Fatal("Expected binaryGet to fail since prefix bytes are not zero")
    82  	}
    83  
    84  	// restore backup
    85  	chunkData = make([]byte, len(backup))
    86  	copy(chunkData, backup)
    87  
    88  	// mess with the length header to trigger an error
    89  	chunkData[2] = 255
    90  	chunkData[3] = 44
    91  	if err := recoveredMetadata.binaryGet(chunkData); err == nil {
    92  		t.Fatal("Expected binaryGet to fail since header length does not match")
    93  	}
    94  
    95  	// restore backup
    96  	chunkData = make([]byte, len(backup))
    97  	copy(chunkData, backup)
    98  
    99  	// mess with name length header to trigger a chunk too short error
   100  	chunkData[20] = 255
   101  	if err := recoveredMetadata.binaryGet(chunkData); err == nil {
   102  		t.Fatal("Expected binaryGet to fail since name length is incorrect")
   103  	}
   104  
   105  	// restore backup
   106  	chunkData = make([]byte, len(backup))
   107  	copy(chunkData, backup)
   108  
   109  	// mess with name length header to trigger an leftover bytes to read error
   110  	chunkData[20] = 3
   111  	if err := recoveredMetadata.binaryGet(chunkData); err == nil {
   112  		t.Fatal("Expected binaryGet to fail since name length is too small")
   113  	}
   114  }
   115  
   116  func TestMetadataSerializerLengthCheck(t *testing.T) {
   117  	metadata := *getTestMetadata()
   118  
   119  	// make a slice that is too small to contain the metadata
   120  	serializedMetadata := make([]byte, 4)
   121  
   122  	if err := metadata.binaryPut(serializedMetadata); err == nil {
   123  		t.Fatal("Expected metadata.binaryPut to fail, since target slice is too small")
   124  	}
   125  
   126  }