Repository navigation
Expand file tree
/
Copy pathserver_nowindows_test.go
More file actions
146 lines (138 loc) · 3.6 KB
/
Copy pathserver_nowindows_test.go
File metadata and controls
146 lines (138 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//go:build !windows
// +build !windows
package sftp
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestServerSymlinkTargets(t *testing.T) {
for _, withWorkDir := range []bool{false, true} {
name := "without working directory"
if withWorkDir {
name = "with working directory"
}
t.Run(name, func(t *testing.T) {
dir := t.TempDir()
require.NoError(t, os.Mkdir(filepath.Join(dir, "sub"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(dir, "sub", "file"), []byte("subdirectory"), 0o600))
require.NoError(t, os.WriteFile(filepath.Join(dir, "file"), []byte("parent"), 0o600))
var options []ServerOption
if withWorkDir {
options = append(options, WithServerWorkingDirectory(dir))
}
client, server := clientServerPair(t, options...)
defer client.Close()
defer server.Close()
for _, tt := range []struct {
name, target, contents string
}{
{"relative", "file", "subdirectory"},
{"parent", "../file", "parent"},
{"absolute", filepath.ToSlash(filepath.Join(dir, "file")), "parent"},
{"dangling", "missing", ""},
} {
t.Run(tt.name, func(t *testing.T) {
link := "sub/" + tt.name
if !withWorkDir {
link = filepath.ToSlash(filepath.Join(dir, link))
}
require.NoError(t, client.Symlink(tt.target, link))
target, err := client.ReadLink(link)
require.NoError(t, err)
require.Equal(t, tt.target, target)
if tt.name == "dangling" {
_, err := client.Stat(link)
require.True(t, os.IsNotExist(err), "stat dangling link: %v", err)
return
}
file, err := client.Open(link)
require.NoError(t, err)
defer file.Close()
contents, err := io.ReadAll(file)
require.NoError(t, err)
require.Equal(t, tt.contents, string(contents))
})
}
})
}
}
func TestServer_toLocalPath(t *testing.T) {
tests := []struct {
name string
withWorkDir string
p string
want string
}{
{
name: "empty path with no workdir",
p: "",
want: "",
},
{
name: "relative path with no workdir",
p: "file",
want: "file",
},
{
name: "absolute path with no workdir",
p: "/file",
want: "/file",
},
{
name: "workdir and empty path",
withWorkDir: "/home/user",
p: "",
want: "/home/user",
},
{
name: "workdir and relative path",
withWorkDir: "/home/user",
p: "file",
want: "/home/user/file",
},
{
name: "workdir and relative path with .",
withWorkDir: "/home/user",
p: ".",
want: "/home/user",
},
{
name: "workdir and relative path with . and file",
withWorkDir: "/home/user",
p: "./file",
want: "/home/user/file",
},
{
name: "workdir and absolute path",
withWorkDir: "/home/user",
p: "/file",
want: "/file",
},
{
name: "workdir and non-unixy path prefixes workdir",
withWorkDir: "/home/user",
p: "C:\\file",
// This may look like a bug but it is the result of passing
// invalid input (a non-unixy path) to the server.
want: "/home/user/C:\\file",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// We don't need to initialize the Server further to test
// toLocalPath behavior.
s := &Server{}
if tt.withWorkDir != "" {
if err := WithServerWorkingDirectory(tt.withWorkDir)(s); err != nil {
t.Fatal(err)
}
}
if got := s.toLocalPath(tt.p); got != tt.want {
t.Errorf("Server.toLocalPath() = %q, want %q", got, tt.want)
}
})
}
}