go.etcd.io/etcd@v3.3.27+incompatible/pkg/fileutil/fileutil_test.go (about)

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