Skip to content

Commit

Permalink
Use slfj4-test instead of logback for unit & integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
norrisjeremy committed Jan 6, 2023
1 parent 3f88c10 commit 667393d
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 360 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.4</version>
<groupId>com.github.valfirst</groupId>
<artifactId>slf4j-test</artifactId>
<version>2.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -639,4 +639,4 @@
</build>
</profile>
</profiles>
</project>
</project>
65 changes: 21 additions & 44 deletions src/test/java/com/jcraft/jsch/Algorithms2IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import static org.junit.jupiter.api.condition.JRE.JAVA_11;
import static org.junit.jupiter.api.condition.JRE.JAVA_15;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import com.github.valfirst.slf4jtest.LoggingEvent;
import com.github.valfirst.slf4jtest.TestLogger;
import com.github.valfirst.slf4jtest.TestLoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
Expand All @@ -19,7 +19,6 @@
import java.util.Optional;
import java.util.Random;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -28,7 +27,6 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.images.builder.ImageFromDockerfile;
Expand All @@ -41,15 +39,14 @@ public class Algorithms2IT {
// Python can be slow for DH group 18
private static final int timeout = 10000;
private static final DigestUtils sha256sum = new DigestUtils(DigestUtils.getSha256Digest());
private static final ListAppender<ILoggingEvent> jschAppender = getListAppender(JSch.class);
private static final ListAppender<ILoggingEvent> sshdAppender =
getListAppender(AlgorithmsIT.class);

@TempDir public Path tmpDir;
private Path in;
private Path out;
private String hash;
private Slf4jLogConsumer sshdLogConsumer;
private TestLogger jschLogger = TestLoggerFactory.getTestLogger(JSch.class);
private TestLogger sshdLogger = TestLoggerFactory.getTestLogger(getClass());

@Container
public GenericContainer<?> sshd =
Expand Down Expand Up @@ -86,7 +83,7 @@ public static void beforeAll() {
@BeforeEach
public void beforeEach() throws IOException {
if (sshdLogConsumer == null) {
sshdLogConsumer = new Slf4jLogConsumer(LoggerFactory.getLogger(Algorithms2IT.class));
sshdLogConsumer = new Slf4jLogConsumer(sshdLogger);
sshd.followOutput(sshdLogConsumer);
}

Expand All @@ -102,18 +99,8 @@ public void beforeEach() throws IOException {
}
hash = sha256sum.digestAsHex(in);

jschAppender.list.clear();
sshdAppender.list.clear();
jschAppender.start();
sshdAppender.start();
}

@AfterEach
public void afterEach() {
jschAppender.stop();
sshdAppender.stop();
jschAppender.list.clear();
sshdAppender.list.clear();
jschLogger.clearAll();
sshdLogger.clearAll();
}

@Test
Expand Down Expand Up @@ -199,7 +186,8 @@ public void testDHGEXSizes(String kex, String size) throws Exception {
doSftp(session, true);

String expectedKex = String.format("kex: algorithm: %s.*", kex);
String expectedSizes = String.format("SSH_MSG_KEX_DH_GEX_REQUEST\\(%s<%s<%s\\) sent", size, size, size);
String expectedSizes =
String.format("SSH_MSG_KEX_DH_GEX_REQUEST\\(%s<%s<%s\\) sent", size, size, size);
checkLogs(expectedKex);
checkLogs(expectedSizes);
}
Expand Down Expand Up @@ -265,11 +253,7 @@ public void testRSA(String keyType) throws Exception {
}

@ParameterizedTest
@CsvSource(
value = {
"seed-cbc@ssh.com,none",
"seed-cbc@ssh.com,zlib@openssh.com"
})
@CsvSource(value = {"seed-cbc@ssh.com,none", "seed-cbc@ssh.com,zlib@openssh.com"})
public void testCiphers(String cipher, String compression) throws Exception {
JSch ssh = createRSAIdentity();
Session session = createSession(ssh);
Expand Down Expand Up @@ -390,8 +374,6 @@ private void doSftp(Session session, boolean debugException) throws Exception {
sftp.get("/root/test", out.toString());
sftp.disconnect();
session.disconnect();
jschAppender.stop();
sshdAppender.stop();
} catch (Exception e) {
if (debugException) {
printInfo();
Expand All @@ -403,20 +385,22 @@ private void doSftp(Session session, boolean debugException) throws Exception {
assertEquals(hash, sha256sum.digestAsHex(out));
}

private static void printInfo() {
jschAppender.stop();
sshdAppender.stop();
jschAppender.list.stream().map(ILoggingEvent::getFormattedMessage).forEach(System.out::println);
sshdAppender.list.stream().map(ILoggingEvent::getFormattedMessage).forEach(System.out::println);
private void printInfo() {
jschLogger.getAllLoggingEvents().stream()
.map(LoggingEvent::getFormattedMessage)
.forEach(System.out::println);
sshdLogger.getAllLoggingEvents().stream()
.map(LoggingEvent::getFormattedMessage)
.forEach(System.out::println);
System.out.println("");
System.out.println("");
System.out.println("");
}

private static void checkLogs(String expected) {
private void checkLogs(String expected) {
Optional<String> actualJsch =
jschAppender.list.stream()
.map(ILoggingEvent::getFormattedMessage)
jschLogger.getAllLoggingEvents().stream()
.map(LoggingEvent::getFormattedMessage)
.filter(msg -> msg.matches(expected))
.findFirst();
try {
Expand All @@ -430,11 +414,4 @@ private static void checkLogs(String expected) {
private String getResourceFile(String fileName) {
return ResourceUtil.getResourceFile(getClass(), fileName);
}

private static ListAppender<ILoggingEvent> getListAppender(Class<?> clazz) {
Logger logger = (Logger) LoggerFactory.getLogger(clazz);
ListAppender<ILoggingEvent> listAppender = new ListAppender2<>();
logger.addAppender(listAppender);
return listAppender;
}
}
56 changes: 18 additions & 38 deletions src/test/java/com/jcraft/jsch/Algorithms3IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import com.github.valfirst.slf4jtest.LoggingEvent;
import com.github.valfirst.slf4jtest.TestLogger;
import com.github.valfirst.slf4jtest.TestLoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
Expand All @@ -17,13 +17,11 @@
import java.util.Optional;
import java.util.Random;
import org.apache.commons.codec.digest.DigestUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.images.builder.ImageFromDockerfile;
Expand All @@ -35,15 +33,14 @@ public class Algorithms3IT {

private static final int timeout = 2000;
private static final DigestUtils sha256sum = new DigestUtils(DigestUtils.getSha256Digest());
private static final ListAppender<ILoggingEvent> jschAppender = getListAppender(JSch.class);
private static final ListAppender<ILoggingEvent> sshdAppender =
getListAppender(AlgorithmsIT.class);

@TempDir public Path tmpDir;
private Path in;
private Path out;
private String hash;
private Slf4jLogConsumer sshdLogConsumer;
private TestLogger jschLogger = TestLoggerFactory.getTestLogger(JSch.class);
private TestLogger sshdLogger = TestLoggerFactory.getTestLogger(getClass());

@Container
public GenericContainer<?> sshd =
Expand All @@ -62,7 +59,7 @@ public static void beforeAll() {
@BeforeEach
public void beforeEach() throws IOException {
if (sshdLogConsumer == null) {
sshdLogConsumer = new Slf4jLogConsumer(LoggerFactory.getLogger(Algorithms3IT.class));
sshdLogConsumer = new Slf4jLogConsumer(sshdLogger);
sshd.followOutput(sshdLogConsumer);
}

Expand All @@ -78,18 +75,8 @@ public void beforeEach() throws IOException {
}
hash = sha256sum.digestAsHex(in);

jschAppender.list.clear();
sshdAppender.list.clear();
jschAppender.start();
sshdAppender.start();
}

@AfterEach
public void afterEach() {
jschAppender.stop();
sshdAppender.stop();
jschAppender.list.clear();
sshdAppender.list.clear();
jschLogger.clearAll();
sshdLogger.clearAll();
}

@ParameterizedTest
Expand Down Expand Up @@ -146,8 +133,6 @@ private void doSftp(Session session, boolean debugException) throws Exception {
sftp.get("/root/test", out.toString());
sftp.disconnect();
session.disconnect();
jschAppender.stop();
sshdAppender.stop();
} catch (Exception e) {
if (debugException) {
printInfo();
Expand All @@ -159,20 +144,22 @@ private void doSftp(Session session, boolean debugException) throws Exception {
assertEquals(hash, sha256sum.digestAsHex(out));
}

private static void printInfo() {
jschAppender.stop();
sshdAppender.stop();
jschAppender.list.stream().map(ILoggingEvent::getFormattedMessage).forEach(System.out::println);
sshdAppender.list.stream().map(ILoggingEvent::getFormattedMessage).forEach(System.out::println);
private void printInfo() {
jschLogger.getAllLoggingEvents().stream()
.map(LoggingEvent::getFormattedMessage)
.forEach(System.out::println);
sshdLogger.getAllLoggingEvents().stream()
.map(LoggingEvent::getFormattedMessage)
.forEach(System.out::println);
System.out.println("");
System.out.println("");
System.out.println("");
}

private static void checkLogs(String expected) {
private void checkLogs(String expected) {
Optional<String> actualJsch =
jschAppender.list.stream()
.map(ILoggingEvent::getFormattedMessage)
jschLogger.getAllLoggingEvents().stream()
.map(LoggingEvent::getFormattedMessage)
.filter(msg -> msg.matches(expected))
.findFirst();
try {
Expand All @@ -186,11 +173,4 @@ private static void checkLogs(String expected) {
private String getResourceFile(String fileName) {
return ResourceUtil.getResourceFile(getClass(), fileName);
}

private static ListAppender<ILoggingEvent> getListAppender(Class<?> clazz) {
Logger logger = (Logger) LoggerFactory.getLogger(clazz);
ListAppender<ILoggingEvent> listAppender = new ListAppender2<>();
logger.addAppender(listAppender);
return listAppender;
}
}
Loading

0 comments on commit 667393d

Please sign in to comment.