-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwriter_test.go
252 lines (215 loc) · 6.54 KB
/
writer_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
package metaflac
import (
"bytes"
"encoding/binary"
"encoding/hex"
"testing"
)
func TestNewMetaflac(t *testing.T) {
// Generate a minimal FLAC buffer
flacData := generateMinimalFlac()
// Initialize Metaflac
m, err := NewMetaflac(flacData)
if err != nil {
t.Fatalf("Failed to initialize Metaflac: %v", err)
}
if m == nil {
t.Fatal("Metaflac instance is nil")
}
}
func TestParseVorbisComment(t *testing.T) {
// Prepare a sample Vorbis comment block
vendorString := "test vendor"
comments := []string{"TITLE=Test Song", "ARTIST=Test Artist"}
vorbisCommentBlock := formatVorbisComment(vendorString, comments)
// Initialize Metaflac with the Vorbis comment block
m := &Metaflac{
vorbisComment: vorbisCommentBlock,
}
err := m.parseVorbisComment()
if err != nil {
t.Fatalf("Failed to parse Vorbis comment: %v", err)
}
if m.GetVendorTag() != vendorString {
t.Errorf("Expected vendor string '%s', got '%s'", vendorString, m.GetVendorTag())
}
if !equalStringSlices(m.GetAllTags(), comments) {
t.Errorf("Expected tags %v, got %v", comments, m.GetAllTags())
}
}
func TestGetMd5sum(t *testing.T) {
// Prepare a sample STREAMINFO block with a known MD5 sum
md5sumBytes, _ := hex.DecodeString("0123456789abcdef0123456789abcdef")
streamInfo := make([]byte, 34)
copy(streamInfo[18:], md5sumBytes)
m := &Metaflac{
streamInfo: streamInfo,
}
md5sum := m.GetMd5sum()
if md5sum != "0123456789abcdef0123456789abcdef" {
t.Errorf("Expected MD5 sum '0123456789abcdef0123456789abcdef', got '%s'", md5sum)
}
}
func TestTagOperations(t *testing.T) {
m := &Metaflac{
tags: []string{"TITLE=Test Song", "ARTIST=Test Artist", "ALBUM=Test Album"},
}
// Test GetTag
artistTags := m.GetTag("ARTIST")
if len(artistTags) != 1 || artistTags[0] != "ARTIST=Test Artist" {
t.Errorf("Expected 'ARTIST=Test Artist', got %v", artistTags)
}
// Test RemoveTag
m.RemoveTag("ARTIST")
if len(m.GetTag("ARTIST")) != 0 {
t.Error("ARTIST tag was not removed")
}
// Test RemoveFirstTag
m.SetTag("GENRE=Rock")
m.SetTag("GENRE=Pop")
m.RemoveFirstTag("GENRE")
genreTags := m.GetTag("GENRE")
if len(genreTags) != 1 || genreTags[0] != "GENRE=Pop" {
t.Errorf("Expected 'GENRE=Pop', got %v", genreTags)
}
// Test RemoveAllTags
m.RemoveAllTags()
if len(m.GetAllTags()) != 0 {
t.Error("All tags were not removed")
}
// Test SetTag
err := m.SetTag("YEAR=2021")
if err != nil {
t.Errorf("Failed to set tag: %v", err)
}
if len(m.GetAllTags()) != 1 || m.GetAllTags()[0] != "YEAR=2021" {
t.Errorf("Expected 'YEAR=2021', got %v", m.GetAllTags())
}
}
func TestImportPicture(t *testing.T) {
// Prepare sample picture data
pictureData := []byte{0xFF, 0xD8, 0xFF} // Start of a JPEG file
spec := PictureSpec{
Type: 3,
Mime: "image/jpeg",
Description: "Cover Art",
Width: 600,
Height: 600,
Depth: 24,
Colors: 0,
}
m := &Metaflac{}
m.ImportPicture(pictureData, spec)
if len(m.GetPicturesSpecs()) != 1 {
t.Error("Picture was not imported correctly")
}
if !comparePictureSpec(m.GetPicturesSpecs()[0], spec) {
t.Errorf("Expected picture spec %v, got %v", spec, m.GetPicturesSpecs()[0])
}
}
func TestBuildPictureBlock(t *testing.T) {
pictureData := []byte{0xFF, 0xD8, 0xFF}
spec := PictureSpec{
Type: 3,
Mime: "image/jpeg",
Description: "Cover Art",
Width: 600,
Height: 600,
Depth: 24,
Colors: 0,
}
m := &Metaflac{}
pictureBlock := m.buildPictureBlock(pictureData, spec)
// Expected structure:
// [Type][Mime Length][Mime][Description Length][Description]
// [Width][Height][Depth][Colors][Picture Data Length][Picture Data]
var buffer bytes.Buffer
binary.Write(&buffer, binary.BigEndian, spec.Type)
binary.Write(&buffer, binary.BigEndian, uint32(len(spec.Mime)))
buffer.WriteString(spec.Mime)
binary.Write(&buffer, binary.BigEndian, uint32(len(spec.Description)))
buffer.WriteString(spec.Description)
binary.Write(&buffer, binary.BigEndian, spec.Width)
binary.Write(&buffer, binary.BigEndian, spec.Height)
binary.Write(&buffer, binary.BigEndian, spec.Depth)
binary.Write(&buffer, binary.BigEndian, spec.Colors)
binary.Write(&buffer, binary.BigEndian, uint32(len(pictureData)))
buffer.Write(pictureData)
expected := buffer.Bytes()
if !bytes.Equal(pictureBlock, expected) {
t.Error("Picture block was not built correctly")
}
}
func TestBuildMetadata(t *testing.T) {
m := &Metaflac{
streamInfo: []byte{0x00, 0x01},
blocks: []Block{{BlockType: 3, Data: []byte{0x02, 0x03}}},
tags: []string{"TITLE=Test Song"},
vendorString: "test vendor",
pictures: [][]byte{{0x04, 0x05}},
picturesSpecs: []PictureSpec{{
Type: 3, Mime: "image/jpeg",
}},
}
metadata := m.buildMetadata()
if len(metadata) != 5 {
t.Errorf("Expected 5 metadata blocks, got %d", len(metadata))
}
// Verify STREAMINFO block
streamInfoBlock := metadata[0]
if streamInfoBlock[0]&0x7F != STREAMINFO {
t.Error("First metadata block is not STREAMINFO")
}
// Verify VORBIS_COMMENT block
vorbisCommentBlock := metadata[2]
if vorbisCommentBlock[0]&0x7F != VORBIS_COMMENT {
t.Error("Third metadata block is not VORBIS_COMMENT")
}
// Verify PICTURE block
pictureBlock := metadata[3]
if pictureBlock[0]&0x7F != PICTURE {
t.Error("Fourth metadata block is not PICTURE")
}
// Verify PADDING block
paddingBlock := metadata[4]
if paddingBlock[0]&0x7F != PADDING {
t.Error("Last metadata block is not PADDING")
}
if paddingBlock[0]&0x80 == 0 {
t.Error("PADDING block is not marked as last")
}
}
func TestBuildStream(t *testing.T) {
originalData := generateMinimalFlac()
m, err := NewMetaflac(originalData)
if err != nil {
t.Fatalf("Failed to initialize Metaflac: %v", err)
}
m.streamInfo = []byte{0x00, 0x01}
m.tags = []string{"TITLE=Test Song"}
m.vendorString = "test vendor"
newStream := m.buildStream()
if !bytes.HasPrefix(newStream, []byte("fLaC")) {
t.Error("FLAC marker not present at the beginning of the stream")
}
if !bytes.Contains(newStream, []byte("Test Song")) {
t.Error("Metadata not included in the stream")
}
}
func TestGetBuffer(t *testing.T) {
originalData := generateMinimalFlac()
m, err := NewMetaflac(originalData)
if err != nil {
t.Fatalf("Failed to initialize Metaflac: %v", err)
}
m.streamInfo = []byte{0x00, 0x01}
m.tags = []string{"TITLE=Test Song"}
m.vendorString = "test vendor"
newBuffer := m.GetBuffer()
if !bytes.HasPrefix(newBuffer, []byte("fLaC")) {
t.Error("FLAC marker not present at the beginning of the buffer")
}
if !bytes.Contains(newBuffer, []byte("Test Song")) {
t.Error("Metadata not included in the buffer")
}
}