* automated/tramp-tests.el: New file.
[bpt/emacs.git] / test / automated / tramp-tests.el
1 ;;; tramp-tests.el --- Tests of remote file access
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6
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.
11 ;;
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.
16 ;;
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/'.
19
20 ;;; Commentary:
21
22 ;; Some of the tests are intended to run over remote files. Set
23 ;; `tramp-test-temporary-file-directory' to a suitable value. It must
24 ;; NOT require an interactive password prompt, when running the tests
25 ;; in batch mode.
26
27 ;; If you want to skip tests for remote files, set this variable to
28 ;; `null-device'.
29
30 ;;; Code:
31
32 (require 'ert)
33 (require 'tramp)
34
35 ;; There is no default value on w32 systems, which could work out of the box.
36 (defconst tramp-test-temporary-file-directory
37 (if (eq system-type 'windows-nt) null-device "/ssh::/tmp")
38 "Temporary directory for Tramp tests.")
39
40 (setq tramp-verbose 0
41 tramp-message-show-message nil)
42 (when noninteractive (defalias 'tramp-read-passwd 'ignore))
43
44 (defvar tramp--test-enabled-checked nil
45 "Cached result of `tramp--test-enabled'.
46 If the function did run, the value is a cons cell, the `cdr'
47 being the result.")
48
49 (defun tramp--test-enabled ()
50 "Whether remote file access is enabled."
51 (unless (consp tramp--test-enabled-checked)
52 (setq
53 tramp--test-enabled-checked
54 (cons
55 t (ignore-errors
56 (and
57 (file-remote-p tramp-test-temporary-file-directory)
58 (file-directory-p tramp-test-temporary-file-directory)
59 (file-writable-p tramp-test-temporary-file-directory))))))
60 ;; Return result.
61 (cdr tramp--test-enabled-checked))
62
63 (defun tramp--test-make-temp-name ()
64 "Create a temporary file name for test."
65 (expand-file-name
66 (make-temp-name "tramp-test") tramp-test-temporary-file-directory))
67
68 (ert-deftest tramp-test00-availability ()
69 "Test availability of Tramp functions."
70 :expected-result (if (tramp--test-enabled) :passed :failed)
71 (should (ignore-errors
72 (and
73 (file-remote-p tramp-test-temporary-file-directory)
74 (file-directory-p tramp-test-temporary-file-directory)
75 (file-writable-p tramp-test-temporary-file-directory)))))
76
77 (ert-deftest tramp-test01-file-name-syntax ()
78 "Check remote file name syntax."
79 ;; Simple cases.
80 (should (tramp-tramp-file-p "/method::"))
81 (should (tramp-tramp-file-p "/host:"))
82 (should (tramp-tramp-file-p "/user@:"))
83 (should (tramp-tramp-file-p "/user@host:"))
84 (should (tramp-tramp-file-p "/method:host:"))
85 (should (tramp-tramp-file-p "/method:user@:"))
86 (should (tramp-tramp-file-p "/method:user@host:"))
87 (should (tramp-tramp-file-p "/method:user@email@host:"))
88
89 ;; Using a port.
90 (should (tramp-tramp-file-p "/host#1234:"))
91 (should (tramp-tramp-file-p "/user@host#1234:"))
92 (should (tramp-tramp-file-p "/method:host#1234:"))
93 (should (tramp-tramp-file-p "/method:user@host#1234:"))
94
95 ;; Using an IPv4 address.
96 (should (tramp-tramp-file-p "/1.2.3.4:"))
97 (should (tramp-tramp-file-p "/user@1.2.3.4:"))
98 (should (tramp-tramp-file-p "/method:1.2.3.4:"))
99 (should (tramp-tramp-file-p "/method:user@1.2.3.4:"))
100
101 ;; Using an IPv6 address.
102 (should (tramp-tramp-file-p "/[]:"))
103 (should (tramp-tramp-file-p "/[::1]:"))
104 (should (tramp-tramp-file-p "/user@[::1]:"))
105 (should (tramp-tramp-file-p "/method:[::1]:"))
106 (should (tramp-tramp-file-p "/method:user@[::1]:"))
107
108 ;; Local file name part.
109 (should (tramp-tramp-file-p "/host:/:"))
110 (should (tramp-tramp-file-p "/method:::"))
111 (should (tramp-tramp-file-p "/method::/path/to/file"))
112 (should (tramp-tramp-file-p "/method::file"))
113
114 ;; Multihop.
115 (should (tramp-tramp-file-p "/method1:|method2::"))
116 (should (tramp-tramp-file-p "/method1:host1|host2:"))
117 (should (tramp-tramp-file-p "/method1:host1|method2:host2:"))
118 (should (tramp-tramp-file-p "/method1:user1@host1|method2:user2@host2:"))
119 (should (tramp-tramp-file-p
120 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:"))
121
122 ;; No strings.
123 (should-not (tramp-tramp-file-p nil))
124 (should-not (tramp-tramp-file-p 'symbol))
125 ;; "/:" suppresses file name handlers.
126 (should-not (tramp-tramp-file-p "/::"))
127 (should-not (tramp-tramp-file-p "/:@:"))
128 (should-not (tramp-tramp-file-p "/:[]:"))
129 ;; Multihops require a method.
130 (should-not (tramp-tramp-file-p "/host1|host2:"))
131 ;; Methods or hostnames shall be at least two characters on MS Windows.
132 (when (memq system-type '(cygwin windows-nt))
133 (should-not (tramp-tramp-file-p "/c:/path/to/file"))
134 (should-not (tramp-tramp-file-p "/c::/path/to/file"))))
135
136 (ert-deftest tramp-test02-file-name-dissect ()
137 "Check remote file name components."
138 (let ((tramp-default-method "default-method")
139 (tramp-default-user "default-user")
140 (tramp-default-host "default-host"))
141 ;; Expand `tramp-default-user' and `tramp-default-host'.
142 (should (string-equal
143 (file-remote-p "/method::")
144 (format "/%s:%s@%s:" "method" "default-user" "default-host")))
145 (should (string-equal (file-remote-p "/method::" 'method) "method"))
146 (should (string-equal (file-remote-p "/method::" 'user) "default-user"))
147 (should (string-equal (file-remote-p "/method::" 'host) "default-host"))
148 (should (string-equal (file-remote-p "/method::" 'localname) ""))
149
150 ;; Expand `tramp-default-method' and `tramp-default-user'.
151 (should (string-equal
152 (file-remote-p "/host:")
153 (format "/%s:%s@%s:" "default-method" "default-user" "host")))
154 (should (string-equal (file-remote-p "/host:" 'method) "default-method"))
155 (should (string-equal (file-remote-p "/host:" 'user) "default-user"))
156 (should (string-equal (file-remote-p "/host:" 'host) "host"))
157 (should (string-equal (file-remote-p "/host:" 'localname) ""))
158
159 ;; Expand `tramp-default-method' and `tramp-default-host'.
160 (should (string-equal
161 (file-remote-p "/user@:")
162 (format "/%s:%s@%s:" "default-method""user" "default-host")))
163 (should (string-equal (file-remote-p "/user@:" 'method) "default-method"))
164 (should (string-equal (file-remote-p "/user@:" 'user) "user"))
165 (should (string-equal (file-remote-p "/user@:" 'host) "default-host"))
166 (should (string-equal (file-remote-p "/user@:" 'localname) ""))
167
168 ;; Expand `tramp-default-method'.
169 (should (string-equal
170 (file-remote-p "/user@host:")
171 (format "/%s:%s@%s:" "default-method" "user" "host")))
172 (should (string-equal
173 (file-remote-p "/user@host:" 'method) "default-method"))
174 (should (string-equal (file-remote-p "/user@host:" 'user) "user"))
175 (should (string-equal (file-remote-p "/user@host:" 'host) "host"))
176 (should (string-equal (file-remote-p "/user@host:" 'localname) ""))
177
178 ;; Expand `tramp-default-user'.
179 (should (string-equal
180 (file-remote-p "/method:host:")
181 (format "/%s:%s@%s:" "method" "default-user" "host")))
182 (should (string-equal (file-remote-p "/method:host:" 'method) "method"))
183 (should (string-equal (file-remote-p "/method:host:" 'user) "default-user"))
184 (should (string-equal (file-remote-p "/method:host:" 'host) "host"))
185 (should (string-equal (file-remote-p "/method:host:" 'localname) ""))
186
187 ;; Expand `tramp-default-host'.
188 (should (string-equal
189 (file-remote-p "/method:user@:")
190 (format "/%s:%s@%s:" "method" "user" "default-host")))
191 (should (string-equal (file-remote-p "/method:user@:" 'method) "method"))
192 (should (string-equal (file-remote-p "/method:user@:" 'user) "user"))
193 (should (string-equal (file-remote-p "/method:user@:" 'host)
194 "default-host"))
195 (should (string-equal (file-remote-p "/method:user@:" 'localname) ""))
196
197 ;; No expansion.
198 (should (string-equal
199 (file-remote-p "/method:user@host:")
200 (format "/%s:%s@%s:" "method" "user" "host")))
201 (should (string-equal
202 (file-remote-p "/method:user@host:" 'method) "method"))
203 (should (string-equal (file-remote-p "/method:user@host:" 'user) "user"))
204 (should (string-equal (file-remote-p "/method:user@host:" 'host) "host"))
205 (should (string-equal (file-remote-p "/method:user@host:" 'localname) ""))
206
207 ;; No expansion.
208 (should (string-equal
209 (file-remote-p "/method:user@email@host:")
210 (format "/%s:%s@%s:" "method" "user@email" "host")))
211 (should (string-equal
212 (file-remote-p "/method:user@email@host:" 'method) "method"))
213 (should (string-equal
214 (file-remote-p "/method:user@email@host:" 'user) "user@email"))
215 (should (string-equal
216 (file-remote-p "/method:user@email@host:" 'host) "host"))
217 (should (string-equal
218 (file-remote-p "/method:user@email@host:" 'localname) ""))
219
220 ;; Expand `tramp-default-method' and `tramp-default-user'.
221 (should (string-equal
222 (file-remote-p "/host#1234:")
223 (format "/%s:%s@%s:" "default-method" "default-user" "host#1234")))
224 (should (string-equal
225 (file-remote-p "/host#1234:" 'method) "default-method"))
226 (should (string-equal (file-remote-p "/host#1234:" 'user) "default-user"))
227 (should (string-equal (file-remote-p "/host#1234:" 'host) "host#1234"))
228 (should (string-equal (file-remote-p "/host#1234:" 'localname) ""))
229
230 ;; Expand `tramp-default-method'.
231 (should (string-equal
232 (file-remote-p "/user@host#1234:")
233 (format "/%s:%s@%s:" "default-method" "user" "host#1234")))
234 (should (string-equal
235 (file-remote-p "/user@host#1234:" 'method) "default-method"))
236 (should (string-equal (file-remote-p "/user@host#1234:" 'user) "user"))
237 (should (string-equal (file-remote-p "/user@host#1234:" 'host) "host#1234"))
238 (should (string-equal (file-remote-p "/user@host#1234:" 'localname) ""))
239
240 ;; Expand `tramp-default-user'.
241 (should (string-equal
242 (file-remote-p "/method:host#1234:")
243 (format "/%s:%s@%s:" "method" "default-user" "host#1234")))
244 (should (string-equal
245 (file-remote-p "/method:host#1234:" 'method) "method"))
246 (should (string-equal
247 (file-remote-p "/method:host#1234:" 'user) "default-user"))
248 (should (string-equal
249 (file-remote-p "/method:host#1234:" 'host) "host#1234"))
250 (should (string-equal (file-remote-p "/method:host#1234:" 'localname) ""))
251
252 ;; No expansion.
253 (should (string-equal
254 (file-remote-p "/method:user@host#1234:")
255 (format "/%s:%s@%s:" "method" "user" "host#1234")))
256 (should (string-equal
257 (file-remote-p "/method:user@host#1234:" 'method) "method"))
258 (should (string-equal
259 (file-remote-p "/method:user@host#1234:" 'user) "user"))
260 (should (string-equal
261 (file-remote-p "/method:user@host#1234:" 'host) "host#1234"))
262 (should (string-equal
263 (file-remote-p "/method:user@host#1234:" 'localname) ""))
264
265 ;; Expand `tramp-default-method' and `tramp-default-user'.
266 (should (string-equal
267 (file-remote-p "/1.2.3.4:")
268 (format "/%s:%s@%s:" "default-method" "default-user" "1.2.3.4")))
269 (should (string-equal (file-remote-p "/1.2.3.4:" 'method) "default-method"))
270 (should (string-equal (file-remote-p "/1.2.3.4:" 'user) "default-user"))
271 (should (string-equal (file-remote-p "/1.2.3.4:" 'host) "1.2.3.4"))
272 (should (string-equal (file-remote-p "/1.2.3.4:" 'localname) ""))
273
274 ;; Expand `tramp-default-method'.
275 (should (string-equal
276 (file-remote-p "/user@1.2.3.4:")
277 (format "/%s:%s@%s:" "default-method" "user" "1.2.3.4")))
278 (should (string-equal
279 (file-remote-p "/user@1.2.3.4:" 'method) "default-method"))
280 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'user) "user"))
281 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'host) "1.2.3.4"))
282 (should (string-equal (file-remote-p "/user@1.2.3.4:" 'localname) ""))
283
284 ;; Expand `tramp-default-user'.
285 (should (string-equal
286 (file-remote-p "/method:1.2.3.4:")
287 (format "/%s:%s@%s:" "method" "default-user" "1.2.3.4")))
288 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'method) "method"))
289 (should (string-equal
290 (file-remote-p "/method:1.2.3.4:" 'user) "default-user"))
291 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'host) "1.2.3.4"))
292 (should (string-equal (file-remote-p "/method:1.2.3.4:" 'localname) ""))
293
294 ;; No expansion.
295 (should (string-equal
296 (file-remote-p "/method:user@1.2.3.4:")
297 (format "/%s:%s@%s:" "method" "user" "1.2.3.4")))
298 (should (string-equal
299 (file-remote-p "/method:user@1.2.3.4:" 'method) "method"))
300 (should (string-equal (file-remote-p "/method:user@1.2.3.4:" 'user) "user"))
301 (should (string-equal
302 (file-remote-p "/method:user@1.2.3.4:" 'host) "1.2.3.4"))
303 (should (string-equal
304 (file-remote-p "/method:user@1.2.3.4:" 'localname) ""))
305
306 ;; This does not work. Why?
307 ;(should (file-remote-p "/[]:"))
308
309 ;; Expand `tramp-default-method' and `tramp-default-user'.
310 (should (string-equal
311 (file-remote-p "/[::1]:")
312 (format "/%s:%s@%s:" "default-method" "default-user" "[::1]")))
313 (should (string-equal (file-remote-p "/[::1]:" 'method) "default-method"))
314 (should (string-equal (file-remote-p "/[::1]:" 'user) "default-user"))
315 (should (string-equal (file-remote-p "/[::1]:" 'host) "::1"))
316 (should (string-equal (file-remote-p "/[::1]:" 'localname) ""))
317
318 ;; Expand `tramp-default-method'.
319 (should (string-equal
320 (file-remote-p "/user@[::1]:")
321 (format "/%s:%s@%s:" "default-method" "user" "[::1]")))
322 (should (string-equal
323 (file-remote-p "/user@[::1]:" 'method) "default-method"))
324 (should (string-equal (file-remote-p "/user@[::1]:" 'user) "user"))
325 (should (string-equal (file-remote-p "/user@[::1]:" 'host) "::1"))
326 (should (string-equal (file-remote-p "/user@[::1]:" 'localname) ""))
327
328 ;; Expand `tramp-default-user'.
329 (should (string-equal
330 (file-remote-p "/method:[::1]:")
331 (format "/%s:%s@%s:" "method" "default-user" "[::1]")))
332 (should (string-equal (file-remote-p "/method:[::1]:" 'method) "method"))
333 (should (string-equal
334 (file-remote-p "/method:[::1]:" 'user) "default-user"))
335 (should (string-equal (file-remote-p "/method:[::1]:" 'host) "::1"))
336 (should (string-equal (file-remote-p "/method:[::1]:" 'localname) ""))
337
338 ;; No expansion.
339 (should (string-equal
340 (file-remote-p "/method:user@[::1]:")
341 (format "/%s:%s@%s:" "method" "user" "[::1]")))
342 (should (string-equal
343 (file-remote-p "/method:user@[::1]:" 'method) "method"))
344 (should (string-equal (file-remote-p "/method:user@[::1]:" 'user) "user"))
345 (should (string-equal (file-remote-p "/method:user@[::1]:" 'host) "::1"))
346 (should (string-equal
347 (file-remote-p "/method:user@[::1]:" 'localname) ""))
348
349 ;; Local file name part.
350 (should (string-equal (file-remote-p "/host:/:" 'localname) "/:"))
351 (should (string-equal (file-remote-p "/method:::" 'localname) ":"))
352 (should (string-equal (file-remote-p "/method:: " 'localname) " "))
353 (should (string-equal (file-remote-p "/method::file" 'localname) "file"))
354 (should (string-equal
355 (file-remote-p "/method::/path/to/file" 'localname)
356 "/path/to/file"))
357
358 ;; Multihop.
359 (should
360 (string-equal
361 (file-remote-p "/method1:user1@host1|method2:user2@host2:/path/to/file")
362 (format "/%s:%s@%s:" "method2" "user2" "host2")))
363 (should
364 (string-equal
365 (file-remote-p
366 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'method)
367 "method2"))
368 (should
369 (string-equal
370 (file-remote-p
371 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'user)
372 "user2"))
373 (should
374 (string-equal
375 (file-remote-p
376 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'host)
377 "host2"))
378 (should
379 (string-equal
380 (file-remote-p
381 "/method1:user1@host1|method2:user2@host2:/path/to/file" 'localname)
382 "/path/to/file"))
383
384 (should
385 (string-equal
386 (file-remote-p
387 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file")
388 (format "/%s:%s@%s:" "method3" "user3" "host3")))
389 (should
390 (string-equal
391 (file-remote-p
392 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
393 'method)
394 "method3"))
395 (should
396 (string-equal
397 (file-remote-p
398 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
399 'user)
400 "user3"))
401 (should
402 (string-equal
403 (file-remote-p
404 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
405 'host)
406 "host3"))
407 (should
408 (string-equal
409 (file-remote-p
410 "/method1:user1@host1|method2:user2@host2|method3:user3@host3:/path/to/file"
411 'localname)
412 "/path/to/file"))))
413
414 (ert-deftest tramp-test03-file-name-defaults ()
415 "Check default values for some methods."
416 ;; Default values in tramp-adb.el.
417 (should (string-equal (file-remote-p "/adb::" 'host) ""))
418 ;; Default values in tramp-ftp.el.
419 (should (string-equal (file-remote-p "/ftp.host:" 'method) "ftp"))
420 (dolist (u '("ftp" "anonymous"))
421 (should (string-equal (file-remote-p (format "/%s@:" u) 'method) "ftp")))
422 ;; Default values in tramp-gvfs.el.
423 ;(should (string-equal (file-remote-p "/synce::" 'user) nil))
424 ;; Default values in tramp-gw.el.
425 (dolist (m '("tunnel" "socks"))
426 (should (string-equal (file-remote-p (format "/%s::" m) 'user)
427 (user-login-name))))
428 ;; Default values in tramp-sh.el.
429 (dolist (h `("127.0.0.1" "[::1]" "localhost" "localhost6" ,(system-name)))
430 (should (string-equal (file-remote-p (format "/root@%s:" h) 'method) "su")))
431 (dolist (m '("su" "sudo" "ksu"))
432 (should (string-equal (file-remote-p (format "/%s::" m) 'user) "root")))
433 (dolist (m '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
434 (should (string-equal (file-remote-p (format "/%s::" m) 'user)
435 (user-login-name))))
436 ;; Default values in tramp-smb.el.
437 (should (string-equal (file-remote-p "/user%domain@host:" 'method) "smb"))
438 (should (string-equal (file-remote-p "/smb::" 'user) nil)))
439
440 (ert-deftest tramp-test04-substitute-in-file-name ()
441 "Check `substitute-in-file-name'."
442 (should (string-equal (substitute-in-file-name "/method:host://foo") "/foo"))
443 (should (string-equal
444 (substitute-in-file-name "/method:host:/path//foo")
445 "/method:host:/foo"))
446 (should (string-equal
447 (substitute-in-file-name "/method:host:/path///foo") "/foo"))
448 (should (string-equal
449 (substitute-in-file-name "/method:host:/path/~/foo")
450 "/method:host:~/foo"))
451 (should (string-equal
452 (substitute-in-file-name "/method:host:/path//~/foo") "~/foo"))
453 (let (process-environment)
454 (should
455 (string-equal (substitute-in-file-name "/method:host:/path/$FOO")
456 "/method:host:/path/$FOO"))
457 (setenv "FOO" "bla")
458 (should (string-equal
459 (substitute-in-file-name "/method:host:/path/$FOO")
460 "/method:host:/path/bla"))
461 (should (string-equal
462 (substitute-in-file-name "/method:host:/path/$$FOO")
463 "/method:host:/path/$FOO"))))
464
465 (ert-deftest tramp-test05-expand-file-name ()
466 "Check `expand-file-name'."
467 (should (string-equal
468 (expand-file-name "/method:host:/path/./file")
469 "/method:host:/path/file"))
470 (should (string-equal
471 (expand-file-name "/method:host:/path/../file")
472 "/method:host:/file")))
473
474 (ert-deftest tramp-test06-directory-file-name ()
475 "Check `directory-file-name'.
476 This checks also `file-name-as-directory', `file-name-directory'
477 and `file-name-nondirectory'."
478 (should (string-equal
479 (directory-file-name "/method:host:/path/to/file")
480 "/method:host:/path/to/file"))
481 (should (string-equal
482 (directory-file-name "/method:host:/path/to/file/")
483 "/method:host:/path/to/file"))
484 (should (string-equal
485 (file-name-as-directory "/method:host:/path/to/file")
486 "/method:host:/path/to/file/"))
487 (should (string-equal
488 (file-name-as-directory "/method:host:/path/to/file/")
489 "/method:host:/path/to/file/"))
490 (should (string-equal
491 (file-name-directory "/method:host:/path/to/file")
492 "/method:host:/path/to/"))
493 (should (string-equal
494 (file-name-directory "/method:host:/path/to/file/")
495 "/method:host:/path/to/file/"))
496 (should (string-equal
497 (file-name-nondirectory "/method:host:/path/to/file") "file"))
498 (should (string-equal
499 (file-name-nondirectory "/method:host:/path/to/file/") ""))
500 (should-not (file-remote-p
501 (unhandled-file-name-directory "/method:host:/path/to/file"))))
502
503 (ert-deftest tramp-test07-file-exists-p ()
504 "Check `file-exist-p'.
505 Implicitely, this checks also `write-region' and `delete-file'."
506 (skip-unless (tramp--test-enabled))
507 (let ((tmp-name (tramp--test-make-temp-name)))
508 (should-not (file-exists-p tmp-name))
509 (write-region "foo" nil tmp-name)
510 (should (file-exists-p tmp-name))
511 (delete-file tmp-name)
512 (should-not (file-exists-p tmp-name))))
513
514 (ert-deftest tramp-test08-file-local-copy ()
515 "Check `file-local-copy'."
516 (skip-unless (tramp--test-enabled))
517 (let ((tmp-name1 (tramp--test-make-temp-name))
518 tmp-name2)
519 (unwind-protect
520 (progn
521 (write-region "foo" nil tmp-name1)
522 (should (setq tmp-name2 (file-local-copy tmp-name1)))
523 (with-temp-buffer
524 (insert-file-contents tmp-name2)
525 (should (string-equal (buffer-string) "foo"))))
526 (ignore-errors
527 (delete-file tmp-name1)
528 (delete-file tmp-name2)))))
529
530 (ert-deftest tramp-test09-insert-file-contents ()
531 "Check `insert-file-contents'."
532 (skip-unless (tramp--test-enabled))
533 (let ((tmp-name (tramp--test-make-temp-name)))
534 (unwind-protect
535 (progn
536 (write-region "foo" nil tmp-name)
537 (with-temp-buffer
538 (insert-file-contents tmp-name)
539 (should (string-equal (buffer-string) "foo"))))
540 (ignore-errors (delete-file tmp-name)))))
541
542 (ert-deftest tramp-test10-write-region ()
543 "Check `write-region'."
544 (skip-unless (tramp--test-enabled))
545 (let ((tmp-name (tramp--test-make-temp-name)))
546 (unwind-protect
547 (progn
548 (with-temp-buffer
549 (insert "foo")
550 (write-region nil nil tmp-name))
551 (with-temp-buffer
552 (insert-file-contents tmp-name)
553 (should (string-equal (buffer-string) "foo"))))
554 (ignore-errors (delete-file tmp-name)))))
555
556 (ert-deftest tramp-test11-copy-file ()
557 "Check `copy-file'."
558 (skip-unless (tramp--test-enabled))
559 (let ((tmp-name1 (tramp--test-make-temp-name))
560 (tmp-name2 (tramp--test-make-temp-name)))
561 (message "%s %s" tmp-name1 tmp-name2)
562 (unwind-protect
563 (progn
564 (write-region "foo" nil tmp-name1)
565 (copy-file tmp-name1 tmp-name2)
566 (should (file-exists-p tmp-name2))
567 (with-temp-buffer
568 (insert-file-contents tmp-name2)
569 (should (string-equal (buffer-string) "foo"))))
570 (ignore-errors
571 (delete-file tmp-name1)
572 (delete-file tmp-name2)))))
573
574 (ert-deftest tramp-test12-rename-file ()
575 "Check `rename-file'."
576 (skip-unless (tramp--test-enabled))
577 (let ((tmp-name1 (tramp--test-make-temp-name))
578 (tmp-name2 (tramp--test-make-temp-name)))
579 (unwind-protect
580 (progn
581 (write-region "foo" nil tmp-name1)
582 (rename-file tmp-name1 tmp-name2)
583 (should-not (file-exists-p tmp-name1))
584 (should (file-exists-p tmp-name2))
585 (with-temp-buffer
586 (insert-file-contents tmp-name2)
587 (should (string-equal (buffer-string) "foo"))))
588 (ignore-errors (delete-file tmp-name2)))))
589
590 (ert-deftest tramp-test13-make-directory ()
591 "Check `make-directory'.
592 This tests also `file-directory-p' and `file-accessible-directory-p'."
593 (skip-unless (tramp--test-enabled))
594 (let ((tmp-name (tramp--test-make-temp-name)))
595 (unwind-protect
596 (progn
597 (make-directory tmp-name)
598 (should (file-directory-p tmp-name))
599 (should (file-accessible-directory-p tmp-name)))
600 (ignore-errors (delete-directory tmp-name)))))
601
602 (ert-deftest tramp-test14-delete-directory ()
603 "Check `delete-directory'."
604 (skip-unless (tramp--test-enabled))
605 (let ((tmp-name (tramp--test-make-temp-name)))
606 ;; Delete empty directory.
607 (make-directory tmp-name)
608 (should (file-directory-p tmp-name))
609 (delete-directory tmp-name)
610 (should-not (file-directory-p tmp-name))
611 ;; Delete non-empty directory.
612 (make-directory tmp-name)
613 (write-region "foo" nil (expand-file-name "bla" tmp-name))
614 (should-error (delete-directory tmp-name))
615 (delete-directory tmp-name 'recursive)
616 (should-not (file-directory-p tmp-name))))
617
618 (ert-deftest tramp-test15-copy-directory ()
619 "Check `copy-directory'."
620 (skip-unless (tramp--test-enabled))
621 (let* ((tmp-name1 (tramp--test-make-temp-name))
622 (tmp-name2 (tramp--test-make-temp-name))
623 (tmp-name3 (expand-file-name
624 (file-name-nondirectory tmp-name1) tmp-name2))
625 (tmp-name4 (expand-file-name "foo" tmp-name1))
626 (tmp-name5 (expand-file-name "foo" tmp-name2))
627 (tmp-name6 (expand-file-name "foo" tmp-name3)))
628 (unwind-protect
629 (progn
630 ;; Copy empty directory.
631 (make-directory tmp-name1)
632 (write-region "foo" nil tmp-name4)
633 (should (file-directory-p tmp-name1))
634 (should (file-exists-p tmp-name4))
635 (copy-directory tmp-name1 tmp-name2)
636 (should (file-directory-p tmp-name2))
637 (should (file-exists-p tmp-name5))
638 ;; Target directory does exist already.
639 (copy-directory tmp-name1 tmp-name2)
640 (should (file-directory-p tmp-name3))
641 (should (file-exists-p tmp-name6)))
642 (delete-directory tmp-name1 'recursive)
643 (delete-directory tmp-name2 'recursive))))
644
645 (ert-deftest tramp-test16-directory-files ()
646 "Check `directory-files'."
647 (skip-unless (tramp--test-enabled))
648 (let* ((tmp-name1 (tramp--test-make-temp-name))
649 (tmp-name2 (expand-file-name "bla" tmp-name1))
650 (tmp-name3 (expand-file-name "foo" tmp-name1)))
651 (unwind-protect
652 (progn
653 (make-directory tmp-name1)
654 (write-region "foo" nil tmp-name2)
655 (write-region "bla" nil tmp-name3)
656 (should (file-directory-p tmp-name1))
657 (should (file-exists-p tmp-name2))
658 (should (file-exists-p tmp-name3))
659 (should (equal (directory-files tmp-name1) '("." ".." "bla" "foo")))
660 (should (equal (directory-files tmp-name1 'full)
661 `(,(concat tmp-name1 "/.")
662 ,(concat tmp-name1 "/..")
663 ,tmp-name2 ,tmp-name3)))
664 (should (equal (directory-files
665 tmp-name1 nil directory-files-no-dot-files-regexp)
666 '("bla" "foo")))
667 (should (equal (directory-files
668 tmp-name1 'full directory-files-no-dot-files-regexp)
669 `(,tmp-name2 ,tmp-name3))))
670 (delete-directory tmp-name1 'recursive))))
671
672 (ert-deftest tramp-test17-insert-directory ()
673 "Check `insert-directory'."
674 (skip-unless (tramp--test-enabled))
675 (let* ((tmp-name1 (tramp--test-make-temp-name))
676 (tmp-name2 (expand-file-name "foo" tmp-name1)))
677 (unwind-protect
678 (progn
679 (make-directory tmp-name1)
680 (write-region "foo" nil tmp-name2)
681 (should (file-directory-p tmp-name1))
682 (should (file-exists-p tmp-name2))
683 (with-temp-buffer
684 (insert-directory tmp-name1 nil)
685 (goto-char (point-min))
686 (should (looking-at-p (regexp-quote tmp-name1))))
687 (with-temp-buffer
688 (insert-directory tmp-name1 "-al")
689 (goto-char (point-min))
690 (should (looking-at-p (format "^.+ %s$" (regexp-quote tmp-name1)))))
691 (with-temp-buffer
692 (insert-directory (file-name-as-directory tmp-name1) "-al")
693 (goto-char (point-min))
694 (should
695 (looking-at-p (format "^.+ %s/$" (regexp-quote tmp-name1)))))
696 (with-temp-buffer
697 (insert-directory
698 (file-name-as-directory tmp-name1) "-al" nil 'full-directory-p)
699 (goto-char (point-min))
700 (should
701 (looking-at-p "total +[[:digit:]]+\n.+ \\.\n.+ \\.\\.\n.+ foo$"))))
702 (delete-directory tmp-name1 'recursive))))
703
704 (ert-deftest tramp-test18-file-attributes ()
705 "Check `file-attributes'.
706 This tests also `file-readable-p' and `file-regular-p'."
707 (skip-unless (tramp--test-enabled))
708 (let ((tmp-name (tramp--test-make-temp-name))
709 attr)
710 (unwind-protect
711 (progn
712 (write-region "foo" nil tmp-name)
713 (should (file-exists-p tmp-name))
714 (setq attr (file-attributes tmp-name))
715 (should (consp attr))
716 (should (file-exists-p tmp-name))
717 (should (file-readable-p tmp-name))
718 (should (file-regular-p tmp-name))
719 ;; We do not test inodes and device numbers.
720 (should (null (car attr)))
721 (should (numberp (nth 1 attr))) ;; Link.
722 (should (numberp (nth 2 attr))) ;; Uid.
723 (should (numberp (nth 3 attr))) ;; Gid.
724 ;; Last access time.
725 (should (stringp (current-time-string (nth 4 attr))))
726 ;; Last modification time.
727 (should (stringp (current-time-string (nth 5 attr))))
728 ;; Last status change time.
729 (should (stringp (current-time-string (nth 6 attr))))
730 (should (numberp (nth 7 attr))) ;; Size.
731 (should (stringp (nth 8 attr))) ;; Modes.
732
733 (setq attr (file-attributes tmp-name 'string))
734 (should (stringp (nth 2 attr))) ;; Uid.
735 (should (stringp (nth 3 attr))) ;; Gid.
736 (delete-file tmp-name)
737
738 (make-directory tmp-name)
739 (should (file-exists-p tmp-name))
740 (should (file-readable-p tmp-name))
741 (should-not (file-regular-p tmp-name))
742 (setq attr (file-attributes tmp-name))
743 (should (eq (car attr) t)))
744 (delete-directory tmp-name))))
745
746 (ert-deftest tramp-test19-directory-files-and-attributes ()
747 "Check `directory-files-and-attributes'."
748 (skip-unless (tramp--test-enabled))
749 (let ((tmp-name (tramp--test-make-temp-name))
750 attr)
751 (unwind-protect
752 (progn
753 (make-directory tmp-name)
754 (should (file-directory-p tmp-name))
755 (write-region "foo" nil (expand-file-name "foo" tmp-name))
756 (write-region "bar" nil (expand-file-name "bar" tmp-name))
757 (write-region "boz" nil (expand-file-name "boz" tmp-name))
758 (setq attr (directory-files-and-attributes tmp-name))
759 (should (consp attr))
760 (dolist (elt attr)
761 (should
762 (equal (file-attributes (expand-file-name (car elt) tmp-name))
763 (cdr elt))))
764 (setq attr (directory-files-and-attributes tmp-name 'full))
765 (dolist (elt attr)
766 (should
767 (equal (file-attributes (car elt)) (cdr elt))))
768 (setq attr (directory-files-and-attributes tmp-name nil "^b"))
769 (should (equal (mapcar 'car attr) '("bar" "boz"))))
770 (delete-directory tmp-name 'recursive))))
771
772 (ert-deftest tramp-test20-file-modes ()
773 "Check `file-modes'.
774 This tests also `file-executable-p', `file-writable-p' and `set-file-modes'."
775 (skip-unless (tramp--test-enabled))
776 (let ((tmp-name1 (tramp--test-make-temp-name))
777 (tmp-name2 (tramp--test-make-temp-name)))
778 (unwind-protect
779 (progn
780 (write-region "foo" nil tmp-name1)
781 (should (file-exists-p tmp-name1))
782 (set-file-modes tmp-name1 #o777)
783 (should (= (file-modes tmp-name1) #o777))
784 (should (file-executable-p tmp-name1))
785 (should (file-writable-p tmp-name1))
786 (set-file-modes tmp-name1 #o444)
787 (should (= (file-modes tmp-name1) #o444))
788 (should-not (file-executable-p tmp-name1))
789 (should-not (file-writable-p tmp-name1)))
790 (delete-file tmp-name1)
791 (delete-file tmp-name2))))
792
793 (ert-deftest tramp-test21-file-links ()
794 "Check `file-symlink-p'.
795 This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
796 (skip-unless (tramp--test-enabled))
797 (let ((tmp-name1 (tramp--test-make-temp-name))
798 (tmp-name2 (tramp--test-make-temp-name))
799 (tmp-name3 (make-temp-name "tramp-")))
800 (unwind-protect
801 (progn
802 (write-region "foo" nil tmp-name1)
803 (should (file-exists-p tmp-name1))
804 (make-symbolic-link tmp-name1 tmp-name2)
805 (should (file-symlink-p tmp-name2))
806 (should-error (make-symbolic-link tmp-name1 tmp-name2))
807 (make-symbolic-link tmp-name1 tmp-name2 'ok-if-already-exists)
808 (should (file-symlink-p tmp-name2))
809 ;; `tmp-name3' is a local file name.
810 (should-error (make-symbolic-link tmp-name1 tmp-name3)))
811 (delete-file tmp-name1)
812 (delete-file tmp-name2))
813
814 (unwind-protect
815 (progn
816 (write-region "foo" nil tmp-name1)
817 (should (file-exists-p tmp-name1))
818 (add-name-to-file tmp-name1 tmp-name2)
819 (should-not (file-symlink-p tmp-name2))
820 (should-error (add-name-to-file tmp-name1 tmp-name2))
821 (add-name-to-file tmp-name1 tmp-name2 'ok-if-already-exists)
822 (should-not (file-symlink-p tmp-name2))
823 ;; `tmp-name3' is a local file name.
824 (should-error (add-name-to-file tmp-name1 tmp-name3)))
825 (delete-file tmp-name1)
826 (delete-file tmp-name2))
827
828 (unwind-protect
829 (progn
830 (write-region "foo" nil tmp-name1)
831 (should (file-exists-p tmp-name1))
832 (make-symbolic-link tmp-name1 tmp-name2)
833 (should (file-symlink-p tmp-name2))
834 (should (string-equal (file-truename tmp-name2) tmp-name1)))
835 (delete-file tmp-name1)
836 (delete-file tmp-name2))))
837
838 (ert-deftest tramp-test22-file-times ()
839 "Check `set-file-times' and `file-newer-than-file-p'."
840 (skip-unless (tramp--test-enabled))
841 (let ((tmp-name1 (tramp--test-make-temp-name))
842 (tmp-name2 (tramp--test-make-temp-name))
843 (tmp-name3 (tramp--test-make-temp-name)))
844 (unwind-protect
845 (progn
846 (write-region "foo" nil tmp-name1)
847 (should (file-exists-p tmp-name1))
848 (should (consp (nth 5 (file-attributes tmp-name1))))
849 ;; '(0 0) means don't know, and will be replaced by `current-time'.
850 (set-file-times tmp-name1 '(0 1))
851 (should (equal (nth 5 (file-attributes tmp-name1)) '(0 1)))
852 (write-region "bla" nil tmp-name2)
853 (should (file-exists-p tmp-name2))
854 (should (file-newer-than-file-p tmp-name2 tmp-name1))
855 ;; `tmp-name3' does not exist.
856 (should (file-newer-than-file-p tmp-name2 tmp-name3))
857 (should-not (file-newer-than-file-p tmp-name3 tmp-name1)))
858 (delete-file tmp-name1)
859 (delete-file tmp-name2))))
860
861 (ert-deftest tramp-test23-visited-file-modtime ()
862 "Check `set-visited-file-modtime' and `verify-visited-file-modtime'."
863 (skip-unless (tramp--test-enabled))
864 (let ((tmp-name (tramp--test-make-temp-name)))
865 (unwind-protect
866 (progn
867 (write-region "foo" nil tmp-name)
868 (should (file-exists-p tmp-name))
869 (with-temp-buffer
870 (insert-file-contents tmp-name)
871 (should (verify-visited-file-modtime))
872 (set-visited-file-modtime '(0 1))
873 (should (verify-visited-file-modtime))
874 (should (equal (visited-file-modtime) '(0 1 0 0)))))
875 (delete-file tmp-name))))
876
877 (ert-deftest tramp-test24-file-name-completion ()
878 "Check `file-name-completion' and `file-name-all-completions'."
879 (skip-unless (tramp--test-enabled))
880 (let ((tmp-name (tramp--test-make-temp-name)))
881 (unwind-protect
882 (progn
883 (make-directory tmp-name)
884 (should (file-directory-p tmp-name))
885 (write-region "foo" nil (expand-file-name "foo" tmp-name))
886 (write-region "bar" nil (expand-file-name "bold" tmp-name))
887 (make-directory (expand-file-name "boz" tmp-name))
888 (should (equal (file-name-completion "fo" tmp-name) "foo"))
889 (should (equal (file-name-completion "b" tmp-name) "bo"))
890 (should
891 (equal (file-name-completion "b" tmp-name 'file-directory-p) "boz/"))
892 (should (equal (file-name-all-completions "fo" tmp-name) '("foo")))
893 (should
894 (equal (sort (file-name-all-completions "b" tmp-name) 'string-lessp)
895 '("bold" "boz/"))))
896 (delete-directory tmp-name 'recursive))))
897
898 (ert-deftest tramp-test25-load ()
899 "Check `load'."
900 (skip-unless (tramp--test-enabled))
901 (let ((tmp-name (tramp--test-make-temp-name)))
902 (unwind-protect
903 (progn
904 (load tmp-name 'noerror 'nomessage)
905 (should-not (featurep 'tramp-test-load))
906 (write-region "(provide 'tramp-test-load)" nil tmp-name)
907 ;; `load' in lread.c does not pass `must-suffix'. Why?
908 ;(should-error (load tmp-name nil 'nomessage 'nosuffix 'must-suffix))
909 (load tmp-name nil 'nomessage 'nosuffix)
910 (should (featurep 'tramp-test-load)))
911 (and (featurep 'tramp-test-load) (unload-feature 'tramp-test-load))
912 (delete-file tmp-name))))
913
914 (ert-deftest tramp-test26-process-file ()
915 "Check `process-file'."
916 (skip-unless (tramp--test-enabled))
917 (let ((default-directory tramp-test-temporary-file-directory))
918 (should (zerop (process-file "/bin/true")))
919 (should-not (zerop (process-file "/bin/false")))
920 (with-temp-buffer
921 (should (zerop (process-file "ls" nil t)))
922 (should (> (point-max) (point-min))))))
923
924 (ert-deftest tramp-test27-start-file-process ()
925 "Check `start-file-process'."
926 (skip-unless (tramp--test-enabled))
927 (let ((default-directory tramp-test-temporary-file-directory)
928 (tmp-name (tramp--test-make-temp-name))
929 kill-buffer-query-functions proc)
930 (unwind-protect
931 (with-temp-buffer
932 (setq proc (start-file-process "test1" (current-buffer) "cat"))
933 (should (processp proc))
934 (should (equal (process-status proc) 'run))
935 (process-send-string proc "foo")
936 (process-send-eof proc)
937 (accept-process-output proc 1)
938 (should (string-equal (buffer-string) "foo")))
939 (delete-process proc))
940
941 (unwind-protect
942 (with-temp-buffer
943 (write-region "foo" nil tmp-name)
944 (should (file-exists-p tmp-name))
945 (setq proc
946 (start-file-process
947 "test2" (current-buffer)
948 "cat" (file-name-nondirectory tmp-name)))
949 (should (processp proc))
950 (accept-process-output proc 1)
951 (should (string-equal (buffer-string) "foo")))
952 (delete-process proc)
953 (delete-file tmp-name))
954
955 (unwind-protect
956 (progn
957 (setq proc (start-file-process "test3" nil "cat"))
958 (should (processp proc))
959 (should (equal (process-status proc) 'run))
960 (set-process-filter
961 proc (lambda (p s) (should (string-equal s "foo"))))
962 (process-send-string proc "foo")
963 (process-send-eof proc)
964 (accept-process-output proc 1))
965 (delete-process proc))))
966
967 (ert-deftest tramp-test28-shell-command ()
968 "Check `shell-command'."
969 (skip-unless (tramp--test-enabled))
970 (let ((default-directory tramp-test-temporary-file-directory))
971 (with-temp-buffer
972 (shell-command "ls" (current-buffer))
973 (should (> (point-max) (point-min))))))
974
975 ;; TODO:
976
977 ;; * dired-compress-file
978 ;; * dired-uncache
979 ;; * file-acl
980 ;; * file-ownership-preserved-p
981 ;; * file-selinux-context
982 ;; * find-backup-file-name
983 ;; * make-auto-save-file-name
984 ;; * set-file-acl
985 ;; * set-file-selinux-context
986 ;; * vc-registered
987
988 (defun tramp-test-all (&optional interactive)
989 "Run all tests for \\[tramp]."
990 (interactive "p")
991 (funcall
992 (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^tramp"))
993
994 (provide 'tramp-tests)
995 ;;; tramp-tests.el ends here