-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
PR for #14 - Change DeviceUtil to strip `[]` from IP addresses
- Loading branch information
Showing
7 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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
128 changes: 128 additions & 0 deletions
128
event-logging-base/src/test/java/event/logging/base/util/TestDeviceUtil.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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package event.logging.base.util; | ||
|
||
import event.logging.Device; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
|
||
class TestDeviceUtil { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(TestDeviceUtil.class); | ||
|
||
@ParameterizedTest | ||
@CsvSource(value = { | ||
"'[0:0:0:0:0:0:0:1]','0:0:0:0:0:0:0:1'", | ||
"'[]',''", | ||
"'NULL','NULL'", | ||
"'',''", | ||
}) | ||
void stripIpv6Brackets(final String input, final String expectedOutput) { | ||
final String cleanedIp = DeviceUtil.stripIpv6Brackets(input); | ||
|
||
Assertions.assertThat(cleanedIp) | ||
.isEqualTo(expectedOutput); | ||
|
||
// Now clean the cleaned thing to make sure it is unchanged. | ||
final String cleanedIp2 = DeviceUtil.stripIpv6Brackets(cleanedIp); | ||
|
||
Assertions.assertThat(cleanedIp2) | ||
.isEqualTo(cleanedIp); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(value = { | ||
"'192.168.0.1','192.168.0.1'", | ||
"' 192.168.0.1','192.168.0.1'", | ||
"'192.168.0.1 ','192.168.0.1'", | ||
"' 192.168.0.1 ','192.168.0.1'", | ||
"'0:0:0:0:0:0:0:1','0:0:0:0:0:0:0:1'", | ||
"'[0:0:0:0:0:0:0:1]','0:0:0:0:0:0:0:1'", | ||
"' [0:0:0:0:0:0:0:1] ','0:0:0:0:0:0:0:1'", | ||
"'NULL','NULL'", | ||
"'UNKNOWN','NULL'", | ||
"'','NULL'", | ||
}, nullValues = {"NULL"}) | ||
void getValidIP(final String input, final String expectedOutput) { | ||
|
||
final String actual = DeviceUtil.getValidIP(input); | ||
LOGGER.debug("input [{}], expectedOutput [{}], actual [{}]", input, expectedOutput, actual); | ||
Assertions.assertThat(actual) | ||
.isEqualTo(expectedOutput); | ||
} | ||
|
||
@Test | ||
void createDeviceFromInetAddress_localhost() throws UnknownHostException { | ||
final InetAddress inetAddress = InetAddress.getLocalHost(); | ||
final Device device = DeviceUtil.createDeviceFromInetAddress(inetAddress); | ||
logDeviceAddressDetails(device); | ||
Assertions.assertThat(device.getIPAddress()) | ||
.matches("^(10\\.|127\\.|0{1,3}:).*"); | ||
|
||
Assertions.assertThat(device.getHostName()) | ||
.isNotNull(); | ||
} | ||
|
||
@Test | ||
void createDeviceFromInetAddress_fromHost() throws UnknownHostException { | ||
final InetAddress inetAddress = InetAddress.getByName("10.0.0.9"); | ||
final Device device = DeviceUtil.createDeviceFromInetAddress(inetAddress); | ||
logDeviceAddressDetails(device); | ||
|
||
Assertions.assertThat(device.getIPAddress()) | ||
.isEqualTo("10.0.0.9"); | ||
// Can't resolve a hostname for this ip so host name will be null | ||
Assertions.assertThat(device.getHostName()) | ||
.isNull(); | ||
} | ||
|
||
@Test | ||
void createDeviceFromInetAddress_ipv6() throws UnknownHostException { | ||
final InetAddress inetAddress = InetAddress.getByName("[0:0:0:0:0:0:0:1]"); | ||
final Device device = DeviceUtil.createDeviceFromInetAddress(inetAddress); | ||
logDeviceAddressDetails(device); | ||
|
||
Assertions.assertThat(device.getIPAddress()) | ||
.isEqualTo("0:0:0:0:0:0:0:1"); | ||
// Will be localhost's hostname | ||
Assertions.assertThat(device.getHostName()) | ||
.isNotNull(); | ||
} | ||
|
||
@Test | ||
void createDeviceFromInetAddress_ipv6_2() throws UnknownHostException { | ||
final InetAddress inetAddress = InetAddress.getByName("0:0:0:0:0:0:0:1"); | ||
final Device device = DeviceUtil.createDeviceFromInetAddress(inetAddress); | ||
logDeviceAddressDetails(device); | ||
|
||
Assertions.assertThat(device.getIPAddress()) | ||
.isEqualTo("0:0:0:0:0:0:0:1"); | ||
// Will be localhost's hostname | ||
Assertions.assertThat(device.getHostName()) | ||
.isNotNull(); | ||
} | ||
|
||
private void logDeviceAddressDetails(final Device device) { | ||
LOGGER.debug("Device: (ip: [{}], host: [{}], mac: [{}])", | ||
device.getIPAddress(), | ||
device.getHostName(), | ||
device.getMACAddress()); | ||
} | ||
|
||
@Test | ||
void createDevice() { | ||
final String hostname = "host.domain"; | ||
final String ip = "192.168.0.1"; | ||
final Device device = DeviceUtil.createDevice(hostname, ip); | ||
logDeviceAddressDetails(device); | ||
|
||
Assertions.assertThat(device.getHostName()) | ||
.isEqualTo(hostname); | ||
Assertions.assertThat(device.getIPAddress()) | ||
.isEqualTo(ip); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
* Issue **#14** : Change DeviceUtil to strip square brackets from IPv6 addresses to ensure they are valid in the Schema. | ||
|
||
|
||
```sh | ||
# ******************************************************************************** | ||
# Issue title: Change DeviceUtil to strip `[]` from IP addresses | ||
# Issue link: https://github.com/gchq/event-logging/issues/14 | ||
# ******************************************************************************** | ||
|
||
# ONLY the top line will be included as a change entry in the CHANGELOG. | ||
# The entry should be in GitHub flavour markdown and should be written on a SINGLE | ||
# line with no hard breaks. You can have multiple change files for a single GitHub issue. | ||
# The entry should be written in the imperative mood, i.e. 'Fix nasty bug' rather than | ||
# 'Fixed nasty bug'. | ||
# | ||
# Examples of acceptable entries are: | ||
# | ||
# | ||
# * Issue **123** : Fix bug with an associated GitHub issue in this repository | ||
# | ||
# * Issue **namespace/other-repo#456** : Fix bug with an associated GitHub issue in another repository | ||
# | ||
# * Fix bug with no associated GitHub issue. | ||
``` |