-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from CDOT-CV/mcook42/fix/inet-test
Fix: update InetPacketFunctionalTest to prevent flaky test failures
- Loading branch information
Showing
2 changed files
with
146 additions
and
134 deletions.
There are no files selected for viewing
117 changes: 64 additions & 53 deletions
117
jpo-ode-common/src/main/java/us/dot/its/jpo/ode/inet/InetPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,84 @@ | ||
/******************************************************************************* | ||
/*============================================================================= | ||
* 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 org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
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 { | ||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
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) { | ||
logger.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" | ||
); | ||
} | ||
} |
163 changes: 82 additions & 81 deletions
163
jpo-ode-common/src/test/java/us/dot/its/jpo/ode/inet/InetPacketFunctionalTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,108 @@ | ||
/******************************************************************************* | ||
/*============================================================================= | ||
* 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; | ||
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.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; | ||
class InetPacketFunctionalTest { | ||
|
||
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); | ||
} | ||
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())); | ||
} | ||
|
||
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) : "<empty>"); | ||
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) : "<empty>"); | ||
System.out.printf("%s bundle: %s\n", header, pb.toHexString()); | ||
} | ||
|
||
} |