You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
4.6 KiB
Plaintext
188 lines
4.6 KiB
Plaintext
3 years ago
|
env GOGARBLE=test/main
|
||
5 years ago
|
|
||
3 years ago
|
# Note that in this test we use "! bincmp" on plaintext output files,
|
||
|
# as a workaround for "cmp" not supporting "! cmp".
|
||
3 years ago
|
# TODO: now that obfuscation with -seed is deterministic,
|
||
|
# can we just rely on the regular "cmp" with fixed output files?
|
||
3 years ago
|
|
||
3 years ago
|
# TODO: consider setting these seeds globally,
|
||
|
# so we can reuse them across tests and make better use of the shared build cache.
|
||
4 years ago
|
env SEED1=OQg9kACEECQ
|
||
|
env SEED2=NruiDmVz6/s
|
||
|
|
||
3 years ago
|
# Check the binary with a given base64 encoded seed.
|
||
4 years ago
|
garble -seed=${SEED1} build
|
||
5 years ago
|
exec ./main$exe
|
||
4 years ago
|
cmp stderr main.stderr
|
||
|
binsubstr main$exe 'teststring' 'imported var value'
|
||
3 years ago
|
! binsubstr main$exe 'ImportedVar' ${SEED1}
|
||
4 years ago
|
|
||
|
[short] stop # the extra checks are relatively expensive
|
||
5 years ago
|
|
||
3 years ago
|
exec ./main$exe test/main/imported
|
||
|
cp stderr importedpkg-seed-static-1
|
||
5 years ago
|
|
||
|
# Also check that the binary is reproducible.
|
||
5 years ago
|
# No packages should be rebuilt either, thanks to the build cache.
|
||
3 years ago
|
cp main$exe main_seed1$exe
|
||
5 years ago
|
rm main$exe
|
||
4 years ago
|
garble -seed=${SEED1}= build -v
|
||
3 years ago
|
! stderr .
|
||
3 years ago
|
bincmp main$exe main_seed1$exe
|
||
|
|
||
|
exec ./main$exe test/main/imported
|
||
|
cmp stderr importedpkg-seed-static-1
|
||
5 years ago
|
|
||
3 years ago
|
# Even if we use the same seed, the same names in a different package
|
||
|
# should still be obfuscated in a different way.
|
||
|
exec ./main$exe test/main
|
||
|
cp stderr mainpkg-seed-static-1
|
||
|
! bincmp mainpkg-seed-static-1 importedpkg-seed-static-1
|
||
|
|
||
|
# Using different flags which affect the build, such as -literals or -tiny,
|
||
|
# should result in the same obfuscation as long as the seed is constant.
|
||
|
# TODO: also test that changing non-garble build parameters,
|
||
|
# such as GOARCH or -tags, still results in the same hashing via the seed.
|
||
|
|
||
|
garble -seed=${SEED1} -literals build
|
||
|
exec ./main$exe test/main/imported
|
||
|
cmp stderr importedpkg-seed-static-1
|
||
|
|
||
|
garble -seed=${SEED1} -tiny build
|
||
|
exec ./main$exe test/main/imported
|
||
|
cmp stderr importedpkg-seed-static-1
|
||
4 years ago
|
|
||
5 years ago
|
# Also check that a different seed leads to a different binary.
|
||
|
# We can't know if caching happens here, because of previous test runs.
|
||
3 years ago
|
cp main$exe main_seed2$exe
|
||
5 years ago
|
rm main$exe
|
||
4 years ago
|
garble -seed=${SEED2} build
|
||
3 years ago
|
! bincmp main$exe main_seed2$exe
|
||
5 years ago
|
|
||
3 years ago
|
exec ./main$exe test/main/imported
|
||
|
cp stderr importedpkg-seed-static-2
|
||
|
! bincmp importedpkg-seed-static-2 importedpkg-seed-static-1
|
||
4 years ago
|
|
||
5 years ago
|
# Use a random seed, which should always trigger a full build.
|
||
4 years ago
|
garble -seed=random build -v
|
||
|
stderr -count=1 '^runtime$'
|
||
|
stderr -count=1 '^test/main$'
|
||
5 years ago
|
exec ./main$exe
|
||
4 years ago
|
cmp stderr main.stderr
|
||
|
binsubstr main$exe 'teststring' 'imported var value'
|
||
|
! binsubstr main$exe 'ImportedVar'
|
||
|
|
||
3 years ago
|
exec ./main$exe test/main/imported
|
||
|
cp stderr importedpkg-seed-random-1
|
||
|
! bincmp importedpkg-seed-random-1 importedpkg-seed-static-1
|
||
5 years ago
|
|
||
|
# Also check that the random binary is not reproducible.
|
||
3 years ago
|
cp main$exe main_random$exe
|
||
5 years ago
|
rm main$exe
|
||
4 years ago
|
garble -seed=random build -v
|
||
5 years ago
|
stderr .
|
||
3 years ago
|
! bincmp main$exe main_random$exe
|
||
4 years ago
|
|
||
3 years ago
|
exec ./main$exe test/main/imported
|
||
|
cp stderr importedpkg-seed-random-2
|
||
|
! bincmp importedpkg-seed-random-2 importedpkg-seed-random-1
|
||
4 years ago
|
|
||
3 years ago
|
# Finally, ensure that our runtime and reflect test code does what we think.
|
||
|
go build
|
||
|
exec ./main$exe
|
||
|
cmp stderr main.stderr
|
||
|
exec ./main$exe test/main
|
||
|
cmp stderr mainpkg.stderr
|
||
|
exec ./main$exe test/main/imported
|
||
|
cmp stderr importedpkg.stderr
|
||
4 years ago
|
|
||
5 years ago
|
-- go.mod --
|
||
|
module test/main
|
||
5 years ago
|
|
||
3 years ago
|
go 1.18
|
||
5 years ago
|
-- main.go --
|
||
|
package main
|
||
|
|
||
4 years ago
|
import (
|
||
|
"os"
|
||
|
|
||
|
"test/main/imported"
|
||
|
)
|
||
5 years ago
|
|
||
5 years ago
|
var teststringVar = "teststring"
|
||
|
|
||
3 years ago
|
func main() { mainFunc() }
|
||
|
|
||
|
func mainFunc() {
|
||
|
if len(os.Args) > 1 {
|
||
|
switch os.Args[1] {
|
||
|
case "test/main":
|
||
|
imported.PrintNames(NamedTypeValue, NamedFunc)
|
||
|
case "test/main/imported":
|
||
|
imported.PrintNames(imported.NamedType{}, imported.NamedFunc)
|
||
|
default:
|
||
|
panic("unknown package")
|
||
|
}
|
||
4 years ago
|
} else {
|
||
|
println(teststringVar)
|
||
|
println(imported.ImportedVar)
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
// A workaround to fool garble's reflect detection,
|
||
|
// because we want it to show us the obfuscated NamedType.
|
||
3 years ago
|
var NamedTypeValue any = NamedType{}
|
||
3 years ago
|
|
||
|
type NamedType struct {
|
||
|
NamedField int
|
||
|
}
|
||
|
|
||
|
func NamedFunc() string {
|
||
|
return imported.CallerFuncName()
|
||
5 years ago
|
}
|
||
3 years ago
|
|
||
5 years ago
|
-- imported/imported.go --
|
||
|
package imported
|
||
|
|
||
3 years ago
|
import (
|
||
|
"reflect"
|
||
|
"runtime"
|
||
|
)
|
||
|
|
||
5 years ago
|
var ImportedVar = "imported var value"
|
||
5 years ago
|
|
||
3 years ago
|
type NamedType struct {
|
||
|
NamedField int
|
||
|
}
|
||
|
|
||
|
func NamedFunc() string {
|
||
|
return CallerFuncName()
|
||
|
}
|
||
|
|
||
3 years ago
|
func PrintNames(v any, fn func() string) {
|
||
3 years ago
|
typ := reflect.TypeOf(v)
|
||
|
println("path:", typ.PkgPath())
|
||
|
println("type:", typ.Name())
|
||
|
println("field:", typ.Field(0).Name)
|
||
|
println("func: ", fn())
|
||
|
}
|
||
|
|
||
|
func CallerFuncName() string {
|
||
|
pc, _, _, _ := runtime.Caller(1)
|
||
|
fn := runtime.FuncForPC(pc)
|
||
|
return fn.Name()
|
||
|
}
|
||
4 years ago
|
-- main.stderr --
|
||
5 years ago
|
teststring
|
||
5 years ago
|
imported var value
|
||
3 years ago
|
-- mainpkg.stderr --
|
||
|
path: main
|
||
|
type: NamedType
|
||
|
field: NamedField
|
||
|
func: main.NamedFunc
|
||
|
-- importedpkg.stderr --
|
||
|
path: test/main/imported
|
||
|
type: NamedType
|
||
|
field: NamedField
|
||
|
func: test/main/imported.NamedFunc
|