]> git.r.bdr.sh - rbdr/mobius/blame - hotline/files_test.go
Limit guest permissions
[rbdr/mobius] / hotline / files_test.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
4 "bytes"
a2ef262a 5 "encoding/binary"
50c837fe 6 "github.com/stretchr/testify/assert"
6988a057 7 "os"
a2ef262a 8 "path/filepath"
6988a057
JH
9 "testing"
10)
11
12func TestEncodeFilePath(t *testing.T) {
13 var tests = []struct {
14 filePath string
15 want []byte
16 }{
17 {
18 filePath: "kitten1.jpg",
19 want: []byte{
20 0x00, 0x01, // number of items in path
21 0x00, 0x00, // leading path separator
22 0x0b, // length of next path section (11)
23 0x6b, 0x69, 0x74, 0x74, 0x65, 0x6e, 0x31, 0x2e, 0x6a, 0x70, 0x67, // kitten1.jpg
24 },
25 },
26 {
27 filePath: "foo/kitten1.jpg",
28 want: []byte{
29 0x00, 0x02, // number of items in path
30 0x00, 0x00,
31 0x03,
32 0x66, 0x6f, 0x6f,
33 0x00, 0x00, // leading path separator
34 0x0b, // length of next path section (11)
35 0x6b, 0x69, 0x74, 0x74, 0x65, 0x6e, 0x31, 0x2e, 0x6a, 0x70, 0x67, // kitten1.jpg
36 },
37 },
38 }
39
40 for _, test := range tests {
41 got := EncodeFilePath(test.filePath)
42 if !bytes.Equal(got, test.want) {
43 t.Errorf("field mismatch: want: %#v got: %#v", test.want, got)
44 }
45 }
46}
47
48func TestCalcTotalSize(t *testing.T) {
49 cwd, _ := os.Getwd()
5c34f875 50 defer func() { _ = os.Chdir(cwd) }()
6988a057
JH
51
52 _ = os.Chdir("test/config/Files")
53
54 type args struct {
55 filePath string
56 }
57 tests := []struct {
58 name string
59 args args
60 want []byte
61 wantErr bool
62 }{
63 {
64 name: "Foo",
65 args: args{
66 filePath: "test",
67 },
68 want: []byte{0x00, 0x00, 0x18, 0x00},
69 wantErr: false,
70 },
71 // TODO: Add more test cases.
72 }
73 for _, tt := range tests {
74 t.Run(tt.name, func(t *testing.T) {
75 got, err := CalcTotalSize(tt.args.filePath)
76 if (err != nil) != tt.wantErr {
77 t.Errorf("CalcTotalSize() error = %v, wantErr %v", err, tt.wantErr)
78 return
79 }
50c837fe 80 if !assert.Equal(t, tt.want, got) {
6988a057
JH
81 t.Errorf("CalcTotalSize() got = %v, want %v", got, tt.want)
82 }
83 })
84 }
85}
a2ef262a
JH
86
87func createTestDirStructure(baseDir string, structure map[string]string) error {
88 // First pass: create directories
89 for path, content := range structure {
90 if content == "dir" {
91 if err := os.MkdirAll(filepath.Join(baseDir, path), 0755); err != nil {
92 return err
93 }
94 }
95 }
96
97 // Second pass: create files
98 for path, content := range structure {
99 if content != "dir" {
100 fullPath := filepath.Join(baseDir, path)
101 dir := filepath.Dir(fullPath)
102 if err := os.MkdirAll(dir, 0755); err != nil {
103 return err
104 }
105 if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
106 return err
107 }
108 }
109 }
110 return nil
111}
112
113func TestCalcItemCount(t *testing.T) {
114 tests := []struct {
115 name string
116 structure map[string]string
117 expected uint16
118 }{
119 {
120 name: "directory with files",
121 structure: map[string]string{
122 "file1.txt": "content1",
123 "file2.txt": "content2",
124 "subdir/": "dir",
125 "subdir/file3.txt": "content3",
126 },
127 expected: 4, // 3 files and 1 directory, should count 4 items
128 },
129 {
130 name: "directory with hidden files",
131 structure: map[string]string{
132 ".hiddenfile": "hiddencontent",
133 "file1.txt": "content1",
134 },
135 expected: 1, // 1 non-hidden file
136 },
137 {
138 name: "empty directory",
139 structure: map[string]string{},
140 expected: 0, // 0 files
141 },
142 }
143
144 for _, tt := range tests {
145 t.Run(tt.name, func(t *testing.T) {
146 // Create a temporary directory for the test
147 tempDir, err := os.MkdirTemp("", "test")
148 if err != nil {
149 t.Fatalf("Failed to create temp dir: %v", err)
150 }
151 defer os.RemoveAll(tempDir)
152
153 // Create the test directory structure
154 if err := createTestDirStructure(tempDir, tt.structure); err != nil {
155 t.Fatalf("Failed to create test dir structure: %v", err)
156 }
157
158 // Calculate item count
159 result, err := CalcItemCount(tempDir)
160 if err != nil {
161 t.Fatalf("CalcItemCount returned an error: %v", err)
162 }
163
164 // Convert result to uint16
165 count := binary.BigEndian.Uint16(result)
166 if count != tt.expected {
167 t.Errorf("expected %d, got %d", tt.expected, count)
168 }
169 })
170 }
171}