]> git.r.bdr.sh - rbdr/mobius/blob - hotline/flattened_file_object_test.go
4b3fdf65f0b015aa421fa04a3abca7ec8395d98b
[rbdr/mobius] / hotline / flattened_file_object_test.go
1 package hotline
2
3 import (
4 "fmt"
5 "github.com/stretchr/testify/assert"
6 "os"
7 "testing"
8 )
9
10 func TestNewFlattenedFileObject(t *testing.T) {
11 type args struct {
12 fileRoot string
13 filePath []byte
14 fileName []byte
15 }
16 tests := []struct {
17 name string
18 args args
19 want *flattenedFileObject
20 wantErr assert.ErrorAssertionFunc
21 }{
22 {
23 name: "with valid file",
24 args: args{
25 fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
26 fileName: []byte("testfile.txt"),
27 filePath: []byte{0, 0},
28 },
29 want: &flattenedFileObject{
30 FlatFileHeader: NewFlatFileHeader(),
31 FlatFileInformationForkHeader: FlatFileInformationForkHeader{},
32 FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8), "", ""),
33 FlatFileDataForkHeader: FlatFileDataForkHeader{
34 ForkType: [4]byte{0x4d, 0x41, 0x43, 0x52}, // DATA
35 CompressionType: [4]byte{0, 0, 0, 0},
36 RSVD: [4]byte{0, 0, 0, 0},
37 DataSize: [4]byte{0x00, 0x00, 0x00, 0x17},
38 },
39 FileData: nil,
40 },
41 wantErr: assert.NoError,
42 },
43 {
44 name: "when file path is invalid",
45 args: args{
46 fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
47 fileName: []byte("nope.txt"),
48 },
49 want: nil,
50 wantErr: assert.Error,
51 },
52 }
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 got, err := NewFlattenedFileObject(tt.args.fileRoot, tt.args.filePath, tt.args.fileName, 0)
56 if tt.wantErr(t, err, fmt.Sprintf("NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)) {
57 return
58 }
59
60 // Clear the file timestamp fields to work around problems running the tests in multiple timezones
61 // TODO: revisit how to test this by mocking the stat calls
62 got.FlatFileInformationFork.CreateDate = make([]byte, 8)
63 got.FlatFileInformationFork.ModifyDate = make([]byte, 8)
64 assert.Equalf(t, tt.want, got, "NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)
65 })
66 }
67 }
68
69 func TestFlatFileInformationFork_UnmarshalBinary(t *testing.T) {
70 type args struct {
71 b []byte
72 }
73 tests := []struct {
74 name string
75 args args
76 wantErr assert.ErrorAssertionFunc
77 }{
78 {
79 name: "when zero length comment size is omitted (Nostalgia client behavior)",
80 args: args{
81 b: []byte{
82 0x41, 0x4d, 0x41, 0x43, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x62, 0x65, 0x61, 0x72, 0x2e, 0x74, 0x69, 0x66, 0x66,
83 },
84 },
85 wantErr: assert.NoError,
86 },
87 {
88 name: "when zero length comment size is included",
89 args: args{
90 b: []byte{
91 0x41, 0x4d, 0x41, 0x43, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x62, 0x65, 0x61, 0x72, 0x2e, 0x74, 0x69, 0x66, 0x66, 0x00, 0x00,
92 },
93 },
94 wantErr: assert.NoError,
95 },
96 }
97 for _, tt := range tests {
98 t.Run(tt.name, func(t *testing.T) {
99 ffif := &FlatFileInformationFork{}
100 tt.wantErr(t, ffif.UnmarshalBinary(tt.args.b), fmt.Sprintf("UnmarshalBinary(%v)", tt.args.b))
101 })
102 }
103 }