TestErroredScript ensures that all of the functions that can be used to add data to a script don't modify the script once an error has happened.
(t *testing.T)
| 369 | // TestErroredScript ensures that all of the functions that can be used to add |
| 370 | // data to a script don't modify the script once an error has happened. |
| 371 | func TestErroredScript(t *testing.T) { |
| 372 | t.Parallel() |
| 373 | |
| 374 | // Start off by constructing a near max size script that has enough |
| 375 | // space left to add each data type without an error and force an |
| 376 | // initial error condition. |
| 377 | builder := NewScriptBuilder() |
| 378 | builder.Reset().AddFullData(make([]byte, MaxScriptSize-8)) |
| 379 | origScript, err := builder.Script() |
| 380 | if err != nil { |
| 381 | t.Fatalf("ScriptBuilder.AddFullData unexpected error: %v", err) |
| 382 | } |
| 383 | script, err := builder.AddData([]byte{0x00, 0x00, 0x00, 0x00, 0x00}).Script() |
| 384 | if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { |
| 385 | t.Fatalf("ScriptBuilder.AddData allowed exceeding max script "+ |
| 386 | "size: %v", len(script)) |
| 387 | } |
| 388 | if !bytes.Equal(script, origScript) { |
| 389 | t.Fatalf("ScriptBuilder.AddData unexpected modified script - "+ |
| 390 | "got len %d, want len %d", len(script), len(origScript)) |
| 391 | } |
| 392 | |
| 393 | // Ensure adding data, even using the non-canonical path, to a script |
| 394 | // that has errored doesn't succeed. |
| 395 | script, err = builder.AddFullData([]byte{0x00}).Script() |
| 396 | if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { |
| 397 | t.Fatal("ScriptBuilder.AddFullData succeeded on errored script") |
| 398 | } |
| 399 | if !bytes.Equal(script, origScript) { |
| 400 | t.Fatalf("ScriptBuilder.AddFullData unexpected modified "+ |
| 401 | "script - got len %d, want len %d", len(script), |
| 402 | len(origScript)) |
| 403 | } |
| 404 | |
| 405 | // Ensure adding data to a script that has errored doesn't succeed. |
| 406 | script, err = builder.AddData([]byte{0x00}).Script() |
| 407 | if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { |
| 408 | t.Fatal("ScriptBuilder.AddData succeeded on errored script") |
| 409 | } |
| 410 | if !bytes.Equal(script, origScript) { |
| 411 | t.Fatalf("ScriptBuilder.AddData unexpected modified "+ |
| 412 | "script - got len %d, want len %d", len(script), |
| 413 | len(origScript)) |
| 414 | } |
| 415 | |
| 416 | // Ensure adding an opcode to a script that has errored doesn't succeed. |
| 417 | script, err = builder.AddOp(OP_0).Script() |
| 418 | if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil { |
| 419 | t.Fatal("ScriptBuilder.AddOp succeeded on errored script") |
| 420 | } |
| 421 | if !bytes.Equal(script, origScript) { |
| 422 | t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+ |
| 423 | "got len %d, want len %d", len(script), len(origScript)) |
| 424 | } |
| 425 | |
| 426 | // Ensure adding an integer to a script that has errored doesn't |
| 427 | // succeed. |
| 428 | script, err = builder.AddInt64(0).Script() |
nothing calls this directly
no test coverage detected
searching dependent graphs…