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

     1  package mru
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/FusionFoundation/efsn/common/hexutil"
     8  )
     9  
    10  const serializedUpdateHeaderMultihashHex = "0x4f000000da070000fb0ed7efa696bdb0b54cd75554cc3117ffc891454317df7dd6fefad978e2f2fbf74a10ce8f26ffc8bfaa07c3031a34b2c61f517955e7deb1592daccf96c69cf001"
    11  
    12  func getTestUpdateHeader(multihash bool) (header *updateHeader) {
    13  	_, metaHash, _, _ := getTestMetadata().serializeAndHash()
    14  	return &updateHeader{
    15  		UpdateLookup: *getTestUpdateLookup(),
    16  		multihash:    multihash,
    17  		metaHash:     metaHash,
    18  	}
    19  }
    20  
    21  func compareUpdateHeader(a, b *updateHeader) bool {
    22  	return compareUpdateLookup(&a.UpdateLookup, &b.UpdateLookup) &&
    23  		a.multihash == b.multihash &&
    24  		bytes.Equal(a.metaHash, b.metaHash)
    25  }
    26  
    27  func TestUpdateHeaderSerializer(t *testing.T) {
    28  	header := getTestUpdateHeader(true)
    29  	serializedHeader := make([]byte, updateHeaderLength)
    30  	if err := header.binaryPut(serializedHeader); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	compareByteSliceToExpectedHex(t, "serializedHeader", serializedHeader, serializedUpdateHeaderMultihashHex)
    34  
    35  	// trigger incorrect slice length error passing a slice that is 1 byte too big
    36  	if err := header.binaryPut(make([]byte, updateHeaderLength+1)); err == nil {
    37  		t.Fatal("Expected updateHeader.binaryPut to fail since supplied slice is of incorrect length")
    38  	}
    39  
    40  	// trigger invalid metaHash error
    41  	header.metaHash = nil
    42  	if err := header.binaryPut(serializedHeader); err == nil {
    43  		t.Fatal("Expected updateHeader.binaryPut to fail metaHash is of incorrect length")
    44  	}
    45  }
    46  
    47  func TestUpdateHeaderDeserializer(t *testing.T) {
    48  	originalUpdate := getTestUpdateHeader(true)
    49  	serializedData, _ := hexutil.Decode(serializedUpdateHeaderMultihashHex)
    50  	var retrievedUpdate updateHeader
    51  	if err := retrievedUpdate.binaryGet(serializedData); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	if !compareUpdateHeader(originalUpdate, &retrievedUpdate) {
    55  		t.Fatalf("Expected deserialized structure to equal the original")
    56  	}
    57  
    58  	// mess with source slice to test length checks
    59  	serializedData = []byte{1, 2, 3}
    60  	if err := retrievedUpdate.binaryGet(serializedData); err == nil {
    61  		t.Fatal("Expected retrievedUpdate.binaryGet, since passed slice is too small")
    62  	}
    63  
    64  }