Skip to content
Merged
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 @@ -34,6 +34,7 @@
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<spotless.check.skip>false</spotless.check.skip>
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
<hpi.strictBundledArtifacts>true</hpi.strictBundledArtifacts>
</properties>
<dependencyManagement>
Expand Down
41 changes: 20 additions & 21 deletions src/test/java/com/nowsecure/models/NowSecureBinaryTest.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package com.nowsecure.models;

import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

import hudson.FilePath;
import java.io.File;
import java.nio.file.Paths;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

@WithJenkins
class NowSecureBinaryTests {
final String arch = System.getProperty("os.arch");
final String osName = System.getProperty("os.name");
class NowSecureBinaryTest {

private static final String ARCH = System.getProperty("os.arch");
private static final String OS_NAME = System.getProperty("os.name");

@CsvSource({"x8664", "amd64", "ia32e", "em64t", "x64"})
@ParameterizedTest
void shouldReturnAmd64ForAll64BitArchitectures_linux(String arch) throws Exception {
var actual = NowSecureBinary.getToolName(arch, "linux");
Assertions.assertEquals("ns_linux-amd64", actual, "Not matching");
assertEquals("ns_linux-amd64", actual, "Not matching");
}

@CsvSource({"x8664", "amd64", "ia32e", "em64t", "x64"})
@ParameterizedTest
void shouldReturnAmd64ForAll64BitArchitectures_windows(String arch) throws Exception {
var actual = NowSecureBinary.getToolName(arch, "windows");
Assertions.assertEquals("ns_windows-amd64.exe", actual, "Not matching");
assertEquals("ns_windows-amd64.exe", actual, "Not matching");
}

@CsvSource({
Expand All @@ -38,40 +38,39 @@ void shouldReturnAmd64ForAll64BitArchitectures_windows(String arch) throws Excep
"arm64,windows",
})
@ParameterizedTest
void shouldFailWhenGivenInvalidArchitecutrePlatformCombo(String arch, String platform) throws Exception {
void shouldFailWhenGivenInvalidArchitecturePlatformCombo(String arch, String platform) throws Exception {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
NowSecureBinary.getToolName(arch, platform);
});
Assertions.assertTrue(exception.getMessage().contains("Unsupported platform / architecture"));
assertTrue(exception.getMessage().contains("Unsupported platform / architecture"));
}

@Test
void shouldTrackTokenIndeces() throws Exception {
void shouldTrackTokenIndices() throws Exception {
var resourceDir = this.getClass().getClassLoader().getResource("./");
var nsb = new NowSecureBinary(arch, osName, new FilePath(new File(resourceDir.getPath())));
Assertions.assertEquals(List.of(), nsb.maskedIndices, "Initial masked indeces list should be empty");
var nsb = new NowSecureBinary(ARCH, OS_NAME, new FilePath(new File(resourceDir.getPath())));
assertEquals(List.of(), nsb.maskedIndices, "Initial masked indices list should be empty");

nsb.addArgument("some-argument").addToken("some-token").addArgument("double", "argument");
Assertions.assertEquals(List.of(3), nsb.maskedIndices);
assertEquals(List.of(3), nsb.maskedIndices);

nsb.addToken("new-token");
Assertions.assertEquals(List.of(3, 7), nsb.maskedIndices);
Assertions.assertEquals(nsb.arguments.size(), 8);
assertEquals(List.of(3, 7), nsb.maskedIndices);
assertEquals(8, nsb.arguments.size());
}

@Test
void shouldAddToolPathToProcessArgumentList() throws Exception {
var resourceDir = this.getClass().getClassLoader().getResource("./");
var nsb = new NowSecureBinary(arch, osName, new FilePath(new File(resourceDir.getPath())));
var toolName = NowSecureBinary.getToolName(arch, osName);
var nsb = new NowSecureBinary(ARCH, OS_NAME, new FilePath(new File(resourceDir.getPath())));
var toolName = NowSecureBinary.getToolName(ARCH, OS_NAME);

var constructedToolPath = nsb.toolPath.getRemote();

Assertions.assertEquals(
String.format("%s%s%s", Paths.get(resourceDir.toURI()).toString(), File.separator, toolName),
assertEquals(
String.format("%s%s%s", Paths.get(resourceDir.toURI()), File.separator, toolName),
constructedToolPath,
"Tool path does not look like it should");
Assertions.assertEquals(
List.of(constructedToolPath), nsb.arguments, "Tool path not properly added to arguments list");
assertEquals(List.of(constructedToolPath), nsb.arguments, "Tool path not properly added to arguments list");
}
}
28 changes: 14 additions & 14 deletions src/test/java/com/nowsecure/plugin/NowSecurePluginTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nowsecure.plugin;

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

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
Expand All @@ -14,7 +15,6 @@
import hudson.util.Secret;
import java.io.IOException;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
Expand All @@ -24,10 +24,10 @@
@WithJenkins
class NowSecurePluginTest {

final String binaryFilePath = "./";
final String group = "group";
private static final String BINARY_FILE_PATH = "./";
private static final String GROUP = "group";

public void setupCredentials(JenkinsRule jenkins, String secretId, String secretText) throws IOException {
private void setupCredentials(JenkinsRule jenkins, String secretId, String secretText) throws IOException {
var secret = Secret.fromString(secretText);
StringCredentialsImpl credential =
new StringCredentialsImpl(CredentialsScope.GLOBAL, secretId, "Test plain text credential", secret);
Expand All @@ -39,7 +39,7 @@ public void setupCredentials(JenkinsRule jenkins, String secretId, String secret
@Test
void invalidCredentialIdShouldFail(JenkinsRule jenkins) throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();
var builder = new NowSecurePlugin(binaryFilePath, group, "bad token credential id");
var builder = new NowSecurePlugin(BINARY_FILE_PATH, GROUP, "bad token credential id");
project.getBuildersList().add(builder);
var build = jenkins.buildAndAssertStatus(Result.FAILURE, project);
jenkins.assertLogContains("Could not find a TextCredential matching the specified credentialId", build);
Expand All @@ -51,7 +51,7 @@ void validCredentialIdShouldSucceed_unix(JenkinsRule jenkins) throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();
var id = "some-id";
setupCredentials(jenkins, id, "some-text");
var builder = new NowSecurePlugin(binaryFilePath, group, id);
var builder = new NowSecurePlugin(BINARY_FILE_PATH, GROUP, id);
project.getBuildersList().add(builder);
var build = jenkins.buildAndAssertStatus(Result.SUCCESS, project);
jenkins.assertLogNotContains("Could not find a TextCredential matching the specified credentialId", build);
Expand All @@ -65,7 +65,7 @@ void validCredentialIdShouldSucceed_windows(JenkinsRule jenkins) throws Exceptio
FreeStyleProject project = jenkins.createFreeStyleProject();
var id = "some-id";
setupCredentials(jenkins, id, "some-text");
var builder = new NowSecurePlugin(binaryFilePath, group, id);
var builder = new NowSecurePlugin(BINARY_FILE_PATH, GROUP, id);
project.getBuildersList().add(builder);
var build = jenkins.buildAndAssertStatus(Result.FAILURE, project);
jenkins.assertLogNotContains("Could not find a TextCredential matching the specified credentialId", build);
Expand All @@ -75,24 +75,24 @@ void validCredentialIdShouldSucceed_windows(JenkinsRule jenkins) throws Exceptio
void shouldAllowUsersToOverrideURLs(JenkinsRule jenkins) throws Exception {
var id = "some-id";
setupCredentials(jenkins, id, "some-text");
var nsStep = new NowSecurePlugin(binaryFilePath, group, id);
var nsStep = new NowSecurePlugin(BINARY_FILE_PATH, GROUP, id);

var apiUrl = "https://httpbin.org/api";
var uiUrl = "https://httpbin.org/ui";

nsStep.setApiHost(apiUrl);
nsStep.setUiHost(uiUrl);

Assertions.assertEquals(apiUrl, nsStep.getApiHost());
Assertions.assertEquals(uiUrl, nsStep.getUiHost());
assertEquals(apiUrl, nsStep.getApiHost());
assertEquals(uiUrl, nsStep.getUiHost());
}

@Test
void shouldPersistUserConfigs(JenkinsRule jenkins) throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();
var id = "some-id";
setupCredentials(jenkins, id, "some-text");
var nsStep = new NowSecurePlugin(binaryFilePath, group, id);
var nsStep = new NowSecurePlugin(BINARY_FILE_PATH, GROUP, id);

var apiUrl = "https://httpbin.org/api";
var uiUrl = "https://httpbin.org/ui";
Expand All @@ -117,8 +117,8 @@ void shouldPersistUserConfigs(JenkinsRule jenkins) throws Exception {
assertNotNull(reloadedStep);

// Data bound constructor fields
assertEquals(binaryFilePath, reloadedStep.getBinaryFilePath());
assertEquals(group, reloadedStep.getGroup());
assertEquals(BINARY_FILE_PATH, reloadedStep.getBinaryFilePath());
assertEquals(GROUP, reloadedStep.getGroup());
assertEquals(id, reloadedStep.getTokenCredentialId());
// Data bound setter fields
assertEquals(apiUrl, reloadedStep.getApiHost());
Expand Down