format testscript files with gofmt

This commit is contained in:
Paul Scheduikat
2025-06-15 18:25:20 +02:00
committed by GitHub
parent 8d8ba00515
commit 87ebebb520
25 changed files with 159 additions and 78 deletions

View File

@@ -18,6 +18,9 @@ Just the usual `go test ./...`; many of the tests are in
`testdata/scripts/`, which allows laying out files and shell-like steps to run as
part of the test.
For editor support of testscript files in vscode (highlighting and formatting), install the
[vscode-testscript](https://marketplace.visualstudio.com/items?itemName=twpayne.vscode-testscript) extension.
Note that the tests do real builds, so they are quite slow; on an average
laptop, `go test` can take over thirty seconds. Here are some tips:

View File

@@ -62,6 +62,7 @@ func main() {
println(imported.AddImpl(3, 4))
}
-- garble_main_amd64.s --
#include "garble_define_amd64.h"

View File

@@ -47,5 +47,6 @@ func main() {
wg.Wait()
println(unalignedUint64.u64.Load())
}
-- main.stderr --
20

View File

@@ -43,6 +43,7 @@ func main() {
level1b.Print()
level1c.Print()
}
-- level1a/pkg.go --
package level1a
@@ -52,14 +53,17 @@ import (
)
func Print() { println(level2x.Value, level2y.Value) }
-- level1a/level2x/pkg.go --
package level2x
var Value = "1a/2x"
-- level1a/level2y/pkg.go --
package level2y
var Value = "1a/2y"
-- level1b/pkg.go --
package level1b
@@ -69,14 +73,17 @@ import (
)
func Print() { println(level2x.Value, level2y.Value) }
-- level1b/level2x/pkg.go --
package level2x
var Value = "1b/2x"
-- level1b/level2y/pkg.go --
package level2y
var Value = "1b/2y"
-- level1c/pkg.go --
package level1c
@@ -86,14 +93,17 @@ import (
)
func Print() { println(level2x.Value, level2y.Value) }
-- level1c/level2x/pkg.go --
package level2x
var Value = "1c/2x"
-- level1c/level2y/pkg.go --
package level2y
var Value = "1c/2y"
-- main.stderr --
1a/2x 1a/2y
1b/2x 1b/2y

View File

@@ -43,6 +43,7 @@ func main() {
imported.RegularFunc()
cgoFunc()
}
-- imported/imported_regular.go --
package imported
@@ -58,6 +59,7 @@ func RegularFunc() {
fmt.Println("regular filename:", filename)
}
}
-- cgo_main.go --
package main
@@ -112,6 +114,7 @@ func goCallback() {
func printString(cs *C.char) {
fmt.Println(C.GoString(cs))
}
-- separate.h --
void separateFunction();
-- separate.c --

View File

@@ -46,37 +46,38 @@ func func1() {}
//garble:controlflow flatten_passes=1 junk_jumps=max block_splits=max flatten_hardening=xor
func xorHardeningTest(i int) int {
if i == 0 {
return 1
}
return i * 2;
if i == 0 {
return 1
}
return i * 2
}
//garble:controlflow flatten_passes=1 junk_jumps=max block_splits=max flatten_hardening=delegate_table
func delegateHardeningTest(i int) int {
if i == 0 {
return 1
}
return i * 3;
if i == 0 {
return 1
}
return i * 3
}
//garble:controlflow flatten_passes=1 junk_jumps=max block_splits=max flatten_hardening=xor,delegate_table
// Trigger multiple hardening using multiple anonymous functions
//
//garble:controlflow flatten_passes=1 junk_jumps=max block_splits=max flatten_hardening=xor,delegate_table
func multiHardeningTest(i int) int {
notZero := func(i int) bool {
return i != 0
}
isZero := func(i int) bool {
return i == 0
}
multiply := func(i int) int {
return i * 4
}
notZero := func(i int) bool {
return i != 0
}
isZero := func(i int) bool {
return i == 0
}
multiply := func(i int) int {
return i * 4
}
if !notZero(i) && isZero(i) {
return 1
}
return multiply(i);
if !notZero(i) && isZero(i) {
return 1
}
return multiply(i)
}
//garble:controlflow

View File

@@ -65,6 +65,7 @@ go 1.23
package main
func main() {}
-- importer/importer.go --
package main
@@ -74,10 +75,12 @@ func main() {
println(imported.LongString)
println("some long string to not obfuscate")
}
-- imported/imported.go --
package imported
var LongString = "some long string to obfuscate"
-- stdimporter/main.go --
package main
@@ -87,8 +90,8 @@ import (
"os"
"runtime"
"runtime/debug"
"time"
"syscall"
"time"
)
func main() {
@@ -106,6 +109,7 @@ func main() {
syscall.Listen(0, 1)
}
}
-- stdimporter.stdout --
runtime.GOROOT: ""
runtime.Version: "unknown"

View File

@@ -96,9 +96,9 @@ import (
func main() {
if len(os.Args) > 0 && os.Args[1] == "env" {
enc, _ := json.Marshal(struct{
enc, _ := json.Marshal(struct {
GOVERSION string
} {
}{
GOVERSION: os.Getenv("TOOLCHAIN_GOVERSION"),
})
fmt.Printf("%s\n", enc)

View File

@@ -47,14 +47,14 @@ var _ privateInterface = T("")
type StructUnnamed = struct {
Foo int
Bar []*struct {
Nested *[]string
Nested *[]string
NestedTagged string
NestedAlias int64 // we can skip the alias too
NestedAlias int64 // we can skip the alias too
}
Named lib3.Named
lib3.StructEmbed
Tagged string // no field tag
Alias int64 // we can skip the alias too
Alias int64 // we can skip the alias too
}
var _ = lib1.Struct1(lib2.Struct2{})
@@ -88,14 +88,14 @@ import "test/main/lib3"
type Struct1 struct {
Foo int
Bar []*struct {
Nested *[]string
Nested *[]string
NestedTagged string `json:"tagged1"`
NestedAlias alias1
NestedAlias alias1
}
Named lib3.Named
lib3.StructEmbed
Tagged string `json:"tagged1"`
Alias alias1
Alias alias1
}
type alias1 = int64
@@ -108,17 +108,18 @@ import "test/main/lib3"
type Struct2 struct {
Foo int
Bar []*struct {
Nested *[]string
Nested *[]string
NestedTagged string `json:"tagged2"`
NestedAlias alias2
NestedAlias alias2
}
Named lib3.Named
lib3.StructEmbed
Tagged string `json:"tagged2"`
Alias alias2
Alias alias2
}
type alias2 = int64
-- lib3/lib3.go --
package lib3
@@ -127,6 +128,7 @@ type Named int
type StructEmbed struct {
Baz any
}
-- main.stdout --
String method for foo
unexported method for foo

View File

@@ -72,14 +72,14 @@ rsc.io/testonly v1.0.0/go.mod h1:OqmGbIFOcF+XrFReLOGZ6BhMM7uMBiQwZsyNmh74SzY=
package main
import (
"fmt"
"database/sql"
"fmt"
"test/main/importedpkg"
"rsc.io/quote"
garbletest "gopkg.in/garbletest.v2"
garbletestconst "gopkg.in/garbletestconst.v2"
"rsc.io/quote"
)
func main() {
@@ -95,12 +95,14 @@ func main() {
fmt.Println(garbletestconst.StrConst)
fmt.Println(sql.Drivers()[0])
}
-- notag_fail.go --
//go:build !buildtag
package main
var foo int = "should be omitted by -tags"
-- withtag_success.go --
//go:build buildtag
@@ -109,6 +111,7 @@ package main
import "fmt"
func init() { fmt.Println("buildtag init func") }
-- differentpkg_unnamed.go --
package main
@@ -119,12 +122,14 @@ import (
var _ = actualpkgname.Noop
var _ = goextension.Noop
-- differentpkg_named.go --
package main
import named "test/main/different-pkg-name"
var _ = named.Noop
-- importedpkg/imported.go --
package importedpkg
@@ -172,10 +177,12 @@ import (
var _ indirect.Indirect
var _ = another.Blank
-- importedpkg/another/pkg.go --
package another
const Blank = 3
-- importedpkg/indirect/indirect.go --
package indirect
@@ -192,6 +199,7 @@ var Noop int
package goextension
var Noop int
-- main.stdout --
buildtag init func
imported var value

View File

@@ -36,52 +36,64 @@ func main() {
println(string(chungus))
println(string(yoshi))
}
-- y.go --
package main
// yoshi is filled by ordered init funcs, all in different files.
// If we change the relative order of filenames when sorted, this file will break.
var yoshi []byte
-- y0.go --
package main
func init() { yoshi = append(yoshi, 'B') }
-- y1.go --
package main
func init() { yoshi = append(yoshi, 'E') }
-- y2.go --
package main
func init() { yoshi = append(yoshi, 'E') }
-- y3.go --
package main
func init() { yoshi = append(yoshi, 'G') }
-- y4.go --
package main
func init() { yoshi = append(yoshi, ' ') }
-- y5.go --
package main
func init() { yoshi = append(yoshi, 'Y') }
-- y6.go --
package main
func init() { yoshi = append(yoshi, 'O') }
-- y7.go --
package main
func init() { yoshi = append(yoshi, 'S') }
-- y8.go --
package main
func init() { yoshi = append(yoshi, 'H') }
-- y9.go --
package main
func init() { yoshi = append(yoshi, 'I') }
-- main.stderr --
Big Chungus
BEEG YOSHI

View File

@@ -57,6 +57,7 @@ func main() {
fmt.Printf("should be kept: %q, %q\n", notReplacedBefore, notReplacedAfter)
fmt.Printf("no longer unset: %q\n", imported.ExportedUnset)
}
-- imported/imported.go --
package imported
@@ -65,6 +66,7 @@ var (
otherVar int
)
-- main.stdout --
version: "v1.22.33"
becomes empty: ""

View File

@@ -43,39 +43,48 @@ import (
)
// A linkname to an external obfuscated func.
//
//go:linkname obfuscatedFunc test/main/imported.ObfuscatedFuncImpl
func obfuscatedFunc() string
// A linkname to an external obfuscated method.
//
//go:linkname obfuscatedMethod test/main/imported.Receiver.obfuscatedMethod
func obfuscatedMethod(imported.Receiver) string
// A linkname to an external unobfuscated method.
//
//go:linkname unobfuscatedMethod test/main/imported.Receiver.UnobfuscatedMethod
func unobfuscatedMethod(imported.Receiver) string
// A linkname to an external obfuscated pointer method, with an extra parameter.
//
//go:linkname obfuscatedPointerMethod test/main/imported.(*Receiver).obfuscatedPointerMethod
func obfuscatedPointerMethod(*imported.Receiver, string) string
// Similar to the above, but for std, plus having to define a type.
// Some libraries do abuse reflect in this way, unfortunately.
type rtype struct{}
//go:linkname rtype_ptrTo reflect.(*rtype).ptrTo
func rtype_ptrTo(*rtype) *rtype
//go:linkname rtype_NumMethod reflect.(*rtype).NumMethod
func rtype_NumMethod(*rtype) int
// A linkname to an entirely made up name, implemented elsewhere.
//
//go:linkname renamedFunc madeup.newName
func renamedFunc() string
// A linkname to an external non-obfuscated func in another
// module whose package path has a dot in it.
//
//go:linkname tagline big.chungus/meme.chungify
func tagline() string
// A linkname to an external non-obfuscated func with receiver which is also non-obfuscated
//
//go:linkname changeThing test/main/imported.(*channel).changeThing
func changeThing(c unsafe.Pointer, to string)
@@ -108,6 +117,7 @@ func main() {
println(a.DoThing())
}
-- imported/imported.go --
package imported
@@ -144,7 +154,7 @@ func ObfuscatedFuncImpl() string {
return "obfuscated func"
}
type Receiver struct{
type Receiver struct {
Field string
}
@@ -167,8 +177,10 @@ func renamedFunc() string {
// A linkname to an external non-obfuscated func.
// Different from byteIndex, as we call this from an importer package.
//
//go:linkname ByteIndex strings.IndexByte
func ByteIndex(s string, c byte) int
-- big.chungus/meme/go.mod --
module test/main
@@ -179,6 +191,7 @@ package meme
func chungify() string {
return "featuring Dante from the Devil May Cry series"
}
-- main.stderr --
obfuscated func
obfuscated method: field value

View File

@@ -23,7 +23,7 @@ import _ "unsafe"
// Note that it doesn't matter that the struct here is empty,
// as the linknames below only use pointers to it.
type moduledata struct {}
type moduledata struct{}
//go:linkname lastmoduledatap runtime.lastmoduledatap
var lastmoduledatap *moduledata

View File

@@ -15,12 +15,14 @@ package broken
// A broken package will list in JSON form like:
// {..., "Incomplete": true, "Error": {...}, ...}
var x string = 123
-- imports_broken/imports.go --
package imports_broken
// Importing a broken package will list in JSON form like:
// {..., "Incomplete": false, ...}
import _ "test/main/broken"
-- imports_missing/imports.go --
package imports_missing

View File

@@ -72,9 +72,9 @@ import (
"test/main/imp"
. "test/main/imp_const"
. "test/main/imp_func"
. "test/main/imp_var"
. "test/main/imp_type"
. "test/main/imp_struct"
. "test/main/imp_type"
. "test/main/imp_var"
)
type structTest struct {
@@ -336,17 +336,17 @@ func shadowTest() {
}
func dotImportTest() {
println(DotImportedStr)
println(DotImportedFunc())
println(DotImportedVar)
println(DotImportedType("str as dot imported type"))
println(DotImportedStruct.Str)
println(DotImportedStr)
println(DotImportedFunc())
println(DotImportedVar)
println(DotImportedType("str as dot imported type"))
println(DotImportedStruct.Str)
}
func multipleTimeImportTest() {
regularAndUnusedRenamed()
regularAndUnusedDotImport()
unusedRegularAndDotImport()
regularAndUnusedRenamed()
regularAndUnusedDotImport()
unusedRegularAndDotImport()
}
func noop(...any) {}
@@ -355,45 +355,46 @@ func noop(...any) {}
package main
import (
"test/main/imp_mult"
imp_mult2 "test/main/imp_mult"
"test/main/imp_mult"
imp_mult2 "test/main/imp_mult"
)
func regularAndUnusedRenamed() {
imp_mult.MultDummy()
noop(imp_mult.MultImpStr, imp_mult2.MultImpStr)
imp_mult.MultDummy()
noop(imp_mult.MultImpStr, imp_mult2.MultImpStr)
}
-- regular_and_unused_dotimport.go --
package main
import (
"test/main/imp_mult"
. "test/main/imp_mult"
"test/main/imp_mult"
. "test/main/imp_mult"
)
func regularAndUnusedDotImport() {
imp_mult.MultDummy()
noop(imp_mult.MultImpStr, MultImpStr)
imp_mult.MultDummy()
noop(imp_mult.MultImpStr, MultImpStr)
}
-- unused_regular_and_dotimport.go --
package main
import (
"test/main/imp_mult"
. "test/main/imp_mult"
"test/main/imp_mult"
. "test/main/imp_mult"
)
func unusedRegularAndDotImport() {
MultDummy()
noop(imp_mult.MultImpStr, MultImpStr)
MultDummy()
noop(imp_mult.MultImpStr, MultImpStr)
}
-- imp/imported.go --
package imported
type ImportedType int
-- imp_const/imported.go --
package imp_const

View File

@@ -37,10 +37,12 @@ var PublicVar int = lib.ImportedFunc()
func privateFunc(n int) { println("Hello, number", n) }
func PublicFunc() { privateFunc(PublicVar) }
-- plugin/lib/lib.go --
package lib
func ImportedFunc() int { return 4 }
-- main.go --
package main
@@ -62,5 +64,6 @@ func main() {
*v.(*int) = 7
f.(func())()
}
-- main.stderr --
Hello, number 7

View File

@@ -68,6 +68,7 @@ func issue_573(s struct{ f int }) {
var _ *int = &s.f
/*x*/
}
-- garble_other_filename.go --
package main

View File

@@ -30,10 +30,10 @@ import (
"math/big"
"os"
"reflect"
"unsafe"
"strings"
"sync"
"text/template"
"unsafe"
"test/main/importedpkg"
"test/main/importedpkg2"
@@ -66,7 +66,7 @@ func main() {
// Another common library, text/template.
tmpl := template.Must(template.New("").Parse("Hello {{.Name}}."))
_ = tmpl.Execute(os.Stdout, struct{Name string}{Name: "Dave"})
_ = tmpl.Execute(os.Stdout, struct{ Name string }{Name: "Dave"})
fmt.Println() // Always print a newline.
// Another complex case, involving embedding and another package.
@@ -133,20 +133,20 @@ func main() {
// Local names not used in reflection should not be in the final binary,
// even if they are embedded in a struct and become a field name.
type unexportedLocalObfuscated struct { LocalObfuscatedA int }
type ExportedLocalObfuscated struct { LocalObfuscatedB int }
type unexportedLocalObfuscated struct{ LocalObfuscatedA int }
type ExportedLocalObfuscated struct{ LocalObfuscatedB int }
type EmbeddingObfuscated struct {
unexportedLocalObfuscated
ExportedLocalObfuscated
}
// Ensure the types are kept in the binary. Use an anonymous type too.
_ = fmt.Sprintf("%#v", EmbeddingObfuscated{})
_ = fmt.Sprintf("%#v", struct{ExportedLocalObfuscated}{})
_ = fmt.Sprintf("%#v", struct{ ExportedLocalObfuscated }{})
// reflection can see all type names, even local ones, so they cannot be obfuscated.
{
type TypeOfNamedField struct { NamedReflectionField int }
type TypeOfEmbeddedField struct { EmbeddedReflectionField int }
type TypeOfNamedField struct{ NamedReflectionField int }
type TypeOfEmbeddedField struct{ EmbeddedReflectionField int }
type TypeOfParent struct {
ReflectionField TypeOfNamedField
TypeOfEmbeddedField
@@ -178,7 +178,7 @@ func main() {
type EmbeddingIndirect struct {
// With field names, to test selectors above.
With importedpkg.AliasIndirectNamedWithReflect
With importedpkg.AliasIndirectNamedWithReflect
Without importedpkg.AliasIndirectNamedWithoutReflect
// Embedding used to crash garble, too.
@@ -348,7 +348,6 @@ func reflectUnrelatedConv() {
}
type StatUser struct {
Id int64 `gorm:"primaryKey"`
User_Id int64
@@ -545,7 +544,7 @@ var ReflectInDefinedVar = ReflectInDefined{ExportedField2: 9000}
var _ = reflect.TypeOf(ReflectInDefinedVar)
var _ = reflect.TypeOf([]*struct{EmbeddingOuter}{})
var _ = reflect.TypeOf([]*struct{ EmbeddingOuter }{})
type EmbeddingOuter struct {
EmbeddingInner
@@ -563,7 +562,7 @@ type UnnamedWithDownstreamReflect = struct {
}
type (
AliasIndirectNamedWithReflect = indirect.IndirectNamedWithReflect
AliasIndirectNamedWithReflect = indirect.IndirectNamedWithReflect
AliasIndirectNamedWithoutReflect = indirect.IndirectNamedWithoutReflect
)

View File

@@ -74,6 +74,7 @@ func unexportedMainFunc() {
}
anonFunc()
}
-- lib/long_lib.go --
package lib
@@ -85,7 +86,7 @@ import (
"runtime/debug"
)
type ExportedLibType struct{
type ExportedLibType struct {
ExportedLibField int
}
@@ -118,6 +119,7 @@ func printStackTrace(w io.Writer) error {
_, err := w.Write(bytes.Join(stackLines, []byte("\n")))
return err
}
-- reverse.stdout --
lib filename: test/main/lib/long_lib.go

View File

@@ -49,6 +49,7 @@ package main
import garbletest "gopkg.in/garbletest.v2"
func main() { garbletest.Test() }
-- mod2/go.mod --
module test/main/mod2

View File

@@ -134,10 +134,10 @@ func mainFunc() {
for _, name := range hashedNames {
name = name[len("main."):]
if len(name) < 6 {
panic("ended up with a hashed name that's too short: "+name)
panic("ended up with a hashed name that's too short: " + name)
}
if len(name) > 12 {
panic("ended up with a hashed name that's too long: "+name)
panic("ended up with a hashed name that's too long: " + name)
}
count[len(name)]++
if count[len(name)] >= 14 {
@@ -218,6 +218,7 @@ func CallerFuncName() string {
fn := runtime.FuncForPC(pc)
return fn.Name()
}
-- main.stderr --
teststring
imported var value

View File

@@ -23,6 +23,7 @@ package extra
func Func() string {
return "This is a separate module to obfuscate."
}
-- go.mod --
module test/main
@@ -129,7 +130,7 @@ func main() {
_ = sub.EmbeddingExternalForeignAlias{
ExternalForeignAlias: nil,
Reader: nil,
Reader: nil,
}
var emb sub.EmbeddingAlias
@@ -185,7 +186,7 @@ var someGlobalVar2 = "2"
func Test() {
var A, B, C, D, E string
noop(A, B, C, D, E)
if someGlobalVar0 != "0" || someGlobalVar1 != "1" || someGlobalVar2 != "2"{
if someGlobalVar0 != "0" || someGlobalVar1 != "1" || someGlobalVar2 != "2" {
panic("name collision detected")
}
}
@@ -221,7 +222,7 @@ type foreignAlias = io.Reader
var _ = embeddingForeignAlias{
foreignAlias: nil,
Reader: nil,
Reader: nil,
}
// Similar to embeddingForeignAlias,

View File

@@ -70,6 +70,7 @@ func OriginalFuncName() string {
fn := runtime.FuncForPC(pc)
return fn.Name()
}
-- bar_test.go --
package bar
@@ -82,6 +83,7 @@ func TestFoo(t *testing.T) {
}
t.Logf("package bar, func name: %s", OriginalFuncName())
}
-- bar_separate_test.go --
package bar_test
@@ -107,6 +109,7 @@ func TestWithFlag(t *testing.T) {
t.Skip()
}
}
-- main_test.go --
package bar
@@ -118,34 +121,41 @@ import (
func TestMain(m *testing.M) {
os.Exit(m.Run())
}
-- somemain/main.go --
package main
func main() {}
-- somemaintest/main.go --
package main
func main() {}
-- somemaintest/main_test.go --
package main
import "testing"
func TestMain(t *testing.T) {}
-- sometest/foo_test.go --
package sometest
import "testing"
func TestFoo(t *testing.T) {}
-- exporttest/foo.go --
package exporttest
type foo int
-- exporttest/export_test.go --
package exporttest
type Foo = foo
-- exporttest/foo_test.go --
package exporttest_test

View File

@@ -36,7 +36,7 @@ import (
"runtime"
)
type testStruct struct {}
type testStruct struct{}
func (testStruct) unexportedFunc() { println("dummy") }