Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- <workflow.version>1254.v3f64639b_11dd</workflow.version> -->
<findbugs.effort>Max</findbugs.effort>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
</properties>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,35 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import hudson.model.FreeStyleProject;
import hudson.model.User;
import hudson.tasks.Mailer;

import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;

import org.htmlunit.html.HtmlPage;
import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerHelper;
import com.synopsys.arc.jenkins.plugins.ownership.nodes.NodeOwnerHelper;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

public class OwnershipActionTest {
@WithJenkins
class OwnershipActionTest {

@Rule public JenkinsRule j = new JenkinsRule();
private JenkinsRule j;

@BeforeEach
void beforeEach(JenkinsRule rule) {
j = rule;
}

@Test
public void test() throws Exception {
void test() throws Exception {
// There is no particular reason why email value should look like this, but for a user configurable field this is a realistic scenario.
String mail = "\"T&J\" <TnJ@mailinator.com>";
String id = "_T&J_";
Expand All @@ -64,7 +72,7 @@ public void test() throws Exception {
assertThat(job.asXml(), not(containsString("<T&J>")));
// Find anchor by partial href match - HtmlUnit normalizes URLs automatically
// Try with URL-encoded version first (as it appears in HTML)
String encodedId = java.net.URLEncoder.encode(id, "UTF-8");
String encodedId = java.net.URLEncoder.encode(id, StandardCharsets.UTF_8);
String userUrlEncoded = j.getURL() + "user/" + encodedId;
String userUrlPlain = j.getURL() + "user/" + id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
import java.util.Collections;
import jenkins.model.IdStrategy;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;


public class OwnershipDescriptionTest {
@WithJenkins
class OwnershipDescriptionTest {
private static final IdStrategy CASE_SENSITIVE = new IdStrategy.CaseSensitive();

@Rule
public final JenkinsRule j = new JenkinsRule();
private JenkinsRule j;

@Before
public void setUp() throws Exception {
@BeforeEach
void beforeEach(JenkinsRule rule) throws Exception {
j = rule;
applyIdStrategy(CASE_SENSITIVE);
}

Expand All @@ -42,13 +44,13 @@ public IdStrategy getGroupIdStrategy() {
}

@Test
public void isOwnerShouldRespectCaseSensitiveIdStrategy() throws Exception {
void isOwnerShouldRespectCaseSensitiveIdStrategy() {
User user = User.get("owner");

OwnershipDescription description = new OwnershipDescription(true, "owner", Collections.<String>emptyList());
OwnershipDescription description = new OwnershipDescription(true, "owner", Collections.emptyList());
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, false), equalTo(true));

description = new OwnershipDescription(true, "OWNER", Collections.<String>emptyList());
description = new OwnershipDescription(true, "OWNER", Collections.emptyList());
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, false), equalTo(false));

description = new OwnershipDescription(true, "another.owner", Arrays.asList("owner"));
Expand All @@ -59,14 +61,14 @@ public void isOwnerShouldRespectCaseSensitiveIdStrategy() throws Exception {
}

@Test
public void isOwnerShouldRespectCaseInsensitiveIdStrategy() throws Exception {
void isOwnerShouldRespectCaseInsensitiveIdStrategy() throws Exception {
applyIdStrategy(IdStrategy.CASE_INSENSITIVE);
User user = User.get("owner");

OwnershipDescription description = new OwnershipDescription(true, "owner", Collections.<String>emptyList());
OwnershipDescription description = new OwnershipDescription(true, "owner", Collections.emptyList());
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, false), equalTo(true));

description = new OwnershipDescription(true, "OWNER", Collections.<String>emptyList());
description = new OwnershipDescription(true, "OWNER", Collections.emptyList());
assertThat("OwnershipDescription doesn't respect case sensitive strategy", description.isOwner(user, false), equalTo(true));

description = new OwnershipDescription(true, "another.owner", Arrays.asList("owner"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,32 @@

import hudson.model.FreeStyleProject;
import org.jenkinsci.plugins.ownership.model.OwnershipHelperLocator;
import static org.junit.Assert.assertEquals;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Tests for {@link JobOwnerHelper}.
* @author Oleg Nenashev
*/
public class JobOwnerHelperTest {

@Rule
public JenkinsRule j = new JenkinsRule();
@WithJenkins
class JobOwnerHelperTest {

private JenkinsRule j;

@BeforeEach
void beforeEach(JenkinsRule rule) {
j = rule;
}

@Test
public void locatorShouldReturnRightHelperForFolder() throws Exception {
void locatorShouldReturnRightHelperForFolder() throws Exception {
FreeStyleProject folder = j.jenkins.createProject(FreeStyleProject.class, "myFolder");
assertEquals("OwnershipHelperLocator should return the FolderOwnershipHelper instance",
OwnershipHelperLocator.locate(folder), JobOwnerHelper.Instance);

assertEquals(
JobOwnerHelper.Instance, OwnershipHelperLocator.locate(folder), "OwnershipHelperLocator should return the FolderOwnershipHelper instance");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,40 @@
import org.htmlunit.FailingHttpStatusCodeException;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebRequest;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlPage;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipPlugin;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.User;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Security tests for JobOwnerJobAction endpoints.
* Tests CSRF protection and permission checks for SECURITY-2062 fixes.
*/
public class JobOwnerJobActionSecurityTest {
@WithJenkins
class JobOwnerJobActionSecurityTest {

@Rule
public JenkinsRule r = new JenkinsRule();
private JenkinsRule r;

private FreeStyleProject project;

@Before
public void setupSecurity() throws Exception {
@BeforeEach
void beforeEach(JenkinsRule rule) throws Exception{
r = rule;
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());

// Create users explicitly
Expand Down Expand Up @@ -109,7 +108,7 @@ public void setupSecurity() throws Exception {

@Test
@Issue("SECURITY-2062")
public void doOwnersSubmit_requiresPOST() throws Exception {
void doOwnersSubmit_requiresPOST() throws Exception {
WebClient wc = r.createWebClient();
wc.login("admin", "admin");

Expand All @@ -128,7 +127,7 @@ public void doOwnersSubmit_requiresPOST() throws Exception {

@Test
@Issue("SECURITY-2062")
public void doOwnersSubmit_requiresManageOwnershipPermission() throws Exception {
void doOwnersSubmit_requiresManageOwnershipPermission() throws Exception {
WebClient wc = r.createWebClient();

// Try with readonly user - should fail
Expand All @@ -153,7 +152,7 @@ public void doOwnersSubmit_requiresManageOwnershipPermission() throws Exception

@Test
@Issue("SECURITY-2062")
public void doOwnersSubmit_allowsPOSTWithProperPermissions() throws Exception {
void doOwnersSubmit_allowsPOSTWithProperPermissions() throws Exception {
WebClient wc = r.createWebClient();
wc.login("admin", "admin");

Expand All @@ -169,7 +168,7 @@ public void doOwnersSubmit_allowsPOSTWithProperPermissions() throws Exception {
wc.createCrumbedUrl(project.getUrl() + "ownership/ownersSubmit"),
HttpMethod.POST);
req.setAdditionalHeader("Content-Type", "application/x-www-form-urlencoded");
req.setRequestBody("json=" + formData.toString());
req.setRequestBody("json=" + formData);
wc.getPage(req);

// Verify ownership was changed
Expand All @@ -178,7 +177,7 @@ public void doOwnersSubmit_allowsPOSTWithProperPermissions() throws Exception {

@Test
@Issue("SECURITY-2062")
public void doProjectSpecificSecuritySubmit_requiresPOST() throws Exception {
void doProjectSpecificSecuritySubmit_requiresPOST() throws Exception {
WebClient wc = r.createWebClient();
wc.login("admin", "admin");

Expand All @@ -197,7 +196,7 @@ public void doProjectSpecificSecuritySubmit_requiresPOST() throws Exception {

@Test
@Issue("SECURITY-2062")
public void doProjectSpecificSecuritySubmit_requiresManageOwnershipPermission() throws Exception {
void doProjectSpecificSecuritySubmit_requiresManageOwnershipPermission() throws Exception {
WebClient wc = r.createWebClient();

// Try with readonly user - should fail
Expand All @@ -219,7 +218,7 @@ public void doProjectSpecificSecuritySubmit_requiresManageOwnershipPermission()

@Test
@Issue("SECURITY-2062")
public void doRestoreDefaultSpecificSecuritySubmit_requiresPOST() throws Exception {
void doRestoreDefaultSpecificSecuritySubmit_requiresPOST() throws Exception {
WebClient wc = r.createWebClient();
wc.login("admin", "admin");

Expand All @@ -238,7 +237,7 @@ public void doRestoreDefaultSpecificSecuritySubmit_requiresPOST() throws Excepti

@Test
@Issue("SECURITY-2062")
public void doRestoreDefaultSpecificSecuritySubmit_requiresManageOwnershipPermission() throws Exception {
void doRestoreDefaultSpecificSecuritySubmit_requiresManageOwnershipPermission() throws Exception {
WebClient wc = r.createWebClient();

// Try with readonly user - should fail
Expand All @@ -260,7 +259,7 @@ public void doRestoreDefaultSpecificSecuritySubmit_requiresManageOwnershipPermis

@Test
@Issue("SECURITY-2062")
public void configureUser_cannotModifyOwnership() throws Exception {
void configureUser_cannotModifyOwnership() throws Exception {
WebClient wc = r.createWebClient();

// User with CONFIGURE but not MANAGE_ITEMS_OWNERSHIP should not be able to modify ownership
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,29 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

public class JobOwnerJobPropertyTest {
@WithJenkins
class JobOwnerJobPropertyTest {

@Rule
public JenkinsRule r = new JenkinsRule();
private JenkinsRule r;

@Before
public void setupSecurity() {
@BeforeEach
void beforeEach(JenkinsRule rule) {
r = rule;
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
MockAuthorizationStrategy mas = new MockAuthorizationStrategy();
mas.grant(Jenkins.ADMINISTER) // Implies MANAGE_ITEMS_OWNERSHIP.
Expand All @@ -72,7 +73,7 @@ public void setupSecurity() {

@Test
@Issue("SECURITY-498")
public void changeOwnerViaPost() throws Exception {
void changeOwnerViaPost() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.getProperty(JobOwnerJobProperty.class).setOwnershipDescription(new OwnershipDescription(true, "admin", null));

Expand Down Expand Up @@ -103,7 +104,7 @@ public void changeOwnerViaPost() throws Exception {

@Test
@Issue("SECURITY-498")
public void changeOwnerViaCLI() throws Exception {
void changeOwnerViaCLI() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.getProperty(JobOwnerJobProperty.class).setOwnershipDescription(new OwnershipDescription(true, "admin", null));

Expand Down
Loading
Loading