MCPcopy Create free account
hub / github.com/btcsuite/btcd / TestExceedMaxScriptSize

Function TestExceedMaxScriptSize

txscript/scriptbuilder_test.go:319–367  ·  view source on GitHub ↗

TestExceedMaxScriptSize ensures that all of the functions that can be used to add data to a script don't allow the script to exceed the max allowed size.

(t *testing.T)

Source from the content-addressed store, hash-verified

317// to add data to a script don't allow the script to exceed the max allowed
318// size.
319func TestExceedMaxScriptSize(t *testing.T) {
320 t.Parallel()
321
322 // Start off by constructing a max size script.
323 builder := NewScriptBuilder()
324 builder.Reset().AddFullData(make([]byte, MaxScriptSize-3))
325 origScript, err := builder.Script()
326 if err != nil {
327 t.Fatalf("Unexpected error for max size script: %v", err)
328 }
329
330 // Ensure adding data that would exceed the maximum size of the script
331 // does not add the data.
332 script, err := builder.AddData([]byte{0x00}).Script()
333 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil {
334 t.Fatalf("ScriptBuilder.AddData allowed exceeding max script "+
335 "size: %v", len(script))
336 }
337 if !bytes.Equal(script, origScript) {
338 t.Fatalf("ScriptBuilder.AddData unexpected modified script - "+
339 "got len %d, want len %d", len(script), len(origScript))
340 }
341
342 // Ensure adding an opcode that would exceed the maximum size of the
343 // script does not add the data.
344 builder.Reset().AddFullData(make([]byte, MaxScriptSize-3))
345 script, err = builder.AddOp(OP_0).Script()
346 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil {
347 t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+
348 "got len %d, want len %d", len(script), len(origScript))
349 }
350 if !bytes.Equal(script, origScript) {
351 t.Fatalf("ScriptBuilder.AddOp unexpected modified script - "+
352 "got len %d, want len %d", len(script), len(origScript))
353 }
354
355 // Ensure adding an integer that would exceed the maximum size of the
356 // script does not add the data.
357 builder.Reset().AddFullData(make([]byte, MaxScriptSize-3))
358 script, err = builder.AddInt64(0).Script()
359 if _, ok := err.(ErrScriptNotCanonical); !ok || err == nil {
360 t.Fatalf("ScriptBuilder.AddInt64 unexpected modified script - "+
361 "got len %d, want len %d", len(script), len(origScript))
362 }
363 if !bytes.Equal(script, origScript) {
364 t.Fatalf("ScriptBuilder.AddInt64 unexpected modified script - "+
365 "got len %d, want len %d", len(script), len(origScript))
366 }
367}
368
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.

Callers

nothing calls this directly

Calls 8

ResetMethod · 0.95
ScriptMethod · 0.95
AddDataMethod · 0.95
AddOpMethod · 0.95
AddInt64Method · 0.95
NewScriptBuilderFunction · 0.85
AddFullDataMethod · 0.80
ScriptMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…