-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvorbis_test.go
126 lines (113 loc) · 2.88 KB
/
vorbis_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
package flacvorbis
import (
"archive/zip"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
flac "github.com/go-flac/go-flac"
)
func httpGetBytes(url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP status %d", res.StatusCode)
}
return ioutil.ReadAll(res.Body)
}
func TestNewVorbisComment(t *testing.T) {
cmt := New()
if !strings.HasPrefix(cmt.Vendor, "flacvorbis") {
t.Errorf("Unexpected vendor: %s\n", cmt.Vendor)
t.Fail()
}
if len(cmt.Comments) != 0 {
t.Error("Unexpected comments: ", cmt.Comments)
t.Fail()
}
}
func TestVorbisFromExistingFlac(t *testing.T) {
zipdata, err := httpGetBytes("http://helpguide.sony.net/high-res/sample1/v1/data/Sample_BeeMoved_96kHz24bit.flac.zip")
if err != nil {
t.Errorf("Error while downloading test file: %s", err.Error())
t.FailNow()
}
zipfile, err := zip.NewReader(bytes.NewReader(zipdata), int64(len(zipdata)))
if err != nil {
t.Errorf("Error while decompressing test file: %s", err.Error())
t.FailNow()
}
if zipfile.File[0].Name != "Sample_BeeMoved_96kHz24bit.flac" {
t.Errorf("Unexpected test file content: %s", zipfile.File[0].Name)
t.FailNow()
}
flachandle, err := zipfile.File[0].Open()
if err != nil {
t.Errorf("Failed to decompress test file: %s", err)
t.FailNow()
}
f, err := flac.ParseBytes(flachandle)
if err != nil {
t.Errorf("Failed to parse flac file: %s", err)
t.FailNow()
}
var cmt *MetaDataBlockVorbisComment
for _, meta := range f.Meta {
if meta.Type == flac.VorbisComment {
cmt, err = ParseFromMetaDataBlock(*meta)
if err != nil {
t.Errorf("Error while parsing metadata image: %s\n", err.Error())
t.Fail()
}
}
}
if err := cmt.Add(FIELD_GENRE, "Bee Pop"); err != nil {
t.Error(err)
t.Fail()
}
check := func(cmt *MetaDataBlockVorbisComment) {
if cmt.Vendor != "reference libFLAC 1.2.1 win64 20080709" {
t.Errorf("Unexpected vendor string: %s\n", cmt.Vendor)
t.Fail()
}
if res, err := cmt.Get(FIELD_ALBUM); err != nil {
t.Error(err)
t.Fail()
} else if len(res) != 1 || res[0] != "Bee Moved" {
t.Error("Unexpected album name: ", res)
t.Fail()
}
if res, err := cmt.Get(FIELD_ARTIST); err != nil {
t.Error(err)
t.Fail()
} else if len(res) != 1 || res[0] != "Blue Monday FM" {
t.Error("Unexpected artist name: ", res)
t.Fail()
}
if res, err := cmt.Get(FIELD_TITLE); err != nil {
t.Error(err)
t.Fail()
} else if len(res) != 1 || res[0] != "Bee Moved" {
t.Error("Unexpected title name: ", res)
t.Fail()
}
if res, err := cmt.Get(FIELD_GENRE); err != nil {
t.Error(err)
t.Fail()
} else if len(res) != 1 || res[0] != "Bee Pop" {
t.Error("Unexpected title name: ", res)
t.Fail()
}
}
check(cmt)
new, err := ParseFromMetaDataBlock(cmt.Marshal())
if err != nil {
t.Error(err)
t.Fail()
}
check(new)
}