Skip to content

Commit

Permalink
[SECURITY-478] Strengthening test.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Sep 28, 2017
1 parent 3ff432a commit 67f68c1
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion test/src/test/java/hudson/slaves/CommandLauncher2Test.java
Expand Up @@ -29,15 +29,19 @@
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import hudson.XmlFile;
import hudson.cli.CLICommand;
import hudson.cli.CLICommandInvoker;
import hudson.cli.UpdateNodeCommand;
import hudson.model.Computer;
import hudson.model.User;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.annotation.CheckForNull;
import jenkins.model.Jenkins;
import org.apache.tools.ant.filters.StringInputStream;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
Expand Down Expand Up @@ -73,6 +77,7 @@ public void evaluate() throws Throwable {
rr.j.submit(form);
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by GUI", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by GUI");
// Then by REST.
String configDotXml = s.toComputer().getUrl() + "config.xml";
String xml = wc.goTo(configDotXml, "application/xml").getWebResponse().getContentAsString();
Expand All @@ -83,14 +88,17 @@ public void evaluate() throws Throwable {
wc.getPage(req);
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by REST", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by REST");
// Then by CLI.
CLICommand cmd = new UpdateNodeCommand();
cmd.setTransportAuth(User.get("admin").impersonate());
assertThat(new CLICommandInvoker(rr.j, cmd).withStdin(new StringInputStream(xml.replace("echo configured by GUI", "echo configured by CLI"))).invokeWithArgs("s"), CLICommandInvoker.Matcher.succeededSilently());
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by CLI", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by CLI");
// Now verify that all modes failed as dev. First as GUI.
s.setLauncher(new CommandLauncher("echo configured by admin"));
s.save();
wc = rr.j.createWebClient().login("dev");
form = wc.getPage(s, "configure").getFormByName("config");
input = form.getInputByName("_.command");
Expand All @@ -104,6 +112,7 @@ public void evaluate() throws Throwable {
}
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertEquals("echo configured by admin", ((CommandLauncher) s.getLauncher()).getCommand());
assertSerialForm(s, "echo configured by admin");
// Then by REST.
req = new WebRequest(wc.createCrumbedUrl(configDotXml), HttpMethod.POST);
req.setEncodingType(null);
Expand All @@ -115,6 +124,7 @@ public void evaluate() throws Throwable {
}
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertNotEquals(CommandLauncher.class, s.getLauncher().getClass()); // currently seems to reset it to JNLPLauncher, whatever
assertSerialForm(s, null);
s.setLauncher(new CommandLauncher("echo configured by admin"));
// Then by CLI.
cmd = new UpdateNodeCommand();
Expand All @@ -123,10 +133,17 @@ public void evaluate() throws Throwable {
CLICommandInvoker.Matcher./* gets swallowed by RobustReflectionConverter, hmm*/succeededSilently());
s = (DumbSlave) rr.j.jenkins.getNode("s");
assertNotEquals(CommandLauncher.class, s.getLauncher().getClass());
assertSerialForm(s, null);
// Now also check that SYSTEM deserialization works after a restart.
s.setLauncher(new CommandLauncher("echo configured by admin"));
s.save();
}
private void assertSerialForm(DumbSlave s, @CheckForNull String expectedCommand) throws IOException {
// cf. private methods in Nodes
File nodesDir = new File(rr.j.jenkins.getRootDir(), "nodes");
XmlFile configXml = new XmlFile(Jenkins.XSTREAM, new File(new File(nodesDir, s.getNodeName()), "config.xml"));
assertThat(configXml.asString(), expectedCommand != null ? containsString("<agentCommand>" + expectedCommand + "</agentCommand>") : not(containsString("<agentCommand>")));
}
});
rr.addStep(new Statement() {
@Override
Expand Down

0 comments on commit 67f68c1

Please sign in to comment.