-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtag_test.go
569 lines (486 loc) · 14.7 KB
/
tag_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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// 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"
"fmt"
"io/ioutil"
"math/big"
"os"
"strings"
"sync"
"testing"
)
const (
mp3Path = "testdata/test.mp3"
frontCoverPath = "testdata/front_cover.jpg"
backCoverPath = "testdata/back_cover.jpg"
framesSize = 211978
tagSize = tagHeaderSize + framesSize
musicSize = 3840834
countOfFrames = 15
)
var (
frontCover = PictureFrame{
Encoding: EncodingUTF8,
MimeType: "image/jpeg",
PictureType: PTFrontCover,
Description: "Front cover",
Picture: mustReadFile(frontCoverPath),
}
backCover = PictureFrame{
Encoding: EncodingUTF8,
MimeType: "image/jpeg",
PictureType: PTBackCover,
Description: "Back cover",
Picture: mustReadFile(backCoverPath),
}
engUSLF = UnsynchronisedLyricsFrame{
Encoding: EncodingUTF8,
Language: "eng",
ContentDescriptor: "Content descriptor",
Lyrics: "bogem/id3v2",
}
gerUSLF = UnsynchronisedLyricsFrame{
Encoding: EncodingUTF8,
Language: "ger",
ContentDescriptor: "Inhaltsdeskriptor",
Lyrics: "Einigkeit und Recht und Freiheit",
}
musicBrainzUDTF = UserDefinedTextFrame{
Encoding: EncodingUTF8,
Description: "MusicBrainz Album Id",
Value: "fbd94fb6-2a74-42d0-acbc-81caf8b84984",
}
musicBrainzUF = UFIDFrame{
OwnerIdentifier: "https://musicbrainz.org",
Identifier: []byte("fbd94fb6-2a74-42d0-acbc-81caf8b84984"),
}
engComm = CommentFrame{
Encoding: EncodingUTF8,
Language: "eng",
Description: "Short description",
Text: "The actual text",
}
gerComm = CommentFrame{
Encoding: EncodingUTF8,
Language: "ger",
Description: "Kurze Beschreibung",
Text: "Der eigentliche Text",
}
popmFrame = PopularimeterFrame{
Email: "foo@bar.com",
Rating: 128,
Counter: big.NewInt(10000000000000000),
}
unknownFrameID = "WPUB"
unknownFrame = UnknownFrame{
Body: []byte("https://soundcloud.com/suicidepart2"),
}
// Parse all frames
parseOpts = Options{Parse: true}
)
func init() {
if err := resetMP3Tag(); err != nil {
panic(fmt.Sprintf("Error while reseting mp3 file: %v", err))
}
}
// resetMP3Tag sets default tag in file located by mp3Path.
func resetMP3Tag() error {
tag, err := Open(mp3Path, Options{Parse: false})
if tag == nil || err != nil {
return err
}
defer tag.Close()
tag.SetTitle("Title")
tag.SetArtist("Artist")
tag.SetAlbum("Album")
tag.SetYear("2016")
tag.SetGenre("Genre")
tag.AddAttachedPicture(frontCover)
tag.AddAttachedPicture(backCover)
tag.AddUnsynchronisedLyricsFrame(engUSLF)
tag.AddUnsynchronisedLyricsFrame(gerUSLF)
tag.AddUserDefinedTextFrame(musicBrainzUDTF)
tag.AddUFIDFrame(musicBrainzUF)
tag.AddFrame(tag.CommonID("Popularimeter"), popmFrame)
tag.AddCommentFrame(engComm)
tag.AddCommentFrame(gerComm)
tag.AddFrame(unknownFrameID, unknownFrame)
return tag.Save()
}
func mustReadFile(path string) []byte {
contents, err := ioutil.ReadFile(path)
if err != nil {
panic(fmt.Sprintf("can't read %q: %v", path, err))
}
return contents
}
func TestCountLenSize(t *testing.T) {
tag, err := Open(mp3Path, parseOpts)
if tag == nil || err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
defer tag.Close()
// Check count.
if tag.Count() != countOfFrames {
t.Errorf("Expected frames: %v, got: %v", countOfFrames, tag.Count())
}
// Check len of tag.AllFrames().
if len(tag.AllFrames()) != 12 {
t.Errorf("Expected: %v, got: %v", 11, len(tag.AllFrames()))
}
// Check saved tag size by reading the 6:10 bytes of mp3 file.
mp3, err := os.Open(mp3Path)
if err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
tagHeader := make([]byte, tagHeaderSize)
n, err := mp3.Read(tagHeader)
if n != tagHeaderSize {
t.Errorf("Expected length of header %v, got %v", tagHeaderSize, n)
}
if err != nil {
t.Error("Error while reading a tag header:", err)
}
size, err := parseSize(tagHeader[6:10], true)
if err != nil {
t.Error("Error while parsing a tag header size:", err)
}
if framesSize != size {
t.Errorf("Expected size of frames: %v, got: %v", framesSize, size)
}
// Check tag.Size().
if tag.Size() != tagSize {
t.Errorf("Expected tag.Size(): %v, got: %v", tagSize, tag.Size())
}
}
// TestIntegrityOfMusicAtTheBeginning checks
// if tag.Save() doesn't truncate or add some extra bytes at the beginning
// of music part.
func TestIntegrityOfMusicAtTheBeginning(t *testing.T) {
mp3, err := os.Open(mp3Path)
if err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
defer mp3.Close()
rd := bufio.NewReader(mp3)
n, err := rd.Discard(tagSize)
if n != tagSize {
t.Errorf("Expected length of discarded bytes %v, got %v", tagSize, n)
}
if err != nil {
t.Fatal("Error while reading mp3 file:", err)
}
expected := []byte{255, 251, 80, 0, 0, 0, 0}
got := make([]byte, len(expected))
n, err = rd.Read(got)
if n != len(expected) {
t.Errorf("Expected length of read bytes %v, got %v", len(expected), n)
}
if err != nil {
t.Fatal("Error while reading mp3 file:", err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("Expected %v, got %v", expected, got)
}
}
// TestIntegrityOfMusicAtTheEnd checks
// if tag.Save() doesn't truncate music part or add some extra bytes at the end
// of music part.
func TestIntegrityOfMusicAtTheEnd(t *testing.T) {
mp3, err := os.Open(mp3Path)
if err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
defer mp3.Close()
rd := bufio.NewReader(mp3)
expected := []byte{85, 85, 85, 85, 85, 85, 85}
toDiscard := tagSize + musicSize - len(expected)
n, err := rd.Discard(toDiscard)
if n != toDiscard {
t.Errorf("Expected length of discarded bytes %v, got %v", toDiscard, n)
}
if err != nil {
t.Fatal("Error while discarding:", err)
}
got := make([]byte, len(expected))
n, err = rd.Read(got)
if n != len(expected) {
t.Errorf("Expected length of read bytes %v, got %v", len(expected), n)
}
if err != nil {
t.Fatal("Error while reading mp3 file:", err)
}
if !bytes.Equal(expected, got) {
t.Fatalf("Expected %v, got %v", expected, got)
}
}
// TestCheckPermissions checks
// if tag.Save() creates file with the same permissions of original file.
func TestCheckPermissions(t *testing.T) {
originalFile, err := os.Open(mp3Path)
if err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
originalStat, err := originalFile.Stat()
if err != nil {
t.Fatal("Error while getting mp3 file stat:", err)
}
originalMode := originalStat.Mode()
originalFile.Close()
tag, err := Open(mp3Path, parseOpts)
if err != nil {
t.Fatal("Error while parsing a tag:", err)
}
if err = tag.Save(); err != nil {
t.Error("Error while saving a tag:", err)
}
tag.Close()
newFile, err := os.Open(mp3Path)
if err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
newStat, err := newFile.Stat()
if err != nil {
t.Fatal("Error while getting mp3 file stats:", err)
}
newMode := newStat.Mode()
if originalMode != newMode {
t.Errorf("Expected permissions: %v, got %v", originalMode, newMode)
}
}
// TestBlankID creates empty tag, adds frame with blank id and checks
// if no tag is written by tag.WriteTo() (it must not write tag to buf
// if there are no or only blank frames).
func TestBlankID(t *testing.T) {
t.Parallel()
tag := NewEmptyTag()
tag.AddFrame("", unknownFrame)
if tag.Count() > 0 {
t.Error("There should be no frames in tag, but there are", tag.Count())
}
if tag.HasFrames() {
t.Error("tag.HasFrames() should return false, but it returns true")
}
if tag.Size() != 0 {
t.Error("Size of tag should be 0. Actual tag size:", tag.Size())
}
// It should write no frames to buf.
buf := new(bytes.Buffer)
if _, err := tag.WriteTo(buf); err != nil {
t.Fatal("Error while writing a tag:", err)
}
if buf.Len() > 0 {
t.Fatal("tag.WriteTo(buf) should write no frames, but it wrote")
}
}
// TestInvalidLanguageCommentFrame checks
// if tag.WriteTo() returns the correct error by writing the comment frame with
// incorrect length of language code.
func TestInvalidLanguageCommentFrame(t *testing.T) {
t.Parallel()
tag := NewEmptyTag()
tag.AddCommentFrame(CommentFrame{
Encoding: EncodingUTF8,
Language: "en", // should be "eng" according to ISO 639-2
Text: "The actual text",
})
_, err := tag.WriteTo(ioutil.Discard)
if err == nil {
t.Fatal("tag.WriteTo() must return the error about invalid language code")
}
if !strings.Contains(err.Error(), "must consist") {
t.Fatalf("Incorrect error. Expected error contains %q, got %q", "must consist", err)
}
}
// TestInvalidLanguageUSLF checks
// if tag.WriteTo() returns the correct error by writing the comment frame with
// incorrect length of language code.
func TestInvalidLanguageUSLF(t *testing.T) {
t.Parallel()
tag := NewEmptyTag()
tag.AddUnsynchronisedLyricsFrame(UnsynchronisedLyricsFrame{
Encoding: EncodingUTF8,
Language: "en", // should be "eng" according to ISO 639-2
Lyrics: "Lyrics",
})
_, err := tag.WriteTo(ioutil.Discard)
if err == nil {
t.Fatal("tag.WriteTo() must return the error about invalid language code")
}
if !strings.Contains(err.Error(), "must consist") {
t.Fatalf("Incorrect error. Expected error contains %q, got %q", "must consist", err)
}
}
// TestSaveAndCloseEmptyTag checks
// if tag.Save() and tag.Close() return an error for empty tag.
func TestSaveAndCloseEmptyTag(t *testing.T) {
t.Parallel()
tag := NewEmptyTag()
if err := tag.Save(); err == nil {
t.Error("By saving empty tag we wait for an error, but it's not returned")
}
if err := tag.Close(); err == nil {
t.Error("By closing empty tag we wait for an error, but it's not returned")
}
}
// TestEmptyTagWriteTo checks
// if tag.WriteTo() works correctly for empty tag.
func TestEmptyTagWriteTo(t *testing.T) {
t.Parallel()
tag := NewEmptyTag()
tag.SetArtist("Artist")
tag.SetTitle("Title")
buf := new(bytes.Buffer)
if _, err := tag.WriteTo(buf); err != nil {
t.Fatal("Error while writing to buf:", err)
}
if buf.Len() == 0 {
t.Fatal("buf is empty, but it must have tag")
}
parsedTag, err := ParseReader(buf, parseOpts)
if err != nil {
t.Fatal("Error while parsing buf:", err)
}
if parsedTag.Artist() != "Artist" {
t.Error("Expected Artist, got", parsedTag.Artist())
}
if parsedTag.Title() != "Title" {
t.Error("Expected Title, got", parsedTag.Title())
}
}
// TestResetTag checks if tag.Reset() correctly parses mp3.
func TestResetTag(t *testing.T) {
mp3, err := os.Open(mp3Path)
if err != nil {
t.Fatal("Error while opening mp3 file:", err)
}
defer mp3.Close()
tag := NewEmptyTag()
if err := tag.Reset(mp3, parseOpts); err != nil {
t.Fatal("Error while reseting tag:", err)
}
// Check if it parsed.
if tag.Count() != countOfFrames {
t.Errorf("Expected frames: %v, got: %v", countOfFrames, tag.Count())
}
}
// TestConcurrent creates sync.Pool with tags, executes 100 goroutines and
// checks if id3v2 works correctly in concurrent environment (one tag per goroutine).
func TestConcurrent(t *testing.T) {
tagPool := sync.Pool{New: func() interface{} { return NewEmptyTag() }}
ec := make(chan error, 100)
var wg sync.WaitGroup
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
defer wg.Done()
tag := tagPool.Get().(*Tag)
defer tagPool.Put(tag)
file, err := os.Open(mp3Path)
if err != nil {
ec <- fmt.Errorf("Error while opening mp3: %v", err)
return
}
defer file.Close()
if err := tag.Reset(file, parseOpts); err != nil {
ec <- fmt.Errorf("Error while reseting tag to file: %v", err)
return
}
// Just check if it's correctly parsed.
if tag.Count() != countOfFrames {
ec <- fmt.Errorf("Expected frames: %v, got: %v", countOfFrames, tag.Count())
return
}
if _, err := tag.WriteTo(ioutil.Discard); err != nil {
ec <- fmt.Errorf("Error while writing to ioutil.Discard: %v", err)
return
}
}()
}
wg.Wait()
close(ec)
err := <-ec
if err != nil {
t.Fatal(err)
}
}
// TestEncodedText checks
// if texts of frames encoded with different encodings are correctly written.
func TestEncodedText(t *testing.T) {
t.Parallel()
encoded := "Héllö"
tag := NewEmptyTag()
tag.AddFrame(tag.CommonID("Title"), TextFrame{
Encoding: EncodingISO,
Text: encoded,
})
tag.AddFrame(tag.CommonID("Attached picture"), PictureFrame{
Encoding: EncodingUTF16,
MimeType: "image/jpeg",
PictureType: PTFrontCover,
Description: encoded,
})
tag.AddFrame(tag.CommonID("Unsynchronised lyrics/text transcription"), UnsynchronisedLyricsFrame{
Encoding: EncodingUTF16BE,
Language: "ger",
ContentDescriptor: encoded,
Lyrics: encoded,
})
tag.AddFrame(tag.CommonID("Comments"), CommentFrame{
Encoding: EncodingUTF8,
Language: "eng",
Description: encoded,
Text: encoded,
})
buf := new(bytes.Buffer)
n, err := tag.WriteTo(buf)
if err != nil {
t.Fatalf("Error by writing to buf: %v", err)
}
if n != int64(tag.Size()) {
t.Errorf("Expected WriteTo n==%v, got %v", tag.Size(), n)
}
tag, err = ParseReader(buf, parseOpts)
if err != nil {
t.Fatalf("Error by parsing the tag: %v", err)
}
tf := tag.GetLastFrame(tag.CommonID("Title")).(TextFrame)
if !tf.Encoding.Equals(EncodingISO) && tf.Text != encoded {
t.Errorf("Expected %q and %q, got %q and %q", EncodingISO, encoded, tf.Encoding, tf.Text)
}
pf := tag.GetLastFrame(tag.CommonID("Attached picture")).(PictureFrame)
if !pf.Encoding.Equals(EncodingUTF16) && pf.Description != encoded {
t.Errorf("Expected %q and %q, got %q and %q", EncodingISO, encoded, pf.Encoding, pf.Description)
}
uslf := tag.GetLastFrame(tag.CommonID("Unsynchronised lyrics/text transcription")).(UnsynchronisedLyricsFrame)
if !uslf.Encoding.Equals(EncodingUTF16BE) && uslf.ContentDescriptor != encoded && uslf.Lyrics != encoded {
t.Errorf("Expected %q, %q and %q; got %q, %q and %q", EncodingISO, encoded, encoded, uslf.Encoding, uslf.ContentDescriptor, uslf.Lyrics)
}
cf := tag.GetLastFrame(tag.CommonID("Comments")).(CommentFrame)
if !cf.Encoding.Equals(EncodingUTF8) && cf.Description != encoded && cf.Text != encoded {
t.Errorf("Expected %q, %q and %q; got %q, %q and %q", EncodingUTF8, encoded, encoded, cf.Encoding, cf.Description, cf.Text)
}
}
func TestWriteToN(t *testing.T) {
tag := NewEmptyTag()
tag.SetTitle("Title")
tag.AddAttachedPicture(frontCover)
tag.AddUnsynchronisedLyricsFrame(engUSLF)
tag.AddCommentFrame(engComm)
tag.AddFrame(unknownFrameID, unknownFrame)
buf := new(bytes.Buffer)
n, err := tag.WriteTo(buf)
if err != nil {
t.Fatalf("Error by writing: %v", err)
}
if n != int64(tag.Size()) {
t.Errorf("Expected WriteTo n==%v, got %v", tag.Size(), n)
}
if int64(buf.Len()) != n {
t.Errorf("buf.Len() and n are not equal: %v != %v ", buf.Len(), n)
}
}