]> git.r.bdr.sh - rbdr/mobius/blob - hotline/file_path_test.go
4c2f6db80c855e1741595ab5e1210b358afda4b4
[rbdr/mobius] / hotline / file_path_test.go
1 package hotline
2
3 import (
4 "github.com/stretchr/testify/assert"
5 "testing"
6 )
7
8 func TestFilePath_UnmarshalBinary(t *testing.T) {
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{
30 ItemCount: [2]byte{0x00, 0x02},
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 },
44 }
45 for _, tt := range tests {
46 t.Run(tt.name, func(t *testing.T) {
47 var fp FilePath
48 if err := fp.UnmarshalBinary(tt.args.b); (err != nil) != tt.wantErr {
49 t.Errorf("UnmarshalBinary() error = %v, wantErr %v", err, tt.wantErr)
50 }
51 if !assert.Equal(t, tt.want, fp) {
52 t.Errorf("Read() got = %v, want %v", fp, tt.want)
53 }
54 })
55 }
56 }
57
58 func Test_readPath(t *testing.T) {
59 type args struct {
60 fileRoot string
61 filePath []byte
62 fileName []byte
63 }
64 tests := []struct {
65 name string
66 args args
67 want string
68 wantErr bool
69 }{
70 {
71 name: "when filePath is invalid",
72 args: args{
73 fileRoot: "/usr/local/var/mobius/Files",
74 filePath: []byte{
75 0x61,
76 },
77 fileName: []byte{
78 0x61, 0x61, 0x61,
79 },
80 },
81 want: "",
82 wantErr: true,
83 },
84 {
85 name: "when filePath is nil",
86 args: args{
87 fileRoot: "/usr/local/var/mobius/Files",
88 filePath: nil,
89 fileName: []byte("foo"),
90
91 },
92 want: "/usr/local/var/mobius/Files/foo",
93 },
94 {
95 name: "when fileName contains .. ",
96 args: args{
97 fileRoot: "/usr/local/var/mobius/Files",
98 filePath: nil,
99 fileName: []byte("../../../foo"),
100 },
101 want: "/usr/local/var/mobius/Files/foo",
102 },
103 {
104 name: "when filePath contains .. ",
105 args: args{
106 fileRoot: "/usr/local/var/mobius/Files",
107 filePath: []byte{
108 0x00, 0x02,
109 0x00, 0x00,
110 0x03,
111 0x2e, 0x2e, 0x2f,
112 0x00, 0x00,
113 0x08,
114 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72,
115 },
116 fileName: []byte("foo"),
117 },
118 want: "/usr/local/var/mobius/Files/A SubDir/foo",
119 },
120 {
121 name: "when a filePath entry contains .. ",
122 args: args{
123 fileRoot: "/usr/local/var/mobius/Files",
124 filePath: []byte{
125 0x00, 0x01,
126 0x00, 0x00,
127 0x0b,
128 0x2e, 0x2e, 0x2f, 0x41, 0x20, 0x53, 0x75, 0x62, 0x44, 0x69, 0x72,
129 },
130 fileName: []byte("foo"),
131 },
132 want: "/usr/local/var/mobius/Files/A SubDir/foo",
133 },
134 {
135 name: "when filePath and fileName are nil",
136 args: args{
137 fileRoot: "/usr/local/var/mobius/Files",
138 filePath: nil,
139 fileName: nil,
140 },
141 want: "/usr/local/var/mobius/Files",
142 },
143 }
144 for _, tt := range tests {
145 t.Run(tt.name, func(t *testing.T) {
146 got, err := readPath(tt.args.fileRoot, tt.args.filePath, tt.args.fileName)
147 if (err != nil) != tt.wantErr {
148 t.Errorf("readPath() error = %v, wantErr %v", err, tt.wantErr)
149 return
150 }
151 if got != tt.want {
152 t.Errorf("readPath() got = %v, want %v", got, tt.want)
153 }
154 })
155 }
156 }