Skip to content

Commit

Permalink
Add smoke test for real plexus-sec-dispatcher DefaultSecDispatcher use
Browse files Browse the repository at this point in the history
  • Loading branch information
iils-hwellmann committed Dec 9, 2024
1 parent 724409a commit c626db4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.maven.plugins.jarsigner;

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -42,6 +43,8 @@
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor;
import org.mockito.hamcrest.MockitoHamcrest;
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;

import static org.apache.maven.plugins.jarsigner.TestJavaToolResults.RESULT_ERROR;
import static org.apache.maven.plugins.jarsigner.TestJavaToolResults.RESULT_OK;
Expand Down Expand Up @@ -462,4 +465,26 @@ public void testLoggingVerboseFalse() throws Exception {
verify(log, times(1)).debug(contains("Processing "));
verify(log, times(1)).info(contains("1 archive(s) processed"));
}

@Test
public void testDefaultSecDispatcher() throws Exception {
String pw = "trivialPW";
String secrectText = "my-sigrid";
File securitySettings = folder.newFile("settings-security.xml");
Files.write(
securitySettings.toPath(),
Arrays.asList( //
"<settingsSecurity>", //
"<master>{tZdWvqoeiY0HNlIBiQwn5a+gsv7v0FVhjlrAqz6Q6Yc=}</master>", //
"</settingsSecurity>"));

DefaultPlexusCipher cypher = new DefaultPlexusCipher();
DefaultSecDispatcher dispatcher = new DefaultSecDispatcher();
dispatcher.setConfigurationFile(securitySettings.toString());
new MojoTestCreator<>(DefaultSecDispatcher.class).setAttribute(dispatcher, "_cipher", cypher);

String encrypted = cypher.decorate(cypher.encrypt(secrectText, pw));
String decrypted = dispatcher.decrypt(encrypted);
assertEquals(secrectText, decrypted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class MojoTestCreator<T extends AbstractJarsignerMojo> {
private SecDispatcher securityDispatcher;
private WaitStrategy waitStrategy;
private Log log;
private List<Field> fields;
private final List<Field> fields;

public MojoTestCreator(Class<T> clazz, MavenProject project, File projectDir, JarSigner jarSigner)
throws Exception {
Expand All @@ -67,6 +67,14 @@ public MojoTestCreator(Class<T> clazz, MavenProject project, File projectDir, Ja
fields = getAllFields(clazz);
}

public MojoTestCreator(Class<?> clazz) {
this.fields = getAllFields(clazz);
this.clazz = null;
this.project = null;
this.projectDir = null;
this.jarSigner = null;
}

public void setToolchainManager(ToolchainManager toolchainManager) {
this.toolchainManager = toolchainManager;
}
Expand Down Expand Up @@ -149,7 +157,7 @@ private Field getField(Object instance, String fieldName) {
+ instance.getClass().getName()));
}

private void setAttribute(Object instance, String fieldName, Object value) throws Exception {
void setAttribute(Object instance, String fieldName, Object value) throws Exception {
Field field = getField(instance, fieldName);
field.setAccessible(true);
field.set(instance, value);
Expand Down

0 comments on commit c626db4

Please sign in to comment.