This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsecureData_test.go
114 lines (110 loc) · 3.01 KB
/
secureData_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
109
110
111
112
113
114
package models
import (
"bytes"
"crypto/rand"
"testing"
"golang.org/x/crypto/nacl/box"
)
func mustKey(t *testing.T) (pubkey, privkey *[32]byte, valid bool) {
t.Helper()
var err error
valid = true
pubkey, privkey, err = box.GenerateKey(rand.Reader)
if err != nil {
t.Errorf("Error generating keys: %v", err)
valid = false
}
return
}
func TestSecureData(t *testing.T) {
ourPublicKey, ourPrivateKey, valid := mustKey(t)
if !valid {
return
}
payload := []byte("Hello, World")
msg := &SecureData{}
if err := msg.Seal(ourPublicKey, payload); err != nil {
t.Errorf("%v", err)
return
} else {
t.Logf("Sealed message `Hello, World`")
}
out, err := msg.Open(ourPrivateKey)
if err != nil {
t.Errorf("%v", err)
return
} else {
t.Logf("Opened sealed message")
}
if !bytes.Equal(out, payload) {
t.Errorf("Expected %s, got %s", string(payload), string(out))
} else {
t.Logf("Sealed message intact")
}
mMsg := &SecureData{}
if err := mMsg.Marshal(ourPublicKey[:], string(payload)); err != nil {
t.Errorf("%v", err)
return
} else {
t.Logf("Marshalled message `Hello, World`")
}
outPayload := ""
if err := mMsg.Unmarshal(ourPrivateKey[:], &outPayload); err != nil {
t.Errorf("%v", err)
return
} else {
t.Logf("Unmarshalled message")
}
if string(payload) != outPayload {
t.Errorf("Expected %s, got %s", string(payload), outPayload)
} else {
t.Logf("Marshalled message intact")
}
var decrypted []byte
msg.Nonce[0] = msg.Nonce[0] ^ byte(0xff)
decrypted, err = msg.Open(ourPrivateKey)
if err != Corrupt {
t.Errorf("corruptViaNonce: Expected error %v, not %v(%v)", Corrupt, err, string(decrypted))
} else {
t.Logf("corruptViaNonce: Got expected error %v", err)
}
msg.Nonce[0] = msg.Nonce[0] ^ byte(0xff)
msg.Key[0] = msg.Key[0] ^ byte(0xff)
decrypted, err = msg.Open(ourPrivateKey)
if err != Corrupt {
t.Errorf("corruptViaKey: Expected error %v, not %v(%v)", Corrupt, err, string(decrypted))
} else {
t.Logf("corruptViaKey: Got expected error %v", err)
}
msg.Key[0] = msg.Key[0] ^ byte(0xff)
msg.Payload[0] = msg.Payload[0] ^ byte(0xff)
decrypted, err = msg.Open(ourPrivateKey)
if err != Corrupt {
t.Errorf("corruptViaPayload: Expected error %v, not %v(%v)", Corrupt, err, string(decrypted))
} else {
t.Logf("corruptViaPayload: Got expected error %v", err)
}
msg.Payload[0] = msg.Payload[0] ^ byte(0xff)
msg.Payload = msg.Payload[1:]
_, err = msg.Open(ourPrivateKey)
if err != Corrupt {
t.Errorf("corruptViaPayload: Expected error %v, not %v", Corrupt, err)
} else {
t.Logf("corruptViaPayload: Got expected error %v", err)
}
var nonce []byte
nonce, msg.Nonce = msg.Nonce, msg.Nonce[1:]
_, err = msg.Open(ourPrivateKey)
if err != BadNonce {
t.Errorf("badNonce: Expected error %v, not %v", BadNonce, err)
} else {
t.Logf("badNonce: Got expected error %v", err)
}
msg.Nonce, msg.Key = nonce, msg.Key[1:]
_, err = msg.Open(ourPrivateKey)
if err != BadKey {
t.Errorf("badKey: Expected error %v, not %v", BadKey, err)
} else {
t.Logf("badKey: Got expected error %v", err)
}
}