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 `testdata/scripts/`, which allows laying out files and shell-like steps to run as
part of the test. 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 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: 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)) println(imported.AddImpl(3, 4))
} }
-- garble_main_amd64.s -- -- garble_main_amd64.s --
#include "garble_define_amd64.h" #include "garble_define_amd64.h"

View File

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

View File

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

View File

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

View File

@@ -46,37 +46,38 @@ func func1() {}
//garble:controlflow flatten_passes=1 junk_jumps=max block_splits=max flatten_hardening=xor //garble:controlflow flatten_passes=1 junk_jumps=max block_splits=max flatten_hardening=xor
func xorHardeningTest(i int) int { func xorHardeningTest(i int) int {
if i == 0 { if i == 0 {
return 1 return 1
} }
return i * 2; return i * 2
} }
//garble:controlflow flatten_passes=1 junk_jumps=max block_splits=max flatten_hardening=delegate_table //garble:controlflow flatten_passes=1 junk_jumps=max block_splits=max flatten_hardening=delegate_table
func delegateHardeningTest(i int) int { func delegateHardeningTest(i int) int {
if i == 0 { if i == 0 {
return 1 return 1
} }
return i * 3; 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 // 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 { func multiHardeningTest(i int) int {
notZero := func(i int) bool { notZero := func(i int) bool {
return i != 0 return i != 0
} }
isZero := func(i int) bool { isZero := func(i int) bool {
return i == 0 return i == 0
} }
multiply := func(i int) int { multiply := func(i int) int {
return i * 4 return i * 4
} }
if !notZero(i) && isZero(i) { if !notZero(i) && isZero(i) {
return 1 return 1
} }
return multiply(i); return multiply(i)
} }
//garble:controlflow //garble:controlflow

View File

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

View File

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

View File

@@ -47,14 +47,14 @@ var _ privateInterface = T("")
type StructUnnamed = struct { type StructUnnamed = struct {
Foo int Foo int
Bar []*struct { Bar []*struct {
Nested *[]string Nested *[]string
NestedTagged string NestedTagged string
NestedAlias int64 // we can skip the alias too NestedAlias int64 // we can skip the alias too
} }
Named lib3.Named Named lib3.Named
lib3.StructEmbed lib3.StructEmbed
Tagged string // no field tag 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{}) var _ = lib1.Struct1(lib2.Struct2{})
@@ -88,14 +88,14 @@ import "test/main/lib3"
type Struct1 struct { type Struct1 struct {
Foo int Foo int
Bar []*struct { Bar []*struct {
Nested *[]string Nested *[]string
NestedTagged string `json:"tagged1"` NestedTagged string `json:"tagged1"`
NestedAlias alias1 NestedAlias alias1
} }
Named lib3.Named Named lib3.Named
lib3.StructEmbed lib3.StructEmbed
Tagged string `json:"tagged1"` Tagged string `json:"tagged1"`
Alias alias1 Alias alias1
} }
type alias1 = int64 type alias1 = int64
@@ -108,17 +108,18 @@ import "test/main/lib3"
type Struct2 struct { type Struct2 struct {
Foo int Foo int
Bar []*struct { Bar []*struct {
Nested *[]string Nested *[]string
NestedTagged string `json:"tagged2"` NestedTagged string `json:"tagged2"`
NestedAlias alias2 NestedAlias alias2
} }
Named lib3.Named Named lib3.Named
lib3.StructEmbed lib3.StructEmbed
Tagged string `json:"tagged2"` Tagged string `json:"tagged2"`
Alias alias2 Alias alias2
} }
type alias2 = int64 type alias2 = int64
-- lib3/lib3.go -- -- lib3/lib3.go --
package lib3 package lib3
@@ -127,6 +128,7 @@ type Named int
type StructEmbed struct { type StructEmbed struct {
Baz any Baz any
} }
-- main.stdout -- -- main.stdout --
String method for foo String method for foo
unexported 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 package main
import ( import (
"fmt"
"database/sql" "database/sql"
"fmt"
"test/main/importedpkg" "test/main/importedpkg"
"rsc.io/quote"
garbletest "gopkg.in/garbletest.v2" garbletest "gopkg.in/garbletest.v2"
garbletestconst "gopkg.in/garbletestconst.v2" garbletestconst "gopkg.in/garbletestconst.v2"
"rsc.io/quote"
) )
func main() { func main() {
@@ -95,12 +95,14 @@ func main() {
fmt.Println(garbletestconst.StrConst) fmt.Println(garbletestconst.StrConst)
fmt.Println(sql.Drivers()[0]) fmt.Println(sql.Drivers()[0])
} }
-- notag_fail.go -- -- notag_fail.go --
//go:build !buildtag //go:build !buildtag
package main package main
var foo int = "should be omitted by -tags" var foo int = "should be omitted by -tags"
-- withtag_success.go -- -- withtag_success.go --
//go:build buildtag //go:build buildtag
@@ -109,6 +111,7 @@ package main
import "fmt" import "fmt"
func init() { fmt.Println("buildtag init func") } func init() { fmt.Println("buildtag init func") }
-- differentpkg_unnamed.go -- -- differentpkg_unnamed.go --
package main package main
@@ -119,12 +122,14 @@ import (
var _ = actualpkgname.Noop var _ = actualpkgname.Noop
var _ = goextension.Noop var _ = goextension.Noop
-- differentpkg_named.go -- -- differentpkg_named.go --
package main package main
import named "test/main/different-pkg-name" import named "test/main/different-pkg-name"
var _ = named.Noop var _ = named.Noop
-- importedpkg/imported.go -- -- importedpkg/imported.go --
package importedpkg package importedpkg
@@ -172,10 +177,12 @@ import (
var _ indirect.Indirect var _ indirect.Indirect
var _ = another.Blank var _ = another.Blank
-- importedpkg/another/pkg.go -- -- importedpkg/another/pkg.go --
package another package another
const Blank = 3 const Blank = 3
-- importedpkg/indirect/indirect.go -- -- importedpkg/indirect/indirect.go --
package indirect package indirect
@@ -192,6 +199,7 @@ var Noop int
package goextension package goextension
var Noop int var Noop int
-- main.stdout -- -- main.stdout --
buildtag init func buildtag init func
imported var value imported var value

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -134,10 +134,10 @@ func mainFunc() {
for _, name := range hashedNames { for _, name := range hashedNames {
name = name[len("main."):] name = name[len("main."):]
if len(name) < 6 { 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 { 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)]++ count[len(name)]++
if count[len(name)] >= 14 { if count[len(name)] >= 14 {
@@ -218,6 +218,7 @@ func CallerFuncName() string {
fn := runtime.FuncForPC(pc) fn := runtime.FuncForPC(pc)
return fn.Name() return fn.Name()
} }
-- main.stderr -- -- main.stderr --
teststring teststring
imported var value imported var value

View File

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

View File

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

View File

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