File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\model\DirectoryBrowserSupportTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
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.model;
25
26 import hudson.FilePath;
27 import hudson.Functions;
28 import hudson.tasks.Shell;
29 import hudson.tasks.BatchFile;
30 import hudson.Launcher;
31 import org.jvnet.hudson.test.Email;
32 import org.jvnet.hudson.test.HudsonTestCase;
33 import org.jvnet.hudson.test.TestBuilder;
34
35 import java.io.IOException;
36
37 /**
38 * @author Kohsuke Kawaguchi
39 */
40 public class DirectoryBrowserSupportTest extends HudsonTestCase {
41 /**
42 * Double dots that appear in file name is OK.
43 */
44 @Email("http://www.nabble.com/Status-Code-400-viewing-or-downloading-artifact-whose-filename-contains-two-consecutive-periods-tt21407604.html")
45 public void testDoubleDots() throws Exception {
46 // create a problematic file name in the workspace
47 FreeStyleProject p = createFreeStyleProject();
48 if(Functions.isWindows())
49 p.getBuildersList().add(new BatchFile("echo > abc..def"));
50 else
51 p.getBuildersList().add(new Shell("touch abc..def"));
52 p.scheduleBuild2(0).get();
53
54 // can we see it?
55 new WebClient().goTo("job/"+p.getName()+"/ws/abc..def","application/octet-stream");
56
57 // TODO: implement negative check to make sure we aren't serving unexpected directories.
58 // the following trivial attempt failed. Someone in between is normalizing.
59 // // but this should fail
60 // try {
61 // new WebClient().goTo("job/" + p.getName() + "/ws/abc/../", "application/octet-stream");
62 // } catch (FailingHttpStatusCodeException e) {
63 // assertEquals(400,e.getStatusCode());
64 // }
65 }
66
67 /**
68 * <strike>Also makes sure '\\' in the file name for Unix is handled correctly</strike>.
69 *
70 * To prevent directory traversal attack, we now treat '\\' just like '/'.
71 */
72 @Email("http://www.nabble.com/Status-Code-400-viewing-or-downloading-artifact-whose-filename-contains-two-consecutive-periods-tt21407604.html")
73 public void testDoubleDots2() throws Exception {
74 if(Functions.isWindows()) return; // can't test this on Windows
75
76 // create a problematic file name in the workspace
77 FreeStyleProject p = createFreeStyleProject();
78 p.getBuildersList().add(new Shell("mkdir abc; touch abc/def.bin"));
79 p.scheduleBuild2(0).get();
80
81 // can we see it?
82 new WebClient().goTo("job/"+p.getName()+"/ws/abc%5Cdef.bin","application/octet-stream");
83 }
84
85 public void testNonAsciiChar() throws Exception {
86 // create a problematic file name in the workspace
87 FreeStyleProject p = createFreeStyleProject();
88 p.getBuildersList().add(new TestBuilder() {
89 public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
90 build.getWorkspace().child("\u6F22\u5B57.bin").touch(0); // Kanji
91 return true;
92 }
93 }); // Kanji
94 p.scheduleBuild2(0).get();
95
96 // can we see it?
97 new WebClient().goTo("job/"+p.getName()+"/ws/%e6%bc%a2%e5%ad%97.bin","application/octet-stream");
98 }
99
100 public void testGlob() throws Exception {
101 FreeStyleProject p = createFreeStyleProject();
102 p.getBuildersList().add(new TestBuilder() {
103 @Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
104 FilePath ws = build.getWorkspace();
105 ws.child("pom.xml").touch(0);
106 ws.child("src/main/java/p").mkdirs();
107 ws.child("src/main/java/p/X.java").touch(0);
108 ws.child("src/main/resources/p").mkdirs();
109 ws.child("src/main/resources/p/x.txt").touch(0);
110 ws.child("src/test/java/p").mkdirs();
111 ws.child("src/test/java/p/XTest.java").touch(0);
112 return true;
113 }
114 });
115 assertEquals(Result.SUCCESS, p.scheduleBuild2(0).get().getResult());
116 String text = new WebClient().goTo("job/"+p.getName()+"/ws/**/*.java").asText();
117 assertTrue(text, text.contains("X.java"));
118 assertTrue(text, text.contains("XTest.java"));
119 assertFalse(text, text.contains("pom.xml"));
120 assertFalse(text, text.contains("x.txt"));
121 }
122
123 }
124