From 77fbf444050b0ea2b1d0a4e0f96108e9d0d75254 Mon Sep 17 00:00:00 2001 From: Matt Cook Date: Thu, 23 Jan 2025 11:38:55 -0700 Subject: [PATCH 1/6] style: address checkstyle violations in InetPacketFunctionalTest --- .../ode/inet/InetPacketFunctionalTest.java | 151 +++++++++--------- 1 file changed, 76 insertions(+), 75 deletions(-) diff --git a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java index f14fcf100..7859a40f8 100644 --- a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java +++ b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java @@ -1,18 +1,19 @@ -/******************************************************************************* +/*============================================================================= * Copyright 2018 572682 - * + * * 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 us.dot.its.jpo.ode.inet; import static org.junit.Assert.assertArrayEquals; @@ -22,86 +23,86 @@ import static org.junit.Assert.assertTrue; import java.net.DatagramPacket; -import java.net.DatagramSocket; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; - import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; - -import mockit.Capturing; import us.dot.its.jpo.ode.util.CodecUtils; public class InetPacketFunctionalTest { - - private static boolean isVerbose = false; - + + private static boolean isVerbose = false; + @Test - public void testConstrcutor() throws UnknownHostException { - InetPacket pkt = new InetPacket("bah.com", 1111, null); - - assertNull(pkt.getPayload()); - } - - @Test @Disabled - public void test() throws UnknownHostException { - test("127.0.0.1", 12, "01234567890".getBytes()); - test("::1", 47561, "0123456789001234567890".getBytes()); - test("1080:0:0:0:8:800:200C:417A", 345, "".getBytes()); - test("1080:0:0:0:8:800:200C:417A", 345, null); - test("1.0.0", 47561, "".getBytes()); - test("::FFFF:129.144.52.38", 4756, "0".getBytes()); - test("1080::8:800:200C:417A", 4756, new byte[] { (byte)0, (byte)1 }); - test("2001:0:9d38:90d7:3ce3:339d:f5c3:c42b", 11111, new byte[] { (byte)0xde, (byte)0xad, (byte)0xbe, (byte)0xef }); - test(null, 22222, new byte[] { (byte)0xde, (byte)0xad, (byte)0xbe, (byte)0xef }); - test("fdf8:f53b:82e4::53", 11111, new byte[] { (byte)0xca, (byte)0xfe, (byte)0xba, (byte)0xbe }); - } - - public void test(String address, int port, byte[] payload) throws UnknownHostException { - if ( payload == null ) - return; - if ( isVerbose ) - System.out.println("---------- Test ----------"); - DatagramPacket packet = new DatagramPacket(payload, payload.length, InetAddress.getByName(address), port); - InetPacket pbOut = new InetPacket(packet); - test(address, port, payload, pbOut); - } + public void testConstrcutor() throws UnknownHostException { + InetPacket pkt = new InetPacket("bah.com", 1111, null); + + assertNull(pkt.getPayload()); + } + + @Test + @Disabled + public void test() throws UnknownHostException { + test("127.0.0.1", 12, "01234567890".getBytes()); + test("::1", 47561, "0123456789001234567890".getBytes()); + test("1080:0:0:0:8:800:200C:417A", 345, "".getBytes()); + test("1080:0:0:0:8:800:200C:417A", 345, null); + test("1.0.0", 47561, "".getBytes()); + test("::FFFF:129.144.52.38", 4756, "0".getBytes()); + test("1080::8:800:200C:417A", 4756, new byte[] {(byte) 0, (byte) 1}); + test("2001:0:9d38:90d7:3ce3:339d:f5c3:c42b", 11111, new byte[] {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef}); + test(null, 22222, new byte[] {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef}); + test("fdf8:f53b:82e4::53", 11111, new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe}); + } + + public void test(String address, int port, byte[] payload) throws UnknownHostException { + if (payload == null) { + return; + } + if (isVerbose) { + System.out.println("---------- Test ----------"); + } + DatagramPacket packet = new DatagramPacket(payload, payload.length, InetAddress.getByName(address), port); + InetPacket pbOut = new InetPacket(packet); + test(address, port, payload, pbOut); + } + + public void test(String address, int port, byte[] payload, InetPacket pbOut) throws UnknownHostException { + InetPacket pbIn = new InetPacket(pbOut.getBundle()); + if (isVerbose) { + print("From params", pbOut); + print("From bundle", pbIn); + } + InetPoint pointIn = pbIn.getPoint(); + assertNotNull(pointIn); + InetPoint pointOut = pbOut.getPoint(); + assertEquals(pointOut.port, pointIn.port); + assertArrayEquals(pointOut.address, pointIn.address); + byte[] pbOutPayload = pbOut.getPayload(); + byte[] pyInPayload = pbIn.getPayload(); + if (pbOutPayload != null && pbOutPayload != null) { + assertTrue(Arrays.equals(pbOutPayload, pyInPayload)); + } else if (pbOutPayload != null || pbOutPayload != null) { + assertTrue(false); + } + assertTrue(Arrays.equals(pbOut.getBundle(), pbIn.getBundle())); + } - public void test(String address, int port, byte[] payload, InetPacket pbOut) throws UnknownHostException { - InetPacket pbIn = new InetPacket(pbOut.getBundle()); - if ( isVerbose ) { - print("From params", pbOut); - print("From bundle", pbIn); - } - InetPoint pointIn = pbIn.getPoint(); - assertNotNull(pointIn); - InetPoint pointOut = pbOut.getPoint(); - assertEquals(pointOut.port, pointIn.port); - assertArrayEquals(pointOut.address, pointIn.address); - byte[] pbOutPayload = pbOut.getPayload(); - byte[] pyInPayload = pbIn.getPayload(); - if ( pbOutPayload != null && pbOutPayload != null ) - assertTrue(Arrays.equals(pbOutPayload, pyInPayload)); - else if (pbOutPayload != null || pbOutPayload != null) - assertTrue(false); - assertTrue(Arrays.equals(pbOut.getBundle(), pbIn.getBundle())); - } - - void print(String header, InetPacket pb) throws UnknownHostException { - assertNotNull(pb); - InetPoint point = pb.getPoint(); - if( point != null ) { - System.out.printf("%s: port: %d (0x%x)\n", header, point.port, point.port ); - System.out.printf("%s: address size: %d value: %s ip: %s\n", header, point.address.length, - CodecUtils.toHex(point.address), InetAddress.getByAddress(point.address).getHostAddress() ); - System.out.printf("%s: forward: %s\n", header, point.forward ? "true" : "false" ); - } else { - System.out.printf("%s: Inet point is null\n", header); - } - byte[] p = pb.getPayload(); - System.out.printf("%s payload: %s\n", header, p != null && p.length > 0 ? CodecUtils.toHex(p) : ""); - System.out.printf("%s bundle: %s\n", header, pb.toHexString()); - } + void print(String header, InetPacket pb) throws UnknownHostException { + assertNotNull(pb); + InetPoint point = pb.getPoint(); + if (point != null) { + System.out.printf("%s: port: %d (0x%x)\n", header, point.port, point.port); + System.out.printf("%s: address size: %d value: %s ip: %s\n", header, point.address.length, + CodecUtils.toHex(point.address), InetAddress.getByAddress(point.address).getHostAddress()); + System.out.printf("%s: forward: %s\n", header, point.forward ? "true" : "false"); + } else { + System.out.printf("%s: Inet point is null\n", header); + } + byte[] p = pb.getPayload(); + System.out.printf("%s payload: %s\n", header, p != null && p.length > 0 ? CodecUtils.toHex(p) : ""); + System.out.printf("%s bundle: %s\n", header, pb.toHexString()); + } } From 70019f3c7bb26914c38dc48fe4a7a617d684f2f3 Mon Sep 17 00:00:00 2001 From: Matt Cook Date: Thu, 23 Jan 2025 11:40:03 -0700 Subject: [PATCH 2/6] chore: remove public modifiers and use junit.jupiter.api consistently --- .../ode/inet/InetPacketFunctionalTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java index 7859a40f8..51edf498c 100644 --- a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java +++ b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java @@ -16,11 +16,11 @@ package us.dot.its.jpo.ode.inet; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.net.DatagramPacket; import java.net.InetAddress; @@ -30,12 +30,12 @@ import org.junit.jupiter.api.Test; import us.dot.its.jpo.ode.util.CodecUtils; -public class InetPacketFunctionalTest { +class InetPacketFunctionalTest { private static boolean isVerbose = false; @Test - public void testConstrcutor() throws UnknownHostException { + void testConstrcutor() throws UnknownHostException { InetPacket pkt = new InetPacket("bah.com", 1111, null); assertNull(pkt.getPayload()); @@ -43,7 +43,7 @@ public void testConstrcutor() throws UnknownHostException { @Test @Disabled - public void test() throws UnknownHostException { + void test() throws UnknownHostException { test("127.0.0.1", 12, "01234567890".getBytes()); test("::1", 47561, "0123456789001234567890".getBytes()); test("1080:0:0:0:8:800:200C:417A", 345, "".getBytes()); @@ -56,7 +56,7 @@ public void test() throws UnknownHostException { test("fdf8:f53b:82e4::53", 11111, new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe}); } - public void test(String address, int port, byte[] payload) throws UnknownHostException { + void test(String address, int port, byte[] payload) throws UnknownHostException { if (payload == null) { return; } @@ -68,7 +68,7 @@ public void test(String address, int port, byte[] payload) throws UnknownHostExc test(address, port, payload, pbOut); } - public void test(String address, int port, byte[] payload, InetPacket pbOut) throws UnknownHostException { + void test(String address, int port, byte[] payload, InetPacket pbOut) throws UnknownHostException { InetPacket pbIn = new InetPacket(pbOut.getBundle()); if (isVerbose) { print("From params", pbOut); From 70f73f8ebe019bfaf1b9c7130104efe62e85c5ca Mon Sep 17 00:00:00 2001 From: Matt Cook Date: Thu, 23 Jan 2025 11:43:25 -0700 Subject: [PATCH 3/6] chore: replace logger factory call with Slf4j annotation --- .../src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java b/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java index c63db8ed5..187a9ee9f 100644 --- a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java +++ b/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java @@ -17,14 +17,11 @@ import java.net.InetAddress; import java.net.UnknownHostException; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - +import lombok.extern.slf4j.Slf4j; import us.dot.its.jpo.ode.util.CodecUtils; +@Slf4j public class InetPoint { - private final Logger logger = LoggerFactory.getLogger(this.getClass()); public final byte[] address; public final int port; @@ -61,7 +58,7 @@ public String toString() { try { host = InetAddress.getByAddress(address).getHostAddress(); } catch (UnknownHostException e) { - logger.error("Error", e); + log.error("Error", e); } return String.format("%s { port = %d (0x%x); address = %s (%s, %s); forward = %s }", getClass().getSimpleName(), From b7d14ffc594141281eaf002959cca19d031b8524 Mon Sep 17 00:00:00 2001 From: Matt Cook Date: Thu, 23 Jan 2025 11:44:34 -0700 Subject: [PATCH 4/6] style: address checkstyle violations in InetPoint --- .../us/dot/its/jpo/ode/inet/InetPoint.java | 110 ++++++++++-------- 1 file changed, 62 insertions(+), 48 deletions(-) diff --git a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java b/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java index 187a9ee9f..5b78f1ee8 100644 --- a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java +++ b/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java @@ -1,18 +1,19 @@ -/******************************************************************************* +/*============================================================================= * Copyright 2018 572682 - * + * * 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 us.dot.its.jpo.ode.inet; import java.net.InetAddress; @@ -20,51 +21,64 @@ import lombok.extern.slf4j.Slf4j; import us.dot.its.jpo.ode.util.CodecUtils; +/** + * Represents a network endpoint with an IP address, port, and an optional forward flag. + * This class provides utilities for handling and representing IP-based network endpoints. + */ @Slf4j public class InetPoint { - public final byte[] address; - public final int port; - public final boolean forward; - - public InetPoint(String host, int port, boolean forward) throws UnknownHostException { - this(InetAddress.getByName(host).getAddress(), port, forward); - } - - public InetPoint(byte[] address, int port) { - this(address, port, false); - } - - public InetPoint(byte[] address, int port, boolean forward ) { - if (address == null) { - throw new IllegalArgumentException("IP Address is required"); - } - this.address = address; - this.port = port; - this.forward = forward; - } - - public InetAddress getInetAddress() throws UnknownHostException { - return InetAddress.getByAddress(address); - } - - public boolean isIPv6Address() { - return address.length == 16; - } - - @Override - public String toString() { - String host = "?"; - try { - host = InetAddress.getByAddress(address).getHostAddress(); - } catch (UnknownHostException e) { - log.error("Error", e); - } - return String.format("%s { port = %d (0x%x); address = %s (%s, %s); forward = %s }", - getClass().getSimpleName(), - port, port, - CodecUtils.toHex(address), address.length == 4 ? "IPv4" : "IPv6", host, - forward ? "true" : "false" - ); - } + public final byte[] address; + public final int port; + public final boolean forward; + + public InetPoint(String host, int port, boolean forward) throws UnknownHostException { + this(InetAddress.getByName(host).getAddress(), port, forward); + } + + public InetPoint(byte[] address, int port) { + this(address, port, false); + } + + /** + * Constructs an InetPoint object. + * + * @param address The byte array representing the IP address of the endpoint. Must not be null. + * @param port The port number of the endpoint. + * @param forward A boolean flag indicating whether the endpoint is configured for forwarding. + * + * @throws IllegalArgumentException if the address parameter is null. + */ + public InetPoint(byte[] address, int port, boolean forward) { + if (address == null) { + throw new IllegalArgumentException("IP Address is required"); + } + this.address = address; + this.port = port; + this.forward = forward; + } + + public InetAddress getInetAddress() throws UnknownHostException { + return InetAddress.getByAddress(address); + } + + public boolean isIPv6Address() { + return address.length == 16; + } + + @Override + public String toString() { + String host = "?"; + try { + host = InetAddress.getByAddress(address).getHostAddress(); + } catch (UnknownHostException e) { + log.error("Error", e); + } + return String.format("%s { port = %d (0x%x); address = %s (%s, %s); forward = %s }", + getClass().getSimpleName(), + port, port, + CodecUtils.toHex(address), address.length == 4 ? "IPv4" : "IPv6", host, + forward ? "true" : "false" + ); + } } From dc8a3548f4ba44adf1f58e13e259d03d9457f9e4 Mon Sep 17 00:00:00 2001 From: Matt Cook Date: Thu, 23 Jan 2025 11:45:27 -0700 Subject: [PATCH 5/6] chore: correct test name typo --- .../java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java index 51edf498c..eae1c8b80 100644 --- a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java +++ b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java @@ -35,7 +35,7 @@ class InetPacketFunctionalTest { private static boolean isVerbose = false; @Test - void testConstrcutor() throws UnknownHostException { + void testConstructor() throws UnknownHostException { InetPacket pkt = new InetPacket("bah.com", 1111, null); assertNull(pkt.getPayload()); From f94d20bae00117dd3da9007c6b7ea80114efa180 Mon Sep 17 00:00:00 2001 From: Matt Cook Date: Thu, 23 Jan 2025 14:56:53 -0700 Subject: [PATCH 6/6] chore: delete unused classes with flaky tests --- .../us/dot/its/jpo/ode/inet/InetPacket.java | 118 ----- .../its/jpo/ode/inet/InetPacketException.java | 30 -- .../its/jpo/ode/inet/InetPacketSender.java | 185 ------- .../us/dot/its/jpo/ode/inet/InetPoint.java | 84 ---- .../ode/inet/InetPacketFunctionalTest.java | 108 ---- .../jpo/ode/inet/InetPacketSenderTest.java | 468 ------------------ .../dot/its/jpo/ode/inet/InetPacketTest.java | 146 ------ .../dot/its/jpo/ode/inet/InetPointTest.java | 140 ------ 8 files changed, 1279 deletions(-) delete mode 100644 jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacket.java delete mode 100644 jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacketException.java delete mode 100644 jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacketSender.java delete mode 100644 jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java delete mode 100644 jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java delete mode 100644 jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketSenderTest.java delete mode 100644 jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketTest.java delete mode 100644 jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPointTest.java diff --git a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacket.java b/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacket.java deleted file mode 100644 index 96cf0a25a..000000000 --- a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacket.java +++ /dev/null @@ -1,118 +0,0 @@ -/******************************************************************************* - * Copyright 2018 572682 - * - * 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 us.dot.its.jpo.ode.inet; - -import java.net.DatagramPacket; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.Arrays; - -import us.dot.its.jpo.ode.util.CodecUtils; -import us.dot.its.jpo.ode.util.CrcCccitt; - -public class InetPacket { - - private static final int MAGIC_NUMBER = 982451653; - private static final int MIN_BUNDLE_LENGTH = 4 + 4 + 1 + 4 + 2; // magic + port + type + ip4 + CRC - - private InetPoint point; - private byte[] payload; - - public InetPacket(String host, int port, byte[] payload) throws UnknownHostException { - this(InetAddress.getByName( host ), port, payload); - } - - public InetPacket(InetAddress ipAddress, int port, byte[] payload) { - this(new InetPoint(ipAddress.getAddress(), port), payload); - } - - public InetPacket(InetPoint point, byte[] payload) { - this.point = point; - if ( !parseBundle(payload) ) - this.payload = payload; - } - - public InetPacket(DatagramPacket packet) { - point = new InetPoint(packet.getAddress().getAddress(), packet.getPort()); - byte[] data = Arrays.copyOfRange(packet.getData(), packet.getOffset(), packet.getLength()); - if ( !parseBundle(data) ) - payload = data; - } - - public InetPacket(byte[] bundle) { - if ( !parseBundle(bundle) ) - payload = bundle; - } - - public InetPoint getPoint() { - return point; - } - - public byte[] getPayload() { - return payload; - } - - public byte[] getBundle() { - if ( point == null ) - return payload; - int payloadLength = payload != null ? payload.length : 0; - int headerLength = MIN_BUNDLE_LENGTH - 4 + point.address.length; - byte [] bundle = new byte[headerLength + payloadLength]; - ByteBuffer buffer = ByteBuffer.allocate(headerLength).order(ByteOrder.BIG_ENDIAN); - buffer.putInt(MAGIC_NUMBER); - buffer.putInt(point.port); - buffer.put((byte)(point.address.length == 16 ? 1 : 0)); - buffer.put(point.address); - byte[] header = buffer.array(); - assert(header.length == headerLength); - CrcCccitt.setMsgCRC(header); - System.arraycopy(header, 0, bundle, 0, headerLength); - if ( payload != null ) - System.arraycopy(payload, 0, bundle, headerLength, payloadLength); - return bundle; - } - - public boolean parseBundle(byte[] bundle) { - if ( bundle == null || bundle.length < MIN_BUNDLE_LENGTH ) - return false; - ByteBuffer buffer = ByteBuffer.wrap(bundle); - int magic = buffer.getInt(); - if ( magic != MAGIC_NUMBER ) - return false; - int port = buffer.getInt(); - byte type = buffer.get(); - int addressLength = type == 1 ? 16 : 4; - if ( buffer.remaining() < addressLength + 2 ) - return false; - if ( !CrcCccitt.isValidMsgCRC(bundle, 0, MIN_BUNDLE_LENGTH - 4 + addressLength) ) - return false; - byte[] address = new byte[addressLength]; - buffer.get(address,0,addressLength); - buffer.getShort(); - point = new InetPoint(address, port, true); - int payloadLength = bundle.length - MIN_BUNDLE_LENGTH + 4 - addressLength; - payload = new byte[payloadLength]; - buffer.get(payload,0,payloadLength); - return true; - } - - public String toHexString() { - return CodecUtils.toHex(getBundle()); - } - -} diff --git a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacketException.java b/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacketException.java deleted file mode 100644 index 10c681bec..000000000 --- a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacketException.java +++ /dev/null @@ -1,30 +0,0 @@ -/******************************************************************************* - * Copyright 2018 572682 - * - * 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 us.dot.its.jpo.ode.inet; - -public class InetPacketException extends Exception { - - private static final long serialVersionUID = 1L; - - public InetPacketException(String message) { - super(message); - } - - public InetPacketException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacketSender.java b/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacketSender.java deleted file mode 100644 index 5ac17ac1c..000000000 --- a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPacketSender.java +++ /dev/null @@ -1,185 +0,0 @@ -/******************************************************************************* - * Copyright 2018 572682. - * - *

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 us.dot.its.jpo.ode.inet; - -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.SocketException; -import lombok.extern.slf4j.Slf4j; - -/** - * Sender/Forwarder helper class for use by Forwarder, Transport, and Data Sink - * that need to send packets around. - */ -@Slf4j -public class InetPacketSender { - - private static final String INVALID_PARAMETERS_MSG = - "Invalid Parameters. Parameters destination point and payload can not be null"; - - /** - * Inet address and port to forward packets to. - */ - private InetPoint frwdPoint; - - /** - * Specifies whether outbound IPv4 messages should be send directly or - * forwarded. Default is send directly. - * To force forwarding IPv4 messages, set this variable to true. - */ - private boolean forwardAll; - - public InetPacketSender() { - } - - /** - * Creates an instance of the forwarder/sender helper class. - * - * @param frwdPoint is the destination to use for forwarding - */ - public InetPacketSender(InetPoint frwdPoint) { - this.frwdPoint = frwdPoint; - } - - /** - * Forward packet. Intended client is the forwarder that received a packet - * - * @param packet UDP packet - * @throws InetPacketException if the packet is null or the destination is not defined - */ - public void forward(DatagramPacket packet) throws InetPacketException { - if (packet == null) { - log.warn("Ignoring forward request for null packet"); - return; - } - if (frwdPoint == null) { - throw new InetPacketException( - "Couldn't forward packet. Reason: Forwarding destination is not defined."); - } - send(frwdPoint, new InetPacket(packet).getBundle()); - } - - /** - * Forward payload to be sent to dstPoint. Intended clients are Transport or - * Data Sink sending via forwarder - * - * @param dstPoint destination address and port for forwarder to forward to - * @param payload data to forward - * @throws InetPacketException if the packet is null or the destination is not defined - */ - public void forward(InetPoint dstPoint, byte[] payload) throws InetPacketException { - if (dstPoint == null || payload == null) { - throw new InetPacketException(INVALID_PARAMETERS_MSG); - } - if (frwdPoint == null) { - log.warn("Couldn't forward packet. Reason: Forwarding destination is not defined."); - } - if (frwdPoint != null && (dstPoint.isIPv6Address() || isForwardAll())) { - send(frwdPoint, new InetPacket(dstPoint, payload).getBundle()); - } else { - log.debug("Using direct send instead of forwarding"); - send(dstPoint, payload); - } - } - - /** - * Forward payload to be sent to dstPoint. Intended clients are Transport or - * Data Sink sending via forwarder or direct - * - * @param dstPoint destination address and port of the final destination - * @param payload data to forward or send - * @param fromForwarder whether the original request came through a forwarder - * @throws InetPacketException if the packet is null or the destination is not defined - */ - public void forward(InetPoint dstPoint, byte[] payload, boolean fromForwarder) - throws InetPacketException { - if (dstPoint == null || payload == null) { - throw new InetPacketException(INVALID_PARAMETERS_MSG); - } - if (frwdPoint != null && (dstPoint.isIPv6Address() || isForwardAll() || fromForwarder)) { - send(frwdPoint, new InetPacket(dstPoint, payload).getBundle()); - } else { - log.debug("Using direct send instead of forwarding"); - send(dstPoint, payload); - } - } - - /** - * Send packet. Intended client is the forwarder that sends outbound packet - * - * @param packet outbound packet that contains destination+payload bundle - * @throws InetPacketException if the packet is null or the destination is not defined - */ - public void send(DatagramPacket packet) throws InetPacketException { - if (packet == null) { - log.warn("Ignoring send request for null packet"); - return; - } - InetPacket p = new InetPacket(packet); - InetPoint point = p.getPoint(); - if (point == null) { - throw new InetPacketException( - "Couldn't send packet. Reason: Destination is not defined in the packet (not a bundle?)"); - } - send(point, p.getPayload()); - } - - /** - * Send payload to the destination specified. Intended clients are Transport or - * Data Sink sending directly to the client - * - * @param dstPoint destination address and port to send to - * @param payload data to send - * @throws InetPacketException if the packet is null or the destination is not defined - */ - public void send(InetPoint dstPoint, byte[] payload) throws InetPacketException { - if (dstPoint == null || payload == null) { - throw new InetPacketException(INVALID_PARAMETERS_MSG); - } - try (DatagramSocket sock = new DatagramSocket()) { - DatagramPacket packet = new DatagramPacket(payload, payload.length, dstPoint.getInetAddress(), - dstPoint.port); - sock.send(packet); - } catch (SocketException ex) { - throw new InetPacketException("Couldn't send packet because socket closed.", ex); - } catch (IOException ex) { - throw new InetPacketException("Couldn't send packet due to IO exception.", ex); - } - } - - /** - * Reports whether outbound IPv4 messages should be sent directly or forwarded. - * - * @return true if IPv4 packets are forwarded in addition to IPv6 packets - */ - public boolean isForwardAll() { - return forwardAll; - } - - /** - * Sets whether outbound IPv4 messages should be sent directly or forwarded. - * - * @param forwardAll Directs how to handle IPv4 messages. - * Specify true to force forwarding IPv4 messages, and false - * to always send them directly. - */ - public void setForwardAll(boolean forwardAll) { - this.forwardAll = forwardAll; - } - -} diff --git a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java b/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java deleted file mode 100644 index 5b78f1ee8..000000000 --- a/jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java +++ /dev/null @@ -1,84 +0,0 @@ -/*============================================================================= - * Copyright 2018 572682 - * - * 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 us.dot.its.jpo.ode.inet; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import lombok.extern.slf4j.Slf4j; -import us.dot.its.jpo.ode.util.CodecUtils; - -/** - * Represents a network endpoint with an IP address, port, and an optional forward flag. - * This class provides utilities for handling and representing IP-based network endpoints. - */ -@Slf4j -public class InetPoint { - - public final byte[] address; - public final int port; - public final boolean forward; - - public InetPoint(String host, int port, boolean forward) throws UnknownHostException { - this(InetAddress.getByName(host).getAddress(), port, forward); - } - - public InetPoint(byte[] address, int port) { - this(address, port, false); - } - - /** - * Constructs an InetPoint object. - * - * @param address The byte array representing the IP address of the endpoint. Must not be null. - * @param port The port number of the endpoint. - * @param forward A boolean flag indicating whether the endpoint is configured for forwarding. - * - * @throws IllegalArgumentException if the address parameter is null. - */ - public InetPoint(byte[] address, int port, boolean forward) { - if (address == null) { - throw new IllegalArgumentException("IP Address is required"); - } - this.address = address; - this.port = port; - this.forward = forward; - } - - public InetAddress getInetAddress() throws UnknownHostException { - return InetAddress.getByAddress(address); - } - - public boolean isIPv6Address() { - return address.length == 16; - } - - @Override - public String toString() { - String host = "?"; - try { - host = InetAddress.getByAddress(address).getHostAddress(); - } catch (UnknownHostException e) { - log.error("Error", e); - } - return String.format("%s { port = %d (0x%x); address = %s (%s, %s); forward = %s }", - getClass().getSimpleName(), - port, port, - CodecUtils.toHex(address), address.length == 4 ? "IPv4" : "IPv6", host, - forward ? "true" : "false" - ); - } -} diff --git a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java deleted file mode 100644 index eae1c8b80..000000000 --- a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/*============================================================================= - * Copyright 2018 572682 - * - * 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 us.dot.its.jpo.ode.inet; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.net.DatagramPacket; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.Arrays; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import us.dot.its.jpo.ode.util.CodecUtils; - -class InetPacketFunctionalTest { - - private static boolean isVerbose = false; - - @Test - void testConstructor() throws UnknownHostException { - InetPacket pkt = new InetPacket("bah.com", 1111, null); - - assertNull(pkt.getPayload()); - } - - @Test - @Disabled - void test() throws UnknownHostException { - test("127.0.0.1", 12, "01234567890".getBytes()); - test("::1", 47561, "0123456789001234567890".getBytes()); - test("1080:0:0:0:8:800:200C:417A", 345, "".getBytes()); - test("1080:0:0:0:8:800:200C:417A", 345, null); - test("1.0.0", 47561, "".getBytes()); - test("::FFFF:129.144.52.38", 4756, "0".getBytes()); - test("1080::8:800:200C:417A", 4756, new byte[] {(byte) 0, (byte) 1}); - test("2001:0:9d38:90d7:3ce3:339d:f5c3:c42b", 11111, new byte[] {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef}); - test(null, 22222, new byte[] {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef}); - test("fdf8:f53b:82e4::53", 11111, new byte[] {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe}); - } - - void test(String address, int port, byte[] payload) throws UnknownHostException { - if (payload == null) { - return; - } - if (isVerbose) { - System.out.println("---------- Test ----------"); - } - DatagramPacket packet = new DatagramPacket(payload, payload.length, InetAddress.getByName(address), port); - InetPacket pbOut = new InetPacket(packet); - test(address, port, payload, pbOut); - } - - void test(String address, int port, byte[] payload, InetPacket pbOut) throws UnknownHostException { - InetPacket pbIn = new InetPacket(pbOut.getBundle()); - if (isVerbose) { - print("From params", pbOut); - print("From bundle", pbIn); - } - InetPoint pointIn = pbIn.getPoint(); - assertNotNull(pointIn); - InetPoint pointOut = pbOut.getPoint(); - assertEquals(pointOut.port, pointIn.port); - assertArrayEquals(pointOut.address, pointIn.address); - byte[] pbOutPayload = pbOut.getPayload(); - byte[] pyInPayload = pbIn.getPayload(); - if (pbOutPayload != null && pbOutPayload != null) { - assertTrue(Arrays.equals(pbOutPayload, pyInPayload)); - } else if (pbOutPayload != null || pbOutPayload != null) { - assertTrue(false); - } - assertTrue(Arrays.equals(pbOut.getBundle(), pbIn.getBundle())); - } - - void print(String header, InetPacket pb) throws UnknownHostException { - assertNotNull(pb); - InetPoint point = pb.getPoint(); - if (point != null) { - System.out.printf("%s: port: %d (0x%x)\n", header, point.port, point.port); - System.out.printf("%s: address size: %d value: %s ip: %s\n", header, point.address.length, - CodecUtils.toHex(point.address), InetAddress.getByAddress(point.address).getHostAddress()); - System.out.printf("%s: forward: %s\n", header, point.forward ? "true" : "false"); - } else { - System.out.printf("%s: Inet point is null\n", header); - } - byte[] p = pb.getPayload(); - System.out.printf("%s payload: %s\n", header, p != null && p.length > 0 ? CodecUtils.toHex(p) : ""); - System.out.printf("%s bundle: %s\n", header, pb.toHexString()); - } - -} diff --git a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketSenderTest.java b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketSenderTest.java deleted file mode 100644 index 05b8e331e..000000000 --- a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketSenderTest.java +++ /dev/null @@ -1,468 +0,0 @@ -/******************************************************************************* - * Copyright 2018 572682 - * - * 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 us.dot.its.jpo.ode.inet; - -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import us.dot.its.jpo.ode.util.CodecUtils; - -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.SocketException; -import java.net.SocketTimeoutException; -import java.net.UnknownHostException; -import java.util.Arrays; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -@Slf4j -public class InetPacketSenderTest { - - static final private boolean isDebugOutput = false; - - private static final int DEFAULT_MAX_PACKET_SIZE = 65535; - - private static final String TRANSPORT_HOST = "localhost"; - private static final int TRANSPORT_PORT = 46751; - - private static final String FORWARDER_HOST = "localhost"; - private static final int FORWARDER_PORT = 46752; - - private static final String CLIENT_HOST_IPV4 = "localhost"; - private static final String CLIENT_HOST_IPV6 = "::1"; - private static final int CLIENT_PORT = 46753; - - private static final byte[] PAYLOAD = new byte[] { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef, (byte) 0xca, - (byte) 0xfe, (byte) 0xba, (byte) 0xbe }; - - AssertionError assertionError = null; - - private enum TestCase { - TestForwardPacketInbound, TestSendPacketOutbound, TestIPv6ForwardOutbound, TestIPv4ForwardOutboundForward, TestIPv4ForwardOutboundSend, TestIPv6SendOutbound, TestIPv4SendOutbound, - }; - - @BeforeAll - public static void init() { - } - - @Test - public void testForwardPacketInboundIPV4() throws UnknownHostException, InetPacketException, InterruptedException { - // Use case: Forwarder forwards incoming IPV4 packet to transport - testForwardPacketInbound(CLIENT_HOST_IPV4); - } - - @Test - public void testForwardPacketInboundIPV6() throws UnknownHostException, InetPacketException, InterruptedException { - // Use case: Forwarder forwards incoming IPV6 packet to transport - testForwardPacketInbound(CLIENT_HOST_IPV6); - } - - public void testForwardPacketInbound(String hostname) - throws UnknownHostException, InetPacketException, InterruptedException { - startUdpListener(TRANSPORT_PORT, TestCase.TestForwardPacketInbound); - InetPoint transport = new InetPoint(getAddressBytes(TRANSPORT_HOST), TRANSPORT_PORT); - InetPacketSender sender = new InetPacketSender(transport); - DatagramPacket packet = new DatagramPacket(PAYLOAD, PAYLOAD.length, InetAddress.getByName(hostname), CLIENT_PORT); - sender.forward(packet); - checkBackgroundThreadAssertion(); - } - - @Test @Disabled - public void testSendPacketOutboundIPv4() throws UnknownHostException, InterruptedException, InetPacketException { - // Use case: Forwarder sends outgoing IPv4 packet out - testSendPacketOutbound(CLIENT_HOST_IPV4); - } - - @Test @Disabled - public void testSendPacketOutboundIPv6() throws UnknownHostException, InterruptedException, InetPacketException { - // Use case: Forwarder sends outgoing IPv6 packet out - testSendPacketOutbound(CLIENT_HOST_IPV6); - } - - public void testSendPacketOutbound(String hostname) - throws UnknownHostException, InetPacketException, InterruptedException { - startUdpListener(CLIENT_PORT, TestCase.TestSendPacketOutbound); - InetPoint client = new InetPoint(getAddressBytes(hostname), CLIENT_PORT); - byte[] bundle = new InetPacket(client, PAYLOAD).getBundle(); - InetPacketSender sender = new InetPacketSender(); - DatagramPacket packet = new DatagramPacket(bundle, bundle.length, InetAddress.getByName(FORWARDER_HOST), - FORWARDER_PORT); - sender.send(packet); - checkBackgroundThreadAssertion(); - } - - @Test - public void testIPv6ForwardOutbound() throws UnknownHostException, InetPacketException, InterruptedException { - // Use case: Transport or Data Sink send IPv6 message out via forwarder - startUdpListener(FORWARDER_PORT, TestCase.TestIPv6ForwardOutbound); - InetPoint forwarder = new InetPoint(getAddressBytes(FORWARDER_HOST), FORWARDER_PORT); - InetPacketSender sender = new InetPacketSender(forwarder); - InetPoint client = new InetPoint(getAddressBytes(CLIENT_HOST_IPV6), CLIENT_PORT); - sender.forward(client, PAYLOAD); - checkBackgroundThreadAssertion(); - } - - @Test - public void testIPv4ForwardOutboundForward() throws InterruptedException, UnknownHostException, InetPacketException { - // Use case: Transport or Data Sink send IPv4 message out via forwarder - startUdpListener(FORWARDER_PORT, TestCase.TestIPv4ForwardOutboundForward); - InetPoint forwarder = new InetPoint(getAddressBytes(FORWARDER_HOST), FORWARDER_PORT); - InetPacketSender sender = new InetPacketSender(forwarder); - sender.setForwardAll(true); - InetPoint client = new InetPoint(getAddressBytes(CLIENT_HOST_IPV4), CLIENT_PORT); - sender.forward(client, PAYLOAD); - checkBackgroundThreadAssertion(); - } - - @Test - public void testIPv4ForwardOutboundSend() throws InterruptedException, UnknownHostException, InetPacketException { - // Use case: Transport or Data Sink send IPv4 message out via forwarder - // but it's being send out directly - startUdpListener(CLIENT_PORT, TestCase.TestIPv4ForwardOutboundSend); - InetPoint forwarder = new InetPoint(getAddressBytes(FORWARDER_HOST), FORWARDER_PORT); - InetPacketSender sender = new InetPacketSender(forwarder); - InetPoint client = new InetPoint(getAddressBytes(CLIENT_HOST_IPV4), CLIENT_PORT); - sender.forward(client, PAYLOAD); - checkBackgroundThreadAssertion(); - } - - @Test @Disabled - public void testIPv6SendOutbound() throws InterruptedException, UnknownHostException, InetPacketException { - // Use case: Transport or Data Sink send IPv6 message out directly - startUdpListener(CLIENT_PORT, TestCase.TestIPv6SendOutbound); - InetPacketSender sender = new InetPacketSender(); - InetPoint client = new InetPoint(getAddressBytes(CLIENT_HOST_IPV6), CLIENT_PORT); - sender.send(client, PAYLOAD); - checkBackgroundThreadAssertion(); - } - - @Test @Disabled - public void testIPv4SendOutbound() throws InterruptedException, UnknownHostException, InetPacketException { - // Use case: Transport or Data Sink send IPv4 message out directly - startUdpListener(CLIENT_PORT, TestCase.TestIPv4SendOutbound); - InetPacketSender sender = new InetPacketSender(); - InetPoint client = new InetPoint(getAddressBytes(CLIENT_HOST_IPV4), CLIENT_PORT); - sender.send(client, PAYLOAD); - checkBackgroundThreadAssertion(); - } - - private static byte[] getAddressBytes(String host) throws UnknownHostException { - return InetAddress.getByName(host).getAddress(); - } - - private void startUdpListener(int port, TestCase tc) { - final int listenPort = port; - final TestCase testCase = tc; - - Thread listener = new Thread(new Runnable() { - @Override - public void run() { - DatagramSocket socket = null; - try { - socket = new DatagramSocket(listenPort); - DatagramPacket datagramPacket = new DatagramPacket(new byte[DEFAULT_MAX_PACKET_SIZE], - DEFAULT_MAX_PACKET_SIZE); - socket.setSoTimeout(1000); - socket.receive(datagramPacket); - validatePacket(datagramPacket); - } catch (SocketTimeoutException ex) { - log.error( - String.format("Caught socket timeout exception while recieving message on port %d. Max size is %d", - listenPort, DEFAULT_MAX_PACKET_SIZE), - ex); - } catch (SocketException ex) { - log.error(String.format("Caught socket exception while recieving message on port %d. Max size is %d", - listenPort, DEFAULT_MAX_PACKET_SIZE), ex); - } catch (IOException ex) { - log.error( - String.format("Caught IO exception exception while recieving message on port %d. Max size is %d", - listenPort, DEFAULT_MAX_PACKET_SIZE), - ex); - } finally { - if (socket != null && !socket.isClosed()) { - socket.close(); - socket = null; - } - } - } - - private void validatePacket(DatagramPacket packet) throws UnknownHostException { - assertNotNull(packet); - - final byte[] data = packet.getData(); - assertNotNull(data); - - final int length = packet.getLength(); - assertTrue(length > 0); - - final int offset = packet.getOffset(); - - byte[] packetData = Arrays.copyOfRange(data, offset, length); - - try { - switch (testCase) { - case TestForwardPacketInbound: - validateForwardPacketInbound(packetData); - validateForwardPacketInbound(packet); - break; - case TestSendPacketOutbound: - validateSendPacketOutbound(packetData); - validateSendPacketOutbound(packet); - break; - case TestIPv6ForwardOutbound: - validateIPv6ForwardOutbound(packetData); - validateIPv6ForwardOutbound(packet); - break; - case TestIPv4ForwardOutboundForward: - validateIPv4ForwardOutboundForward(packetData); - validateIPv4ForwardOutboundForward(packet); - break; - case TestIPv4ForwardOutboundSend: - validateIPv4ForwardOutboundSend(packetData); - validateIPv4ForwardOutboundSend(packet); - break; - case TestIPv6SendOutbound: - validateIPv6SendOutbound(packetData); - validateIPv6SendOutbound(packet); - break; - case TestIPv4SendOutbound: - validateIPv4SendOutbound(packetData); - validateIPv4SendOutbound(packet); - break; - } - } catch (AssertionError ex) { - assertionError = ex; - } - } - - private void validateForwardPacketInbound(byte[] payload) throws UnknownHostException { - validateForwardPacketInbound("Payload", new InetPacket(payload)); - } - - private void validateForwardPacketInbound(DatagramPacket packet) throws UnknownHostException { - validateForwardPacketInbound("Packet", new InetPacket(packet)); - } - - private void validateForwardPacketInbound(String tail, InetPacket p) throws UnknownHostException { - assertNotNull(p); - if (isDebugOutput) - print("ForwardPacketInbound", p); - assertEquals(TRANSPORT_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNotNull(point); - byte[] srcAddress = point.address; - assertNotNull(srcAddress); - if (point.isIPv6Address()) - assertArrayEquals(getAddressBytes(CLIENT_HOST_IPV6), point.address); - else - assertArrayEquals(getAddressBytes(CLIENT_HOST_IPV4), point.address); - assertEquals(CLIENT_PORT, point.port); - assertTrue(point.forward); - assertArrayEquals(PAYLOAD, p.getPayload()); - } - - private void validateSendPacketOutbound(byte[] payload) throws UnknownHostException { - InetPacket p = new InetPacket(payload); - assertNotNull(p); - if (isDebugOutput) - print("SendPacketOutbound Payload", p); - assertEquals(CLIENT_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNull(point); - assertArrayEquals(p.getPayload(), PAYLOAD); - } - - private void validateSendPacketOutbound(DatagramPacket packet) throws UnknownHostException { - InetPacket p = new InetPacket(packet); - assertNotNull(p); - if (isDebugOutput) - print("SendPacketOutbound Packet", p); - assertEquals(CLIENT_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNotNull(point); - byte[] srcAddress = point.address; - assertNotNull(srcAddress); - if (point.isIPv6Address()) - assertArrayEquals(point.address, getAddressBytes(CLIENT_HOST_IPV6)); - else - assertArrayEquals(point.address, getAddressBytes(CLIENT_HOST_IPV4)); - assertFalse(point.forward); - assertArrayEquals(p.getPayload(), PAYLOAD); - } - - private void validateIPv6ForwardOutbound(byte[] payload) throws UnknownHostException { - validateIPv6ForwardOutbound("Payload", new InetPacket(payload)); - } - - private void validateIPv6ForwardOutbound(DatagramPacket packet) throws UnknownHostException { - validateIPv6ForwardOutbound("Packet", new InetPacket(packet)); - } - - private void validateIPv6ForwardOutbound(String tail, InetPacket p) throws UnknownHostException { - assertNotNull(p); - if (isDebugOutput) - print("IPv6ForwardOutbound " + tail, p); - assertEquals(FORWARDER_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNotNull(point); - byte[] srcAddress = point.address; - assertNotNull(srcAddress); - assertTrue(point.isIPv6Address()); - assertArrayEquals(point.address, getAddressBytes(CLIENT_HOST_IPV6)); - assertEquals(CLIENT_PORT, point.port); - assertTrue(point.forward); - assertArrayEquals(p.getPayload(), PAYLOAD); - } - - private void validateIPv4ForwardOutboundForward(byte[] payload) throws UnknownHostException { - validateIPv4ForwardOutboundForward("Payload", new InetPacket(payload)); - } - - private void validateIPv4ForwardOutboundForward(DatagramPacket packet) throws UnknownHostException { - validateIPv4ForwardOutboundForward("Packet", new InetPacket(packet)); - } - - private void validateIPv4ForwardOutboundForward(String tail, InetPacket p) throws UnknownHostException { - assertNotNull(p); - if (isDebugOutput) - print("IPv4ForwardOutboundForward " + tail, p); - assertEquals(FORWARDER_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNotNull(point); - byte[] srcAddress = point.address; - assertNotNull(srcAddress); - assertFalse(point.isIPv6Address()); - assertArrayEquals(point.address, getAddressBytes(CLIENT_HOST_IPV4)); - assertEquals(CLIENT_PORT, point.port); - assertTrue(point.forward); - assertArrayEquals(p.getPayload(), PAYLOAD); - } - - private void validateIPv4ForwardOutboundSend(byte[] payload) throws UnknownHostException { - InetPacket p = new InetPacket(payload); - assertNotNull(p); - if (isDebugOutput) - print("IPv4ForwardOutboundSend Payload", p); - assertEquals(CLIENT_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNull(point); - assertArrayEquals(p.getPayload(), PAYLOAD); - } - - private void validateIPv4ForwardOutboundSend(DatagramPacket packet) throws UnknownHostException { - InetPacket p = new InetPacket(packet); - assertNotNull(p); - if (isDebugOutput) - print("IPv4ForwardOutboundSend Packet", p); - assertEquals(CLIENT_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNotNull(point); - byte[] srcAddress = point.address; - assertNotNull(srcAddress); - if (point.isIPv6Address()) - assertArrayEquals(point.address, getAddressBytes(CLIENT_HOST_IPV6)); - else - assertArrayEquals(point.address, getAddressBytes(CLIENT_HOST_IPV4)); - assertFalse(point.forward); - assertArrayEquals(p.getPayload(), PAYLOAD); - } - - private void validateIPv6SendOutbound(byte[] payload) throws UnknownHostException { - validateIPvXSendOutboundPayload(new InetPacket(payload)); - } - - private void validateIPv4SendOutbound(byte[] payload) throws UnknownHostException { - validateIPvXSendOutboundPayload(new InetPacket(payload)); - } - - private void validateIPv6SendOutbound(DatagramPacket packet) throws UnknownHostException { - validateIPvXSendOutboundPacket(new InetPacket(packet)); - } - - private void validateIPv4SendOutbound(DatagramPacket packet) throws UnknownHostException { - validateIPvXSendOutboundPacket(new InetPacket(packet)); - } - - private void validateIPvXSendOutboundPayload(InetPacket p) throws UnknownHostException { - assertNotNull(p); - if (isDebugOutput) - print("IPvXSendOutbound Payload", p); - assertEquals(CLIENT_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNull(point); - assertArrayEquals(p.getPayload(), PAYLOAD); - } - - private void validateIPvXSendOutboundPacket(InetPacket p) throws UnknownHostException { - assertNotNull(p); - if (isDebugOutput) - print("IPvXSendOutbound Packet", p); - assertEquals(CLIENT_PORT, listenPort); - InetPoint point = p.getPoint(); - assertNotNull(point); - byte[] srcAddress = point.address; - assertNotNull(srcAddress); - if (point.isIPv6Address()) - assertArrayEquals(point.address, getAddressBytes(CLIENT_HOST_IPV6)); - else - assertArrayEquals(point.address, getAddressBytes(CLIENT_HOST_IPV4)); - assertFalse(point.forward); - assertArrayEquals(p.getPayload(), PAYLOAD); - } - - private void print(String header, InetPacket pb) throws UnknownHostException { - assertNotNull(pb); - InetPoint point = pb.getPoint(); - if (point != null) { - System.out.printf("%s: port: %d (0x%x)\n", header, point.port, point.port); - System.out.printf("%s: address size: %d value: %s ip: %s\n", header, point.address.length, - CodecUtils.toHex(point.address), InetAddress.getByAddress(point.address).getHostAddress()); - System.out.printf("%s: forward: %s\n", header, point.forward ? "true" : "false"); - } else { - System.out.printf("%s: Inet point is null\n", header); - } - byte[] p = pb.getPayload(); - System.out.printf("%s: payload: %s\n", header, p != null && p.length > 0 ? CodecUtils.toHex(p) : ""); - System.out.printf("%s: bundle: %s\n", header, pb.toHexString()); - } - }); - listener.start(); - try { - Thread.sleep(500); - } catch (InterruptedException e) { - } - } - - void checkBackgroundThreadAssertion() { - try { - Thread.sleep(1500); - } catch (InterruptedException unused) { - } - - if (assertionError != null) - throw assertionError; - } - -} diff --git a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketTest.java b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketTest.java deleted file mode 100644 index 68cb18db0..000000000 --- a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketTest.java +++ /dev/null @@ -1,146 +0,0 @@ -/******************************************************************************* - * Copyright 2018 572682 - * - * 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 us.dot.its.jpo.ode.inet; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.mock; - -import java.net.DatagramPacket; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import org.junit.jupiter.api.Test; -import org.mockito.MockedStatic; -import org.mockito.Mockito; - -import mockit.Expectations; -import mockit.Mocked; -import us.dot.its.jpo.ode.util.CrcCccitt; -import static org.junit.Assert.assertArrayEquals; - -public class InetPacketTest { - - @Mocked - DatagramPacket mockDatagramPacket; - - @Test - public void testStringConstructorCallsPointConstructor() { - try { - new InetPacket("localhost", 5, new byte[] { 1, 2, 3 }); - } catch (UnknownHostException e) { - fail("Unexpected exception: " + e); - } - } - - @Test - void testDatagramPacketConstructor() { - try (MockedStatic mockedInetAddress = Mockito.mockStatic(InetAddress.class)) { - mockedInetAddress.when(() -> InetAddress.getByName(Mockito.anyString())).thenReturn(mock(InetAddress.class)); - mockDatagramPacket = new DatagramPacket(new byte[] { 1, 2, 3 }, 3); - new InetPacket(mockDatagramPacket); - } - } - -@Test -public void testByteConstructor() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - assertArrayEquals(new byte[] { 1, 2, 3 }, testPacket.getPayload()); -} - - /* - * @Test public void testEvenNum() { boolean ans = false; boolean val; byte[] - * bundle = null; - * - * - * val = InetPacket.parseBundle(bundle); assertEquals(ans,val); } - */ - -@Test -public void testParseBundleNull() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = null; - - assertFalse(testPacket.parseBundle(bundle)); -} - -@Test -public void testParseBundleNotMagic() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = new byte[] { 1, 2, 3, 4, 5, 6 }; - assertFalse(testPacket.parseBundle(bundle)); -} - -@Test -public void testParseBundleMagic() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = new byte[] { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; - assertFalse(testPacket.parseBundle(bundle)); -} - -@Test -public void testParseBundleMagicOther() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = new byte[] { 1, 1, 1, 1, 1 }; - assertFalse(testPacket.parseBundle(bundle)); -} - - @Test - public void testParseBundleMaxLength() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 }; - assertFalse(testPacket.parseBundle(bundle)); - } - - @Test - public void testParseBundleMaxMaxLength() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = new byte[] { 9, 8, 2, 4, 5, 1, 6, 5, 3 }; - assertFalse(testPacket.parseBundle(bundle)); - } - - @Test - public void testSetByteBuffer() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = new byte[] { 58, (byte) 143, 5, (byte) 197, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; - assertFalse(testPacket.parseBundle(bundle)); - } - - @Test - public void testParseBundleAddressLengthLessThanRemaining() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = new byte[] { 58, (byte) 143, 5, (byte) 197, 1, 2, 3, 4, 9, 1, 1, 1, 1, 1, 1, 1, 1 }; - - assertFalse(testPacket.parseBundle(bundle)); - - } - - @Test - public void testParseBundleCrcCccittReturnsTrue() { - InetPacket testPacket = new InetPacket(new byte[] { 1, 2, 3 }); - byte[] bundle = new byte[] { 58, (byte) 143, 5, (byte) 197, 1, 2, 3, 4, 9, 1, 1, 1, 1, 1, 1, 1, 1 }; - - new Expectations() { - { - CrcCccitt.isValidMsgCRC((byte[]) any, anyInt, anyInt); - result = true; - } - }; - assertFalse(testPacket.parseBundle(bundle)); - - } - -} \ No newline at end of file diff --git a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPointTest.java b/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPointTest.java deleted file mode 100644 index df85098db..000000000 --- a/jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPointTest.java +++ /dev/null @@ -1,140 +0,0 @@ -/******************************************************************************* - * Copyright 2018 572682 - * - * 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 us.dot.its.jpo.ode.inet; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import org.junit.jupiter.api.Test; - -import mockit.Capturing; -import mockit.Expectations; - -public class InetPointTest { - - - @Test - public void testStringConstructorCreatesAddress() { - try { - new Expectations() { - { - InetAddress.getByName(anyString).getAddress(); - } - }; - - new InetPoint("bah.com", 5, true); - } catch (UnknownHostException e) { - fail("Unexpected exception: " + e); - } - } - - @Test - public void testStringConstructorFailsNullAddress() { - assertThrows(AssertionError.class, () -> { - try { - new Expectations() { - { - InetAddress.getByName(anyString).getAddress(); - result = null; - } - }; - new InetPoint("something123", 5, true); - } catch (UnknownHostException e) { - fail("Unexpected exception: " + e); - } - }); - } - - @Test - public void testByteConstructorCreatesAddress() { - new InetPoint(new byte[] { 1, 2, 3 }, 5, true); - } - - @Test - public void testByteConstructorFailsNullAddress() { - Exception err = assertThrows(IllegalArgumentException.class, () -> { - new InetPoint((byte[]) null, 5, true); - }); - - assertEquals("IP Address is required", err.getMessage()); - } - - @Test - public void testBBytePortConstructorCreatesAddress() { - new InetPoint(new byte[] { 1, 2, 3 }, 5); - } - - @Test - public void testBytePortConstructorFailsNullAddress() { - Exception err = assertThrows(IllegalArgumentException.class, () -> { - new InetPoint((byte[]) null, 5); - }); - assertEquals("IP Address is required", err.getMessage()); - } - - @Test - public void getInetAddressCallsGetAddress() { - try { - new Expectations() { - { - InetAddress.getByAddress(new byte[]{127, 0, 0, 1}); - } - }; - new InetPoint(new byte[] { 1, 2, 3 ,0}, 5).getInetAddress(); - } catch (UnknownHostException e) { - fail("Unexpected exception: " + e); - } - } - - @Test - public void testAddressLength16IsIpv6() { - assertTrue(new InetPoint(new byte[16], 5).isIPv6Address()); - } - - @Test - public void testAddressLength4IsNotIpv6() { - assertFalse(new InetPoint(new byte[4], 5).isIPv6Address()); - } - - @Test - public void testToStringMethodIPV6() { - assertEquals( - "InetPoint { port = 5 (0x5); address = 00000000000000000000000000000000 (IPv6, 0:0:0:0:0:0:0:0); forward = false }", - new InetPoint(new byte[16], 5).toString()); - } - - @Test - public void testToStringException() { - assertEquals( - "InetPoint { port = 5 (0x5); address = 00000000000000000000000000000000 (IPv6, 0:0:0:0:0:0:0:0); forward = false }", - new InetPoint(new byte[16], 5).toString()); - } - - @Test - public void testToStringMethodIPV4() { - assertEquals("InetPoint { port = 5 (0x5); address = 00000000 (IPv4, 0.0.0.0); forward = false }", - new InetPoint(new byte[4], 5).toString()); - } - -}