-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathzlib_compressor_test.go
More file actions
153 lines (135 loc) · 4.12 KB
/
zlib_compressor_test.go
File metadata and controls
153 lines (135 loc) · 4.12 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// -----------------------------------------------------------------------------
// github.com/balacode/udpt /[zlib_compressor_test.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package udpt
import (
"bytes"
"io"
"strings"
"testing"
)
// to run all tests in this file:
// go test -v -run Test_zlibCompressor_*
// -----------------------------------------------------------------------------
// must succeed compressing and uncompressing data:
func Test_zlibCompressor_1(t *testing.T) {
comp := zCompress(t)
zc := zlibCompressor{}
uncomp, err := zc.Uncompress(comp) // must succeed
if err != nil {
t.Error("0xE38FD2", err)
}
if !bytes.Equal(zInput(), uncomp) {
t.Error("0xEB0E80", "data corrupted")
}
}
// uncompressing less than 4 bytes must fail:
func Test_zlibCompressor_2(t *testing.T) {
zc := zlibCompressor{}
comp, err := zc.Uncompress([]byte{1, 2, 3})
if comp != nil {
t.Error("0xEE55FC")
}
if !matchError(err, "invalid 'comp'") {
t.Error("0xE4AB37", "wrong error:", err)
}
}
// uncompressing must fail when wr.Write() fails in compressDI():
func Test_zlibCompressor_3(t *testing.T) {
zc := zlibCompressor{}
cbuf := bytes.Buffer{}
wrc := &mockWriteCloser{failWrite: true}
comp, err := zc.compressDI(zInput(), wrc, &cbuf)
if comp != nil {
t.Error("0xE27BB2")
}
if !matchError(err, "failed mockWriteCloser.Write") {
t.Error("0xEE9C54", "wrong error:", err)
}
}
// uncompressing must fail when wr.Close() fails in compressDI():
func Test_zlibCompressor_4(t *testing.T) {
zc := zlibCompressor{}
cbuf := bytes.Buffer{}
wrc := &mockWriteCloser{failClose: true}
comp, err := zc.compressDI(zInput(), wrc, &cbuf)
if comp != nil {
t.Error("0xE6F6A5")
}
if !matchError(err, "failed mockWriteCloser.Close") {
t.Error("0xE4DF92", "wrong error:", err)
}
}
// uncompressing must fail when reader.Read or io.Copy fail in uncompressDI():
func Test_zlibCompressor_5(t *testing.T) {
comp, zc := zCompress(t), zlibCompressor{}
newMockReadCloser := func(io.Reader) (io.ReadCloser, error) {
return &mockReadCloser{failRead: true}, nil
}
uncomp, err := zc.uncompressDI(comp, newMockReadCloser)
if uncomp != nil {
t.Error("0xE3DA4F")
}
if !matchError(err, "failed mockReadCloser.Read") {
t.Error("0xE81C62", "wrong error:", err)
}
}
// uncompressing must fail when reader.Close() fails in uncompressDI():
func Test_zlibCompressor_6(t *testing.T) {
comp, zc := zCompress(t), zlibCompressor{}
newMockReadCloser := func(io.Reader) (io.ReadCloser, error) {
return &mockReadCloser{failClose: true}, nil
}
uncomp, err := zc.uncompressDI(comp, newMockReadCloser)
if uncomp != nil {
t.Error("0xEF3A01")
}
if !matchError(err, "failed mockReadCloser.Close") {
t.Error("0xEA0F76", "wrong error:", err)
}
}
// -----------------------------------------------------------------------------
// mockReadCloser is a mock io.ReadCloser with methods you can make fail.
type mockReadCloser struct {
failRead bool
failClose bool
}
// Read is a method of mockReadCloser implementing io.ReadCloser.
//
// You can make it return an error by setting mockReadCloser.failRead.
//
func (mk *mockReadCloser) Read(p []byte) (n int, err error) {
if mk.failRead {
return 0, makeError(0xEF8E54, "failed mockReadCloser.Read")
}
return len(p), nil
}
// Close is a method of mockReadCloser implementing io.ReadCloser.
//
// You can make it return an error by setting mockReadCloser.failClose.
//
func (mk *mockReadCloser) Close() error {
if mk.failClose {
return makeError(0xE1FB2C, "failed mockReadCloser.Close")
}
return nil
}
// -----------------------------------------------------------------------------
// zCompress compresses the string from zInput(): must always succeed
func zCompress(t *testing.T) []byte {
zc := zlibCompressor{}
input := zInput()
comp, err := zc.Compress(input)
if err != nil {
t.Error("0xE26CD5", err)
}
return comp
}
// zInput: provides a string to compress
func zInput() []byte {
return []byte(
strings.Repeat("The quick brown fox jumps over the lazy dog!", 7),
)
}
// end