-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplebox_test.go
67 lines (59 loc) · 1.57 KB
/
simplebox_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
package simplebox
import (
"crypto/rand"
"fmt"
"testing"
)
var (
simpleBox *SimpleBox
)
func init() {
var secretKey [KeySize]byte
rand.Read(secretKey[:])
simpleBox = NewFromSecretKey(&secretKey)
}
func TestDecryptBadCipher(t *testing.T) {
garbage := "not-actually-ciphertext-but-longer-than-a-nonce-at-least"
plain, err := simpleBox.Decrypt([]byte(garbage))
if plain != nil {
t.Errorf("Expected plaintext to be nil")
}
expected := fmt.Errorf("Ciphertext could not be decrypted.")
if err.Error() != expected.Error() {
t.Errorf("Expected error to be '%v', but got '%v'", expected, err)
}
}
func TestDecryptBadLength(t *testing.T) {
garbage := "not-actually-ciphertext"
plain, err := simpleBox.Decrypt([]byte(garbage))
if plain != nil {
t.Errorf("Expected plaintext to be nil")
}
expected := fmt.Errorf("Ciphertext is of invalid length.")
if err.Error() != expected.Error() {
t.Errorf("Expected error to be '%v', but got '%v'", expected, err)
}
}
func TestEncryptionSymmetry(t *testing.T) {
expected := "hello"
cipher := simpleBox.Encrypt([]byte(expected))
actual, err := simpleBox.Decrypt(cipher)
if err != nil {
t.Fatal(err)
}
if string(actual) != expected {
t.Errorf("Expected plaintext to be '%v', but got '%v'",
expected, string(actual))
}
}
func TestNewFromSecretKey(t *testing.T) {
var secretKey [KeySize]byte
for i := 0; i < KeySize; i++ {
secretKey[i] = byte(i)
}
simpleBox := NewFromSecretKey(&secretKey)
if simpleBox.secretKey != &secretKey {
t.Errorf("Expected secretKey to be '%v', but got '%v'",
secretKey, simpleBox.secretKey)
}
}