-
Notifications
You must be signed in to change notification settings - Fork 0
/
picture_test.go
142 lines (126 loc) · 3.61 KB
/
picture_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
package flacpicture
import (
"archive/zip"
"bytes"
"fmt"
"io/ioutil"
"net/http"
"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 TestPNGPictureDecode(t *testing.T) {
imgdata, err := httpGetBytes("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png")
if err != nil {
t.Errorf("Error while downloading test file: %s", err.Error())
t.FailNow()
}
pic, err := NewFromImageData(PictureTypeArtist, "test description", imgdata, "image/png")
if err != nil {
t.Errorf("Error while constructing image data: %s", err.Error())
t.Fail()
}
if pic.MIME != "image/png" {
t.Errorf("MIME decode error: got %s", pic.MIME)
t.Fail()
}
if pic.Height != 600 || pic.Width != 800 {
t.Errorf("JPEG size error: got %dx%d", pic.Width, pic.Height)
t.Fail()
}
}
func TestJPEGPictureDecode(t *testing.T) {
imgdata, err := httpGetBytes("https://jpeg.org/images/jpeg-home.jpg")
if err != nil {
t.Errorf("Error while downloading test file: %s", err.Error())
t.FailNow()
}
pic, err := NewFromImageData(PictureTypeArtist, "test description", imgdata, "image/jpeg")
if err != nil {
t.Errorf("Error while constructing image data: %s", err.Error())
t.Fail()
}
if pic.MIME != "image/jpeg" {
t.Errorf("MIME decode error: got %s", pic.MIME)
t.Fail()
}
if pic.Height != 400 || pic.Width != 800 {
t.Errorf("JPEG size error: got %dx%d", pic.Width, pic.Height)
t.Fail()
}
}
func TestPictureModify(t *testing.T) {
imgdata, err := httpGetBytes("https://jpeg.org/images/jpeg-home.jpg")
if err != nil {
t.Errorf("Error while downloading test file: %s", err.Error())
t.FailNow()
}
pic, err := NewFromImageData(PictureTypeArtist, "test description", imgdata, "image/jpeg")
if err != nil {
t.Errorf("Error while constructing image data: %s", err.Error())
t.Fail()
}
pic.Description = "another description"
pic, err = ParseFromMetaDataBlock(pic.Marshal())
if err != nil {
t.Errorf("Error while parsing modified image data: %s", err.Error())
t.Fail()
}
if pic.Description != "another description" {
t.Errorf("description unepected: %s", pic.Description)
t.Fail()
}
}
func TestJPEGFromExistingFLAC(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 pic *MetadataBlockPicture
for _, meta := range f.Meta {
if meta.Type == flac.Picture {
pic, err = ParseFromMetaDataBlock(*meta)
if err != nil {
t.Errorf("Error while parsing metadata image: %s", err.Error())
t.Fail()
}
}
}
if pic.PictureType != PictureTypeFrontCover {
t.Error("Picture type does not match")
t.Fail()
}
if pic.MIME != "image/jpeg" {
t.Errorf("Picture MIME does not match: %s", pic.MIME)
t.Fail()
}
}