Skip to content

Commit

Permalink
Merge 11fe787 from mathieu-pousse/master #954
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergondza committed Sep 26, 2013
2 parents b36f7e1 + 82af0fb commit 32c451f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private static void zip(OutputStream outputStream, VirtualFile dir, String glob)
VirtualFile f = dir.child(n);
e.setTime(f.lastModified());
zos.putNextEntry(e);
Util.copyStream(f.open(), outputStream);
Util.copyStream(f.open(), zos);
zos.closeEntry();
}
zos.close();
Expand Down
91 changes: 74 additions & 17 deletions test/src/test/java/hudson/model/DirectoryBrowserSupportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,58 @@
*/
package hudson.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import hudson.FilePath;
import hudson.Functions;
import hudson.tasks.Shell;
import hudson.tasks.BatchFile;
import hudson.Launcher;
import hudson.Util;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.BatchFile;
import hudson.tasks.Shell;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipFile;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Email;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.SingleFileSCM;
import org.jvnet.hudson.test.TestBuilder;

import java.io.IOException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.UnexpectedPage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
* @author Kohsuke Kawaguchi
*/
public class DirectoryBrowserSupportTest extends HudsonTestCase {
public class DirectoryBrowserSupportTest {

@Rule public JenkinsRule j = new JenkinsRule();

/**
* Double dots that appear in file name is OK.
*/
@Email("http://www.nabble.com/Status-Code-400-viewing-or-downloading-artifact-whose-filename-contains-two-consecutive-periods-tt21407604.html")
public void testDoubleDots() throws Exception {
@Test
public void doubleDots() throws Exception {
// create a problematic file name in the workspace
FreeStyleProject p = createFreeStyleProject();
FreeStyleProject p = j.createFreeStyleProject();
if(Functions.isWindows())
p.getBuildersList().add(new BatchFile("echo > abc..def"));
else
p.getBuildersList().add(new Shell("touch abc..def"));
p.scheduleBuild2(0).get();

// can we see it?
new WebClient().goTo("job/"+p.getName()+"/ws/abc..def","application/octet-stream");
j.createWebClient().goTo("job/"+p.getName()+"/ws/abc..def","application/octet-stream");

// TODO: implement negative check to make sure we aren't serving unexpected directories.
// the following trivial attempt failed. Someone in between is normalizing.
Expand All @@ -70,21 +92,23 @@ public void testDoubleDots() throws Exception {
* To prevent directory traversal attack, we now treat '\\' just like '/'.
*/
@Email("http://www.nabble.com/Status-Code-400-viewing-or-downloading-artifact-whose-filename-contains-two-consecutive-periods-tt21407604.html")
public void testDoubleDots2() throws Exception {
@Test
public void doubleDots2() throws Exception {
if(Functions.isWindows()) return; // can't test this on Windows

// create a problematic file name in the workspace
FreeStyleProject p = createFreeStyleProject();
FreeStyleProject p = j.createFreeStyleProject();
p.getBuildersList().add(new Shell("mkdir abc; touch abc/def.bin"));
p.scheduleBuild2(0).get();

// can we see it?
new WebClient().goTo("job/"+p.getName()+"/ws/abc%5Cdef.bin","application/octet-stream");
j.createWebClient().goTo("job/"+p.getName()+"/ws/abc%5Cdef.bin","application/octet-stream");
}

public void testNonAsciiChar() throws Exception {
@Test
public void nonAsciiChar() throws Exception {
// create a problematic file name in the workspace
FreeStyleProject p = createFreeStyleProject();
FreeStyleProject p = j.createFreeStyleProject();
p.getBuildersList().add(new TestBuilder() {
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("\u6F22\u5B57.bin").touch(0); // Kanji
Expand All @@ -94,11 +118,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
p.scheduleBuild2(0).get();

// can we see it?
new WebClient().goTo("job/"+p.getName()+"/ws/%e6%bc%a2%e5%ad%97.bin","application/octet-stream");
j.createWebClient().goTo("job/"+p.getName()+"/ws/%e6%bc%a2%e5%ad%97.bin","application/octet-stream");
}

public void testGlob() throws Exception {
FreeStyleProject p = createFreeStyleProject();
@Test
public void glob() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.getBuildersList().add(new TestBuilder() {
@Override public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
FilePath ws = build.getWorkspace();
Expand All @@ -113,11 +138,43 @@ public void testGlob() throws Exception {
}
});
assertEquals(Result.SUCCESS, p.scheduleBuild2(0).get().getResult());
String text = new WebClient().goTo("job/"+p.getName()+"/ws/**/*.java").asText();
String text = j.createWebClient().goTo("job/"+p.getName()+"/ws/**/*.java").asText();
assertTrue(text, text.contains("X.java"));
assertTrue(text, text.contains("XTest.java"));
assertFalse(text, text.contains("pom.xml"));
assertFalse(text, text.contains("x.txt"));
}

@Bug(19752)
@Test
public void zipDownload() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.setScm(new SingleFileSCM("artifact.out", "Hello world!"));
p.getPublishersList().add(new ArtifactArchiver("*", "", true));
assertEquals(Result.SUCCESS, p.scheduleBuild2(0).get().getResult());

HtmlPage page = j.createWebClient().goTo("job/"+p.getName()+"/lastSuccessfulBuild/artifact/");
Page download = page.getAnchorByHref("./*zip*/archive.zip").click();
File zipfile = download((UnexpectedPage) download);

ZipFile readzip = new ZipFile(zipfile);

InputStream is = readzip.getInputStream(readzip.getEntry("artifact.out"));

// ZipException in case of JENKINS-19752
assertFalse("Downloaded zip file must not be empty", is.read() == -1);

is.close();
readzip.close();
zipfile.delete();
}

private File download(UnexpectedPage page) throws IOException {

File file = new File("/home/ogondza/zipfile");//File.createTempFile("DirectoryBrowserSupport", "zipDownload");
file.delete();
Util.copyStreamAndClose(page.getInputStream(), new FileOutputStream(file));

return file;
}
}

0 comments on commit 32c451f

Please sign in to comment.