]>
Commit | Line | Data |
---|---|---|
72dd37f1 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "github.com/stretchr/testify/assert" | |
5 | "testing" | |
6 | ) | |
7 | ||
8 | func TestFilePath_UnmarshalBinary(t *testing.T) { | |
72dd37f1 JH |
9 | type args struct { |
10 | b []byte | |
11 | } | |
12 | tests := []struct { | |
13 | name string | |
14 | args args | |
15 | want FilePath | |
16 | wantErr bool | |
17 | }{ | |
18 | { | |
19 | name: "unmarshals bytes into struct", | |
20 | args: args{b: []byte{ | |
21 | 0x00, 0x02, | |
22 | 0x00, 0x00, | |
23 | 0x0f, | |
24 | 0x46, 0x69, 0x72, 0x73, 0x74, 0x20, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x20, 0x44, 0x69, 0x72, | |
25 | 0x00, 0x00, | |
26 | 0x08, | |
27 | 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72, | |
28 | }}, | |
29 | want: FilePath{ | |
00d1ef67 | 30 | ItemCount: [2]byte{0x00, 0x02}, |
72dd37f1 JH |
31 | Items: []FilePathItem{ |
32 | { | |
33 | Len: 0x0f, | |
34 | Name: []byte("First Level Dir"), | |
35 | }, | |
36 | { | |
37 | Len: 0x08, | |
38 | Name: []byte("A SubDir"), | |
39 | }, | |
40 | }, | |
41 | }, | |
42 | wantErr: false, | |
43 | }, | |
050407a3 JH |
44 | { |
45 | name: "handles empty data payload", | |
46 | args: args{b: []byte{ | |
47 | 0x00, 0x00, | |
48 | }}, | |
49 | want: FilePath{ | |
50 | ItemCount: [2]byte{0x00, 0x00}, | |
51 | Items: []FilePathItem(nil), | |
52 | }, | |
53 | wantErr: false, | |
54 | }, | |
72dd37f1 JH |
55 | } |
56 | for _, tt := range tests { | |
57 | t.Run(tt.name, func(t *testing.T) { | |
58 | var fp FilePath | |
59 | if err := fp.UnmarshalBinary(tt.args.b); (err != nil) != tt.wantErr { | |
60 | t.Errorf("UnmarshalBinary() error = %v, wantErr %v", err, tt.wantErr) | |
61 | } | |
62 | if !assert.Equal(t, tt.want, fp) { | |
63 | t.Errorf("Read() got = %v, want %v", fp, tt.want) | |
64 | } | |
65 | }) | |
66 | } | |
67 | } | |
92a7e455 JH |
68 | |
69 | func Test_readPath(t *testing.T) { | |
70 | type args struct { | |
71 | fileRoot string | |
72 | filePath []byte | |
73 | fileName []byte | |
74 | } | |
75 | tests := []struct { | |
76 | name string | |
77 | args args | |
78 | want string | |
79 | wantErr bool | |
80 | }{ | |
81 | { | |
82 | name: "when filePath is invalid", | |
83 | args: args{ | |
84 | fileRoot: "/usr/local/var/mobius/Files", | |
85 | filePath: []byte{ | |
86 | 0x61, | |
87 | }, | |
88 | fileName: []byte{ | |
89 | 0x61, 0x61, 0x61, | |
90 | }, | |
91 | }, | |
050407a3 | 92 | want: "", |
92a7e455 JH |
93 | wantErr: true, |
94 | }, | |
95 | { | |
96 | name: "when filePath is nil", | |
97 | args: args{ | |
98 | fileRoot: "/usr/local/var/mobius/Files", | |
99 | filePath: nil, | |
100 | fileName: []byte("foo"), | |
92a7e455 JH |
101 | }, |
102 | want: "/usr/local/var/mobius/Files/foo", | |
103 | }, | |
104 | { | |
105 | name: "when fileName contains .. ", | |
106 | args: args{ | |
107 | fileRoot: "/usr/local/var/mobius/Files", | |
108 | filePath: nil, | |
109 | fileName: []byte("../../../foo"), | |
110 | }, | |
111 | want: "/usr/local/var/mobius/Files/foo", | |
112 | }, | |
113 | { | |
114 | name: "when filePath contains .. ", | |
115 | args: args{ | |
116 | fileRoot: "/usr/local/var/mobius/Files", | |
117 | filePath: []byte{ | |
118 | 0x00, 0x02, | |
119 | 0x00, 0x00, | |
120 | 0x03, | |
121 | 0x2e, 0x2e, 0x2f, | |
122 | 0x00, 0x00, | |
123 | 0x08, | |
124 | 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72, | |
125 | }, | |
126 | fileName: []byte("foo"), | |
127 | }, | |
128 | want: "/usr/local/var/mobius/Files/A SubDir/foo", | |
129 | }, | |
130 | { | |
131 | name: "when a filePath entry contains .. ", | |
132 | args: args{ | |
133 | fileRoot: "/usr/local/var/mobius/Files", | |
134 | filePath: []byte{ | |
135 | 0x00, 0x01, | |
136 | 0x00, 0x00, | |
137 | 0x0b, | |
138 | 0x2e, 0x2e, 0x2f, 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72, | |
139 | }, | |
140 | fileName: []byte("foo"), | |
141 | }, | |
142 | want: "/usr/local/var/mobius/Files/A SubDir/foo", | |
143 | }, | |
144 | { | |
145 | name: "when filePath and fileName are nil", | |
146 | args: args{ | |
147 | fileRoot: "/usr/local/var/mobius/Files", | |
148 | filePath: nil, | |
149 | fileName: nil, | |
150 | }, | |
151 | want: "/usr/local/var/mobius/Files", | |
152 | }, | |
153 | } | |
154 | for _, tt := range tests { | |
155 | t.Run(tt.name, func(t *testing.T) { | |
156 | got, err := readPath(tt.args.fileRoot, tt.args.filePath, tt.args.fileName) | |
157 | if (err != nil) != tt.wantErr { | |
158 | t.Errorf("readPath() error = %v, wantErr %v", err, tt.wantErr) | |
159 | return | |
160 | } | |
161 | if got != tt.want { | |
162 | t.Errorf("readPath() got = %v, want %v", got, tt.want) | |
163 | } | |
164 | }) | |
165 | } | |
050407a3 | 166 | } |