-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressors_test.go
50 lines (40 loc) · 1.35 KB
/
compressors_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
package encryptedbox
import (
"testing"
"github.com/jswidler/encryptedbox/aesutil"
"github.com/stretchr/testify/assert"
)
func TestGzipCompression(t *testing.T) {
cipher := newCipher(t, aesutil.NewKey256, NewAESCipher)
compressorTest(t, cipher, Gzip)
}
func TestZlibCompression(t *testing.T) {
cipher := newCipher(t, aesutil.NewKey256, NewAESCipher)
compressorTest(t, cipher, Zlib)
}
func compressorTest(t *testing.T, cipher *Cipher, c Compressor) {
message := "a very compressible message "
for len(message) < 1000 {
message += message
}
// Encrypt without any compression
var out string
ciphertext, err := cipher.Encrypt(message)
assert.NoError(t, err)
err = cipher.Decrypt(ciphertext, &out)
assert.NoError(t, err)
assert.GreaterOrEqual(t, len(ciphertext), len(message),
"ciphertext must be at least as long as the original (via pigeonhole principle)")
assert.Equal(t, message, out)
// Turn on compression and check it is shorter
cipher.Compressor = c
out = ""
compressedCiphertext, err := cipher.Encrypt(message)
assert.NoError(t, err)
err = cipher.Decrypt(compressedCiphertext, &out)
assert.NoError(t, err)
assert.Less(t, len(compressedCiphertext), len(ciphertext))
assert.Equal(t, message, out)
t.Logf("message length: %d, encrypted length %d, encrypted with compression: %d",
len(message), len(ciphertext), len(compressedCiphertext))
}