Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature/nro-profile] Add reverse domain check (fixes #27).
- Loading branch information
Showing
5 changed files
with
309 additions
and
1 deletion.
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
114 changes: 114 additions & 0 deletions
114
src/main/java/net/apnic/rdap/conformance/valuetest/ReverseDomainMatch.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,114 @@ | ||
package net.apnic.rdap.conformance.valuetest; | ||
|
||
import net.apnic.rdap.conformance.Result; | ||
import net.apnic.rdap.conformance.Context; | ||
import net.apnic.rdap.conformance.ValueTest; | ||
import net.apnic.rdap.conformance.Utils; | ||
|
||
import net.ripe.ipresource.IpResource; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* <p>ReverseDomainMatch class.</p> | ||
* | ||
* See RFC 9083 [5.3]. | ||
* | ||
* @author Tom Harrison <tomh@apnic.net> | ||
* @version 0.7-SNAPSHOT | ||
*/ | ||
public final class ReverseDomainMatch implements ValueTest { | ||
/** | ||
* <p>Constructor for ReverseDomainMatch.</p> | ||
*/ | ||
public ReverseDomainMatch() { } | ||
|
||
/** {@inheritDoc} */ | ||
public boolean run(final Context context, final Result proto, | ||
final Object argData) { | ||
Map<String, Object> object = | ||
Utils.castToMap(context, proto, argData); | ||
if (object == null) { | ||
return false; | ||
} | ||
|
||
/* Get the domain name for this object. */ | ||
String domainName = Utils.castToString(object.get("ldhName")); | ||
if (domainName == null) { | ||
return true; | ||
} | ||
if (!domainName.matches(".*\\.arpa\\.?")) { | ||
return true; | ||
} | ||
String prefix = Utils.arpaToPrefix(domainName); | ||
if (prefix.equals("")) { | ||
Result res = new Result(proto); | ||
res.setStatus(Result.Status.Failure); | ||
res.setInfo("invalid reverse domain name"); | ||
context.addResult(res); | ||
return false; | ||
} | ||
|
||
IpResource domainIp; | ||
try { | ||
domainIp = IpResource.parse(prefix); | ||
} catch (Exception e) { | ||
Result res = new Result(proto); | ||
res.setStatus(Result.Status.Failure); | ||
res.setInfo("invalid reverse domain name"); | ||
context.addResult(res); | ||
return false; | ||
} | ||
|
||
/* Get the network for this object (if present). */ | ||
Map<String, Object> networkObject = | ||
Utils.castToMap(context, proto, object.get("network")); | ||
if (networkObject == null) { | ||
return true; | ||
} | ||
|
||
String startAddress = | ||
Utils.castToString(networkObject.get("startAddress")); | ||
if (startAddress == null) { | ||
Result res = new Result(proto); | ||
res.setStatus(Result.Status.Failure); | ||
res.setInfo("network does not contain start address"); | ||
context.addResult(res); | ||
return false; | ||
} | ||
String endAddress = | ||
Utils.castToString(networkObject.get("endAddress")); | ||
if (endAddress == null) { | ||
Result res = new Result(proto); | ||
res.setStatus(Result.Status.Failure); | ||
res.setInfo("network does not contain end address"); | ||
context.addResult(res); | ||
return false; | ||
} | ||
|
||
IpResource networkIp; | ||
try { | ||
networkIp = IpResource.parse(startAddress + "-" + | ||
endAddress); | ||
} catch (Exception e) { | ||
Result res = new Result(proto); | ||
res.setStatus(Result.Status.Failure); | ||
res.setInfo("invalid network start/end address"); | ||
context.addResult(res); | ||
return false; | ||
} | ||
|
||
/* Confirm that the domain range is contained within the | ||
* network. */ | ||
Result res = new Result(proto); | ||
boolean contains = networkIp.contains(domainIp); | ||
if (contains) { | ||
res.setStatus(Result.Status.Success); | ||
} else { | ||
res.setStatus(Result.Status.Failure); | ||
} | ||
res.setInfo("network range contains domain range"); | ||
context.addResult(res); | ||
return contains; | ||
} | ||
} |
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,72 @@ | ||
package net.apnic.rdap.conformance; | ||
|
||
import org.testng.annotations.Test; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
import static org.testng.Assert.assertNull; | ||
|
||
import net.apnic.rdap.conformance.Utils; | ||
import java.io.*; | ||
|
||
public class UtilsTest | ||
{ | ||
public UtilsTest() | ||
{ | ||
} | ||
|
||
@Test | ||
public void testIpv4ArpaToPrefix() throws Exception | ||
{ | ||
String s; | ||
|
||
s = Utils.arpaToPrefix("in-addr.arpa"); | ||
assertEquals(s, "", | ||
"Got empty string for invalid domain"); | ||
|
||
s = Utils.arpaToPrefix("1.in-addr.arpa"); | ||
assertEquals(s, "1.0.0.0/8", | ||
"Got correct prefix for a /8"); | ||
|
||
s = Utils.arpaToPrefix("123.123.in-addr.arpa"); | ||
assertEquals(s, "123.123.0.0/16", | ||
"Got correct prefix for a /16"); | ||
|
||
s = Utils.arpaToPrefix("1.2.3.in-addr.arpa"); | ||
assertEquals(s, "3.2.1.0/24", | ||
"Got correct prefix for a /24"); | ||
|
||
s = Utils.arpaToPrefix("4.3.2.1.in-addr.arpa"); | ||
assertEquals(s, "1.2.3.4/32", | ||
"Got correct prefix for a /32"); | ||
|
||
s = Utils.arpaToPrefix("5.4.3.2.1.in-addr.arpa"); | ||
assertEquals(s, "", | ||
"Got empty string for invalid domain (too many segments)"); | ||
|
||
s = Utils.arpaToPrefix("a.b.c.in-addr.arpa"); | ||
assertEquals(s, "", | ||
"Got empty string for invalid domain (contains letters)"); | ||
} | ||
|
||
@Test | ||
public void testIpv6ArpaToPrefix() throws Exception | ||
{ | ||
String s; | ||
|
||
s = Utils.arpaToPrefix("ip6.arpa"); | ||
assertEquals(s, "", | ||
"Got empty string for invalid domain"); | ||
|
||
s = Utils.arpaToPrefix("a.ip6.arpa"); | ||
assertEquals(s, "a000::/4", | ||
"Got correct prefix for a /4"); | ||
|
||
s = Utils.arpaToPrefix("a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.ip6.arpa"); | ||
assertEquals(s, "dcba:dcba:dcba:dcba:dcba:dcba:dcba:dcba/128", | ||
"Got correct prefix for a /128"); | ||
|
||
s = Utils.arpaToPrefix("a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.d.a.b.c.ip6.arpa"); | ||
assertEquals(s, "cbad:cbad:cbad:cbad:cbad:cbad:cbad:cba0/124", | ||
"Got correct prefix for a /124"); | ||
} | ||
} |