Debian Codegraph @

guile/tests/session-record-port.scm

raw
1 ;;; GnuTLS --- Guile bindings for GnuTLS.
2 ;;; Copyright (C) 2007-2025 Free Software Foundation, Inc.
3 ;;;
4 ;;; This file is part of Guile-GnuTLS.
5 ;;;
6 ;;; This program is free software: you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation, either version 3 of the License, or
9 ;;; (at your option) any later version.
10 ;;;
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19 ;;; Written by Ludovic Courtès <[email protected]>.
20
21
22 ;;;
23 ;;; Test session establishment using anonymous authentication. Exercise the
24 ;;; `session-record-port' API.
25 ;;;
26
27 (use-modules (gnutls)
28 (gnutls build tests)
29 (srfi srfi-4))
30
31 ;; TLS session settings.
32 (define priorities
33 "NONE:+VERS-TLS1.2:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH")
34
35 ;; Message sent by the client.
36 (define %message (apply u8vector (iota 256)))
37
38 (define (import-something import-proc file fmt)
39 (let* ((path (search-path %load-path file))
40 (size (stat:size (stat path)))
41 (raw (make-u8vector size)))
42 (uniform-vector-read! raw (open-input-file path))
43 (import-proc raw fmt)))
44
45 (define (import-dh-params file)
46 (import-something pkcs3-import-dh-parameters file
47 x509-certificate-format/pem))
48
49 ;; Debugging.
50 ;; (set-log-level! 100)
51 ;; (set-log-procedure! (lambda (level str)
52 ;; (format #t "[~a|~a] ~a" (getpid) level str)))
53
54 (run-test
55 (lambda ()
56 ;; Stress the GC. In 0.0, this triggered an abort due to
57 ;; "scm_unprotect_object called during GC".
58 (let ((sessions (map (lambda (i)
59 (make-session connection-end/server))
60 (iota 123))))
61 (for-each session-record-port sessions)
62 (gc)(gc)(gc))
63
64 ;; Stress the GC. The session associated with each port in PORTS should
65 ;; remain reachable.
66 (let ((ports (map session-record-port
67 (map (lambda (i)
68 (make-session connection-end/server))
69 (iota 123)))))
70 (gc)(gc)(gc)
71 (for-each (lambda (p)
72 (catch 'gnutls-error
73 (lambda ()
74 (read p))
75 (lambda (key . args)
76 #t)))
77 ports))
78
79 ;; Try using the record port for I/O.
80 (let ((socket-pair (socketpair PF_UNIX SOCK_STREAM 0)))
81 (with-child-process pid
82
83 ;; server-side
84 (let ((server (make-session connection-end/server)))
85 (set-session-priorities! server priorities)
86
87 (set-session-transport-fd! server (fileno (cdr socket-pair)))
88 (let ((cred (make-anonymous-server-credentials))
89 (dh-params (import-dh-params "dh-parameters.pem")))
90 ;; Note: DH parameter generation can take some time.
91 (set-anonymous-server-dh-parameters! cred dh-params)
92 (set-session-credentials! server cred))
93 (set-session-dh-prime-bits! server 1024)
94
95 (handshake server)
96 (let* ((buf (make-u8vector (u8vector-length %message)))
97 (amount
98 (uniform-vector-read! buf (session-record-port server))))
99 (bye server close-request/rdwr)
100
101 ;; Make sure we got everything right.
102 (and (eq? (session-record-port server)
103 (session-record-port server))
104 (zero? (cdr (waitpid pid)))
105 (= amount (u8vector-length %message))
106 (equal? buf %message)
107 (eof-object?
108 (read-char (session-record-port server)))
109
110 ;; Close the port and make sure its 'close' procedure is
111 ;; called.
112 (let* ((closed? #f)
113 (port (session-record-port server))
114 (close (lambda (p)
115 (format #t "closing port ~s~%" p)
116 (set! closed? (eq? p port)))))
117 (set-session-record-port-close! port close)
118 (close-port port)
119 closed?))))
120
121 ;; client-side (child process)
122 (let ((client (make-session connection-end/client)))
123 (set-session-priorities! client priorities)
124
125 (set-session-transport-port! client (car socket-pair))
126 (set-session-credentials! client (make-anonymous-client-credentials))
127 (set-session-dh-prime-bits! client 1024)
128
129 (handshake client)
130 (uniform-vector-write %message (session-record-port client))
131 (bye client close-request/rdwr)
132
133 (primitive-exit))))))
134
135 ;;; arch-tag: e873226a-d0b6-4a93-87ec-a1b5ad2ae8a2