-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer_test.go
126 lines (102 loc) · 3.03 KB
/
signer_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
115
116
117
118
119
120
121
122
123
124
125
126
package wmsigner
import (
"crypto/rand"
"testing"
"errors"
)
const wmid = "405002833238"
const keyFileName = "test.kwm"
const keyPassword = "FvGqPdAy8reVWw789"
const testString = "TEST"
const testSignature = "642c2f71aafe930bcd238d925833414ccba29f2d408f3b77f1d7100111269865a100c6550258420734e96b4c11153ed597af9a28a066ffece8b50b0c5ffa15fe068f"
const anotherTestString = "Another test..."
func TestSign(t *testing.T) {
signer, err := New(wmid, keyFileName, keyPassword)
if err != nil {
t.Fatal(err.Error())
}
// Mock the random generator to get nulls only
rand.Reader = new(randReaderMock)
signature, err := signer.Sign(testString)
if err != nil {
t.Fatal(err.Error())
}
if signature != testSignature {
t.Errorf("Wrong signature %s", signature)
}
sameSignature, err := signer.Sign(testString)
if err != nil {
t.Fatal(err.Error())
}
if sameSignature != signature {
t.Errorf("Expected the same signature, got different")
}
anotherSignature, err := signer.Sign(anotherTestString)
if err != nil {
t.Fatal(err.Error())
}
if anotherSignature == signature {
t.Errorf("Expected a different signature, got the same")
}
}
func TestSignHalfPasswordCase(t *testing.T) {
// Double password to make its half to be the right password
// A char added to check if the length of a password is odd
doubleLengthPassword := keyPassword + keyPassword + "!"
signer, err := New(wmid, keyFileName, doubleLengthPassword)
if err != nil {
t.Fatal(err.Error())
}
// Mock the random generator to get nulls only
rand.Reader = new(randReaderMock)
signature, err := signer.Sign(testString)
if err != nil {
t.Error(err.Error())
}
if signature != testSignature {
t.Errorf("Wrong signature %s", signature)
}
}
func TestSignRandReaderError(t *testing.T) {
signer, err := New(wmid, keyFileName, keyPassword)
if err != nil {
t.Fatal(err.Error())
}
// Mock the random generator to get nulls only
rand.Reader = new(randReaderBrokenMock)
_, err = signer.Sign(testString)
expectedError(t, err, "Rand reader error")
}
func TestWmidError(t *testing.T) {
_, err := New("", keyFileName, keyPassword)
expectedError(t, err, "WMID not provided")
}
func TestKeyFileNotFoundError(t *testing.T) {
const noSuchFile = "no_such_file"
_, err := New(wmid, noSuchFile, keyPassword)
expectedError(t, err, "open " + noSuchFile + ": no such file or directory")
}
func TestKeyFileCorruptedError(t *testing.T) {
_, err := New(wmid, keyFileName, "")
expectedError(t, err, "Hash check failed. Key file seems to be corrupted.")
}
func expectedError(t *testing.T, err error, text string) {
if err == nil {
t.Fatal("Error expected, none found")
}
if err.Error() != text {
t.Errorf("Error text expected: “%s”, actual: “%s”", text, err.Error())
}
}
type randReaderMock struct{}
func (reader *randReaderMock) Read(b []byte) (n int, err error) {
n = len(b)
for i := 0; i < n; i++ {
b[i] = 0
}
return n, nil
}
type randReaderBrokenMock struct{}
func (reader *randReaderBrokenMock) Read(b []byte) (n int, err error) {
return 0, errors.New("Rand reader error")
}