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