]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
92a7e455 JH |
4 | "fmt" |
5 | "github.com/stretchr/testify/assert" | |
6 | "os" | |
6988a057 JH |
7 | "testing" |
8 | ) | |
9 | ||
72dd37f1 JH |
10 | func TestNewFlattenedFileObject(t *testing.T) { |
11 | type args struct { | |
92a7e455 JH |
12 | fileRoot string |
13 | filePath []byte | |
14 | fileName []byte | |
72dd37f1 JH |
15 | } |
16 | tests := []struct { | |
17 | name string | |
18 | args args | |
19 | want *flattenedFileObject | |
92a7e455 | 20 | wantErr assert.ErrorAssertionFunc |
72dd37f1 JH |
21 | }{ |
22 | { | |
92a7e455 | 23 | name: "with valid file", |
72dd37f1 | 24 | args: args{ |
92a7e455 JH |
25 | fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), |
26 | fileName: []byte("testfile.txt"), | |
27 | filePath: []byte{0, 0}, | |
72dd37f1 JH |
28 | }, |
29 | want: &flattenedFileObject{ | |
30 | FlatFileHeader: NewFlatFileHeader(), | |
31 | FlatFileInformationForkHeader: FlatFileInformationForkHeader{}, | |
2d52424e | 32 | FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8), "", ""), |
92a7e455 | 33 | FlatFileDataForkHeader: FlatFileDataForkHeader{ |
85767504 JH |
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}, | |
72dd37f1 | 38 | }, |
92a7e455 | 39 | FileData: nil, |
72dd37f1 | 40 | }, |
92a7e455 | 41 | wantErr: assert.NoError, |
72dd37f1 JH |
42 | }, |
43 | { | |
44 | name: "when file path is invalid", | |
45 | args: args{ | |
92a7e455 JH |
46 | fileRoot: func() string { path, _ := os.Getwd(); return path + "/test/config/Files" }(), |
47 | fileName: []byte("nope.txt"), | |
72dd37f1 | 48 | }, |
92a7e455 JH |
49 | want: nil, |
50 | wantErr: assert.Error, | |
72dd37f1 JH |
51 | }, |
52 | } | |
53 | for _, tt := range tests { | |
54 | t.Run(tt.name, func(t *testing.T) { | |
16a4ad70 | 55 | got, err := NewFlattenedFileObject(tt.args.fileRoot, tt.args.filePath, tt.args.fileName, 0) |
29f329ae | 56 | if tt.wantErr(t, err, fmt.Sprintf("NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName)) { |
72dd37f1 JH |
57 | return |
58 | } | |
29f329ae JH |
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) | |
92a7e455 | 64 | assert.Equalf(t, tt.want, got, "NewFlattenedFileObject(%v, %v, %v)", tt.args.fileRoot, tt.args.filePath, tt.args.fileName) |
72dd37f1 JH |
65 | }) |
66 | } | |
92a7e455 | 67 | } |
050407a3 JH |
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)) | |
050407a3 JH |
101 | }) |
102 | } | |
103 | } |