fileutil_test.go 3.6 KB

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