2016-12-13 03:24:10 -07:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-12-13 03:24:10 -07:00
|
|
|
|
2017-02-07 01:34:24 -07:00
|
|
|
// +build !windows
|
|
|
|
|
2016-12-13 03:24:10 -07:00
|
|
|
package osutil_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-19 07:36:56 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2016-12-13 03:24:10 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
|
|
|
)
|
|
|
|
|
2017-01-10 00:09:31 -07:00
|
|
|
func TestTraversesSymlink(t *testing.T) {
|
2016-12-13 03:24:10 -07:00
|
|
|
os.RemoveAll("testdata")
|
|
|
|
defer os.RemoveAll("testdata")
|
2017-08-19 07:36:56 -07:00
|
|
|
|
|
|
|
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
|
|
|
|
fs.MkdirAll("a/b/c", 0755)
|
|
|
|
fs.CreateSymlink("b", "a/l")
|
2016-12-13 03:24:10 -07:00
|
|
|
|
|
|
|
// a/l -> b, so a/l/c should resolve by normal stat
|
2017-08-19 07:36:56 -07:00
|
|
|
info, err := fs.Lstat("a/l/c")
|
2016-12-13 03:24:10 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
t.Fatal("error in setup, a/l/c should be a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
2017-01-10 00:09:31 -07:00
|
|
|
name string
|
|
|
|
traverses bool
|
2016-12-13 03:24:10 -07:00
|
|
|
}{
|
|
|
|
// Exist
|
2017-01-10 00:09:31 -07:00
|
|
|
{".", false},
|
|
|
|
{"a", false},
|
|
|
|
{"a/b", false},
|
|
|
|
{"a/b/c", false},
|
2016-12-13 03:24:10 -07:00
|
|
|
// Don't exist
|
|
|
|
{"x", false},
|
|
|
|
{"a/x", false},
|
|
|
|
{"a/b/x", false},
|
|
|
|
{"a/x/c", false},
|
|
|
|
// Symlink or behind symlink
|
2017-01-10 00:09:31 -07:00
|
|
|
{"a/l", true},
|
|
|
|
{"a/l/c", true},
|
|
|
|
// Non-existing behind a symlink
|
|
|
|
{"a/l/x", true},
|
2016-12-13 03:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2017-08-19 07:36:56 -07:00
|
|
|
if res := osutil.TraversesSymlink(fs, tc.name); tc.traverses == (res == nil) {
|
2017-01-10 00:09:31 -07:00
|
|
|
t.Errorf("TraversesSymlink(%q) = %v, should be %v", tc.name, res, tc.traverses)
|
2016-12-13 03:24:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 00:09:31 -07:00
|
|
|
var traversesSymlinkResult error
|
2016-12-13 03:24:10 -07:00
|
|
|
|
2017-01-10 00:09:31 -07:00
|
|
|
func BenchmarkTraversesSymlink(b *testing.B) {
|
2016-12-13 03:24:10 -07:00
|
|
|
os.RemoveAll("testdata")
|
|
|
|
defer os.RemoveAll("testdata")
|
2017-08-19 07:36:56 -07:00
|
|
|
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata")
|
|
|
|
fs.MkdirAll("a/b/c", 0755)
|
2016-12-13 03:24:10 -07:00
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-08-19 07:36:56 -07:00
|
|
|
traversesSymlinkResult = osutil.TraversesSymlink(fs, "a/b/c")
|
2016-12-13 03:24:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|