forked from n10v/id3v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
320 lines (283 loc) · 8.55 KB
/
parse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2016 Albert Nigmatzianov. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package id3v2
import (
"bufio"
"bytes"
"errors"
"fmt"
"strings"
"testing"
)
// TestParse compares parsed frames with expected frames.
func TestParse(t *testing.T) {
if err := resetMP3Tag(); err != nil {
t.Fatal("Error while reseting mp3 file:", err)
}
tag, err := Open(mp3Name, defaultOpts)
if tag == nil || err != nil {
t.Error("Error while opening mp3 file:", err)
}
defer tag.Close()
testTextFrames(t, tag)
testPictureFrames(t, tag)
testUSLTFrames(t, tag)
testCommentFrames(t, tag)
testUnknownFrames(t, tag)
}
func testTextFrames(t *testing.T, tag *Tag) {
if err := compareTwoStrings(tag.Artist(), "Artist"); err != nil {
t.Error(err)
}
if err := compareTwoStrings(tag.Title(), "Title"); err != nil {
t.Error(err)
}
if err := compareTwoStrings(tag.Album(), "Album"); err != nil {
t.Error(err)
}
if err := compareTwoStrings(tag.Year(), "2016"); err != nil {
t.Error(err)
}
if err := compareTwoStrings(tag.Genre(), "Genre"); err != nil {
t.Error(err)
}
}
func compareTwoStrings(actual, expected string) error {
if actual != expected {
return fmt.Errorf("Expected %q, got %q", expected, actual)
}
return nil
}
func testPictureFrames(t *testing.T, tag *Tag) {
picFrames := tag.GetFrames(tag.CommonID("Attached picture"))
if len(picFrames) != 2 {
t.Fatalf("Expected picture frames: %v, got %v", 2, len(picFrames))
}
var parsedFrontCover, parsedBackCover PictureFrame
for _, f := range picFrames {
pf, ok := f.(PictureFrame)
if !ok {
t.Fatal("Couldn't assert picture frame")
}
if pf.PictureType == PTFrontCover {
parsedFrontCover = pf
}
if pf.PictureType == PTBackCover {
parsedBackCover = pf
}
}
if err := comparePictureFrames(parsedFrontCover, frontCover); err != nil {
t.Error(err)
}
if err := comparePictureFrames(parsedBackCover, backCover); err != nil {
t.Error(err)
}
}
func comparePictureFrames(actual, expected PictureFrame) error {
if err := compareTwoBytes(actual.Encoding.Key, expected.Encoding.Key); err != nil {
return err
}
if err := compareTwoStrings(actual.MimeType, expected.MimeType); err != nil {
return err
}
if err := compareTwoBytes(actual.PictureType, expected.PictureType); err != nil {
return err
}
if err := compareTwoStrings(actual.Description, expected.Description); err != nil {
return err
}
if !bytes.Equal(actual.Picture, expected.Picture) {
return errors.New("Pictures of picture frames' are different")
}
return nil
}
func testUSLTFrames(t *testing.T, tag *Tag) {
usltFrames := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription"))
if len(usltFrames) != 2 {
t.Fatalf("Expected USLT frames: %v, got %v", 2, len(usltFrames))
}
var parsedEngUSLF, parsedGerUSLF UnsynchronisedLyricsFrame
for _, f := range usltFrames {
uslf, ok := f.(UnsynchronisedLyricsFrame)
if !ok {
t.Fatal("Couldn't assert USLT frame")
}
if uslf.Language == "eng" {
parsedEngUSLF = uslf
}
if uslf.Language == "ger" {
parsedGerUSLF = uslf
}
}
if err := compareUSLTFrames(parsedEngUSLF, engUSLF); err != nil {
t.Error(err)
}
if err := compareUSLTFrames(parsedGerUSLF, gerUSLF); err != nil {
t.Error(err)
}
}
func compareUSLTFrames(actual, expected UnsynchronisedLyricsFrame) error {
if err := compareTwoBytes(actual.Encoding.Key, expected.Encoding.Key); err != nil {
return err
}
if err := compareTwoStrings(actual.Language, expected.Language); err != nil {
return err
}
if err := compareTwoStrings(actual.ContentDescriptor, expected.ContentDescriptor); err != nil {
return err
}
if err := compareTwoStrings(actual.Lyrics, expected.Lyrics); err != nil {
return err
}
return nil
}
func testCommentFrames(t *testing.T, tag *Tag) {
commFrames := tag.GetFrames(tag.CommonID("Comments"))
if len(commFrames) != 2 {
t.Fatalf("Expected comment frames: %v, got: %v", 2, len(commFrames))
}
var parsedEngComm, parsedGerComm CommentFrame
for _, f := range commFrames {
cf, ok := f.(CommentFrame)
if !ok {
t.Fatal("Couldn't assert comment frame")
}
if cf.Language == "eng" {
parsedEngComm = cf
}
if cf.Language == "ger" {
parsedGerComm = cf
}
}
if err := compareCommentFrames(parsedEngComm, engComm); err != nil {
t.Error(err)
}
if err := compareCommentFrames(parsedGerComm, gerComm); err != nil {
t.Error(err)
}
}
func compareCommentFrames(actual, expected CommentFrame) error {
if err := compareTwoBytes(actual.Encoding.Key, expected.Encoding.Key); err != nil {
return err
}
if err := compareTwoStrings(actual.Language, expected.Language); err != nil {
return err
}
if err := compareTwoStrings(actual.Description, expected.Description); err != nil {
return err
}
if err := compareTwoStrings(actual.Text, expected.Text); err != nil {
return err
}
return nil
}
func testUnknownFrames(t *testing.T, tag *Tag) {
parsedUnknownFramer := tag.GetLastFrame(unknownFrameID)
if parsedUnknownFramer == nil {
t.Fatal("Parsed unknown frame is nil")
}
parsedUnknownFrame := parsedUnknownFramer.(UnknownFrame)
if err := compareUnknownFrames(parsedUnknownFrame, unknownFrame); err != nil {
t.Error(err)
}
}
func compareUnknownFrames(actual, expected UnknownFrame) error {
actualBody := new(bytes.Buffer)
expectedBody := new(bytes.Buffer)
if _, err := actual.WriteTo(actualBody); err != nil {
return err
}
if _, err := expected.WriteTo(expectedBody); err != nil {
return err
}
if !bytes.Equal(actualBody.Bytes(), expectedBody.Bytes()) {
return errors.New("Body of unknown frame isn't the same as expected")
}
return nil
}
func compareTwoBytes(actual, expected byte) error {
if actual != expected {
return fmt.Errorf("Expected %v, got %v", expected, actual)
}
return nil
}
// TestParseOptionsParseFalse checks,
// if parseTag() will not parse the tag, if Options{Parse: false} is set.
func TestParseOptionsParseFalse(t *testing.T) {
tag, err := Open(mp3Name, Options{Parse: false})
if tag == nil || err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
if tag.HasFrames() {
t.Errorf("tag has %v frames, but should have no frames at all", tag.Count())
}
}
// TestParseOptionsParseFrames checks,
// if tag.parseAllFrames() will parse only frames, that set in Options.ParseFrames.
func TestParseOptionsParseFrames(t *testing.T) {
tag, err := Open(mp3Name, Options{Parse: true, ParseFrames: []string{"Artist", "Title"}})
if tag == nil || err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
if tag.Count() > 2 {
t.Errorf("tag should have only artist and title frames, but it has %v frames", tag.Count())
}
if tag.Artist() == "" {
t.Errorf("tag should have an artist, but it doesn't")
}
if tag.Title() == "" {
t.Errorf("tag should have a title, but it doesn't")
}
}
// TestParseInvalidFrameSize creates an empty tag, writes tag header,
// valid TIT2 frame and frame with invalid size, then checks
// if valid frame is parsed and there is only this frame in tag.
func TestParseInvalidFrameSize(t *testing.T) {
t.Parallel()
buf := new(bytes.Buffer)
bw := bufio.NewWriter(buf)
// Write tag header.
if err := writeTagHeader(bw, tagHeaderSize+16, 4); err != nil {
t.Fatal(err)
}
bw.Flush()
// Write valid TIT2 frame.
buf.Write([]byte{0x54, 0x49, 0x54, 0x32, 00, 00, 00, 06, 00, 00, 03}) // header and encoding
buf.WriteString("Title")
// Write invalid frame (size byte can't be greater than 127).
buf.Write([]byte{0x54, 0x49, 0x54, 0x32, 255, 255, 255, 255, 00, 00})
tag, err := ParseReader(buf, defaultOpts)
if tag == nil || err != nil {
t.Fatal("Error while parsing mp3 file:", err)
}
if tag.Title() != "Title" {
t.Errorf("Expected title: %q, got: %q", "Title", tag.Title())
}
if tag.Count() != 1 {
t.Error("There should be only 1 frame in tag, but there are", tag.Count())
}
}
// TestParseEmptyReader checks if ParseReader() correctly parses empty readers.
func TestParseEmptyReader(t *testing.T) {
t.Parallel()
tag, err := ParseReader(new(bytes.Buffer), Options{Parse: true})
if err != nil {
t.Error("Error while parsing empty reader:", err)
}
if tag.HasFrames() {
t.Error("Tag should not have any frames, but it has", tag.Count())
}
}
// TestParseReaderNil checks
// if ParseReader returns correct error when calling ParseReader(nil, Options{}).
func TestParseReaderNil(t *testing.T) {
t.Parallel()
_, err := ParseReader(nil, Options{Parse: true})
if err == nil {
t.Fatal("Expected that err is not nil, but err is nil")
}
if !strings.Contains(err.Error(), "rd is nil") {
t.Fatalf("Expected err contains %q, got %q", "rd is nil", err)
}
}