Fixed some codacy issues

This commit is contained in:
Jeroen van Erp
2018-06-18 14:27:45 +02:00
parent 80d93ae8e7
commit 49a450fb53
7 changed files with 32 additions and 26 deletions

View File

@@ -55,7 +55,7 @@
<dependency> <dependency>
<groupId>com.hierynomus</groupId> <groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId> <artifactId>sshj</artifactId>
<version>0.19.0</version> <version>0.24.0</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -5,28 +5,32 @@ import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session; import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command; import net.schmizz.sshj.connection.channel.direct.Session.Command;
import java.io.Console;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** This examples demonstrates how a remote command can be executed. */ /** This examples demonstrates how a remote command can be executed. */
public class Exec { public class Exec {
private static final Console con = System.console();
public static void main(String... args) public static void main(String... args)
throws IOException { throws IOException {
final SSHClient ssh = new SSHClient(); final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts(); ssh.loadKnownHosts();
ssh.connect("localhost"); ssh.connect("localhost");
Session session = null;
try { try {
ssh.authPublickey(System.getProperty("user.name")); ssh.authPublickey(System.getProperty("user.name"));
final Session session = ssh.startSession(); session = ssh.startSession();
final Command cmd = session.exec("ping -c 1 google.com"); final Command cmd = session.exec("ping -c 1 google.com");
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString()); con.writer().print(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS); cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus()); con.writer().print("\n** exit status: " + cmd.getExitStatus());
} finally { } finally {
try { try {
session.close(); if (session != null) {
session.close();
}
} catch (IOException e) { } catch (IOException e) {
// Do Nothing // Do Nothing
} }

View File

@@ -147,7 +147,6 @@ public class Buffer<T extends Buffer<T>> {
/** Compact this {@link SSHPacket} */ /** Compact this {@link SSHPacket} */
public void compact() { public void compact() {
System.err.println("COMPACTING");
if (available() > 0) if (available() > 0)
System.arraycopy(data, rpos, data, 0, wpos - rpos); System.arraycopy(data, rpos, data, 0, wpos - rpos);
wpos -= rpos; wpos -= rpos;
@@ -356,8 +355,9 @@ public class Buffer<T extends Buffer<T>> {
} }
public T putUInt64(long uint64) { public T putUInt64(long uint64) {
if (uint64 < 0) if (uint64 < 0) {
throw new IllegalArgumentException("Invalid value: " + uint64); throw new IllegalArgumentException("Invalid value: " + uint64);
}
return putUInt64Unchecked(uint64); return putUInt64Unchecked(uint64);
} }

View File

@@ -60,10 +60,8 @@ public final class Reader
} }
} }
} catch (Exception e) { } catch (Exception e) {
//noinspection StatementWithEmptyBody // We are meant to shut up and draw to a close if interrupted
if (isInterrupted()) { if (!isInterrupted()) {
// We are meant to shut up and draw to a close if interrupted
} else {
trans.die(e); trans.die(e);
} }
} }

View File

@@ -71,9 +71,7 @@ public class RemoteFileTest {
n += rs.read(test, n, 3072 - n); n += rs.read(test, n, 3072 - n);
} }
if (test[3072] != 0) { assertThat("buffer overrun", test[3072] == 0);
System.err.println("buffer overrun!");
}
n += rs.read(test, n, test.length - n); // --> ArrayIndexOutOfBoundsException n += rs.read(test, n, test.length - n); // --> ArrayIndexOutOfBoundsException

View File

@@ -22,6 +22,8 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import static org.junit.Assert.fail;
public class LoadsOfConnects { public class LoadsOfConnects {
protected final Logger log = LoggerFactory.getLogger(getClass()); protected final Logger log = LoggerFactory.getLogger(getClass());
@@ -29,15 +31,19 @@ public class LoadsOfConnects {
private final SshFixture fixture = new SshFixture(); private final SshFixture fixture = new SshFixture();
@Test @Test
public void loadsOfConnects() public void loadsOfConnects() {
throws IOException, InterruptedException { try {
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
System.out.println("Try " + i); log.info("Try " + i);
fixture.start(); fixture.start();
fixture.setupConnectedDefaultClient(); fixture.setupConnectedDefaultClient();
fixture.stopClient(); fixture.stopClient();
fixture.stopServer(); fixture.stopServer();
}
} catch (Exception e) {
fail(e.getMessage());
} }
} }
} }

View File

@@ -251,10 +251,10 @@ public class OpenSSHKeyFileTest {
} }
@Before @Before
public void setup() public void checkBCRegistration() {
throws UnsupportedEncodingException, GeneralSecurityException { if (!SecurityUtils.isBouncyCastleRegistered()) {
if (!SecurityUtils.isBouncyCastleRegistered())
throw new AssertionError("bouncy castle needed"); throw new AssertionError("bouncy castle needed");
}
} }
private String readFile(String pathname) private String readFile(String pathname)