]>
Commit | Line | Data |
---|---|---|
43ecc0f4 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "github.com/stretchr/testify/assert" | |
b129b7cb | 5 | "io" |
43ecc0f4 JH |
6 | "testing" |
7 | ) | |
8 | ||
72dd37f1 | 9 | func TestFileNameWithInfo_MarshalBinary(t *testing.T) { |
43ecc0f4 | 10 | type fields struct { |
fd740bc4 | 11 | fileNameWithInfoHeader FileNameWithInfoHeader |
72dd37f1 JH |
12 | name []byte |
13 | } | |
14 | tests := []struct { | |
15 | name string | |
16 | fields fields | |
17 | wantData []byte | |
18 | wantErr bool | |
19 | }{ | |
20 | { | |
21 | name: "returns expected bytes", | |
22 | fields: fields{ | |
fd740bc4 | 23 | fileNameWithInfoHeader: FileNameWithInfoHeader{ |
72dd37f1 JH |
24 | Type: [4]byte{0x54, 0x45, 0x58, 0x54}, // TEXT |
25 | Creator: [4]byte{0x54, 0x54, 0x58, 0x54}, // TTXT | |
26 | FileSize: [4]byte{0x00, 0x43, 0x16, 0xd3}, // File Size | |
27 | RSVD: [4]byte{0, 0, 0, 0}, | |
28 | NameScript: [2]byte{0, 0}, | |
29 | NameSize: [2]byte{0x00, 0x03}, | |
30 | }, | |
31 | name: []byte("foo"), | |
32 | }, | |
33 | wantData: []byte{ | |
34 | 0x54, 0x45, 0x58, 0x54, | |
35 | 0x54, 0x54, 0x58, 0x54, | |
36 | 0x00, 0x43, 0x16, 0xd3, | |
37 | 0, 0, 0, 0, | |
38 | 0, 0, | |
39 | 0x00, 0x03, | |
40 | 0x66, 0x6f, 0x6f, | |
41 | }, | |
42 | wantErr: false, | |
43 | }, | |
44 | } | |
45 | for _, tt := range tests { | |
46 | t.Run(tt.name, func(t *testing.T) { | |
47 | f := &FileNameWithInfo{ | |
fd740bc4 | 48 | FileNameWithInfoHeader: tt.fields.fileNameWithInfoHeader, |
95159e55 | 49 | Name: tt.fields.name, |
72dd37f1 | 50 | } |
b129b7cb | 51 | gotData, err := io.ReadAll(f) |
72dd37f1 JH |
52 | if (err != nil) != tt.wantErr { |
53 | t.Errorf("MarshalBinary() error = %v, wantErr %v", err, tt.wantErr) | |
54 | return | |
55 | } | |
50c837fe | 56 | if !assert.Equal(t, tt.wantData, gotData) { |
72dd37f1 JH |
57 | t.Errorf("MarshalBinary() gotData = %v, want %v", gotData, tt.wantData) |
58 | } | |
59 | }) | |
60 | } | |
61 | } | |
62 | ||
63 | func TestFileNameWithInfo_UnmarshalBinary(t *testing.T) { | |
64 | type fields struct { | |
fd740bc4 | 65 | fileNameWithInfoHeader FileNameWithInfoHeader |
72dd37f1 | 66 | name []byte |
43ecc0f4 JH |
67 | } |
68 | type args struct { | |
72dd37f1 | 69 | data []byte |
43ecc0f4 JH |
70 | } |
71 | tests := []struct { | |
72 | name string | |
73 | fields fields | |
74 | args args | |
8fc43f8e | 75 | want *FileNameWithInfo |
43ecc0f4 JH |
76 | wantErr bool |
77 | }{ | |
78 | { | |
72dd37f1 | 79 | name: "writes bytes into struct", |
43ecc0f4 | 80 | args: args{ |
72dd37f1 | 81 | data: []byte{ |
43ecc0f4 JH |
82 | 0x54, 0x45, 0x58, 0x54, // TEXT |
83 | 0x54, 0x54, 0x58, 0x54, // TTXT | |
84 | 0x00, 0x43, 0x16, 0xd3, // File Size | |
85 | 0x00, 0x00, 0x00, 0x00, // RSVD | |
86 | 0x00, 0x00, // NameScript | |
87 | 0x00, 0x0e, // Name Size | |
88 | 0x41, 0x75, 0x64, 0x69, 0x6f, 0x6e, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x7a, 0x69, 0x70, | |
89 | }, | |
90 | }, | |
91 | want: &FileNameWithInfo{ | |
fd740bc4 | 92 | FileNameWithInfoHeader: FileNameWithInfoHeader{ |
72dd37f1 JH |
93 | Type: [4]byte{0x54, 0x45, 0x58, 0x54}, // TEXT |
94 | Creator: [4]byte{0x54, 0x54, 0x58, 0x54}, // TTXT | |
95 | FileSize: [4]byte{0x00, 0x43, 0x16, 0xd3}, // File Size | |
96 | RSVD: [4]byte{0, 0, 0, 0}, | |
97 | NameScript: [2]byte{0, 0}, | |
98 | NameSize: [2]byte{0x00, 0x0e}, | |
99 | }, | |
95159e55 | 100 | Name: []byte("Audion.app.zip"), |
43ecc0f4 | 101 | }, |
43ecc0f4 JH |
102 | wantErr: false, |
103 | }, | |
104 | } | |
105 | for _, tt := range tests { | |
106 | t.Run(tt.name, func(t *testing.T) { | |
107 | f := &FileNameWithInfo{ | |
fd740bc4 | 108 | FileNameWithInfoHeader: tt.fields.fileNameWithInfoHeader, |
95159e55 | 109 | Name: tt.fields.name, |
43ecc0f4 | 110 | } |
b129b7cb | 111 | if _, err := f.Write(tt.args.data); (err != nil) != tt.wantErr { |
8fc43f8e | 112 | t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr) |
43ecc0f4 JH |
113 | } |
114 | if !assert.Equal(t, tt.want, f) { | |
115 | t.Errorf("Read() got = %v, want %v", f, tt.want) | |
43ecc0f4 JH |
116 | } |
117 | }) | |
118 | } | |
8fc43f8e | 119 | } |