tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/image/internal/compress/zlib/reader_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package zlib 6 7 import ( 8 "bytes" 9 "io" 10 "testing" 11 ) 12 13 type zlibTest struct { 14 desc string 15 raw string 16 compressed []byte 17 dict []byte 18 err error 19 } 20 21 // Compare-to-golden test data was generated by the ZLIB example program at 22 // https://www.zlib.net/zpipe.c 23 24 var zlibTests = []zlibTest{ 25 { 26 "truncated empty", 27 "", 28 []byte{}, 29 nil, 30 io.ErrUnexpectedEOF, 31 }, 32 { 33 "truncated dict", 34 "", 35 []byte{0x78, 0xbb}, 36 []byte{0x00}, 37 io.ErrUnexpectedEOF, 38 }, 39 { 40 "truncated checksum", 41 "", 42 []byte{0x78, 0xbb, 0x00, 0x01, 0x00, 0x01, 0xca, 0x48, 43 0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0x28, 0xcf, 0x2f, 44 0xca, 0x49, 0x01, 0x04, 0x00, 0x00, 0xff, 0xff, 45 }, 46 []byte{0x00}, 47 io.ErrUnexpectedEOF, 48 }, 49 { 50 "empty", 51 "", 52 []byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01}, 53 nil, 54 nil, 55 }, 56 { 57 "goodbye", 58 "goodbye, world", 59 []byte{ 60 0x78, 0x9c, 0x4b, 0xcf, 0xcf, 0x4f, 0x49, 0xaa, 61 0x4c, 0xd5, 0x51, 0x28, 0xcf, 0x2f, 0xca, 0x49, 62 0x01, 0x00, 0x28, 0xa5, 0x05, 0x5e, 63 }, 64 nil, 65 nil, 66 }, 67 { 68 "bad header", 69 "", 70 []byte{0x78, 0x9f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01}, 71 nil, 72 ErrHeader, 73 }, 74 { 75 "bad checksum", 76 "", 77 []byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0xff}, 78 nil, 79 ErrChecksum, 80 }, 81 { 82 "not enough data", 83 "", 84 []byte{0x78, 0x9c, 0x03, 0x00, 0x00, 0x00}, 85 nil, 86 io.ErrUnexpectedEOF, 87 }, 88 { 89 "excess data is silently ignored", 90 "", 91 []byte{ 92 0x78, 0x9c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 93 0x78, 0x9c, 0xff, 94 }, 95 nil, 96 nil, 97 }, 98 { 99 "dictionary", 100 "Hello, World!\n", 101 []byte{ 102 0x78, 0xbb, 0x1c, 0x32, 0x04, 0x27, 0xf3, 0x00, 103 0xb1, 0x75, 0x20, 0x1c, 0x45, 0x2e, 0x00, 0x24, 104 0x12, 0x04, 0x74, 105 }, 106 []byte{ 107 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0a, 108 }, 109 nil, 110 }, 111 { 112 "wrong dictionary", 113 "", 114 []byte{ 115 0x78, 0xbb, 0x1c, 0x32, 0x04, 0x27, 0xf3, 0x00, 116 0xb1, 0x75, 0x20, 0x1c, 0x45, 0x2e, 0x00, 0x24, 117 0x12, 0x04, 0x74, 118 }, 119 []byte{ 120 0x48, 0x65, 0x6c, 0x6c, 121 }, 122 ErrDictionary, 123 }, 124 { 125 "truncated zlib stream amid raw-block", 126 "hello", 127 []byte{ 128 0x78, 0x9c, 0x00, 0x0c, 0x00, 0xf3, 0xff, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 129 }, 130 nil, 131 io.ErrUnexpectedEOF, 132 }, 133 { 134 "truncated zlib stream amid fixed-block", 135 "He", 136 []byte{ 137 0x78, 0x9c, 0xf2, 0x48, 0xcd, 138 }, 139 nil, 140 io.ErrUnexpectedEOF, 141 }, 142 } 143 144 func TestDecompressor(t *testing.T) { 145 b := new(bytes.Buffer) 146 for _, tt := range zlibTests { 147 in := bytes.NewReader(tt.compressed) 148 zr, err := NewReaderDict(in, tt.dict) 149 if err != nil { 150 if err != tt.err { 151 t.Errorf("%s: NewReader: %s", tt.desc, err) 152 } 153 continue 154 } 155 defer zr.Close() 156 157 // Read and verify correctness of data. 158 b.Reset() 159 n, err := io.Copy(b, zr) 160 if err != nil { 161 if err != tt.err { 162 t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err) 163 } 164 continue 165 } 166 s := b.String() 167 if s != tt.raw { 168 t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw) 169 } 170 171 // Check for sticky errors. 172 if n, err := zr.Read([]byte{0}); n != 0 || err != io.EOF { 173 t.Errorf("%s: Read() = (%d, %v), want (0, io.EOF)", tt.desc, n, err) 174 } 175 if err := zr.Close(); err != nil { 176 t.Errorf("%s: Close() = %v, want nil", tt.desc, err) 177 } 178 } 179 }