chiark / gitweb /
gnupg2 (2.1.17-3) unstable; urgency=medium
[gnupg2.git] / tests / gpgscm / t-child.scm
1 ;; Tests for the low-level process and IPC primitives.
2 ;;
3 ;; Copyright (C) 2016 g10 Code GmbH
4 ;;
5 ;; This file is part of GnuPG.
6 ;;
7 ;; GnuPG is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; GnuPG is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program; if not, see <http://www.gnu.org/licenses/>.
19
20 (echo "Testing process and IPC primitives...")
21
22 (define (qualify executable)
23   (string-append executable (getenv "EXEEXT")))
24
25 (define child (qualify "t-child"))
26
27 (assert (= 0 (call `(,(qualify "t-child") "return0"))))
28 (assert (= 1 (call `(,(qualify "t-child") "return1"))))
29 (assert (= 77 (call `(,(qualify "t-child") "return77"))))
30
31 (let ((r (call-with-io `(,(qualify "t-child") "return0") "")))
32   (assert (= 0 (:retcode r)))
33   (assert (string=? "" (:stdout r)))
34   (assert (string=? "" (:stderr r))))
35
36 (let ((r (call-with-io `(,(qualify "t-child") "return1") "")))
37   (assert (= 1 (:retcode r)))
38   (assert (string=? "" (:stdout r)))
39   (assert (string=? "" (:stderr r))))
40
41 (let ((r (call-with-io `(,(qualify "t-child") "return77") "")))
42   (assert (= 77 (:retcode r)))
43   (assert (string=? "" (:stdout r)))
44   (assert (string=? "" (:stderr r))))
45
46 (let ((r (call-with-io `(,(qualify "t-child") "hello_stdout") "")))
47   (assert (= 0 (:retcode r)))
48   (assert (string=? "hello" (:stdout r)))
49   (assert (string=? "" (:stderr r))))
50
51 (let ((r (call-with-io `(,(qualify "t-child") "hello_stderr") "")))
52   (assert (= 0 (:retcode r)))
53   (assert (string=? "" (:stdout r)))
54   (assert (string=? "hello" (:stderr r))))
55
56 (let ((r (call-with-io `(,(qualify "t-child") "stdout4096") "")))
57   (assert (= 0 (:retcode r)))
58   (assert (= 4096 (string-length (:stdout r))))
59   (assert (string=? "" (:stderr r))))
60
61 (let ((r (call-with-io `(,(qualify "t-child") "stdout8192") "")))
62   (assert (= 0 (:retcode r)))
63   (assert (= 8192 (string-length (:stdout r))))
64   (assert (string=? "" (:stderr r))))
65
66 (let ((r (call-with-io `(,(qualify "t-child") "cat") "hellohello")))
67   (assert (= 0 (:retcode r)))
68   (assert (string=? "hellohello" (:stdout r)))
69   (assert (string=? "" (:stderr r))))
70
71 (define (spawn what)
72   (spawn-process-fd what CLOSED_FD STDOUT_FILENO STDERR_FILENO))
73
74 (let ((pid0 (spawn `(,(qualify "t-child") "return0")))
75       (pid1 (spawn `(,(qualify "t-child") "return0"))))
76   (assert (equal? '(0 0)
77                   (wait-processes '("child0" "child1") (list pid0 pid1) #t))))
78
79 (let ((pid0 (spawn `(,(qualify "t-child") "return1")))
80       (pid1 (spawn `(,(qualify "t-child") "return0"))))
81   (assert (equal? '(1 0)
82                   (wait-processes '("child0" "child1") (list pid0 pid1) #t))))
83
84 (let ((pid0 (spawn `(,(qualify "t-child") "return0")))
85       (pid1 (spawn `(,(qualify "t-child") "return77")))
86       (pid2 (spawn `(,(qualify "t-child") "return1"))))
87   (assert (equal? '(0 77 1)
88                   (wait-processes '("child0" "child1" "child2")
89                                   (list pid0 pid1 pid2) #t))))
90
91 (let* ((p (pipe))
92        (pid0 (spawn-process-fd
93                `(,(qualify "t-child") "hello_stdout")
94                CLOSED_FD (:write-end p) STDERR_FILENO))
95        (_ (close (:write-end p)))
96        (pid1 (spawn-process-fd
97                `(,(qualify "t-child") "cat")
98                (:read-end p) STDOUT_FILENO STDERR_FILENO)))
99   (close (:read-end p))
100   (assert
101    (equal? '(0 0)
102            (wait-processes '("child0" "child1") (list pid0 pid1) #t))))
103 (echo " world.")
104
105 (tr:do
106  (tr:pipe-do
107   (pipe:spawn `(,child stdout4096))
108   (pipe:spawn `(,child cat)))
109  (tr:call-with-content (lambda (c)
110                          (assert (= 4096 (length c))))))
111 (tr:do
112  (tr:pipe-do
113   (pipe:spawn `(,child stdout8192))
114   (pipe:spawn `(,child cat)))
115  (tr:call-with-content (lambda (c)
116                          (assert (= 8192 (length c))))))
117
118 (echo "All good.")