Skip to content
Permalink
Browse files
[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 9ea857b279b86dd9c27268ffddad1359b3df8e27
@@ -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;

@@ -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);
@@ -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);
@@ -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));
}
}
@@ -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;

/**
@@ -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
@@ -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;
}

@@ -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
@@ -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.