-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
69 lines (59 loc) · 1.81 KB
/
main_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
package main
import (
"math"
"reflect"
"testing"
)
func TestEncryptDecrypt(t *testing.T) {
private := PrivateKey{65473, 75827}
public := PublicKey{75827, 60457}
plaintext := "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem"
ciphertext := Encrypt(plaintext, public)
decrypted_plaintext := Decrypt(ciphertext, private)
if plaintext != decrypted_plaintext {
t.Error("Decrypted plaintext does not match")
}
}
func TestEncryptRandomized(t *testing.T) {
public := PublicKey{75827, 60457}
plaintext := "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem"
ciphertext1 := Encrypt(plaintext, public)
ciphertext2 := Encrypt(plaintext, public)
if reflect.DeepEqual(ciphertext1, ciphertext2) {
t.Error("ciphertexts are equal, therefore randomization does not work")
}
}
func TestGenerateKeysPair(t *testing.T) {
for i := 0; i < 10; i++ {
_, _, err := GenerateKeysPair()
if err != nil {
t.Error("Failed to generate keys pair: ", err)
}
}
}
func Test_genPrime_small(t *testing.T) {
for i := 0; i < 100; i++ {
p := genPrime(100, 1000)
if p < 0 {
t.Error("Failed to generate prime number")
}
for d := 2; d < int(math.Sqrt(float64(p)))+1; d++ {
if p%d == 0 {
t.Errorf("%d is not prime (divided by %d)", p, d)
}
}
}
}
func Test_genPrime_big(t *testing.T) {
for i := 0; i < 100; i++ {
p := genPrime(100, 1000000)
if p < 0 {
t.Error("Failed to generate prime number")
}
for d := 2; d < int(math.Sqrt(float64(p)))+1; d++ {
if p%d == 0 {
t.Errorf("%d is not prime (divided by %d)", p, d)
}
}
}
}