fileutil_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/ioutil"
  17. "os"
  18. "os/user"
  19. "path/filepath"
  20. "reflect"
  21. "runtime"
  22. "strings"
  23. "testing"
  24. )
  25. func TestIsDirWriteable(t *testing.T) {
  26. tmpdir, err := ioutil.TempDir("", "")
  27. if err != nil {
  28. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  29. }
  30. defer os.RemoveAll(tmpdir)
  31. if err = IsDirWriteable(tmpdir); err != nil {
  32. t.Fatalf("unexpected IsDirWriteable error: %v", err)
  33. }
  34. if err = os.Chmod(tmpdir, 0444); err != nil {
  35. t.Fatalf("unexpected os.Chmod error: %v", err)
  36. }
  37. me, err := user.Current()
  38. if err != nil {
  39. // err can be non-nil when cross compiled
  40. // http://stackoverflow.com/questions/20609415/cross-compiling-user-current-not-implemented-on-linux-amd64
  41. t.Skipf("failed to get current user: %v", err)
  42. }
  43. if me.Name == "root" || runtime.GOOS == "windows" {
  44. // ideally we should check CAP_DAC_OVERRIDE.
  45. // but it does not matter for tests.
  46. // Chmod is not supported under windows.
  47. t.Skipf("running as a superuser or in windows")
  48. }
  49. if err := IsDirWriteable(tmpdir); err == nil {
  50. t.Fatalf("expected IsDirWriteable to error")
  51. }
  52. }
  53. func TestReadDir(t *testing.T) {
  54. tmpdir, err := ioutil.TempDir("", "")
  55. defer os.RemoveAll(tmpdir)
  56. if err != nil {
  57. t.Fatalf("unexpected ioutil.TempDir error: %v", err)
  58. }
  59. files := []string{"def", "abc", "xyz", "ghi"}
  60. for _, f := range files {
  61. var fh *os.File
  62. fh, err = os.Create(filepath.Join(tmpdir, f))
  63. if err != nil {
  64. t.Fatalf("error creating file: %v", err)
  65. }
  66. if err = fh.Close(); err != nil {
  67. t.Fatalf("error closing file: %v", err)
  68. }
  69. }
  70. fs, err := ReadDir(tmpdir)
  71. if err != nil {
  72. t.Fatalf("error calling ReadDir: %v", err)
  73. }
  74. wfs := []string{"abc", "def", "ghi", "xyz"}
  75. if !reflect.DeepEqual(fs, wfs) {
  76. t.Fatalf("ReadDir: got %v, want %v", fs, wfs)
  77. }
  78. }
  79. func TestCreateDirAll(t *testing.T) {
  80. tmpdir, err := ioutil.TempDir(os.TempDir(), "foo")
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. defer os.RemoveAll(tmpdir)
  85. tmpdir2 := filepath.Join(tmpdir, "testdir")
  86. if err = CreateDirAll(tmpdir2); err != nil {
  87. t.Fatal(err)
  88. }
  89. if err = ioutil.WriteFile(filepath.Join(tmpdir2, "text.txt"), []byte("test text"), PrivateFileMode); err != nil {
  90. t.Fatal(err)
  91. }
  92. if err = CreateDirAll(tmpdir2); err == nil || !strings.Contains(err.Error(), "to be empty, got") {
  93. t.Fatalf("unexpected error %v", err)
  94. }
  95. }
  96. func TestExist(t *testing.T) {
  97. f, err := ioutil.TempFile(os.TempDir(), "fileutil")
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. f.Close()
  102. if g := Exist(f.Name()); !g {
  103. t.Errorf("exist = %v, want true", g)
  104. }
  105. os.Remove(f.Name())
  106. if g := Exist(f.Name()); g {
  107. t.Errorf("exist = %v, want false", g)
  108. }
  109. }
  110. func TestZeroToEnd(t *testing.T) {
  111. f, err := ioutil.TempFile(os.TempDir(), "fileutil")
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. defer f.Close()
  116. b := make([]byte, 1024)
  117. for i := range b {
  118. b[i] = 12
  119. }
  120. if _, err = f.Write(b); err != nil {
  121. t.Fatal(err)
  122. }
  123. if _, err = f.Seek(512, os.SEEK_SET); err != nil {
  124. t.Fatal(err)
  125. }
  126. if err = ZeroToEnd(f); err != nil {
  127. t.Fatal(err)
  128. }
  129. off, serr := f.Seek(0, os.SEEK_CUR)
  130. if serr != nil {
  131. t.Fatal(serr)
  132. }
  133. if off != 512 {
  134. t.Fatalf("expected offset 512, got %d", off)
  135. }
  136. b = make([]byte, 512)
  137. if _, err = f.Read(b); err != nil {
  138. t.Fatal(err)
  139. }
  140. for i := range b {
  141. if b[i] != 0 {
  142. t.Errorf("expected b[%d] = 0, got %d", i, b[i])
  143. }
  144. }
  145. }