Skip to content

Commit b886085

Browse files
Add empty data to SSH_MSG_IGNORE messages (#974)
As required by RFC 4253 Section 11.2, and RFC 4251 Section 5 Co-authored-by: Jeroen van Erp <[email protected]>
1 parent 7f8f43c commit b886085

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/itest/java/com/hierynomus/sshj/transport/kex/StrictKeyExchangeTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public boolean isEnabled() {
145145

146146
@Override
147147
protected void doKeepAlive() throws TransportException {
148-
conn.getTransport().write(new SSHPacket(Message.IGNORE));
148+
conn.getTransport().write(new SSHPacket(Message.IGNORE).putString(""));
149149
}
150150

151151
}

src/main/java/net/schmizz/keepalive/Heartbeater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ final class Heartbeater
2929

3030
@Override
3131
protected void doKeepAlive() throws TransportException {
32-
conn.getTransport().write(new SSHPacket(Message.IGNORE));
32+
conn.getTransport().write(new SSHPacket(Message.IGNORE).putString(""));
3333
}
3434
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C)2009 - SSHJ Contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package net.schmizz.keepalive;
17+
18+
import net.schmizz.sshj.DefaultConfig;
19+
import net.schmizz.sshj.common.Message;
20+
import net.schmizz.sshj.common.SSHPacket;
21+
import net.schmizz.sshj.connection.ConnectionImpl;
22+
import net.schmizz.sshj.transport.Transport;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.mockito.ArgumentCaptor;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.when;
30+
31+
class HeartbeaterTest {
32+
33+
@Test
34+
void ignoreMessageContainsData() throws Exception {
35+
Transport transport = mock(Transport.class);
36+
when(transport.getConfig()).thenReturn(new DefaultConfig());
37+
ArgumentCaptor<SSHPacket> sshPacketCaptor = ArgumentCaptor.forClass(SSHPacket.class);
38+
when(transport.write(sshPacketCaptor.capture())).thenReturn(0L);
39+
ConnectionImpl connection = new ConnectionImpl(transport, KeepAliveProvider.HEARTBEAT);
40+
41+
KeepAlive heartbeater = connection.getKeepAlive();
42+
assertThat(heartbeater).isInstanceOf(Heartbeater.class);
43+
44+
heartbeater.doKeepAlive();
45+
46+
SSHPacket sshPacket = sshPacketCaptor.getValue();
47+
assertThat(sshPacket.readMessageID()).isEqualTo(Message.IGNORE);
48+
assertThat(sshPacket.readBytes()).isNotNull();
49+
}
50+
51+
}

0 commit comments

Comments
 (0)