1package io.github.grantchan.sshengine.server.connection;2import io.github.grantchan.sshengine.common.connection.TtyMode;3import org.junit.FixMethodOrder;4import org.junit.Test;5import org.junit.runner.RunWith;6import org.junit.runners.MethodSorters;7import org.junit.runners.Parameterized;8import org.junit.runners.Parameterized.Parameter;9import org.junit.runners.Parameterized.Parameters;10import java.io.ByteArrayInputStream;11import java.io.IOException;12import java.nio.charset.StandardCharsets;13import java.util.Arrays;14import java.util.Collection;15import java.util.EnumSet;16import java.util.List;17import java.util.concurrent.atomic.AtomicInteger;18import static org.junit.Assert.assertEquals;19@FixMethodOrder(MethodSorters.NAME_ASCENDING)20@RunWith(Parameterized.class)21public class TtyInputStreamTest {22 private static final List<String> LINES =23 Arrays.asList(24 "A quick brown fox jumps over the lazy dog.",25 "The five boxing wizards jump quickly.",26 "A quick movement of the enemy will jeopardize six gunboats.",27 "Who packed five dozen old quart jugs in my box?",28 "The quick brown fox jumped over the lazy dogs.",29 "Few black taxis drive up major roads on quiet hazy nights.",30 "Pack my box with five dozen liquor jugs.",31 "My girl wove six dozen plaid jackets before she quit.",32 "Pack my red box with five dozen quality jugs."33 );34 private static final String data = String.join("\r\n", LINES);35 @Parameter(0)36 public TtyMode mode;37 @Parameter(1)38 public int expectedNumberOfCR;39 @Parameter(2)40 public int expectedNumberOfLF;41 @Parameters(name = "{index}: mode={0}")42 public static Collection<Object[]> parameters() {43 int numberOfLines = LINES.size() - 1;44 int dblNumberOfLines = (LINES.size() - 1) << 1;45 return Arrays.asList(new Object[][] {46 { TtyMode.ECHO, numberOfLines , numberOfLines },47 { TtyMode.ONLCR, numberOfLines , numberOfLines },48 { TtyMode.OCRNL, 0 , dblNumberOfLines },49 { TtyMode.ONLRET, dblNumberOfLines, 0 },50 { TtyMode.ONOCR, numberOfLines , numberOfLines }51 });52 }53 @Test54 public void whenUsingDifferentTtyMode_shouldProduceDifferentCRAndLF() throws IOException {55 try (ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));56 TtyInputStream tty = new TtyInputStream(bais, EnumSet.of(mode))) {57 final AtomicInteger actualNumberOfCR = new AtomicInteger(0);58 final AtomicInteger actualNumberOfLF = new AtomicInteger(0);59 int c;60 do {61 c = tty.read();62 if (c == '\r') {63 actualNumberOfCR.incrementAndGet();64 } else if (c == '\n') {65 actualNumberOfLF.incrementAndGet();66 }67 } while (c != -1);68 assertEquals("", expectedNumberOfCR, actualNumberOfCR.get());69 assertEquals("", expectedNumberOfLF, actualNumberOfLF.get());70 }71 }72}...