| 1 | |
| 2 | |
| 3 | |
| 4 | package sftp |
| 5 | |
| 6 | import ( |
| 7 | "io" |
| 8 | "math" |
| 9 | "net" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "sync/atomic" |
| 13 | "syscall" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/stretchr/testify/assert" |
| 18 | "github.com/stretchr/testify/require" |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | |
| 23 | type osHandlers struct { |
| 24 | dir string |
| 25 | } |
| 26 | |
| 27 | func (h *osHandlers) p(r *Request) string { return filepath.Join(h.dir, r.Filepath) } |
| 28 | |
| 29 | func (h *osHandlers) Fileread(r *Request) (io.ReaderAt, error) { return os.Open(h.p(r)) } |
| 30 | func (h *osHandlers) Filewrite(r *Request) (io.WriterAt, error) { |
| 31 | return os.OpenFile(h.p(r), os.O_WRONLY|os.O_CREATE, 0o644) |
| 32 | } |
| 33 | func (h *osHandlers) OpenFile(r *Request) (WriterAtReaderAt, error) { |
| 34 | return os.OpenFile(h.p(r), os.O_RDWR|os.O_CREATE, 0o644) |
| 35 | } |
| 36 | func (h *osHandlers) Filecmd(r *Request) error { return ErrSSHFxOpUnsupported } |
| 37 | func (h *osHandlers) Filelist(r *Request) (ListerAt, error) { |
| 38 | switch r.Filepath { |
| 39 | case "/panic": |
| 40 | panic("handler bug") |
| 41 | case "/slow": |
| 42 | time.Sleep(100 * time.Millisecond) |
| 43 | } |
| 44 | if r.Method == "List" { |
| 45 | f, err := os.Open(h.p(r)) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | defer f.Close() |
| 50 | fis, err := f.Readdir(-1) |
| 51 | return listerat(fis), err |
| 52 | } |
| 53 | fi, err := os.Stat(h.p(r)) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | return listerat{fi}, nil |
| 58 | } |
| 59 | |
| 60 | |
| 61 | type rawClient struct { |
| 62 | t *testing.T |
| 63 | c net.Conn |
| 64 | rs *RequestServer |
| 65 | id uint32 |
| 66 | } |
| 67 | |
| 68 | func newRawClient(t *testing.T, h Handlers, opts ...RequestServerOption) *rawClient { |
| 69 | a, b := net.Pipe() |
| 70 | rs := NewRequestServer(a, h, append([]RequestServerOption{WithRSSequential()}, opts...)...) |
| 71 | go rs.Serve() |
| 72 | t.Cleanup(func() { b.Close() }) |
| 73 | return &rawClient{t: t, c: b, rs: rs} |
| 74 | } |
| 75 | |
| 76 | type rawPacket []byte |
| 77 | |
| 78 | func (b rawPacket) MarshalBinary() ([]byte, error) { return b, nil } |
| 79 | |
| 80 | func (r *rawClient) send(typ byte, fields ...any) uint32 { |
| 81 | r.id++ |
| 82 | b := []byte{0, 0, 0, 0, typ} |
| 83 | b = marshalUint32(b, r.id) |
| 84 | for _, f := range fields { |
| 85 | b = marshal(b, f) |
| 86 | } |
| 87 | require.NoError(r.t, sendPacket(r.c, rawPacket(b))) |
| 88 | return r.id |
| 89 | } |
| 90 | |
| 91 | func (r *rawClient) recv() (fxp, []byte) { |
| 92 | require.NoError(r.t, r.c.SetReadDeadline(time.Now().Add(5*time.Second))) |
| 93 | typ, data, err := recvPacket(r.c, nil, 0) |
| 94 | require.NoError(r.t, err) |
| 95 | return typ, data |
| 96 | } |
| 97 | |
| 98 | |
| 99 | func (r *rawClient) status() uint32 { |
| 100 | typ, data := r.recv() |
| 101 | require.Equal(r.t, fxp(sshFxpStatus), typ) |
| 102 | _, data = unmarshalUint32(data) |
| 103 | code, _ := unmarshalUint32(data) |
| 104 | return code |
| 105 | } |
| 106 | |
| 107 | func (r *rawClient) handle(typ byte, path string, fields ...any) string { |
| 108 | r.send(typ, append([]any{path}, fields...)...) |
| 109 | rtyp, data := r.recv() |
| 110 | require.Equal(r.t, fxp(sshFxpHandle), rtyp) |
| 111 | _, data = unmarshalUint32(data) |
| 112 | h, _ := unmarshalString(data) |
| 113 | return h |
| 114 | } |
| 115 | |
| 116 | |
| 117 | func TestRequestHandlePacketMismatch(t *testing.T) { |
| 118 | dir := t.TempDir() |
| 119 | require.NoError(t, os.WriteFile(filepath.Join(dir, "f"), []byte("hello"), 0o644)) |
| 120 | require.NoError(t, os.WriteFile(filepath.Join(dir, "g"), nil, 0o644)) |
| 121 | h := &osHandlers{dir: dir} |
| 122 | c := newRawClient(t, Handlers{h, h, h, h}) |
| 123 | |
| 124 | wh := c.handle(sshFxpOpen, "/f", uint32(sshFxfWrite), uint32(0)) |
| 125 | rh := c.handle(sshFxpOpen, "/f", uint32(sshFxfRead), uint32(0)) |
| 126 | dh := c.handle(sshFxpOpendir, "/") |
| 127 | |
| 128 | c.send(sshFxpRead, wh, uint64(10), uint32(100)) |
| 129 | assert.NotEqual(t, uint32(sshFxOk), c.status(), "READ on a write-only handle") |
| 130 | c.send(sshFxpWrite, rh, uint64(0), "XXXX") |
| 131 | assert.NotEqual(t, uint32(sshFxOk), c.status(), "WRITE on a read-only handle") |
| 132 | c.send(sshFxpReaddir, rh) |
| 133 | assert.NotEqual(t, uint32(sshFxOk), c.status(), "READDIR on a file handle") |
| 134 | c.send(sshFxpRead, dh, uint64(0), uint32(100)) |
| 135 | assert.NotEqual(t, uint32(sshFxOk), c.status(), "READ on a directory handle") |
| 136 | |
| 137 | b, err := os.ReadFile(filepath.Join(dir, "f")) |
| 138 | require.NoError(t, err) |
| 139 | assert.Equal(t, "hello", string(b)) |
| 140 | c.send(sshFxpReaddir, dh) |
| 141 | typ, data := c.recv() |
| 142 | require.Equal(t, fxp(sshFxpName), typ, "the READ must not have consumed the entries") |
| 143 | _, data = unmarshalUint32(data) |
| 144 | n, _ := unmarshalUint32(data) |
| 145 | assert.Equal(t, uint32(2), n) |
| 146 | } |
| 147 | |
| 148 | func TestRequestServerMaxHandles(t *testing.T) { |
| 149 | dir := t.TempDir() |
| 150 | require.NoError(t, os.WriteFile(filepath.Join(dir, "f"), nil, 0o644)) |
| 151 | h := &osHandlers{dir: dir} |
| 152 | c := newRawClient(t, Handlers{h, h, h, h}, WithRSMaxHandles(2)) |
| 153 | |
| 154 | first := c.handle(sshFxpOpen, "/f", uint32(sshFxfRead), uint32(0)) |
| 155 | c.handle(sshFxpOpendir, "/") |
| 156 | c.send(sshFxpOpen, "/f", uint32(sshFxfRead), uint32(0)) |
| 157 | assert.Equal(t, uint32(sshFxFailure), c.status(), "OPEN over the limit") |
| 158 | c.send(sshFxpOpendir, "/") |
| 159 | assert.Equal(t, uint32(sshFxFailure), c.status(), "OPENDIR over the limit") |
| 160 | c.send(sshFxpClose, first) |
| 161 | require.Equal(t, uint32(sshFxOk), c.status()) |
| 162 | c.handle(sshFxpOpen, "/f", uint32(sshFxfRead), uint32(0)) |
| 163 | } |
| 164 | |
| 165 | |
| 166 | func TestRequestServerHandlerPanic(t *testing.T) { |
| 167 | h := &osHandlers{dir: t.TempDir()} |
| 168 | c := newRawClient(t, Handlers{h, h, h, h}) |
| 169 | c.send(sshFxpStat, "/panic") |
| 170 | assert.Equal(t, uint32(sshFxFailure), c.status()) |
| 171 | c.send(sshFxpStat, "/") |
| 172 | typ, _ := c.recv() |
| 173 | assert.Equal(t, fxp(sshFxpAttrs), typ) |
| 174 | } |
| 175 | |
| 176 | type syncCounter struct { |
| 177 | *os.File |
| 178 | n *atomic.Int32 |
| 179 | } |
| 180 | |
| 181 | func (s syncCounter) Sync() error { |
| 182 | s.n.Add(1) |
| 183 | return s.File.Sync() |
| 184 | } |
| 185 | |
| 186 | type syncHandlers struct { |
| 187 | *osHandlers |
| 188 | n atomic.Int32 |
| 189 | } |
| 190 | |
| 191 | func (h *syncHandlers) Filewrite(r *Request) (io.WriterAt, error) { |
| 192 | f, err := os.OpenFile(h.p(r), os.O_WRONLY|os.O_CREATE, 0o644) |
| 193 | if err != nil { |
| 194 | return nil, err |
| 195 | } |
| 196 | return syncCounter{f, &h.n}, nil |
| 197 | } |
| 198 | |
| 199 | func TestRequestFsync(t *testing.T) { |
| 200 | h := &syncHandlers{osHandlers: &osHandlers{dir: t.TempDir()}} |
| 201 | c := newRawClient(t, Handlers{h, h, h, h}) |
| 202 | c.send(sshFxpInit) |
| 203 | typ, data := c.recv() |
| 204 | require.Equal(t, fxp(sshFxpVersion), typ) |
| 205 | assert.Contains(t, string(data), "[email protected]") |
| 206 | |
| 207 | wh := c.handle(sshFxpOpen, "/f", uint32(sshFxfWrite|sshFxfCreat), uint32(0)) |
| 208 | c.send(sshFxpExtended, "[email protected]", wh) |
| 209 | assert.Equal(t, uint32(sshFxOk), c.status()) |
| 210 | assert.Equal(t, int32(1), h.n.Load()) |
| 211 | |
| 212 | dh := c.handle(sshFxpOpendir, "/") |
| 213 | c.send(sshFxpExtended, "[email protected]", dh) |
| 214 | assert.Equal(t, uint32(sshFxOPUnsupported), c.status()) |
| 215 | c.send(sshFxpExtended, "[email protected]", "nope") |
| 216 | assert.NotEqual(t, uint32(sshFxOk), c.status()) |
| 217 | } |
| 218 | |
| 219 | |
| 220 | func TestRequestServerOrderIDWrap(t *testing.T) { |
| 221 | dir := t.TempDir() |
| 222 | require.NoError(t, os.WriteFile(filepath.Join(dir, "slow"), nil, 0o644)) |
| 223 | h := &osHandlers{dir: dir} |
| 224 | a, b := net.Pipe() |
| 225 | rs := NewRequestServer(a, Handlers{h, h, h, h}) |
| 226 | rs.pktMgr.packetCount = math.MaxUint32 - 1 |
| 227 | go rs.Serve() |
| 228 | t.Cleanup(func() { b.Close() }) |
| 229 | c := &rawClient{t: t, c: b, rs: rs} |
| 230 | |
| 231 | id1 := c.send(sshFxpStat, "/slow") |
| 232 | id2 := c.send(sshFxpStat, "/") |
| 233 | _, d1 := c.recv() |
| 234 | _, d2 := c.recv() |
| 235 | g1, _ := unmarshalUint32(d1) |
| 236 | g2, _ := unmarshalUint32(d2) |
| 237 | assert.Equal(t, []uint32{id1, id2}, []uint32{g1, g2}) |
| 238 | } |
| 239 | |
| 240 | func TestStatusFromErrorErrno(t *testing.T) { |
| 241 | for _, tc := range []struct { |
| 242 | err error |
| 243 | code uint32 |
| 244 | }{ |
| 245 | {&os.LinkError{Op: "rename", Old: "a", New: "b", Err: syscall.EACCES}, sshFxPermissionDenied}, |
| 246 | {&os.SyscallError{Syscall: "x", Err: syscall.EPERM}, sshFxPermissionDenied}, |
| 247 | {&os.PathError{Op: "open", Path: "x", Err: syscall.ENOTDIR}, sshFxNoSuchFile}, |
| 248 | {&os.PathError{Op: "open", Path: "x", Err: syscall.ELOOP}, sshFxNoSuchFile}, |
| 249 | {&os.PathError{Op: "open", Path: "x", Err: syscall.ENAMETOOLONG}, sshFxBadMessage}, |
| 250 | {syscall.EINVAL, sshFxBadMessage}, |
| 251 | {syscall.ENOSYS, sshFxOPUnsupported}, |
| 252 | {syscall.EEXIST, sshFxFailure}, |
| 253 | } { |
| 254 | assert.Equal(t, tc.code, statusFromError(1, tc.err).Code, "%v", tc.err) |
| 255 | } |
| 256 | } |