unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 9599c26783359167c5c7944148215c8c6d4537e3 5600 bytes (raw)
name: test/src/emacs-tests.el 	 # note: path name is non-authoritative(*)

  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
125
126
127
128
129
130
 
;;; emacs-tests.el --- unit tests for emacs.c -*- lexical-binding: t; -*-

;; Copyright (C) 2020  Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

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

;; GNU Emacs 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 General Public License for more details.

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

;;; Commentary:

;; Unit tests for src/emacs.c.

;;; Code:

(require 'cl-lib)
(require 'ert)
(require 'ert-x)
(require 'subr-x)

(ert-deftest emacs-tests/seccomp/absent-file ()
  (let ((emacs (expand-file-name invocation-name invocation-directory))
        (process-environment nil))
    (skip-unless (file-executable-p emacs))
    (should-not (file-exists-p "/does-not-exist.bpf"))
    (should-not
     (eql (call-process emacs nil nil nil
                        "--quick" "--batch" "--seccomp=/does-not-exist.bpf")
          0))))

(cl-defmacro emacs-tests--with-temp-file
    (var (prefix &optional suffix text) &rest body)
  "Evaluate BODY while a new temporary file exists.
Bind VAR to the name of the file.  Pass PREFIX, SUFFIX, and TEXT
to `make-temp-file', which see."
  (declare (indent 2) (debug (symbolp (form form form) body)))
  (cl-check-type var symbol)
  ;; Use an uninterned symbol so that the code still works if BODY changes VAR.
  (let ((filename (make-symbol "filename")))
    `(let ((,filename (make-temp-file ,prefix nil ,suffix ,text)))
       (unwind-protect
           (let ((,var ,filename))
             ,@body)
         (delete-file ,filename)))))

(ert-deftest emacs-tests/seccomp/empty-file ()
  (let ((emacs (expand-file-name invocation-name invocation-directory))
        (process-environment nil))
    (skip-unless (file-executable-p emacs))
    (emacs-tests--with-temp-file filter ("seccomp-invalid-" ".bpf")
      ;; The --seccomp option is processed early, without filename handlers.
      ;; Therefore remote or quoted filenames wouldn't work.
      (should-not (file-remote-p filter))
      (cl-callf file-name-unquote filter)
      ;; According to the Seccomp man page, a filter must have at least one
      ;; element, so Emacs should reject an empty file.
      (should-not
       (eql (call-process emacs nil nil nil
                          "--quick" "--batch" (concat "--seccomp=" filter))
            0)))))

(ert-deftest emacs-tests/seccomp/invalid-file-size ()
  (let ((emacs (expand-file-name invocation-name invocation-directory))
        (process-environment nil))
    (skip-unless (file-executable-p emacs))
    (emacs-tests--with-temp-file filter ("seccomp-invalid-" ".bpf" "123456")
      ;; The --seccomp option is processed early, without filename handlers.
      ;; Therefore remote or quoted filenames wouldn't work.
      (should-not (file-remote-p filter))
      (cl-callf file-name-unquote filter)
      ;; The Seccomp filter file must have a file size that's a multiple of the
      ;; size of struct sock_filter, which is 8 or 16, but never 6.
      (should-not
       (eql (call-process emacs nil nil nil
                          "--quick" "--batch" (concat "--seccomp=" filter))
            0)))))

(ert-deftest emacs-tests/seccomp/allows-stdout ()
  (let ((emacs (expand-file-name invocation-name invocation-directory))
        (filter (ert-resource-file "seccomp-filter.bpf"))
        (process-environment nil))
    (skip-unless (file-executable-p emacs))
    (skip-unless (file-readable-p filter))
    ;; The --seccomp option is processed early, without filename handlers.
    ;; Therefore remote or quoted filenames wouldn't work.
    (should-not (file-remote-p filter))
    (cl-callf file-name-unquote filter)
    (with-temp-buffer
      (let ((status (call-process emacs nil t nil
                                  "--quick" "--batch"
                                  (concat "--seccomp=" filter)
                                  (concat "--eval=" (prin1-to-string
                                                     '(message "Hi"))))))
        (ert-info ((format "Process output: %s" (buffer-string)))
          (should (eql status 0)))
        (should (equal (string-trim (buffer-string)) "Hi"))))))

(ert-deftest emacs-tests/seccomp/forbids-subprocess ()
  (let ((emacs (expand-file-name invocation-name invocation-directory))
        (filter (ert-resource-file "seccomp-filter.bpf"))
        (process-environment nil))
    (skip-unless (file-executable-p emacs))
    (skip-unless (file-readable-p filter))
    ;; The --seccomp option is processed early, without filename handlers.
    ;; Therefore remote or quoted filenames wouldn't work.
    (should-not (file-remote-p filter))
    (cl-callf file-name-unquote filter)
    (with-temp-buffer
      (let ((status
             (call-process
              emacs nil t nil
              "--quick" "--batch"
              (concat "--seccomp=" filter)
              (concat "--eval=" (prin1-to-string
                                 `(call-process ,emacs nil nil nil
                                                "--version"))))))
        (ert-info ((format "Process output: %s" (buffer-string)))
          (should-not (eql status 0)))))))

;;; emacs-tests.el ends here

debug log:

solving 9599c26783 ...
found 9599c26783 in https://yhetil.org/emacs-bugs/CAArVCkREr5S=k42mXhW8dMzAtmiv7AG8m5yTSFZ72Bey14=9tw@mail.gmail.com/
found 279ecb210c in https://yhetil.org/emacs-bugs/CAArVCkSiTR6UBT3AcKYOe3Yr8TgizjVBOobF_PDv0JNUySj2CA@mail.gmail.com/

applying [1/2] https://yhetil.org/emacs-bugs/CAArVCkSiTR6UBT3AcKYOe3Yr8TgizjVBOobF_PDv0JNUySj2CA@mail.gmail.com/
diff --git a/test/src/emacs-tests.el b/test/src/emacs-tests.el
new file mode 100644
index 0000000000..279ecb210c


applying [2/2] https://yhetil.org/emacs-bugs/CAArVCkREr5S=k42mXhW8dMzAtmiv7AG8m5yTSFZ72Bey14=9tw@mail.gmail.com/
diff --git a/test/src/emacs-tests.el b/test/src/emacs-tests.el
index 279ecb210c..9599c26783 100644

Checking patch test/src/emacs-tests.el...
Applied patch test/src/emacs-tests.el cleanly.
Checking patch test/src/emacs-tests.el...
Applied patch test/src/emacs-tests.el cleanly.

index at:
100644 9599c26783359167c5c7944148215c8c6d4537e3	test/src/emacs-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).