]>
Commit | Line | Data |
---|---|---|
72dd37f1 JH |
1 | package hotline |
2 | ||
3 | import ( | |
7e6b49e4 | 4 | "fmt" |
72dd37f1 JH |
5 | "github.com/stretchr/testify/assert" |
6 | "testing" | |
7 | ) | |
8 | ||
8fc43f8e | 9 | func TestFilePath_Write(t *testing.T) { |
72dd37f1 JH |
10 | type args struct { |
11 | b []byte | |
12 | } | |
13 | tests := []struct { | |
14 | name string | |
15 | args args | |
16 | want FilePath | |
17 | wantErr bool | |
18 | }{ | |
19 | { | |
20 | name: "unmarshals bytes into struct", | |
21 | args: args{b: []byte{ | |
22 | 0x00, 0x02, | |
23 | 0x00, 0x00, | |
24 | 0x0f, | |
25 | 0x46, 0x69, 0x72, 0x73, 0x74, 0x20, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x20, 0x44, 0x69, 0x72, | |
26 | 0x00, 0x00, | |
27 | 0x08, | |
28 | 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72, | |
29 | }}, | |
30 | want: FilePath{ | |
00d1ef67 | 31 | ItemCount: [2]byte{0x00, 0x02}, |
72dd37f1 JH |
32 | Items: []FilePathItem{ |
33 | { | |
34 | Len: 0x0f, | |
35 | Name: []byte("First Level Dir"), | |
36 | }, | |
37 | { | |
38 | Len: 0x08, | |
39 | Name: []byte("A SubDir"), | |
40 | }, | |
41 | }, | |
42 | }, | |
43 | wantErr: false, | |
44 | }, | |
050407a3 JH |
45 | { |
46 | name: "handles empty data payload", | |
47 | args: args{b: []byte{ | |
48 | 0x00, 0x00, | |
49 | }}, | |
50 | want: FilePath{ | |
51 | ItemCount: [2]byte{0x00, 0x00}, | |
52 | Items: []FilePathItem(nil), | |
53 | }, | |
54 | wantErr: false, | |
55 | }, | |
72dd37f1 JH |
56 | } |
57 | for _, tt := range tests { | |
58 | t.Run(tt.name, func(t *testing.T) { | |
59 | var fp FilePath | |
8fc43f8e JH |
60 | if _, err := fp.Write(tt.args.b); (err != nil) != tt.wantErr { |
61 | t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr) | |
72dd37f1 JH |
62 | } |
63 | if !assert.Equal(t, tt.want, fp) { | |
64 | t.Errorf("Read() got = %v, want %v", fp, tt.want) | |
65 | } | |
66 | }) | |
67 | } | |
68 | } | |
92a7e455 JH |
69 | |
70 | func Test_readPath(t *testing.T) { | |
71 | type args struct { | |
72 | fileRoot string | |
73 | filePath []byte | |
74 | fileName []byte | |
75 | } | |
76 | tests := []struct { | |
77 | name string | |
78 | args args | |
79 | want string | |
80 | wantErr bool | |
81 | }{ | |
82 | { | |
83 | name: "when filePath is invalid", | |
84 | args: args{ | |
85 | fileRoot: "/usr/local/var/mobius/Files", | |
86 | filePath: []byte{ | |
87 | 0x61, | |
88 | }, | |
89 | fileName: []byte{ | |
90 | 0x61, 0x61, 0x61, | |
91 | }, | |
92 | }, | |
050407a3 | 93 | want: "", |
92a7e455 JH |
94 | wantErr: true, |
95 | }, | |
96 | { | |
97 | name: "when filePath is nil", | |
98 | args: args{ | |
99 | fileRoot: "/usr/local/var/mobius/Files", | |
100 | filePath: nil, | |
101 | fileName: []byte("foo"), | |
92a7e455 JH |
102 | }, |
103 | want: "/usr/local/var/mobius/Files/foo", | |
104 | }, | |
105 | { | |
106 | name: "when fileName contains .. ", | |
107 | args: args{ | |
108 | fileRoot: "/usr/local/var/mobius/Files", | |
109 | filePath: nil, | |
110 | fileName: []byte("../../../foo"), | |
111 | }, | |
112 | want: "/usr/local/var/mobius/Files/foo", | |
113 | }, | |
114 | { | |
115 | name: "when filePath contains .. ", | |
116 | args: args{ | |
117 | fileRoot: "/usr/local/var/mobius/Files", | |
118 | filePath: []byte{ | |
119 | 0x00, 0x02, | |
120 | 0x00, 0x00, | |
121 | 0x03, | |
122 | 0x2e, 0x2e, 0x2f, | |
123 | 0x00, 0x00, | |
124 | 0x08, | |
125 | 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72, | |
126 | }, | |
127 | fileName: []byte("foo"), | |
128 | }, | |
129 | want: "/usr/local/var/mobius/Files/A SubDir/foo", | |
130 | }, | |
131 | { | |
132 | name: "when a filePath entry contains .. ", | |
133 | args: args{ | |
134 | fileRoot: "/usr/local/var/mobius/Files", | |
135 | filePath: []byte{ | |
136 | 0x00, 0x01, | |
137 | 0x00, 0x00, | |
138 | 0x0b, | |
139 | 0x2e, 0x2e, 0x2f, 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72, | |
140 | }, | |
141 | fileName: []byte("foo"), | |
142 | }, | |
143 | want: "/usr/local/var/mobius/Files/A SubDir/foo", | |
144 | }, | |
145 | { | |
146 | name: "when filePath and fileName are nil", | |
147 | args: args{ | |
148 | fileRoot: "/usr/local/var/mobius/Files", | |
149 | filePath: nil, | |
150 | fileName: nil, | |
151 | }, | |
152 | want: "/usr/local/var/mobius/Files", | |
153 | }, | |
154 | } | |
155 | for _, tt := range tests { | |
156 | t.Run(tt.name, func(t *testing.T) { | |
fd740bc4 | 157 | got, err := ReadPath(tt.args.fileRoot, tt.args.filePath, tt.args.fileName) |
92a7e455 | 158 | if (err != nil) != tt.wantErr { |
fd740bc4 | 159 | t.Errorf("ReadPath() error = %v, wantErr %v", err, tt.wantErr) |
92a7e455 JH |
160 | return |
161 | } | |
162 | if got != tt.want { | |
fd740bc4 | 163 | t.Errorf("ReadPath() got = %v, want %v", got, tt.want) |
92a7e455 JH |
164 | } |
165 | }) | |
166 | } | |
050407a3 | 167 | } |
7e6b49e4 JH |
168 | |
169 | func Test_fileItemScanner(t *testing.T) { | |
170 | type args struct { | |
171 | data []byte | |
172 | in1 bool | |
173 | } | |
174 | tests := []struct { | |
175 | name string | |
176 | args args | |
177 | wantAdvance int | |
178 | wantToken []byte | |
179 | wantErr assert.ErrorAssertionFunc | |
180 | }{ | |
181 | { | |
182 | name: "when a full fileItem is provided", | |
183 | args: args{ | |
184 | data: []byte{ | |
185 | 0, 0, | |
186 | 0x09, | |
187 | 0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, | |
188 | }, | |
189 | in1: false, | |
190 | }, | |
191 | wantAdvance: 12, | |
192 | wantToken: []byte{ | |
193 | 0, 0, | |
194 | 0x09, | |
195 | 0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, | |
196 | }, | |
197 | wantErr: assert.NoError, | |
198 | }, | |
199 | { | |
200 | name: "when a full fileItem with extra bytes is provided", | |
201 | args: args{ | |
202 | data: []byte{ | |
203 | 0, 0, | |
204 | 0x09, | |
205 | 0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, | |
206 | 1, 1, 1, 1, 1, 1, | |
207 | }, | |
208 | in1: false, | |
209 | }, | |
210 | wantAdvance: 12, | |
211 | wantToken: []byte{ | |
212 | 0, 0, | |
213 | 0x09, | |
214 | 0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, | |
215 | }, | |
216 | wantErr: assert.NoError, | |
217 | }, | |
218 | { | |
219 | name: "when insufficient bytes are provided", | |
220 | args: args{ | |
221 | data: []byte{ | |
222 | 0, 0, | |
223 | }, | |
224 | in1: false, | |
225 | }, | |
226 | wantAdvance: 0, | |
227 | wantToken: []byte(nil), | |
228 | wantErr: assert.NoError, | |
229 | }, | |
230 | } | |
231 | for _, tt := range tests { | |
232 | t.Run(tt.name, func(t *testing.T) { | |
233 | gotAdvance, gotToken, err := fileItemScanner(tt.args.data, tt.args.in1) | |
234 | if !tt.wantErr(t, err, fmt.Sprintf("fileItemScanner(%v, %v)", tt.args.data, tt.args.in1)) { | |
235 | return | |
236 | } | |
237 | assert.Equalf(t, tt.wantAdvance, gotAdvance, "fileItemScanner(%v, %v)", tt.args.data, tt.args.in1) | |
238 | assert.Equalf(t, tt.wantToken, gotToken, "fileItemScanner(%v, %v)", tt.args.data, tt.args.in1) | |
239 | }) | |
240 | } | |
241 | } |