-
Notifications
You must be signed in to change notification settings - Fork 0
/
box_test.go
108 lines (95 loc) · 2.27 KB
/
box_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
package vbox
import (
"bytes"
"testing"
)
var plaintext = []byte(`
Hello, world!
This is a super-secret message.
do not share!
This message is encrypted with the AES256-GCM cipher and XChaCha20-Poly1305.
So, it is highly encrypted!
The key of the this message are hashed with blake3-512 Alogrithm.
`)
func TestBlackBox(t *testing.T) {
box := NewBlackBox([]byte("test"))
sealed := box.Seal(plaintext)
opened, ok := box.Open(sealed)
if !ok {
t.Error("Failed to open sealed message")
}
if len(plaintext) != len(opened) || !bytes.Equal(plaintext, opened) {
t.Error("Failed to open sealed message")
}
}
func TestSealAndOpenOverWrite(t *testing.T) {
box := NewBlackBox([]byte("test"))
sealed := box.Seal(plaintext)
opened, ok := box.OpenOverWrite(sealed)
if !ok {
t.Error("Failed to open sealed message")
}
if len(plaintext) != len(opened) || !bytes.Equal(plaintext, opened) {
t.Error("Failed to open sealed message")
}
}
func TestBase64SealAndBase64Open(t *testing.T) {
box := NewBlackBox([]byte("test"))
sealed := box.Base64Seal(plaintext)
opened, ok := box.Base64Open(sealed)
if !ok {
t.Error("Failed to open sealed message")
}
if len(plaintext) != len(opened) || !bytes.Equal(plaintext, opened) {
t.Error("Failed to open sealed message")
}
}
func BenchmarkBlackBoxSeal(b *testing.B) {
box := NewBlackBox([]byte("test"))
b.RunParallel(func(p *testing.PB) {
for p.Next() {
box.Seal(plaintext)
}
})
}
func BenchmarkBlackBoxOpen(b *testing.B) {
box := NewBlackBox([]byte("test"))
sealed := box.Seal(plaintext)
b.RunParallel(func(p *testing.PB) {
for p.Next() {
box.Open(sealed)
}
})
}
func BenchmarkBlackBoxSealAndOpen(b *testing.B) {
box := NewBlackBox([]byte("test"))
b.RunParallel(func(p *testing.PB) {
for p.Next() {
sealed := box.Seal(plaintext)
box.Open(sealed)
}
})
}
func BenchmarkBlackBoxSealAndOpenOverWrite(b *testing.B) {
box := NewBlackBox([]byte("test"))
b.RunParallel(func(p *testing.PB) {
for p.Next() {
sealed := box.Seal(plaintext)
box.OpenOverWrite(sealed)
}
})
}
func TestInvalidLen(t *testing.T) {
box := NewBlackBox([]byte("Hello, World!"))
for i := 0; i < 128; i++ {
a := make([]byte, i)
_, ok := box.Open(a)
if ok {
panic("ok")
}
_, ok = box.OpenOverWrite(a)
if ok {
panic("ok")
}
}
}