github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/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 // http://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 126 func TestDecompressor(t *testing.T) { 127 b := new(bytes.Buffer) 128 for _, tt := range zlibTests { 129 in := bytes.NewReader(tt.compressed) 130 zlib, err := NewReaderDict(in, tt.dict) 131 if err != nil { 132 if err != tt.err { 133 t.Errorf("%s: NewReader: %s", tt.desc, err) 134 } 135 continue 136 } 137 defer zlib.Close() 138 b.Reset() 139 n, err := io.Copy(b, zlib) 140 if err != nil { 141 if err != tt.err { 142 t.Errorf("%s: io.Copy: %v want %v", tt.desc, err, tt.err) 143 } 144 continue 145 } 146 s := b.String() 147 if s != tt.raw { 148 t.Errorf("%s: got %d-byte %q want %d-byte %q", tt.desc, n, s, len(tt.raw), tt.raw) 149 } 150 } 151 }