fileutil_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutil
  15. import (
  16. "io"
  17. "io/ioutil"
  18. "os"
  19. "os/user"
  20. "path/filepath"
  21. "reflect"
  22. "runtime"
  23. "strings"
  24. "testing"
  25. )
  26. func TestIsDirWriteable(t *testing.T) {
  27. tmpdir, err := ioutil.TempDir("", "")
  28. if err != nil {
  29. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  30. }
  31. defer os.RemoveAll(tmpdir)
  32. if err = IsDirWriteable(tmpdir); err != nil {
  33. t.Fatalf("unexpected IsDirWriteable error: %v", err)
  34. }
  35. if err = os.Chmod(tmpdir, 0444); err != nil {
  36. t.Fatalf("unexpected os.Chmod error: %v", err)
  37. }
  38. me, err := user.Current()
  39. if err != nil {
  40. // err can be non-nil when cross compiled
  41. // http://stackoverflow.com/questions/20609415/cross-compiling-user-current-not-implemented-on-linux-amd64
  42. t.Skipf("failed to get current user: %v", err)
  43. }
  44. if me.Name == "root" || runtime.GOOS == "windows" {
  45. // ideally we should check CAP_DAC_OVERRIDE.
  46. // but it does not matter for tests.
  47. // Chmod is not supported under windows.
  48. t.Skipf("running as a superuser or in windows")
  49. }
  50. if err := IsDirWriteable(tmpdir); err == nil {
  51. t.Fatalf("expected IsDirWriteable to error")
  52. }
  53. }
  54. func TestReadDir(t *testing.T) {
  55. tmpdir, err := ioutil.TempDir("", "")
  56. defer os.RemoveAll(tmpdir)
  57. if err != nil {
  58. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  59. }
  60. files := []string{"def", "abc", "xyz", "ghi"}
  61. for _, f := range files {
  62. var fh *os.File
  63. fh, err = os.Create(filepath.Join(tmpdir, f))
  64. if err != nil {
  65. t.Fatalf("error creating file: %v", err)
  66. }
  67. if err = fh.Close(); err != nil {
  68. t.Fatalf("error closing file: %v", err)
  69. }
  70. }
  71. fs, err := ReadDir(tmpdir)
  72. if err != nil {
  73. t.Fatalf("error calling ReadDir: %v", err)
  74. }
  75. wfs := []string{"abc", "def", "ghi", "xyz"}
  76. if !reflect.DeepEqual(fs, wfs) {
  77. t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
  78. }
  79. }
  80. func TestCreateDirAll(t *testing.T) {
  81. tmpdir, err := ioutil.TempDir(os.TempDir(), "foo")
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. defer os.RemoveAll(tmpdir)
  86. tmpdir2 := filepath.Join(tmpdir, "testdir")
  87. if err = CreateDirAll(tmpdir2); err != nil {
  88. t.Fatal(err)
  89. }
  90. if err = ioutil.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode); err != nil {
  91. t.Fatal(err)
  92. }
  93. if err = CreateDirAll(tmpdir2); err == nil || !strings.Contains(err.Error(), "to be empty, got") {
  94. t.Fatalf("unexpected error %v", err)
  95. }
  96. }
  97. func TestExist(t *testing.T) {
  98. f, err := ioutil.TempFile(os.TempDir(), "fileutil")
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. f.Close()
  103. if g := Exist(f.Name()); !g {
  104. t.Errorf("exist = %v, want true", g)
  105. }
  106. os.Remove(f.Name())
  107. if g := Exist(f.Name()); g {
  108. t.Errorf("exist = %v, want false", g)
  109. }
  110. }
  111. func TestZeroToEnd(t *testing.T) {
  112. f, err := ioutil.TempFile(os.TempDir(), "fileutil")
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. defer f.Close()
  117. // Ensure 0 size is a nop so zero-to-end on an empty file won't give EINVAL.
  118. if err = ZeroToEnd(f); err != nil {
  119. t.Fatal(err)
  120. }
  121. b := make([]byte, 1024)
  122. for i := range b {
  123. b[i] = 12
  124. }
  125. if _, err = f.Write(b); err != nil {
  126. t.Fatal(err)
  127. }
  128. if _, err = f.Seek(512, io.SeekStart); err != nil {
  129. t.Fatal(err)
  130. }
  131. if err = ZeroToEnd(f); err != nil {
  132. t.Fatal(err)
  133. }
  134. off, serr := f.Seek(0, io.SeekCurrent)
  135. if serr != nil {
  136. t.Fatal(serr)
  137. }
  138. if off != 512 {
  139. t.Fatalf("expected offset 512, got %d", off)
  140. }
  141. b = make([]byte, 512)
  142. if _, err = f.Read(b); err != nil {
  143. t.Fatal(err)
  144. }
  145. for i := range b {
  146. if b[i] != 0 {
  147. t.Errorf("expected b[%d] = 0, got %d", i, b[i])
  148. }
  149. }
  150. }