1 // Copyright 2010 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.
16 // pipe creates two ends of a pipe that gzip and gunzip, and runs dfunc at the
17 // writer end and cfunc at the reader end.
18 func pipe(t *testing.T, dfunc func(*Compressor), cfunc func(*Decompressor)) {
19 piper, pipew := io.Pipe()
23 compressor, err := NewWriter(pipew)
27 defer compressor.Close()
30 decompressor, err := NewReader(piper)
34 defer decompressor.Close()
38 // Tests that an empty payload still forms a valid GZIP stream.
39 func TestEmpty(t *testing.T) {
41 func(compressor *Compressor) {},
42 func(decompressor *Decompressor) {
43 b, err := ioutil.ReadAll(decompressor)
48 t.Fatalf("did not read an empty slice")
53 // Tests that gzipping and then gunzipping is the identity function.
54 func TestWriter(t *testing.T) {
56 func(compressor *Compressor) {
57 compressor.Comment = "Äußerung"
58 //compressor.Comment = "comment"
59 compressor.Extra = []byte("extra")
60 compressor.ModTime = time.Unix(1e8, 0)
61 compressor.Name = "name"
62 _, err := compressor.Write([]byte("payload"))
67 func(decompressor *Decompressor) {
68 b, err := ioutil.ReadAll(decompressor)
72 if string(b) != "payload" {
73 t.Fatalf("payload is %q, want %q", string(b), "payload")
75 if decompressor.Comment != "Äußerung" {
76 t.Fatalf("comment is %q, want %q", decompressor.Comment, "Äußerung")
78 if string(decompressor.Extra) != "extra" {
79 t.Fatalf("extra is %q, want %q", decompressor.Extra, "extra")
81 if decompressor.ModTime.Unix() != 1e8 {
82 t.Fatalf("mtime is %d, want %d", decompressor.ModTime.Unix(), uint32(1e8))
84 if decompressor.Name != "name" {
85 t.Fatalf("name is %q, want %q", decompressor.Name, "name")
90 func TestLatin1(t *testing.T) {
91 latin1 := []byte{0xc4, 'u', 0xdf, 'e', 'r', 'u', 'n', 'g', 0}
93 z := Decompressor{r: bufio.NewReader(bytes.NewBuffer(latin1))}
94 s, err := z.readString()
99 t.Fatalf("string is %q, want %q", s, utf8)
102 buf := bytes.NewBuffer(make([]byte, 0, len(latin1)))
103 c := Compressor{w: buf}
104 if err = c.writeString(utf8); err != nil {
108 if s != string(latin1) {
109 t.Fatalf("string is %v, want %v", s, latin1)
111 //if s, err = buf.ReadString(0); err != nil {
112 //t.Fatalf("%v", err)