2017-04-06 18:36:09 +00:00
|
|
|
// +build !windows
|
2017-04-05 18:51:26 +00:00
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
package restic
|
2017-04-05 18:51:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2017-04-07 18:37:20 +00:00
|
|
|
"runtime"
|
2017-04-05 18:51:26 +00:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func stat(t testing.TB, filename string) (fi os.FileInfo, ok bool) {
|
|
|
|
fi, err := os.Lstat(filename)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
return fi, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi, true
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
func checkFile(t testing.TB, stat *syscall.Stat_t, node *Node) {
|
|
|
|
if uint32(node.Mode.Perm()) != uint32(stat.Mode&0777) {
|
2017-04-05 18:51:26 +00:00
|
|
|
t.Errorf("Mode does not match, want %v, got %v", stat.Mode&0777, node.Mode)
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
if node.Inode != uint64(stat.Ino) {
|
2017-04-05 18:51:26 +00:00
|
|
|
t.Errorf("Inode does not match, want %v, got %v", stat.Ino, node.Inode)
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
if node.DeviceID != uint64(stat.Dev) {
|
2017-04-05 18:51:26 +00:00
|
|
|
t.Errorf("Dev does not match, want %v, got %v", stat.Dev, node.DeviceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Size != uint64(stat.Size) {
|
|
|
|
t.Errorf("Size does not match, want %v, got %v", stat.Size, node.Size)
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
if node.Links != uint64(stat.Nlink) {
|
2017-04-05 18:51:26 +00:00
|
|
|
t.Errorf("Links does not match, want %v, got %v", stat.Nlink, node.Links)
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
if node.UID != stat.Uid {
|
|
|
|
t.Errorf("UID does not match, want %v, got %v", stat.Uid, node.UID)
|
2017-04-05 18:51:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
if node.GID != stat.Gid {
|
|
|
|
t.Errorf("UID does not match, want %v, got %v", stat.Gid, node.GID)
|
2017-04-05 18:51:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
// use the os dependent function to compare the timestamps
|
|
|
|
s, ok := toStatT(stat)
|
|
|
|
if !ok {
|
|
|
|
return
|
2017-04-05 18:51:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
mtime := s.mtim()
|
|
|
|
if node.ModTime != time.Unix(mtime.Unix()) {
|
|
|
|
t.Errorf("ModTime does not match, want %v, got %v", time.Unix(mtime.Unix()), node.ModTime)
|
2017-04-05 18:51:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
ctime := s.ctim()
|
|
|
|
if node.ChangeTime != time.Unix(ctime.Unix()) {
|
|
|
|
t.Errorf("ChangeTime does not match, want %v, got %v", time.Unix(ctime.Unix()), node.ChangeTime)
|
2017-04-05 18:51:26 +00:00
|
|
|
}
|
2017-04-06 18:36:09 +00:00
|
|
|
|
|
|
|
atime := s.atim()
|
|
|
|
if node.AccessTime != time.Unix(atime.Unix()) {
|
|
|
|
t.Errorf("AccessTime does not match, want %v, got %v", time.Unix(atime.Unix()), node.AccessTime)
|
|
|
|
}
|
|
|
|
|
2017-04-05 18:51:26 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
func checkDevice(t testing.TB, stat *syscall.Stat_t, node *Node) {
|
|
|
|
if node.Device != uint64(stat.Rdev) {
|
2017-04-05 18:51:26 +00:00
|
|
|
t.Errorf("Rdev does not match, want %v, got %v", stat.Rdev, node.Device)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeFromFileInfo(t *testing.T) {
|
2017-04-07 18:37:20 +00:00
|
|
|
type Test struct {
|
2017-04-05 18:51:26 +00:00
|
|
|
filename string
|
|
|
|
canSkip bool
|
2017-04-07 18:37:20 +00:00
|
|
|
}
|
|
|
|
var tests = []Test{
|
2017-04-05 18:51:26 +00:00
|
|
|
{"node_test.go", false},
|
|
|
|
{"/dev/sda", true},
|
|
|
|
}
|
|
|
|
|
2017-04-07 18:37:20 +00:00
|
|
|
// on darwin, users are not permitted to list the extended attributes of
|
|
|
|
// /dev/null, therefore skip it.
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
tests = append(tests, Test{"/dev/null", true})
|
|
|
|
}
|
|
|
|
|
2017-04-05 18:51:26 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
fi, found := stat(t, test.filename)
|
|
|
|
if !found && test.canSkip {
|
|
|
|
t.Skipf("%v not found in filesystem")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Sys() == nil {
|
|
|
|
t.Skip("fi.Sys() is nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s, ok := fi.Sys().(*syscall.Stat_t)
|
|
|
|
if !ok {
|
|
|
|
t.Skip("fi type is %T, not stat_t", fi.Sys())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:36:09 +00:00
|
|
|
node, err := NodeFromFileInfo(test.filename, fi)
|
2017-04-05 18:51:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.Type {
|
|
|
|
case "file":
|
|
|
|
checkFile(t, s, node)
|
|
|
|
case "dev", "chardev":
|
|
|
|
checkFile(t, s, node)
|
|
|
|
checkDevice(t, s, node)
|
|
|
|
default:
|
|
|
|
t.Fatalf("invalid node type %q", node.Type)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|