]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
481631f6 JH |
3 | import ( |
4 | "bytes" | |
5 | "fmt" | |
6 | "github.com/stretchr/testify/assert" | |
7 | "io" | |
8 | "testing" | |
9 | ) | |
6988a057 | 10 | |
95159e55 | 11 | func TestTransfer_Write(t *testing.T) { |
df2735b2 JH |
12 | type fields struct { |
13 | Protocol [4]byte | |
14 | ReferenceNumber [4]byte | |
15 | DataSize [4]byte | |
16 | RSVD [4]byte | |
17 | } | |
18 | type args struct { | |
19 | b []byte | |
20 | } | |
21 | tests := []struct { | |
22 | name string | |
23 | fields fields | |
24 | args args | |
25 | want int | |
26 | wantErr bool | |
27 | }{ | |
28 | { | |
29 | name: "when b is a valid transfer", | |
30 | fields: fields{ | |
31 | Protocol: [4]byte{}, | |
32 | ReferenceNumber: [4]byte{}, | |
33 | DataSize: [4]byte{}, | |
34 | RSVD: [4]byte{}, | |
35 | }, | |
36 | args: args{ | |
37 | b: []byte{ | |
38 | 0x48, 0x54, 0x58, 0x46, | |
39 | 0x00, 0x00, 0x00, 0x01, | |
40 | 0x00, 0x00, 0x00, 0x02, | |
41 | 0x00, 0x00, 0x00, 0x00, | |
42 | }, | |
43 | }, | |
44 | want: 16, | |
45 | wantErr: false, | |
46 | }, | |
47 | { | |
48 | name: "when b contains invalid transfer protocol", | |
49 | fields: fields{ | |
50 | Protocol: [4]byte{}, | |
51 | ReferenceNumber: [4]byte{}, | |
52 | DataSize: [4]byte{}, | |
53 | RSVD: [4]byte{}, | |
54 | }, | |
55 | args: args{ | |
56 | b: []byte{ | |
57 | 0x11, 0x11, 0x11, 0x11, | |
58 | 0x00, 0x00, 0x00, 0x01, | |
59 | 0x00, 0x00, 0x00, 0x02, | |
60 | 0x00, 0x00, 0x00, 0x00, | |
61 | }, | |
62 | }, | |
63 | want: 0, | |
64 | wantErr: true, | |
65 | }, | |
66 | { | |
67 | name: "when b does not contain expected len of bytes", | |
68 | fields: fields{ | |
69 | Protocol: [4]byte{}, | |
70 | ReferenceNumber: [4]byte{}, | |
71 | DataSize: [4]byte{}, | |
72 | RSVD: [4]byte{}, | |
73 | }, | |
74 | args: args{ | |
75 | b: []byte{ | |
76 | 0x48, 0x54, 0x58, 0x46, | |
77 | 0x00, 0x00, 0x00, 0x01, | |
78 | 0x00, 0x00, 0x00, 0x02, | |
79 | 0x00, 0x00, 0x00, | |
80 | }, | |
81 | }, | |
82 | want: 0, | |
83 | wantErr: true, | |
84 | }, | |
85 | } | |
86 | for _, tt := range tests { | |
87 | t.Run(tt.name, func(t *testing.T) { | |
88 | tf := &transfer{ | |
89 | Protocol: tt.fields.Protocol, | |
90 | ReferenceNumber: tt.fields.ReferenceNumber, | |
91 | DataSize: tt.fields.DataSize, | |
92 | RSVD: tt.fields.RSVD, | |
93 | } | |
94 | got, err := tf.Write(tt.args.b) | |
95 | if (err != nil) != tt.wantErr { | |
96 | t.Errorf("Read() error = %v, wantErr %v", err, tt.wantErr) | |
97 | return | |
98 | } | |
99 | if got != tt.want { | |
100 | t.Errorf("Read() got = %v, want %v", got, tt.want) | |
101 | } | |
102 | }) | |
103 | } | |
6988a057 | 104 | } |
481631f6 JH |
105 | |
106 | func Test_receiveFile(t *testing.T) { | |
107 | type args struct { | |
108 | conn io.Reader | |
109 | } | |
110 | tests := []struct { | |
111 | name string | |
112 | args args | |
113 | wantTargetFile []byte | |
114 | wantResForkFile []byte | |
115 | wantErr assert.ErrorAssertionFunc | |
116 | }{ | |
117 | { | |
7cd900d6 | 118 | name: "transfers file when there is no resource fork", |
481631f6 JH |
119 | args: args{ |
120 | conn: func() io.Reader { | |
121 | testFile := flattenedFileObject{ | |
7cd900d6 JH |
122 | FlatFileHeader: FlatFileHeader{ |
123 | Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP" | |
124 | Version: [2]byte{0, 1}, | |
7cd900d6 JH |
125 | ForkCount: [2]byte{0, 2}, |
126 | }, | |
127 | FlatFileInformationForkHeader: FlatFileForkHeader{}, | |
a55350da | 128 | FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", [8]byte{}, "TEXT", "TEXT"), |
7cd900d6 | 129 | FlatFileDataForkHeader: FlatFileForkHeader{ |
fd740bc4 JH |
130 | ForkType: [4]byte{0x4d, 0x41, 0x43, 0x52}, // DATA |
131 | DataSize: [4]byte{0x00, 0x00, 0x00, 0x03}, | |
481631f6 | 132 | }, |
481631f6 JH |
133 | } |
134 | fakeFileData := []byte{1, 2, 3} | |
9cf66aea | 135 | b, _ := io.ReadAll(&testFile) |
481631f6 JH |
136 | b = append(b, fakeFileData...) |
137 | return bytes.NewReader(b) | |
138 | }(), | |
139 | }, | |
140 | wantTargetFile: []byte{1, 2, 3}, | |
141 | wantResForkFile: []byte(nil), | |
142 | ||
143 | wantErr: assert.NoError, | |
144 | }, | |
7cd900d6 | 145 | // { |
95159e55 | 146 | // Name: "transfers fileWrapper when there is a resource fork", |
7cd900d6 JH |
147 | // args: args{ |
148 | // conn: func() io.Reader { | |
149 | // testFile := flattenedFileObject{ | |
150 | // FlatFileHeader: FlatFileHeader{ | |
151 | // Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP" | |
152 | // Version: [2]byte{0, 1}, | |
153 | // RSVD: [16]byte{}, | |
154 | // ForkCount: [2]byte{0, 3}, | |
155 | // }, | |
156 | // FlatFileInformationForkHeader: FlatFileForkHeader{}, | |
157 | // FlatFileInformationFork: NewFlatFileInformationFork("testfile.txt", make([]byte, 8), "TEXT", "TEXT"), | |
158 | // FlatFileDataForkHeader: FlatFileForkHeader{ | |
159 | // ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // DATA | |
160 | // CompressionType: [4]byte{0, 0, 0, 0}, | |
161 | // RSVD: [4]byte{0, 0, 0, 0}, | |
162 | // DataSize: [4]byte{0x00, 0x00, 0x00, 0x03}, | |
163 | // }, | |
164 | // FlatFileResForkHeader: FlatFileForkHeader{ | |
165 | // ForkType: [4]byte{0x4d, 0x41, 0x43, 0x52}, // MACR | |
166 | // CompressionType: [4]byte{0, 0, 0, 0}, | |
167 | // RSVD: [4]byte{0, 0, 0, 0}, | |
168 | // DataSize: [4]byte{0x00, 0x00, 0x00, 0x03}, | |
169 | // }, | |
170 | // } | |
171 | // fakeFileData := []byte{1, 2, 3} | |
172 | // b := testFile.BinaryMarshal() | |
173 | // b = append(b, fakeFileData...) | |
174 | // return bytes.NewReader(b) | |
175 | // }(), | |
176 | // }, | |
177 | // wantTargetFile: []byte{1, 2, 3}, | |
178 | // wantResForkFile: []byte(nil), | |
179 | // | |
180 | // wantErr: assert.NoError, | |
181 | // }, | |
481631f6 JH |
182 | } |
183 | for _, tt := range tests { | |
184 | t.Run(tt.name, func(t *testing.T) { | |
185 | targetFile := &bytes.Buffer{} | |
186 | resForkFile := &bytes.Buffer{} | |
7cd900d6 | 187 | infoForkFile := &bytes.Buffer{} |
df1ade54 | 188 | err := receiveFile(tt.args.conn, targetFile, resForkFile, infoForkFile, io.Discard) |
481631f6 JH |
189 | if !tt.wantErr(t, err, fmt.Sprintf("receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile)) { |
190 | return | |
191 | } | |
192 | ||
193 | assert.Equalf(t, tt.wantTargetFile, targetFile.Bytes(), "receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile) | |
194 | assert.Equalf(t, tt.wantResForkFile, resForkFile.Bytes(), "receiveFile(%v, %v, %v)", tt.args.conn, targetFile, resForkFile) | |
195 | }) | |
196 | } | |
197 | } |