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