| 1 |
;;; GnuTLS --- Guile bindings for GnuTLS. |
| 2 |
;;; Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 3 |
;;; |
| 4 |
;;; This file is part of Guile-GnuTLS. |
| 5 |
;;; |
| 6 |
;;; This library is free software; you can redistribute it and/or |
| 7 |
;;; modify it under the terms of the GNU Lesser General Public |
| 8 |
;;; License as published by the Free Software Foundation; either |
| 9 |
;;; version 2.1 of the License, or (at your option) any later version. |
| 10 |
;;; |
| 11 |
;;; This library 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 GNU |
| 14 |
;;; Lesser General Public License for more details. |
| 15 |
;;; |
| 16 |
;;; You should have received a copy of the GNU Lesser General Public |
| 17 |
;;; License along with this library; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
;;; Written by Ludovic Courtès <[email protected]>. |
| 20 |
|
| 21 |
(define-module (gnutls build tests) |
| 22 |
#:export (run-test |
| 23 |
with-child-process)) |
| 24 |
|
| 25 |
(define (run-test thunk) |
| 26 |
"Call `(exit (THUNK))'. If THUNK raises an exception, then call `(exit 1)' and |
| 27 |
display a backtrace. Otherwise, return THUNK's return value." |
| 28 |
(exit |
| 29 |
(catch |
| 30 |
#t |
| 31 |
thunk |
| 32 |
(lambda (key . args) |
| 33 |
;; Never reached. |
| 34 |
(exit 1)) |
| 35 |
(lambda (key . args) |
| 36 |
(dynamic-wind ;; to be on the safe side |
| 37 |
(lambda () #t) |
| 38 |
(lambda () |
| 39 |
(format (current-error-port) |
| 40 |
"~%throw to `~a' with args ~s [PID ~a]~%" |
| 41 |
key args (getpid)) |
| 42 |
(display-backtrace (make-stack #t) (current-output-port))) |
| 43 |
(lambda () |
| 44 |
(exit 1))) |
| 45 |
(exit 1))))) |
| 46 |
|
| 47 |
(define (call-with-child-process child parent) |
| 48 |
"Run thunk CHILD in a child process and invoke PARENT from the parent |
| 49 |
process, passing it the PID of the child process. Make sure the child |
| 50 |
process exits upon failure." |
| 51 |
(let ((pid (primitive-fork))) |
| 52 |
(if (zero? pid) |
| 53 |
(dynamic-wind |
| 54 |
(const #t) |
| 55 |
(lambda () |
| 56 |
(primitive-exit (if (child) 0 1))) |
| 57 |
(lambda () |
| 58 |
(primitive-exit 2))) |
| 59 |
(parent pid)))) |
| 60 |
|
| 61 |
(use-modules (rnrs io ports) |
| 62 |
(rnrs bytevectors) |
| 63 |
(ice-9 match)) |
| 64 |
|
| 65 |
(define-syntax-rule (define-replacement (name args ...) body ...) |
| 66 |
;; Define a compatibility replacement for NAME, if needed. |
| 67 |
(define-public name |
| 68 |
(if (module-defined? the-scm-module 'name) |
| 69 |
(module-ref the-scm-module 'name) |
| 70 |
(lambda (args ...) |
| 71 |
body ...)))) |
| 72 |
|
| 73 |
;; 'uniform-vector-read!' and 'uniform-vector-write' are deprecated in 2.0 |
| 74 |
;; and absent in 2.2. |
| 75 |
;; TODO: Switch to the R6RS bytevector and I/O interface. |
| 76 |
|
| 77 |
(define-replacement (uniform-vector-read! buf port) |
| 78 |
(match (get-bytevector-n! port buf |
| 79 |
0 (bytevector-length buf)) |
| 80 |
((? eof-object?) 0) |
| 81 |
((? integer? n) n))) |
| 82 |
|
| 83 |
(define-replacement (uniform-vector-write buf port) |
| 84 |
(put-bytevector port buf)) |
| 85 |
|
| 86 |
(define-syntax-rule (with-child-process pid parent child) |
| 87 |
"Fork and evaluate expression PARENT in the current process, with PID bound |
| 88 |
to the PID of its child process; the child process evaluated CHILD." |
| 89 |
(call-with-child-process |
| 90 |
(lambda () child) |
| 91 |
(lambda (pid) parent))) |
| 92 |
|
| 93 |
;;; Local Variables: |
| 94 |
;;; eval: (put 'define-replacement 'scheme-indent-function 1) |
| 95 |
;;; End: |