forked from NuVotifier/go-votifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
protocol_v1_test.go
57 lines (49 loc) · 1020 Bytes
/
protocol_v1_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
package votifier
import (
"crypto/rsa"
"math/rand"
"reflect"
"testing"
)
// Extremely bad random number generation, used only for testing purposes.
type badRandomReader struct{}
func (badRandomReader) Read(p []byte) (n int, err error) {
for i := range p {
p[i] = byte(rand.Intn(255))
}
return len(p), nil
}
func TestEncodeV1(t *testing.T) {
// Generate a set of keys for later use
key, err := rsa.GenerateKey(new(badRandomReader), 2048)
if err != nil {
t.Error(err)
return
}
// Try to encrypt this vote.
v := Vote{
ServiceName: "golang",
Username: "golang",
Address: "127.0.0.1",
}
s, err := v.EncodeV1(&key.PublicKey)
if err != nil {
t.Error(err)
return
}
if len(*s) != 256 {
t.Errorf("Encrypted PKCS1v15 output should be 256 bytes, but it is %d bytes long", len(*s))
return
}
// Try to decrypt this vote.
var d Vote
err = d.DecodeV1(*s, key)
if err != nil {
t.Error(err)
return
}
if reflect.DeepEqual(v, d) {
t.Error("votes don't match")
return
}
}