tramp-archive-tests.el 33.2 KB
Newer Older
1 2
;;; tramp-archive-tests.el --- Tests of file archive access  -*- lexical-binding:t -*-

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2017-2019 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

;; Author: Michael Albinus <michael.albinus@gmx.de>

;; This program 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.
;;
;; This program 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 `https://www.gnu.org/licenses/'.

20 21 22 23
;;; Commentary:

;; A testsuite for testing file archives.

24 25
;;; Code:

Michael Albinus's avatar
Michael Albinus committed
26 27 28
;; The `tramp-archive-testnn-*' tests correspond to the respective
;; tests in tramp-tests.el.

29 30
(require 'ert)
(require 'tramp-archive)
31 32
(defvar tramp-copy-size-limit)
(defvar tramp-persistency-file-name)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

(defconst tramp-archive-test-resource-directory
  (let ((default-directory
	  (if load-in-progress
	      (file-name-directory load-file-name)
	    default-directory)))
    (cond
     ((file-accessible-directory-p (expand-file-name "resources"))
      (expand-file-name "resources"))
     ((file-accessible-directory-p (expand-file-name "tramp-archive-resources"))
      (expand-file-name "tramp-archive-resources"))))
  "The resources directory test files are located in.")

(defconst tramp-archive-test-file-archive
  (file-truename
   (expand-file-name "foo.tar.gz" tramp-archive-test-resource-directory))
  "The test file archive.")

(defconst tramp-archive-test-archive
  (file-name-as-directory tramp-archive-test-file-archive)
  "The test archive.")

Michael Albinus's avatar
Michael Albinus committed
55 56 57 58 59
(defconst tramp-archive-test-directory
  (file-truename
   (expand-file-name "foo.iso" tramp-archive-test-resource-directory))
  "A directory file name, which looks like an archive.")

60 61 62 63
(setq password-cache-expiry nil
      tramp-cache-read-persistent-data t ;; For auth-sources.
      tramp-copy-size-limit nil
      tramp-message-show-message nil
64 65
      tramp-persistency-file-name nil
      tramp-verbose 0)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

(defun tramp-archive--test-make-temp-name ()
  "Return a temporary file name for test.
The temporary file is not created."
  (expand-file-name
   (make-temp-name "tramp-archive-test") temporary-file-directory))

(defun tramp-archive--test-delete (tmpfile)
  "Delete temporary file or directory TMPFILE.
This needs special support, because archive file names, which are
the origin of the temporary TMPFILE, have no write permissions."
  (unless (file-writable-p (file-name-directory tmpfile))
    (set-file-modes
     (file-name-directory tmpfile)
     (logior (file-modes (file-name-directory tmpfile)) #o0700)))
  (set-file-modes tmpfile #o0700)
  (if (file-regular-p tmpfile)
      (delete-file tmpfile)
    (mapc
85
     #'tramp-archive--test-delete
86 87 88 89 90 91 92 93 94
     (directory-files tmpfile 'full directory-files-no-dot-files-regexp))
    (delete-directory tmpfile)))

(defun tramp-archive--test-emacs26-p ()
  "Check for Emacs version >= 26.1.
Some semantics has been changed for there, w/o new functions or
variables, so we check the Emacs version directly."
  (>= emacs-major-version 26))

95 96 97 98 99 100
(defun tramp-archive--test-emacs27-p ()
  "Check for Emacs version >= 27.1.
Some semantics has been changed for there, w/o new functions or
variables, so we check the Emacs version directly."
  (>= emacs-major-version 27))

101
(ert-deftest tramp-archive-test00-availability ()
102 103
  "Test availability of archive file name functions."
  :expected-result (if tramp-archive-enabled :passed :failed)
104 105
  (should
   (and
106
    tramp-archive-enabled
107 108 109 110 111 112 113
    (file-exists-p tramp-archive-test-file-archive)
    (tramp-archive-file-name-p tramp-archive-test-archive))))

(ert-deftest tramp-archive-test01-file-name-syntax ()
  "Check archive file name syntax."
  (should-not (tramp-archive-file-name-p tramp-archive-test-file-archive))
  (should (tramp-archive-file-name-p tramp-archive-test-archive))
Michael Albinus's avatar
Michael Albinus committed
114 115 116 117 118 119 120
  (should
   (string-equal
    (tramp-archive-file-name-archive tramp-archive-test-archive)
    tramp-archive-test-file-archive))
  (should
   (string-equal
    (tramp-archive-file-name-localname tramp-archive-test-archive) "/"))
121
  (should (tramp-archive-file-name-p (concat tramp-archive-test-archive "foo")))
Michael Albinus's avatar
Michael Albinus committed
122 123 124 125 126
  (should
   (string-equal
    (tramp-archive-file-name-localname
     (concat tramp-archive-test-archive "foo"))
    "/foo"))
127 128
  (should
   (tramp-archive-file-name-p (concat tramp-archive-test-archive "foo/bar")))
Michael Albinus's avatar
Michael Albinus committed
129 130 131 132 133
  (should
   (string-equal
    (tramp-archive-file-name-localname
     (concat tramp-archive-test-archive "foo/bar"))
    "/foo/bar"))
134 135
  ;; A file archive inside a file archive.
  (should
Michael Albinus's avatar
Michael Albinus committed
136
   (tramp-archive-file-name-p (concat tramp-archive-test-archive "baz.tar")))
137
  (should
Michael Albinus's avatar
Michael Albinus committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
   (string-equal
    (tramp-archive-file-name-archive
     (concat tramp-archive-test-archive "baz.tar"))
    tramp-archive-test-file-archive))
  (should
   (string-equal
    (tramp-archive-file-name-localname
     (concat tramp-archive-test-archive "baz.tar"))
    "/baz.tar"))
  (should
   (tramp-archive-file-name-p (concat tramp-archive-test-archive "baz.tar/")))
  (should
   (string-equal
    (tramp-archive-file-name-archive
     (concat tramp-archive-test-archive "baz.tar/"))
    (concat tramp-archive-test-archive "baz.tar")))
  (should
   (string-equal
    (tramp-archive-file-name-localname
     (concat tramp-archive-test-archive "baz.tar/"))
    "/")))
159 160 161

(ert-deftest tramp-archive-test02-file-name-dissect ()
  "Check archive file name components."
162
  (skip-unless tramp-archive-enabled)
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  ;; Suppress method name check.
  (let ((non-essential t))
    (with-parsed-tramp-archive-file-name tramp-archive-test-archive nil
      (should (string-equal method tramp-archive-method))
      (should-not user)
      (should-not domain)
      (should
       (string-equal
	host
	(file-remote-p
	 (tramp-archive-gvfs-file-name tramp-archive-test-archive) 'host)))
      (should
       (string-equal
	host
	(url-hexify-string (concat "file://" tramp-archive-test-file-archive))))
      (should-not port)
      (should (string-equal localname "/"))
      (should (string-equal archive tramp-archive-test-file-archive)))
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    ;; Localname.
    (with-parsed-tramp-archive-file-name
	(concat tramp-archive-test-archive "foo") nil
      (should (string-equal method tramp-archive-method))
      (should-not user)
      (should-not domain)
      (should
       (string-equal
	host
	(file-remote-p
	 (tramp-archive-gvfs-file-name tramp-archive-test-archive) 'host)))
      (should
       (string-equal
	host
	(url-hexify-string (concat "file://" tramp-archive-test-file-archive))))
      (should-not port)
      (should (string-equal localname "/foo"))
      (should (string-equal archive tramp-archive-test-file-archive)))
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    ;; File archive in file archive.
    (let* ((tramp-archive-test-file-archive
	    (concat tramp-archive-test-archive "baz.tar"))
	   (tramp-archive-test-archive
	    (file-name-as-directory tramp-archive-test-file-archive))
	   (tramp-methods (cons `(,tramp-archive-method) tramp-methods))
	   (tramp-gvfs-methods tramp-archive-all-gvfs-methods))
      (unwind-protect
	  (with-parsed-tramp-archive-file-name
	      (expand-file-name "bar" tramp-archive-test-archive) nil
	    (should (string-equal method tramp-archive-method))
	    (should-not user)
	    (should-not domain)
	    (should
	     (string-equal
	      host
	      (file-remote-p
	       (tramp-archive-gvfs-file-name tramp-archive-test-archive)
	       'host)))
	    ;; We reimplement the logic of tramp-archive.el here.
	    ;; Don't know, whether it is worth the test.
	    (should
	     (string-equal
	      host
	      (url-hexify-string
	       (concat
		(tramp-gvfs-url-file-name
		 (tramp-make-tramp-file-name
		  tramp-archive-method
		  ;; User and Domain.
		  nil nil
		  ;; Host.
		  (url-hexify-string
		   (concat
		    "file://"
		    ;; `directory-file-name' does not leave file
		    ;; archive boundaries.  So we must cut the
		    ;; trailing slash ourselves.
		    (substring
		     (file-name-directory tramp-archive-test-file-archive)
		     0 -1)))
		  nil "/"))
		(file-name-nondirectory tramp-archive-test-file-archive)))))
	    (should-not port)
	    (should (string-equal localname "/bar"))
	    (should (string-equal archive tramp-archive-test-file-archive)))
248

249 250
	;; Cleanup.
	(tramp-archive-cleanup-hash)))))
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

(ert-deftest tramp-archive-test05-expand-file-name ()
  "Check `expand-file-name'."
  (should
   (string-equal
    (expand-file-name "/foo.tar/path/./file") "/foo.tar/path/file"))
  (should
   (string-equal (expand-file-name "/foo.tar/path/../file") "/foo.tar/file"))
  ;; `expand-file-name' does not care "~/" in archive file names.
  (should
   (string-equal (expand-file-name "/foo.tar/~/file") "/foo.tar/~/file"))
  ;; `expand-file-name' does not care file archive boundaries.
  (should (string-equal (expand-file-name "/foo.tar/./file") "/foo.tar/file"))
  (should (string-equal (expand-file-name "/foo.tar/../file") "/file")))

Michael Albinus's avatar
Michael Albinus committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
;; This test is inspired by Bug#30293.
(ert-deftest tramp-archive-test05-expand-file-name-non-archive-directory ()
  "Check existing directories with archive file name syntax.
They shall still be supported"
  (should (file-directory-p tramp-archive-test-directory))
  ;; `tramp-archive-file-name-p' tests only for file name syntax.  It
  ;; doesn't test, whether it is really a file archive.
  (should
   (tramp-archive-file-name-p
    (file-name-as-directory tramp-archive-test-directory)))
  (should
   (file-directory-p (file-name-as-directory tramp-archive-test-directory)))
  (should
   (file-exists-p (expand-file-name "foo" tramp-archive-test-directory))))

281 282 283 284
(ert-deftest tramp-archive-test06-directory-file-name ()
  "Check `directory-file-name'.
This checks also `file-name-as-directory', `file-name-directory',
`file-name-nondirectory' and `unhandled-file-name-directory'."
285
  (skip-unless tramp-archive-enabled)
Michael Albinus's avatar
Michael Albinus committed
286

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
  (should
   (string-equal
    (directory-file-name "/foo.tar/path/to/file") "/foo.tar/path/to/file"))
  (should
   (string-equal
    (directory-file-name "/foo.tar/path/to/file/") "/foo.tar/path/to/file"))
  ;; `directory-file-name' does not leave file archive boundaries.
  (should (string-equal (directory-file-name "/foo.tar/") "/foo.tar/"))

  (should
   (string-equal
    (file-name-as-directory "/foo.tar/path/to/file") "/foo.tar/path/to/file/"))
  (should
   (string-equal
    (file-name-as-directory "/foo.tar/path/to/file/") "/foo.tar/path/to/file/"))
  (should (string-equal (file-name-as-directory "/foo.tar/") "/foo.tar/"))
  (should (string-equal (file-name-as-directory "/foo.tar") "/foo.tar/"))

  (should
   (string-equal
    (file-name-directory "/foo.tar/path/to/file") "/foo.tar/path/to/"))
  (should
   (string-equal
    (file-name-directory "/foo.tar/path/to/file/") "/foo.tar/path/to/file/"))
  (should (string-equal (file-name-directory "/foo.tar/") "/foo.tar/"))

  (should
   (string-equal (file-name-nondirectory "/foo.tar/path/to/file") "file"))
  (should
   (string-equal (file-name-nondirectory "/foo.tar/path/to/file/") ""))
  (should (string-equal (file-name-nondirectory "/foo.tar/") ""))

  (should-not
   (unhandled-file-name-directory "/foo.tar/path/to/file")))

(ert-deftest tramp-archive-test07-file-exists-p ()
  "Check `file-exist-p', `write-region' and `delete-file'."
324
  :tags '(:expensive-test)
325
  (skip-unless tramp-archive-enabled)
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

  (unwind-protect
      (let ((default-directory tramp-archive-test-archive))
	(should (file-exists-p tramp-archive-test-file-archive))
	(should (file-exists-p tramp-archive-test-archive))
	(should (file-exists-p "foo.txt"))
	(should (file-exists-p "foo.lnk"))
	(should (file-exists-p "bar"))
	(should (file-exists-p "bar/bar"))
	(should-error
	 (write-region "foo" nil "baz")
	 :type 'file-error)
	(should-error
	 (delete-file "baz")
	 :type 'file-error))

    ;; Cleanup.
    (tramp-archive-cleanup-hash)))

(ert-deftest tramp-archive-test08-file-local-copy ()
  "Check `file-local-copy'."
347
  :tags '(:expensive-test)
348
  (skip-unless tramp-archive-enabled)
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

  (let (tmp-name)
    (unwind-protect
	(progn
	  (should
	   (setq tmp-name
		 (file-local-copy
		  (expand-file-name "bar/bar" tramp-archive-test-archive))))
	  (with-temp-buffer
	    (insert-file-contents tmp-name)
	    (should (string-equal (buffer-string) "bar\n")))
	    ;; Error case.
	    (tramp-archive--test-delete tmp-name)
	    (should-error
	     (setq tmp-name
		   (file-local-copy
		    (expand-file-name "what" tramp-archive-test-archive)))
	     :type tramp-file-missing))

      ;; Cleanup.
Michael Albinus's avatar
Michael Albinus committed
369 370
      (ignore-errors (tramp-archive--test-delete tmp-name))
      (tramp-archive-cleanup-hash))))
371 372 373

(ert-deftest tramp-archive-test09-insert-file-contents ()
  "Check `insert-file-contents'."
374
  :tags '(:expensive-test)
375
  (skip-unless tramp-archive-enabled)
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

  (let ((tmp-name (expand-file-name "bar/bar" tramp-archive-test-archive)))
    (unwind-protect
	(with-temp-buffer
	  (insert-file-contents tmp-name)
	  (should (string-equal (buffer-string) "bar\n"))
	  (insert-file-contents tmp-name)
	  (should (string-equal (buffer-string) "bar\nbar\n"))
	  ;; Insert partly.
	  (insert-file-contents tmp-name nil 1 3)
	  (should (string-equal (buffer-string) "arbar\nbar\n"))
	  ;; Replace.
	  (insert-file-contents tmp-name nil nil nil 'replace)
	  (should (string-equal (buffer-string) "bar\n"))
	  ;; Error case.
	  (should-error
	   (insert-file-contents
	    (expand-file-name "what" tramp-archive-test-archive))
	   :type tramp-file-missing))

	;; Cleanup.
	(tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test11-copy-file ()
  "Check `copy-file'."
401
  :tags '(:expensive-test)
402
  (skip-unless tramp-archive-enabled)
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

  ;; Copy simple file.
  (let ((tmp-name1 (expand-file-name "bar/bar" tramp-archive-test-archive))
	(tmp-name2 (tramp-archive--test-make-temp-name)))
    (unwind-protect
	(progn
	  (copy-file tmp-name1 tmp-name2)
	  (should (file-exists-p tmp-name2))
	  (with-temp-buffer
	    (insert-file-contents tmp-name2)
	    (should (string-equal (buffer-string) "bar\n")))
	  (should-error
	   (copy-file tmp-name1 tmp-name2)
	   :type 'file-already-exists)
	  (copy-file tmp-name1 tmp-name2 'ok)
	  ;; The file archive is not writable.
	  (should-error
	   (copy-file tmp-name2 tmp-name1 'ok)
	   :type 'file-error))

      ;; Cleanup.
      (ignore-errors (tramp-archive--test-delete tmp-name2))
      (tramp-archive-cleanup-hash)))

  ;; Copy directory to existing directory.
  (let ((tmp-name1 (expand-file-name "bar" tramp-archive-test-archive))
	(tmp-name2 (tramp-archive--test-make-temp-name)))
    (unwind-protect
	(progn
	  (make-directory tmp-name2)
	  (should (file-directory-p tmp-name2))
	  ;; Directory `tmp-name2' exists already, so we must use
	  ;; `file-name-as-directory'.
	  (copy-file tmp-name1 (file-name-as-directory tmp-name2))
	  (should
	   (file-exists-p
	    (expand-file-name
	     (concat (file-name-nondirectory tmp-name1) "/bar") tmp-name2))))

      ;; Cleanup.
      (ignore-errors (tramp-archive--test-delete tmp-name2))
      (tramp-archive-cleanup-hash)))

  ;; Copy directory/file to non-existing directory.
  (let ((tmp-name1 (expand-file-name "bar" tramp-archive-test-archive))
	(tmp-name2 (tramp-archive--test-make-temp-name)))
    (unwind-protect
	(progn
	  (make-directory tmp-name2)
	  (should (file-directory-p tmp-name2))
	  (copy-file
	   tmp-name1
	   (expand-file-name (file-name-nondirectory tmp-name1) tmp-name2))
	  (should
	   (file-exists-p
	    (expand-file-name
	     (concat (file-name-nondirectory tmp-name1) "/bar") tmp-name2))))

      ;; Cleanup.
      (ignore-errors (tramp-archive--test-delete tmp-name2))
      (tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test15-copy-directory ()
  "Check `copy-directory'."
467
  :tags '(:expensive-test)
468
  (skip-unless tramp-archive-enabled)
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

  (let* ((tmp-name1 (expand-file-name "bar" tramp-archive-test-archive))
	 (tmp-name2 (tramp-archive--test-make-temp-name))
	 (tmp-name3 (expand-file-name
		     (file-name-nondirectory tmp-name1) tmp-name2))
	 (tmp-name4 (expand-file-name "bar" tmp-name2))
	 (tmp-name5 (expand-file-name "bar" tmp-name3)))

    ;; Copy complete directory.
    (unwind-protect
	(progn
	  ;; Copy empty directory.
	  (copy-directory tmp-name1 tmp-name2)
	  (should (file-directory-p tmp-name2))
	  (should (file-exists-p tmp-name4))
	  ;; Target directory does exist already.
	  ;; This has been changed in Emacs 26.1.
	  (when (tramp-archive--test-emacs26-p)
	    (should-error
	     (copy-directory tmp-name1 tmp-name2)
	     :type 'file-error))
	  (tramp-archive--test-delete tmp-name4)
	  (copy-directory tmp-name1 (file-name-as-directory tmp-name2))
	  (should (file-directory-p tmp-name3))
	  (should (file-exists-p tmp-name5)))

      ;; Cleanup.
      (ignore-errors (tramp-archive--test-delete tmp-name2))
      (tramp-archive-cleanup-hash))

    ;; Copy directory contents.
    (unwind-protect
        (progn
          ;; Copy empty directory.
          (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
          (should (file-directory-p tmp-name2))
          (should (file-exists-p tmp-name4))
          ;; Target directory does exist already.
          (tramp-archive--test-delete tmp-name4)
          (copy-directory
           tmp-name1 (file-name-as-directory tmp-name2)
           nil 'parents 'contents)
          (should (file-directory-p tmp-name2))
          (should (file-exists-p tmp-name4))
          (should-not (file-directory-p tmp-name3))
          (should-not (file-exists-p tmp-name5)))

      ;; Cleanup.
      (ignore-errors (tramp-archive--test-delete tmp-name2))
      (tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test16-directory-files ()
  "Check `directory-files'."
522
  :tags '(:expensive-test)
523
  (skip-unless tramp-archive-enabled)
524 525

  (let ((tmp-name tramp-archive-test-archive)
526
	(files '("." ".." "bar" "baz.tar" "foo.hrd" "foo.lnk" "foo.txt")))
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    (unwind-protect
	(progn
	  (should (file-directory-p tmp-name))
	  (should (equal (directory-files tmp-name) files))
	  (should (equal (directory-files tmp-name 'full)
			 (mapcar (lambda (x) (concat tmp-name x)) files)))
	  (should (equal (directory-files
			  tmp-name nil directory-files-no-dot-files-regexp)
			 (delete "." (delete ".." files))))
	  (should (equal (directory-files
			  tmp-name 'full directory-files-no-dot-files-regexp)
			 (mapcar (lambda (x) (concat tmp-name x))
				 (delete "." (delete ".." files))))))

      ;; Cleanup.
      (tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test17-insert-directory ()
  "Check `insert-directory'."
546
  :tags '(:expensive-test)
547
  (skip-unless tramp-archive-enabled)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

  (let (;; We test for the summary line.  Keyword "total" could be localized.
	(process-environment
	 (append '("LANG=C" "LANGUAGE=C" "LC_ALL=C") process-environment)))
    (unwind-protect
	(progn
	  ;; Due to Bug#29423, this works only since for Emacs 26.1.
	  (when nil ;; TODO (tramp-archive--test-emacs26-p)
	    (with-temp-buffer
	      (insert-directory tramp-archive-test-archive nil)
	      (goto-char (point-min))
	      (should
	       (looking-at-p (regexp-quote tramp-archive-test-archive)))))
	  (with-temp-buffer
	    (insert-directory tramp-archive-test-archive "-al")
	    (goto-char (point-min))
	    (should
	     (looking-at-p
	      (format "^.+ %s$" (regexp-quote tramp-archive-test-archive)))))
	  (with-temp-buffer
	    (insert-directory
	     (file-name-as-directory tramp-archive-test-archive)
	     "-al" nil 'full-directory-p)
	    (goto-char (point-min))
	    (should
	     (looking-at-p
	      (concat
	       ;; There might be a summary line.
Michael Albinus's avatar
Michael Albinus committed
576
	       "\\(total.+[[:digit:]]+ ?[kKMGTPEZY]?i?B?\n\\)?"
577 578 579 580
	       ;; We don't know in which order the files appear.
	       (format
		"\\(.+ %s\\( ->.+\\)?\n\\)\\{%d\\}"
		(regexp-opt (directory-files tramp-archive-test-archive))
581 582 583 584 585 586 587 588
		(length (directory-files tramp-archive-test-archive)))))))

	  ;; Check error case.
	  (with-temp-buffer
	    (should-error
	     (insert-directory
	      (expand-file-name "baz" tramp-archive-test-archive) nil)
	     :type tramp-file-missing)))
589 590 591 592 593 594

      ;; Cleanup.
      (tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test18-file-attributes ()
  "Check `file-attributes'.
595
This tests also `access-file', `file-readable-p' and `file-regular-p'."
596
  :tags '(:expensive-test)
597
  (skip-unless tramp-archive-enabled)
598 599 600 601

  (let ((tmp-name1 (expand-file-name "foo.txt" tramp-archive-test-archive))
	(tmp-name2 (expand-file-name "foo.lnk" tramp-archive-test-archive))
	(tmp-name3 (expand-file-name "bar" tramp-archive-test-archive))
602
	(tmp-name4 (expand-file-name "baz" tramp-archive-test-archive))
603 604 605 606 607 608
	attr)
    (unwind-protect
	(progn
	  (should (file-exists-p tmp-name1))
	  (should (file-readable-p tmp-name1))
	  (should (file-regular-p tmp-name1))
609
	  (should-not (access-file tmp-name1 "error"))
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

	  ;; We do not test inodes and device numbers.
	  (setq attr (file-attributes tmp-name1))
	  (should (consp attr))
	  (should (null (car attr)))
	  (should (numberp (nth 1 attr))) ;; Link.
	  (should (numberp (nth 2 attr))) ;; Uid.
	  (should (numberp (nth 3 attr))) ;; Gid.
	  ;; Last access time.
	  (should (stringp (current-time-string (nth 4 attr))))
	  ;; Last modification time.
	  (should (stringp (current-time-string (nth 5 attr))))
	  ;; Last status change time.
	  (should (stringp (current-time-string (nth 6 attr))))
	  (should (numberp (nth 7 attr))) ;; Size.
	  (should (stringp (nth 8 attr))) ;; Modes.

	  (setq attr (file-attributes tmp-name1 'string))
	  (should (stringp (nth 2 attr))) ;; Uid.
	  (should (stringp (nth 3 attr))) ;; Gid.

	  ;; Symlink.
	  (should (file-exists-p tmp-name2))
	  (should (file-symlink-p tmp-name2))
	  (setq attr (file-attributes tmp-name2))
	  (should (string-equal (car attr) (file-name-nondirectory tmp-name1)))

	  ;; Directory.
	  (should (file-exists-p tmp-name3))
	  (should (file-readable-p tmp-name3))
	  (should-not (file-regular-p tmp-name3))
	  (setq attr (file-attributes tmp-name3))
642 643 644 645 646 647 648
	  (should (eq (car attr) t))
	  (should-not (access-file tmp-name3 "error"))

	  ;; Check error case.
	  (should-error
	   (access-file tmp-name4  "error")
	   :type tramp-file-missing))
649 650 651 652 653 654

      ;; Cleanup.
      (tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test19-directory-files-and-attributes ()
  "Check `directory-files-and-attributes'."
655
  :tags '(:expensive-test)
656
  (skip-unless tramp-archive-enabled)
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672

  (let ((tmp-name (expand-file-name "bar" tramp-archive-test-archive))
	attr)
    (unwind-protect
	(progn
	  (should (file-directory-p tmp-name))
	  (setq attr (directory-files-and-attributes tmp-name))
	  (should (consp attr))
	  (dolist (elt attr)
	    (should
	     (equal (file-attributes (expand-file-name (car elt) tmp-name))
		    (cdr elt))))
	  (setq attr (directory-files-and-attributes tmp-name 'full))
	  (dolist (elt attr)
	    (should (equal (file-attributes (car elt)) (cdr elt))))
	  (setq attr (directory-files-and-attributes tmp-name nil "^b"))
673
	  (should (equal (mapcar #'car attr) '("bar"))))
674 675 676 677 678 679 680

      ;; Cleanup.
      (tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test20-file-modes ()
  "Check `file-modes'.
This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
681
  :tags '(:expensive-test)
682
  (skip-unless tramp-archive-enabled)
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710

  (let ((tmp-name1 (expand-file-name "foo.txt" tramp-archive-test-archive))
	(tmp-name2 (expand-file-name "bar" tramp-archive-test-archive)))
    (unwind-protect
	(progn
	  (should (file-exists-p tmp-name1))
	  ;; `set-file-modes' is not implemented.
	  (should-error
	   (set-file-modes tmp-name1 #o777)
	   :type 'file-error)
	  (should (= (file-modes tmp-name1) #o400))
	  (should-not (file-executable-p tmp-name1))
	  (should-not (file-writable-p tmp-name1))

	  (should (file-exists-p tmp-name2))
	  ;; `set-file-modes' is not implemented.
	  (should-error
	   (set-file-modes tmp-name2 #o777)
	   :type 'file-error)
	  (should (= (file-modes tmp-name2) #o500))
	  (should (file-executable-p tmp-name2))
	  (should-not (file-writable-p tmp-name2)))

      ;; Cleanup.
      (tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test21-file-links ()
  "Check `file-symlink-p' and `file-truename'"
711
  :tags '(:expensive-test)
712
  (skip-unless tramp-archive-enabled)
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

  ;; We must use `file-truename' for the file archive, because it
  ;; could be located on a symlinked directory.  This would let the
  ;; test fail.
  (let* ((tramp-archive-test-archive (file-truename tramp-archive-test-archive))
	 (tmp-name1 (expand-file-name "foo.txt" tramp-archive-test-archive))
	 (tmp-name2 (expand-file-name "foo.lnk" tramp-archive-test-archive)))

    (unwind-protect
	(progn
	  (should (file-exists-p tmp-name1))
	  (should (string-equal tmp-name1 (file-truename tmp-name1)))
	  ;; `make-symbolic-link' is not implemented.
	  (should-error
	   (make-symbolic-link tmp-name1 tmp-name2)
	   :type 'file-error)
	  (should (file-symlink-p tmp-name2))
	  (should
	   (string-equal
	    ;; This is "/foo.txt".
	    (with-parsed-tramp-archive-file-name tmp-name1 nil localname)
	    ;; `file-symlink-p' returns "foo.txt".  Wer must expand, therefore.
	    (with-parsed-tramp-archive-file-name
		(expand-file-name
		 (file-symlink-p tmp-name2) tramp-archive-test-archive)
		nil
	      localname)))
	  (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
	  (should
	   (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
	  (should (file-equal-p tmp-name1 tmp-name2)))

      ;; Cleanup.
      (tramp-archive-cleanup-hash))))

(ert-deftest tramp-archive-test26-file-name-completion ()
  "Check `file-name-completion' and `file-name-all-completions'."
750
  :tags '(:expensive-test)
751
  (skip-unless tramp-archive-enabled)
752 753 754 755 756 757 758

  (let ((tmp-name tramp-archive-test-archive))
    (unwind-protect
	(progn
	  ;; Local files.
	  (should (equal (file-name-completion "fo" tmp-name) "foo."))
	  (should (equal (file-name-completion "foo.txt" tmp-name) t))
Michael Albinus's avatar
Michael Albinus committed
759
	  (should (equal (file-name-completion "b" tmp-name) "ba"))
760 761 762
	  (should-not (file-name-completion "a" tmp-name))
	  (should
	   (equal
763
	    (file-name-completion "b" tmp-name #'file-directory-p) "bar/"))
764 765
	  (should
	   (equal
766
	    (sort (file-name-all-completions "fo" tmp-name) #'string-lessp)
767 768 769
	    '("foo.hrd" "foo.lnk" "foo.txt")))
	  (should
	   (equal
770
	    (sort (file-name-all-completions "b" tmp-name) #'string-lessp)
Michael Albinus's avatar
Michael Albinus committed
771
	    '("bar/" "baz.tar")))
772 773 774 775 776 777
	  (should-not (file-name-all-completions "a" tmp-name))
	  ;; `completion-regexp-list' restricts the completion to
	  ;; files which match all expressions in this list.
	  (let ((completion-regexp-list
		 `(,directory-files-no-dot-files-regexp "b")))
	    (should
Michael Albinus's avatar
Michael Albinus committed
778
	     (equal (file-name-completion "" tmp-name) "ba"))
779 780
	    (should
	     (equal
781
	      (sort (file-name-all-completions "" tmp-name) #'string-lessp)
Michael Albinus's avatar
Michael Albinus committed
782
	      '("bar/" "baz.tar")))))
783 784 785 786 787

      ;; Cleanup.
      (tramp-archive-cleanup-hash))))

;; The functions were introduced in Emacs 26.1.
788
(ert-deftest tramp-archive-test39-make-nearby-temp-file ()
789
  "Check `make-nearby-temp-file' and `temporary-file-directory'."
790
  (skip-unless tramp-archive-enabled)
791 792 793 794 795 796 797 798 799 800 801
  ;; Since Emacs 26.1.
  (skip-unless
   (and (fboundp 'make-nearby-temp-file) (fboundp 'temporary-file-directory)))

  ;; `make-nearby-temp-file' and `temporary-file-directory' exists
  ;; since Emacs 26.1.  We don't want to see compiler warnings for
  ;; older Emacsen.
  (let ((default-directory tramp-archive-test-archive)
	tmp-file)
    ;; The file archive shall know a temporary file directory.  It is
    ;; not in the archive itself.
802 803
    (should
     (stringp (with-no-warnings (with-no-warnings (temporary-file-directory)))))
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
    (should-not
     (tramp-archive-file-name-p (with-no-warnings (temporary-file-directory))))

    ;; A temporary file or directory shall not be located in the
    ;; archive itself.
    (setq tmp-file
	  (with-no-warnings (make-nearby-temp-file "tramp-archive-test")))
    (should (file-exists-p tmp-file))
    (should (file-regular-p tmp-file))
    (should-not (tramp-archive-file-name-p tmp-file))
    (delete-file tmp-file)
    (should-not (file-exists-p tmp-file))

    (setq tmp-file
	  (with-no-warnings (make-nearby-temp-file "tramp-archive-test" 'dir)))
    (should (file-exists-p tmp-file))
    (should (file-directory-p tmp-file))
    (should-not (tramp-archive-file-name-p tmp-file))
    (delete-directory tmp-file)
    (should-not (file-exists-p tmp-file))))

825
(ert-deftest tramp-archive-test42-file-system-info ()
826
  "Check that `file-system-info' returns proper values."
827
  (skip-unless tramp-archive-enabled)
828 829 830 831 832 833 834 835 836 837 838 839 840 841
  ;; Since Emacs 27.1.
  (skip-unless (fboundp 'file-system-info))

  ;; `file-system-info' exists since Emacs 27.  We don't want to see
  ;; compiler warnings for older Emacsen.
  (let ((fsi (with-no-warnings (file-system-info tramp-archive-test-archive))))
    (skip-unless fsi)
    (should (and (consp fsi)
		 (= (length fsi) 3)
		 (numberp (nth 0 fsi))
		 ;; FREE and AVAIL are always 0.
		 (zerop (nth 1 fsi))
		 (zerop (nth 2 fsi))))))

842
(ert-deftest tramp-archive-test45-auto-load ()
Michael Albinus's avatar
Michael Albinus committed
843
  "Check that `tramp-archive' autoloads properly."
844
  :tags '(:expensive-test)
845 846 847
  (skip-unless tramp-archive-enabled)
  ;; Autoloading tramp-archive works since Emacs 27.1.
  (skip-unless (tramp-archive--test-emacs27-p))
Michael Albinus's avatar
Michael Albinus committed
848

849
  ;; tramp-archive is neither loaded at Emacs startup, nor when
850
  ;; loading a file like "/mock::foo" (which loads Tramp).
Michael Albinus's avatar
Michael Albinus committed
851 852
  (let ((default-directory (expand-file-name temporary-file-directory))
	(code
853 854 855 856 857 858
	 "(progn \
	    (message \"tramp-archive loaded: %%s %%s\" \
              (featurep 'tramp) (featurep 'tramp-archive)) \
	    (file-attributes %S \"/\") \
	    (message \"tramp-archive loaded: %%s %%s\" \
              (featurep 'tramp) (featurep 'tramp-archive)))"))
859
    (dolist (file `("/mock::foo" ,(concat tramp-archive-test-archive "foo")))
860 861 862 863 864 865
      (should
       (string-match
	(format
	 "tramp-archive loaded: nil nil[[:ascii:]]+tramp-archive loaded: t %s"
	 (tramp-archive-file-name-p file))
	(shell-command-to-string
Michael Albinus's avatar
Michael Albinus committed
866
	 (format
867 868 869
	  "%s -batch -Q -L %s --eval %s"
	  (shell-quote-argument
	   (expand-file-name invocation-name invocation-directory))
870
	  (mapconcat #'shell-quote-argument load-path " -L ")
871
	  (shell-quote-argument (format code file)))))))))
Michael Albinus's avatar
Michael Albinus committed
872

873
(ert-deftest tramp-archive-test45-delay-load ()
Michael Albinus's avatar
Michael Albinus committed
874
  "Check that `tramp-archive' is loaded lazily, only when needed."
875
  :tags '(:expensive-test)
876 877 878
  (skip-unless tramp-archive-enabled)
  ;; Autoloading tramp-archive works since Emacs 27.1.
  (skip-unless (tramp-archive--test-emacs27-p))
Michael Albinus's avatar
Michael Albinus committed
879

880 881 882
  ;; tramp-archive is neither loaded at Emacs startup, nor when
  ;; loading a file like "/foo.tar".  It is loaded only when
  ;; `tramp-archive-enabled' is t.
Michael Albinus's avatar
Michael Albinus committed
883 884
  (let ((default-directory (expand-file-name temporary-file-directory))
	(code
885
	 "(progn \
886 887 888 889 890 891 892 893 894
            (setq tramp-archive-enabled %s) \
	    (message \"tramp-archive loaded: %%s\" \
              (featurep 'tramp-archive)) \
	    (file-attributes %S \"/\") \
	    (message \"tramp-archive loaded: %%s\" \
              (featurep 'tramp-archive)) \
	    (file-attributes %S \"/\") \
	    (message \"tramp-archive loaded: %%s\" \
              (featurep 'tramp-archive)))"))
895 896 897 898 899
    ;; tramp-archive doesn't load when `tramp-archive-enabled' is nil.
    (dolist (tae '(t nil))
      (should
       (string-match
	(format
900
	 "tramp-archive loaded: nil[[:ascii:]]+tramp-archive loaded: nil[[:ascii:]]+tramp-archive loaded: %s"
901 902
	 tae)
        (shell-command-to-string
Michael Albinus's avatar
Michael Albinus committed
903
         (format
904 905 906
	  "%s -batch -Q -L %s --eval %s"
	  (shell-quote-argument
	   (expand-file-name invocation-name invocation-directory))
907
	  (mapconcat #'shell-quote-argument load-path " -L ")
908 909 910 911
	  (shell-quote-argument
           (format
            code tae tramp-archive-test-file-archive
            (concat tramp-archive-test-archive "foo"))))))))))
Michael Albinus's avatar
Michael Albinus committed
912

Michael Albinus's avatar
Michael Albinus committed
913
(ert-deftest tramp-archive-test99-libarchive-tests ()
914
  "Run tests of libarchive test files."
915
  :tags '(:expensive-test :unstable)
916
  (skip-unless tramp-archive-enabled)
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
  ;; We do not want to run unless chosen explicitly.  This test makes
  ;; sense only in my local environment.  Michael Albinus.
  (skip-unless
   (equal
    (ert--stats-selector ert--current-run-stats)
    (ert-test-name (ert-running-test))))

  (url-handler-mode)
  (unwind-protect
      (dolist (dir
	       '("~/Downloads" "/sftp::~/Downloads" "/ssh::~/Downloads"
		 "http://ftp.debian.org/debian/pool/main/c/coreutils"))
	(dolist
	    (file
	     '("coreutils_8.26-3_amd64.deb"
	       "coreutils_8.26-3ubuntu3_amd64.deb"))
	  (setq file (expand-file-name file dir))
	  (when (file-exists-p file)
	    (setq file (expand-file-name "control.tar.gz/control" file))
	    (message "%s" file)
	    (should (file-attributes (file-name-as-directory file))))))

    ;; Cleanup.
    (tramp-archive-cleanup-hash))

  (unwind-protect
      (dolist (dir '("" "/sftp::" "/ssh::"))
	(dolist
	    (file
	     (apply
	      'append
	      (mapcar
	       (lambda (x) (directory-files (concat dir x) 'full "uu\\'" 'sort))
	       '("~/src/libarchive-3.2.2/libarchive/test"
		 "~/src/libarchive-3.2.2/cpio/test"
		 "~/src/libarchive-3.2.2/tar/test"))))
	  (setq file (file-name-as-directory file))
	  (cond
	   ((not (tramp-archive-file-name-p file))
	    (message "skipped: %s" file))
	   ((file-attributes file)
	    (message "%s" file))
	   (t (message "failed: %s" file)))
	  (tramp-archive-cleanup-hash)))

    ;; Cleanup.
    (tramp-archive-cleanup-hash)))

(defun tramp-archive-test-all (&optional interactive)
966 967
  "Run all tests for \\[tramp-archive].
If INTERACTIVE is non-nil, the tests are run interactively."
968 969
  (interactive "p")
  (funcall
970
   (if interactive #'ert-run-tests-interactively #'ert-run-tests-batch)
971 972 973 974
   "^tramp-archive"))

(provide 'tramp-archive-tests)
;;; tramp-archive-tests.el ends here