github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/swarm/storage/mru/update_test.go (about) 1 package mru 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 const serializedUpdateHex = "0x490034004f000000da070000fb0ed7efa696bdb0b54cd75554cc3117ffc891454317df7dd6fefad978e2f2fbf74a10ce8f26ffc8bfaa07c3031a34b2c61f517955e7deb1592daccf96c69cf000456c20717565206c6565206d7563686f207920616e6461206d7563686f2c207665206d7563686f20792073616265206d7563686f" 9 const serializedUpdateMultihashHex = "0x490022004f000000da070000fb0ed7efa696bdb0b54cd75554cc3117ffc891454317df7dd6fefad978e2f2fbf74a10ce8f26ffc8bfaa07c3031a34b2c61f517955e7deb1592daccf96c69cf0011b200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1c1e1f20" 10 11 func getTestResourceUpdate() *resourceUpdate { 12 return &resourceUpdate{ 13 updateHeader: *getTestUpdateHeader(false), 14 data: []byte("El que lee mucho y anda mucho, ve mucho y sabe mucho"), 15 } 16 } 17 18 func getTestResourceUpdateMultihash() *resourceUpdate { 19 return &resourceUpdate{ 20 updateHeader: *getTestUpdateHeader(true), 21 data: []byte{0x1b, 0x20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 30, 31, 32}, 22 } 23 } 24 25 func compareResourceUpdate(a, b *resourceUpdate) bool { 26 return compareUpdateHeader(&a.updateHeader, &b.updateHeader) && 27 bytes.Equal(a.data, b.data) 28 } 29 30 func TestResourceUpdateSerializer(t *testing.T) { 31 var serializedUpdateLength = len(serializedUpdateHex)/2 - 1 // hack to calculate the byte length out of the hex representation 32 update := getTestResourceUpdate() 33 serializedUpdate := make([]byte, serializedUpdateLength) 34 if err := update.binaryPut(serializedUpdate); err != nil { 35 t.Fatal(err) 36 } 37 compareByteSliceToExpectedHex(t, "serializedUpdate", serializedUpdate, serializedUpdateHex) 38 39 // Test fail if update does not contain data 40 update.data = nil 41 if err := update.binaryPut(serializedUpdate); err == nil { 42 t.Fatal("Expected resourceUpdate.binaryPut to fail since update does not contain data") 43 } 44 45 // Test fail if update is too big 46 update.data = make([]byte, 10000) 47 if err := update.binaryPut(serializedUpdate); err == nil { 48 t.Fatal("Expected resourceUpdate.binaryPut to fail since update is too big") 49 } 50 51 // Test fail if passed slice is not of the exact size required for this update 52 update.data = make([]byte, 1) 53 if err := update.binaryPut(serializedUpdate); err == nil { 54 t.Fatal("Expected resourceUpdate.binaryPut to fail since passed slice is not of the appropriate size") 55 } 56 57 // Test serializing a multihash update 58 var serializedUpdateMultihashLength = len(serializedUpdateMultihashHex)/2 - 1 // hack to calculate the byte length out of the hex representation 59 update = getTestResourceUpdateMultihash() 60 serializedUpdate = make([]byte, serializedUpdateMultihashLength) 61 if err := update.binaryPut(serializedUpdate); err != nil { 62 t.Fatal(err) 63 } 64 compareByteSliceToExpectedHex(t, "serializedUpdate", serializedUpdate, serializedUpdateMultihashHex) 65 66 // mess with the multihash to test it fails with a wrong multihash error 67 update.data[1] = 79 68 if err := update.binaryPut(serializedUpdate); err == nil { 69 t.Fatal("Expected resourceUpdate.binaryPut to fail since data contains an invalid multihash") 70 } 71 72 }