1 // Copyright 2016 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
4 // The go-ethereum library is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Lesser General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // The go-ethereum library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
33 func getnacl() (string, error) {
34 switch runtime.GOARCH {
36 _, err := exec.LookPath("sel_ldr_x86_64")
37 return "amd64p32", err
39 _, err := exec.LookPath("sel_ldr_i386")
42 return "", errors.New("nacl is not supported on " + runtime.GOARCH)
46 // runWithPlaygroundTime executes the caller
47 // in the NaCl sandbox with faketime enabled.
49 // This function must be called from a Test* function
50 // and the caller must skip the actual test when isHost is true.
51 func runWithPlaygroundTime(t *testing.T) (isHost bool) {
52 if runtime.GOOS == "nacl" {
57 callerPC, _, _, ok := runtime.Caller(1)
59 panic("can't get caller")
61 callerFunc := runtime.FuncForPC(callerPC)
62 if callerFunc == nil {
63 panic("can't get caller")
65 callerName := callerFunc.Name()[strings.LastIndexByte(callerFunc.Name(), '.')+1:]
66 if !strings.HasPrefix(callerName, "Test") {
67 panic("must be called from witin a Test* function")
69 testPattern := "^" + callerName + "$"
71 // Unfortunately runtime.faketime (playground time mode) only works on NaCl. The NaCl
72 // SDK must be installed and linked into PATH for this to work.
73 arch, err := getnacl()
78 // Compile and run the calling test using NaCl.
79 // The extra tag ensures that the TestMain function in sim_main_test.go is used.
80 cmd := exec.Command("go", "test", "-v", "-tags", "faketime_simulation", "-timeout", "100h", "-run", testPattern, ".")
81 cmd.Env = append([]string{"GOOS=nacl", "GOARCH=" + arch}, os.Environ()...)
82 stdout, _ := cmd.StdoutPipe()
83 stderr, _ := cmd.StderrPipe()
84 go skipPlaygroundOutputHeaders(os.Stdout, stdout)
85 go skipPlaygroundOutputHeaders(os.Stderr, stderr)
86 if err := cmd.Run(); err != nil {
90 // Ensure that the test function doesn't run in the (non-NaCl) host process.
94 func skipPlaygroundOutputHeaders(out io.Writer, in io.Reader) {
95 // Additional output can be printed without the headers
96 // before the NaCl binary starts running (e.g. compiler error messages).
97 bufin := bufio.NewReader(in)
98 output, err := bufin.ReadBytes(0)
99 output = bytes.TrimSuffix(output, []byte{0})
108 // Playback header: 0 0 P B <8-byte time> <4-byte data length>
109 head := make([]byte, 4+8+4)
111 if _, err := io.ReadFull(bufin, head); err != nil {
113 fmt.Fprintln(out, "read error:", err)
117 if !bytes.HasPrefix(head, []byte{0x00, 0x00, 'P', 'B'}) {
118 fmt.Fprintf(out, "expected playback header, got %q\n", head)
122 // Copy data until next header.
123 size := binary.BigEndian.Uint32(head[12:])
124 io.CopyN(out, bufin, int64(size))