-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathworkingdir_test.go
More file actions
181 lines (149 loc) · 4.98 KB
/
workingdir_test.go
File metadata and controls
181 lines (149 loc) · 4.98 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
type fakeConn struct {
user string
}
func (f fakeConn) User() string { return f.user }
func (fakeConn) RemoteAddr() string { return "" }
func (fakeConn) UniqueID() string { return "" }
func (fakeConn) GetMeta(key string) string { return "" }
type workingdirFactory = workdingdirFactory
func TestIsUsernameSecure(t *testing.T) {
cases := []struct {
name string
user string
want bool
}{
{name: "simple", user: "alice", want: true},
{name: "withDash", user: "a-user", want: true},
{name: "withUnderscore", user: "a_user", want: true},
{name: "leadingUnderscore", user: "_alice", want: true},
{name: "withNumber", user: "a1", want: true},
{name: "leadingDash", user: "-alice", want: false},
{name: "startsWithNumber", user: "1alice", want: false},
{name: "containsUppercase", user: "Alice", want: false},
{name: "tooLong", user: strings.Repeat("a", 33), want: false},
{name: "empty", user: "", want: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := isUsernameSecure(tc.user); got != tc.want {
t.Fatalf("isUsernameSecure(%q) = %v, want %v", tc.user, got, tc.want)
}
})
}
}
func TestParseUpstreamFile(t *testing.T) {
t.Run("parseUserAndHost", func(t *testing.T) {
host, user, err := parseUpstreamFile("[email protected]:2200\n")
if err != nil {
t.Fatalf("parseUpstreamFile returned error: %v", err)
}
if host != "example.com:2200" || user != "bob" {
t.Fatalf("unexpected parse result host=%q user=%q", host, user)
}
})
t.Run("skipCommentsAndWhitespace", func(t *testing.T) {
data := "# comment line\n\nexample.com\n"
host, user, err := parseUpstreamFile(data)
if err != nil {
t.Fatalf("parseUpstreamFile returned error: %v", err)
}
if host != "example.com" || user != "" {
t.Fatalf("unexpected parse result host=%q user=%q", host, user)
}
})
t.Run("emptyContentReturnsError", func(t *testing.T) {
if _, _, err := parseUpstreamFile("# only comment\n"); err == nil {
t.Fatal("expected error for empty content but got nil")
}
})
}
func TestWorkingdirPermissionChecks(t *testing.T) {
tmp := t.TempDir()
w := &workingdir{Path: tmp}
secureFile := "secure"
if err := os.WriteFile(filepath.Join(tmp, secureFile), []byte("ok"), 0o400); err != nil {
t.Fatalf("failed to write secure file: %v", err)
}
if _, err := w.Readfile(secureFile); err != nil {
t.Fatalf("Readfile on secure file returned error: %v", err)
}
openFile := "open"
if err := os.WriteFile(filepath.Join(tmp, openFile), []byte("nope"), 0o644); err != nil {
t.Fatalf("failed to write open file: %v", err)
}
if err := w.checkPerm(openFile); err == nil {
t.Fatal("expected error for file with open permission, got nil")
}
w.NoCheckPerm = true
if _, err := w.Readfile(openFile); err != nil {
t.Fatalf("Readfile should ignore permission check when NoCheckPerm is true, got error: %v", err)
}
}
func TestWorkingdirFactoryListPipe(t *testing.T) {
root := t.TempDir()
user := "alice"
userDir := filepath.Join(root, user)
if err := os.MkdirAll(userDir, 0o700); err != nil {
t.Fatalf("failed to create user dir: %v", err)
}
if err := os.WriteFile(filepath.Join(userDir, userUpstreamFile), []byte("[email protected]:2200\n"), 0o400); err != nil {
t.Fatalf("failed to write upstream file: %v", err)
}
// presence of both files should cause From to return public key wrapper
if err := os.WriteFile(filepath.Join(userDir, userAuthorizedKeysFile), []byte("key"), 0o400); err != nil {
t.Fatalf("failed to write authorized_keys: %v", err)
}
if err := os.WriteFile(filepath.Join(userDir, userKeyFile), []byte("private"), 0o400); err != nil {
t.Fatalf("failed to write id_rsa: %v", err)
}
fac := workingdirFactory{
root: root,
allowBadUsername: true,
noCheckPerm: false,
strictHostKey: true,
recursiveSearch: false,
}
pipes, err := fac.listPipe(fakeConn{user: user})
if err != nil {
t.Fatalf("listPipe returned error: %v", err)
}
if len(pipes) != 1 {
t.Fatalf("expected 1 pipe, got %d", len(pipes))
}
pipe, ok := pipes[0].(*skelpipeWrapper)
if !ok {
t.Fatalf("unexpected pipe type %T", pipes[0])
}
if pipe.host != "example.com:2200" || pipe.username != "bob" {
t.Fatalf("unexpected upstream host/user %q/%q", pipe.host, pipe.username)
}
if !pipe.dir.Strict {
t.Fatal("expected Strict flag to propagate to workingdir")
}
from := pipe.From()
if len(from) != 1 {
t.Fatalf("expected 1 From entry, got %d", len(from))
}
pub, ok := from[0].(*skelpipePublicKeyWrapper)
if !ok {
t.Fatalf("expected skelpipePublicKeyWrapper, got %T", from[0])
}
to, err := pub.MatchConn(fakeConn{user: user})
if err != nil {
t.Fatalf("MatchConn returned error: %v", err)
}
private, ok := to.(*skelpipeToPrivateKeyWrapper)
if !ok {
t.Fatalf("expected skelpipeToPrivateKeyWrapper, got %T", to)
}
if private.IgnoreHostKey(fakeConn{user: user}) {
t.Fatal("expected IgnoreHostKey to respect strict host key settings")
}
}