-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage_test.go
151 lines (136 loc) · 4.69 KB
/
message_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// @author: Brian Wojtczak
// @copyright: 2024 by Brian Wojtczak
// @license: BSD-style license found in the LICENSE file
package altcha
import (
"github.com/k42-software/go-altcha/rand"
"testing"
)
func TestMessageString(t *testing.T) {
originalMsg := Message{
Algorithm: "SHA-256",
Salt: "0V5xzYiSFmY1swbb",
Number: 49500,
Challenge: "69df4e03d8fffc1d66aeba60384ad28d70caed4bcf10c69f80e0a16666eae6a7",
Signature: "-gytD6e0qjPZknud02kOzq8KqsayfXfGI1exZXFjI6k",
}
expectedText := `Altcha algorithm=SHA-256, number=49500, salt=0V5xzYiSFmY1swbb, challenge=69df4e03d8fffc1d66aeba60384ad28d70caed4bcf10c69f80e0a16666eae6a7, signature=-gytD6e0qjPZknud02kOzq8KqsayfXfGI1exZXFjI6k`
actualText := originalMsg.String()
if actualText != expectedText {
t.Errorf("Expected encoded string to be %s, got %s", expectedText, actualText)
}
}
func TestMessageIsValidResponse(t *testing.T) {
randomInt = rand.Int // Reset randomInt to use the real function
randomString = rand.String // Reset randomString to use the real function
// Test with a valid response
validMsg := Message{
Algorithm: "SHA-256",
Salt: "0V5xzYiSFmY1swbb",
Number: 49500,
Challenge: "69df4e03d8fffc1d66aeba60384ad28d70caed4bcf10c69f80e0a16666eae6a7",
Signature: "-gytD6e0qjPZknud02kOzq8KqsayfXfGI1exZXFjI6k",
}
if !validMsg.IsValidResponse() {
t.Error("Expected valid response to be true, got false")
}
// Test with invalid algorithm
invalidAlgoMsg := validMsg
invalidAlgoMsg.Algorithm = "InvalidAlgorithm"
if invalidAlgoMsg.IsValidResponse() {
t.Error("Expected response with invalid algorithm to be false, got true")
}
// Test with invalid number
invalidNumberMsg := validMsg
invalidNumberMsg.Number = -1
if invalidNumberMsg.IsValidResponse() {
t.Error("Expected response with invalid number to be false, got true")
}
// Test with an invalid challenge
invalidChallengeMsg := validMsg
invalidChallengeMsg.Challenge = "incorrect_challenge"
if invalidChallengeMsg.IsValidResponse() {
t.Error("Expected response with invalid challenge to be false, got true")
}
// Test with invalid signature
invalidSignatureMsg := validMsg
invalidSignatureMsg.Signature = "incorrect_signature"
if invalidSignatureMsg.IsValidResponse() {
t.Error("Expected response with invalid signature to be false, got true")
}
}
func TestMessageSolve(t *testing.T) {
tests := []struct {
name string
message Message
maximumComplexity int
wantNumber int
wantOk bool
}{
{
name: "ValidChallenge",
message: Message{
Algorithm: "SHA-256",
Salt: "0V5xzYiSFmY1swbb",
Challenge: "69df4e03d8fffc1d66aeba60384ad28d70caed4bcf10c69f80e0a16666eae6a7",
},
maximumComplexity: DefaultComplexity,
wantNumber: 49500,
wantOk: true,
},
{
name: "UnsolvableChallenge",
message: Message{
Algorithm: "SHA-256",
Salt: "0V5xzYiSFmY1swbb",
Challenge: "unsolvableChallengeHash",
},
maximumComplexity: DefaultComplexity,
wantNumber: -1,
wantOk: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotNumber, gotOk := tc.message.Solve(tc.maximumComplexity)
if gotNumber != tc.wantNumber || gotOk != tc.wantOk {
t.Errorf("Message.Solve() = (%v, %v), want (%v, %v)", gotNumber, gotOk, tc.wantNumber, tc.wantOk)
}
})
}
}
func TestSolveChallenge(t *testing.T) {
tests := []struct {
name string
challenge string
maximumComplexity int
wantResponse string
wantOk bool
}{
{
name: "ValidChallenge",
challenge: `{"algorithm":"SHA-256","salt":"0V5xzYiSFmY1swbb","challenge":"69df4e03d8fffc1d66aeba60384ad28d70caed4bcf10c69f80e0a16666eae6a7"}`,
maximumComplexity: DefaultComplexity,
wantResponse: `eyJhbGdvcml0aG0iOiJTSEEtMjU2Iiwic2FsdCI6IjBWNXh6WWlTRm1ZMXN3YmIiLCJudW1iZXIiOjQ5NTAwLCJjaGFsbGVuZ2UiOiI2OWRmNGUwM2Q4ZmZmYzFkNjZhZWJhNjAzODRhZDI4ZDcwY2FlZDRiY2YxMGM2OWY4MGUwYTE2NjY2ZWFlNmE3Iiwic2lnbmF0dXJlIjoiIn0=`,
wantOk: true,
},
{
name: "InvalidChallenge",
challenge: "invalidEncodedChallenge",
maximumComplexity: DefaultComplexity,
wantResponse: "",
wantOk: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotResponse, gotOk := SolveChallenge(tc.challenge, tc.maximumComplexity)
if gotOk != tc.wantOk || (tc.wantOk && gotResponse != tc.wantResponse) {
t.Logf("SolveChallenge(%v, %v)", tc.challenge, tc.maximumComplexity)
t.Logf("got: (%v, %v)", gotResponse, gotOk)
t.Logf("want: (%v, %v)", tc.wantResponse, tc.wantOk)
t.Errorf("SolveChallenge did not return expected result")
}
})
}
}