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.

140 lines
4.6 KiB

  1. package tempfile
  2. // Need access to internal variables, so can't use _test package
  3. import (
  4. "bytes"
  5. fmt "fmt"
  6. "io/ioutil"
  7. "os"
  8. testing "testing"
  9. "github.com/stretchr/testify/require"
  10. tmrand "github.com/tendermint/tendermint/libs/rand"
  11. )
  12. func TestWriteFileAtomic(t *testing.T) {
  13. var (
  14. data = []byte(tmrand.Str(tmrand.Intn(2048)))
  15. old = tmrand.Bytes(tmrand.Intn(2048))
  16. perm os.FileMode = 0600
  17. )
  18. f, err := ioutil.TempFile("/tmp", "write-atomic-test-")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. defer os.Remove(f.Name())
  23. if err = ioutil.WriteFile(f.Name(), old, 0600); err != nil {
  24. t.Fatal(err)
  25. }
  26. if err = WriteFileAtomic(f.Name(), data, perm); err != nil {
  27. t.Fatal(err)
  28. }
  29. rData, err := ioutil.ReadFile(f.Name())
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. if !bytes.Equal(data, rData) {
  34. t.Fatalf("data mismatch: %v != %v", data, rData)
  35. }
  36. stat, err := os.Stat(f.Name())
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if have, want := stat.Mode().Perm(), perm; have != want {
  41. t.Errorf("have %v, want %v", have, want)
  42. }
  43. }
  44. // This tests atomic write file when there is a single duplicate file.
  45. // Expected behavior is for a new file to be created, and the original write file to be unaltered.
  46. func TestWriteFileAtomicDuplicateFile(t *testing.T) {
  47. var (
  48. defaultSeed uint64 = 1
  49. testString = "This is a glorious test string"
  50. expectedString = "Did the test file's string appear here?"
  51. fileToWrite = "/tmp/TestWriteFileAtomicDuplicateFile-test.txt"
  52. )
  53. // Create a file at the seed, and reset the seed.
  54. atomicWriteFileRand = defaultSeed
  55. firstFileRand := randWriteFileSuffix()
  56. atomicWriteFileRand = defaultSeed
  57. fname := "/tmp/" + atomicWriteFilePrefix + firstFileRand
  58. f, err := os.OpenFile(fname, atomicWriteFileFlag, 0777)
  59. defer os.Remove(fname)
  60. // Defer here, in case there is a panic in WriteFileAtomic.
  61. defer os.Remove(fileToWrite)
  62. require.Nil(t, err)
  63. f.WriteString(testString)
  64. WriteFileAtomic(fileToWrite, []byte(expectedString), 0777)
  65. // Check that the first atomic file was untouched
  66. firstAtomicFileBytes, err := ioutil.ReadFile(fname)
  67. require.Nil(t, err, "Error reading first atomic file")
  68. require.Equal(t, []byte(testString), firstAtomicFileBytes, "First atomic file was overwritten")
  69. // Check that the resultant file is correct
  70. resultantFileBytes, err := ioutil.ReadFile(fileToWrite)
  71. require.Nil(t, err, "Error reading resultant file")
  72. require.Equal(t, []byte(expectedString), resultantFileBytes, "Written file had incorrect bytes")
  73. // Check that the intermediate write file was deleted
  74. // Get the second write files' randomness
  75. atomicWriteFileRand = defaultSeed
  76. _ = randWriteFileSuffix()
  77. secondFileRand := randWriteFileSuffix()
  78. _, err = os.Stat("/tmp/" + atomicWriteFilePrefix + secondFileRand)
  79. require.True(t, os.IsNotExist(err), "Intermittent atomic write file not deleted")
  80. }
  81. // This tests atomic write file when there are many duplicate files.
  82. // Expected behavior is for a new file to be created under a completely new seed,
  83. // and the original write files to be unaltered.
  84. func TestWriteFileAtomicManyDuplicates(t *testing.T) {
  85. var (
  86. defaultSeed uint64 = 2
  87. testString = "This is a glorious test string, from file %d"
  88. expectedString = "Did any of the test file's string appear here?"
  89. fileToWrite = "/tmp/TestWriteFileAtomicDuplicateFile-test.txt"
  90. )
  91. // Initialize all of the atomic write files
  92. atomicWriteFileRand = defaultSeed
  93. for i := 0; i < atomicWriteFileMaxNumConflicts+2; i++ {
  94. fileRand := randWriteFileSuffix()
  95. fname := "/tmp/" + atomicWriteFilePrefix + fileRand
  96. f, err := os.OpenFile(fname, atomicWriteFileFlag, 0777)
  97. require.Nil(t, err)
  98. f.WriteString(fmt.Sprintf(testString, i))
  99. defer os.Remove(fname)
  100. }
  101. atomicWriteFileRand = defaultSeed
  102. // Defer here, in case there is a panic in WriteFileAtomic.
  103. defer os.Remove(fileToWrite)
  104. WriteFileAtomic(fileToWrite, []byte(expectedString), 0777)
  105. // Check that all intermittent atomic file were untouched
  106. atomicWriteFileRand = defaultSeed
  107. for i := 0; i < atomicWriteFileMaxNumConflicts+2; i++ {
  108. fileRand := randWriteFileSuffix()
  109. fname := "/tmp/" + atomicWriteFilePrefix + fileRand
  110. firstAtomicFileBytes, err := ioutil.ReadFile(fname)
  111. require.Nil(t, err, "Error reading first atomic file")
  112. require.Equal(t, []byte(fmt.Sprintf(testString, i)), firstAtomicFileBytes,
  113. "atomic write file %d was overwritten", i)
  114. }
  115. // Check that the resultant file is correct
  116. resultantFileBytes, err := ioutil.ReadFile(fileToWrite)
  117. require.Nil(t, err, "Error reading resultant file")
  118. require.Equal(t, []byte(expectedString), resultantFileBytes, "Written file had incorrect bytes")
  119. }