github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/os/error_test.go (about)

     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  
     5  package os_test
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  )
    14  
    15  func TestErrIsExist(t *testing.T) {
    16  	f, err := ioutil.TempFile("", "_Go_ErrIsExist")
    17  	if err != nil {
    18  		t.Fatalf("open ErrIsExist tempfile: %s", err)
    19  		return
    20  	}
    21  	defer os.Remove(f.Name())
    22  	defer f.Close()
    23  	f2, err := os.OpenFile(f.Name(), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
    24  	if err == nil {
    25  		f2.Close()
    26  		t.Fatal("Open should have failed")
    27  		return
    28  	}
    29  	if s := checkErrorPredicate("os.IsExist", os.IsExist, err); s != "" {
    30  		t.Fatal(s)
    31  		return
    32  	}
    33  }
    34  
    35  func testErrNotExist(name string) string {
    36  	f, err := os.Open(name)
    37  	if err == nil {
    38  		f.Close()
    39  		return "Open should have failed"
    40  	}
    41  	if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" {
    42  		return s
    43  	}
    44  
    45  	err = os.Chdir(name)
    46  	if err == nil {
    47  		return "Chdir should have failed"
    48  	}
    49  	if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" {
    50  		return s
    51  	}
    52  	return ""
    53  }
    54  
    55  func TestErrIsNotExist(t *testing.T) {
    56  	tmpDir, err := ioutil.TempDir("", "_Go_ErrIsNotExist")
    57  	if err != nil {
    58  		t.Fatalf("create ErrIsNotExist tempdir: %s", err)
    59  		return
    60  	}
    61  	defer os.RemoveAll(tmpDir)
    62  
    63  	name := filepath.Join(tmpDir, "NotExists")
    64  	if s := testErrNotExist(name); s != "" {
    65  		t.Fatal(s)
    66  		return
    67  	}
    68  
    69  	name = filepath.Join(name, "NotExists2")
    70  	if s := testErrNotExist(name); s != "" {
    71  		t.Fatal(s)
    72  		return
    73  	}
    74  }
    75  
    76  func checkErrorPredicate(predName string, pred func(error) bool, err error) string {
    77  	if !pred(err) {
    78  		return fmt.Sprintf("%s does not work as expected for %#v", predName, err)
    79  	}
    80  	return ""
    81  }
    82  
    83  type isExistTest struct {
    84  	err   error
    85  	is    bool
    86  	isnot bool
    87  }
    88  
    89  var isExistTests = []isExistTest{
    90  	{&os.PathError{Err: os.ErrInvalid}, false, false},
    91  	{&os.PathError{Err: os.ErrPermission}, false, false},
    92  	{&os.PathError{Err: os.ErrExist}, true, false},
    93  	{&os.PathError{Err: os.ErrNotExist}, false, true},
    94  	{&os.PathError{Err: os.ErrClosed}, false, false},
    95  	{&os.LinkError{Err: os.ErrInvalid}, false, false},
    96  	{&os.LinkError{Err: os.ErrPermission}, false, false},
    97  	{&os.LinkError{Err: os.ErrExist}, true, false},
    98  	{&os.LinkError{Err: os.ErrNotExist}, false, true},
    99  	{&os.LinkError{Err: os.ErrClosed}, false, false},
   100  	{&os.SyscallError{Err: os.ErrNotExist}, false, true},
   101  	{&os.SyscallError{Err: os.ErrExist}, true, false},
   102  	{nil, false, false},
   103  }
   104  
   105  func TestIsExist(t *testing.T) {
   106  	for _, tt := range isExistTests {
   107  		if is := os.IsExist(tt.err); is != tt.is {
   108  			t.Errorf("os.IsExist(%T %v) = %v, want %v", tt.err, tt.err, is, tt.is)
   109  		}
   110  		if isnot := os.IsNotExist(tt.err); isnot != tt.isnot {
   111  			t.Errorf("os.IsNotExist(%T %v) = %v, want %v", tt.err, tt.err, isnot, tt.isnot)
   112  		}
   113  	}
   114  }
   115  
   116  type isPermissionTest struct {
   117  	err  error
   118  	want bool
   119  }
   120  
   121  var isPermissionTests = []isPermissionTest{
   122  	{nil, false},
   123  	{&os.PathError{Err: os.ErrPermission}, true},
   124  	{&os.SyscallError{Err: os.ErrPermission}, true},
   125  }
   126  
   127  func TestIsPermission(t *testing.T) {
   128  	for _, tt := range isPermissionTests {
   129  		if got := os.IsPermission(tt.err); got != tt.want {
   130  			t.Errorf("os.IsPermission(%#v) = %v; want %v", tt.err, got, tt.want)
   131  		}
   132  	}
   133  }
   134  
   135  func TestErrPathNUL(t *testing.T) {
   136  	f, err := ioutil.TempFile("", "_Go_ErrPathNUL\x00")
   137  	if err == nil {
   138  		f.Close()
   139  		t.Fatal("TempFile should have failed")
   140  	}
   141  	f, err = ioutil.TempFile("", "_Go_ErrPathNUL")
   142  	if err != nil {
   143  		t.Fatalf("open ErrPathNUL tempfile: %s", err)
   144  	}
   145  	defer os.Remove(f.Name())
   146  	defer f.Close()
   147  	f2, err := os.OpenFile(f.Name(), os.O_RDWR, 0600)
   148  	if err != nil {
   149  		t.Fatalf("open ErrPathNUL: %s", err)
   150  	}
   151  	f2.Close()
   152  	f2, err = os.OpenFile(f.Name()+"\x00", os.O_RDWR, 0600)
   153  	if err == nil {
   154  		f2.Close()
   155  		t.Fatal("Open should have failed")
   156  	}
   157  }