Skip to content

Commit

Permalink
Merge branch 'master' into openssh-key-gcm-ciphers-1
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus authored Oct 11, 2023
2 parents 622373f + a3cce0d commit 7a8486c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/itest/java/com/hierynomus/sshj/SshdContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ public SshdContainer(@NotNull Future<String> future) {
case STDERR:
logger().info("sshd stderr: {}", outputFrame.getUtf8String().stripTrailing());
break;
case END:
break;

}
});
}
Expand Down
81 changes: 81 additions & 0 deletions src/itest/java/com/hierynomus/sshj/sftp/PutFileCompressedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hierynomus.sshj.sftp;

import com.hierynomus.sshj.SshdContainer;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.xfer.InMemorySourceFile;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

@Testcontainers
public class PutFileCompressedTest {

private static class TestInMemorySourceFile extends InMemorySourceFile {

private final String name;
private final byte[] data;

public TestInMemorySourceFile(String name, byte[] data) {
this.name = name;
this.data = data;
}

@Override
public String getName() {
return name;
}

@Override
public long getLength() {
return data.length;
}

@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}

}

@Container
private static SshdContainer sshd = new SshdContainer();

@Test
public void shouldPutCompressedFile_GH893() throws Throwable {
try (SSHClient client = sshd.getConnectedClient()) {
client.authPublickey("sshj", "src/test/resources/id_rsa");
client.useCompression();
try (SFTPClient sftp = client.newSFTPClient()) {
String filename = "test.txt";
// needs to be a larger file for bug taking effect
byte[] content = new byte[5000];
Random r = new Random(1);
r.nextBytes(content);

sftp.put(new TestInMemorySourceFile(filename,content), "/home/sshj/");
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.hierynomus.sshj.SshdContainer;
import com.hierynomus.sshj.SshdContainer.SshdConfigBuilder;

import net.schmizz.sshj.Config;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.testcontainers.junit.jupiter.Testcontainers;

import com.hierynomus.sshj.SshdContainer;
import com.hierynomus.sshj.transport.mac.Macs;

import net.schmizz.sshj.Config;
import net.schmizz.sshj.DefaultConfig;
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/hierynomus/sshj/key/KeyAlgorithms.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
import net.schmizz.sshj.signature.SignatureECDSA;
import net.schmizz.sshj.signature.SignatureRSA;

import java.util.Arrays;
import java.util.List;

public class KeyAlgorithms {

public static Factory SSHRSA() { return new Factory("ssh-rsa", new SignatureRSA.FactorySSHRSA(), KeyType.RSA); }
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/schmizz/sshj/SSHClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.schmizz.sshj.connection.channel.forwarded.X11Forwarder;
import net.schmizz.sshj.connection.channel.forwarded.X11Forwarder.X11Channel;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.sftp.SFTPEngine;
import net.schmizz.sshj.sftp.StatefulSFTPClient;
import net.schmizz.sshj.transport.Transport;
import net.schmizz.sshj.transport.TransportException;
Expand Down Expand Up @@ -733,7 +732,7 @@ public SFTPClient newSFTPClient()
throws IOException {
checkConnected();
checkAuthenticated();
return new SFTPClient(new SFTPEngine(this).init());
return new SFTPClient(this);
}

/**
Expand All @@ -746,7 +745,7 @@ public StatefulSFTPClient newStatefulSFTPClient()
throws IOException {
checkConnected();
checkAuthenticated();
return new StatefulSFTPClient(new SFTPEngine(this).init());
return new StatefulSFTPClient(this);
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/net/schmizz/sshj/sftp/SFTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package net.schmizz.sshj.sftp;

import net.schmizz.sshj.connection.channel.direct.SessionFactory;
import net.schmizz.sshj.xfer.FilePermission;
import net.schmizz.sshj.xfer.LocalDestFile;
import net.schmizz.sshj.xfer.LocalSourceFile;
Expand All @@ -39,6 +40,13 @@ public SFTPClient(SFTPEngine engine) {
this.xfer = new SFTPFileTransfer(engine);
}

public SFTPClient(SessionFactory sessionFactory) throws IOException {
this.engine = new SFTPEngine(sessionFactory);
this.engine.init();
log = engine.getLoggerFactory().getLogger(getClass());
this.xfer = new SFTPFileTransfer(engine);
}

public SFTPEngine getSFTPEngine() {
return engine;
}
Expand Down Expand Up @@ -232,7 +240,7 @@ public void get(String source, String dest)
throws IOException {
xfer.download(source, dest);
}

public void get(String source, String dest, long byteOffset)
throws IOException {
xfer.download(source, dest, byteOffset);
Expand All @@ -252,7 +260,7 @@ public void get(String source, LocalDestFile dest)
throws IOException {
xfer.download(source, dest);
}

public void get(String source, LocalDestFile dest, long byteOffset)
throws IOException {
xfer.download(source, dest, byteOffset);
Expand All @@ -262,7 +270,7 @@ public void put(LocalSourceFile source, String dest)
throws IOException {
xfer.upload(source, dest);
}

public void put(LocalSourceFile source, String dest, long byteOffset)
throws IOException {
xfer.upload(source, dest, byteOffset);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/schmizz/sshj/sftp/StatefulSFTPClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package net.schmizz.sshj.sftp;

import net.schmizz.sshj.connection.channel.direct.SessionFactory;
import net.schmizz.sshj.xfer.LocalDestFile;
import net.schmizz.sshj.xfer.LocalSourceFile;

Expand All @@ -34,6 +35,12 @@ public StatefulSFTPClient(SFTPEngine engine)
log.debug("Start dir = {}", cwd);
}

public StatefulSFTPClient(SessionFactory sessionFactory) throws IOException {
super(sessionFactory);
this.cwd = getSFTPEngine().canonicalize(".");
log.debug("Start dir = {}", cwd);
}

private synchronized String cwdify(String path) {
return engine.getPathHelper().adjustForParent(cwd, path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import net.schmizz.sshj.common.Buffer;
import net.schmizz.sshj.common.DisconnectReason;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.compression.Compression;

public class ZlibCompression implements Compression {

Expand Down Expand Up @@ -71,10 +70,14 @@ public boolean isDelayed() {
public void compress(Buffer buffer) {
deflater.setInput(buffer.array(), buffer.rpos(), buffer.available());
buffer.wpos(buffer.rpos());
do {
while (true) {
final int len = deflater.deflate(tempBuf, 0, BUF_SIZE, Deflater.SYNC_FLUSH);
buffer.putRawBytes(tempBuf, 0, len);
} while (!deflater.needsInput());
if(len > 0) {
buffer.putRawBytes(tempBuf, 0, len);
} else {
return;
}
}
}

@Override
Expand Down

0 comments on commit 7a8486c

Please sign in to comment.