forked from bwmarrin/discordgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactions_test.go
68 lines (57 loc) · 1.96 KB
/
interactions_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
package discordgo
import (
"bytes"
"crypto/ed25519"
"encoding/hex"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
func TestVerifyInteraction(t *testing.T) {
pubkey, privkey, err := ed25519.GenerateKey(nil)
if err != nil {
t.Errorf("error generating signing keypair: %s", err)
}
timestamp := "1608597133"
t.Run("success", func(t *testing.T) {
body := "body"
request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader(body))
request.Header.Set("X-Signature-Timestamp", timestamp)
var msg bytes.Buffer
msg.WriteString(timestamp)
msg.WriteString(body)
signature := ed25519.Sign(privkey, msg.Bytes())
request.Header.Set("X-Signature-Ed25519", hex.EncodeToString(signature[:ed25519.SignatureSize]))
if !VerifyInteraction(request, pubkey) {
t.Error("expected true, got false")
}
})
t.Run("failure/modified body", func(t *testing.T) {
body := "body"
request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader("WRONG"))
request.Header.Set("X-Signature-Timestamp", timestamp)
var msg bytes.Buffer
msg.WriteString(timestamp)
msg.WriteString(body)
signature := ed25519.Sign(privkey, msg.Bytes())
request.Header.Set("X-Signature-Ed25519", hex.EncodeToString(signature[:ed25519.SignatureSize]))
if VerifyInteraction(request, pubkey) {
t.Error("expected false, got true")
}
})
t.Run("failure/modified timestamp", func(t *testing.T) {
body := "body"
request := httptest.NewRequest("POST", "http://localhost/interaction", strings.NewReader("WRONG"))
request.Header.Set("X-Signature-Timestamp", strconv.FormatInt(time.Now().Add(time.Minute).Unix(), 10))
var msg bytes.Buffer
msg.WriteString(timestamp)
msg.WriteString(body)
signature := ed25519.Sign(privkey, msg.Bytes())
request.Header.Set("X-Signature-Ed25519", hex.EncodeToString(signature[:ed25519.SignatureSize]))
if VerifyInteraction(request, pubkey) {
t.Error("expected false, got true")
}
})
}