-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathverify_request_test.go
More file actions
71 lines (61 loc) · 1.82 KB
/
Copy pathverify_request_test.go
File metadata and controls
71 lines (61 loc) · 1.82 KB
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
package tempest
import (
"bytes"
"crypto/ed25519"
"encoding/hex"
"net/http"
"net/http/httptest"
"sync"
"testing"
)
func TestVerifyRequest(t *testing.T) {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
client := &HTTPClient{
bufferPool: &sync.Pool{
New: func() any {
return new(bytes.Buffer)
},
},
}
timestamp := "1234567890"
body := []byte(`{"type":1}`)
msg := append([]byte(timestamp), body...)
sig := ed25519.Sign(priv, msg)
sigHex := hex.EncodeToString(sig)
t.Run("Valid request", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body))
req.Header.Set("X-Signature-Ed25519", sigHex)
req.Header.Set("X-Signature-Timestamp", timestamp)
resBody, cleanup, verified := client.verifyRequest(req, pub, 1024)
if !verified {
t.Error("expected verification to succeed")
}
if !bytes.Equal(resBody, body) {
t.Errorf("expected body %s, got %s", body, resBody)
}
cleanup()
})
t.Run("Invalid signature", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body))
req.Header.Set("X-Signature-Ed25519", "invalid")
req.Header.Set("X-Signature-Timestamp", timestamp)
_, _, verified := client.verifyRequest(req, pub, 1024)
if verified {
t.Error("expected verification to fail")
}
})
t.Run("Body too large", func(t *testing.T) {
largeBody := make([]byte, 2048)
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(largeBody))
req.Header.Set("X-Signature-Ed25519", sigHex)
req.Header.Set("X-Signature-Timestamp", timestamp)
_, _, verified := client.verifyRequest(req, pub, 1024)
if verified {
// It should fail because ed25519.Verify will check truncated body against signature of full body
t.Error("expected verification to fail due to truncated body")
}
})
}