aboutsummaryrefslogtreecommitdiff
path: root/hotline/file_path_test.go
blob: 9fe82f5dfd5ac125e2c6073d2cecdddcca0cee8c (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package hotline

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/charmap"
)

func TestFilePath_Write(t *testing.T) {
	type args struct {
		b []byte
	}
	tests := []struct {
		name    string
		args    args
		want    FilePath
		wantErr bool
	}{
		{
			name: "unmarshals bytes into struct",
			args: args{b: []byte{
				0x00, 0x02,
				0x00, 0x00,
				0x0f,
				0x46, 0x69, 0x72, 0x73, 0x74, 0x20, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x20, 0x44, 0x69, 0x72,
				0x00, 0x00,
				0x08,
				0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72,
			}},
			want: FilePath{
				ItemCount: [2]byte{0x00, 0x02},
				Items: []FilePathItem{
					{
						Len:  0x0f,
						Name: []byte("First Level Dir"),
					},
					{
						Len:  0x08,
						Name: []byte("A SubDir"),
					},
				},
			},
			wantErr: false,
		},
		{
			name: "handles empty data payload",
			args: args{b: []byte{
				0x00, 0x00,
			}},
			want: FilePath{
				ItemCount: [2]byte{0x00, 0x00},
				Items:     []FilePathItem(nil),
			},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var fp FilePath
			if _, err := fp.Write(tt.args.b); (err != nil) != tt.wantErr {
				t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr)
			}
			if !assert.Equal(t, tt.want, fp) {
				t.Errorf("Read() got = %v, want %v", fp, tt.want)
			}
		})
	}
}

func Test_readPath(t *testing.T) {
	type args struct {
		fileRoot string
		filePath []byte
		fileName []byte
	}
	tests := []struct {
		name    string
		args    args
		want    string
		wantErr bool
	}{
		{
			name: "when filePath is invalid",
			args: args{
				fileRoot: "/usr/local/var/mobius/Files",
				filePath: []byte{
					0x61,
				},
				fileName: []byte{
					0x61, 0x61, 0x61,
				},
			},
			want:    "",
			wantErr: true,
		},
		{
			name: "when filePath is nil",
			args: args{
				fileRoot: "/usr/local/var/mobius/Files",
				filePath: nil,
				fileName: []byte("foo"),
			},
			want: "/usr/local/var/mobius/Files/foo",
		},
		{
			name: "when fileName contains .. ",
			args: args{
				fileRoot: "/usr/local/var/mobius/Files",
				filePath: nil,
				fileName: []byte("../../../foo"),
			},
			want: "/usr/local/var/mobius/Files/foo",
		},
		{
			name: "when filePath contains .. ",
			args: args{
				fileRoot: "/usr/local/var/mobius/Files",
				filePath: []byte{
					0x00, 0x02,
					0x00, 0x00,
					0x03,
					0x2e, 0x2e, 0x2f,
					0x00, 0x00,
					0x08,
					0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72,
				},
				fileName: []byte("foo"),
			},
			want: "/usr/local/var/mobius/Files/A SubDir/foo",
		},
		{
			name: "when a filePath entry contains .. ",
			args: args{
				fileRoot: "/usr/local/var/mobius/Files",
				filePath: []byte{
					0x00, 0x01,
					0x00, 0x00,
					0x0b,
					0x2e, 0x2e, 0x2f, 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72,
				},
				fileName: []byte("foo"),
			},
			want: "/usr/local/var/mobius/Files/A SubDir/foo",
		},
		{
			name: "when filePath and fileName are nil",
			args: args{
				fileRoot: "/usr/local/var/mobius/Files",
				filePath: nil,
				fileName: nil,
			},
			want: "/usr/local/var/mobius/Files",
		},
		{
			name: "when fileRoot contains non-ASCII UTF-8 characters",
			args: args{
				fileRoot: "/files/español",
				filePath: nil,
				fileName: []byte("foo"),
			},
			want: "/files/español/foo",
		},
		{
			name: "when fileRoot contains non-ASCII and filePath has Mac Roman bytes",
			args: args{
				fileRoot: "/files/español",
				filePath: []byte{
					0x00, 0x01,
					0x00, 0x00,
					0x06,
					0x63, 0x61, 0x66, 0x8e, 0x73, 0x21, // "caf\x8es!" where 0x8e is Mac Roman é
				},
				fileName: []byte("foo"),
			},
			want: "/files/español/cafés!/foo",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := ReadPath(tt.args.fileRoot, tt.args.filePath, tt.args.fileName, charmap.Macintosh.NewDecoder())
			if (err != nil) != tt.wantErr {
				t.Errorf("ReadPath() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("ReadPath() got = %v, want %v", got, tt.want)
			}
		})
	}
}

