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
|
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gzip
import (
"io"
"io/ioutil"
"testing"
)
// pipe creates two ends of a pipe that gzip and gunzip, and runs dfunc at the
// writer end and ifunc at the reader end.
func pipe(t *testing.T, dfunc func(*Compressor), cfunc func(*Decompressor)) {
piper, pipew := io.Pipe()
defer piper.Close()
go func() {
defer pipew.Close()
compressor, err := NewWriter(pipew)
if err != nil {
t.Fatalf("%v", err)
}
defer compressor.Close()
dfunc(compressor)
}()
decompressor, err := NewReader(piper)
if err != nil {
t.Fatalf("%v", err)
}
defer decompressor.Close()
cfunc(decompressor)
}
// Tests that an empty payload still forms a valid GZIP stream.
func TestEmpty(t *testing.T) {
pipe(t,
func(compressor *Compressor) {},
func(decompressor *Decompressor) {
b, err := ioutil.ReadAll(decompressor)
if err != nil {
t.Fatalf("%v", err)
}
if len(b) != 0 {
t.Fatalf("did not read an empty slice")
}
})
}
// Tests that gzipping and then gunzipping is the identity function.
func TestWriter(t *testing.T) {
pipe(t,
func(compressor *Compressor) {
compressor.Comment = "comment"
compressor.Extra = []byte("extra")
compressor.Mtime = 1e8
compressor.Name = "name"
_, err := compressor.Write([]byte("payload"))
if err != nil {
t.Fatalf("%v", err)
}
},
func(decompressor *Decompressor) {
b, err := ioutil.ReadAll(decompressor)
if err != nil {
t.Fatalf("%v", err)
}
if string(b) != "payload" {
t.Fatalf("payload is %q, want %q", string(b), "payload")
}
if decompressor.Comment != "comment" {
t.Fatalf("comment is %q, want %q", decompressor.Comment, "comment")
}
if string(decompressor.Extra) != "extra" {
t.Fatalf("extra is %q, want %q", decompressor.Extra, "extra")
}
if decompressor.Mtime != 1e8 {
t.Fatalf("mtime is %d, want %d", decompressor.Mtime, uint32(1e8))
}
if decompressor.Name != "name" {
t.Fatalf("name is %q, want %q", decompressor.Name, "name")
}
})
}
|