Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-9277 JENKINS-25448] Reject invalid axis name/value at …
…the time of configuration
  • Loading branch information
olivergondza committed Jan 11, 2015
1 parent f106844 commit 9ea857b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 14 deletions.
22 changes: 9 additions & 13 deletions src/main/java/hudson/matrix/AxisDescriptor.java
Expand Up @@ -26,7 +26,6 @@
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.Failure;
import hudson.model.Messages;
import jenkins.model.Jenkins;
import hudson.util.FormValidation;

Expand Down Expand Up @@ -60,15 +59,10 @@ public boolean isInstantiable() {
*/
public FormValidation doCheckName(@QueryParameter String value) {
if(Util.fixEmpty(value)==null)
return FormValidation.ok();

if (value.contains(",")) return FormValidation.error(
Messages.Hudson_UnsafeChar(',')
);
return FormValidation.error(Messages.AxisDescriptor_EmptyAxisName());

if (value.contains("=")) return FormValidation.error(
Messages.Hudson_UnsafeChar('=')
);
if (value.contains(",")) return unsafeChar(',');
if (value.contains("=")) return unsafeChar('=');

try {
Jenkins.checkGoodName(value);
Expand All @@ -90,11 +84,9 @@ public FormValidation doCheckName(@QueryParameter String value) {
*/
public FormValidation checkValue(@QueryParameter String value) {
if(Util.fixEmpty(value)==null)
return FormValidation.ok();
return FormValidation.error(Messages.AxisDescriptor_EmptyAxisName());

if (value.contains(",")) return FormValidation.error(
Messages.Hudson_UnsafeChar(',')
);
if (value.contains(",")) return unsafeChar(',');

try {
Jenkins.checkGoodName(value);
Expand All @@ -103,4 +95,8 @@ public FormValidation checkValue(@QueryParameter String value) {
return FormValidation.error(e.getMessage());
}
}

private FormValidation unsafeChar(char chr) {
return FormValidation.error(hudson.model.Messages.Hudson_UnsafeChar(chr));
}
}
4 changes: 3 additions & 1 deletion src/main/java/hudson/matrix/JDKAxis.java
Expand Up @@ -25,9 +25,11 @@

import hudson.Extension;
import jenkins.model.Jenkins;

import org.kohsuke.stapler.DataBoundConstructor;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -46,7 +48,7 @@ public JDKAxis(List<String> values) {

@DataBoundConstructor
public JDKAxis(String[] values) {
super("jdk", Arrays.asList(values));
super("jdk", values == null ? Collections.<String>emptyList() : Arrays.asList(values));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/hudson/matrix/LabelExpAxis.java
Expand Up @@ -90,6 +90,7 @@ public static List<String> getExprValues(String valuesString){
for(String expr: exprs){
expressions.add(Util.fixEmptyAndTrim(expr));
}
expressions.remove(null); // Empty / whitespace-only lines
return expressions;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/hudson/matrix/Messages.properties
Expand Up @@ -42,3 +42,4 @@ JDKAxis.DisplayName=JDK
LabelAxis.DisplayName=Slaves
LabelExpAxis.DisplayName=Label expression
TextArea.DisplayName=User-defined Axis
AxisDescriptor.EmptyAxisName=Axis name can not be empty
113 changes: 113 additions & 0 deletions src/test/java/hudson/matrix/AxisTest.java
@@ -0,0 +1,113 @@
/*
* The MIT License
*
* Copyright (c) 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.matrix;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

import hudson.model.JDK;

import org.hamcrest.collection.IsEmptyCollection;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;

import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class AxisTest {

public @Rule JenkinsRule j = new JenkinsRule();

private MatrixProject p;
private WebClient wc;

@Before
public void setUp() throws Exception {
wc = j.createWebClient();
p = j.createMatrixProject();

// Setup to make all axes available
j.jenkins.getJDKs().add(new JDK("jdk1.7", "/fake/home"));
j.createSlave();
}

@Test
public void submitEmptyAxisName() throws Exception {
wc.setThrowExceptionOnFailingStatusCode(false);

final String expectedMsg = "Matrix axis name '' is invalid: Axis name can not be empty";
assertFailedWith(emptyName("User-defined Axis"), expectedMsg);
assertFailedWith(emptyName("Slaves"), expectedMsg);
assertFailedWith(emptyName("Label expression"), expectedMsg);
//assertFailedWith(emptyName("JDK"), expectedMsg); // No "name" attribute
}

private HtmlPage emptyName(String axis) throws Exception {
HtmlForm form = addAxis(axis);
form.getInputByName("_.name").setValueAttribute("");
HtmlPage ret = j.submit(form);
return ret;
}

@Test
public void emptyAxisValueListResultInNoConfigurations() throws Exception {
emptyValue("User-defined Axis");
emptyValue("Slaves");
emptyValue("Label expression");
emptyValue("JDK");

MatrixBuild build = j.buildAndAssertSuccess(p);
assertThat(build.getRuns(), new IsEmptyCollection<MatrixRun>());
assertThat(p.getItems(), new IsEmptyCollection<MatrixConfiguration>());
}

private HtmlPage emptyValue(String axis) throws Exception {
HtmlForm form = addAxis(axis);
if (!"JDK".equals(axis)) { // No "name" attribute
form.getInputByName("_.name").setValueAttribute("some_name");
}

HtmlPage ret = j.submit(form);
return ret;
}

private void assertFailedWith(HtmlPage res, String expected) {
String actual = res.getWebResponse().getContentAsString();

assertThat(actual, res.getWebResponse().getStatusCode(), equalTo(400));
assertThat(actual, containsString(expected));
}

private HtmlForm addAxis(String axis) throws Exception {
HtmlPage page = wc.getPage(p, "configure");
HtmlForm form = page.getFormByName("config");
form.getButtonByCaption("Add axis").click();
page.getAnchorByText(axis).click();
return form;
}
}

0 comments on commit 9ea857b

Please sign in to comment.