github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/storage/mru/lookup_test.go (about) 1 package mru 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/FusionFoundation/efsn/common/hexutil" 8 ) 9 10 func getTestUpdateLookup() *UpdateLookup { 11 metadata := *getTestMetadata() 12 rootAddr, _, _, _ := metadata.serializeAndHash() 13 return &UpdateLookup{ 14 period: 79, 15 version: 2010, 16 rootAddr: rootAddr, 17 } 18 } 19 20 func compareUpdateLookup(a, b *UpdateLookup) bool { 21 return a.version == b.version && 22 a.period == b.period && 23 bytes.Equal(a.rootAddr, b.rootAddr) 24 } 25 26 func TestUpdateLookupUpdateAddr(t *testing.T) { 27 ul := getTestUpdateLookup() 28 updateAddr := ul.UpdateAddr() 29 compareByteSliceToExpectedHex(t, "updateAddr", updateAddr, "0x8fbc8d4777ef6da790257eda80ab4321fabd08cbdbe67e4e3da6caca386d64e0") 30 } 31 32 func TestUpdateLookupSerializer(t *testing.T) { 33 serializedUpdateLookup := make([]byte, updateLookupLength) 34 ul := getTestUpdateLookup() 35 if err := ul.binaryPut(serializedUpdateLookup); err != nil { 36 t.Fatal(err) 37 } 38 compareByteSliceToExpectedHex(t, "serializedUpdateLookup", serializedUpdateLookup, "0x4f000000da070000fb0ed7efa696bdb0b54cd75554cc3117ffc891454317df7dd6fefad978e2f2fb") 39 40 // set receiving slice to the wrong size 41 serializedUpdateLookup = make([]byte, updateLookupLength+7) 42 if err := ul.binaryPut(serializedUpdateLookup); err == nil { 43 t.Fatalf("Expected UpdateLookup.binaryPut to fail when receiving slice has a length != %d", updateLookupLength) 44 } 45 46 // set rootAddr to an invalid length 47 ul.rootAddr = []byte{1, 2, 3, 4} 48 serializedUpdateLookup = make([]byte, updateLookupLength) 49 if err := ul.binaryPut(serializedUpdateLookup); err == nil { 50 t.Fatal("Expected UpdateLookup.binaryPut to fail when rootAddr is not of the correct size") 51 } 52 } 53 54 func TestUpdateLookupDeserializer(t *testing.T) { 55 serializedUpdateLookup, _ := hexutil.Decode("0x4f000000da070000fb0ed7efa696bdb0b54cd75554cc3117ffc891454317df7dd6fefad978e2f2fb") 56 var recoveredUpdateLookup UpdateLookup 57 if err := recoveredUpdateLookup.binaryGet(serializedUpdateLookup); err != nil { 58 t.Fatal(err) 59 } 60 originalUpdateLookup := *getTestUpdateLookup() 61 if !compareUpdateLookup(&originalUpdateLookup, &recoveredUpdateLookup) { 62 t.Fatalf("Expected recovered UpdateLookup to match") 63 } 64 65 // set source slice to the wrong size 66 serializedUpdateLookup = make([]byte, updateLookupLength+4) 67 if err := recoveredUpdateLookup.binaryGet(serializedUpdateLookup); err == nil { 68 t.Fatalf("Expected UpdateLookup.binaryGet to fail when source slice has a length != %d", updateLookupLength) 69 } 70 } 71 72 func TestUpdateLookupSerializeDeserialize(t *testing.T) { 73 serializedUpdateLookup := make([]byte, updateLookupLength) 74 originalUpdateLookup := getTestUpdateLookup() 75 if err := originalUpdateLookup.binaryPut(serializedUpdateLookup); err != nil { 76 t.Fatal(err) 77 } 78 var recoveredUpdateLookup UpdateLookup 79 if err := recoveredUpdateLookup.binaryGet(serializedUpdateLookup); err != nil { 80 t.Fatal(err) 81 } 82 if !compareUpdateLookup(originalUpdateLookup, &recoveredUpdateLookup) { 83 t.Fatalf("Expected recovered UpdateLookup to match") 84 } 85 }