Download
41 lines 1.2 KiBGoModified 2026-10-31 raw
1234567891011121314151617181920212223242526272829303132333435363738394041
package tooldef

import (
	"reflect"
	"strings"
	"testing"
)

// Only the header says what a tool is: nothing after it, the script the
// model's arguments run in, can add an argument, a path or a host.
func FuzzParse(f *testing.F) {
	f.Add("# description: Commit.\n# arg message string: The message.\n# write: .git\n# network: github.com", "# write: /\n# network: *")
	f.Add("# description: x\n#\n# timeout: 5m", "\n# arg evil string: x")
	f.Fuzz(func(t *testing.T, header, body string) {
		if strings.Contains(header, "\n\n") {
			return
		}
		lines := strings.Split(header, "\n")
		for _, l := range lines {
			if !strings.HasPrefix(strings.TrimSpace(l), "#") {
				return // the header ends at its first line that is not a comment
			}
		}
		base := "#!/bin/bash\n# blinker-tool\n" + header + "\necho run\n"
		a, errA := Parse("T", "T.sh", []byte(base))
		b, errB := Parse("T", "T.sh", []byte(base+body))
		if errA != nil {
			return
		}
		if errB != nil {
			if len(base+body) > maxScriptBytes || strings.ContainsRune(body, 0) {
				return
			}
			t.Fatalf("the script's body made the header fail: %v", errB)
		}
		a.Script, b.Script = "", ""
		if !reflect.DeepEqual(a, b) {
			t.Fatalf("the script's body changed the tool:\n%+v\n%+v", a, b)
		}
	})
}