github.com/lbryio/lbcd@v0.22.119/txscript/scriptbuilder_test.go (about) 1 // Copyright (c) 2013-2016 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package txscript 6 7 import ( 8 "bytes" 9 "testing" 10 ) 11 12 // TestScriptBuilderAddOp tests that pushing opcodes to a script via the 13 // ScriptBuilder API works as expected. 14 func TestScriptBuilderAddOp(t *testing.T) { 15 t.Parallel() 16 17 tests := []struct { 18 name string 19 opcodes []byte 20 expected []byte 21 }{ 22 { 23 name: "push OP_0", 24 opcodes: []byte{OP_0}, 25 expected: []byte{OP_0}, 26 }, 27 { 28 name: "push OP_1 OP_2", 29 opcodes: []byte{OP_1, OP_2}, 30 expected: []byte{OP_1, OP_2}, 31 }, 32 { 33 name: "push OP_HASH160 OP_EQUAL", 34 opcodes: []byte{OP_HASH160, OP_EQUAL}, 35 expected: []byte{OP_HASH160, OP_EQUAL}, 36 }, 37 } 38 39 // Run tests and individually add each op via AddOp. 40 builder := NewScriptBuilder() 41 t.Logf("Running %d tests", len(tests)) 42 for i, test := range tests { 43 builder.Reset() 44 for _, opcode := range test.opcodes { 45 builder.AddOp(opcode) 46 } 47 result, err := builder.Script() 48 if err != nil { 49 t.Errorf("ScriptBuilder.AddOp #%d (%s) unexpected "+ 50 "error: %v", i, test.name, err) 51 continue 52 } 53 if !bytes.Equal(result, test.expected) { 54 t.Errorf("ScriptBuilder.AddOp #%d (%s) wrong result\n"+ 55 "got: %x\nwant: %x", i, test.name, result, 56 test.expected) 57 continue 58 } 59 } 60 61 // Run tests and bulk add ops via AddOps. 62 t.Logf("Running %d tests", len(tests)) 63 for i, test := range tests { 64 builder.Reset() 65 result, err := builder.AddOps(test.opcodes).Script() 66 if err != nil { 67 t.Errorf("ScriptBuilder.AddOps #%d (%s) unexpected "+ 68 "error: %v", i, test.name, err) 69 continue 70 } 71 if !bytes.Equal(result, test.expected) { 72 t.Errorf("ScriptBuilder.AddOps #%d (%s) wrong result\n"+ 73 "got: %x\nwant: %x", i, test.name, result, 74 test.expected) 75 continue 76 } 77 } 78 79 } 80 81 // TestScriptBuilderAddInt64 tests that pushing signed integers to a script via 82 // the ScriptBuilder API works as expected. 83 func TestScriptBuilderAddInt64(t *testing.T) { 84 t.Parallel() 85 86 tests := []struct { 87 name string 88 val int64 89 expected []byte 90 }{ 91 {name: "push -1", val: -1, expected: []byte{OP_1NEGATE}}, 92 {name: "push small int 0", val: 0, expected: []byte{OP_0}}, 93 {name: "push small int 1", val: 1, expected: []byte{OP_1}}, 94 {name: "push small int 2", val: 2, expected: []byte{OP_2}}, 95 {name: "push small int 3", val: 3, expected: []byte{OP_3}}, 96 {name: "push small int 4", val: 4, expected: []byte{OP_4}}, 97 {name: "push small int 5", val: 5, expected: []byte{OP_5}}, 98 {name: "push small int 6", val: 6, expected: []byte{OP_6}}, 99 {name: "push small int 7", val: 7, expected: []byte{OP_7}}, 100 {name: "push small int 8", val: 8, expected: []byte{OP_8}}, 101 {name: "push small int 9", val: 9, expected: []byte{OP_9}}, 102 {name: "push small int 10", val: 10, expected: []byte{OP_10}}, 103 {name: "push small int 11", val: 11, expected: []byte{OP_11}}, 104 {name: "push small int 12", val: 12, expected: []byte{OP_12}}, 105 {name: "push small int 13", val: 13, expected: []byte{OP_13}}, 106 {name: "push small int 14", val: 14, expected: []byte{OP_14}}, 107 {name: "push small int 15", val: 15, expected: []byte{OP_15}}, 108 {name: "push small int 16", val: 16, expected: []byte{OP_16}}, 109 {name: "push 17", val: 17, expected: []byte{OP_DATA_1, 0x11}}, 110 {name: "push 65", val: 65, expected: []byte{OP_DATA_1, 0x41}}, 111 {name: "push 127", val: 127, expected: []byte{OP_DATA_1, 0x7f}}, 112 {name: "push 128", val: 128, expected: []byte{OP_DATA_2, 0x80, 0}}, 113 {name: "push 255", val: 255, expected: []byte{OP_DATA_2, 0xff, 0}}, 114 {name: "push 256", val: 256, expected: []byte{OP_DATA_2, 0, 0x01}}, 115 {name: "push 32767", val: 32767, expected: []byte{OP_DATA_2, 0xff, 0x7f}}, 116 {name: "push 32768", val: 32768, expected: []byte{OP_DATA_3, 0, 0x80, 0}}, 117 {name: "push -2", val: -2, expected: []byte{OP_DATA_1, 0x82}}, 118 {name: "push -3", val: -3, expected: []byte{OP_DATA_1, 0x83}}, 119 {name: "push -4", val: -4, expected: []byte{OP_DATA_1, 0x84}}, 120 {name: "push -5", val: -5, expected: []byte{OP_DATA_1, 0x85}}, 121 {name: "push -17", val: -17, expected: []byte{OP_DATA_1, 0x91}}, 122 {name: "push -65", val: -65, expected: []byte{OP_DATA_1, 0xc1}}, 123 {name: "push -127", val: -127, expected: []byte{OP_DATA_1, 0xff}}, 124 {name: "push -128", val: -128, expected: []byte{OP_DATA_2, 0x80, 0x80}}, 125 {name: "push -255", val: -255, expected: []byte{OP_DATA_2, 0xff, 0x80}}, 126 {name: "push -256", val: -256, expected: []byte{OP_DATA_2, 0x00, 0x81}}, 127 {name: "push -32767", val: -32767, expected: []byte{OP_DATA_2, 0xff, 0xff}}, 128 {name: "push -32768", val: -32768, expected: []byte{OP_DATA_3, 0x00, 0x80, 0x80}}, 129 } 130 131 builder := NewScriptBuilder() 132 t.Logf("Running %d tests", len(tests)) 133 for i, test := range tests { 134 builder.Reset().AddInt64(test.val) 135 result, err := builder.Script() 136 if err != nil { 137 t.Errorf("ScriptBuilder.AddInt64 #%d (%s) unexpected "+ 138 "error: %v", i, test.name, err) 139 continue 140 } 141 if !bytes.Equal(result, test.expected) { 142 t.Errorf("ScriptBuilder.AddInt64 #%d (%s) wrong result\n"+ 143 "got: %x\nwant: %x", i, test.name, result, 144 test.expected) 145 continue 146 } 147 } 148 } 149 150 // TestScriptBuilderAddData tests that pushing data to a script via the 151 // ScriptBuilder API works as expected and conforms to BIP0062. 152 func TestScriptBuilderAddData(t *testing.T) { 153 t.Parallel() 154 155 tests := []struct { 156 name string 157 data []byte 158 expected []byte 159 useFull bool // use AddFullData instead of AddData. 160 }{ 161 // BIP0062: Pushing an empty byte sequence must use OP_0. 162 {name: "push empty byte sequence", data: nil, expected: []byte{OP_0}}, 163 {name: "push 1 byte 0x00", data: []byte{0x00}, expected: []byte{OP_0}}, 164 165 // BIP0062: Pushing a 1-byte sequence of byte 0x01 through 0x10 must use OP_n. 166 {name: "push 1 byte 0x01", data: []byte{0x01}, expected: []byte{OP_1}}, 167 {name: "push 1 byte 0x02", data: []byte{0x02}, expected: []byte{OP_2}}, 168 {name: "push 1 byte 0x03", data: []byte{0x03}, expected: []byte{OP_3}}, 169 {name: "push 1 byte 0x04", data: []byte{0x04}, expected: []byte{OP_4}}, 170 {name: "push 1 byte 0x05", data: []byte{0x05}, expected: []byte{OP_5}}, 171 {name: "push 1 byte 0x06", data: []byte{0x06}, expected: []byte{OP_6}}, 172 {name: "push 1 byte 0x07", data: []byte{0x07}, expected: []byte{OP_7}}, 173 {name: "push 1 byte 0x08", data: []byte{0x08}, expected: []byte{OP_8}}, 174 {name: "push 1 byte 0x09", data: []byte{0x09}, expected: []byte{OP_9}}, 175 {name: "push 1 byte 0x0a", data: []byte{0x0a}, expected: []byte{OP_10}}, 176 {name: "push 1 byte 0x0b", data: []byte{0x0b}, expected: []byte{OP_11}}, 177 {name: "push 1 byte 0x0c", data: []byte{0x0c}, expected: []byte{OP_12}}, 178 {name: "push 1 byte 0x0d", data: []byte{0x0d}, expected: []byte{OP_13}}, 179 {name: "push 1 byte 0x0e", data: []byte{0x0e}, expected: []byte{OP_14}}, 180 {name: "push 1 byte 0x0f", data: []byte{0x0f}, expected: []byte{OP_15}}, 181 {name: "push 1 byte 0x10", data: []byte{0x10}, expected: []byte{OP_16}}, 182 183 // BIP0062: Pushing the byte 0x81 must use OP_1NEGATE. 184 {name: "push 1 byte 0x81", data: []byte{0x81}, expected: []byte{OP_1NEGATE}}, 185 186 // BIP0062: Pushing any other byte sequence up to 75 bytes must 187 // use the normal data push (opcode byte n, with n the number of 188 // bytes, followed n bytes of data being pushed). 189 {name: "push 1 byte 0x11", data: []byte{0x11}, expected: []byte{OP_DATA_1, 0x11}}, 190 {name: "push 1 byte 0x80", data: []byte{0x80}, expected: []byte{OP_DATA_1, 0x80}}, 191 {name: "push 1 byte 0x82", data: []byte{0x82}, expected: []byte{OP_DATA_1, 0x82}}, 192 {name: "push 1 byte 0xff", data: []byte{0xff}, expected: []byte{OP_DATA_1, 0xff}}, 193 { 194 name: "push data len 17", 195 data: bytes.Repeat([]byte{0x49}, 17), 196 expected: append([]byte{OP_DATA_17}, bytes.Repeat([]byte{0x49}, 17)...), 197 }, 198 { 199 name: "push data len 75", 200 data: bytes.Repeat([]byte{0x49}, 75), 201 expected: append([]byte{OP_DATA_75}, bytes.Repeat([]byte{0x49}, 75)...), 202 }, 203 204 // BIP0062: Pushing 76 to 255 bytes must use OP_PUSHDATA1. 205 { 206 name: "push data len 76", 207 data: bytes.Repeat([]byte{0x49}, 76), 208 expected: append([]byte{OP_PUSHDATA1, 76}, bytes.Repeat([]byte{0x49}, 76)...), 209 }, 210 { 211 name: "push data len 255", 212 data: bytes.Repeat([]byte{0x49}, 255), 213 expected: append([]byte{OP_PUSHDATA1, 255}, bytes.Repeat([]byte{0x49}, 255)...), 214 }, 215 216 // BIP0062: Pushing 256 to 520 bytes must use OP_PUSHDATA2. 217 { 218 name: "push data len 256", 219 data: bytes.Repeat([]byte{0x49}, 256), 220 expected: append([]byte{OP_PUSHDATA2, 0, 1}, bytes.Repeat([]byte{0x49}, 256)...), 221 }, 222 { 223 name: "push data len 520", // bitcoin has a 520 byte cap, but lbry is 20k 224 data: bytes.Repeat([]byte{0x49}, 520), 225 expected: append([]byte{OP_PUSHDATA2, 0x08, 0x02}, bytes.Repeat([]byte{0x49}, 520)...), 226 }, 227 { 228 name: "push data len 32767 (canonical)", 229 data: bytes.Repeat([]byte{0x49}, 32767), 230 expected: nil, 231 }, 232 { 233 name: "push data len 65536 (canonical)", 234 data: bytes.Repeat([]byte{0x49}, 65536), 235 expected: nil, 236 }, 237 238 // Additional tests for the PushFullData function that 239 // intentionally allows data pushes to exceed the limit for 240 // regression testing purposes. 241 242 // 3-byte data push via OP_PUSHDATA_2. 243 { 244 name: "push data len 32767 (non-canonical)", 245 data: bytes.Repeat([]byte{0x49}, 32767), 246 expected: append([]byte{OP_PUSHDATA2, 255, 127}, bytes.Repeat([]byte{0x49}, 32767)...), 247 useFull: true, 248 }, 249 250 // 5-byte data push via OP_PUSHDATA_4. 251 { 252 name: "push data len 65536 (non-canonical)", 253 data: bytes.Repeat([]byte{0x49}, 65536), 254 expected: append([]byte{OP_PUSHDATA4, 0, 0, 1, 0}, bytes.Repeat([]byte{0x49}, 65536)...), 255 useFull: true, 256 }, 257 } 258 259 builder := NewScriptBuilder() 260 t.Logf("Running %d tests", len(tests)) 261 for i, test := range tests { 262 if !test.useFull { 263 builder.Reset().AddData(test.data) 264 } else { 265 builder.Reset().AddFullData(test.data) 266 } 267 result, _ := builder.Script() 268 if !bytes.Equal(result, test.expected) { 269 t.Errorf("ScriptBuilder.AddData #%d (%s) wrong result\n"+ 270 "got: %x\nwant: %x", i, test.name, result, 271 test.expected) 272 continue 273 } 274 } 275 } 276 277 // TestExceedMaxScriptSize ensures that all of the functions that can be used 278 // to add data to a script don't allow the script to exceed the max allowed 279 // size. 280 func TestExceedMaxScriptSize(t *testing.T) { 281 t.Parallel() 282 283 // Start off by constructing a max size script. 284 builder := NewScriptBuilder() 285 builder.Reset().AddFullData(make([]byte, MaxScriptSize-3)) 286 origScript, err := builder.Script() 287 if err != nil { 288 t.Fatalf("Unexpected error for max size script: %v", err) 289 } 290 291 // Ensure adding data that would exceed the maximum size of the script 292 // does not add the data. 293 script, err := builder.AddData([]byte{0x00}).Script() 294 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { 295 t.Fatalf("ScriptBuilder.AddData allowed exceeding max script "+ 296 "size: %v", len(script)) 297 } 298 if !bytes.Equal(script, origScript) { 299 t.Fatalf("ScriptBuilder.AddData unexpected modified script - "+ 300 "got len %d, want len %d", len(script), len(origScript)) 301 } 302 303 // Ensure adding an opcode that would exceed the maximum size of the 304 // script does not add the data. 305 builder.Reset().AddFullData(make([]byte, MaxScriptSize-3)) 306 script, err = builder.AddOp(OP_0).Script() 307 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { 308 t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+ 309 "got len %d, want len %d", len(script), len(origScript)) 310 } 311 if !bytes.Equal(script, origScript) { 312 t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+ 313 "got len %d, want len %d", len(script), len(origScript)) 314 } 315 316 // Ensure adding an integer that would exceed the maximum size of the 317 // script does not add the data. 318 builder.Reset().AddFullData(make([]byte, MaxScriptSize-3)) 319 script, err = builder.AddInt64(0).Script() 320 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { 321 t.Fatalf("ScriptBuilder.AddInt64 unexpected modified script - "+ 322 "got len %d, want len %d", len(script), len(origScript)) 323 } 324 if !bytes.Equal(script, origScript) { 325 t.Fatalf("ScriptBuilder.AddInt64 unexpected modified script - "+ 326 "got len %d, want len %d", len(script), len(origScript)) 327 } 328 } 329 330 // TestErroredScript ensures that all of the functions that can be used to add 331 // data to a script don't modify the script once an error has happened. 332 func TestErroredScript(t *testing.T) { 333 t.Parallel() 334 335 // Start off by constructing a near max size script that has enough 336 // space left to add each data type without an error and force an 337 // initial error condition. 338 builder := NewScriptBuilder() 339 builder.Reset().AddFullData(make([]byte, MaxScriptSize-8)) 340 origScript, err := builder.Script() 341 if err != nil { 342 t.Fatalf("ScriptBuilder.AddFullData unexpected error: %v", err) 343 } 344 script, err := builder.AddData([]byte{0x00, 0x00, 0x00, 0x00, 0x00}).Script() 345 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { 346 t.Fatalf("ScriptBuilder.AddData allowed exceeding max script "+ 347 "size: %v", len(script)) 348 } 349 if !bytes.Equal(script, origScript) { 350 t.Fatalf("ScriptBuilder.AddData unexpected modified script - "+ 351 "got len %d, want len %d", len(script), len(origScript)) 352 } 353 354 // Ensure adding data, even using the non-canonical path, to a script 355 // that has errored doesn't succeed. 356 script, err = builder.AddFullData([]byte{0x00}).Script() 357 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { 358 t.Fatal("ScriptBuilder.AddFullData succeeded on errored script") 359 } 360 if !bytes.Equal(script, origScript) { 361 t.Fatalf("ScriptBuilder.AddFullData unexpected modified "+ 362 "script - got len %d, want len %d", len(script), 363 len(origScript)) 364 } 365 366 // Ensure adding data to a script that has errored doesn't succeed. 367 script, err = builder.AddData([]byte{0x00}).Script() 368 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { 369 t.Fatal("ScriptBuilder.AddData succeeded on errored script") 370 } 371 if !bytes.Equal(script, origScript) { 372 t.Fatalf("ScriptBuilder.AddData unexpected modified "+ 373 "script - got len %d, want len %d", len(script), 374 len(origScript)) 375 } 376 377 // Ensure adding an opcode to a script that has errored doesn't succeed. 378 script, err = builder.AddOp(OP_0).Script() 379 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { 380 t.Fatal("ScriptBuilder.AddOp succeeded on errored script") 381 } 382 if !bytes.Equal(script, origScript) { 383 t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+ 384 "got len %d, want len %d", len(script), len(origScript)) 385 } 386 387 // Ensure adding an integer to a script that has errored doesn't 388 // succeed. 389 script, err = builder.AddInt64(0).Script() 390 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { 391 t.Fatal("ScriptBuilder.AddInt64 succeeded on errored script") 392 } 393 if !bytes.Equal(script, origScript) { 394 t.Fatalf("ScriptBuilder.AddInt64 unexpected modified script - "+ 395 "got len %d, want len %d", len(script), len(origScript)) 396 } 397 398 // Ensure the error has a message set. 399 if err.Error() == "" { 400 t.Fatal("ErrScriptNotCanonical.Error does not have any text") 401 } 402 }