1 extern crate filetime;
2 extern crate tar;
3 extern crate tempfile;
4 #[cfg(all(unix, feature = "xattr"))]
5 extern crate xattr;
6 
7 use std::fs::{self, File};
8 use std::io::prelude::*;
9 use std::io::{self, Cursor};
10 use std::iter::repeat;
11 use std::path::{Path, PathBuf};
12 
13 use filetime::FileTime;
14 use tar::{Archive, Builder, EntryType, Header, HeaderMode};
15 use tempfile::{Builder as TempBuilder, TempDir};
16 
17 macro_rules! t {
18     ($e:expr) => {
19         match $e {
20             Ok(v) => v,
21             Err(e) => panic!("{} returned {}", stringify!($e), e),
22         }
23     };
24 }
25 
26 macro_rules! tar {
27     ($e:expr) => {
28         &include_bytes!(concat!("archives/", $e))[..]
29     };
30 }
31 
32 mod header;
33 
34 /// test that we can concatenate the simple.tar archive and extract the same entries twice when we
35 /// use the ignore_zeros option.
36 #[test]
simple_concat()37 fn simple_concat() {
38     let bytes = tar!("simple.tar");
39     let mut archive_bytes = Vec::new();
40     archive_bytes.extend(bytes);
41 
42     let original_names: Vec<String> = decode_names(&mut Archive::new(Cursor::new(&archive_bytes)));
43     let expected: Vec<&str> = original_names.iter().map(|n| n.as_str()).collect();
44 
45     // concat two archives (with null in-between);
46     archive_bytes.extend(bytes);
47 
48     // test now that when we read the archive, it stops processing at the first zero header.
49     let actual = decode_names(&mut Archive::new(Cursor::new(&archive_bytes)));
50     assert_eq!(expected, actual);
51 
52     // extend expected by itself.
53     let expected: Vec<&str> = {
54         let mut o = Vec::new();
55         o.extend(&expected);
56         o.extend(&expected);
57         o
58     };
59 
60     let mut ar = Archive::new(Cursor::new(&archive_bytes));
61     ar.set_ignore_zeros(true);
62 
63     let actual = decode_names(&mut ar);
64     assert_eq!(expected, actual);
65 
66     fn decode_names<R>(ar: &mut Archive<R>) -> Vec<String>
67     where
68         R: Read,
69     {
70         let mut names = Vec::new();
71 
72         for entry in t!(ar.entries()) {
73             let e = t!(entry);
74             names.push(t!(::std::str::from_utf8(&e.path_bytes())).to_string());
75         }
76 
77         names
78     }
79 }
80 
81 #[test]
header_impls()82 fn header_impls() {
83     let mut ar = Archive::new(Cursor::new(tar!("simple.tar")));
84     let hn = Header::new_old();
85     let hnb = hn.as_bytes();
86     for file in t!(ar.entries()) {
87         let file = t!(file);
88         let h1 = file.header();
89         let h1b = h1.as_bytes();
90         let h2 = h1.clone();
91         let h2b = h2.as_bytes();
92         assert!(h1b[..] == h2b[..] && h2b[..] != hnb[..])
93     }
94 }
95 
96 #[test]
header_impls_missing_last_header()97 fn header_impls_missing_last_header() {
98     let mut ar = Archive::new(Cursor::new(tar!("simple_missing_last_header.tar")));
99     let hn = Header::new_old();
100     let hnb = hn.as_bytes();
101     for file in t!(ar.entries()) {
102         let file = t!(file);
103         let h1 = file.header();
104         let h1b = h1.as_bytes();
105         let h2 = h1.clone();
106         let h2b = h2.as_bytes();
107         assert!(h1b[..] == h2b[..] && h2b[..] != hnb[..])
108     }
109 }
110 
111 #[test]
reading_files()112 fn reading_files() {
113     let rdr = Cursor::new(tar!("reading_files.tar"));
114     let mut ar = Archive::new(rdr);
115     let mut entries = t!(ar.entries());
116 
117     let mut a = t!(entries.next().unwrap());
118     assert_eq!(&*a.header().path_bytes(), b"a");
119     let mut s = String::new();
120     t!(a.read_to_string(&mut s));
121     assert_eq!(s, "a\na\na\na\na\na\na\na\na\na\na\n");
122 
123     let mut b = t!(entries.next().unwrap());
124     assert_eq!(&*b.header().path_bytes(), b"b");
125     s.truncate(0);
126     t!(b.read_to_string(&mut s));
127     assert_eq!(s, "b\nb\nb\nb\nb\nb\nb\nb\nb\nb\nb\n");
128 
129     assert!(entries.next().is_none());
130 }
131 
132 #[test]
writing_files()133 fn writing_files() {
134     let mut ar = Builder::new(Vec::new());
135     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
136 
137     let path = td.path().join("test");
138     t!(t!(File::create(&path)).write_all(b"test"));
139 
140     t!(ar.append_file("test2", &mut t!(File::open(&path))));
141 
142     let data = t!(ar.into_inner());
143     let mut ar = Archive::new(Cursor::new(data));
144     let mut entries = t!(ar.entries());
145     let mut f = t!(entries.next().unwrap());
146 
147     assert_eq!(&*f.header().path_bytes(), b"test2");
148     assert_eq!(f.header().size().unwrap(), 4);
149     let mut s = String::new();
150     t!(f.read_to_string(&mut s));
151     assert_eq!(s, "test");
152 
153     assert!(entries.next().is_none());
154 }
155 
156 #[test]
large_filename()157 fn large_filename() {
158     let mut ar = Builder::new(Vec::new());
159     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
160 
161     let path = td.path().join("test");
162     t!(t!(File::create(&path)).write_all(b"test"));
163 
164     let filename = repeat("abcd/").take(50).collect::<String>();
165     let mut header = Header::new_ustar();
166     header.set_path(&filename).unwrap();
167     header.set_metadata(&t!(fs::metadata(&path)));
168     header.set_cksum();
169     t!(ar.append(&header, &b"test"[..]));
170     let too_long = repeat("abcd").take(200).collect::<String>();
171     t!(ar.append_file(&too_long, &mut t!(File::open(&path))));
172     t!(ar.append_data(&mut header, &too_long, &b"test"[..]));
173 
174     let rd = Cursor::new(t!(ar.into_inner()));
175     let mut ar = Archive::new(rd);
176     let mut entries = t!(ar.entries());
177 
178     // The short entry added with `append`
179     let mut f = entries.next().unwrap().unwrap();
180     assert_eq!(&*f.header().path_bytes(), filename.as_bytes());
181     assert_eq!(f.header().size().unwrap(), 4);
182     let mut s = String::new();
183     t!(f.read_to_string(&mut s));
184     assert_eq!(s, "test");
185 
186     // The long entry added with `append_file`
187     let mut f = entries.next().unwrap().unwrap();
188     assert_eq!(&*f.path_bytes(), too_long.as_bytes());
189     assert_eq!(f.header().size().unwrap(), 4);
190     let mut s = String::new();
191     t!(f.read_to_string(&mut s));
192     assert_eq!(s, "test");
193 
194     // The long entry added with `append_data`
195     let mut f = entries.next().unwrap().unwrap();
196     assert!(f.header().path_bytes().len() < too_long.len());
197     assert_eq!(&*f.path_bytes(), too_long.as_bytes());
198     assert_eq!(f.header().size().unwrap(), 4);
199     let mut s = String::new();
200     t!(f.read_to_string(&mut s));
201     assert_eq!(s, "test");
202 
203     assert!(entries.next().is_none());
204 }
205 
206 #[test]
reading_entries()207 fn reading_entries() {
208     let rdr = Cursor::new(tar!("reading_files.tar"));
209     let mut ar = Archive::new(rdr);
210     let mut entries = t!(ar.entries());
211     let mut a = t!(entries.next().unwrap());
212     assert_eq!(&*a.header().path_bytes(), b"a");
213     let mut s = String::new();
214     t!(a.read_to_string(&mut s));
215     assert_eq!(s, "a\na\na\na\na\na\na\na\na\na\na\n");
216     s.truncate(0);
217     t!(a.read_to_string(&mut s));
218     assert_eq!(s, "");
219     let mut b = t!(entries.next().unwrap());
220 
221     assert_eq!(&*b.header().path_bytes(), b"b");
222     s.truncate(0);
223     t!(b.read_to_string(&mut s));
224     assert_eq!(s, "b\nb\nb\nb\nb\nb\nb\nb\nb\nb\nb\n");
225     assert!(entries.next().is_none());
226 }
227 
check_dirtree(td: &TempDir)228 fn check_dirtree(td: &TempDir) {
229     let dir_a = td.path().join("a");
230     let dir_b = td.path().join("a/b");
231     let file_c = td.path().join("a/c");
232     assert!(fs::metadata(&dir_a).map(|m| m.is_dir()).unwrap_or(false));
233     assert!(fs::metadata(&dir_b).map(|m| m.is_dir()).unwrap_or(false));
234     assert!(fs::metadata(&file_c).map(|m| m.is_file()).unwrap_or(false));
235 }
236 
237 #[test]
extracting_directories()238 fn extracting_directories() {
239     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
240     let rdr = Cursor::new(tar!("directory.tar"));
241     let mut ar = Archive::new(rdr);
242     t!(ar.unpack(td.path()));
243     check_dirtree(&td);
244 }
245 
246 #[test]
extracting_duplicate_file_fail()247 fn extracting_duplicate_file_fail() {
248     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
249     let path_present = td.path().join("a");
250     t!(File::create(path_present));
251 
252     let rdr = Cursor::new(tar!("reading_files.tar"));
253     let mut ar = Archive::new(rdr);
254     ar.set_overwrite(false);
255     if let Err(err) = ar.unpack(td.path()) {
256         if err.kind() == std::io::ErrorKind::AlreadyExists {
257             // as expected with overwrite false
258             return;
259         }
260         panic!("unexpected error: {:?}", err);
261     }
262     panic!(
263         "unpack() should have returned an error of kind {:?}, returned Ok",
264         std::io::ErrorKind::AlreadyExists
265     )
266 }
267 
268 #[test]
extracting_duplicate_file_succeed()269 fn extracting_duplicate_file_succeed() {
270     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
271     let path_present = td.path().join("a");
272     t!(File::create(path_present));
273 
274     let rdr = Cursor::new(tar!("reading_files.tar"));
275     let mut ar = Archive::new(rdr);
276     ar.set_overwrite(true);
277     t!(ar.unpack(td.path()));
278 }
279 
280 #[test]
281 #[cfg(unix)]
extracting_duplicate_link_fail()282 fn extracting_duplicate_link_fail() {
283     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
284     let path_present = td.path().join("lnk");
285     t!(std::os::unix::fs::symlink("file", path_present));
286 
287     let rdr = Cursor::new(tar!("link.tar"));
288     let mut ar = Archive::new(rdr);
289     ar.set_overwrite(false);
290     if let Err(err) = ar.unpack(td.path()) {
291         if err.kind() == std::io::ErrorKind::AlreadyExists {
292             // as expected with overwrite false
293             return;
294         }
295         panic!("unexpected error: {:?}", err);
296     }
297     panic!(
298         "unpack() should have returned an error of kind {:?}, returned Ok",
299         std::io::ErrorKind::AlreadyExists
300     )
301 }
302 
303 #[test]
304 #[cfg(unix)]
extracting_duplicate_link_succeed()305 fn extracting_duplicate_link_succeed() {
306     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
307     let path_present = td.path().join("lnk");
308     t!(std::os::unix::fs::symlink("file", path_present));
309 
310     let rdr = Cursor::new(tar!("link.tar"));
311     let mut ar = Archive::new(rdr);
312     ar.set_overwrite(true);
313     t!(ar.unpack(td.path()));
314 }
315 
316 #[test]
317 #[cfg(all(unix, feature = "xattr"))]
xattrs()318 fn xattrs() {
319     // If /tmp is a tmpfs, xattr will fail
320     // The xattr crate's unit tests also use /var/tmp for this reason
321     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir_in("/var/tmp"));
322     let rdr = Cursor::new(tar!("xattrs.tar"));
323     let mut ar = Archive::new(rdr);
324     ar.set_unpack_xattrs(true);
325     t!(ar.unpack(td.path()));
326 
327     let val = xattr::get(td.path().join("a/b"), "user.pax.flags").unwrap();
328     assert_eq!(val.unwrap(), "epm".as_bytes());
329 }
330 
331 #[test]
332 #[cfg(all(unix, feature = "xattr"))]
no_xattrs()333 fn no_xattrs() {
334     // If /tmp is a tmpfs, xattr will fail
335     // The xattr crate's unit tests also use /var/tmp for this reason
336     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir_in("/var/tmp"));
337     let rdr = Cursor::new(tar!("xattrs.tar"));
338     let mut ar = Archive::new(rdr);
339     ar.set_unpack_xattrs(false);
340     t!(ar.unpack(td.path()));
341 
342     assert_eq!(
343         xattr::get(td.path().join("a/b"), "user.pax.flags").unwrap(),
344         None
345     );
346 }
347 
348 #[test]
writing_and_extracting_directories()349 fn writing_and_extracting_directories() {
350     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
351 
352     let mut ar = Builder::new(Vec::new());
353     let tmppath = td.path().join("tmpfile");
354     t!(t!(File::create(&tmppath)).write_all(b"c"));
355     t!(ar.append_dir("a", "."));
356     t!(ar.append_dir("a/b", "."));
357     t!(ar.append_file("a/c", &mut t!(File::open(&tmppath))));
358     t!(ar.finish());
359 
360     let rdr = Cursor::new(t!(ar.into_inner()));
361     let mut ar = Archive::new(rdr);
362     t!(ar.unpack(td.path()));
363     check_dirtree(&td);
364 }
365 
366 #[test]
writing_directories_recursively()367 fn writing_directories_recursively() {
368     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
369 
370     let base_dir = td.path().join("base");
371     t!(fs::create_dir(&base_dir));
372     t!(t!(File::create(base_dir.join("file1"))).write_all(b"file1"));
373     let sub_dir = base_dir.join("sub");
374     t!(fs::create_dir(&sub_dir));
375     t!(t!(File::create(sub_dir.join("file2"))).write_all(b"file2"));
376 
377     let mut ar = Builder::new(Vec::new());
378     t!(ar.append_dir_all("foobar", base_dir));
379     let data = t!(ar.into_inner());
380 
381     let mut ar = Archive::new(Cursor::new(data));
382     t!(ar.unpack(td.path()));
383     let base_dir = td.path().join("foobar");
384     assert!(fs::metadata(&base_dir).map(|m| m.is_dir()).unwrap_or(false));
385     let file1_path = base_dir.join("file1");
386     assert!(fs::metadata(&file1_path)
387         .map(|m| m.is_file())
388         .unwrap_or(false));
389     let sub_dir = base_dir.join("sub");
390     assert!(fs::metadata(&sub_dir).map(|m| m.is_dir()).unwrap_or(false));
391     let file2_path = sub_dir.join("file2");
392     assert!(fs::metadata(&file2_path)
393         .map(|m| m.is_file())
394         .unwrap_or(false));
395 }
396 
397 #[test]
append_dir_all_blank_dest()398 fn append_dir_all_blank_dest() {
399     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
400 
401     let base_dir = td.path().join("base");
402     t!(fs::create_dir(&base_dir));
403     t!(t!(File::create(base_dir.join("file1"))).write_all(b"file1"));
404     let sub_dir = base_dir.join("sub");
405     t!(fs::create_dir(&sub_dir));
406     t!(t!(File::create(sub_dir.join("file2"))).write_all(b"file2"));
407 
408     let mut ar = Builder::new(Vec::new());
409     t!(ar.append_dir_all("", base_dir));
410     let data = t!(ar.into_inner());
411 
412     let mut ar = Archive::new(Cursor::new(data));
413     t!(ar.unpack(td.path()));
414     let base_dir = td.path();
415     assert!(fs::metadata(&base_dir).map(|m| m.is_dir()).unwrap_or(false));
416     let file1_path = base_dir.join("file1");
417     assert!(fs::metadata(&file1_path)
418         .map(|m| m.is_file())
419         .unwrap_or(false));
420     let sub_dir = base_dir.join("sub");
421     assert!(fs::metadata(&sub_dir).map(|m| m.is_dir()).unwrap_or(false));
422     let file2_path = sub_dir.join("file2");
423     assert!(fs::metadata(&file2_path)
424         .map(|m| m.is_file())
425         .unwrap_or(false));
426 }
427 
428 #[test]
append_dir_all_does_not_work_on_non_directory()429 fn append_dir_all_does_not_work_on_non_directory() {
430     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
431     let path = td.path().join("test");
432     t!(t!(File::create(&path)).write_all(b"test"));
433 
434     let mut ar = Builder::new(Vec::new());
435     let result = ar.append_dir_all("test", path);
436     assert!(result.is_err());
437 }
438 
439 #[test]
extracting_duplicate_dirs()440 fn extracting_duplicate_dirs() {
441     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
442     let rdr = Cursor::new(tar!("duplicate_dirs.tar"));
443     let mut ar = Archive::new(rdr);
444     t!(ar.unpack(td.path()));
445 
446     let some_dir = td.path().join("some_dir");
447     assert!(fs::metadata(&some_dir).map(|m| m.is_dir()).unwrap_or(false));
448 }
449 
450 #[test]
unpack_old_style_bsd_dir()451 fn unpack_old_style_bsd_dir() {
452     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
453 
454     let mut ar = Builder::new(Vec::new());
455 
456     let mut header = Header::new_old();
457     header.set_entry_type(EntryType::Regular);
458     t!(header.set_path("testdir/"));
459     header.set_size(0);
460     header.set_cksum();
461     t!(ar.append(&header, &mut io::empty()));
462 
463     // Extracting
464     let rdr = Cursor::new(t!(ar.into_inner()));
465     let mut ar = Archive::new(rdr);
466     t!(ar.unpack(td.path()));
467 
468     // Iterating
469     let rdr = Cursor::new(ar.into_inner().into_inner());
470     let mut ar = Archive::new(rdr);
471     assert!(t!(ar.entries()).all(|fr| fr.is_ok()));
472 
473     assert!(td.path().join("testdir").is_dir());
474 }
475 
476 #[test]
handling_incorrect_file_size()477 fn handling_incorrect_file_size() {
478     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
479 
480     let mut ar = Builder::new(Vec::new());
481 
482     let path = td.path().join("tmpfile");
483     t!(File::create(&path));
484     let mut file = t!(File::open(&path));
485     let mut header = Header::new_old();
486     t!(header.set_path("somepath"));
487     header.set_metadata(&t!(file.metadata()));
488     header.set_size(2048); // past the end of file null blocks
489     header.set_cksum();
490     t!(ar.append(&header, &mut file));
491 
492     // Extracting
493     let rdr = Cursor::new(t!(ar.into_inner()));
494     let mut ar = Archive::new(rdr);
495     assert!(ar.unpack(td.path()).is_err());
496 
497     // Iterating
498     let rdr = Cursor::new(ar.into_inner().into_inner());
499     let mut ar = Archive::new(rdr);
500     assert!(t!(ar.entries()).any(|fr| fr.is_err()));
501 }
502 
503 #[test]
extracting_malicious_tarball()504 fn extracting_malicious_tarball() {
505     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
506 
507     let mut evil_tar = Vec::new();
508 
509     {
510         let mut a = Builder::new(&mut evil_tar);
511         let mut append = |path: &str| {
512             let mut header = Header::new_gnu();
513             assert!(header.set_path(path).is_err(), "was ok: {:?}", path);
514             {
515                 let h = header.as_gnu_mut().unwrap();
516                 for (a, b) in h.name.iter_mut().zip(path.as_bytes()) {
517                     *a = *b;
518                 }
519             }
520             header.set_size(1);
521             header.set_cksum();
522             t!(a.append(&header, io::repeat(1).take(1)));
523         };
524         append("/tmp/abs_evil.txt");
525         append("//tmp/abs_evil2.txt");
526         append("///tmp/abs_evil3.txt");
527         append("/./tmp/abs_evil4.txt");
528         append("//./tmp/abs_evil5.txt");
529         append("///./tmp/abs_evil6.txt");
530         append("/../tmp/rel_evil.txt");
531         append("../rel_evil2.txt");
532         append("./../rel_evil3.txt");
533         append("some/../../rel_evil4.txt");
534         append("");
535         append("././//./..");
536         append("..");
537         append("/////////..");
538         append("/////////");
539     }
540 
541     let mut ar = Archive::new(&evil_tar[..]);
542     t!(ar.unpack(td.path()));
543 
544     assert!(fs::metadata("/tmp/abs_evil.txt").is_err());
545     assert!(fs::metadata("/tmp/abs_evil.txt2").is_err());
546     assert!(fs::metadata("/tmp/abs_evil.txt3").is_err());
547     assert!(fs::metadata("/tmp/abs_evil.txt4").is_err());
548     assert!(fs::metadata("/tmp/abs_evil.txt5").is_err());
549     assert!(fs::metadata("/tmp/abs_evil.txt6").is_err());
550     assert!(fs::metadata("/tmp/rel_evil.txt").is_err());
551     assert!(fs::metadata("/tmp/rel_evil.txt").is_err());
552     assert!(fs::metadata(td.path().join("../tmp/rel_evil.txt")).is_err());
553     assert!(fs::metadata(td.path().join("../rel_evil2.txt")).is_err());
554     assert!(fs::metadata(td.path().join("../rel_evil3.txt")).is_err());
555     assert!(fs::metadata(td.path().join("../rel_evil4.txt")).is_err());
556 
557     // The `some` subdirectory should not be created because the only
558     // filename that references this has '..'.
559     assert!(fs::metadata(td.path().join("some")).is_err());
560 
561     // The `tmp` subdirectory should be created and within this
562     // subdirectory, there should be files named `abs_evil.txt` through
563     // `abs_evil6.txt`.
564     assert!(fs::metadata(td.path().join("tmp"))
565         .map(|m| m.is_dir())
566         .unwrap_or(false));
567     assert!(fs::metadata(td.path().join("tmp/abs_evil.txt"))
568         .map(|m| m.is_file())
569         .unwrap_or(false));
570     assert!(fs::metadata(td.path().join("tmp/abs_evil2.txt"))
571         .map(|m| m.is_file())
572         .unwrap_or(false));
573     assert!(fs::metadata(td.path().join("tmp/abs_evil3.txt"))
574         .map(|m| m.is_file())
575         .unwrap_or(false));
576     assert!(fs::metadata(td.path().join("tmp/abs_evil4.txt"))
577         .map(|m| m.is_file())
578         .unwrap_or(false));
579     assert!(fs::metadata(td.path().join("tmp/abs_evil5.txt"))
580         .map(|m| m.is_file())
581         .unwrap_or(false));
582     assert!(fs::metadata(td.path().join("tmp/abs_evil6.txt"))
583         .map(|m| m.is_file())
584         .unwrap_or(false));
585 }
586 
587 #[test]
octal_spaces()588 fn octal_spaces() {
589     let rdr = Cursor::new(tar!("spaces.tar"));
590     let mut ar = Archive::new(rdr);
591 
592     let entry = ar.entries().unwrap().next().unwrap().unwrap();
593     assert_eq!(entry.header().mode().unwrap() & 0o777, 0o777);
594     assert_eq!(entry.header().uid().unwrap(), 0);
595     assert_eq!(entry.header().gid().unwrap(), 0);
596     assert_eq!(entry.header().size().unwrap(), 2);
597     assert_eq!(entry.header().mtime().unwrap(), 0o12440016664);
598     assert_eq!(entry.header().cksum().unwrap(), 0o4253);
599 }
600 
601 #[test]
extracting_malformed_tar_null_blocks()602 fn extracting_malformed_tar_null_blocks() {
603     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
604 
605     let mut ar = Builder::new(Vec::new());
606 
607     let path1 = td.path().join("tmpfile1");
608     let path2 = td.path().join("tmpfile2");
609     t!(File::create(&path1));
610     t!(File::create(&path2));
611     t!(ar.append_file("tmpfile1", &mut t!(File::open(&path1))));
612     let mut data = t!(ar.into_inner());
613     let amt = data.len();
614     data.truncate(amt - 512);
615     let mut ar = Builder::new(data);
616     t!(ar.append_file("tmpfile2", &mut t!(File::open(&path2))));
617     t!(ar.finish());
618 
619     let data = t!(ar.into_inner());
620     let mut ar = Archive::new(&data[..]);
621     assert!(ar.unpack(td.path()).is_ok());
622 }
623 
624 #[test]
empty_filename()625 fn empty_filename() {
626     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
627     let rdr = Cursor::new(tar!("empty_filename.tar"));
628     let mut ar = Archive::new(rdr);
629     assert!(ar.unpack(td.path()).is_ok());
630 }
631 
632 #[test]
file_times()633 fn file_times() {
634     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
635     let rdr = Cursor::new(tar!("file_times.tar"));
636     let mut ar = Archive::new(rdr);
637     t!(ar.unpack(td.path()));
638 
639     let meta = fs::metadata(td.path().join("a")).unwrap();
640     let mtime = FileTime::from_last_modification_time(&meta);
641     let atime = FileTime::from_last_access_time(&meta);
642     assert_eq!(mtime.unix_seconds(), 1000000000);
643     assert_eq!(mtime.nanoseconds(), 0);
644     assert_eq!(atime.unix_seconds(), 1000000000);
645     assert_eq!(atime.nanoseconds(), 0);
646 }
647 
648 #[test]
zero_file_times()649 fn zero_file_times() {
650     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
651 
652     let mut ar = Builder::new(Vec::new());
653     ar.mode(HeaderMode::Deterministic);
654     let path = td.path().join("tmpfile");
655     t!(File::create(&path));
656     t!(ar.append_path_with_name(&path, "a"));
657 
658     let data = t!(ar.into_inner());
659     let mut ar = Archive::new(&data[..]);
660     assert!(ar.unpack(td.path()).is_ok());
661 
662     let meta = fs::metadata(td.path().join("a")).unwrap();
663     let mtime = FileTime::from_last_modification_time(&meta);
664     let atime = FileTime::from_last_access_time(&meta);
665     assert!(mtime.unix_seconds() != 0);
666     assert!(atime.unix_seconds() != 0);
667 }
668 
669 #[test]
backslash_treated_well()670 fn backslash_treated_well() {
671     // Insert a file into an archive with a backslash
672     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
673     let mut ar = Builder::new(Vec::<u8>::new());
674     t!(ar.append_dir("foo\\bar", td.path()));
675     let mut ar = Archive::new(Cursor::new(t!(ar.into_inner())));
676     let f = t!(t!(ar.entries()).next().unwrap());
677     if cfg!(unix) {
678         assert_eq!(t!(f.header().path()).to_str(), Some("foo\\bar"));
679     } else {
680         assert_eq!(t!(f.header().path()).to_str(), Some("foo/bar"));
681     }
682 
683     // Unpack an archive with a backslash in the name
684     let mut ar = Builder::new(Vec::<u8>::new());
685     let mut header = Header::new_gnu();
686     header.set_metadata(&t!(fs::metadata(td.path())));
687     header.set_size(0);
688     for (a, b) in header.as_old_mut().name.iter_mut().zip(b"foo\\bar\x00") {
689         *a = *b;
690     }
691     header.set_cksum();
692     t!(ar.append(&header, &mut io::empty()));
693     let data = t!(ar.into_inner());
694     let mut ar = Archive::new(&data[..]);
695     let f = t!(t!(ar.entries()).next().unwrap());
696     assert_eq!(t!(f.header().path()).to_str(), Some("foo\\bar"));
697 
698     let mut ar = Archive::new(&data[..]);
699     t!(ar.unpack(td.path()));
700     assert!(fs::metadata(td.path().join("foo\\bar")).is_ok());
701 }
702 
703 #[cfg(unix)]
704 #[test]
nul_bytes_in_path()705 fn nul_bytes_in_path() {
706     use std::ffi::OsStr;
707     use std::os::unix::prelude::*;
708 
709     let nul_path = OsStr::from_bytes(b"foo\0");
710     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
711     let mut ar = Builder::new(Vec::<u8>::new());
712     let err = ar.append_dir(nul_path, td.path()).unwrap_err();
713     assert!(err.to_string().contains("contains a nul byte"));
714 }
715 
716 #[test]
links()717 fn links() {
718     let mut ar = Archive::new(Cursor::new(tar!("link.tar")));
719     let mut entries = t!(ar.entries());
720     let link = t!(entries.next().unwrap());
721     assert_eq!(
722         t!(link.header().link_name()).as_ref().map(|p| &**p),
723         Some(Path::new("file"))
724     );
725     let other = t!(entries.next().unwrap());
726     assert!(t!(other.header().link_name()).is_none());
727 }
728 
729 #[test]
730 #[cfg(unix)] // making symlinks on windows is hard
unpack_links()731 fn unpack_links() {
732     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
733     let mut ar = Archive::new(Cursor::new(tar!("link.tar")));
734     t!(ar.unpack(td.path()));
735 
736     let md = t!(fs::symlink_metadata(td.path().join("lnk")));
737     assert!(md.file_type().is_symlink());
738     assert_eq!(
739         &*t!(fs::read_link(td.path().join("lnk"))),
740         Path::new("file")
741     );
742     t!(File::open(td.path().join("lnk")));
743 }
744 
745 #[test]
pax_size()746 fn pax_size() {
747     let mut ar = Archive::new(tar!("pax_size.tar"));
748     let mut entries = t!(ar.entries());
749     let mut entry = t!(entries.next().unwrap());
750     let mut attributes = t!(entry.pax_extensions()).unwrap();
751 
752     let _first = t!(attributes.next().unwrap());
753     let _second = t!(attributes.next().unwrap());
754     let _third = t!(attributes.next().unwrap());
755     let fourth = t!(attributes.next().unwrap());
756     assert!(attributes.next().is_none());
757 
758     assert_eq!(fourth.key(), Ok("size"));
759     assert_eq!(fourth.value(), Ok("4"));
760 
761     assert_eq!(entry.header().size().unwrap(), 0);
762     assert_eq!(entry.size(), 4);
763 }
764 
765 #[test]
pax_simple()766 fn pax_simple() {
767     let mut ar = Archive::new(tar!("pax.tar"));
768     let mut entries = t!(ar.entries());
769 
770     let mut first = t!(entries.next().unwrap());
771     let mut attributes = t!(first.pax_extensions()).unwrap();
772     let first = t!(attributes.next().unwrap());
773     let second = t!(attributes.next().unwrap());
774     let third = t!(attributes.next().unwrap());
775     assert!(attributes.next().is_none());
776 
777     assert_eq!(first.key(), Ok("mtime"));
778     assert_eq!(first.value(), Ok("1453146164.953123768"));
779     assert_eq!(second.key(), Ok("atime"));
780     assert_eq!(second.value(), Ok("1453251915.24892486"));
781     assert_eq!(third.key(), Ok("ctime"));
782     assert_eq!(third.value(), Ok("1453146164.953123768"));
783 }
784 
785 #[test]
pax_path()786 fn pax_path() {
787     let mut ar = Archive::new(tar!("pax2.tar"));
788     let mut entries = t!(ar.entries());
789 
790     let first = t!(entries.next().unwrap());
791     assert!(first.path().unwrap().ends_with("aaaaaaaaaaaaaaa"));
792 }
793 
794 #[test]
long_name_trailing_nul()795 fn long_name_trailing_nul() {
796     let mut b = Builder::new(Vec::<u8>::new());
797 
798     let mut h = Header::new_gnu();
799     t!(h.set_path("././@LongLink"));
800     h.set_size(4);
801     h.set_entry_type(EntryType::new(b'L'));
802     h.set_cksum();
803     t!(b.append(&h, "foo\0".as_bytes()));
804     let mut h = Header::new_gnu();
805 
806     t!(h.set_path("bar"));
807     h.set_size(6);
808     h.set_entry_type(EntryType::file());
809     h.set_cksum();
810     t!(b.append(&h, "foobar".as_bytes()));
811 
812     let contents = t!(b.into_inner());
813     let mut a = Archive::new(&contents[..]);
814 
815     let e = t!(t!(a.entries()).next().unwrap());
816     assert_eq!(&*e.path_bytes(), b"foo");
817 }
818 
819 #[test]
long_linkname_trailing_nul()820 fn long_linkname_trailing_nul() {
821     let mut b = Builder::new(Vec::<u8>::new());
822 
823     let mut h = Header::new_gnu();
824     t!(h.set_path("././@LongLink"));
825     h.set_size(4);
826     h.set_entry_type(EntryType::new(b'K'));
827     h.set_cksum();
828     t!(b.append(&h, "foo\0".as_bytes()));
829     let mut h = Header::new_gnu();
830 
831     t!(h.set_path("bar"));
832     h.set_size(6);
833     h.set_entry_type(EntryType::file());
834     h.set_cksum();
835     t!(b.append(&h, "foobar".as_bytes()));
836 
837     let contents = t!(b.into_inner());
838     let mut a = Archive::new(&contents[..]);
839 
840     let e = t!(t!(a.entries()).next().unwrap());
841     assert_eq!(&*e.link_name_bytes().unwrap(), b"foo");
842 }
843 
844 #[test]
encoded_long_name_has_trailing_nul()845 fn encoded_long_name_has_trailing_nul() {
846     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
847     let path = td.path().join("foo");
848     t!(t!(File::create(&path)).write_all(b"test"));
849 
850     let mut b = Builder::new(Vec::<u8>::new());
851     let long = repeat("abcd").take(200).collect::<String>();
852 
853     t!(b.append_file(&long, &mut t!(File::open(&path))));
854 
855     let contents = t!(b.into_inner());
856     let mut a = Archive::new(&contents[..]);
857 
858     let mut e = t!(t!(a.entries()).raw(true).next().unwrap());
859     let mut name = Vec::new();
860     t!(e.read_to_end(&mut name));
861     assert_eq!(name[name.len() - 1], 0);
862 
863     let header_name = &e.header().as_gnu().unwrap().name;
864     assert!(header_name.starts_with(b"././@LongLink\x00"));
865 }
866 
867 #[test]
reading_sparse()868 fn reading_sparse() {
869     let rdr = Cursor::new(tar!("sparse.tar"));
870     let mut ar = Archive::new(rdr);
871     let mut entries = t!(ar.entries());
872 
873     let mut a = t!(entries.next().unwrap());
874     let mut s = String::new();
875     assert_eq!(&*a.header().path_bytes(), b"sparse_begin.txt");
876     t!(a.read_to_string(&mut s));
877     assert_eq!(&s[..5], "test\n");
878     assert!(s[5..].chars().all(|x| x == '\u{0}'));
879 
880     let mut a = t!(entries.next().unwrap());
881     let mut s = String::new();
882     assert_eq!(&*a.header().path_bytes(), b"sparse_end.txt");
883     t!(a.read_to_string(&mut s));
884     assert!(s[..s.len() - 9].chars().all(|x| x == '\u{0}'));
885     assert_eq!(&s[s.len() - 9..], "test_end\n");
886 
887     let mut a = t!(entries.next().unwrap());
888     let mut s = String::new();
889     assert_eq!(&*a.header().path_bytes(), b"sparse_ext.txt");
890     t!(a.read_to_string(&mut s));
891     assert!(s[..0x1000].chars().all(|x| x == '\u{0}'));
892     assert_eq!(&s[0x1000..0x1000 + 5], "text\n");
893     assert!(s[0x1000 + 5..0x3000].chars().all(|x| x == '\u{0}'));
894     assert_eq!(&s[0x3000..0x3000 + 5], "text\n");
895     assert!(s[0x3000 + 5..0x5000].chars().all(|x| x == '\u{0}'));
896     assert_eq!(&s[0x5000..0x5000 + 5], "text\n");
897     assert!(s[0x5000 + 5..0x7000].chars().all(|x| x == '\u{0}'));
898     assert_eq!(&s[0x7000..0x7000 + 5], "text\n");
899     assert!(s[0x7000 + 5..0x9000].chars().all(|x| x == '\u{0}'));
900     assert_eq!(&s[0x9000..0x9000 + 5], "text\n");
901     assert!(s[0x9000 + 5..0xb000].chars().all(|x| x == '\u{0}'));
902     assert_eq!(&s[0xb000..0xb000 + 5], "text\n");
903 
904     let mut a = t!(entries.next().unwrap());
905     let mut s = String::new();
906     assert_eq!(&*a.header().path_bytes(), b"sparse.txt");
907     t!(a.read_to_string(&mut s));
908     assert!(s[..0x1000].chars().all(|x| x == '\u{0}'));
909     assert_eq!(&s[0x1000..0x1000 + 6], "hello\n");
910     assert!(s[0x1000 + 6..0x2fa0].chars().all(|x| x == '\u{0}'));
911     assert_eq!(&s[0x2fa0..0x2fa0 + 6], "world\n");
912     assert!(s[0x2fa0 + 6..0x4000].chars().all(|x| x == '\u{0}'));
913 
914     assert!(entries.next().is_none());
915 }
916 
917 #[test]
extract_sparse()918 fn extract_sparse() {
919     let rdr = Cursor::new(tar!("sparse.tar"));
920     let mut ar = Archive::new(rdr);
921     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
922     t!(ar.unpack(td.path()));
923 
924     let mut s = String::new();
925     t!(t!(File::open(td.path().join("sparse_begin.txt"))).read_to_string(&mut s));
926     assert_eq!(&s[..5], "test\n");
927     assert!(s[5..].chars().all(|x| x == '\u{0}'));
928 
929     s.truncate(0);
930     t!(t!(File::open(td.path().join("sparse_end.txt"))).read_to_string(&mut s));
931     assert!(s[..s.len() - 9].chars().all(|x| x == '\u{0}'));
932     assert_eq!(&s[s.len() - 9..], "test_end\n");
933 
934     s.truncate(0);
935     t!(t!(File::open(td.path().join("sparse_ext.txt"))).read_to_string(&mut s));
936     assert!(s[..0x1000].chars().all(|x| x == '\u{0}'));
937     assert_eq!(&s[0x1000..0x1000 + 5], "text\n");
938     assert!(s[0x1000 + 5..0x3000].chars().all(|x| x == '\u{0}'));
939     assert_eq!(&s[0x3000..0x3000 + 5], "text\n");
940     assert!(s[0x3000 + 5..0x5000].chars().all(|x| x == '\u{0}'));
941     assert_eq!(&s[0x5000..0x5000 + 5], "text\n");
942     assert!(s[0x5000 + 5..0x7000].chars().all(|x| x == '\u{0}'));
943     assert_eq!(&s[0x7000..0x7000 + 5], "text\n");
944     assert!(s[0x7000 + 5..0x9000].chars().all(|x| x == '\u{0}'));
945     assert_eq!(&s[0x9000..0x9000 + 5], "text\n");
946     assert!(s[0x9000 + 5..0xb000].chars().all(|x| x == '\u{0}'));
947     assert_eq!(&s[0xb000..0xb000 + 5], "text\n");
948 
949     s.truncate(0);
950     t!(t!(File::open(td.path().join("sparse.txt"))).read_to_string(&mut s));
951     assert!(s[..0x1000].chars().all(|x| x == '\u{0}'));
952     assert_eq!(&s[0x1000..0x1000 + 6], "hello\n");
953     assert!(s[0x1000 + 6..0x2fa0].chars().all(|x| x == '\u{0}'));
954     assert_eq!(&s[0x2fa0..0x2fa0 + 6], "world\n");
955     assert!(s[0x2fa0 + 6..0x4000].chars().all(|x| x == '\u{0}'));
956 }
957 
958 #[test]
sparse_with_trailing()959 fn sparse_with_trailing() {
960     let rdr = Cursor::new(tar!("sparse-1.tar"));
961     let mut ar = Archive::new(rdr);
962     let mut entries = t!(ar.entries());
963     let mut a = t!(entries.next().unwrap());
964     let mut s = String::new();
965     t!(a.read_to_string(&mut s));
966     assert_eq!(0x100_00c, s.len());
967     assert_eq!(&s[..0xc], "0MB through\n");
968     assert!(s[0xc..0x100_000].chars().all(|x| x == '\u{0}'));
969     assert_eq!(&s[0x100_000..], "1MB through\n");
970 }
971 
972 #[test]
path_separators()973 fn path_separators() {
974     let mut ar = Builder::new(Vec::new());
975     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
976 
977     let path = td.path().join("test");
978     t!(t!(File::create(&path)).write_all(b"test"));
979 
980     let short_path: PathBuf = repeat("abcd").take(2).collect();
981     let long_path: PathBuf = repeat("abcd").take(50).collect();
982 
983     // Make sure UStar headers normalize to Unix path separators
984     let mut header = Header::new_ustar();
985 
986     t!(header.set_path(&short_path));
987     assert_eq!(t!(header.path()), short_path);
988     assert!(!header.path_bytes().contains(&b'\\'));
989 
990     t!(header.set_path(&long_path));
991     assert_eq!(t!(header.path()), long_path);
992     assert!(!header.path_bytes().contains(&b'\\'));
993 
994     // Make sure GNU headers normalize to Unix path separators,
995     // including the `@LongLink` fallback used by `append_file`.
996     t!(ar.append_file(&short_path, &mut t!(File::open(&path))));
997     t!(ar.append_file(&long_path, &mut t!(File::open(&path))));
998 
999     let rd = Cursor::new(t!(ar.into_inner()));
1000     let mut ar = Archive::new(rd);
1001     let mut entries = t!(ar.entries());
1002 
1003     let entry = t!(entries.next().unwrap());
1004     assert_eq!(t!(entry.path()), short_path);
1005     assert!(!entry.path_bytes().contains(&b'\\'));
1006 
1007     let entry = t!(entries.next().unwrap());
1008     assert_eq!(t!(entry.path()), long_path);
1009     assert!(!entry.path_bytes().contains(&b'\\'));
1010 
1011     assert!(entries.next().is_none());
1012 }
1013 
1014 #[test]
1015 #[cfg(unix)]
append_path_symlink()1016 fn append_path_symlink() {
1017     use std::borrow::Cow;
1018     use std::env;
1019     use std::os::unix::fs::symlink;
1020 
1021     let mut ar = Builder::new(Vec::new());
1022     ar.follow_symlinks(false);
1023     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
1024 
1025     let long_linkname = repeat("abcd").take(30).collect::<String>();
1026     let long_pathname = repeat("dcba").take(30).collect::<String>();
1027     t!(env::set_current_dir(td.path()));
1028     // "short" path name / short link name
1029     t!(symlink("testdest", "test"));
1030     t!(ar.append_path("test"));
1031     // short path name / long link name
1032     t!(symlink(&long_linkname, "test2"));
1033     t!(ar.append_path("test2"));
1034     // long path name / long link name
1035     t!(symlink(&long_linkname, &long_pathname));
1036     t!(ar.append_path(&long_pathname));
1037 
1038     let rd = Cursor::new(t!(ar.into_inner()));
1039     let mut ar = Archive::new(rd);
1040     let mut entries = t!(ar.entries());
1041 
1042     let entry = t!(entries.next().unwrap());
1043     assert_eq!(t!(entry.path()), Path::new("test"));
1044     assert_eq!(
1045         t!(entry.link_name()),
1046         Some(Cow::from(Path::new("testdest")))
1047     );
1048     assert_eq!(t!(entry.header().size()), 0);
1049 
1050     let entry = t!(entries.next().unwrap());
1051     assert_eq!(t!(entry.path()), Path::new("test2"));
1052     assert_eq!(
1053         t!(entry.link_name()),
1054         Some(Cow::from(Path::new(&long_linkname)))
1055     );
1056     assert_eq!(t!(entry.header().size()), 0);
1057 
1058     let entry = t!(entries.next().unwrap());
1059     assert_eq!(t!(entry.path()), Path::new(&long_pathname));
1060     assert_eq!(
1061         t!(entry.link_name()),
1062         Some(Cow::from(Path::new(&long_linkname)))
1063     );
1064     assert_eq!(t!(entry.header().size()), 0);
1065 
1066     assert!(entries.next().is_none());
1067 }
1068 
1069 #[test]
name_with_slash_doesnt_fool_long_link_and_bsd_compat()1070 fn name_with_slash_doesnt_fool_long_link_and_bsd_compat() {
1071     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
1072 
1073     let mut ar = Builder::new(Vec::new());
1074 
1075     let mut h = Header::new_gnu();
1076     t!(h.set_path("././@LongLink"));
1077     h.set_size(4);
1078     h.set_entry_type(EntryType::new(b'L'));
1079     h.set_cksum();
1080     t!(ar.append(&h, "foo\0".as_bytes()));
1081 
1082     let mut header = Header::new_gnu();
1083     header.set_entry_type(EntryType::Regular);
1084     t!(header.set_path("testdir/"));
1085     header.set_size(0);
1086     header.set_cksum();
1087     t!(ar.append(&header, &mut io::empty()));
1088 
1089     // Extracting
1090     let rdr = Cursor::new(t!(ar.into_inner()));
1091     let mut ar = Archive::new(rdr);
1092     t!(ar.unpack(td.path()));
1093 
1094     // Iterating
1095     let rdr = Cursor::new(ar.into_inner().into_inner());
1096     let mut ar = Archive::new(rdr);
1097     assert!(t!(ar.entries()).all(|fr| fr.is_ok()));
1098 
1099     assert!(td.path().join("foo").is_file());
1100 }
1101 
1102 #[test]
insert_local_file_different_name()1103 fn insert_local_file_different_name() {
1104     let mut ar = Builder::new(Vec::new());
1105     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
1106     let path = td.path().join("directory");
1107     t!(fs::create_dir(&path));
1108     ar.append_path_with_name(&path, "archive/dir").unwrap();
1109     let path = td.path().join("file");
1110     t!(t!(File::create(&path)).write_all(b"test"));
1111     ar.append_path_with_name(&path, "archive/dir/f").unwrap();
1112 
1113     let rd = Cursor::new(t!(ar.into_inner()));
1114     let mut ar = Archive::new(rd);
1115     let mut entries = t!(ar.entries());
1116     let entry = t!(entries.next().unwrap());
1117     assert_eq!(t!(entry.path()), Path::new("archive/dir"));
1118     let entry = t!(entries.next().unwrap());
1119     assert_eq!(t!(entry.path()), Path::new("archive/dir/f"));
1120     assert!(entries.next().is_none());
1121 }
1122 
1123 #[test]
1124 #[cfg(unix)]
tar_directory_containing_symlink_to_directory()1125 fn tar_directory_containing_symlink_to_directory() {
1126     use std::os::unix::fs::symlink;
1127 
1128     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
1129     let dummy_src = t!(TempBuilder::new().prefix("dummy_src").tempdir());
1130     let dummy_dst = td.path().join("dummy_dst");
1131     let mut ar = Builder::new(Vec::new());
1132     t!(symlink(dummy_src.path().display().to_string(), &dummy_dst));
1133 
1134     assert!(dummy_dst.read_link().is_ok());
1135     assert!(dummy_dst.read_link().unwrap().is_dir());
1136     ar.append_dir_all("symlinks", td.path()).unwrap();
1137     ar.finish().unwrap();
1138 }
1139 
1140 #[test]
long_path()1141 fn long_path() {
1142     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
1143     let rdr = Cursor::new(tar!("7z_long_path.tar"));
1144     let mut ar = Archive::new(rdr);
1145     assert!(ar.unpack(td.path()).is_ok());
1146 }
1147 
1148 #[test]
unpack_path_larger_than_windows_max_path()1149 fn unpack_path_larger_than_windows_max_path() {
1150     let dir_name = "iamaprettylongnameandtobepreciseiam91characterslongwhichsomethinkisreallylongandothersdonot";
1151     // 183 character directory name
1152     let really_long_path = format!("{}{}", dir_name, dir_name);
1153     let td = t!(TempBuilder::new().prefix(&really_long_path).tempdir());
1154     // directory in 7z_long_path.tar is over 100 chars
1155     let rdr = Cursor::new(tar!("7z_long_path.tar"));
1156     let mut ar = Archive::new(rdr);
1157     // should unpack path greater than windows MAX_PATH length of 260 characters
1158     assert!(ar.unpack(td.path()).is_ok());
1159 }
1160 
1161 #[test]
append_long_multibyte()1162 fn append_long_multibyte() {
1163     let mut x = tar::Builder::new(Vec::new());
1164     let mut name = String::new();
1165     let data: &[u8] = &[];
1166     for _ in 0..512 {
1167         name.push('a');
1168         name.push('��');
1169         x.append_data(&mut Header::new_gnu(), &name, data).unwrap();
1170         name.pop();
1171     }
1172 }
1173 
1174 #[test]
read_only_directory_containing_files()1175 fn read_only_directory_containing_files() {
1176     let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
1177 
1178     let mut b = Builder::new(Vec::<u8>::new());
1179 
1180     let mut h = Header::new_gnu();
1181     t!(h.set_path("dir/"));
1182     h.set_size(0);
1183     h.set_entry_type(EntryType::dir());
1184     h.set_mode(0o444);
1185     h.set_cksum();
1186     t!(b.append(&h, "".as_bytes()));
1187 
1188     let mut h = Header::new_gnu();
1189     t!(h.set_path("dir/file"));
1190     h.set_size(2);
1191     h.set_entry_type(EntryType::file());
1192     h.set_cksum();
1193     t!(b.append(&h, "hi".as_bytes()));
1194 
1195     let contents = t!(b.into_inner());
1196     let mut ar = Archive::new(&contents[..]);
1197     assert!(ar.unpack(td.path()).is_ok());
1198 }
1199