1 ;;; tramp-tests.el --- Tests of remote file access
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; This program is free software: you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License as
9 ;; published by the Free Software Foundation, either version 3 of the
10 ;; License, or (at your option) any later version.
12 ;; This program 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.
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/'.
22 ;; The tests require a recent ert.el from Emacs 24.4.
24 ;; Some of the tests require access to a remote host files. Since
25 ;; this could be problematic, a mock-up connection method "mock" is
26 ;; used. Emulating a remote connection, it simply calls "sh -i".
27 ;; Tramp's file name handlers still run, so this test is sufficient
28 ;; except for connection establishing.
30 ;; If you want to test a real Tramp connection, set
31 ;; $REMOTE_TEMPORARY_FILE_DIRECTORY to a suitable value in order to
32 ;; overwrite the default value. If you want to skip tests accessing a
33 ;; remote host, set this environment variable to "/dev/null" or
34 ;; whatever is appropriate on your system.
36 ;; A whole test run can be performed calling the command `tramp-test-all'.
47 (autoload 'dired-uncache "dired")
48 (declare-function tramp-find-executable "tramp-sh")
49 (declare-function tramp-get-remote-path "tramp-sh")
50 (declare-function tramp-get-remote-stat "tramp-sh")
51 (declare-function tramp-get-remote-perl "tramp-sh")
52 (defvar tramp-copy-size-limit)
53 (defvar tramp-persistency-file-name)
54 (defvar tramp-remote-process-environment)
56 ;; There is no default value on w32 systems, which could work out of the box.
57 (defconst tramp-test-temporary-file-directory
59 ((getenv "REMOTE_TEMPORARY_FILE_DIRECTORY"))
60 ((eq system-type 'windows-nt) null-device)
64 (tramp-login-program "sh")
65 (tramp-login-args (("-i")))
66 (tramp-remote-shell "/bin/sh")
67 (tramp-remote-shell-args ("-c"))
68 (tramp-connection-timeout 10)))
69 (format "/mock::%s" temporary-file-directory)))
70 "Temporary directory for Tramp tests.")
72 (setq password-cache-expiry nil
74 tramp-copy-size-limit nil
75 tramp-message-show-message nil
76 tramp-persistency-file-name nil)
78 ;; This shall happen on hydra only.
79 (when (getenv "NIX_STORE")
80 (add-to-list 'tramp-remote-path 'tramp-own-remote-path))
82 (defvar tramp--test-enabled-checked nil
83 "Cached result of `tramp--test-enabled'.
84 If the function did run, the value is a cons cell, the `cdr'
87 (defun tramp--test-enabled ()
88 "Whether remote file access is enabled."
89 (unless (consp tramp--test-enabled-checked)
91 tramp--test-enabled-checked
95 (file-remote-p tramp-test-temporary-file-directory)
96 (file-directory-p tramp-test-temporary-file-directory)
97 (file-writable-p tramp-test-temporary-file-directory))))))
99 (when (cdr tramp--test-enabled-checked)
100 ;; Cleanup connection.
102 (tramp-cleanup-connection
103 (tramp-dissect-file-name tramp-test-temporary-file-directory)
104 nil 'keep-password)))
107 (cdr tramp--test-enabled-checked))
109 (defun tramp--test-make-temp-name (&optional local)
110 "Create a temporary file name for test."
112 (make-temp-name "tramp-test")
113 (if local temporary-file-directory tramp-test-temporary-file-directory)))
115 (defmacro tramp--instrument-test-case (verbose &rest body)
116 "Run BODY with `tramp-verbose' equal VERBOSE.
117 Print the the content of the Tramp debug buffer, if BODY does not
118 eval properly in `should', `should-not' or `should-error'. BODY
119 shall not contain a timeout."
120 (declare (indent 1) (debug (natnump body)))
121 `(let ((tramp-verbose ,verbose)
122 (tramp-message-show-message t)
123 (tramp-debug-on-error t)
124 (debug-ignored-errors
125 (cons "^make-symbolic-link not supported$" debug-ignored-errors)))
128 (when (> tramp-verbose 3)
129 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
130 (with-current-buffer (tramp-get-connection-buffer v)
131 (message "%s" (buffer-string)))
132 (with-current-buffer (tramp-get-debug-buffer v)
133 (message "%s" (buffer-string))))))))
135 (ert-deftest tramp-test00-availability ()
136 "Test availability of Tramp functions."
137 :expected-result (if (tramp--test-enabled) :passed :failed)
138 (message "Remote directory: `%s'" tramp-test-temporary-file-directory)
139 (should (ignore-errors
141 (file-remote-p tramp-test-temporary-file-directory)
142 (file-directory-p tramp-test-temporary-file-directory)
143 (file-writable-p tramp-test-temporary-file-directory)))))
145 (ert-deftest tramp-test01-file-name-syntax ()
146 "Check remote file name syntax."
148 (should (tramp-tramp-file-p "/method::"))
149 (should (tramp-tramp-file-p "/host:"))
150 (should (tramp-tramp-file-p "/user@:"))
151 (should (tramp-tramp-file-p "/user@host:"))
152 (should (tramp-tramp-file-p "/method:host:"))
153 (should (tramp-tramp-file-p "/method:user@:"))
154 (should (tramp-tramp-file-p "/method:user@host:"))
155 (should (tramp-tramp-file-p "/method:user@email@host:"))
158 (should (tramp-tramp-file-p "/host#1234:"))
159 (should (tramp-tramp-file-p "/user@host#1234:"))
160 (should (tramp-tramp-file-p "/method:host#1234:"))
161 (should (tramp-tramp-file-p "/method:user@host#1234:"))
163 ;; Using an IPv4 address.
164 (should (tramp-tramp-file-p "/1.2.3.4:"))
165 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
166 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
167 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
169 ;; Using an IPv6 address.
170 (should (tramp-tramp-file-p "/[]:"))
171 (should (tramp-tramp-file-p "/[::1]:"))
172 (should (tramp-tramp-file-p "/user@[::1]:"))
173 (should (tramp-tramp-file-p "/method:[::1]:"))
174 (should (tramp-tramp-file-p "/method:user@[::1]:"))
176 ;; Local file name part.
177 (should (tramp-tramp-file-p "/host:/:"))
178 (should (tramp-tramp-file-p "/method:::"))
179 (should (tramp-tramp-file-p "/method::/path/to/file"))
180 (should (tramp-tramp-file-p "/method::file"))
183 (should (tramp-tramp-file-p "/method1:|method2::"))
184 (should (tramp-tramp-file-p "/method1:host1|host2:"))
185 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
186 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
187 (should (tramp-tramp-file-p
188 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
191 (should-not (tramp-tramp-file-p nil))
192 (should-not (tramp-tramp-file-p 'symbol))
193 ;; "/:" suppresses file name handlers.
194 (should-not (tramp-tramp-file-p "/::"))
195 (should-not (tramp-tramp-file-p "/:@:"))
196 (should-not (tramp-tramp-file-p "/:[]:"))
197 ;; Multihops require a method.
198 (should-not (tramp-tramp-file-p "/host1|host2:"))
199 ;; Methods or hostnames shall be at least two characters on MS Windows.
200 (when (memq system-type '(cygwin windows-nt))
201 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
202 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
204 (ert-deftest tramp-test02-file-name-dissect ()
205 "Check remote file name components."
206 (let ((tramp-default-method "default-method")
207 (tramp-default-user "default-user")
208 (tramp-default-host "default-host"))
209 ;; Expand `tramp-default-user' and `tramp-default-host'.
210 (should (string-equal
211 (file-remote-p "/method::")
212 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
213 (should (string-equal (file-remote-p "/method::" 'method) "method"))
214 (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
215 (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
216 (should (string-equal (file-remote-p "/method::" 'localname) ""))
217 (should (string-equal (file-remote-p "/method::" 'hop) nil))
219 ;; Expand `tramp-default-method' and `tramp-default-user'.
220 (should (string-equal
221 (file-remote-p "/host:")
222 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
223 (should (string-equal (file-remote-p "/host:" 'method) "default-method"))
224 (should (string-equal (file-remote-p "/host:" 'user) "default-user"))
225 (should (string-equal (file-remote-p "/host:" 'host) "host"))
226 (should (string-equal (file-remote-p "/host:" 'localname) ""))
227 (should (string-equal (file-remote-p "/host:" 'hop) nil))
229 ;; Expand `tramp-default-method' and `tramp-default-host'.
230 (should (string-equal
231 (file-remote-p "/user@:")
232 (format "/%s:%s@%s:" "default-method""user" "default-host")))
233 (should (string-equal (file-remote-p "/user@:" 'method) "default-method"))
234 (should (string-equal (file-remote-p "/user@:" 'user) "user"))
235 (should (string-equal (file-remote-p "/user@:" 'host) "default-host"))
236 (should (string-equal (file-remote-p "/user@:" 'localname) ""))
237 (should (string-equal (file-remote-p "/user@:" 'hop) nil))
239 ;; Expand `tramp-default-method'.
240 (should (string-equal
241 (file-remote-p "/user@host:")
242 (format "/%s:%s@%s:" "default-method" "user" "host")))
243 (should (string-equal
244 (file-remote-p "/user@host:" 'method) "default-method"))
245 (should (string-equal (file-remote-p "/user@host:" 'user) "user"))
246 (should (string-equal (file-remote-p "/user@host:" 'host) "host"))
247 (should (string-equal (file-remote-p "/user@host:" 'localname) ""))
248 (should (string-equal (file-remote-p "/user@host:" 'hop) nil))
250 ;; Expand `tramp-default-user'.
251 (should (string-equal
252 (file-remote-p "/method:host:")
253 (format "/%s:%s@%s:" "method" "default-user" "host")))
254 (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
255 (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
256 (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
257 (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
258 (should (string-equal (file-remote-p "/method:host:" 'hop) nil))
260 ;; Expand `tramp-default-host'.
261 (should (string-equal
262 (file-remote-p "/method:user@:")
263 (format "/%s:%s@%s:" "method" "user" "default-host")))
264 (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
265 (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
266 (should (string-equal (file-remote-p "/method:user@:" 'host)
268 (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
269 (should (string-equal (file-remote-p "/method:user@:" 'hop) nil))
272 (should (string-equal
273 (file-remote-p "/method:user@host:")
274 (format "/%s:%s@%s:" "method" "user" "host")))
275 (should (string-equal
276 (file-remote-p "/method:user@host:" 'method) "method"))
277 (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
278 (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
279 (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
280 (should (string-equal (file-remote-p "/method:user@host:" 'hop) nil))
283 (should (string-equal
284 (file-remote-p "/method:user@email@host:")
285 (format "/%s:%s@%s:" "method" "user@email" "host")))
286 (should (string-equal
287 (file-remote-p "/method:user@email@host:" 'method) "method"))
288 (should (string-equal
289 (file-remote-p "/method:user@email@host:" 'user) "user@email"))
290 (should (string-equal
291 (file-remote-p "/method:user@email@host:" 'host) "host"))
292 (should (string-equal
293 (file-remote-p "/method:user@email@host:" 'localname) ""))
294 (should (string-equal
295 (file-remote-p "/method:user@email@host:" 'hop) nil))
297 ;; Expand `tramp-default-method' and `tramp-default-user'.
298 (should (string-equal
299 (file-remote-p "/host#1234:")
300 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
301 (should (string-equal
302 (file-remote-p "/host#1234:" 'method) "default-method"))
303 (should (string-equal (file-remote-p "/host#1234:" 'user) "default-user"))
304 (should (string-equal (file-remote-p "/host#1234:" 'host) "host#1234"))
305 (should (string-equal (file-remote-p "/host#1234:" 'localname) ""))
306 (should (string-equal (file-remote-p "/host#1234:" 'hop) nil))
308 ;; Expand `tramp-default-method'.
309 (should (string-equal
310 (file-remote-p "/user@host#1234:")
311 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
312 (should (string-equal
313 (file-remote-p "/user@host#1234:" 'method) "default-method"))
314 (should (string-equal (file-remote-p "/user@host#1234:" 'user) "user"))
315 (should (string-equal (file-remote-p "/user@host#1234:" 'host) "host#1234"))
316 (should (string-equal (file-remote-p "/user@host#1234:" 'localname) ""))
317 (should (string-equal (file-remote-p "/user@host#1234:" 'hop) nil))
319 ;; Expand `tramp-default-user'.
320 (should (string-equal
321 (file-remote-p "/method:host#1234:")
322 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
323 (should (string-equal
324 (file-remote-p "/method:host#1234:" 'method) "method"))
325 (should (string-equal
326 (file-remote-p "/method:host#1234:" 'user) "default-user"))
327 (should (string-equal
328 (file-remote-p "/method:host#1234:" 'host) "host#1234"))
329 (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
330 (should (string-equal (file-remote-p "/method:host#1234:" 'hop) nil))
333 (should (string-equal
334 (file-remote-p "/method:user@host#1234:")
335 (format "/%s:%s@%s:" "method" "user" "host#1234")))
336 (should (string-equal
337 (file-remote-p "/method:user@host#1234:" 'method) "method"))
338 (should (string-equal
339 (file-remote-p "/method:user@host#1234:" 'user) "user"))
340 (should (string-equal
341 (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
342 (should (string-equal
343 (file-remote-p "/method:user@host#1234:" 'localname) ""))
344 (should (string-equal
345 (file-remote-p "/method:user@host#1234:" 'hop) nil))
347 ;; Expand `tramp-default-method' and `tramp-default-user'.
348 (should (string-equal
349 (file-remote-p "/1.2.3.4:")
350 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
351 (should (string-equal (file-remote-p "/1.2.3.4:" 'method) "default-method"))
352 (should (string-equal (file-remote-p "/1.2.3.4:" 'user) "default-user"))
353 (should (string-equal (file-remote-p "/1.2.3.4:" 'host) "1.2.3.4"))
354 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname) ""))
355 (should (string-equal (file-remote-p "/1.2.3.4:" 'hop) nil))
357 ;; Expand `tramp-default-method'.
358 (should (string-equal
359 (file-remote-p "/user@1.2.3.4:")
360 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
361 (should (string-equal
362 (file-remote-p "/user@1.2.3.4:" 'method) "default-method"))
363 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user) "user"))
364 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host) "1.2.3.4"))
365 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname) ""))
366 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'hop) nil))
368 ;; Expand `tramp-default-user'.
369 (should (string-equal
370 (file-remote-p "/method:1.2.3.4:")
371 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
372 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
373 (should (string-equal
374 (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
375 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
376 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
377 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'hop) nil))
380 (should (string-equal
381 (file-remote-p "/method:user@1.2.3.4:")
382 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
383 (should (string-equal
384 (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
385 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
386 (should (string-equal
387 (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
388 (should (string-equal
389 (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
390 (should (string-equal
391 (file-remote-p "/method:user@1.2.3.4:" 'hop) nil))
393 ;; Expand `tramp-default-method', `tramp-default-user' and
394 ;; `tramp-default-host'.
395 (should (string-equal
396 (file-remote-p "/[]:")
398 "/%s:%s@%s:" "default-method" "default-user" "default-host")))
399 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
400 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
401 (should (string-equal (file-remote-p "/[]:" 'host) "default-host"))
402 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
403 (should (string-equal (file-remote-p "/[]:" 'hop) nil))
405 ;; Expand `tramp-default-method' and `tramp-default-user'.
406 (let ((tramp-default-host "::1"))
407 (should (string-equal
408 (file-remote-p "/[]:")
409 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
410 (should (string-equal (file-remote-p "/[]:" 'method) "default-method"))
411 (should (string-equal (file-remote-p "/[]:" 'user) "default-user"))
412 (should (string-equal (file-remote-p "/[]:" 'host) "::1"))
413 (should (string-equal (file-remote-p "/[]:" 'localname) ""))
414 (should (string-equal (file-remote-p "/[]:" 'hop) nil)))
416 ;; Expand `tramp-default-method' and `tramp-default-user'.
417 (should (string-equal
418 (file-remote-p "/[::1]:")
419 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
420 (should (string-equal (file-remote-p "/[::1]:" 'method) "default-method"))
421 (should (string-equal (file-remote-p "/[::1]:" 'user) "default-user"))
422 (should (string-equal (file-remote-p "/[::1]:" 'host) "::1"))
423 (should (string-equal (file-remote-p "/[::1]:" 'localname) ""))
424 (should (string-equal (file-remote-p "/[::1]:" 'hop) nil))
426 ;; Expand `tramp-default-method'.
427 (should (string-equal
428 (file-remote-p "/user@[::1]:")
429 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
430 (should (string-equal
431 (file-remote-p "/user@[::1]:" 'method) "default-method"))
432 (should (string-equal (file-remote-p "/user@[::1]:" 'user) "user"))
433 (should (string-equal (file-remote-p "/user@[::1]:" 'host) "::1"))
434 (should (string-equal (file-remote-p "/user@[::1]:" 'localname) ""))
435 (should (string-equal (file-remote-p "/user@[::1]:" 'hop) nil))
437 ;; Expand `tramp-default-user'.
438 (should (string-equal
439 (file-remote-p "/method:[::1]:")
440 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
441 (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
442 (should (string-equal
443 (file-remote-p "/method:[::1]:" 'user) "default-user"))
444 (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
445 (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
446 (should (string-equal (file-remote-p "/method:[::1]:" 'hop) nil))
449 (should (string-equal
450 (file-remote-p "/method:user@[::1]:")
451 (format "/%s:%s@%s:" "method" "user" "[::1]")))
452 (should (string-equal
453 (file-remote-p "/method:user@[::1]:" 'method) "method"))
454 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
455 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
456 (should (string-equal
457 (file-remote-p "/method:user@[::1]:" 'localname) ""))
458 (should (string-equal (file-remote-p "/method:user@[::1]:" 'hop) nil))
460 ;; Local file name part.
461 (should (string-equal (file-remote-p "/host:/:" 'localname) "/:"))
462 (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
463 (should (string-equal (file-remote-p "/method:: " 'localname) " "))
464 (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
465 (should (string-equal
466 (file-remote-p "/method::/path/to/file" 'localname)
472 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
473 (format "/%s:%s@%s|%s:%s@%s:"
474 "method1" "user1" "host1" "method2" "user2" "host2")))
478 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
483 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
488 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
493 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
498 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'hop)
500 "method1" "user1" "host1")))
505 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
506 (format "/%s:%s@%s|%s:%s@%s|%s:%s@%s:"
507 "method1" "user1" "host1"
508 "method2" "user2" "host2"
509 "method3" "user3" "host3")))
513 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
519 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
525 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
531 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
537 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
539 (format "%s:%s@%s|%s:%s@%s|"
540 "method1" "user1" "host1" "method2" "user2" "host2")))))
542 (ert-deftest tramp-test03-file-name-defaults ()
543 "Check default values for some methods."
544 ;; Default values in tramp-adb.el.
545 (should (string-equal (file-remote-p "/adb::" 'host) ""))
546 ;; Default values in tramp-ftp.el.
547 (should (string-equal (file-remote-p "/ftp.host:" 'method) "ftp"))
548 (dolist (u '("ftp" "anonymous"))
549 (should (string-equal (file-remote-p (format "/%s@:" u) 'method) "ftp")))
550 ;; Default values in tramp-gvfs.el.
551 (when (and (load "tramp-gvfs" 'noerror 'nomessage)
552 (symbol-value 'tramp-gvfs-enabled))
553 (should (string-equal (file-remote-p "/synce::" 'user) nil)))
554 ;; Default values in tramp-gw.el.
555 (dolist (m '("tunnel" "socks"))
557 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
558 ;; Default values in tramp-sh.el.
559 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
560 (should (string-equal (file-remote-p (format "/root@%s:" h) 'method) "su")))
561 (dolist (m '("su" "sudo" "ksu"))
562 (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
563 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
565 (string-equal (file-remote-p (format "/%s::" m) 'user) (user-login-name))))
566 ;; Default values in tramp-smb.el.
567 (should (string-equal (file-remote-p "/user%domain@host:" 'method) "smb"))
568 (should (string-equal (file-remote-p "/smb::" 'user) nil)))
570 (ert-deftest tramp-test04-substitute-in-file-name ()
571 "Check `substitute-in-file-name'."
572 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
575 (substitute-in-file-name "/method:host:/path//foo") "/method:host:/foo"))
577 (string-equal (substitute-in-file-name "/method:host:/path///foo") "/foo"))
580 (substitute-in-file-name "/method:host:/path/~/foo") "/method:host:~/foo"))
582 (string-equal (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
583 (let (process-environment)
586 (substitute-in-file-name "/method:host:/path/$FOO")
587 "/method:host:/path/$FOO"))
591 (substitute-in-file-name "/method:host:/path/$FOO")
592 "/method:host:/path/bla"))
595 (substitute-in-file-name "/method:host:/path/$$FOO")
596 "/method:host:/path/$FOO"))))
598 (ert-deftest tramp-test05-expand-file-name ()
599 "Check `expand-file-name'."
602 (expand-file-name "/method:host:/path/./file") "/method:host:/path/file"))
605 (expand-file-name "/method:host:/path/../file") "/method:host:/file")))
607 (ert-deftest tramp-test06-directory-file-name ()
608 "Check `directory-file-name'.
609 This checks also `file-name-as-directory', `file-name-directory',
610 `file-name-nondirectory' and `unhandled-file-name-directory'."
613 (directory-file-name "/method:host:/path/to/file")
614 "/method:host:/path/to/file"))
617 (directory-file-name "/method:host:/path/to/file/")
618 "/method:host:/path/to/file"))
621 (file-name-as-directory "/method:host:/path/to/file")
622 "/method:host:/path/to/file/"))
625 (file-name-as-directory "/method:host:/path/to/file/")
626 "/method:host:/path/to/file/"))
629 (file-name-directory "/method:host:/path/to/file")
630 "/method:host:/path/to/"))
633 (file-name-directory "/method:host:/path/to/file/")
634 "/method:host:/path/to/file/"))
636 (string-equal (file-name-nondirectory "/method:host:/path/to/file") "file"))
638 (string-equal (file-name-nondirectory "/method:host:/path/to/file/") ""))
640 (unhandled-file-name-directory "/method:host:/path/to/file"))
643 (dolist (n-e '(nil t))
644 ;; We must clear `tramp-default-method'. On hydra, it is "ftp",
645 ;; which ruins the tests.
646 (let ((non-essential n-e)
647 tramp-default-method)
649 `(,(file-remote-p tramp-test-temporary-file-directory 'method)
650 ,(file-remote-p tramp-test-temporary-file-directory 'host)))
651 (unless (zerop (length file))
652 (setq file (format "/%s:" file))
653 (should (string-equal (directory-file-name file) file))
656 (file-name-as-directory file)
657 (if (tramp-completion-mode-p) file (concat file "./"))))
658 (should (string-equal (file-name-directory file) file))
659 (should (string-equal (file-name-nondirectory file) "")))))))
661 (ert-deftest tramp-test07-file-exists-p ()
662 "Check `file-exist-p', `write-region' and `delete-file'."
663 (skip-unless (tramp--test-enabled))
665 (let ((tmp-name (tramp--test-make-temp-name)))
666 (should-not (file-exists-p tmp-name))
667 (write-region "foo" nil tmp-name)
668 (should (file-exists-p tmp-name))
669 (delete-file tmp-name)
670 (should-not (file-exists-p tmp-name))))
672 (ert-deftest tramp-test08-file-local-copy ()
673 "Check `file-local-copy'."
674 (skip-unless (tramp--test-enabled))
676 (let ((tmp-name1 (tramp--test-make-temp-name))
680 (write-region "foo" nil tmp-name1)
681 (should (setq tmp-name2 (file-local-copy tmp-name1)))
683 (insert-file-contents tmp-name2)
684 (should (string-equal (buffer-string) "foo")))
685 ;; Check also that a file transfer with compression works.
686 (let ((default-directory tramp-test-temporary-file-directory)
687 (tramp-copy-size-limit 4)
688 (tramp-inline-compress-start-size 2))
689 (delete-file tmp-name2)
690 (should (setq tmp-name2 (file-local-copy tmp-name1)))))
694 (delete-file tmp-name1)
695 (delete-file tmp-name2)))))
697 (ert-deftest tramp-test09-insert-file-contents ()
698 "Check `insert-file-contents'."
699 (skip-unless (tramp--test-enabled))
701 (let ((tmp-name (tramp--test-make-temp-name)))
704 (write-region "foo" nil tmp-name)
706 (insert-file-contents tmp-name)
707 (should (string-equal (buffer-string) "foo"))
708 (insert-file-contents tmp-name)
709 (should (string-equal (buffer-string) "foofoo"))
711 (insert-file-contents tmp-name nil 1 3)
712 (should (string-equal (buffer-string) "oofoofoo"))
714 (insert-file-contents tmp-name nil nil nil 'replace)
715 (should (string-equal (buffer-string) "foo"))))
718 (ignore-errors (delete-file tmp-name)))))
720 (ert-deftest tramp-test10-write-region ()
721 "Check `write-region'."
722 (skip-unless (tramp--test-enabled))
724 (let ((tmp-name (tramp--test-make-temp-name)))
729 (write-region nil nil tmp-name))
731 (insert-file-contents tmp-name)
732 (should (string-equal (buffer-string) "foo")))
736 (write-region nil nil tmp-name 'append))
738 (insert-file-contents tmp-name)
739 (should (string-equal (buffer-string) "foobla")))
741 (write-region "foo" nil tmp-name)
743 (insert-file-contents tmp-name)
744 (should (string-equal (buffer-string) "foo")))
748 (write-region 3 5 tmp-name))
750 (insert-file-contents tmp-name)
751 (should (string-equal (buffer-string) "34"))))
754 (ignore-errors (delete-file tmp-name)))))
756 (ert-deftest tramp-test11-copy-file ()
758 (skip-unless (tramp--test-enabled))
760 (let ((tmp-name1 (tramp--test-make-temp-name))
761 (tmp-name2 (tramp--test-make-temp-name))
762 (tmp-name3 (tramp--test-make-temp-name))
763 (tmp-name4 (tramp--test-make-temp-name 'local))
764 (tmp-name5 (tramp--test-make-temp-name 'local)))
766 ;; Copy on remote side.
769 (write-region "foo" nil tmp-name1)
770 (copy-file tmp-name1 tmp-name2)
771 (should (file-exists-p tmp-name2))
773 (insert-file-contents tmp-name2)
774 (should (string-equal (buffer-string) "foo")))
775 (should-error (copy-file tmp-name1 tmp-name2))
776 (copy-file tmp-name1 tmp-name2 'ok)
777 (make-directory tmp-name3)
778 (copy-file tmp-name1 tmp-name3)
781 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
784 (ignore-errors (delete-file tmp-name1))
785 (ignore-errors (delete-file tmp-name2))
786 (ignore-errors (delete-directory tmp-name3 'recursive)))
788 ;; Copy from remote side to local side.
791 (write-region "foo" nil tmp-name1)
792 (copy-file tmp-name1 tmp-name4)
793 (should (file-exists-p tmp-name4))
795 (insert-file-contents tmp-name4)
796 (should (string-equal (buffer-string) "foo")))
797 (should-error (copy-file tmp-name1 tmp-name4))
798 (copy-file tmp-name1 tmp-name4 'ok)
799 (make-directory tmp-name5)
800 (copy-file tmp-name1 tmp-name5)
803 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
806 (ignore-errors (delete-file tmp-name1))
807 (ignore-errors (delete-file tmp-name4))
808 (ignore-errors (delete-directory tmp-name5 'recursive)))
810 ;; Copy from local side to remote side.
813 (write-region "foo" nil tmp-name4 nil 'nomessage)
814 (copy-file tmp-name4 tmp-name1)
815 (should (file-exists-p tmp-name1))
817 (insert-file-contents tmp-name1)
818 (should (string-equal (buffer-string) "foo")))
819 (should-error (copy-file tmp-name4 tmp-name1))
820 (copy-file tmp-name4 tmp-name1 'ok)
821 (make-directory tmp-name3)
822 (copy-file tmp-name4 tmp-name3)
825 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
828 (ignore-errors (delete-file tmp-name1))
829 (ignore-errors (delete-file tmp-name4))
830 (ignore-errors (delete-directory tmp-name3 'recursive)))))
832 (ert-deftest tramp-test12-rename-file ()
833 "Check `rename-file'."
834 (skip-unless (tramp--test-enabled))
836 (let ((tmp-name1 (tramp--test-make-temp-name))
837 (tmp-name2 (tramp--test-make-temp-name))
838 (tmp-name3 (tramp--test-make-temp-name))
839 (tmp-name4 (tramp--test-make-temp-name 'local))
840 (tmp-name5 (tramp--test-make-temp-name 'local)))
842 ;; Rename on remote side.
845 (write-region "foo" nil tmp-name1)
846 (rename-file tmp-name1 tmp-name2)
847 (should-not (file-exists-p tmp-name1))
848 (should (file-exists-p tmp-name2))
850 (insert-file-contents tmp-name2)
851 (should (string-equal (buffer-string) "foo")))
852 (write-region "foo" nil tmp-name1)
853 (should-error (rename-file tmp-name1 tmp-name2))
854 (rename-file tmp-name1 tmp-name2 'ok)
855 (should-not (file-exists-p tmp-name1))
856 (write-region "foo" nil tmp-name1)
857 (make-directory tmp-name3)
858 (rename-file tmp-name1 tmp-name3)
859 (should-not (file-exists-p tmp-name1))
862 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name3))))
865 (ignore-errors (delete-file tmp-name1))
866 (ignore-errors (delete-file tmp-name2))
867 (ignore-errors (delete-directory tmp-name3 'recursive)))
869 ;; Rename from remote side to local side.
872 (write-region "foo" nil tmp-name1)
873 (rename-file tmp-name1 tmp-name4)
874 (should-not (file-exists-p tmp-name1))
875 (should (file-exists-p tmp-name4))
877 (insert-file-contents tmp-name4)
878 (should (string-equal (buffer-string) "foo")))
879 (write-region "foo" nil tmp-name1)
880 (should-error (rename-file tmp-name1 tmp-name4))
881 (rename-file tmp-name1 tmp-name4 'ok)
882 (should-not (file-exists-p tmp-name1))
883 (write-region "foo" nil tmp-name1)
884 (make-directory tmp-name5)
885 (rename-file tmp-name1 tmp-name5)
886 (should-not (file-exists-p tmp-name1))
889 (expand-file-name (file-name-nondirectory tmp-name1) tmp-name5))))
892 (ignore-errors (delete-file tmp-name1))
893 (ignore-errors (delete-file tmp-name4))
894 (ignore-errors (delete-directory tmp-name5 'recursive)))
896 ;; Rename from local side to remote side.
899 (write-region "foo" nil tmp-name4 nil 'nomessage)
900 (rename-file tmp-name4 tmp-name1)
901 (should-not (file-exists-p tmp-name4))
902 (should (file-exists-p tmp-name1))
904 (insert-file-contents tmp-name1)
905 (should (string-equal (buffer-string) "foo")))
906 (write-region "foo" nil tmp-name4 nil 'nomessage)
907 (should-error (rename-file tmp-name4 tmp-name1))
908 (rename-file tmp-name4 tmp-name1 'ok)
909 (should-not (file-exists-p tmp-name4))
910 (write-region "foo" nil tmp-name4 nil 'nomessage)
911 (make-directory tmp-name3)
912 (rename-file tmp-name4 tmp-name3)
913 (should-not (file-exists-p tmp-name4))
916 (expand-file-name (file-name-nondirectory tmp-name4) tmp-name3))))
919 (ignore-errors (delete-file tmp-name1))
920 (ignore-errors (delete-file tmp-name4))
921 (ignore-errors (delete-directory tmp-name3 'recursive)))))
923 (ert-deftest tramp-test13-make-directory ()
924 "Check `make-directory'.
925 This tests also `file-directory-p' and `file-accessible-directory-p'."
926 (skip-unless (tramp--test-enabled))
928 (let* ((tmp-name1 (tramp--test-make-temp-name))
929 (tmp-name2 (expand-file-name "foo/bar" tmp-name1)))
932 (make-directory tmp-name1)
933 (should (file-directory-p tmp-name1))
934 (should (file-accessible-directory-p tmp-name1))
935 (should-error (make-directory tmp-name2) :type 'file-error)
936 (make-directory tmp-name2 'parents)
937 (should (file-directory-p tmp-name2))
938 (should (file-accessible-directory-p tmp-name2)))
941 (ignore-errors (delete-directory tmp-name1 'recursive)))))
943 (ert-deftest tramp-test14-delete-directory ()
944 "Check `delete-directory'."
945 (skip-unless (tramp--test-enabled))
947 (let ((tmp-name (tramp--test-make-temp-name)))
948 ;; Delete empty directory.
949 (make-directory tmp-name)
950 (should (file-directory-p tmp-name))
951 (delete-directory tmp-name)
952 (should-not (file-directory-p tmp-name))
953 ;; Delete non-empty directory.
954 (make-directory tmp-name)
955 (write-region "foo" nil (expand-file-name "bla" tmp-name))
956 (should-error (delete-directory tmp-name) :type 'file-error)
957 (delete-directory tmp-name 'recursive)
958 (should-not (file-directory-p tmp-name))))
960 (ert-deftest tramp-test15-copy-directory ()
961 "Check `copy-directory'."
962 (skip-unless (tramp--test-enabled))
964 (let* ((tmp-name1 (tramp--test-make-temp-name))
965 (tmp-name2 (tramp--test-make-temp-name))
966 (tmp-name3 (expand-file-name
967 (file-name-nondirectory tmp-name1) tmp-name2))
968 (tmp-name4 (expand-file-name "foo" tmp-name1))
969 (tmp-name5 (expand-file-name "foo" tmp-name2))
970 (tmp-name6 (expand-file-name "foo" tmp-name3)))
972 ;; Copy complete directory.
975 ;; Copy empty directory.
976 (make-directory tmp-name1)
977 (write-region "foo" nil tmp-name4)
978 (should (file-directory-p tmp-name1))
979 (should (file-exists-p tmp-name4))
980 (copy-directory tmp-name1 tmp-name2)
981 (should (file-directory-p tmp-name2))
982 (should (file-exists-p tmp-name5))
983 ;; Target directory does exist already.
984 (copy-directory tmp-name1 tmp-name2)
985 (should (file-directory-p tmp-name3))
986 (should (file-exists-p tmp-name6)))
990 (delete-directory tmp-name1 'recursive)
991 (delete-directory tmp-name2 'recursive)))
993 ;; Copy directory contents.
996 ;; Copy empty directory.
997 (make-directory tmp-name1)
998 (write-region "foo" nil tmp-name4)
999 (should (file-directory-p tmp-name1))
1000 (should (file-exists-p tmp-name4))
1001 (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
1002 (should (file-directory-p tmp-name2))
1003 (should (file-exists-p tmp-name5))
1004 ;; Target directory does exist already.
1005 (delete-file tmp-name5)
1006 (should-not (file-exists-p tmp-name5))
1007 (copy-directory tmp-name1 tmp-name2 nil 'parents 'contents)
1008 (should (file-directory-p tmp-name2))
1009 (should (file-exists-p tmp-name5))
1010 (should-not (file-directory-p tmp-name3))
1011 (should-not (file-exists-p tmp-name6)))
1015 (delete-directory tmp-name1 'recursive)
1016 (delete-directory tmp-name2 'recursive)))))
1018 (ert-deftest tramp-test16-directory-files ()
1019 "Check `directory-files'."
1020 (skip-unless (tramp--test-enabled))
1022 (let* ((tmp-name1 (tramp--test-make-temp-name))
1023 (tmp-name2 (expand-file-name "bla" tmp-name1))
1024 (tmp-name3 (expand-file-name "foo" tmp-name1)))
1027 (make-directory tmp-name1)
1028 (write-region "foo" nil tmp-name2)
1029 (write-region "bla" nil tmp-name3)
1030 (should (file-directory-p tmp-name1))
1031 (should (file-exists-p tmp-name2))
1032 (should (file-exists-p tmp-name3))
1033 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
1034 (should (equal (directory-files tmp-name1 'full)
1035 `(,(concat tmp-name1 "/.")
1036 ,(concat tmp-name1 "/..")
1037 ,tmp-name2 ,tmp-name3)))
1038 (should (equal (directory-files
1039 tmp-name1 nil directory-files-no-dot-files-regexp)
1041 (should (equal (directory-files
1042 tmp-name1 'full directory-files-no-dot-files-regexp)
1043 `(,tmp-name2 ,tmp-name3))))
1046 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1048 (ert-deftest tramp-test17-insert-directory ()
1049 "Check `insert-directory'."
1050 (skip-unless (tramp--test-enabled))
1052 (let* ((tmp-name1 (tramp--test-make-temp-name))
1053 (tmp-name2 (expand-file-name "foo" tmp-name1))
1054 ;; We test for the summary line. Keyword "total" could be localized.
1055 (process-environment
1056 (append '("LANG=C" "LANGUAGE=C" "LC_ALL=C") process-environment)))
1059 (make-directory tmp-name1)
1060 (write-region "foo" nil tmp-name2)
1061 (should (file-directory-p tmp-name1))
1062 (should (file-exists-p tmp-name2))
1064 (insert-directory tmp-name1 nil)
1065 (goto-char (point-min))
1066 (should (looking-at-p (regexp-quote tmp-name1))))
1068 (insert-directory tmp-name1 "-al")
1069 (goto-char (point-min))
1070 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
1072 (insert-directory (file-name-as-directory tmp-name1) "-al")
1073 (goto-char (point-min))
1075 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
1078 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
1079 (goto-char (point-min))
1083 ;; There might be a summary line.
1084 "\\(total.+[[:digit:]]+\n\\)?"
1085 ;; We don't know in which order ".", ".." and "foo" appear.
1086 "\\(.+ \\(\\.?\\.\\|foo\\)\n\\)\\{3\\}")))))
1089 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1091 (ert-deftest tramp-test18-file-attributes ()
1092 "Check `file-attributes'.
1093 This tests also `file-readable-p' and `file-regular-p'."
1094 (skip-unless (tramp--test-enabled))
1096 ;; We must use `file-truename' for the temporary directory, because
1097 ;; it could be located on a symlinked directory. This would let the
1099 (let* ((tramp-test-temporary-file-directory
1100 (file-truename tramp-test-temporary-file-directory))
1101 (tmp-name1 (tramp--test-make-temp-name))
1102 (tmp-name2 (tramp--test-make-temp-name))
1103 ;; File name with "//".
1107 (file-remote-p tmp-name1)
1108 (replace-regexp-in-string
1109 "/" "//" (file-remote-p tmp-name1 'localname))))
1113 (write-region "foo" nil tmp-name1)
1114 (should (file-exists-p tmp-name1))
1115 (setq attr (file-attributes tmp-name1))
1116 (should (consp attr))
1117 (should (file-exists-p tmp-name1))
1118 (should (file-readable-p tmp-name1))
1119 (should (file-regular-p tmp-name1))
1120 ;; We do not test inodes and device numbers.
1121 (should (null (car attr)))
1122 (should (numberp (nth 1 attr))) ;; Link.
1123 (should (numberp (nth 2 attr))) ;; Uid.
1124 (should (numberp (nth 3 attr))) ;; Gid.
1125 ;; Last access time.
1126 (should (stringp (current-time-string (nth 4 attr))))
1127 ;; Last modification time.
1128 (should (stringp (current-time-string (nth 5 attr))))
1129 ;; Last status change time.
1130 (should (stringp (current-time-string (nth 6 attr))))
1131 (should (numberp (nth 7 attr))) ;; Size.
1132 (should (stringp (nth 8 attr))) ;; Modes.
1134 (setq attr (file-attributes tmp-name1 'string))
1135 (should (stringp (nth 2 attr))) ;; Uid.
1136 (should (stringp (nth 3 attr))) ;; Gid.
1140 (make-symbolic-link tmp-name1 tmp-name2)
1141 (should (file-exists-p tmp-name2))
1142 (should (file-symlink-p tmp-name2))
1143 (setq attr (file-attributes tmp-name2))
1144 (should (string-equal
1146 (file-remote-p (file-truename tmp-name1) 'localname)))
1147 (delete-file tmp-name2))
1149 (should (string-equal (error-message-string err)
1150 "make-symbolic-link not supported"))))
1152 ;; Check, that "//" in symlinks are handled properly.
1154 (let ((default-directory tramp-test-temporary-file-directory))
1158 (tramp-file-name-localname (tramp-dissect-file-name tmp-name3))
1159 (tramp-file-name-localname (tramp-dissect-file-name tmp-name2)))
1161 (when (file-symlink-p tmp-name2)
1162 (setq attr (file-attributes tmp-name2))
1166 (tramp-file-name-localname (tramp-dissect-file-name tmp-name3))))
1167 (delete-file tmp-name2))
1169 (delete-file tmp-name1)
1170 (make-directory tmp-name1)
1171 (should (file-exists-p tmp-name1))
1172 (should (file-readable-p tmp-name1))
1173 (should-not (file-regular-p tmp-name1))
1174 (setq attr (file-attributes tmp-name1))
1175 (should (eq (car attr) t)))
1178 (ignore-errors (delete-directory tmp-name1))
1179 (ignore-errors (delete-file tmp-name1))
1180 (ignore-errors (delete-file tmp-name2)))))
1182 (ert-deftest tramp-test19-directory-files-and-attributes ()
1183 "Check `directory-files-and-attributes'."
1184 (skip-unless (tramp--test-enabled))
1186 ;; `directory-files-and-attributes' contains also values for "../".
1187 ;; Ensure that this doesn't change during tests, for
1188 ;; example due to handling temporary files.
1189 (let* ((tmp-name1 (tramp--test-make-temp-name))
1190 (tmp-name2 (expand-file-name "bla" tmp-name1))
1194 (make-directory tmp-name1)
1195 (should (file-directory-p tmp-name1))
1196 (make-directory tmp-name2)
1197 (should (file-directory-p tmp-name2))
1198 (write-region "foo" nil (expand-file-name "foo" tmp-name2))
1199 (write-region "bar" nil (expand-file-name "bar" tmp-name2))
1200 (write-region "boz" nil (expand-file-name "boz" tmp-name2))
1201 (setq attr (directory-files-and-attributes tmp-name2))
1202 (should (consp attr))
1203 ;; Dumb remote shells without perl(1) or stat(1) are not
1204 ;; able to return the date correctly. They say "don't know".
1209 (file-attributes (expand-file-name (car elt) tmp-name2)))
1212 (equal (file-attributes (expand-file-name (car elt) tmp-name2))
1214 (setq attr (directory-files-and-attributes tmp-name2 'full))
1216 (unless (equal (nth 5 (file-attributes (car elt))) '(0 0))
1218 (equal (file-attributes (car elt)) (cdr elt)))))
1219 (setq attr (directory-files-and-attributes tmp-name2 nil "^b"))
1220 (should (equal (mapcar 'car attr) '("bar" "boz"))))
1223 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1225 (ert-deftest tramp-test20-file-modes ()
1226 "Check `file-modes'.
1227 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
1228 (skip-unless (tramp--test-enabled))
1232 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1233 '(tramp-adb-file-name-handler
1234 tramp-gvfs-file-name-handler
1235 tramp-smb-file-name-handler))))
1237 (let ((tmp-name (tramp--test-make-temp-name)))
1240 (write-region "foo" nil tmp-name)
1241 (should (file-exists-p tmp-name))
1242 (set-file-modes tmp-name #o777)
1243 (should (= (file-modes tmp-name) #o777))
1244 (should (file-executable-p tmp-name))
1245 (should (file-writable-p tmp-name))
1246 (set-file-modes tmp-name #o444)
1247 (should (= (file-modes tmp-name) #o444))
1248 (should-not (file-executable-p tmp-name))
1249 ;; A file is always writable for user "root".
1250 (unless (zerop (nth 2 (file-attributes tmp-name)))
1251 (should-not (file-writable-p tmp-name))))
1254 (ignore-errors (delete-file tmp-name)))))
1256 (ert-deftest tramp-test21-file-links ()
1257 "Check `file-symlink-p'.
1258 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
1259 (skip-unless (tramp--test-enabled))
1261 ;; We must use `file-truename' for the temporary directory, because
1262 ;; it could be located on a symlinked directory. This would let the
1264 (let* ((tramp-test-temporary-file-directory
1265 (file-truename tramp-test-temporary-file-directory))
1266 (tmp-name1 (tramp--test-make-temp-name))
1267 (tmp-name2 (tramp--test-make-temp-name))
1268 (tmp-name3 (tramp--test-make-temp-name 'local)))
1270 ;; Check `make-symbolic-link'.
1273 (write-region "foo" nil tmp-name1)
1274 (should (file-exists-p tmp-name1))
1275 ;; Method "smb" supports `make-symbolic-link' only if the
1276 ;; remote host has CIFS capabilities. tramp-adb.el and
1277 ;; tramp-gvfs.el do not support symbolic links at all.
1279 (make-symbolic-link tmp-name1 tmp-name2)
1282 (not (string-equal (error-message-string err)
1283 "make-symbolic-link not supported")))))
1284 (should (file-symlink-p tmp-name2))
1285 (should-error (make-symbolic-link tmp-name1 tmp-name2))
1286 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
1287 (should (file-symlink-p tmp-name2))
1288 ;; `tmp-name3' is a local file name.
1289 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
1293 (delete-file tmp-name1)
1294 (delete-file tmp-name2)))
1296 ;; Check `add-name-to-file'.
1299 (write-region "foo" nil tmp-name1)
1300 (should (file-exists-p tmp-name1))
1301 (add-name-to-file tmp-name1 tmp-name2)
1302 (should-not (file-symlink-p tmp-name2))
1303 (should-error (add-name-to-file tmp-name1 tmp-name2))
1304 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
1305 (should-not (file-symlink-p tmp-name2))
1306 ;; `tmp-name3' is a local file name.
1307 (should-error (add-name-to-file tmp-name1 tmp-name3)))
1311 (delete-file tmp-name1)
1312 (delete-file tmp-name2)))
1314 ;; Check `file-truename'.
1317 (write-region "foo" nil tmp-name1)
1318 (should (file-exists-p tmp-name1))
1319 (make-symbolic-link tmp-name1 tmp-name2)
1320 (should (file-symlink-p tmp-name2))
1321 (should-not (string-equal tmp-name2 (file-truename tmp-name2)))
1323 (string-equal (file-truename tmp-name1) (file-truename tmp-name2)))
1324 (should (file-equal-p tmp-name1 tmp-name2)))
1326 (delete-file tmp-name1)
1327 (delete-file tmp-name2)))
1329 ;; `file-truename' shall preserve trailing link of directories.
1330 (unless (file-symlink-p tramp-test-temporary-file-directory)
1331 (let* ((dir1 (directory-file-name tramp-test-temporary-file-directory))
1332 (dir2 (file-name-as-directory dir1)))
1333 (should (string-equal (file-truename dir1) (expand-file-name dir1)))
1334 (should (string-equal (file-truename dir2) (expand-file-name dir2)))))))
1336 (ert-deftest tramp-test22-file-times ()
1337 "Check `set-file-times' and `file-newer-than-file-p'."
1338 (skip-unless (tramp--test-enabled))
1342 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1343 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1345 (let ((tmp-name1 (tramp--test-make-temp-name))
1346 (tmp-name2 (tramp--test-make-temp-name))
1347 (tmp-name3 (tramp--test-make-temp-name)))
1350 (write-region "foo" nil tmp-name1)
1351 (should (file-exists-p tmp-name1))
1352 (should (consp (nth 5 (file-attributes tmp-name1))))
1353 ;; '(0 0) means don't know, and will be replaced by
1354 ;; `current-time'. Therefore, we use '(0 1).
1355 ;; We skip the test, if the remote handler is not able to
1356 ;; set the correct time.
1357 (skip-unless (set-file-times tmp-name1 '(0 1)))
1358 ;; Dumb remote shells without perl(1) or stat(1) are not
1359 ;; able to return the date correctly. They say "don't know".
1360 (unless (equal (nth 5 (file-attributes tmp-name1)) '(0 0))
1361 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
1362 (write-region "bla" nil tmp-name2)
1363 (should (file-exists-p tmp-name2))
1364 (should (file-newer-than-file-p tmp-name2 tmp-name1))
1365 ;; `tmp-name3' does not exist.
1366 (should (file-newer-than-file-p tmp-name2 tmp-name3))
1367 (should-not (file-newer-than-file-p tmp-name3 tmp-name1))))
1371 (delete-file tmp-name1)
1372 (delete-file tmp-name2)))))
1374 (ert-deftest tramp-test23-visited-file-modtime ()
1375 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
1376 (skip-unless (tramp--test-enabled))
1378 (let ((tmp-name (tramp--test-make-temp-name)))
1381 (write-region "foo" nil tmp-name)
1382 (should (file-exists-p tmp-name))
1384 (insert-file-contents tmp-name)
1385 (should (verify-visited-file-modtime))
1386 (set-visited-file-modtime '(0 1))
1387 (should (verify-visited-file-modtime))
1388 (should (equal (visited-file-modtime) '(0 1 0 0)))))
1391 (ignore-errors (delete-file tmp-name)))))
1393 (ert-deftest tramp-test24-file-name-completion ()
1394 "Check `file-name-completion' and `file-name-all-completions'."
1395 (skip-unless (tramp--test-enabled))
1397 (dolist (n-e '(nil t))
1398 (let ((non-essential n-e)
1399 (tmp-name (tramp--test-make-temp-name))
1400 (method (file-remote-p tramp-test-temporary-file-directory 'method))
1401 (host (file-remote-p tramp-test-temporary-file-directory 'host)))
1405 ;; Method and host name in completion mode. This kind of
1406 ;; completion does not work on MS Windows.
1407 (when (and (tramp-completion-mode-p)
1408 (not (memq system-type '(cygwin windows-nt))))
1409 (unless (zerop (length method))
1412 (format "%s:" method)
1413 (file-name-all-completions (substring method 0 1) "/"))))
1414 (unless (zerop (length host))
1415 (let ((tramp-default-method (or method tramp-default-method)))
1419 (file-name-all-completions (substring host 0 1) "/")))))
1420 (unless (or (zerop (length method)) (zerop (length host)))
1424 (file-name-all-completions
1425 (substring host 0 1) (format "/%s:" method))))))
1428 (make-directory tmp-name)
1429 (should (file-directory-p tmp-name))
1430 (write-region "foo" nil (expand-file-name "foo" tmp-name))
1431 (should (file-exists-p (expand-file-name "foo" tmp-name)))
1432 (write-region "bar" nil (expand-file-name "bold" tmp-name))
1433 (should (file-exists-p (expand-file-name "bold" tmp-name)))
1434 (make-directory (expand-file-name "boz" tmp-name))
1435 (should (file-directory-p (expand-file-name "boz" tmp-name)))
1436 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1437 (should (equal (file-name-completion "foo" tmp-name) t))
1438 (should (equal (file-name-completion "b" tmp-name) "bo"))
1439 (should-not (file-name-completion "a" tmp-name))
1442 (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
1443 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
1446 (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
1448 (should-not (file-name-all-completions "a" tmp-name))
1449 ;; `completion-regexp-list' restricts the completion to
1450 ;; files which match all expressions in this list.
1451 (let ((completion-regexp-list
1452 `(,directory-files-no-dot-files-regexp "b")))
1454 (equal (file-name-completion "" tmp-name) "bo"))
1457 (sort (file-name-all-completions "" tmp-name) 'string-lessp)
1459 ;; `file-name-completion' ignores file names that end in
1460 ;; any string in `completion-ignored-extensions'.
1461 (let ((completion-ignored-extensions '(".ext")))
1462 (write-region "foo" nil (expand-file-name "foo.ext" tmp-name))
1463 (should (file-exists-p (expand-file-name "foo.ext" tmp-name)))
1464 (should (equal (file-name-completion "fo" tmp-name) "foo"))
1465 (should (equal (file-name-completion "foo" tmp-name) t))
1466 (should (equal (file-name-completion "foo." tmp-name) "foo.ext"))
1467 (should (equal (file-name-completion "foo.ext" tmp-name) t))
1468 ;; `file-name-all-completions' is not affected.
1471 (sort (file-name-all-completions "" tmp-name) 'string-lessp)
1472 '("../" "./" "bold" "boz/" "foo" "foo.ext")))))
1475 (ignore-errors (delete-directory tmp-name 'recursive))))))
1477 (ert-deftest tramp-test25-load ()
1479 (skip-unless (tramp--test-enabled))
1481 (let ((tmp-name (tramp--test-make-temp-name)))
1484 (load tmp-name 'noerror 'nomessage)
1485 (should-not (featurep 'tramp-test-load))
1486 (write-region "(provide 'tramp-test-load)" nil tmp-name)
1487 ;; `load' in lread.c does not pass `must-suffix'. Why?
1488 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
1489 (load tmp-name nil 'nomessage 'nosuffix)
1490 (should (featurep 'tramp-test-load)))
1494 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
1495 (delete-file tmp-name)))))
1497 (ert-deftest tramp-test26-process-file ()
1498 "Check `process-file'."
1499 :tags '(:expensive-test)
1500 (skip-unless (tramp--test-enabled))
1504 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1505 '(tramp-gvfs-file-name-handler tramp-smb-file-name-handler))))
1507 (let* ((tmp-name (tramp--test-make-temp-name))
1508 (fnnd (file-name-nondirectory tmp-name))
1509 (default-directory tramp-test-temporary-file-directory)
1510 kill-buffer-query-functions)
1513 ;; We cannot use "/bin/true" and "/bin/false"; those paths
1514 ;; do not exist on hydra.
1515 (should (zerop (process-file "true")))
1516 (should-not (zerop (process-file "false")))
1517 (should-not (zerop (process-file "binary-does-not-exist")))
1519 (write-region "foo" nil tmp-name)
1520 (should (file-exists-p tmp-name))
1521 (should (zerop (process-file "ls" nil t nil fnnd)))
1522 ;; `ls' could produce colorized output.
1523 (goto-char (point-min))
1525 (re-search-forward tramp-display-escape-sequence-regexp nil t)
1526 (replace-match "" nil nil))
1527 (should (string-equal (format "%s\n" fnnd) (buffer-string)))
1528 (should-not (get-buffer-window (current-buffer) t))
1530 ;; Second run. The output must be appended.
1531 (goto-char (point-max))
1532 (should (zerop (process-file "ls" nil t t fnnd)))
1533 ;; `ls' could produce colorized output.
1534 (goto-char (point-min))
1536 (re-search-forward tramp-display-escape-sequence-regexp nil t)
1537 (replace-match "" nil nil))
1539 (string-equal (format "%s\n%s\n" fnnd fnnd) (buffer-string)))
1540 ;; A non-nil DISPLAY must not raise the buffer.
1541 (should-not (get-buffer-window (current-buffer) t))))
1544 (ignore-errors (delete-file tmp-name)))))
1546 (ert-deftest tramp-test27-start-file-process ()
1547 "Check `start-file-process'."
1548 :tags '(:expensive-test)
1549 (skip-unless (tramp--test-enabled))
1553 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1554 '(tramp-adb-file-name-handler
1555 tramp-gvfs-file-name-handler
1556 tramp-smb-file-name-handler))))
1558 (let ((default-directory tramp-test-temporary-file-directory)
1559 (tmp-name (tramp--test-make-temp-name))
1560 kill-buffer-query-functions proc)
1563 (setq proc (start-file-process "test1" (current-buffer) "cat"))
1564 (should (processp proc))
1565 (should (equal (process-status proc) 'run))
1566 (process-send-string proc "foo")
1567 (process-send-eof proc)
1569 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1570 (while (< (- (point-max) (point-min)) (length "foo"))
1571 (accept-process-output proc 1)))
1572 (should (string-equal (buffer-string) "foo")))
1575 (ignore-errors (delete-process proc)))
1579 (write-region "foo" nil tmp-name)
1580 (should (file-exists-p tmp-name))
1583 "test2" (current-buffer)
1584 "cat" (file-name-nondirectory tmp-name)))
1585 (should (processp proc))
1587 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1588 (while (< (- (point-max) (point-min)) (length "foo"))
1589 (accept-process-output proc 1)))
1590 (should (string-equal (buffer-string) "foo")))
1594 (delete-process proc)
1595 (delete-file tmp-name)))
1599 (setq proc (start-file-process "test3" (current-buffer) "cat"))
1600 (should (processp proc))
1601 (should (equal (process-status proc) 'run))
1604 (lambda (p s) (with-current-buffer (process-buffer p) (insert s))))
1605 (process-send-string proc "foo")
1606 (process-send-eof proc)
1608 (with-timeout (10 (ert-fail "`start-file-process' timed out"))
1609 (while (< (- (point-max) (point-min)) (length "foo"))
1610 (accept-process-output proc 1)))
1611 (should (string-equal (buffer-string) "foo")))
1614 (ignore-errors (delete-process proc)))))
1616 (ert-deftest tramp-test28-shell-command ()
1617 "Check `shell-command'."
1618 :tags '(:expensive-test)
1619 (skip-unless (tramp--test-enabled))
1623 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1624 '(tramp-adb-file-name-handler
1625 tramp-gvfs-file-name-handler
1626 tramp-smb-file-name-handler))))
1628 (let ((tmp-name (tramp--test-make-temp-name))
1629 (default-directory tramp-test-temporary-file-directory)
1630 kill-buffer-query-functions)
1633 (write-region "foo" nil tmp-name)
1634 (should (file-exists-p tmp-name))
1636 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1637 ;; `ls' could produce colorized output.
1638 (goto-char (point-min))
1639 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1640 (replace-match "" nil nil))
1643 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1646 (ignore-errors (delete-file tmp-name)))
1650 (write-region "foo" nil tmp-name)
1651 (should (file-exists-p tmp-name))
1652 (async-shell-command
1653 (format "ls %s" (file-name-nondirectory tmp-name)) (current-buffer))
1654 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1656 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1657 (while (< (- (point-max) (point-min))
1658 (1+ (length (file-name-nondirectory tmp-name))))
1659 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1660 ;; `ls' could produce colorized output.
1661 (goto-char (point-min))
1662 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1663 (replace-match "" nil nil))
1664 ;; There might be a nasty "Process *Async Shell* finished" message.
1665 (goto-char (point-min))
1667 (narrow-to-region (point-min) (point))
1670 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1673 (ignore-errors (delete-file tmp-name)))
1677 (write-region "foo" nil tmp-name)
1678 (should (file-exists-p tmp-name))
1679 (async-shell-command "read line; ls $line" (current-buffer))
1680 (set-process-sentinel (get-buffer-process (current-buffer)) nil)
1681 (process-send-string
1682 (get-buffer-process (current-buffer))
1683 (format "%s\n" (file-name-nondirectory tmp-name)))
1685 (with-timeout (10 (ert-fail "`async-shell-command' timed out"))
1686 (while (< (- (point-max) (point-min))
1687 (1+ (length (file-name-nondirectory tmp-name))))
1688 (accept-process-output (get-buffer-process (current-buffer)) 1)))
1689 ;; `ls' could produce colorized output.
1690 (goto-char (point-min))
1691 (while (re-search-forward tramp-display-escape-sequence-regexp nil t)
1692 (replace-match "" nil nil))
1693 ;; There might be a nasty "Process *Async Shell* finished" message.
1694 (goto-char (point-min))
1696 (narrow-to-region (point-min) (point))
1699 (format "%s\n" (file-name-nondirectory tmp-name)) (buffer-string))))
1702 (ignore-errors (delete-file tmp-name)))))
1704 (ert-deftest tramp-test29-vc-registered ()
1705 "Check `vc-registered'."
1706 :tags '(:expensive-test)
1707 (skip-unless (tramp--test-enabled))
1710 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
1711 'tramp-sh-file-name-handler))
1713 (let* ((default-directory tramp-test-temporary-file-directory)
1714 (tmp-name1 (tramp--test-make-temp-name))
1715 (tmp-name2 (expand-file-name "foo" tmp-name1))
1716 (tramp-remote-process-environment tramp-remote-process-environment)
1717 (vc-handled-backends
1718 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
1720 ((tramp-find-executable v vc-git-program (tramp-get-remote-path v))
1722 ((tramp-find-executable v vc-hg-program (tramp-get-remote-path v))
1724 ((tramp-find-executable v vc-bzr-program (tramp-get-remote-path v))
1725 (setq tramp-remote-process-environment
1726 (cons (format "BZR_HOME=%s"
1727 (file-remote-p tmp-name1 'localname))
1728 tramp-remote-process-environment))
1729 ;; We must force a reconnect, in order to activate $BZR_HOME.
1730 (tramp-cleanup-connection
1731 (tramp-dissect-file-name tramp-test-temporary-file-directory)
1735 (skip-unless vc-handled-backends)
1736 (message "%s" vc-handled-backends)
1740 (make-directory tmp-name1)
1741 (write-region "foo" nil tmp-name2)
1742 (should (file-directory-p tmp-name1))
1743 (should (file-exists-p tmp-name2))
1744 (should-not (vc-registered tmp-name1))
1745 (should-not (vc-registered tmp-name2))
1747 (let ((default-directory tmp-name1))
1748 ;; Create empty repository, and register the file.
1749 ;; Sometimes, creation of repository fails (bzr!); we skip
1752 (vc-create-repo (car vc-handled-backends))
1753 (error (skip-unless nil)))
1754 ;; The structure of VC-FILESET is not documented. Let's
1755 ;; hope it won't change.
1758 (list (car vc-handled-backends)
1759 (list (file-name-nondirectory tmp-name2))))
1760 ;; `vc-register' has changed its arguments in Emacs 25.1.
1763 nil (list (car vc-handled-backends)
1764 (list (file-name-nondirectory tmp-name2))))))
1765 ;; vc-git uses an own process sentinel, Tramp's sentinel
1766 ;; for flushing the cache isn't used.
1767 (dired-uncache (concat (file-remote-p default-directory) "/"))
1768 (should (vc-registered (file-name-nondirectory tmp-name2)))))
1771 (ignore-errors (delete-directory tmp-name1 'recursive)))))
1773 (ert-deftest tramp-test30-make-auto-save-file-name ()
1774 "Check `make-auto-save-file-name'."
1775 (skip-unless (tramp--test-enabled))
1777 (let ((tmp-name1 (tramp--test-make-temp-name))
1778 (tmp-name2 (tramp--test-make-temp-name)))
1782 ;; Use default `auto-save-file-name-transforms' mechanism.
1783 (let (tramp-auto-save-directory)
1785 (setq buffer-file-name tmp-name1)
1788 (make-auto-save-file-name)
1789 ;; This is taken from original `make-auto-save-file-name'.
1793 (subst-char-in-string
1794 ?/ ?! (replace-regexp-in-string "!" "!!" tmp-name1)))
1795 temporary-file-directory)))))
1798 (let (tramp-auto-save-directory auto-save-file-name-transforms)
1800 (setq buffer-file-name tmp-name1)
1803 (make-auto-save-file-name)
1805 (format "#%s#" (file-name-nondirectory tmp-name1))
1806 tramp-test-temporary-file-directory)))))
1808 ;; Use default `tramp-auto-save-directory' mechanism.
1809 (let ((tramp-auto-save-directory tmp-name2))
1811 (setq buffer-file-name tmp-name1)
1814 (make-auto-save-file-name)
1815 ;; This is taken from Tramp.
1819 (tramp-subst-strs-in-string
1828 (should (file-directory-p tmp-name2))))
1830 ;; Relative file names shall work, too.
1831 (let ((tramp-auto-save-directory "."))
1833 (setq buffer-file-name tmp-name1
1834 default-directory tmp-name2)
1837 (make-auto-save-file-name)
1838 ;; This is taken from Tramp.
1842 (tramp-subst-strs-in-string
1851 (should (file-directory-p tmp-name2)))))
1854 (ignore-errors (delete-file tmp-name1))
1855 (ignore-errors (delete-directory tmp-name2 'recursive)))))
1857 (defun tramp--test-adb-p ()
1858 "Check, whether the remote host runs Android.
1859 This requires restrictions of file name syntax."
1860 (tramp-adb-file-name-p tramp-test-temporary-file-directory))
1862 (defun tramp--test-ftp-p ()
1863 "Check, whether an FTP-like method is used.
1864 This does not support globbing characters in file names (yet)."
1865 ;; Globbing characters are ??, ?* and ?\[.
1866 (and (eq (tramp-find-foreign-file-name-handler
1867 tramp-test-temporary-file-directory)
1868 'tramp-sh-file-name-handler)
1870 "ftp$" (file-remote-p tramp-test-temporary-file-directory 'method))))
1872 (defun tramp--test-rsync-p ()
1873 "Check, whether the rsync method is used.
1874 This does not support special file names."
1876 "rsync" (file-remote-p tramp-test-temporary-file-directory 'method)))
1878 (defun tramp--test-gvfs-p ()
1879 "Check, whether the remote host runs a GVFS based method.
1880 This requires restrictions of file name syntax."
1881 (tramp-gvfs-file-name-p tramp-test-temporary-file-directory))
1883 (defun tramp--test-smb-or-windows-nt-p ()
1884 "Check, whether the locale or remote host runs MS Windows.
1885 This requires restrictions of file name syntax."
1886 (or (eq system-type 'windows-nt)
1887 (tramp-smb-file-name-p tramp-test-temporary-file-directory)))
1889 (defun tramp--test-hpux-p ()
1890 "Check, whether the remote host runs HP-UX.
1891 Several special characters do not work properly there."
1892 ;; We must refill the cache. `file-truename' does it.
1893 (with-parsed-tramp-file-name
1894 (file-truename tramp-test-temporary-file-directory) nil
1895 (string-match "^HP-UX" (tramp-get-connection-property v "uname" ""))))
1897 (defun tramp--test-check-files (&rest files)
1898 "Run a simple but comprehensive test over every file in FILES."
1899 ;; We must use `file-truename' for the temporary directory, because
1900 ;; it could be located on a symlinked directory. This would let the
1902 (let* ((tramp-test-temporary-file-directory
1903 (file-truename tramp-test-temporary-file-directory))
1904 (tmp-name1 (tramp--test-make-temp-name))
1905 (tmp-name2 (tramp--test-make-temp-name 'local))
1906 (files (delq nil files)))
1909 (make-directory tmp-name1)
1910 (make-directory tmp-name2)
1912 (let* ((file1 (expand-file-name elt tmp-name1))
1913 (file2 (expand-file-name elt tmp-name2))
1914 (file3 (expand-file-name (concat elt "foo") tmp-name1)))
1915 (write-region elt nil file1)
1916 (should (file-exists-p file1))
1918 ;; Check file contents.
1920 (insert-file-contents file1)
1921 (should (string-equal (buffer-string) elt)))
1923 ;; Copy file both directions.
1924 (copy-file file1 tmp-name2)
1925 (should (file-exists-p file2))
1927 (should-not (file-exists-p file1))
1928 (copy-file file2 tmp-name1)
1929 (should (file-exists-p file1))
1931 ;; Method "smb" supports `make-symbolic-link' only if the
1932 ;; remote host has CIFS capabilities. tramp-adb.el and
1933 ;; tramp-gvfs.el do not support symbolic links at all.
1936 (make-symbolic-link file1 file3)
1937 (should (file-symlink-p file3))
1940 (expand-file-name file1) (file-truename file3)))
1943 (car (file-attributes file3))
1944 (file-remote-p (file-truename file1) 'localname)))
1945 ;; Check file contents.
1947 (insert-file-contents file3)
1948 (should (string-equal (buffer-string) elt)))
1949 (delete-file file3))
1951 (should (string-equal (error-message-string err)
1952 "make-symbolic-link not supported"))))))
1954 ;; Check file names.
1955 (should (equal (directory-files
1956 tmp-name1 nil directory-files-no-dot-files-regexp)
1957 (sort (copy-sequence files) 'string-lessp)))
1958 (should (equal (directory-files
1959 tmp-name2 nil directory-files-no-dot-files-regexp)
1960 (sort (copy-sequence files) 'string-lessp)))
1962 ;; `substitute-in-file-name' could return different values.
1963 ;; For `adb', there could be strange file permissions
1964 ;; preventing overwriting a file. We don't care in this
1968 (substitute-in-file-name (expand-file-name elt tmp-name1)))
1970 (substitute-in-file-name (expand-file-name elt tmp-name2))))
1971 (ignore-errors (write-region elt nil file1))
1972 (should (file-exists-p file1))
1973 (ignore-errors (write-region elt nil file2 nil 'nomessage))
1974 (should (file-exists-p file2))))
1976 (should (equal (directory-files
1977 tmp-name1 nil directory-files-no-dot-files-regexp)
1979 tmp-name2 nil directory-files-no-dot-files-regexp)))
1981 ;; Check directory creation. We use a subdirectory "foo"
1982 ;; in order to avoid conflicts with previous file name tests.
1984 (let* ((elt1 (concat elt "foo"))
1985 (file1 (expand-file-name (concat "foo/" elt) tmp-name1))
1986 (file2 (expand-file-name elt file1))
1987 (file3 (expand-file-name elt1 file1)))
1988 (make-directory file1 'parents)
1989 (should (file-directory-p file1))
1990 (write-region elt nil file2)
1991 (should (file-exists-p file2))
1994 (directory-files file1 nil directory-files-no-dot-files-regexp)
1998 (caar (directory-files-and-attributes
1999 file1 nil directory-files-no-dot-files-regexp))
2002 ;; Check symlink in `directory-files-and-attributes'.
2005 (make-symbolic-link file2 file3)
2006 (should (file-symlink-p file3))
2009 (caar (directory-files-and-attributes
2010 file1 nil (regexp-quote elt1)))
2014 (cadr (car (directory-files-and-attributes
2015 file1 nil (regexp-quote elt1))))
2016 (file-remote-p (file-truename file2) 'localname)))
2018 (should-not (file-exists-p file3)))
2020 (should (string-equal (error-message-string err)
2021 "make-symbolic-link not supported"))))
2024 (should-not (file-exists-p file2))
2025 (delete-directory file1)
2026 (should-not (file-exists-p file1)))))
2029 (ignore-errors (delete-directory tmp-name1 'recursive))
2030 (ignore-errors (delete-directory tmp-name2 'recursive)))))
2032 (defun tramp--test-special-characters ()
2033 "Perform the test in `tramp-test31-special-characters*'."
2034 ;; Newlines, slashes and backslashes in file names are not
2035 ;; supported. So we don't test. And we don't test the tab
2036 ;; character on Windows or Cygwin, because the backslash is
2037 ;; interpreted as a path separator, preventing "\t" from being
2038 ;; expanded to <TAB>.
2039 (tramp--test-check-files
2040 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2042 (if (or (tramp--test-adb-p) (eq system-type 'cygwin))
2049 (unless (or (tramp--test-ftp-p)
2050 (tramp--test-gvfs-p)
2051 (tramp--test-smb-or-windows-nt-p))
2053 (unless (or (tramp--test-ftp-p)
2054 (tramp--test-gvfs-p)
2055 (tramp--test-smb-or-windows-nt-p))
2057 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2061 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2064 (if (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2067 (unless (or (tramp--test-gvfs-p) (tramp--test-smb-or-windows-nt-p))
2070 (unless (or (tramp--test-ftp-p) (tramp--test-gvfs-p)) "[foo]bar[baz]")
2073 ;; These tests are inspired by Bug#17238.
2074 (ert-deftest tramp-test31-special-characters ()
2075 "Check special characters in file names."
2076 (skip-unless (tramp--test-enabled))
2077 (skip-unless (not (tramp--test-rsync-p)))
2079 (tramp--test-special-characters))
2081 (ert-deftest tramp-test31-special-characters-with-stat ()
2082 "Check special characters in file names.
2083 Use the `stat' command."
2084 :tags '(:expensive-test)
2085 (skip-unless (tramp--test-enabled))
2086 (skip-unless (not (tramp--test-rsync-p)))
2089 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2090 'tramp-sh-file-name-handler))
2091 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2092 (skip-unless (tramp-get-remote-stat v)))
2094 (let ((tramp-connection-properties
2096 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2098 tramp-connection-properties)))
2099 (tramp--test-special-characters)))
2101 (ert-deftest tramp-test31-special-characters-with-perl ()
2102 "Check special characters in file names.
2103 Use the `perl' command."
2104 :tags '(:expensive-test)
2105 (skip-unless (tramp--test-enabled))
2106 (skip-unless (not (tramp--test-rsync-p)))
2109 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2110 'tramp-sh-file-name-handler))
2111 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2112 (skip-unless (tramp-get-remote-perl v)))
2114 (let ((tramp-connection-properties
2116 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2118 ;; See `tramp-sh-handle-file-truename'.
2119 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2121 tramp-connection-properties)))
2122 (tramp--test-special-characters)))
2124 (ert-deftest tramp-test31-special-characters-with-ls ()
2125 "Check special characters in file names.
2126 Use the `ls' command."
2127 :tags '(:expensive-test)
2128 (skip-unless (tramp--test-enabled))
2129 (skip-unless (not (tramp--test-rsync-p)))
2132 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2133 'tramp-sh-file-name-handler))
2135 (let ((tramp-connection-properties
2137 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2139 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2141 ;; See `tramp-sh-handle-file-truename'.
2142 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2144 tramp-connection-properties)))
2145 (tramp--test-special-characters)))
2147 (defun tramp--test-utf8 ()
2148 "Perform the test in `tramp-test32-utf8*'."
2149 (let* ((utf8 (if (and (eq system-type 'darwin)
2150 (memq 'utf-8-hfs (coding-system-list)))
2152 (coding-system-for-read utf8)
2153 (coding-system-for-write utf8)
2154 (file-name-coding-system utf8))
2155 (tramp--test-check-files
2156 (unless (tramp--test-hpux-p) "Γυρίστε το Γαλαξία με Ώτο Στοπ")
2157 (unless (tramp--test-hpux-p)
2158 "أصبح بوسعك الآن تنزيل نسخة كاملة من موسوعة ويكيبيديا العربية لتصفحها بلا اتصال بالإنترنت")
2160 "Автостопом по гала́ктике")))
2162 (ert-deftest tramp-test32-utf8 ()
2163 "Check UTF8 encoding in file names and file contents."
2164 (skip-unless (tramp--test-enabled))
2165 (skip-unless (not (tramp--test-rsync-p)))
2169 (ert-deftest tramp-test32-utf8-with-stat ()
2170 "Check UTF8 encoding in file names and file contents.
2171 Use the `stat' command."
2172 :tags '(:expensive-test)
2173 (skip-unless (tramp--test-enabled))
2174 (skip-unless (not (tramp--test-rsync-p)))
2177 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2178 'tramp-sh-file-name-handler))
2179 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2180 (skip-unless (tramp-get-remote-stat v)))
2182 (let ((tramp-connection-properties
2184 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2186 tramp-connection-properties)))
2187 (tramp--test-utf8)))
2189 (ert-deftest tramp-test32-utf8-with-perl ()
2190 "Check UTF8 encoding in file names and file contents.
2191 Use the `perl' command."
2192 :tags '(:expensive-test)
2193 (skip-unless (tramp--test-enabled))
2194 (skip-unless (not (tramp--test-rsync-p)))
2197 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2198 'tramp-sh-file-name-handler))
2199 (with-parsed-tramp-file-name tramp-test-temporary-file-directory nil
2200 (skip-unless (tramp-get-remote-perl v)))
2202 (let ((tramp-connection-properties
2204 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2206 ;; See `tramp-sh-handle-file-truename'.
2207 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2209 tramp-connection-properties)))
2210 (tramp--test-utf8)))
2212 (ert-deftest tramp-test32-utf8-with-ls ()
2213 "Check UTF8 encoding in file names and file contents.
2214 Use the `ls' command."
2215 :tags '(:expensive-test)
2216 (skip-unless (tramp--test-enabled))
2217 (skip-unless (not (tramp--test-rsync-p)))
2220 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2221 'tramp-sh-file-name-handler))
2223 (let ((tramp-connection-properties
2225 `((,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2227 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2229 ;; See `tramp-sh-handle-file-truename'.
2230 (,(regexp-quote (file-remote-p tramp-test-temporary-file-directory))
2232 tramp-connection-properties)))
2233 (tramp--test-utf8)))
2235 ;; This test is inspired by Bug#16928.
2236 (ert-deftest tramp-test33-asynchronous-requests ()
2237 "Check parallel asynchronous requests.
2238 Such requests could arrive from timers, process filters and
2239 process sentinels. They shall not disturb each other."
2240 ;; Mark as failed until bug has been fixed.
2241 :expected-result :failed
2242 :tags '(:expensive-test)
2243 (skip-unless (tramp--test-enabled))
2246 (tramp-find-foreign-file-name-handler tramp-test-temporary-file-directory)
2247 'tramp-sh-file-name-handler))
2249 ;; Keep instrumentation verbosity 0 until Tramp bug is fixed. This
2250 ;; has the side effect, that this test fails instead to abort. Good
2252 (tramp--instrument-test-case 0
2253 (let* ((tmp-name (tramp--test-make-temp-name))
2254 (default-directory tmp-name)
2255 (remote-file-name-inhibit-cache t)
2256 timer buffers kill-buffer-query-functions)
2260 (make-directory tmp-name)
2262 ;; Setup a timer in order to raise an ordinary command again
2263 ;; and again. `vc-registered' is well suited, because there
2272 (buffer-name (nth (random (length buffers)) buffers)))))))
2274 ;; Create temporary buffers. The number of buffers
2275 ;; corresponds to the number of processes; it could be
2276 ;; increased in order to make pressure on Tramp.
2278 (add-to-list 'buffers (generate-new-buffer "*temp*")))
2280 ;; Open asynchronous processes. Set process sentinel.
2281 (dolist (buf buffers)
2282 (async-shell-command "read line; touch $line; echo $line" buf)
2283 (set-process-sentinel
2284 (get-buffer-process buf)
2285 (lambda (proc _state)
2286 (delete-file (buffer-name (process-buffer proc))))))
2288 ;; Send a string. Use a random order of the buffers. Mix
2289 ;; with regular operation.
2290 (let ((buffers (copy-sequence buffers))
2293 (setq buf (nth (random (length buffers)) buffers))
2294 (process-send-string
2295 (get-buffer-process buf) (format "'%s'\n" buf))
2296 (file-attributes (buffer-name buf))
2297 (setq buffers (delq buf buffers))))
2299 ;; Wait until the whole output has been read.
2300 (with-timeout ((* 10 (length buffers))
2301 (ert-fail "`async-shell-command' timed out"))
2302 (let ((buffers (copy-sequence buffers))
2305 (setq buf (nth (random (length buffers)) buffers))
2307 (memq (process-status (get-buffer-process buf))
2309 (accept-process-output (get-buffer-process buf) 0.1)
2310 (setq buffers (delq buf buffers))))))
2313 (dolist (buf buffers)
2314 (with-current-buffer buf
2316 (string-equal (format "'%s'\n" buf) (buffer-string)))))
2318 (directory-files tmp-name nil directory-files-no-dot-files-regexp)))
2321 (ignore-errors (cancel-timer timer))
2322 (ignore-errors (delete-directory tmp-name 'recursive))
2323 (dolist (buf buffers)
2324 (ignore-errors (kill-buffer buf)))))))
2326 (ert-deftest tramp-test34-recursive-load ()
2327 "Check that Tramp does not fail due to recursive load."
2328 (skip-unless (tramp--test-enabled))
2333 "(expand-file-name %S)"
2334 tramp-test-temporary-file-directory)
2336 "(let ((default-directory %S)) (expand-file-name %S))"
2337 tramp-test-temporary-file-directory
2338 temporary-file-directory)))
2342 (shell-command-to-string
2344 "%s -batch -Q -L %s --eval %s"
2345 (expand-file-name invocation-name invocation-directory)
2346 (mapconcat 'shell-quote-argument load-path " -L ")
2347 (shell-quote-argument code)))))))
2349 (ert-deftest tramp-test35-unload ()
2350 "Check that Tramp and its subpackages unload completely.
2351 Since it unloads Tramp, it shall be the last test to run."
2352 ;; Mark as failed until all symbols are unbound.
2353 :expected-result (if (featurep 'tramp) :failed :passed)
2354 :tags '(:expensive-test)
2355 (when (featurep 'tramp)
2356 (unload-feature 'tramp 'force)
2357 ;; No Tramp feature must be left.
2358 (should-not (featurep 'tramp))
2359 (should-not (all-completions "tramp" (delq 'tramp-tests features)))
2360 ;; `file-name-handler-alist' must be clean.
2361 (should-not (all-completions "tramp" (mapcar 'cdr file-name-handler-alist)))
2362 ;; There shouldn't be left a bound symbol. We do not regard our
2363 ;; test symbols, and the Tramp unload hooks.
2366 (and (or (boundp x) (functionp x))
2367 (string-match "^tramp" (symbol-name x))
2368 (not (string-match "^tramp--?test" (symbol-name x)))
2369 (not (string-match "unload-hook$" (symbol-name x)))
2370 (ert-fail (format "`%s' still bound" x)))))
2371 ;; There shouldn't be left a hook function containing a Tramp
2372 ;; function. We do not regard the Tramp unload hooks.
2376 (string-match "-hooks?$" (symbol-name x))
2377 (not (string-match "unload-hook$" (symbol-name x)))
2378 (consp (symbol-value x))
2379 (ignore-errors (all-completions "tramp" (symbol-value x)))
2380 (ert-fail (format "Hook `%s' still contains Tramp function" x)))))))
2384 ;; * dired-compress-file
2387 ;; * file-ownership-preserved-p
2388 ;; * file-selinux-context
2389 ;; * find-backup-file-name
2391 ;; * set-file-selinux-context
2393 ;; * Work on skipped tests. Make a comment, when it is impossible.
2394 ;; * Fix `tramp-test06-directory-file-name' for `ftp'.
2395 ;; * Fix `tramp-test15-copy-directory' for `rsync'.
2396 ;; * Fix `tramp-test27-start-file-process' on MS Windows (`process-send-eof'?).
2397 ;; * Fix Bug#16928. Set expected error of `tramp-test33-asynchronous-requests'.
2398 ;; * Fix `tramp-test35-unload' (Not all symbols are unbound). Set
2401 (defun tramp-test-all (&optional interactive)
2402 "Run all tests for \\[tramp]."
2405 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
2407 (provide 'tramp-tests)
2408 ;;; tramp-tests.el ends here