sim_run_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  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.
  8. //
  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.
  13. //
  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/>.
  16. package discv5
  17. import (
  18. "bufio"
  19. "bytes"
  20. "encoding/binary"
  21. "errors"
  22. "fmt"
  23. "io"
  24. "os"
  25. "os/exec"
  26. "runtime"
  27. "strings"
  28. "testing"
  29. )
  30. func getnacl() (string, error) {
  31. switch runtime.GOARCH {
  32. case "amd64":
  33. _, err := exec.LookPath("sel_ldr_x86_64")
  34. return "amd64p32", err
  35. case "i386":
  36. _, err := exec.LookPath("sel_ldr_i386")
  37. return "i386", err
  38. default:
  39. return "", errors.New("nacl is not supported on " + runtime.GOARCH)
  40. }
  41. }
  42. // runWithPlaygroundTime executes the caller
  43. // in the NaCl sandbox with faketime enabled.
  44. //
  45. // This function must be called from a Test* function
  46. // and the caller must skip the actual test when isHost is true.
  47. func runWithPlaygroundTime(t *testing.T) (isHost bool) {
  48. if runtime.GOOS == "nacl" {
  49. return false
  50. }
  51. // Get the caller.
  52. callerPC, _, _, ok := runtime.Caller(1)
  53. if !ok {
  54. panic("can't get caller")
  55. }
  56. callerFunc := runtime.FuncForPC(callerPC)
  57. if callerFunc == nil {
  58. panic("can't get caller")
  59. }
  60. callerName := callerFunc.Name()[strings.LastIndexByte(callerFunc.Name(), '.')+1:]
  61. if !strings.HasPrefix(callerName, "Test") {
  62. panic("must be called from witin a Test* function")
  63. }
  64. testPattern := "^" + callerName + "$"
  65. // Unfortunately runtime.faketime (playground time mode) only works on NaCl. The NaCl
  66. // SDK must be installed and linked into PATH for this to work.
  67. arch, err := getnacl()
  68. if err != nil {
  69. t.Skip(err)
  70. }
  71. // Compile and run the calling test using NaCl.
  72. // The extra tag ensures that the TestMain function in sim_main_test.go is used.
  73. cmd := exec.Command("go", "test", "-v", "-tags", "faketime_simulation", "-timeout", "100h", "-run", testPattern, ".")
  74. cmd.Env = append([]string{"GOOS=nacl", "GOARCH=" + arch}, os.Environ()...)
  75. stdout, _ := cmd.StdoutPipe()
  76. stderr, _ := cmd.StderrPipe()
  77. go skipPlaygroundOutputHeaders(os.Stdout, stdout)
  78. go skipPlaygroundOutputHeaders(os.Stderr, stderr)
  79. if err := cmd.Run(); err != nil {
  80. t.Error(err)
  81. }
  82. // Ensure that the test function doesn't run in the (non-NaCl) host process.
  83. return true
  84. }
  85. func skipPlaygroundOutputHeaders(out io.Writer, in io.Reader) {
  86. // Additional output can be printed without the headers
  87. // before the NaCl binary starts running (e.g. compiler error messages).
  88. bufin := bufio.NewReader(in)
  89. output, err := bufin.ReadBytes(0)
  90. output = bytes.TrimSuffix(output, []byte{0})
  91. if len(output) > 0 {
  92. out.Write(output)
  93. }
  94. if err != nil {
  95. return
  96. }
  97. bufin.UnreadByte()
  98. // Playback header: 0 0 P B <8-byte time> <4-byte data length>
  99. head := make([]byte, 4+8+4)
  100. for {
  101. if _, err := io.ReadFull(bufin, head); err != nil {
  102. if err != io.EOF {
  103. fmt.Fprintln(out, "read error:", err)
  104. }
  105. return
  106. }
  107. if !bytes.HasPrefix(head, []byte{0x00, 0x00, 'P', 'B'}) {
  108. fmt.Fprintf(out, "expected playback header, got %q\n", head)
  109. io.Copy(out, bufin)
  110. return
  111. }
  112. // Copy data until next header.
  113. size := binary.BigEndian.Uint32(head[12:])
  114. io.CopyN(out, bufin, int64(size))
  115. }
  116. }