2017-08-19 22:44:18 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2017-09-10 15:20:26 +02:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/test"
|
2017-08-19 22:44:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsExcludedByFile(t *testing.T) {
|
|
|
|
const (
|
|
|
|
tagFilename = "CACHEDIR.TAG"
|
|
|
|
header = "Signature: 8a477f597d28d172789f06886806bc55"
|
|
|
|
)
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
tagFile string
|
|
|
|
content string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"NoTagfile", "", "", false},
|
|
|
|
{"EmptyTagfile", tagFilename, "", true},
|
|
|
|
{"UnnamedTagFile", "", header, false},
|
|
|
|
{"WrongTagFile", "notatagfile", header, false},
|
|
|
|
{"IncorrectSig", tagFilename, header[1:], false},
|
|
|
|
{"ValidSig", tagFilename, header, true},
|
|
|
|
{"ValidPlusStuff", tagFilename, header + "foo", true},
|
|
|
|
{"ValidPlusNewlineAndStuff", tagFilename, header + "\nbar", true},
|
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2017-09-10 15:20:26 +02:00
|
|
|
tempDir, cleanup := test.TempDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-08-19 22:44:18 +02:00
|
|
|
foo := filepath.Join(tempDir, "foo")
|
2017-09-10 15:20:26 +02:00
|
|
|
err := ioutil.WriteFile(foo, []byte("foo"), 0666)
|
2017-08-19 22:44:18 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not write file: %v", err)
|
|
|
|
}
|
|
|
|
if tc.tagFile != "" {
|
|
|
|
tagFile := filepath.Join(tempDir, tc.tagFile)
|
|
|
|
err = ioutil.WriteFile(tagFile, []byte(tc.content), 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not write tagfile: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h := header
|
|
|
|
if tc.content == "" {
|
|
|
|
h = ""
|
|
|
|
}
|
|
|
|
if got := isExcludedByFile(foo, tagFilename, h); tc.want != got {
|
|
|
|
t.Fatalf("expected %v, got %v", tc.want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|