func Test_readPath_NopDecoder(t *testing.T) {
	type args struct {
		fileRoot string
		filePath []byte
		fileName []byte
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		{
			name: "Mac Roman bytes pass through unchanged with Nop decoder",
			args: args{
				fileRoot: "/files",
				filePath: []byte{
					0x00, 0x01,
					0x00, 0x00,
					0x06,
					0x63, 0x61, 0x66, 0x8e, 0x73, 0x21, // "caf\x8es!" where 0x8e is Mac Roman é
				},
				fileName: []byte("foo"),
			},
			// With Nop decoder, 0x8e is NOT converted to UTF-8 é — it passes through as the raw byte.
			want: "/files/caf\x8es!/foo",
		},
		{
			name: "plain ASCII is unchanged with Nop decoder",
			args: args{
				fileRoot: "/files",
				filePath: nil,
				fileName: []byte("hello.txt"),
			},
			want: "/files/hello.txt",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := ReadPath(tt.args.fileRoot, tt.args.filePath, tt.args.fileName, encoding.Nop.NewDecoder())
			if err != nil {
				t.Errorf("ReadPath() error = %v", err)
				return
			}
			if got != tt.want {
				t.Errorf("ReadPath() got = %q, want %q", got, tt.want)
			}
		})
	}
}

func Test_fileItemScanner(t *testing.T) {
	type args struct {
		data []byte
		in1  bool
	}
	tests := []struct {
		name        string
		args        args
		wantAdvance int
		wantToken   []byte
		wantErr     assert.ErrorAssertionFunc
	}{
		{
			name: "when a full fileItem is provided",
			args: args{
				data: []byte{
					0, 0,
					0x09,
					0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72,
				},
				in1: false,
			},
			wantAdvance: 12,
			wantToken: []byte{
				0, 0,
				0x09,
				0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72,
			},
			wantErr: assert.NoError,
		},
		{
			name: "when a full fileItem with extra bytes is provided",
			args: args{
				data: []byte{
					0, 0,
					0x09,
					0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72,
					1, 1, 1, 1, 1, 1,
				},
				in1: false,
			},
			wantAdvance: 12,
			wantToken: []byte{
				0, 0,
				0x09,
				0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72,
			},
			wantErr: assert.NoError,
		},
		{
			name: "when insufficient bytes are provided",
			args: args{
				data: []byte{
					0, 0,
				},
				in1: false,
			},
			wantAdvance: 0,
			wantToken:   []byte(nil),
			wantErr:     assert.NoError,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			gotAdvance, gotToken, err := fileItemScanner(tt.args.data, tt.args.in1)
			if !tt.wantErr(t, err, fmt.Sprintf("fileItemScanner(%v, %v)", tt.args.data, tt.args.in1)) {
				return
			}
			assert.Equalf(t, tt.wantAdvance, gotAdvance, "fileItemScanner(%v, %v)", tt.args.data, tt.args.in1)
			assert.Equalf(t, tt.wantToken, gotToken, "fileItemScanner(%v, %v)", tt.args.data, tt.args.in1)
		})
	}
}