aboutsummaryrefslogtreecommitdiffstats
path: root/test/src
diff options
context:
space:
mode:
authorAndrea Corallo2021-04-13 12:06:23 +0200
committerAndrea Corallo2021-04-13 12:06:23 +0200
commitb064ddd3f600ed28e62b09d556ecced5f80d9883 (patch)
tree2ddf4889f385beb34cd064f245a7e59265377c37 /test/src
parent2d23f19e7d5ff8a1ec1a188dcd530c185029d1f8 (diff)
parent6de79542e43ece9a12ebc032c275a6c3fee0b73b (diff)
downloademacs-b064ddd3f600ed28e62b09d556ecced5f80d9883.tar.gz
emacs-b064ddd3f600ed28e62b09d556ecced5f80d9883.zip
Merge remote-tracking branch 'savannah/master' into native-comp
Diffstat (limited to 'test/src')
l---------test/src/emacs-resources/seccomp-filter-exec.bpf1
l---------test/src/emacs-resources/seccomp-filter.bpf1
-rw-r--r--test/src/emacs-tests.el213
3 files changed, 215 insertions, 0 deletions
diff --git a/test/src/emacs-resources/seccomp-filter-exec.bpf b/test/src/emacs-resources/seccomp-filter-exec.bpf
new file mode 120000
index 00000000000..5b0e9978221
--- /dev/null
+++ b/test/src/emacs-resources/seccomp-filter-exec.bpf
@@ -0,0 +1 @@
../../../lib-src/seccomp-filter-exec.bpf \ No newline at end of file
diff --git a/test/src/emacs-resources/seccomp-filter.bpf b/test/src/emacs-resources/seccomp-filter.bpf
new file mode 120000
index 00000000000..b3d603d0aeb
--- /dev/null
+++ b/test/src/emacs-resources/seccomp-filter.bpf
@@ -0,0 +1 @@
../../../lib-src/seccomp-filter.bpf \ No newline at end of file
diff --git a/test/src/emacs-tests.el b/test/src/emacs-tests.el
new file mode 100644
index 00000000000..09f9a248efb
--- /dev/null
+++ b/test/src/emacs-tests.el
@@ -0,0 +1,213 @@
1;;; emacs-tests.el --- unit tests for emacs.c -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2020 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published
9;; by the Free Software Foundation, either version 3 of the License,
10;; or (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful, but
13;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15;; General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;; Unit tests for src/emacs.c.
23
24;;; Code:
25
26(require 'cl-lib)
27(require 'ert)
28(require 'ert-x)
29(require 'rx)
30(require 'subr-x)
31
32(ert-deftest emacs-tests/seccomp/absent-file ()
33 (skip-unless (string-match-p (rx bow "SECCOMP" eow)
34 system-configuration-features))
35 (let ((emacs
36 (expand-file-name invocation-name invocation-directory))
37 (process-environment nil))
38 (skip-unless (file-executable-p emacs))
39 (should-not (file-exists-p "/does-not-exist.bpf"))
40 (should-not
41 (eql (call-process emacs nil nil nil
42 "--quick" "--batch"
43 "--seccomp=/does-not-exist.bpf")
44 0))))
45
46(cl-defmacro emacs-tests--with-temp-file
47 (var (prefix &optional suffix text) &rest body)
48 "Evaluate BODY while a new temporary file exists.
49Bind VAR to the name of the file. Pass PREFIX, SUFFIX, and TEXT
50to `make-temp-file', which see."
51 (declare (indent 2) (debug (symbolp (form form form) body)))
52 (cl-check-type var symbol)
53 ;; Use an uninterned symbol so that the code still works if BODY
54 ;; changes VAR.
55 (let ((filename (make-symbol "filename")))
56 `(let ((,filename (make-temp-file ,prefix nil ,suffix ,text)))
57 (unwind-protect
58 (let ((,var ,filename))
59 ,@body)
60 (delete-file ,filename)))))
61
62(ert-deftest emacs-tests/seccomp/empty-file ()
63 (skip-unless (string-match-p (rx bow "SECCOMP" eow)
64 system-configuration-features))
65 (let ((emacs
66 (expand-file-name invocation-name invocation-directory))
67 (process-environment nil))
68 (skip-unless (file-executable-p emacs))
69 (emacs-tests--with-temp-file filter ("seccomp-invalid-" ".bpf")
70 ;; The --seccomp option is processed early, without filename
71 ;; handlers. Therefore remote or quoted filenames wouldn't
72 ;; work.
73 (should-not (file-remote-p filter))
74 (cl-callf file-name-unquote filter)
75 ;; According to the Seccomp man page, a filter must have at
76 ;; least one element, so Emacs should reject an empty file.
77 (should-not
78 (eql (call-process emacs nil nil nil
79 "--quick" "--batch"
80 (concat "--seccomp=" filter))
81 0)))))
82
83(ert-deftest emacs-tests/seccomp/file-too-large ()
84 (skip-unless (string-match-p (rx bow "SECCOMP" eow)
85 system-configuration-features))
86 (let ((emacs
87 (expand-file-name invocation-name invocation-directory))
88 (process-environment nil)
89 ;; This value should be correct on all supported systems.
90 (ushort-max #xFFFF)
91 ;; Either 8 or 16, but 16 should be large enough in all cases.
92 (filter-size 16))
93 (skip-unless (file-executable-p emacs))
94 (emacs-tests--with-temp-file
95 filter ("seccomp-too-large-" ".bpf"
96 (make-string (* (1+ ushort-max) filter-size) ?a))
97 ;; The --seccomp option is processed early, without filename
98 ;; handlers. Therefore remote or quoted filenames wouldn't
99 ;; work.
100 (should-not (file-remote-p filter))
101 (cl-callf file-name-unquote filter)
102 ;; The filter count must fit into an `unsigned short'. A bigger
103 ;; file should be rejected.
104 (should-not
105 (eql (call-process emacs nil nil nil
106 "--quick" "--batch"
107 (concat "--seccomp=" filter))
108 0)))))
109
110(ert-deftest emacs-tests/seccomp/invalid-file-size ()
111 (skip-unless (string-match-p (rx bow "SECCOMP" eow)
112 system-configuration-features))
113 (let ((emacs
114 (expand-file-name invocation-name invocation-directory))
115 (process-environment nil))
116 (skip-unless (file-executable-p emacs))
117 (emacs-tests--with-temp-file filter ("seccomp-invalid-" ".bpf"
118 "123456")
119 ;; The --seccomp option is processed early, without filename
120 ;; handlers. Therefore remote or quoted filenames wouldn't
121 ;; work.
122 (should-not (file-remote-p filter))
123 (cl-callf file-name-unquote filter)
124 ;; The Seccomp filter file must have a file size that's a
125 ;; multiple of the size of struct sock_filter, which is 8 or 16,
126 ;; but never 6.
127 (should-not
128 (eql (call-process emacs nil nil nil
129 "--quick" "--batch"
130 (concat "--seccomp=" filter))
131 0)))))
132
133(ert-deftest emacs-tests/seccomp/allows-stdout ()
134 (skip-unless (string-match-p (rx bow "SECCOMP" eow)
135 system-configuration-features))
136 (let ((emacs
137 (expand-file-name invocation-name invocation-directory))
138 (filter (ert-resource-file "seccomp-filter.bpf"))
139 (process-environment nil))
140 (skip-unless (file-executable-p emacs))
141 (skip-unless (file-readable-p filter))
142 ;; The --seccomp option is processed early, without filename
143 ;; handlers. Therefore remote or quoted filenames wouldn't work.
144 (should-not (file-remote-p filter))
145 (cl-callf file-name-unquote filter)
146 (with-temp-buffer
147 (let ((status (call-process
148 emacs nil t nil
149 "--quick" "--batch"
150 (concat "--seccomp=" filter)
151 (format "--eval=%S" '(message "Hi")))))
152 (ert-info ((format "Process output: %s" (buffer-string)))
153 (should (eql status 0)))
154 (should (equal (string-trim (buffer-string)) "Hi"))))))
155
156(ert-deftest emacs-tests/seccomp/forbids-subprocess ()
157 (skip-unless (string-match-p (rx bow "SECCOMP" eow)
158 system-configuration-features))
159 (let ((emacs
160 (expand-file-name invocation-name invocation-directory))
161 (filter (ert-resource-file "seccomp-filter.bpf"))
162 (process-environment nil))
163 (skip-unless (file-executable-p emacs))
164 (skip-unless (file-readable-p filter))
165 ;; The --seccomp option is processed early, without filename
166 ;; handlers. Therefore remote or quoted filenames wouldn't work.
167 (should-not (file-remote-p filter))
168 (cl-callf file-name-unquote filter)
169 (with-temp-buffer
170 (let ((status
171 (call-process
172 emacs nil t nil
173 "--quick" "--batch"
174 (concat "--seccomp=" filter)
175 (format "--eval=%S" `(call-process ,emacs nil nil nil
176 "--version")))))
177 (ert-info ((format "Process output: %s" (buffer-string)))
178 (should-not (eql status 0)))))))
179
180(ert-deftest emacs-tests/bwrap/allows-stdout ()
181 (let ((bash (executable-find "bash"))
182 (bwrap (executable-find "bwrap"))
183 (emacs
184 (expand-file-name invocation-name invocation-directory))
185 (filter (ert-resource-file "seccomp-filter-exec.bpf"))
186 (process-environment nil))
187 (skip-unless bash)
188 (skip-unless bwrap)
189 (skip-unless (file-executable-p emacs))
190 (skip-unless (file-readable-p filter))
191 (should-not (file-remote-p bwrap))
192 (should-not (file-remote-p emacs))
193 (should-not (file-remote-p filter))
194 (with-temp-buffer
195 (let* ((command
196 (concat
197 (mapconcat #'shell-quote-argument
198 `(,(file-name-unquote bwrap)
199 "--ro-bind" "/" "/"
200 "--seccomp" "20"
201 "--"
202 ,(file-name-unquote emacs)
203 "--quick" "--batch"
204 ,(format "--eval=%S" '(message "Hi")))
205 " ")
206 " 20< "
207 (shell-quote-argument (file-name-unquote filter))))
208 (status (call-process bash nil t nil "-c" command)))
209 (ert-info ((format "Process output: %s" (buffer-string)))
210 (should (eql status 0)))
211 (should (equal (string-trim (buffer-string)) "Hi"))))))
212
213;;; emacs-tests.el ends here