-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto_test.go
More file actions
101 lines (86 loc) · 1.84 KB
/
Copy pathcrypto_test.go
File metadata and controls
101 lines (86 loc) · 1.84 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
package main
import (
"bytes"
"crypto/rand"
"io/ioutil"
"os"
"testing"
)
func TestIntegration(t *testing.T) {
defer gc()
key, err := ioutil.TempFile("/tmp", "tulum-")
if err != nil {
t.Fatal(err)
}
key.Close()
// We just need a name, but the file should not exist. We'll
// create it elsewhere.
os.Remove(key.Name())
defer os.Remove(key.Name())
plaintext, err := randBytes(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
ptBuf := bytes.NewBuffer(plaintext)
plaintext = ptBuf.Bytes()
ct := &bytes.Buffer{}
if err := encrypt(ptBuf, ct, rand.Reader, key.Name()); err != nil {
t.Fatal(err)
}
if bytes.Equal(ct.Bytes(), plaintext) {
t.Fatal("ciphertext should not equal plaintext")
}
newPT := &bytes.Buffer{}
if err := decrypt(ct, newPT, key.Name()); err != nil {
t.Fatal(err)
}
if !bytes.Equal(newPT.Bytes(), plaintext) {
t.Fatal("decrypted plaintext should match original plaintext")
}
info, err := os.Stat(key.Name())
if err != nil {
t.Fatal(err)
}
if info.Mode() != keyAttributes {
t.Fatal("invalid mode")
}
}
func TestRandBytes(t *testing.T) {
b1, err := randBytes(rand.Reader, 32)
if err != nil {
t.Fatal(err)
}
b2, err := randBytes(rand.Reader, 32)
if err != nil {
t.Fatal(err)
}
t.Logf("%x", b1)
t.Logf("%x", b2)
if bytes.Equal(b1, b2) {
t.Fatal("rand bytes should not be equal")
}
}
func TestDeriveKeys(t *testing.T) {
s1 := []byte{1}
s2 := []byte{2}
ks1, err := deriveKeys(s1)
if err != nil {
t.Fatal(err)
}
ks2, err := deriveKeys(s2)
if err != nil {
t.Fatal(err)
}
if len(ks1.EncKey) != int(encKeySize) {
t.Fatal("invalid enc key size")
}
if len(ks1.MACKey) != int(hashSize) {
t.Fatal("invalid mac key size")
}
if bytes.Equal(ks1.EncKey, ks2.EncKey) {
t.Fatal("keys should not match")
}
if bytes.Equal(ks1.MACKey, ks2.MACKey) {
t.Fatal("keys should not match")
}
}