Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Bouncy Castle usage from Tests #984

Merged
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
14 changes: 10 additions & 4 deletions src/itest/java/com/hierynomus/sshj/sftp/FileWriteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.shaded.org.bouncycastle.util.Arrays;

import com.hierynomus.sshj.SshdContainer;

Expand All @@ -31,11 +30,12 @@
import net.schmizz.sshj.sftp.SFTPClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;

@Testcontainers
public class FileWriteTest {
@Container
private static SshdContainer sshd = new SshdContainer();
private static final SshdContainer sshd = new SshdContainer();

@Test
public void shouldAppendToFile_GH390() throws Throwable {
Expand Down Expand Up @@ -63,8 +63,14 @@ public void shouldAppendToFile_GH390() throws Throwable {
try (RemoteFile read = sftp.open(file, EnumSet.of(OpenMode.READ))) {
byte[] readBytes = new byte[initialText.length + appendText.length];
read.read(0, readBytes, 0, readBytes.length);
assertThat(Arrays.copyOfRange(readBytes, 0, initialText.length)).isEqualTo(initialText);
assertThat(Arrays.copyOfRange(readBytes, initialText.length, initialText.length + appendText.length)).isEqualTo(appendText);

final byte[] expectedInitialText = new byte[initialText.length];
System.arraycopy(readBytes, 0, expectedInitialText, 0, expectedInitialText.length);
assertArrayEquals(expectedInitialText, initialText);

final byte[] expectedAppendText = new byte[appendText.length];
System.arraycopy(readBytes, initialText.length, expectedAppendText, 0, expectedAppendText.length);
assertArrayEquals(expectedAppendText, appendText);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.SSHPacket;
import net.schmizz.sshj.transport.cipher.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.security.Security;
import java.util.Arrays;
import java.util.stream.Stream;

Expand All @@ -42,11 +39,6 @@ public static Stream<Arguments> sets() {
return Stream.of(Arguments.of("mina-sshd", 3), Arguments.of("openssh", 4));
}

@BeforeAll
public static void setupBeforeClass() {
Security.addProvider(new BouncyCastleProvider());
}

@ParameterizedTest
@MethodSource("sets")
public void testDecryptPacket(String ssh, int nr) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
package com.hierynomus.sshj.transport.verification;

import net.schmizz.sshj.common.Buffer;
import net.schmizz.sshj.common.SecurityUtils;
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts;
import net.schmizz.sshj.util.KeyUtil;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -41,11 +39,6 @@ public class OpenSSHKnownHostsTest {
@TempDir
public File tempDir;

@BeforeAll
public static void setup() {
SecurityUtils.registerSecurityProvider("org.bouncycastle.jce.provider.BouncyCastleProvider");
}

@Test
public void shouldParseAndVerifyHashedHostEntry() throws Exception {
File knownHosts = knownHosts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
import net.schmizz.sshj.common.SSHException;
import net.schmizz.sshj.common.SSHPacket;
import net.schmizz.sshj.transport.cipher.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.security.SecureRandom;
import java.security.Security;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -49,7 +47,6 @@ public class DecoderDecryptGcmCipherSshPacketTest {

@BeforeEach
public void setUp() throws Exception {
Security.addProvider(new BouncyCastleProvider());
ClassLoader classLoader = DecoderDecryptGcmCipherSshPacketTest.class.getClassLoader();
iv = IOUtils.readFully(classLoader.getResourceAsStream("ssh-packets/gcm/mina-sshd/s2c.iv.bin" )).toByteArray();
key = IOUtils.readFully(classLoader.getResourceAsStream("ssh-packets/gcm/mina-sshd/s2c.key.bin" )).toByteArray();
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/net/schmizz/sshj/transport/mac/BaseMacTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,29 @@

import com.hierynomus.sshj.transport.mac.Macs;
import net.schmizz.sshj.common.SSHRuntimeException;
import org.bouncycastle.util.encoders.Hex;
import org.apache.sshd.common.util.buffer.BufferUtils;
import org.junit.jupiter.api.Test;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

public class BaseMacTest {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final Charset CHARSET = StandardCharsets.US_ASCII;
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "24ddeed57ad91465c5b59dce74ef73778bfb0cb9";
private static final String EXPECTED_HMAC = "24 dd ee d5 7a d9 14 65 c5 b5 9d ce 74 ef 73 77 8b fb 0c b9";
private static final String KEY = "et1Quo5ooCie6theel8i";

@Test
public void testResizeTooBigKeys() {
BaseMAC hmac = Macs.HMACSHA1().create();
hmac.init((KEY + "foo").getBytes(CHARSET));
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
Expand All @@ -54,7 +55,7 @@ public void testUnknownAlgorithm() {
public void testUpdateWithDoFinal() {
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
Expand All @@ -67,13 +68,13 @@ public void testUpdateWithRange() {

// update with the range from the second to penultimate byte
hmac.update(plainText, 1, PLAIN_TEXT.length);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}

@Test
Expand All @@ -82,7 +83,7 @@ public void testUpdateWithDoFinalWithResultBuffer() {
byte[] resultBuf = new byte[20];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(resultBuf), is(EXPECTED_HMAC));
}

private BaseMAC initHmac() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@
package net.schmizz.sshj.transport.mac;

import com.hierynomus.sshj.transport.mac.Macs;
import org.bouncycastle.util.encoders.Hex;
import org.apache.sshd.common.util.buffer.BufferUtils;
import org.junit.jupiter.api.Test;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HMACMD596Test {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final Charset CHARSET = StandardCharsets.US_ASCII;
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "dff33c507463f9cf088a5ce8";
private static final String EXPECTED_HMAC = "df f3 3c 50 74 63 f9 cf 08 8a 5c e8";

@Test
public void testUpdateWithDoFinal() {
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)),
assertThat(BufferUtils.toHex(hmac.doFinal(PLAIN_TEXT)),
is(EXPECTED_HMAC));
}

Expand All @@ -49,7 +50,7 @@ public void testUpdateWithDoFinalWithResultBuffer() {
byte[] resultBuf = new byte[12];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(resultBuf), is(EXPECTED_HMAC));
}

private BaseMAC initHmac() {
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/net/schmizz/sshj/transport/mac/HMACMD5Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@
package net.schmizz.sshj.transport.mac;

import com.hierynomus.sshj.transport.mac.Macs;
import org.bouncycastle.util.encoders.Hex;
import org.apache.sshd.common.util.buffer.BufferUtils;
import org.junit.jupiter.api.Test;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HMACMD5Test {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final Charset CHARSET = StandardCharsets.US_ASCII;
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "dff33c507463f9cf088a5ce8d969c386";
private static final String EXPECTED_HMAC = "df f3 3c 50 74 63 f9 cf 08 8a 5c e8 d9 69 c3 86";

@Test
public void testUpdateWithDoFinal() {
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}

@Test
Expand All @@ -48,7 +49,7 @@ public void testUpdateWithDoFinalWithResultBuffer() {
byte[] resultBuf = new byte[16];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(resultBuf), is(EXPECTED_HMAC));
}

private BaseMAC initHmac() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@
package net.schmizz.sshj.transport.mac;

import com.hierynomus.sshj.transport.mac.Macs;
import org.bouncycastle.util.encoders.Hex;
import org.apache.sshd.common.util.buffer.BufferUtils;
import org.junit.jupiter.api.Test;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HMACSHA196Test {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final Charset CHARSET = StandardCharsets.US_ASCII;
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "24ddeed57ad91465c5b59dce";
private static final String EXPECTED_HMAC = "24 dd ee d5 7a d9 14 65 c5 b5 9d ce";

@Test
public void testUpdateWithDoFinal() {
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}

@Test
Expand All @@ -48,7 +49,7 @@ public void testUpdateWithDoFinalWithResultBuffer() {
byte[] resultBuf = new byte[12];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(resultBuf), is(EXPECTED_HMAC));
}

private BaseMAC initHmac() {
Expand Down
13 changes: 7 additions & 6 deletions src/test/java/net/schmizz/sshj/transport/mac/HMACSHA1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@
package net.schmizz.sshj.transport.mac;

import com.hierynomus.sshj.transport.mac.Macs;
import org.bouncycastle.util.encoders.Hex;
import org.apache.sshd.common.util.buffer.BufferUtils;
import org.junit.jupiter.api.Test;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class HMACSHA1Test {
private static final Charset CHARSET = Charset.forName("US-ASCII");
private static final Charset CHARSET = StandardCharsets.US_ASCII;
private static final byte[] PLAIN_TEXT = "Hello World".getBytes(CHARSET);
private static final String EXPECTED_HMAC = "24ddeed57ad91465c5b59dce74ef73778bfb0cb9";
private static final String EXPECTED_HMAC = "24 dd ee d5 7a d9 14 65 c5 b5 9d ce 74 ef 73 77 8b fb 0c b9";

@Test
public void testUpdateWithDoFinal() {
BaseMAC hmac = initHmac();
hmac.update(PLAIN_TEXT);
assertThat(Hex.toHexString(hmac.doFinal()), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal()), is(EXPECTED_HMAC));
}

@Test
public void testDoFinalWithInput() {
BaseMAC hmac = initHmac();
assertThat(Hex.toHexString(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(hmac.doFinal(PLAIN_TEXT)), is(EXPECTED_HMAC));
}

@Test
Expand All @@ -48,7 +49,7 @@ public void testUpdateWithDoFinalWithResultBuffer() {
byte[] resultBuf = new byte[20];
hmac.update(PLAIN_TEXT);
hmac.doFinal(resultBuf, 0);
assertThat(Hex.toHexString(resultBuf), is(EXPECTED_HMAC));
assertThat(BufferUtils.toHex(resultBuf), is(EXPECTED_HMAC));
}

private BaseMAC initHmac() {
Expand Down
Loading