forked from DataDog/golz4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_test.go
209 lines (187 loc) · 5.09 KB
/
header_test.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package lz4
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"strconv"
"strings"
"testing"
"time"
)
// test extended compression/decompression w/ headers
func TestCompressHdrRatio(t *testing.T) {
input, err := ioutil.ReadFile(sampleFilePath)
if err != nil {
t.Fatal(err)
}
output := make([]byte, CompressBoundHdr(input))
outSize, err := CompressHdr(output, input)
if err != nil {
t.Fatal(err)
}
outputNoHdr := make([]byte, CompressBoundHdr(input))
outNoHdrSize, err := Compress(outputNoHdr, input)
if err != nil {
t.Fatal(err)
}
if outSize != outNoHdrSize+4 {
t.Fatalf("Compressed output length != expected: %d != %d", outSize, outNoHdrSize+4)
}
}
func TestCompressionHdr(t *testing.T) {
input := []byte(strings.Repeat("Hello world, this is quite something", 10))
output := make([]byte, CompressBoundHdr(input))
outSize, err := CompressHdr(output, input)
if err != nil {
t.Fatalf("Compression failed: %v", err)
}
if outSize == 0 {
t.Fatal("Output buffer is empty.")
}
output = output[:outSize]
decompressed, err := UncompressAllocHdr(nil, output)
if err != nil {
t.Fatalf("Decompression failed: %v", err)
}
if string(decompressed) != string(input) {
t.Fatalf("Decompressed output != input: %q != %q", decompressed, input)
}
}
func TestEmptyCompressionHdr(t *testing.T) {
input := []byte("")
output := make([]byte, CompressBoundHdr(input))
outSize, err := CompressHdr(output, input)
if err != nil {
t.Fatalf("Compression failed: %v", err)
}
if outSize == 0 {
t.Fatal("Output buffer is empty.")
}
output = output[:outSize]
decompressed := make([]byte, len(input))
err = UncompressHdr(decompressed, output)
if err != nil {
t.Fatalf("Decompression failed: %v", err)
}
if string(decompressed) != string(input) {
t.Fatalf("Decompressed output != input: %q != %q", decompressed, input)
}
}
func TestUncompressHdrShort(t *testing.T) {
// calling Uncompress(Alloc)Hdr with input that is too short should return an error, not panic
output := make([]byte, 1)
for i := 0; i < 4; i++ {
tooShortInput := make([]byte, i)
out2, err := UncompressAllocHdr(output, tooShortInput)
if err != errTooShort {
t.Errorf("UncompressAllocHdr(output, [%d zero bytes]) returned unexpected err=%v",
len(tooShortInput), err)
}
// UncompressAllocHdr should always returns its first argument
// sadly slice identity is hard; cheat with Sprintf("%p")
if fmt.Sprintf("%p", out2) != fmt.Sprintf("%p", output) {
t.Errorf("UncompressAllocHdr([%p], [%d zero bytes]) returned output=%p",
output, len(tooShortInput), out2)
}
err = UncompressHdr(output, tooShortInput)
if err != errTooShort {
t.Errorf("UncompressHdr(output, [%d zero bytes]) returned unexpected err=%v",
len(tooShortInput), err)
}
}
}
func TestCompressAllocHdr(t *testing.T) {
// test compressing a set of random sized inputs
inBuf := make([]byte, 70*1024)
for i := range inBuf {
inBuf[i] = byte(i)
}
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 1000; i++ {
inSize := rng.Intn(len(inBuf))
compressed, err := CompressAllocHdr(inBuf[:inSize])
if err != nil {
t.Fatal(err)
}
uncompressed, err := UncompressAllocHdr(nil, compressed)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(uncompressed, inBuf[:inSize]) {
t.Fatal("uncompressed != input")
}
}
}
// test python interoperability
// pymod returns whether or not a python module is importable. For checking
// whether or not we can test the python lz4 interop
func pymod(module string) bool {
cmd := exec.Command("python3", "-c", fmt.Sprintf("import %s", module))
err := cmd.Run()
if err != nil {
return false
}
return cmd.ProcessState.Success()
}
func TestPythonIntegration(t *testing.T) {
if !pymod("os") {
t.Errorf("pymod could not find 'os' module")
}
if pymod("faojfeiajwofe") {
t.Errorf("pymod found non-existent junk module")
}
}
func TestPythonInterop(t *testing.T) {
pycompat := pymod("lz4.block")
if !pycompat {
t.Log("Warning: not testing python module compat: no module lz4 found")
t.Skip()
return
}
corpus, err := ioutil.ReadFile(sampleFilePath)
if err != nil {
t.Fatal(err)
}
out := make([]byte, CompressBoundHdr(corpus))
count, err := CompressHdr(out, corpus)
if err != nil {
t.Fatal(err)
}
out = out[:count]
dst := "/tmp/lz4test.z"
err = ioutil.WriteFile(dst, out, 0644)
if err != nil {
t.Fatal(err)
}
defer os.Remove(dst)
err = pythonLz4Compat(dst, len(corpus))
if err != nil {
t.Fatal(err)
}
}
// given the original length of an lz4 encoded file, check that the python
// lz4 library returns the correct length.
func pythonLz4Compat(path string, length int) error {
var out bytes.Buffer
cmd := exec.Command("python3", "-c", fmt.Sprintf(`import lz4.block; print(len(lz4.block.decompress(open("%s", "rb").read())))`, path))
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
output := out.String()
if err != nil {
return errors.New(output)
}
output = strings.Trim(output, "\n")
l, err := strconv.Atoi(output)
if err != nil {
return err
}
if l == length {
return nil
}
return fmt.Errorf("Expected length %d, got %d", length, l)
}