summaryrefslogtreecommitdiff
path: root/tests/reverse-proxy-anonymous.scm
blob: 34e113d5374c2893d1089ca01d0236bee89ab41c (plain)
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
;; disfluid, implementation of the Solid specification
;; Copyright (C) 2021  Vivien Kraus

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.

;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(define-module (tests reverse-proxy-anonymous)
  #:use-module (webid-oidc server endpoint)
  #:use-module (webid-oidc server endpoint reverse-proxy)
  #:use-module (webid-oidc testing)
  #:use-module (webid-oidc offloading)
  #:use-module ((webid-oidc parameters) #:prefix p:)
  #:use-module (oop goops)
  #:use-module (web server)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (web uri)
  #:use-module (ice-9 match)
  #:use-module (ice-9 receive)
  #:use-module (srfi srfi-26)
  #:use-module (rnrs bytevectors)
  #:declarative? #t
  #:duplicates (merge-generics))

(with-test-environment
 "reverse-proxy-anonymous"
 (lambda ()
   (define request-characters-reversed '())
   (define (push-char c)
     (set! request-characters-reversed
           `(,c ,@request-characters-reversed)))
   (define (push-string str)
     (for-each push-char (string->list str)))
   (define chars-to-read
     (string->list
      (call-with-output-string
        (lambda (port)
          (let ((updated
                 (write-response (build-response
                                  #:headers '((content-type text/plain)))
                                 port)))
            (write-response-body updated (string->utf8 "Hello!")))))))
   (parameterize
       ((p:current-date 0)
        (open-socket-for-uri
         (lambda _
           (make-soft-port
            (vector
             ;; Request character is written:
             push-char
             ;; Request string is written:
             push-string
             ;; Flushing output:
             (lambda () #t)
             ;; Get one character:
             (lambda ()
               (match chars-to-read
                 ((next rest ...)
                  (set! chars-to-read rest)
                  next)
                 (else
                  (call-with-input-string "" read)))) ;; EOF
             ;; Close the port:
             (lambda () #t))
            "rw"))))
     (with-threads
      (let ((reverse-proxy
             (make <reverse-proxy>
               #:backend-uri (string->uri "https://example.com")
               #:authentication-header 'test))
            (request
             (build-request
              (string->uri "https://example.com")
              #:headers '((content-type text/plain)
                          (test . "https://attack.com/profile/card#me"))
              #:meta '()))
            (request-body (string->utf8 "Hello, world!")))
        (receive (response response-body response-meta)
            (handle reverse-proxy request request-body)
          (unless (eqv? (response-code response) 200)
            (exit 1))
          (let ((request-read
                 (list->string (reverse request-characters-reversed)))
                (expected-request
                 (call-with-output-string
                   (lambda (port)
                     (write-request-body
                      (write-request
                       (build-request
                        (string->uri "https://example.com")
                        #:headers '((host . ("example.com" . #f))
                                    (content-type text/plain)))
                       port)
                      request-body)))))
            (unless (equal? request-read expected-request)
              (format (current-error-port) "Expected request:
~s
Actual request:
~s
"
                      expected-request request-read)
              (exit 2)))
          (set! response-body
                (read-response-body response))
          (unless (null? chars-to-read)
            (format (current-error-port) "Remaining chars to read: ~s\n"
                    (list->string chars-to-read))
            (exit 3))
          (unless (equal? response-body (string->utf8 "Hello!"))
            (exit 4))
          (primitive-exit 0))))
     (sleep 120)
     (format (current-error-port) "Test timeout.\n")
     (exit 5))))