You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.6 KiB

  1. package os_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "syscall"
  10. "testing"
  11. "time"
  12. "github.com/stretchr/testify/require"
  13. tmos "github.com/tendermint/tendermint/libs/os"
  14. )
  15. func TestCopyFile(t *testing.T) {
  16. tmpfile, err := ioutil.TempFile("", "example")
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. defer os.Remove(tmpfile.Name())
  21. content := []byte("hello world")
  22. if _, err := tmpfile.Write(content); err != nil {
  23. t.Fatal(err)
  24. }
  25. copyfile := fmt.Sprintf("%s.copy", tmpfile.Name())
  26. if err := tmos.CopyFile(tmpfile.Name(), copyfile); err != nil {
  27. t.Fatal(err)
  28. }
  29. if _, err := os.Stat(copyfile); os.IsNotExist(err) {
  30. t.Fatal("copy should exist")
  31. }
  32. data, err := ioutil.ReadFile(copyfile)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if !bytes.Equal(data, content) {
  37. t.Fatalf("copy file content differs: expected %v, got %v", content, data)
  38. }
  39. os.Remove(copyfile)
  40. }
  41. func TestTrapSignal(t *testing.T) {
  42. if os.Getenv("TM_TRAP_SIGNAL_TEST") == "1" {
  43. t.Log("inside test process")
  44. killer()
  45. return
  46. }
  47. cmd, _, mockStderr := newTestProgram(t, "TM_TRAP_SIGNAL_TEST")
  48. err := cmd.Run()
  49. if err == nil {
  50. wantStderr := "exiting"
  51. if mockStderr.String() != wantStderr {
  52. t.Fatalf("stderr: want %q, got %q", wantStderr, mockStderr.String())
  53. }
  54. return
  55. }
  56. if e, ok := err.(*exec.ExitError); ok && !e.Success() {
  57. t.Fatalf("wrong exit code, want 0, got %d", e.ExitCode())
  58. }
  59. t.Fatal("this error should not be triggered")
  60. }
  61. func TestEnsureDir(t *testing.T) {
  62. tmp, err := ioutil.TempDir("", "ensure-dir")
  63. require.NoError(t, err)
  64. defer os.RemoveAll(tmp)
  65. // Should be possible to create a new directory.
  66. err = tmos.EnsureDir(filepath.Join(tmp, "dir"), 0755)
  67. require.NoError(t, err)
  68. require.DirExists(t, filepath.Join(tmp, "dir"))
  69. // Should succeed on existing directory.
  70. err = tmos.EnsureDir(filepath.Join(tmp, "dir"), 0755)
  71. require.NoError(t, err)
  72. // Should fail on file.
  73. err = ioutil.WriteFile(filepath.Join(tmp, "file"), []byte{}, 0644)
  74. require.NoError(t, err)
  75. err = tmos.EnsureDir(filepath.Join(tmp, "file"), 0755)
  76. require.Error(t, err)
  77. // Should allow symlink to dir.
  78. err = os.Symlink(filepath.Join(tmp, "dir"), filepath.Join(tmp, "linkdir"))
  79. require.NoError(t, err)
  80. err = tmos.EnsureDir(filepath.Join(tmp, "linkdir"), 0755)
  81. require.NoError(t, err)
  82. // Should error on symlink to file.
  83. err = os.Symlink(filepath.Join(tmp, "file"), filepath.Join(tmp, "linkfile"))
  84. require.NoError(t, err)
  85. err = tmos.EnsureDir(filepath.Join(tmp, "linkfile"), 0755)
  86. require.Error(t, err)
  87. }
  88. type mockLogger struct{}
  89. func (ml mockLogger) Info(msg string, keyvals ...interface{}) {}
  90. func killer() {
  91. logger := mockLogger{}
  92. tmos.TrapSignal(logger, func() { _, _ = fmt.Fprintf(os.Stderr, "exiting") })
  93. time.Sleep(1 * time.Second)
  94. p, err := os.FindProcess(os.Getpid())
  95. if err != nil {
  96. panic(err)
  97. }
  98. if err := p.Signal(syscall.SIGTERM); err != nil {
  99. panic(err)
  100. }
  101. time.Sleep(1 * time.Second)
  102. }
  103. func newTestProgram(t *testing.T, environVar string) (cmd *exec.Cmd, stdout *bytes.Buffer, stderr *bytes.Buffer) {
  104. t.Helper()
  105. cmd = exec.Command(os.Args[0], "-test.run="+t.Name())
  106. stdout, stderr = bytes.NewBufferString(""), bytes.NewBufferString("")
  107. cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", environVar))
  108. cmd.Stdout = stdout
  109. cmd.Stderr = stderr
  110. return
  111. }
  112. // Ensure that using CopyFile does not truncate the destination file before
  113. // the origin is positively a non-directory and that it is ready for copying.
  114. // See https://github.com/tendermint/tendermint/issues/6427
  115. func TestTrickedTruncation(t *testing.T) {
  116. tmpDir, err := ioutil.TempDir(os.TempDir(), "pwn_truncate")
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. defer os.Remove(tmpDir)
  121. originalWALPath := filepath.Join(tmpDir, "wal")
  122. originalWALContent := []byte("I AM BECOME DEATH, DESTROYER OF ALL WORLDS!")
  123. if err := ioutil.WriteFile(originalWALPath, originalWALContent, 0755); err != nil {
  124. t.Fatal(err)
  125. }
  126. // 1. Sanity check.
  127. readWAL, err := ioutil.ReadFile(originalWALPath)
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. if !bytes.Equal(readWAL, originalWALContent) {
  132. t.Fatalf("Cannot proceed as the content does not match\nGot: %q\nWant: %q", readWAL, originalWALContent)
  133. }
  134. // 2. Now cause the truncation of the original file.
  135. // It is absolutely legal to invoke os.Open on a directory.
  136. if err := tmos.CopyFile(tmpDir, originalWALPath); err == nil {
  137. t.Fatal("Expected an error")
  138. }
  139. // 3. Check the WAL's content
  140. reReadWAL, err := ioutil.ReadFile(originalWALPath)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. if !bytes.Equal(reReadWAL, originalWALContent) {
  145. t.Fatalf("Oops, the WAL's content was changed :(\nGot: %q\nWant: %q", reReadWAL, originalWALContent)
  146. }
  147. }