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.

109 lines
3.0 KiB

  1. package os_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. tmos "github.com/tendermint/tendermint/libs/os"
  10. )
  11. func TestCopyFile(t *testing.T) {
  12. tmpfile, err := os.CreateTemp(t.TempDir(), "example")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. defer os.Remove(tmpfile.Name())
  17. content := []byte("hello world")
  18. if _, err := tmpfile.Write(content); err != nil {
  19. t.Fatal(err)
  20. }
  21. copyfile := fmt.Sprintf("%s.copy", tmpfile.Name())
  22. if err := tmos.CopyFile(tmpfile.Name(), copyfile); err != nil {
  23. t.Fatal(err)
  24. }
  25. if _, err := os.Stat(copyfile); os.IsNotExist(err) {
  26. t.Fatal("copy should exist")
  27. }
  28. data, err := os.ReadFile(copyfile)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if !bytes.Equal(data, content) {
  33. t.Fatalf("copy file content differs: expected %v, got %v", content, data)
  34. }
  35. os.Remove(copyfile)
  36. }
  37. func TestEnsureDir(t *testing.T) {
  38. tmp := t.TempDir()
  39. // Should be possible to create a new directory.
  40. err := tmos.EnsureDir(filepath.Join(tmp, "dir"), 0755)
  41. require.NoError(t, err)
  42. require.DirExists(t, filepath.Join(tmp, "dir"))
  43. // Should succeed on existing directory.
  44. err = tmos.EnsureDir(filepath.Join(tmp, "dir"), 0755)
  45. require.NoError(t, err)
  46. // Should fail on file.
  47. err = os.WriteFile(filepath.Join(tmp, "file"), []byte{}, 0644)
  48. require.NoError(t, err)
  49. err = tmos.EnsureDir(filepath.Join(tmp, "file"), 0755)
  50. require.Error(t, err)
  51. // Should allow symlink to dir.
  52. err = os.Symlink(filepath.Join(tmp, "dir"), filepath.Join(tmp, "linkdir"))
  53. require.NoError(t, err)
  54. err = tmos.EnsureDir(filepath.Join(tmp, "linkdir"), 0755)
  55. require.NoError(t, err)
  56. // Should error on symlink to file.
  57. err = os.Symlink(filepath.Join(tmp, "file"), filepath.Join(tmp, "linkfile"))
  58. require.NoError(t, err)
  59. err = tmos.EnsureDir(filepath.Join(tmp, "linkfile"), 0755)
  60. require.Error(t, err)
  61. }
  62. // Ensure that using CopyFile does not truncate the destination file before
  63. // the origin is positively a non-directory and that it is ready for copying.
  64. // See https://github.com/tendermint/tendermint/issues/6427
  65. func TestTrickedTruncation(t *testing.T) {
  66. tmpDir := t.TempDir()
  67. originalWALPath := filepath.Join(tmpDir, "wal")
  68. originalWALContent := []byte("I AM BECOME DEATH, DESTROYER OF ALL WORLDS!")
  69. if err := os.WriteFile(originalWALPath, originalWALContent, 0755); err != nil {
  70. t.Fatal(err)
  71. }
  72. // 1. Sanity check.
  73. readWAL, err := os.ReadFile(originalWALPath)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. if !bytes.Equal(readWAL, originalWALContent) {
  78. t.Fatalf("Cannot proceed as the content does not match\nGot: %q\nWant: %q", readWAL, originalWALContent)
  79. }
  80. // 2. Now cause the truncation of the original file.
  81. // It is absolutely legal to invoke os.Open on a directory.
  82. if err := tmos.CopyFile(tmpDir, originalWALPath); err == nil {
  83. t.Fatal("Expected an error")
  84. }
  85. // 3. Check the WAL's content
  86. reReadWAL, err := os.ReadFile(originalWALPath)
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. if !bytes.Equal(reReadWAL, originalWALContent) {
  91. t.Fatalf("Oops, the WAL's content was changed :(\nGot: %q\nWant: %q", reReadWAL, originalWALContent)
  92. }
  93. }