|
| 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