2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07:00
|
|
|
// 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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-03-08 15:02:01 -07:00
|
|
|
package scanner
|
2014-03-02 15:58:14 -07:00
|
|
|
|
|
|
|
import (
|
2014-08-26 01:11:25 -07:00
|
|
|
"bytes"
|
2014-03-02 15:58:14 -07:00
|
|
|
"fmt"
|
2015-03-18 15:54:50 -07:00
|
|
|
"os"
|
2014-08-16 09:33:01 -07:00
|
|
|
"path/filepath"
|
2014-08-30 00:22:23 -07:00
|
|
|
"reflect"
|
2015-03-18 15:54:50 -07:00
|
|
|
"runtime"
|
2014-08-30 00:55:01 -07:00
|
|
|
rdebug "runtime/debug"
|
2014-07-30 11:10:46 -07:00
|
|
|
"sort"
|
2014-03-02 15:58:14 -07:00
|
|
|
"testing"
|
2014-07-17 05:48:02 -07:00
|
|
|
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore"
|
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 10:38:46 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-09-04 03:54:01 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/symlinks"
|
2015-03-18 15:54:50 -07:00
|
|
|
"golang.org/x/text/unicode/norm"
|
2014-03-02 15:58:14 -07:00
|
|
|
)
|
|
|
|
|
2014-08-30 00:22:23 -07:00
|
|
|
type testfile struct {
|
2014-03-02 15:58:14 -07:00
|
|
|
name string
|
|
|
|
size int
|
|
|
|
hash string
|
2014-08-30 00:22:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type testfileList []testfile
|
|
|
|
|
|
|
|
var testdata = testfileList{
|
|
|
|
{"afile", 4, "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"},
|
|
|
|
{"dir1", 128, ""},
|
2014-08-31 14:32:27 -07:00
|
|
|
{filepath.Join("dir1", "dfile"), 5, "49ae93732fcf8d63fe1cce759664982dbd5b23161f007dba8561862adc96d063"},
|
2014-08-30 00:22:23 -07:00
|
|
|
{"dir2", 128, ""},
|
2014-08-31 14:32:27 -07:00
|
|
|
{filepath.Join("dir2", "cfile"), 4, "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c"},
|
2014-09-04 13:29:53 -07:00
|
|
|
{"excludes", 37, "df90b52f0c55dba7a7a940affe482571563b1ac57bd5be4d8a0291e7de928e06"},
|
2014-08-30 00:22:23 -07:00
|
|
|
{"further-excludes", 5, "7eb0a548094fa6295f7fd9200d69973e5f5ec5c04f2a86d998080ac43ecf89f1"},
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var correctIgnores = map[string][]string{
|
2014-06-16 07:18:19 -07:00
|
|
|
".": {".*", "quux"},
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-08-30 00:55:01 -07:00
|
|
|
func init() {
|
|
|
|
// This test runs the risk of entering infinite recursion if it fails.
|
2015-04-28 08:34:55 -07:00
|
|
|
// Limit the stack size to 10 megs to crash early in that case instead of
|
2014-08-30 00:55:01 -07:00
|
|
|
// potentially taking down the box...
|
|
|
|
rdebug.SetMaxStack(10 * 1 << 20)
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:20:01 -07:00
|
|
|
func TestWalkSub(t *testing.T) {
|
2014-12-23 02:05:08 -07:00
|
|
|
ignores := ignore.New(false)
|
|
|
|
err := ignores.Load("testdata/.stignore")
|
2014-09-04 13:29:53 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:20:01 -07:00
|
|
|
w := Walker{
|
2014-09-04 13:29:53 -07:00
|
|
|
Dir: "testdata",
|
2015-03-27 01:51:18 -07:00
|
|
|
Subs: []string{"dir2"},
|
2014-09-04 13:29:53 -07:00
|
|
|
BlockSize: 128 * 1024,
|
2014-10-14 13:30:00 -07:00
|
|
|
Matcher: ignores,
|
2015-04-29 11:46:32 -07:00
|
|
|
Hashers: 2,
|
2014-08-11 11:20:01 -07:00
|
|
|
}
|
2014-08-26 01:11:25 -07:00
|
|
|
fchan, err := w.Walk()
|
2014-08-11 11:20:01 -07:00
|
|
|
var files []protocol.FileInfo
|
|
|
|
for f := range fchan {
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-30 00:22:23 -07:00
|
|
|
// The directory contains two files, where one is ignored from a higher
|
|
|
|
// level. We should see only the directory and one of the files.
|
|
|
|
|
|
|
|
if len(files) != 2 {
|
|
|
|
t.Fatalf("Incorrect length %d != 2", len(files))
|
2014-08-11 11:20:01 -07:00
|
|
|
}
|
2014-08-30 00:22:23 -07:00
|
|
|
if files[0].Name != "dir2" {
|
|
|
|
t.Errorf("Incorrect file %v != dir2", files[0])
|
|
|
|
}
|
2014-08-31 14:32:27 -07:00
|
|
|
if files[1].Name != filepath.Join("dir2", "cfile") {
|
2014-08-30 00:22:23 -07:00
|
|
|
t.Errorf("Incorrect file %v != dir2/cfile", files[1])
|
2014-08-11 11:20:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
func TestWalk(t *testing.T) {
|
2014-12-23 02:05:08 -07:00
|
|
|
ignores := ignore.New(false)
|
|
|
|
err := ignores.Load("testdata/.stignore")
|
2014-09-04 13:29:53 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log(ignores)
|
|
|
|
|
2014-03-08 15:02:01 -07:00
|
|
|
w := Walker{
|
2014-09-04 13:29:53 -07:00
|
|
|
Dir: "testdata",
|
|
|
|
BlockSize: 128 * 1024,
|
2014-10-14 13:30:00 -07:00
|
|
|
Matcher: ignores,
|
2015-04-29 11:46:32 -07:00
|
|
|
Hashers: 2,
|
2014-03-08 15:02:01 -07:00
|
|
|
}
|
2014-05-04 09:20:25 -07:00
|
|
|
|
2014-08-30 00:22:23 -07:00
|
|
|
fchan, err := w.Walk()
|
2014-05-04 09:20:25 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-08-30 00:22:23 -07:00
|
|
|
var tmp []protocol.FileInfo
|
|
|
|
for f := range fchan {
|
|
|
|
tmp = append(tmp, f)
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
2014-08-30 00:22:23 -07:00
|
|
|
sort.Sort(fileList(tmp))
|
|
|
|
files := fileList(tmp).testfiles()
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-08-30 00:22:23 -07:00
|
|
|
if !reflect.DeepEqual(files, testdata) {
|
|
|
|
t.Errorf("Walk returned unexpected data\nExpected: %v\nActual: %v", testdata, files)
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 09:20:25 -07:00
|
|
|
func TestWalkError(t *testing.T) {
|
|
|
|
w := Walker{
|
2014-09-04 13:29:53 -07:00
|
|
|
Dir: "testdata-missing",
|
|
|
|
BlockSize: 128 * 1024,
|
2015-04-29 11:46:32 -07:00
|
|
|
Hashers: 2,
|
2014-05-04 09:20:25 -07:00
|
|
|
}
|
2014-08-26 01:11:25 -07:00
|
|
|
_, err := w.Walk()
|
2014-05-04 09:20:25 -07:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("no error from missing directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
w = Walker{
|
2014-09-04 13:29:53 -07:00
|
|
|
Dir: "testdata/bar",
|
|
|
|
BlockSize: 128 * 1024,
|
2014-05-04 09:20:25 -07:00
|
|
|
}
|
2014-08-26 01:11:25 -07:00
|
|
|
_, err = w.Walk()
|
2014-05-04 09:20:25 -07:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Error("no error from non-directory")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
func TestVerify(t *testing.T) {
|
|
|
|
blocksize := 16
|
|
|
|
// data should be an even multiple of blocksize long
|
|
|
|
data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e")
|
|
|
|
buf := bytes.NewBuffer(data)
|
2015-08-27 23:57:37 -07:00
|
|
|
var progress int64
|
2014-09-27 05:44:15 -07:00
|
|
|
|
2015-08-26 15:49:06 -07:00
|
|
|
blocks, err := Blocks(buf, blocksize, 0, &progress)
|
2014-09-27 05:44:15 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if exp := len(data) / blocksize; len(blocks) != exp {
|
|
|
|
t.Fatalf("Incorrect number of blocks %d != %d", len(blocks), exp)
|
|
|
|
}
|
|
|
|
|
2015-08-27 23:57:37 -07:00
|
|
|
if int64(len(data)) != progress {
|
2015-08-26 15:49:06 -07:00
|
|
|
t.Fatalf("Incorrect counter value %d != %d", len(data), progress)
|
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
buf = bytes.NewBuffer(data)
|
|
|
|
err = Verify(buf, blocksize, blocks)
|
|
|
|
t.Log(err)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected verify failure", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bytes.NewBuffer(append(data, '\n'))
|
|
|
|
err = Verify(buf, blocksize, blocks)
|
|
|
|
t.Log(err)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected verify success")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bytes.NewBuffer(data[:len(data)-1])
|
|
|
|
err = Verify(buf, blocksize, blocks)
|
|
|
|
t.Log(err)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected verify success")
|
|
|
|
}
|
|
|
|
|
|
|
|
data[42] = 42
|
|
|
|
buf = bytes.NewBuffer(data)
|
|
|
|
err = Verify(buf, blocksize, blocks)
|
|
|
|
t.Log(err)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected verify success")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:54:50 -07:00
|
|
|
func TestNormalization(t *testing.T) {
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
t.Skip("Normalization test not possible on darwin")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll("testdata/normalization")
|
|
|
|
defer os.RemoveAll("testdata/normalization")
|
|
|
|
|
|
|
|
tests := []string{
|
|
|
|
"0-A", // ASCII A -- accepted
|
|
|
|
"1-\xC3\x84", // NFC 'Ä' -- conflicts with the entry below, accepted
|
|
|
|
"1-\x41\xCC\x88", // NFD 'Ä' -- conflicts with the entry above, ignored
|
|
|
|
"2-\xC3\x85", // NFC 'Å' -- accepted
|
|
|
|
"3-\x41\xCC\x83", // NFD 'Ã' -- converted to NFC
|
|
|
|
"4-\xE2\x98\x95", // U+2615 HOT BEVERAGE (☕) -- accepted
|
|
|
|
"5-\xCD\xE2", // EUC-CN "wài" (外) -- ignored (not UTF8)
|
|
|
|
}
|
|
|
|
numInvalid := 2
|
2015-04-16 13:18:17 -07:00
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// On Windows, in case 5 the character gets replaced with a
|
|
|
|
// replacement character \xEF\xBF\xBD at the point it's written to disk,
|
|
|
|
// which means it suddenly becomes valid (sort of).
|
|
|
|
numInvalid--
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:54:50 -07:00
|
|
|
numValid := len(tests) - numInvalid
|
|
|
|
|
|
|
|
for _, s1 := range tests {
|
|
|
|
// Create a directory for each of the interesting strings above
|
2015-05-20 13:46:37 -07:00
|
|
|
if err := osutil.MkdirAll(filepath.Join("testdata/normalization", s1), 0755); err != nil {
|
2015-03-18 15:54:50 -07:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s2 := range tests {
|
|
|
|
// Within each dir, create a file with each of the interesting
|
|
|
|
// file names. Ensure that the file doesn't exist when it's
|
|
|
|
// created. This detects and fails if there's file name
|
|
|
|
// normalization stuff at the filesystem level.
|
|
|
|
if fd, err := os.OpenFile(filepath.Join("testdata/normalization", s1, s2), os.O_CREATE|os.O_EXCL, 0644); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
|
|
|
fd.WriteString("test")
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can normalize a directory name, but we can't descend into it in the
|
|
|
|
// same pass due to how filepath.Walk works. So we run the scan twice to
|
|
|
|
// make sure it all gets done. In production, things will be correct
|
|
|
|
// eventually...
|
|
|
|
|
|
|
|
_, err := walkDir("testdata/normalization")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tmp, err := walkDir("testdata/normalization")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
files := fileList(tmp).testfiles()
|
|
|
|
|
|
|
|
// We should have one file per combination, plus the directories
|
|
|
|
// themselves
|
|
|
|
|
|
|
|
expectedNum := numValid*numValid + numValid
|
|
|
|
if len(files) != expectedNum {
|
|
|
|
t.Errorf("Expected %d files, got %d", expectedNum, len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file names should all be in NFC form.
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
t.Logf("%q (% x) %v", f.name, f.name, norm.NFC.IsNormalString(f.name))
|
|
|
|
if !norm.NFC.IsNormalString(f.name) {
|
|
|
|
t.Errorf("File name %q is not NFC normalized", f.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 07:06:29 -07:00
|
|
|
func TestIssue1507(t *testing.T) {
|
|
|
|
w := Walker{}
|
|
|
|
c := make(chan protocol.FileInfo, 100)
|
2015-08-13 04:01:50 -07:00
|
|
|
fn := w.walkAndHashFiles(c, c)
|
2015-03-22 07:06:29 -07:00
|
|
|
|
|
|
|
fn("", nil, protocol.ErrClosed)
|
|
|
|
}
|
|
|
|
|
2015-03-18 15:54:50 -07:00
|
|
|
func walkDir(dir string) ([]protocol.FileInfo, error) {
|
|
|
|
w := Walker{
|
|
|
|
Dir: dir,
|
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
AutoNormalize: true,
|
2015-04-29 11:46:32 -07:00
|
|
|
Hashers: 2,
|
2015-03-18 15:54:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fchan, err := w.Walk()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmp []protocol.FileInfo
|
|
|
|
for f := range fchan {
|
|
|
|
tmp = append(tmp, f)
|
|
|
|
}
|
|
|
|
sort.Sort(fileList(tmp))
|
|
|
|
|
|
|
|
return tmp, nil
|
|
|
|
}
|
|
|
|
|
2014-07-30 11:10:46 -07:00
|
|
|
type fileList []protocol.FileInfo
|
|
|
|
|
2014-12-08 08:36:15 -07:00
|
|
|
func (l fileList) Len() int {
|
|
|
|
return len(l)
|
2014-07-30 11:10:46 -07:00
|
|
|
}
|
|
|
|
|
2014-12-08 08:36:15 -07:00
|
|
|
func (l fileList) Less(a, b int) bool {
|
|
|
|
return l[a].Name < l[b].Name
|
2014-07-30 11:10:46 -07:00
|
|
|
}
|
|
|
|
|
2014-12-08 08:36:15 -07:00
|
|
|
func (l fileList) Swap(a, b int) {
|
|
|
|
l[a], l[b] = l[b], l[a]
|
2014-07-30 11:10:46 -07:00
|
|
|
}
|
2014-08-30 00:22:23 -07:00
|
|
|
|
|
|
|
func (l fileList) testfiles() testfileList {
|
|
|
|
testfiles := make(testfileList, len(l))
|
|
|
|
for i, f := range l {
|
|
|
|
if len(f.Blocks) > 1 {
|
|
|
|
panic("simple test case stuff only supports a single block per file")
|
|
|
|
}
|
|
|
|
testfiles[i] = testfile{name: f.Name, size: int(f.Size())}
|
|
|
|
if len(f.Blocks) == 1 {
|
|
|
|
testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return testfiles
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l testfileList) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("{\n")
|
|
|
|
for _, f := range l {
|
|
|
|
fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.size, f.hash)
|
|
|
|
}
|
|
|
|
b.WriteString("}")
|
|
|
|
return b.String()
|
|
|
|
}
|
2015-09-04 03:54:01 -07:00
|
|
|
|
|
|
|
func TestSymlinkTypeEqual(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
onDiskType symlinks.TargetType
|
|
|
|
inIndexFlags uint32
|
|
|
|
equal bool
|
|
|
|
}{
|
|
|
|
// File is only equal to file
|
|
|
|
{symlinks.TargetFile, 0, true},
|
|
|
|
{symlinks.TargetFile, protocol.FlagDirectory, false},
|
|
|
|
{symlinks.TargetFile, protocol.FlagSymlinkMissingTarget, false},
|
|
|
|
// Directory is only equal to directory
|
|
|
|
{symlinks.TargetDirectory, 0, false},
|
|
|
|
{symlinks.TargetDirectory, protocol.FlagDirectory, true},
|
|
|
|
{symlinks.TargetDirectory, protocol.FlagSymlinkMissingTarget, false},
|
|
|
|
// Unknown is equal to anything
|
|
|
|
{symlinks.TargetUnknown, 0, true},
|
|
|
|
{symlinks.TargetUnknown, protocol.FlagDirectory, true},
|
|
|
|
{symlinks.TargetUnknown, protocol.FlagSymlinkMissingTarget, true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
res := SymlinkTypeEqual(tc.onDiskType, protocol.FileInfo{Flags: tc.inIndexFlags})
|
|
|
|
if res != tc.equal {
|
|
|
|
t.Errorf("Incorrect result %v for %v, %v", res, tc.onDiskType, tc.inIndexFlags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|