-
Notifications
You must be signed in to change notification settings - Fork 1
/
protocol_v1_test.go
52 lines (43 loc) · 952 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
package votifier
import (
"crypto/rsa"
"math/rand"
"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 TestSerializationv1(t *testing.T) {
v := NewVote("golang", "golang", "127.0.0.1")
// 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.
s, err := v.serializev1(&key.PublicKey)
if err != nil {
t.Error(err)
return
}
if len(*s) != 256 {
t.Error("Encrypted PKCS1v15 output should be 256 bytes, but it is %d bytes long", len(*s))
return
}
// Try to decrypt this vote.
d, err := deserializev1(*s, key)
if err != nil {
t.Error(err)
return
}
if v != *d {
t.Error("Votes don't match")
return
}
}