跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

ls_formatting_test.go

179 行 · 5.2 KB历史下载
1package sftp
2
3import (
4 "os"
5 "regexp"
6 "runtime"
7 "strings"
8 "testing"
9 "time"
10)
11
12const (
13 typeDirectory = "d"
14 typeFile = "[^d]"
15)
16
17func TestRunLsWithExamplesDirectory(t *testing.T) {
18 path := "examples"
19 item, _ := os.Stat(path)
20 result := runLs(nil, item)
21 runLsTestHelper(t, result, typeDirectory, path)
22}
23
24func TestRunLsWithSourceFile(t *testing.T) {
25 path := "ls_formatting_test.go"
26 item, _ := os.Stat(path)
27 result := runLs(nil, item)
28 runLsTestHelper(t, result, typeFile, path)
29}
30
31func TestRunLsWithExamplesDirectoryWithOSLookup(t *testing.T) {
32 path := "examples"
33 item, _ := os.Stat(path)
34 result := runLs(osIDLookup{}, item)
35 runLsTestHelper(t, result, typeDirectory, path)
36}
37
38func TestRunLsWithSourceFileWithOSLookup(t *testing.T) {
39 path := "ls_formatting_test.go"
40 item, _ := os.Stat(path)
41 result := runLs(osIDLookup{}, item)
42 runLsTestHelper(t, result, typeFile, path)
43}
44
45/*
46The format of the `longname' field is unspecified by this protocol.
47It MUST be suitable for use in the output of a directory listing
48command (in fact, the recommended operation for a directory listing
49command is to simply display this data). However, clients SHOULD NOT
50attempt to parse the longname field for file attributes; they SHOULD
51use the attrs field instead.
52
53 The recommended format for the longname field is as follows:
54
55 -rwxr-xr-x 1 mjos staff 348911 Mar 25 14:29 t-filexfer
56 1234567890 123 12345678 12345678 12345678 123456789012
57
58Here, the first line is sample output, and the second field indicates
59widths of the various fields. Fields are separated by spaces. The
60first field lists file permissions for user, group, and others; the
61second field is link count; the third field is the name of the user
62who owns the file; the fourth field is the name of the group that
63owns the file; the fifth field is the size of the file in bytes; the
64sixth field (which actually may contain spaces, but is fixed to 12
65characters) is the file modification time, and the seventh field is
66the file name. Each field is specified to be a minimum of certain
67number of character positions (indicated by the second line above),
68but may also be longer if the data does not fit in the specified
69length.
70
71 The SSH_FXP_ATTRS response has the following format:
72
73 uint32 id
74 ATTRS attrs
75
76where `id' is the request identifier, and `attrs' is the returned
77file attributes as described in Section “File Attributes”.
78
79N.B.: FileZilla does parse this ls formatting, and so not rendering it
80on any particular GOOS/GOARCH can cause compatibility issues with this client.
81*/
82func runLsTestHelper(t *testing.T, result, expectedType, path string) {
83 // using regular expressions to make tests work on all systems
84 // a virtual file system (like afero) would be needed to mock valid filesystem checks
85 // expected layout is:
86 // drwxr-xr-x 8 501 20 272 Aug 9 19:46 examples
87
88 t.Log(result)
89
90 sparce := strings.Split(result, " ")
91
92 var fields []string
93 for _, field := range sparce {
94 if field == "" {
95 continue
96 }
97
98 fields = append(fields, field)
99 }
100
101 perms, linkCnt, user, group, size := fields[0], fields[1], fields[2], fields[3], fields[4]
102 dateTime := strings.Join(fields[5:8], " ")
103 filename := fields[8]
104
105 if runtime.GOOS == "zos" {
106 // User and Group are always only uppercase characters on z/OS
107 user = strings.ToLower(user)
108 group = strings.ToLower(group)
109 }
110
111 // permissions (len 10, "drwxr-xr-x")
112 const (
113 rwxs = "[-r][-w][-xsS]"
114 rwxt = "[-r][-w][-xtT]"
115 )
116 if ok, err := regexp.MatchString("^"+expectedType+rwxs+rwxs+rwxt+"$", perms); !ok {
117 if err != nil {
118 t.Fatal("unexpected error:", err)
119 }
120
121 t.Errorf("runLs(%q): permission field mismatch, expected dir, got: %#v, err: %#v", path, perms, err)
122 }
123
124 // link count (len 3, number)
125 const (
126 number = "(?:[0-9]+)"
127 )
128 if ok, err := regexp.MatchString("^"+number+"$", linkCnt); !ok {
129 if err != nil {
130 t.Fatal("unexpected error:", err)
131 }
132
133 t.Errorf("runLs(%q): link count field mismatch, got: %#v, err: %#v", path, linkCnt, err)
134 }
135
136 // username / uid (len 8, number or string)
137 const (
138 name = "(?:[A-Za-z_][-A-Z.a-z0-9_]*)"
139 )
140 if ok, err := regexp.MatchString("^(?:"+number+"|"+name+")+$", user); !ok {
141 if err != nil {
142 t.Fatal("unexpected error:", err)
143 }
144
145 t.Errorf("runLs(%q): username / uid mismatch, expected user, got: %#v, err: %#v", path, user, err)
146 }
147
148 // groupname / gid (len 8, number or string)
149 if ok, err := regexp.MatchString("^(?:"+number+"|"+name+")+$", group); !ok {
150 if err != nil {
151 t.Fatal("unexpected error:", err)
152 }
153
154 t.Errorf("runLs(%q): groupname / gid mismatch, expected group, got: %#v, err: %#v", path, group, err)
155 }
156
157 // filesize (len 8)
158 if ok, err := regexp.MatchString("^"+number+"$", size); !ok {
159 if err != nil {
160 t.Fatal("unexpected error:", err)
161 }
162
163 t.Errorf("runLs(%q): filesize field mismatch, expected size in bytes, got: %#v, err: %#v", path, size, err)
164 }
165
166 // mod time (len 12, e.g. Aug 9 19:46)
167 _, err := time.Parse("Jan 2 15:04", dateTime)
168 if err != nil {
169 _, err = time.Parse("Jan 2 2006", dateTime)
170 if err != nil {
171 t.Errorf("runLs.dateTime = %#v should match `Jan 2 15:04` or `Jan 2 2006`: %+v", dateTime, err)
172 }
173 }
174
175 // filename
176 if path != filename {
177 t.Errorf("runLs.filename = %#v, expected: %#v", filename, path)
178 }
179}