-
Notifications
You must be signed in to change notification settings - Fork 1
/
id3_test.go
191 lines (157 loc) · 3.83 KB
/
id3_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
package id3
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
)
func TestBaseFile(t *testing.T) {
if os.Getenv("INT") != "1" {
t.SkipNow()
}
filename := os.Getenv("FILENAME")
f := &File{Filename: filename, Debug: true}
handle, err := os.Open(f.Filename)
if err != nil {
t.Fatalf("Unable to open file, %#v", err)
}
defer func() {
if a := recover(); a != nil {
t.Fatal(a)
}
}()
f.Process(handle)
}
func TestNoFile(t *testing.T) {
b := &tfile{}
b.Write([]byte("ID3\x03\x00\x00\x00\x00\x00\x17" +
"TPE2\x00\x00\x00\x0d\x00\x00\x00Cult of Luna" +
"TAGBob is great " +
"Bob " +
"Bobbum " +
"2016" +
"This is just a comment here " +
"01\x01"))
f := &File{Debug: false}
f.Process(b)
expected := "Artist: Cult of Luna\nAlbum: Bobbum\nTitle: Bob is great\n"
var o bytes.Buffer
f.PrettyPrint(&o, "text")
if expected != o.String() {
t.Fatalf("Got [%s] but expected [%s]", o.String(), expected)
}
}
func TestNoV2(t *testing.T) {
b := &tfile{}
b.Write([]byte("TAGBob is great " +
"Bob " +
"Bobbum " +
"2016" +
"This is just a comment here " +
"01\x01"))
f := &File{Debug: false}
f.Process(b)
expected := "Artist: Bob\nAlbum: Bobbum\nTitle: Bob is great\n"
var o bytes.Buffer
f.PrettyPrint(&o, "text")
if expected != o.String() {
t.Fatalf("Got [%s] but expected [%s]", o.String(), expected)
}
}
func TestNoV2Json(t *testing.T) {
b := &tfile{}
b.Write([]byte("TAGBob is great " +
"Bob " +
"Bobbum " +
"2016" +
"This is just a comment here " +
"01\x01"))
f := &File{Debug: false}
f.Process(b)
expected := `{"filename":"","id3v1":{"artist":"Bob","title":"Bob is great","album":"Bobbum","year":2016,` +
`"comment":"This is just a comment here","track":1,"genre":0},"id3v2":{"frames":[],"major_version":0,` +
`"min_version":0,"flag":0,"tag_size":0,"unsynchronised":false,"extended":false,"experimental":false,` +
`"footer":false,"extended_size":0,"extended_flag":null,"extended_padding":0,"crc":false,"crc_content":null}}`
var o bytes.Buffer
f.PrettyPrint(&o, "json")
found := strings.TrimRight(o.String(), "\n")
if expected != found {
t.Fatalf("Got [%s] but expected [%s]", found, expected)
}
}
func TestNoV2Yaml(t *testing.T) {
b := &tfile{}
b.Write([]byte("TAGBob is great " +
"Bob " +
"Bobbum " +
"2016" +
"This is just a comment here " +
"01\x01"))
f := &File{Debug: false}
f.Process(b)
expected := `filename: ""
v1:
artist: Bob
title: Bob is great
album: Bobbum
year: 2016
comment: This is just a comment here
track: 1
genre: 0
debug: false
v2:
frames: []
major: 0
min: 0
flag: 0
size: 0
unsynchronised: false
extended: false
experimental: false
footer: false
extended_size: 0
extended_flag: []
extended_padding: 0
crc: false
crc_content: []
debug: false
debug: false`
var o bytes.Buffer
f.PrettyPrint(&o, "yaml")
found := strings.TrimRight(o.String(), "\n")
if expected != found {
t.Fatalf("Got [%s] but expected [%s]", found, expected)
}
}
type tfile struct {
seekVal int
buf *bytes.Buffer
}
func (t *tfile) Seek(o int64, w int) (int64, error) {
t.seekVal = w
return 0, nil
}
func (t *tfile) Close() error {
return nil
}
func (t *tfile) Read(b []byte) (int, error) {
if t.seekVal == v1TagLocation {
length := t.buf.Len()
fmt.Println(length, len(b))
if length > v1TagSize {
r := bytes.NewReader(t.buf.Bytes())
r.ReadAt(b, int64(length-v1TagSize))
} else {
t.buf.Read(b)
}
t.seekVal = 0
} else {
t.buf.Read(b)
}
return len(b), nil
}
func (t *tfile) Write(b []byte) (int, error) {
t.buf = bytes.NewBuffer(b)
return t.buf.Len(), nil
}