github.com/Hellyna/notify@v0.0.0-20210101060149-8ebdd4ef22cf/util_unix_test.go (about)

     1  // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be
     3  // found in the LICENSE file.
     4  
     5  // +build !windows
     6  
     7  package notify
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  )
    15  
    16  func tmpfile(s string) (string, error) {
    17  	f, err := ioutil.TempFile(filepath.Split(s))
    18  	if err != nil {
    19  		return "", err
    20  	}
    21  	if err = nonil(f.Sync(), f.Close()); err != nil {
    22  		return "", err
    23  	}
    24  	return f.Name(), nil
    25  }
    26  
    27  func symlink(src, dst string) (string, error) {
    28  	name, err := tmpfile(dst)
    29  	if err != nil {
    30  		return "", err
    31  	}
    32  	if err = nonil(os.Remove(name), os.Symlink(src, name)); err != nil {
    33  		return "", err
    34  	}
    35  	return name, nil
    36  }
    37  
    38  func removeall(s ...string) {
    39  	for _, s := range s {
    40  		os.Remove(s)
    41  	}
    42  }
    43  
    44  func TestCanonical(t *testing.T) {
    45  	wd, err := os.Getwd()
    46  	if err != nil {
    47  		t.Fatalf("os.Getwd()=%v", err)
    48  	}
    49  	wdsym, err := symlink(wd, "")
    50  	if err != nil {
    51  		t.Fatalf(`symlink(%q, "")=%v`, wd, err)
    52  	}
    53  	td := filepath.Join(wd, "testdata")
    54  	tdsym, err := symlink(td, td)
    55  	if err != nil {
    56  		t.Errorf("symlink(%q, %q)=%v", td, td, nonil(err, os.Remove(wdsym)))
    57  	}
    58  	defer removeall(wdsym, tdsym)
    59  	vfstxt := filepath.Join(td, "vfs.txt")
    60  	cases := [...]caseCanonical{
    61  		{wdsym, wd},
    62  		{tdsym, td},
    63  		{filepath.Join(wdsym, "notify.go"), filepath.Join(wd, "notify.go")},
    64  		{filepath.Join(tdsym, "vfs.txt"), vfstxt},
    65  		{filepath.Join(wdsym, filepath.Base(tdsym), "vfs.txt"), vfstxt},
    66  	}
    67  	testCanonical(t, cases[:])
    68  }
    69  
    70  func TestCanonicalCircular(t *testing.T) {
    71  	tmp1, err := tmpfile("circular")
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	tmp2, err := tmpfile("circular")
    76  	if err != nil {
    77  		t.Fatal(nonil(err, os.Remove(tmp1)))
    78  	}
    79  	defer removeall(tmp1, tmp2)
    80  	// Symlink tmp1 -> tmp2.
    81  	if err = nonil(os.Remove(tmp1), os.Symlink(tmp2, tmp1)); err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	// Symlnik tmp2 -> tmp1.
    85  	if err = nonil(os.Remove(tmp2), os.Symlink(tmp1, tmp2)); err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	if _, err = canonical(tmp1); err == nil {
    89  		t.Fatalf("want canonical(%q)!=nil", tmp1)
    90  	}
    91  	if _, ok := err.(*os.PathError); !ok {
    92  		t.Fatalf("want canonical(%q)=os.PathError; got %T", tmp1, err)
    93  	}
    94  }
    95  
    96  // issue #83
    97  func TestCanonical_RelativeSymlink(t *testing.T) {
    98  	dir, err := ioutil.TempDir(wd, "")
    99  	if err != nil {
   100  		t.Fatalf("TempDir()=%v", err)
   101  	}
   102  	var (
   103  		path     = filepath.Join(dir, filepath.FromSlash("a/b/c/d/e/f"))
   104  		realpath = filepath.Join(dir, filepath.FromSlash("a/b/x/y/z/d/e/f"))
   105  		rel      = filepath.FromSlash("x/y/z/../z/../z")
   106  		chdir    = filepath.Join(dir, filepath.FromSlash("a/b"))
   107  	)
   108  	defer os.RemoveAll(dir)
   109  	if err = os.MkdirAll(realpath, 0755); err != nil {
   110  		t.Fatalf("MkdirAll()=%v", err)
   111  	}
   112  	if err := os.Chdir(chdir); err != nil {
   113  		t.Fatalf("Chdir()=%v", err)
   114  	}
   115  	if err := nonil(os.Symlink(rel, "c"), os.Chdir(wd)); err != nil {
   116  		t.Fatalf("Symlink()=%v", err)
   117  	}
   118  	got, err := canonical(path)
   119  	if err != nil {
   120  		t.Fatalf("canonical(%s)=%v", path, err)
   121  	}
   122  	if got != realpath {
   123  		t.Fatalf("want canonical()=%s; got %s", realpath, got)
   124  	}
   125  }