From d7df0a7d86505089b6b374b2f7a427936eae4eb4 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Tue, 29 Sep 2020 12:05:03 +0300 Subject: [PATCH] Binary data shall be emitted in the form of hexa numbers. Added converter character->to HEX in AsyncSshHandlerWriter class logging. JIRA: NETCONF-135 Change-Id: I462baf94eecbc67221ca349f2b900eedf80ea483 Signed-off-by: Iaroslav --- .../ssh/client/AsyncSshHandlerWriter.java | 17 ++++++- .../ssh/client/AsyncSshHandlerWriterTest.java | 44 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerWriterTest.java diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerWriter.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerWriter.java index e9ade8e474..549bbe18cb 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerWriter.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerWriter.java @@ -17,6 +17,8 @@ import java.nio.charset.StandardCharsets; import java.util.Deque; import java.util.LinkedList; import java.util.Queue; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.checkerframework.checker.lock.qual.GuardedBy; import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream; import org.opendaylight.netconf.shaded.sshd.common.io.WritePendingException; @@ -34,6 +36,8 @@ public final class AsyncSshHandlerWriter implements AutoCloseable { private static final Logger LOG = LoggerFactory .getLogger(AsyncSshHandlerWriter.class); + private static final Pattern NON_ASCII = Pattern.compile("([^\\x20-\\x7E\\x0D\\x0A])+"); + // public static final int MAX_PENDING_WRITES = 1000; // TODO implement Limiting mechanism for pending writes // But there is a possible issue with limiting: @@ -156,9 +160,18 @@ public final class AsyncSshHandlerWriter implements AutoCloseable { } public static String byteBufToString(final ByteBuf msg) { - final String s = msg.toString(StandardCharsets.UTF_8); + final String message = msg.toString(StandardCharsets.UTF_8); msg.resetReaderIndex(); - return s; + Matcher matcher = NON_ASCII.matcher(message); + return matcher.replaceAll((data) -> { + StringBuilder buf = new StringBuilder(); + buf.append("\""); + for (byte b : data.group().getBytes(StandardCharsets.US_ASCII)) { + buf.append(String.format("%02X", b)); + } + buf.append("\""); + return buf.toString(); + }); } private void queueRequest(final ChannelHandlerContext ctx, final ByteBuf msg, final ChannelPromise promise) { diff --git a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerWriterTest.java b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerWriterTest.java new file mode 100644 index 0000000000..9644315c47 --- /dev/null +++ b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandlerWriterTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.netconf.nettyutil.handler.ssh.client; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import io.netty.buffer.ByteBuf; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.StrictStubs.class) +public class AsyncSshHandlerWriterTest { + + private ByteBuf byteBuf; + + @Before + public void setUp() throws Exception { + byteBuf = mock(ByteBuf.class, Mockito.CALLS_REAL_METHODS); + doReturn(byteBuf).when(byteBuf).resetReaderIndex(); + } + + @Test + public void testByteBufToString() { + String testText = "Lorem Ipsum 0123456780!@#$%^&*<>\\|/?[]()\n\r"; + doReturn(testText).when(byteBuf).toString(ArgumentMatchers.any()); + assertEquals(testText, AsyncSshHandlerWriter.byteBufToString(byteBuf)); + + testText = "Lorem Ipsum" + (char) 0x8 + " 0123456780" + (char) 0x11 + (char) 0x7F + "9 !@#$%^&*<>\\|/?[]()\n\r"; + doReturn(testText).when(byteBuf).toString(ArgumentMatchers.any()); + assertEquals("Lorem Ipsum\"08\" 0123456780\"117F\"9 !@#$%^&*<>\\|/?[]()\n\r", + AsyncSshHandlerWriter.byteBufToString(byteBuf)); + } +} \ No newline at end of file -- 2.36.6