2014-07-12 22:45:33 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-03-08 22:02:01 +00:00
|
|
|
package scanner
|
2014-03-02 22:58:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-16 16:33:01 +00:00
|
|
|
"path/filepath"
|
2014-03-02 22:58:14 +00:00
|
|
|
"reflect"
|
2014-08-16 16:33:01 +00:00
|
|
|
"runtime"
|
2014-07-30 18:10:46 +00:00
|
|
|
"sort"
|
2014-03-02 22:58:14 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-07-17 12:48:02 +00:00
|
|
|
|
2014-08-01 14:35:37 +00:00
|
|
|
"github.com/syncthing/syncthing/protocol"
|
2014-03-02 22:58:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var testdata = []struct {
|
|
|
|
name string
|
|
|
|
size int
|
|
|
|
hash string
|
|
|
|
}{
|
|
|
|
{"bar", 10, "2f72cc11a6fcd0271ecef8c61056ee1eb1243be3805bf9a9df98f92f7636b05c"},
|
2014-07-17 12:48:02 +00:00
|
|
|
{"baz", 0, ""},
|
2014-03-02 22:58:14 +00:00
|
|
|
{"empty", 0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
|
|
|
|
{"foo", 7, "aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var correctIgnores = map[string][]string{
|
2014-06-16 14:18:19 +00:00
|
|
|
".": {".*", "quux"},
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 18:20:01 +00:00
|
|
|
func TestWalkSub(t *testing.T) {
|
|
|
|
w := Walker{
|
|
|
|
Dir: "testdata",
|
|
|
|
Sub: "foo",
|
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
IgnoreFile: ".stignore",
|
|
|
|
}
|
|
|
|
fchan, _, err := w.Walk()
|
|
|
|
var files []protocol.FileInfo
|
|
|
|
for f := range fchan {
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(files) != 1 {
|
|
|
|
t.Fatalf("Incorrect length %d != 1", len(files))
|
|
|
|
}
|
|
|
|
if files[0].Name != "foo" {
|
|
|
|
t.Errorf("Incorrect file %v != foo", files[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
func TestWalk(t *testing.T) {
|
2014-03-08 22:02:01 +00:00
|
|
|
w := Walker{
|
|
|
|
Dir: "testdata",
|
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
IgnoreFile: ".stignore",
|
|
|
|
}
|
2014-07-17 12:48:02 +00:00
|
|
|
fchan, ignores, err := w.Walk()
|
|
|
|
var files []protocol.FileInfo
|
|
|
|
for f := range fchan {
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
2014-07-30 18:10:46 +00:00
|
|
|
sort.Sort(fileList(files))
|
2014-05-04 16:20:25 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-03-02 22:58:14 +00:00
|
|
|
|
|
|
|
if l1, l2 := len(files), len(testdata); l1 != l2 {
|
2014-07-17 12:48:02 +00:00
|
|
|
t.Log(files)
|
|
|
|
t.Log(testdata)
|
2014-03-02 22:58:14 +00:00
|
|
|
t.Fatalf("Incorrect number of walked files %d != %d", l1, l2)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range testdata {
|
|
|
|
if n1, n2 := testdata[i].name, files[i].Name; n1 != n2 {
|
|
|
|
t.Errorf("Incorrect file name %q != %q for case #%d", n1, n2, i)
|
|
|
|
}
|
|
|
|
|
2014-07-17 12:48:02 +00:00
|
|
|
if testdata[i].hash != "" {
|
|
|
|
if h1, h2 := fmt.Sprintf("%x", files[i].Blocks[0].Hash), testdata[i].hash; h1 != h2 {
|
|
|
|
t.Errorf("Incorrect hash %q != %q for case #%d", h1, h2, i)
|
|
|
|
}
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t0 := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
|
|
|
t1 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
|
|
|
if mt := files[i].Modified; mt < t0 || mt > t1 {
|
|
|
|
t.Errorf("Unrealistic modtime %d for test %d", mt, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(ignores, correctIgnores) {
|
|
|
|
t.Errorf("Incorrect ignores\n %v\n %v", correctIgnores, ignores)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 16:20:25 +00:00
|
|
|
func TestWalkError(t *testing.T) {
|
|
|
|
w := Walker{
|
|
|
|
Dir: "testdata-missing",
|
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
IgnoreFile: ".stignore",
|
|
|
|
}
|
|
|
|
_, _, err := w.Walk()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("no error from missing directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
w = Walker{
|
|
|
|
Dir: "testdata/bar",
|
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
IgnoreFile: ".stignore",
|
|
|
|
}
|
|
|
|
_, _, err = w.Walk()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("no error from non-directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
func TestIgnore(t *testing.T) {
|
2014-08-16 16:33:01 +00:00
|
|
|
pattern := "q\\[abc\\]y"
|
|
|
|
// On Windows, escaping is disabled.
|
|
|
|
// Instead, '\\' is treated as path separator.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
pattern = "q[abc]y"
|
|
|
|
}
|
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
var patterns = map[string][]string{
|
2014-08-16 16:33:01 +00:00
|
|
|
".": {"t2"},
|
|
|
|
"foo": {"bar", "z*", "q[abc]x", pattern},
|
|
|
|
filepath.Join("foo", "baz"): {"quux", ".*"},
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
var tests = []struct {
|
|
|
|
f string
|
|
|
|
r bool
|
|
|
|
}{
|
2014-08-16 16:33:01 +00:00
|
|
|
{filepath.Join("foo", "bar"), true},
|
|
|
|
{filepath.Join("foofoo"), false},
|
|
|
|
{filepath.Join("foo", "quux"), false},
|
|
|
|
{filepath.Join("foo", "zuux"), true},
|
|
|
|
{filepath.Join("foo", "qzuux"), false},
|
|
|
|
{filepath.Join("foo", "baz", "t1"), false},
|
|
|
|
{filepath.Join("foo", "baz", "t2"), true},
|
|
|
|
{filepath.Join("foo", "baz", "bar"), true},
|
|
|
|
{filepath.Join("foo", "baz", "quuxa"), false},
|
|
|
|
{filepath.Join("foo", "baz", "aquux"), false},
|
|
|
|
{filepath.Join("foo", "baz", ".quux"), true},
|
|
|
|
{filepath.Join("foo", "baz", "zquux"), true},
|
|
|
|
{filepath.Join("foo", "baz", "quux"), true},
|
|
|
|
{filepath.Join("foo", "bazz", "quux"), false},
|
|
|
|
{filepath.Join("foo", "bazz", "q[abc]x"), true},
|
|
|
|
{filepath.Join("foo", "bazz", "qax"), true},
|
|
|
|
{filepath.Join("foo", "bazz", "q[abc]y"), true},
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2014-03-08 22:02:01 +00:00
|
|
|
w := Walker{}
|
2014-03-02 22:58:14 +00:00
|
|
|
for i, tc := range tests {
|
2014-03-08 22:02:01 +00:00
|
|
|
if r := w.ignoreFile(patterns, tc.f); r != tc.r {
|
2014-08-16 16:33:01 +00:00
|
|
|
t.Errorf("Incorrect ignoreFile() #%d (%s); E: %v, A: %v", i, tc.f, tc.r, r)
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 18:10:46 +00:00
|
|
|
|
|
|
|
type fileList []protocol.FileInfo
|
|
|
|
|
|
|
|
func (f fileList) Len() int {
|
|
|
|
return len(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fileList) Less(a, b int) bool {
|
|
|
|
return f[a].Name < f[b].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fileList) Swap(a, b int) {
|
|
|
|
f[a], f[b] = f[b], f[a]
|
|
|
|
}
|