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