]> git.r.bdr.sh - rbdr/mobius/blame - hotline/flattened_file_object_test.go
Add partial support for file create/modify timestamps
[rbdr/mobius] / hotline / flattened_file_object_test.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
4 "bytes"
5 "encoding/hex"
92a7e455
JH
6 "fmt"
7 "github.com/stretchr/testify/assert"
8 "os"
6988a057
JH
9 "testing"
10)
11
12func TestReadFlattenedFileObject(t *testing.T) {
13 testData, _ := hex.DecodeString("46494c500001000000000000000000000000000000000002494e464f000000000000000000000052414d414354455854747478740000000000000100000000000000000000000000000000000000000000000000000000000000000007700000ba74247307700000ba74247300000008746573742e74787400004441544100000000000000000000000474657374")
14
15 ffo := ReadFlattenedFileObject(testData)
16
72dd37f1 17 format := ffo.FlatFileHeader.Format[:]
6988a057
JH
18 want := []byte("FILP")
19 if !bytes.Equal(format, want) {
20 t.Errorf("ReadFlattenedFileObject() = %q, want %q", format, want)
21 }
22}
5c34f875 23
72dd37f1
JH
24func TestNewFlattenedFileObject(t *testing.T) {
25 type args struct {
92a7e455
JH
26 fileRoot string
27 filePath []byte
28 fileName []byte
72dd37f1
JH
29 }
30 tests := []struct {
31 name string
32 args args
33 want *flattenedFileObject
92a7e455 34 wantErr assert.ErrorAssertionFunc
72dd37f1
JH
35 }{
36 {
92a7e455 37 name: "with valid file",
72dd37f1 38 args: args{
92a7e455
JH
39 fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
40 fileName: []byte("testfile.txt"),
41 filePath: []byte{0, 0},
72dd37f1
JH
42 },
43 want: &flattenedFileObject{
44 FlatFileHeader: NewFlatFileHeader(),
45 FlatFileInformationForkHeader: FlatFileInformationForkHeader{},
29f329ae 46 FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8)),
92a7e455 47 FlatFileDataForkHeader: FlatFileDataForkHeader{
72dd37f1
JH
48 ForkType: []byte("DATA"),
49 CompressionType: []byte{0, 0, 0, 0},
50 RSVD: []byte{0, 0, 0, 0},
51 DataSize: []byte{0x00, 0x00, 0x00, 0x17},
52 },
92a7e455 53 FileData: nil,
72dd37f1 54 },
92a7e455 55 wantErr: assert.NoError,
72dd37f1
JH
56 },
57 {
58 name: "when file path is invalid",
59 args: args{
92a7e455
JH
60 fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
61 fileName: []byte("nope.txt"),
72dd37f1 62 },
92a7e455
JH
63 want: nil,
64 wantErr: assert.Error,
72dd37f1
JH
65 },
66 }
67 for _, tt := range tests {
68 t.Run(tt.name, func(t *testing.T) {
92a7e455 69 got, err := NewFlattenedFileObject(tt.args.fileRoot, tt.args.filePath, tt.args.fileName)
29f329ae 70 if tt.wantErr(t, err, fmt.Sprintf("NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)) {
72dd37f1
JH
71 return
72 }
29f329ae
JH
73
74 // Clear the file timestamp fields to work around problems running the tests in multiple timezones
75 // TODO: revisit how to test this by mocking the stat calls
76 got.FlatFileInformationFork.CreateDate = make([]byte, 8)
77 got.FlatFileInformationFork.ModifyDate = make([]byte, 8)
92a7e455 78 assert.Equalf(t, tt.want, got, "NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)
72dd37f1
JH
79 })
80 }
92a7e455 81}
bb7fe19f
JH
82
83func Test_flattenedFileObject_BinaryMarshal(t *testing.T) {
84
85 testData, _ := hex.DecodeString("46494c500001000000000000000000000000000000000002494e464f000000000000000000000052414d414354455854747478740000000000000100000000000000000000000000000000000000000000000000000000000000000007700000ba74247307700000ba74247300000008746573742e74787400004441544100000000000000000000000474657374")
86 testFile := ReadFlattenedFileObject(testData)
87 testFile.FlatFileInformationFork.Comment = []byte("test!")
88 testFile.FlatFileInformationFork.CommentSize = []byte{0x00, 0x05}
89
90 type fields struct {
91 FlatFileHeader FlatFileHeader
92 FlatFileInformationForkHeader FlatFileInformationForkHeader
93 FlatFileInformationFork FlatFileInformationFork
94 FlatFileDataForkHeader FlatFileDataForkHeader
95 FileData []byte
96 }
97 tests := []struct {
98 name string
99 fields fields
100 want []byte
101 }{
102 {
103 name: "with a valid file",
104 fields: fields{
105 FlatFileHeader: testFile.FlatFileHeader,
106 FlatFileInformationForkHeader: testFile.FlatFileInformationForkHeader,
107 FlatFileInformationFork: testFile.FlatFileInformationFork,
108 FlatFileDataForkHeader: testFile.FlatFileDataForkHeader,
109 FileData: testFile.FileData,
110 },
111 want: []byte{
112 0x46, 0x49, 0x4c, 0x50, 0x00, 0x01, 0x00, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
115 0x49, 0x4e, 0x46, 0x4f, 0x00, 0x00, 0x00, 0x00,
116 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x57,
117 0x41, 0x4d, 0x41, 0x43, 0x54, 0x45, 0x58, 0x54,
118 0x74, 0x74, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00,
119 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 0x00, 0x00, 0x00, 0x00, 0x07, 0x70, 0x00, 0x00,
124 0xba, 0x74, 0x24, 0x73, 0x07, 0x70, 0x00, 0x00,
125 0xba, 0x74, 0x24, 0x73, 0x00, 0x00, 0x00, 0x08,
126 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74,
127 0x00, 0x05, 0x74, 0x65, 0x73, 0x74, 0x21, 0x44,
128 0x41, 0x54, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00,
129 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
130 },
131 },
132 }
133 for _, tt := range tests {
134 t.Run(tt.name, func(t *testing.T) {
135 f := flattenedFileObject{
136 FlatFileHeader: tt.fields.FlatFileHeader,
137 FlatFileInformationForkHeader: tt.fields.FlatFileInformationForkHeader,
138 FlatFileInformationFork: tt.fields.FlatFileInformationFork,
139 FlatFileDataForkHeader: tt.fields.FlatFileDataForkHeader,
140 FileData: tt.fields.FileData,
141 }
142 assert.Equalf(t, tt.want, f.BinaryMarshal(), "BinaryMarshal()")
143 })
144 }
145}