// The current row for y is the previous row for y+1.
pr, cr = cr, pr
}
+
+ // Check for EOF, to verify the zlib checksum.
+ n, err := r.Read(pr[:1])
+ if err != os.EOF {
+ return nil, FormatError(err.String())
+ }
+ if n != 0 {
+ return nil, FormatError("too much pixel data")
+ }
+
return img, nil
}
"image"
"io"
"os"
+ "strings"
"testing"
)
"basn6a16",
}
-func readPng(filename string) (image.Image, os.Error) {
+func readPNG(filename string) (image.Image, os.Error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
for _, fn := range names {
// Read the .png file.
- img, err := readPng("testdata/pngsuite/" + fn + ".png")
+ img, err := readPNG("testdata/pngsuite/" + fn + ".png")
if err != nil {
t.Error(fn, err)
continue
}
}
}
+
+var readerErrors = []struct {
+ file string
+ err string
+}{
+ {"invalid-zlib.png", "zlib checksum error"},
+ {"invalid-crc32.png", "invalid checksum"},
+ {"invalid-noend.png", "unexpected EOF"},
+ {"invalid-trunc.png", "unexpected EOF"},
+}
+
+func TestReaderError(t *testing.T) {
+ for _, tt := range readerErrors {
+ img, err := readPNG("testdata/" + tt.file)
+ if err == nil {
+ t.Errorf("decoding %s: missing error", tt.file)
+ continue
+ }
+ if !strings.Contains(err.String(), tt.err) {
+ t.Errorf("decoding %s: %s, want %s", tt.file, err, tt.err)
+ }
+ if img != nil {
+ t.Errorf("decoding %s: have image + error")
+ }
+ }
+}
for _, fn := range names {
qfn := "testdata/pngsuite/" + fn + ".png"
// Read the image.
- m0, err := readPng(qfn)
+ m0, err := readPNG(qfn)
if err != nil {
t.Error(fn, err)
continue
}
// Read the image again, encode it, and decode it.
- m1, err := readPng(qfn)
+ m1, err := readPNG(qfn)
if err != nil {
t.Error(fn, err)
return