]> git.r.bdr.sh - rbdr/mobius/blob - hotline/flattened_file_object_test.go
Add -config flag to client
[rbdr/mobius] / hotline / flattened_file_object_test.go
1 package hotline
2
3 import (
4 "fmt"
5 "github.com/davecgh/go-spew/spew"
6 "github.com/stretchr/testify/assert"
7 "os"
8 "testing"
9 )
10
11 func TestNewFlattenedFileObject(t *testing.T) {
12 type args struct {
13 fileRoot string
14 filePath []byte
15 fileName []byte
16 }
17 tests := []struct {
18 name string
19 args args
20 want *flattenedFileObject
21 wantErr assert.ErrorAssertionFunc
22 }{
23 {
24 name: "with valid file",
25 args: args{
26 fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
27 fileName: []byte("testfile.txt"),
28 filePath: []byte{0, 0},
29 },
30 want: &flattenedFileObject{
31 FlatFileHeader: NewFlatFileHeader(),
32 FlatFileInformationForkHeader: FlatFileInformationForkHeader{},
33 FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8)),
34 FlatFileDataForkHeader: FlatFileDataForkHeader{
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},
39 },
40 FileData: nil,
41 },
42 wantErr: assert.NoError,
43 },
44 {
45 name: "when file path is invalid",
46 args: args{
47 fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(),
48 fileName: []byte("nope.txt"),
49 },
50 want: nil,
51 wantErr: assert.Error,
52 },
53 }
54 for _, tt := range tests {
55 t.Run(tt.name, func(t *testing.T) {
56 got, err := NewFlattenedFileObject(tt.args.fileRoot, tt.args.filePath, tt.args.fileName, 0)
57 if tt.wantErr(t, err, fmt.Sprintf("NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)) {
58 return
59 }
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)
65 assert.Equalf(t, tt.want, got, "NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)
66 })
67 }
68 }
69
70 func 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 }