error_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package os_test
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "testing"
  11. )
  12. func TestErrIsExist(t *testing.T) {
  13. f, err := ioutil.TempFile("", "_Go_ErrIsExist")
  14. if err != nil {
  15. t.Fatalf("open ErrIsExist tempfile: %s", err)
  16. return
  17. }
  18. defer os.Remove(f.Name())
  19. defer f.Close()
  20. f2, err := os.OpenFile(f.Name(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
  21. if err == nil {
  22. f2.Close()
  23. t.Fatal("Open should have failed")
  24. return
  25. }
  26. if s := checkErrorPredicate("os.IsExist", os.IsExist, err); s != "" {
  27. t.Fatal(s)
  28. return
  29. }
  30. }
  31. func testErrNotExist(name string) string {
  32. f, err := os.Open(name)
  33. if err == nil {
  34. f.Close()
  35. return "Open should have failed"
  36. }
  37. if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" {
  38. return s
  39. }
  40. err = os.Chdir(name)
  41. if err == nil {
  42. return "Chdir should have failed"
  43. }
  44. if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" {
  45. return s
  46. }
  47. return ""
  48. }
  49. func TestErrIsNotExist(t *testing.T) {
  50. tmpDir, err := ioutil.TempDir("", "_Go_ErrIsNotExist")
  51. if err != nil {
  52. t.Fatalf("create ErrIsNotExist tempdir: %s", err)
  53. return
  54. }
  55. defer os.RemoveAll(tmpDir)
  56. name := filepath.Join(tmpDir, "NotExists")
  57. if s := testErrNotExist(name); s != "" {
  58. t.Fatal(s)
  59. return
  60. }
  61. name = filepath.Join(name, "NotExists2")
  62. if s := testErrNotExist(name); s != "" {
  63. t.Fatal(s)
  64. return
  65. }
  66. }
  67. func checkErrorPredicate(predName string, pred func(error) bool, err error) string {
  68. if !pred(err) {
  69. return fmt.Sprintf("%s does not work as expected for %#v", predName, err)
  70. }
  71. return ""
  72. }
  73. var isExistTests = []struct {
  74. err error
  75. is bool
  76. isnot bool
  77. }{
  78. {&os.PathError{Err: os.ErrInvalid}, false, false},
  79. {&os.PathError{Err: os.ErrPermission}, false, false},
  80. {&os.PathError{Err: os.ErrExist}, true, false},
  81. {&os.PathError{Err: os.ErrNotExist}, false, true},
  82. {&os.LinkError{Err: os.ErrInvalid}, false, false},
  83. {&os.LinkError{Err: os.ErrPermission}, false, false},
  84. {&os.LinkError{Err: os.ErrExist}, true, false},
  85. {&os.LinkError{Err: os.ErrNotExist}, false, true},
  86. {nil, false, false},
  87. }
  88. func TestIsExist(t *testing.T) {
  89. for _, tt := range isExistTests {
  90. if is := os.IsExist(tt.err); is != tt.is {
  91. t.Errorf("os.IsExist(%T %v) = %v, want %v", tt.err, tt.err, is, tt.is)
  92. }
  93. if isnot := os.IsNotExist(tt.err); isnot != tt.isnot {
  94. t.Errorf("os.IsNotExist(%T %v) = %v, want %v", tt.err, tt.err, isnot, tt.isnot)
  95. }
  96. }
  97. }
  98. func TestErrPathNUL(t *testing.T) {
  99. f, err := ioutil.TempFile("", "_Go_ErrPathNUL\x00")
  100. if err == nil {
  101. f.Close()
  102. t.Fatal("TempFile should have failed")
  103. }
  104. f, err = ioutil.TempFile("", "_Go_ErrPathNUL")
  105. if err != nil {
  106. t.Fatalf("open ErrPathNUL tempfile: %s", err)
  107. }
  108. defer os.Remove(f.Name())
  109. defer f.Close()
  110. f2, err := os.OpenFile(f.Name(), os.O_RDWR, 0600)
  111. if err != nil {
  112. t.Fatalf("open ErrPathNUL: %s", err)
  113. }
  114. f2.Close()
  115. f2, err = os.OpenFile(f.Name()+"\x00", os.O_RDWR, 0600)
  116. if err == nil {
  117. f2.Close()
  118. t.Fatal("Open should have failed")
  119. }
  120. }