2015-05-14 16:05:15 +00:00
|
|
|
package restic_test
|
2015-05-01 21:52:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2015-05-14 16:05:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-07-09 14:52:44 +00:00
|
|
|
"runtime"
|
2015-05-01 21:52:36 +00:00
|
|
|
"testing"
|
2015-05-14 16:05:15 +00:00
|
|
|
"time"
|
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic"
|
|
|
|
. "restic/test"
|
2015-05-01 21:52:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkNodeFillUser(t *testing.B) {
|
|
|
|
tempfile, err := ioutil.TempFile("", "restic-test-temp-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := tempfile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := tempfile.Name()
|
|
|
|
|
|
|
|
t.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < t.N; i++ {
|
2015-05-14 16:05:15 +00:00
|
|
|
restic.NodeFromFileInfo(path, fi)
|
2015-05-01 21:52:36 +00:00
|
|
|
}
|
2015-10-31 13:53:03 +00:00
|
|
|
|
|
|
|
OK(t, tempfile.Close())
|
|
|
|
RemoveAll(t, tempfile.Name())
|
2015-05-01 21:52:36 +00:00
|
|
|
}
|
2015-05-01 22:26:32 +00:00
|
|
|
|
|
|
|
func BenchmarkNodeFromFileInfo(t *testing.B) {
|
|
|
|
tempfile, err := ioutil.TempFile("", "restic-test-temp-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := tempfile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := tempfile.Name()
|
|
|
|
|
|
|
|
t.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < t.N; i++ {
|
2015-05-14 16:05:15 +00:00
|
|
|
_, err := restic.NodeFromFileInfo(path, fi)
|
2015-05-01 22:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 13:53:03 +00:00
|
|
|
|
|
|
|
OK(t, tempfile.Close())
|
|
|
|
RemoveAll(t, tempfile.Name())
|
2015-05-01 22:26:32 +00:00
|
|
|
}
|
2015-05-14 16:05:15 +00:00
|
|
|
|
|
|
|
func parseTime(s string) time.Time {
|
|
|
|
t, err := time.Parse("2006-01-02 15:04:05.999", s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Local()
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodeTests = []restic.Node{
|
|
|
|
restic.Node{
|
|
|
|
Name: "testFile",
|
2016-09-01 19:20:03 +00:00
|
|
|
Type: "file",
|
2016-08-31 20:39:36 +00:00
|
|
|
Content: restic.IDs{},
|
2015-05-14 16:05:15 +00:00
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
|
|
|
Mode: 0604,
|
|
|
|
ModTime: parseTime("2015-05-14 21:07:23.111"),
|
|
|
|
AccessTime: parseTime("2015-05-14 21:07:24.222"),
|
|
|
|
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
|
|
|
|
},
|
2015-12-20 18:45:36 +00:00
|
|
|
restic.Node{
|
|
|
|
Name: "testSuidFile",
|
2016-09-01 19:20:03 +00:00
|
|
|
Type: "file",
|
2016-08-31 20:39:36 +00:00
|
|
|
Content: restic.IDs{},
|
2015-12-20 18:45:36 +00:00
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
|
|
|
Mode: 0755 | os.ModeSetuid,
|
|
|
|
ModTime: parseTime("2015-05-14 21:07:23.111"),
|
|
|
|
AccessTime: parseTime("2015-05-14 21:07:24.222"),
|
|
|
|
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
|
|
|
|
},
|
|
|
|
restic.Node{
|
|
|
|
Name: "testSuidFile2",
|
2016-09-01 19:20:03 +00:00
|
|
|
Type: "file",
|
2016-08-31 20:39:36 +00:00
|
|
|
Content: restic.IDs{},
|
2015-12-20 18:45:36 +00:00
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
|
|
|
Mode: 0755 | os.ModeSetgid,
|
|
|
|
ModTime: parseTime("2015-05-14 21:07:23.111"),
|
|
|
|
AccessTime: parseTime("2015-05-14 21:07:24.222"),
|
|
|
|
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
|
|
|
|
},
|
|
|
|
restic.Node{
|
|
|
|
Name: "testSticky",
|
2016-09-01 19:20:03 +00:00
|
|
|
Type: "file",
|
2016-08-31 20:39:36 +00:00
|
|
|
Content: restic.IDs{},
|
2015-12-20 18:45:36 +00:00
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
|
|
|
Mode: 0755 | os.ModeSticky,
|
|
|
|
ModTime: parseTime("2015-05-14 21:07:23.111"),
|
|
|
|
AccessTime: parseTime("2015-05-14 21:07:24.222"),
|
|
|
|
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
|
|
|
|
},
|
2015-05-14 16:05:15 +00:00
|
|
|
restic.Node{
|
|
|
|
Name: "testDir",
|
2016-09-01 19:20:03 +00:00
|
|
|
Type: "dir",
|
2015-05-14 16:05:15 +00:00
|
|
|
Subtree: nil,
|
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
2015-12-20 18:45:36 +00:00
|
|
|
Mode: 0750 | os.ModeDir,
|
2015-05-14 16:05:15 +00:00
|
|
|
ModTime: parseTime("2015-05-14 21:07:23.111"),
|
|
|
|
AccessTime: parseTime("2015-05-14 21:07:24.222"),
|
|
|
|
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
|
|
|
|
},
|
|
|
|
restic.Node{
|
|
|
|
Name: "testSymlink",
|
2016-09-01 19:20:03 +00:00
|
|
|
Type: "symlink",
|
2015-05-14 16:05:15 +00:00
|
|
|
LinkTarget: "invalid",
|
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
2015-12-20 18:45:36 +00:00
|
|
|
Mode: 0777 | os.ModeSymlink,
|
2015-05-14 16:05:15 +00:00
|
|
|
ModTime: parseTime("2015-05-14 21:07:23.111"),
|
|
|
|
AccessTime: parseTime("2015-05-14 21:07:24.222"),
|
|
|
|
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
|
|
|
|
},
|
2016-11-15 20:41:41 +00:00
|
|
|
|
|
|
|
// include "testFile" and "testDir" again with slightly different
|
|
|
|
// metadata, so we can test if CreateAt works with pre-existing files.
|
|
|
|
restic.Node{
|
|
|
|
Name: "testFile",
|
|
|
|
Type: "file",
|
|
|
|
Content: restic.IDs{},
|
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
|
|
|
Mode: 0604,
|
|
|
|
ModTime: parseTime("2005-05-14 21:07:03.111"),
|
|
|
|
AccessTime: parseTime("2005-05-14 21:07:04.222"),
|
|
|
|
ChangeTime: parseTime("2005-05-14 21:07:05.333"),
|
|
|
|
},
|
|
|
|
restic.Node{
|
|
|
|
Name: "testDir",
|
|
|
|
Type: "dir",
|
|
|
|
Subtree: nil,
|
|
|
|
UID: uint32(os.Getuid()),
|
|
|
|
GID: uint32(os.Getgid()),
|
|
|
|
Mode: 0750 | os.ModeDir,
|
|
|
|
ModTime: parseTime("2005-05-14 21:07:03.111"),
|
|
|
|
AccessTime: parseTime("2005-05-14 21:07:04.222"),
|
|
|
|
ChangeTime: parseTime("2005-05-14 21:07:05.333"),
|
|
|
|
},
|
2015-05-14 16:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeRestoreAt(t *testing.T) {
|
2015-06-13 10:35:19 +00:00
|
|
|
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
|
2015-05-14 16:05:15 +00:00
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
defer func() {
|
2016-01-23 18:12:02 +00:00
|
|
|
if TestCleanupTempDirs {
|
2015-08-18 19:05:49 +00:00
|
|
|
RemoveAll(t, tempdir)
|
2015-05-14 16:05:15 +00:00
|
|
|
} else {
|
|
|
|
t.Logf("leaving tempdir at %v", tempdir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, test := range nodeTests {
|
|
|
|
nodePath := filepath.Join(tempdir, test.Name)
|
|
|
|
OK(t, test.CreateAt(nodePath, nil))
|
|
|
|
|
2016-09-01 19:20:03 +00:00
|
|
|
if test.Type == "symlink" && runtime.GOOS == "windows" {
|
2015-08-14 13:57:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-09-01 19:20:03 +00:00
|
|
|
if test.Type == "dir" {
|
2015-05-14 16:05:15 +00:00
|
|
|
OK(t, test.RestoreTimestamps(nodePath))
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Lstat(nodePath)
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
n2, err := restic.NodeFromFileInfo(nodePath, fi)
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
Assert(t, test.Name == n2.Name,
|
2016-09-01 19:20:03 +00:00
|
|
|
"%v: name doesn't match (%v != %v)", test.Type, test.Name, n2.Name)
|
|
|
|
Assert(t, test.Type == n2.Type,
|
|
|
|
"%v: type doesn't match (%v != %v)", test.Type, test.Type, n2.Type)
|
2015-05-14 16:05:15 +00:00
|
|
|
Assert(t, test.Size == n2.Size,
|
2015-07-08 13:15:06 +00:00
|
|
|
"%v: size doesn't match (%v != %v)", test.Size, test.Size, n2.Size)
|
2015-08-14 13:57:47 +00:00
|
|
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
Assert(t, test.UID == n2.UID,
|
2016-09-01 19:20:03 +00:00
|
|
|
"%v: UID doesn't match (%v != %v)", test.Type, test.UID, n2.UID)
|
2015-08-14 13:57:47 +00:00
|
|
|
Assert(t, test.GID == n2.GID,
|
2016-09-01 19:20:03 +00:00
|
|
|
"%v: GID doesn't match (%v != %v)", test.Type, test.GID, n2.GID)
|
|
|
|
if test.Type != "symlink" {
|
2015-08-14 13:57:47 +00:00
|
|
|
Assert(t, test.Mode == n2.Mode,
|
2016-09-01 19:20:03 +00:00
|
|
|
"%v: mode doesn't match (0%o != 0%o)", test.Type, test.Mode, n2.Mode)
|
2015-08-14 13:57:47 +00:00
|
|
|
}
|
2015-07-09 14:52:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:20:03 +00:00
|
|
|
AssertFsTimeEqual(t, "AccessTime", test.Type, test.AccessTime, n2.AccessTime)
|
|
|
|
AssertFsTimeEqual(t, "ModTime", test.Type, test.ModTime, n2.ModTime)
|
2015-07-09 14:52:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func AssertFsTimeEqual(t *testing.T, label string, nodeType string, t1 time.Time, t2 time.Time) {
|
|
|
|
var equal bool
|
|
|
|
|
2015-07-20 19:29:21 +00:00
|
|
|
// Go currently doesn't support setting timestamps of symbolic links on darwin and bsd
|
2015-07-20 19:02:38 +00:00
|
|
|
if nodeType == "symlink" {
|
|
|
|
switch runtime.GOOS {
|
2015-07-20 19:29:21 +00:00
|
|
|
case "darwin", "freebsd", "openbsd":
|
2015-07-09 14:52:44 +00:00
|
|
|
return
|
|
|
|
}
|
2015-07-20 19:02:38 +00:00
|
|
|
}
|
2015-07-09 14:52:44 +00:00
|
|
|
|
2015-07-20 19:02:38 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
2015-07-09 14:52:44 +00:00
|
|
|
// HFS+ timestamps don't support sub-second precision,
|
|
|
|
// see https://en.wikipedia.org/wiki/Comparison_of_file_systems
|
|
|
|
diff := int(t1.Sub(t2).Seconds())
|
|
|
|
equal = diff == 0
|
2015-07-20 19:02:38 +00:00
|
|
|
default:
|
2015-07-09 14:52:44 +00:00
|
|
|
equal = t1.Equal(t2)
|
2015-05-14 16:05:15 +00:00
|
|
|
}
|
2015-07-09 14:52:44 +00:00
|
|
|
|
|
|
|
Assert(t, equal, "%s: %s doesn't match (%v != %v)", label, nodeType, t1, t2)
|
2015-05-14 16:05:15 +00:00
|
|
|
}
|