File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\util\io\TarArchiverTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2011, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util.io;
25
26 import hudson.FilePath;
27 import hudson.Functions;
28 import hudson.Launcher.LocalLauncher;
29 import hudson.Util;
30 import hudson.model.TaskListener;
31 import hudson.util.NullStream;
32 import hudson.util.StreamTaskListener;
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import static org.junit.Assert.*;
36 import static org.junit.Assume.*;
37 import org.junit.Test;
38 import org.jvnet.hudson.test.Bug;
39
40 public class TarArchiverTest {
41
42 /**
43 * Makes sure that permissions are properly stored in the tar file.
44 */
45 @Bug(9397)
46 @Test public void permission() throws Exception {
47 assumeTrue(!Functions.isWindows());
48
49 File tar = File.createTempFile("test","tar");
50 File zip = File.createTempFile("test","zip");
51
52 FilePath dir = new FilePath(File.createTempFile("test","dir"));
53
54 try {
55 dir.delete();
56 dir.child("subdir").mkdirs();
57
58 FilePath f = dir.child("a.txt");
59 f.touch(0);
60 f.chmod(0755);
61
62 f = dir.child("subdir/b.txt");
63 f.touch(0);
64 f.chmod(0644);
65 int dirMode = dir.child("subdir").mode();
66
67 dir.tar(new FileOutputStream(tar),"**/*");
68 dir.zip(new FileOutputStream(zip));
69
70
71 FilePath e = dir.child("extract");
72 e.mkdirs();
73
74 // extract via the tar command
75 assertEquals(0, new LocalLauncher(new StreamTaskListener(System.out)).launch().cmds("tar", "xvpf", tar.getAbsolutePath()).pwd(e).join());
76
77 assertEquals(0100755,e.child("a.txt").mode());
78 assertEquals(dirMode,e.child("subdir").mode());
79 assertEquals(0100644,e.child("subdir/b.txt").mode());
80
81
82 // extract via the zip command
83 e.deleteContents();
84 assertEquals(0, new LocalLauncher(new StreamTaskListener(System.out)).launch().cmds("unzip", zip.getAbsolutePath()).pwd(e).join());
85 e = e.listDirectories().get(0);
86
87 assertEquals(0100755, e.child("a.txt").mode());
88 assertEquals(dirMode,e.child("subdir").mode());
89 assertEquals(0100644,e.child("subdir/b.txt").mode());
90 } finally {
91 tar.delete();
92 zip.delete();
93 dir.deleteRecursive();
94 }
95 }
96
97 @Bug(14922)
98 @Test public void brokenSymlinks() throws Exception {
99 assumeTrue(!Functions.isWindows());
100 File dir = Util.createTempDir();
101 Util.createSymlink(dir, "nonexistent", "link", TaskListener.NULL);
102 new FilePath(dir).tar(new NullStream(), "**");
103 }
104
105 }
106