decred.org/dcrdex@v1.0.5/dex/encode/encode_test.go (about) 1 package encode 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 var ( 9 bEqual = bytes.Equal 10 tEmpty = []byte{} 11 tA = []byte{0xaa} 12 tB = []byte{0xbb, 0xbb} 13 tC = []byte{0xcc, 0xcc, 0xcc} 14 ) 15 16 func TestBuildyBytes(t *testing.T) { 17 type test struct { 18 pushes [][]byte 19 exp []byte 20 } 21 tests := []test{ 22 { 23 pushes: [][]byte{tA}, 24 exp: []byte{0x01, 0xaa}, 25 }, 26 { 27 pushes: [][]byte{tA, tB}, 28 exp: []byte{1, 0xaa, 2, 0xbb, 0xbb}, 29 }, 30 { 31 pushes: [][]byte{tA, nil}, 32 exp: []byte{1, 0xaa, 0}, 33 }, 34 { 35 pushes: [][]byte{tEmpty, tEmpty}, 36 exp: []byte{0, 0}, 37 }, 38 } 39 for i, tt := range tests { 40 var b BuildyBytes 41 for _, p := range tt.pushes { 42 b = b.AddData(p) 43 } 44 if !bytes.Equal(b, tt.exp) { 45 t.Fatalf("test %d failed", i) 46 } 47 } 48 } 49 50 func TestDecodeBlob(t *testing.T) { 51 almostLongBlob := RandomBytes(254) 52 longBlob := RandomBytes(255) 53 longBlob2 := RandomBytes(555) 54 longestUint16Blob := RandomBytes(65535) 55 longerBlob := RandomBytes(65536) 56 longerBlob2 := RandomBytes(65599) 57 megaBlob := RandomBytes(12_345_678) 58 almostLargestBlob := RandomBytes(MaxDataLen - 1) 59 largestBlob := RandomBytes(MaxDataLen) 60 // tooLargeBlob tested after the loop, recovering the expected panic 61 62 type test struct { 63 v byte 64 b []byte 65 exp [][]byte 66 wantErr bool 67 } 68 tests := []test{ 69 { 70 v: 1, 71 b: BuildyBytes{1}.AddData(nil).AddData(tEmpty).AddData(tA), 72 exp: [][]byte{tEmpty, tEmpty, tA}, 73 }, 74 { 75 v: 5, 76 b: BuildyBytes{5}.AddData(tB).AddData(tC), 77 exp: [][]byte{tB, tC}, 78 }, 79 { 80 v: 250, 81 b: BuildyBytes{250}.AddData(tA).AddData(almostLongBlob), 82 exp: [][]byte{tA, almostLongBlob}, 83 }, 84 { 85 v: 255, 86 b: BuildyBytes{255}.AddData(tA).AddData(longBlob), 87 exp: [][]byte{tA, longBlob}, 88 }, 89 { 90 v: 255, 91 b: BuildyBytes{255}.AddData(tA).AddData(longBlob2), 92 exp: [][]byte{tA, longBlob2}, 93 }, 94 { 95 v: 255, 96 b: BuildyBytes{255}.AddData(tA).AddData(longestUint16Blob), 97 exp: [][]byte{tA, longestUint16Blob}, 98 }, 99 { 100 v: 255, 101 b: BuildyBytes{255}.AddData(tA).AddData(longerBlob), 102 exp: [][]byte{tA, longerBlob}, 103 }, 104 { 105 v: 255, 106 b: BuildyBytes{255}.AddData(tA).AddData(longerBlob2), 107 exp: [][]byte{tA, longerBlob2}, 108 }, 109 { 110 v: 255, 111 b: BuildyBytes{255}.AddData(tA).AddData(megaBlob), 112 exp: [][]byte{tA, megaBlob}, 113 }, 114 { 115 v: 255, 116 b: BuildyBytes{255}.AddData(tA).AddData(almostLargestBlob), 117 exp: [][]byte{tA, almostLargestBlob}, 118 }, 119 { 120 v: 255, 121 b: BuildyBytes{255}.AddData(tA).AddData(largestBlob), 122 exp: [][]byte{tA, largestBlob}, 123 }, 124 { 125 b: []byte{0x01, 0x02}, // missing two bytes 126 wantErr: true, 127 }, 128 } 129 for i, tt := range tests { 130 ver, pushes, err := DecodeBlob(tt.b) 131 if (err != nil) != tt.wantErr { 132 t.Fatalf("test %d: %v", i, err) 133 } 134 if tt.wantErr { 135 continue 136 } 137 if ver != tt.v { 138 t.Fatalf("test %d: wanted version %d, got %d", i, tt.v, ver) 139 } 140 if len(pushes) != len(tt.exp) { 141 t.Fatalf("wrongs number of pushes. wanted %d, got %d", len(tt.exp), len(pushes)) 142 } 143 for j, push := range pushes { 144 check := tt.exp[j] 145 if !bytes.Equal(check, push) { 146 t.Fatalf("push %d:%d incorrect. wanted %x, got %x", i, j, check, push) 147 } 148 } 149 } 150 151 tooLargeBlob := RandomBytes(MaxDataLen + 1) 152 func() { 153 defer func() { 154 if r := recover(); r == nil { 155 t.Errorf("no panic encoding data that's too large") 156 } 157 }() 158 BuildyBytes{255}.AddData(tA).AddData(tooLargeBlob) 159 }() 160 }