forked from awnumar/memguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enclave_test.go
91 lines (85 loc) · 1.75 KB
/
enclave_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
package memguard
import (
"bytes"
"testing"
"github.com/awnumar/memguard/core"
)
func TestNewEnclave(t *testing.T) {
e := NewEnclave([]byte("yellow submarine"))
if e == nil {
t.Error("got nil enclave")
}
data, err := e.Open()
if err != nil {
t.Error("unexpected error:", err)
}
if !bytes.Equal(data.Bytes(), []byte("yellow submarine")) {
t.Error("data doesn't match input")
}
data.Destroy()
e = NewEnclave([]byte{})
if e != nil {
t.Error("enclave should be nil")
}
}
func TestNewEnclaveRandom(t *testing.T) {
e := NewEnclaveRandom(32)
if e == nil {
t.Error("got nil enclave")
}
data, err := e.Open()
if err != nil {
t.Error("unexpected error:", err)
}
if len(data.Bytes()) != 32 || cap(data.Bytes()) != 32 {
t.Error("buffer sizes incorrect")
}
if bytes.Equal(data.Bytes(), make([]byte, 32)) {
t.Error("buffer not randomised")
}
data.Destroy()
e = NewEnclaveRandom(0)
if e != nil {
t.Error("should be nil")
}
}
func TestOpen(t *testing.T) {
e := NewEnclave([]byte("yellow submarine"))
if e == nil {
t.Error("got nil enclave")
}
b, err := e.Open()
if err != nil {
t.Error("unexpected error;", err)
}
if b == nil {
t.Error("buffer should not be nil")
}
if e.Size() != b.Size() {
t.Error("sizes don't match")
}
if !bytes.Equal(b.Bytes(), []byte("yellow submarine")) {
t.Error("data does not match")
}
Purge() // reset the session
b, err = e.Open()
if err != core.ErrDecryptionFailed {
t.Error("expected decryption error; got", err)
}
if b != nil {
t.Error("buffer should be nil")
}
e = NewEnclaveRandom(0)
if !panics(func() {
e.Open()
}) {
t.Error("func should panic on nil enclave")
}
}
func panics(fn func()) (panicked bool) {
defer func() {
panicked = (recover() != nil)
}()
fn()
return
}