github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/types/unlockhash_test.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 "sort" 6 "testing" 7 ) 8 9 // TestUnlockHashJSONMarshalling checks that when an unlock hash is marshalled 10 // and unmarshalled using JSON, the result is what is expected. 11 func TestUnlockHashJSONMarshalling(t *testing.T) { 12 // Create an unlock hash. 13 uc := UnlockConditions{ 14 Timelock: 5, 15 SignaturesRequired: 3, 16 } 17 uh := uc.UnlockHash() 18 19 // Marshal the unlock hash. 20 marUH, err := json.Marshal(uh) 21 if err != nil { 22 t.Fatal(err) 23 } 24 25 // Unmarshal the unlock hash and compare to the original. 26 var umarUH UnlockHash 27 err = json.Unmarshal(marUH, &umarUH) 28 if err != nil { 29 t.Fatal(err) 30 } 31 if umarUH != uh { 32 t.Error("Marshalled and unmarshalled unlock hash are not equivalent") 33 } 34 35 // Corrupt the checksum. 36 marUH[36]++ 37 err = umarUH.UnmarshalJSON(marUH) 38 if err != ErrInvalidUnlockHashChecksum { 39 t.Error("expecting an invalid checksum:", err) 40 } 41 marUH[36]-- 42 43 // Try an input that's not correct hex. 44 marUH[7] += 100 45 err = umarUH.UnmarshalJSON(marUH) 46 if err == nil { 47 t.Error("Expecting error after corrupting input") 48 } 49 marUH[7] -= 100 50 51 // Try an input of the wrong length. 52 err = (&umarUH).UnmarshalJSON(marUH[2:]) 53 if err != ErrUnlockHashWrongLen { 54 t.Error("Got wrong error:", err) 55 } 56 } 57 58 // TestUnlockHashStringMarshalling checks that when an unlock hash is 59 // marshalled and unmarshalled using String and LoadString, the result is what 60 // is expected. 61 func TestUnlockHashStringMarshalling(t *testing.T) { 62 // Create an unlock hash. 63 uc := UnlockConditions{ 64 Timelock: 2, 65 SignaturesRequired: 7, 66 } 67 uh := uc.UnlockHash() 68 69 // Marshal the unlock hash. 70 marUH := uh.String() 71 72 // Unmarshal the unlock hash and compare to the original. 73 var umarUH UnlockHash 74 err := umarUH.LoadString(marUH) 75 if err != nil { 76 t.Fatal(err) 77 } 78 if umarUH != uh { 79 t.Error("Marshalled and unmarshalled unlock hash are not equivalent") 80 } 81 82 // Corrupt the checksum. 83 byteMarUH := []byte(marUH) 84 byteMarUH[36]++ 85 err = umarUH.LoadString(string(byteMarUH)) 86 if err != ErrInvalidUnlockHashChecksum { 87 t.Error("expecting an invalid checksum:", err) 88 } 89 byteMarUH[36]-- 90 91 // Try an input that's not correct hex. 92 byteMarUH[7] += 100 93 err = umarUH.LoadString(string(byteMarUH)) 94 if err == nil { 95 t.Error("Expecting error after corrupting input") 96 } 97 byteMarUH[7] -= 100 98 99 // Try an input of the wrong length. 100 err = umarUH.LoadString(string(byteMarUH[2:])) 101 if err != ErrUnlockHashWrongLen { 102 t.Error("Got wrong error:", err) 103 } 104 } 105 106 // TestUnlockHashSliceSorting checks that the sort method correctly sorts 107 // unlock hash slices. 108 func TestUnlockHashSliceSorting(t *testing.T) { 109 // To test that byte-order is done correctly, a semi-random second byte is 110 // used that is equal to the first byte * 23 % 7 111 uhs := UnlockHashSlice{ 112 UnlockHash{4, 1}, 113 UnlockHash{0, 0}, 114 UnlockHash{2, 4}, 115 UnlockHash{3, 6}, 116 UnlockHash{1, 2}, 117 } 118 sort.Sort(uhs) 119 for i := byte(0); i < 5; i++ { 120 if uhs[i] != (UnlockHash{i, (i * 23) % 7}) { 121 t.Error("sorting failed on index", i, uhs[i]) 122 } 123 } 124 }