restic: test NodeFromFileInfo for symlinks

This commit is contained in:
Michael Eischer 2023-07-08 18:18:13 +02:00
parent 1f1e50f49e
commit 4a5ae2ba84
1 changed files with 11 additions and 2 deletions

View File

@ -5,10 +5,13 @@ package restic
import ( import (
"os" "os"
"path/filepath"
"runtime" "runtime"
"syscall" "syscall"
"testing" "testing"
"time" "time"
rtest "github.com/restic/restic/internal/test"
) )
func stat(t testing.TB, filename string) (fi os.FileInfo, ok bool) { func stat(t testing.TB, filename string) (fi os.FileInfo, ok bool) {
@ -25,6 +28,7 @@ func stat(t testing.TB, filename string) (fi os.FileInfo, ok bool) {
} }
func checkFile(t testing.TB, stat *syscall.Stat_t, node *Node) { func checkFile(t testing.TB, stat *syscall.Stat_t, node *Node) {
t.Helper()
if uint32(node.Mode.Perm()) != uint32(stat.Mode&0777) { if uint32(node.Mode.Perm()) != uint32(stat.Mode&0777) {
t.Errorf("Mode does not match, want %v, got %v", stat.Mode&0777, node.Mode) t.Errorf("Mode does not match, want %v, got %v", stat.Mode&0777, node.Mode)
} }
@ -37,7 +41,7 @@ func checkFile(t testing.TB, stat *syscall.Stat_t, node *Node) {
t.Errorf("Dev does not match, want %v, got %v", stat.Dev, node.DeviceID) t.Errorf("Dev does not match, want %v, got %v", stat.Dev, node.DeviceID)
} }
if node.Size != uint64(stat.Size) { if node.Size != uint64(stat.Size) && node.Type != "symlink" {
t.Errorf("Size does not match, want %v, got %v", stat.Size, node.Size) t.Errorf("Size does not match, want %v, got %v", stat.Size, node.Size)
} }
@ -83,6 +87,10 @@ func checkDevice(t testing.TB, stat *syscall.Stat_t, node *Node) {
} }
func TestNodeFromFileInfo(t *testing.T) { func TestNodeFromFileInfo(t *testing.T) {
tmp := t.TempDir()
symlink := filepath.Join(tmp, "symlink")
rtest.OK(t, os.Symlink("target", symlink))
type Test struct { type Test struct {
filename string filename string
canSkip bool canSkip bool
@ -90,6 +98,7 @@ func TestNodeFromFileInfo(t *testing.T) {
var tests = []Test{ var tests = []Test{
{"node_test.go", false}, {"node_test.go", false},
{"/dev/sda", true}, {"/dev/sda", true},
{symlink, false},
} }
// on darwin, users are not permitted to list the extended attributes of // on darwin, users are not permitted to list the extended attributes of
@ -125,7 +134,7 @@ func TestNodeFromFileInfo(t *testing.T) {
} }
switch node.Type { switch node.Type {
case "file": case "file", "symlink":
checkFile(t, s, node) checkFile(t, s, node)
case "dev", "chardev": case "dev", "chardev":
checkFile(t, s, node) checkFile(t, s, node)