AtlatestRepositorysigil-cloudflare

sigil-cloudflare / tree / testtest-signing.sgl

1(import (sigil test)
2 (sigil crypto)
3 (cloudflare r2)
4 (cloudflare r2 signing))
5
6(define access "AKIAIOSFODNN7EXAMPLE")
7(define secret "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
8(test-group "R2 SigV4"
9 (test "official AWS S3 GET range signature"
10 (let ((r (r2-sign-request 'GET "/test.txt" '()
11 '(("Host" . "examplebucket.s3.amazonaws.com") ("Range" . "bytes=0-9"))
12 "" access secret "20130524T000000Z" region: "us-east-1")))
13 (assert-equal (sha256 (dict-ref r canonical-request:)) "7344ae5b7ee6c3e7e6b0fe0640412a37625d1fbfff95c48bbb2dc43964946972")
14 (assert-equal (dict-ref r signature:) "f0e8bdb87c964420e857bd35b5d6ed310bd44f0170aba48dd91039c6036bdb41")))
15 (test "official AWS S3 lifecycle query signature"
16 (let ((r (r2-sign-request 'GET "/" '(("lifecycle" . ""))
17 '(("host" . "examplebucket.s3.amazonaws.com"))
18 "" access secret "20130524T000000Z" region: "us-east-1")))
19 (assert-equal (dict-ref r signature:) "fea454ca298b7da1c68078a5d1bdbfbbe0d65c699e0f91ac7a200a0136783543")))
20 (test "UTF-8 path encoding preserves slashes and dot segments"
21 (assert-equal (r2-uri-encode "/logos//../é +%?雪.png" preserve-slashes?: #t)
22 "/logos//../%C3%A9%20%2B%25%3F%E9%9B%AA.png"))
23 (test "query sorting occurs after encoding and preserves duplicate keys"
24 (assert-equal (r2-canonical-query '(("z" . "") ("a" . "z") ("a" . "+") ("é" . "x")))
25 "%C3%A9=x&a=%2B&a=z&z="))
26 (test "maximum-length key does not exceed variadic call limits"
27 (assert-equal (string-length (r2-uri-encode (make-string 1024 #\space))) 3072))
28 (test "header spaces collapse and names lowercase"
29 (let ((a (r2-sign-request 'GET "/" '() '(("Host" . "example.com") ("X-Test" . " a \t b ")) "" access secret "20130524T000000Z"))
30 (b (r2-sign-request 'GET "/" '() '(("host" . "example.com") ("x-test" . "a b")) "" access secret "20130524T000000Z")))
31 (assert-equal (dict-ref a signature:) (dict-ref b signature:))))
32 (test "header injection and duplicate headers fail before network"
33 (assert-error (r2-sign-request 'GET "/" '() '(("host" . "x\r\ny")) "" access secret "20130524T000000Z"))
34 (assert-error (r2-sign-request 'GET "/" '() '(("host" . "x") ("Host" . "x")) "" access secret "20130524T000000Z")))
35 (test "public custom domain is separate from API bucket path"
36 (let ((c (r2-client "0123456789abcdef0123456789abcdef" access secret "rezvibe-assets" public-base-url: "https://assets.rezvibe.com")))
37 (assert-equal (r2-public-url c "logos/é.png") "https://assets.rezvibe.com/logos/%C3%A9.png")))
38 (test "invalid client configuration fails locally"
39 (assert-error (r2-client "bad" access secret "rezvibe-assets"))
40 (assert-error (r2-client "0123456789abcdef0123456789abcdef" access secret "../wrong"))))