From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matt Armstrong Newsgroups: gmane.emacs.bugs Subject: bug#46397: [PATCH] Re: bug#46397: 27.1; Cannot delete buffer pointing to a file in a path that includes a file Date: Mon, 22 Feb 2021 11:24:56 -0800 Message-ID: References: <87h7mllgin.fsf@nexoid.at> <83a6scj745.fsf@gnu.org> <39d0e035-27b6-e2bd-daa2-747dda2c1a35@cs.ucla.edu> <83tuqhg3j9.fsf@gnu.org> <83czx4e5h5.fsf@gnu.org> <83zh06a1yv.fsf@gnu.org> <83wnv99xkz.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="14100"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 46397@debbugs.gnu.org, eggert@cs.ucla.edu, craven@gmx.net To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Feb 22 20:26:32 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lEGqm-0003Yk-Gg for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 22 Feb 2021 20:26:32 +0100 Original-Received: from localhost ([::1]:51856 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lEGql-0004JU-9f for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 22 Feb 2021 14:26:31 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:54028) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lEGqK-0004IS-A4 for bug-gnu-emacs@gnu.org; Mon, 22 Feb 2021 14:26:04 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:46031) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lEGqI-0003qp-K7 for bug-gnu-emacs@gnu.org; Mon, 22 Feb 2021 14:26:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lEGqI-0006FJ-Gr for bug-gnu-emacs@gnu.org; Mon, 22 Feb 2021 14:26:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Matt Armstrong Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 22 Feb 2021 19:26:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 46397 X-GNU-PR-Package: emacs Original-Received: via spool by 46397-submit@debbugs.gnu.org id=B46397.161402191223949 (code B ref 46397); Mon, 22 Feb 2021 19:26:02 +0000 Original-Received: (at 46397) by debbugs.gnu.org; 22 Feb 2021 19:25:12 +0000 Original-Received: from localhost ([127.0.0.1]:57577 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lEGpT-0006EB-VF for submit@debbugs.gnu.org; Mon, 22 Feb 2021 14:25:12 -0500 Original-Received: from relay10.mail.gandi.net ([217.70.178.230]:44607) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lEGpR-0006Du-Io for 46397@debbugs.gnu.org; Mon, 22 Feb 2021 14:25:10 -0500 Original-Received: from matts-mbp-2016.lan (24-113-169-116.wavecable.com [24.113.169.116]) (Authenticated sender: matt@rfc20.org) by relay10.mail.gandi.net (Postfix) with ESMTPSA id 91F18240005; Mon, 22 Feb 2021 19:25:00 +0000 (UTC) In-Reply-To: X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:200614 Archived-At: --=-=-= Content-Type: text/plain Tags: patch Attached is a rev of my prior patch to add some test coverage for src/filelock.c. P.S. my FSF copyright papers are done. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Add-some-test-coverage-for-src-filelock.c-Bug-46397.patch >From 18529d097624e0d4647cf04118d4ce98adc474ba Mon Sep 17 00:00:00 2001 From: Matt Armstrong Date: Mon, 15 Feb 2021 12:59:08 -0800 Subject: [PATCH] Add some test coverage for src/filelock.c (Bug#46397). * test/src/filelock-tests.el: New file. --- test/src/filelock-tests.el | 148 +++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 test/src/filelock-tests.el diff --git a/test/src/filelock-tests.el b/test/src/filelock-tests.el new file mode 100644 index 0000000000..d9534e2d16 --- /dev/null +++ b/test/src/filelock-tests.el @@ -0,0 +1,148 @@ +;;; filelock-tests.el --- test file locking -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 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 this program. If not, see . + +;;; Commentary: + +;; This file tests code in src/filelock.c and related buffer and file +;; I/O code. + +;;; Code: + +(require 'ert) + +(defun filelock-tests--supported-p () + "Return t if the current system implements file locking." + (not (memq system-type '(ms-dos)))) + +(defun filelock-tests--call-with-fixture (body) + "Call BODY under a standard test fixture. +See `filelock-tests--with-fixture'." + (let* ((test-dir (make-temp-file "filelock-tests-" t)) + (test-dir-modes (file-modes test-dir))) + (unwind-protect + (let ((file-name (file-truename + (concat (file-name-as-directory test-dir) + "file")))) + (with-temp-buffer + (let ((temp-buffer (current-buffer))) + ;; Setting both file names is sufficient to enable file + ;; locks. + (setq buffer-file-name file-name + buffer-file-truename file-name) + (unwind-protect + (let ((create-lockfiles t)) + (funcall body)) + (set-file-modes test-dir test-dir-modes) + (when (buffer-live-p temp-buffer) + (with-current-buffer temp-buffer + (set-buffer-modified-p nil))))))) + (delete-directory test-dir t nil)))) + +(defmacro filelock-tests--with-fixture (&rest body) + "Create a test fixture evaluate BODY there like `progn'. +Create a temporary directory, then create a temporary buffer and +set it the variable `buffer-file-name' and `buffer-file-truename' +to it, let bind `create-lockfiles' to 't' and evaluate BODY. +Finally, delete the temporary directory." + (declare (indent 0)) + `(filelock-tests--call-with-fixture + (lambda () ,@body))) + +(ert-deftest filelock-tests-when-create-lockfiles-nil () + (filelock-tests--with-fixture + (let ((create-lockfiles nil)) + (should (not (file-locked-p buffer-file-truename))) + (insert "some text") + (lock-buffer) + (should (not (file-locked-p buffer-file-truename)))))) + +(ert-deftest filelock-tests-when-create-lockfiles-t () + (filelock-tests--with-fixture + (should (equal t create-lockfiles)) + (insert "some text") + (lock-buffer) + ;; Verify 't' explicitly and not any true value; 't' alone means + ;; the current process owns the lock. + (let ((expected (if (filelock-tests--supported-p) + t + nil))) + (should (equal expected (file-locked-p buffer-file-truename))) + (unlock-buffer) + (should (equal nil (file-locked-p buffer-file-truename)))))) + +(ert-deftest filelock-tests-file-locked-p-inaccessible-dir () + (skip-unless (filelock-tests--supported-p)) + (filelock-tests--with-fixture + (set-file-modes (file-name-directory (buffer-file-name)) + (file-modes-symbolic-to-number "ugo=")) + (set-buffer-modified-p t) + (let ((full-error (should-error (file-locked-p (buffer-file-name)) + :type 'file-error))) + (should (equal "Testing file lock" (nth 1 full-error)))))) + +(ert-deftest filelock-tests-lock-buffer-inaccessible-dir () + (skip-unless (filelock-tests--supported-p)) + (filelock-tests--with-fixture + (let* ((test-dir (file-name-directory (buffer-file-name))) + (original-modes (file-modes test-dir))) + (set-file-modes test-dir (file-modes-symbolic-to-number "ugo=")) + (insert "some text") + ;; FIXME: (lock-buffer) should signal an error here. + (lock-buffer) + (set-file-modes test-dir original-modes) + (should (equal nil (file-locked-p (buffer-file-name))))))) + +(ert-deftest filelock-tests-lock-buffer-dir-not-writeable () + (skip-unless (filelock-tests--supported-p)) + (filelock-tests--with-fixture + (set-file-modes (file-name-directory (buffer-file-name)) + (file-modes-symbolic-to-number "u=rx")) + (insert "some text") + ;; FIXME: (lock-buffer) should signal here, but lock_file() in + ;; filelock.c ignores some file system errors; see the related + ;; FIXME there. + (lock-buffer) + (should (equal nil (file-locked-p (buffer-file-name)))))) + +(ert-deftest filelock-tests-unlock-buffer-inaccessible-dir () + (skip-unless (filelock-tests--supported-p)) + (filelock-tests--with-fixture + (insert "some text") + (lock-buffer) + (should (file-locked-p (buffer-file-name))) + (set-file-modes (file-name-directory (buffer-file-name)) + (file-modes-symbolic-to-number "ugo=")) + (let ((full-error (should-error (unlock-buffer) + :type 'file-error))) + (should (equal "Unlocking file" (nth 1 full-error)))))) + +(ert-deftest filelock-tests-unlock-buffer-dir-not-writeable () + (skip-unless (filelock-tests--supported-p)) + (filelock-tests--with-fixture + (insert "some text") + (should (file-locked-p (buffer-file-name))) + (set-file-modes (file-name-directory (buffer-file-name)) + (file-modes-symbolic-to-number "u=rx")) + (let ((full-error (should-error (unlock-buffer) + :type 'file-error))) + (should (equal "Unlocking file" (nth 1 full-error)))))) + +(provide 'filelock-tests) + +;;; filelock-tests.el ends here -- 2.30.0 --=-=-=--