From 1f761f3591242a4ef2b6c7630342e9ecd402574f Mon Sep 17 00:00:00 2001 From: IanHowell Date: Fri, 27 Apr 2018 10:32:53 -0500 Subject: Improve coverage of cadi-client Issue-ID: AAF-224 Change-Id: I950cffc889945205fada35b6f1d772c5318ba3b4 Signed-off-by: IanHowell --- .../java/org/onap/aaf/cadi/locator/DNSLocator.java | 69 +++++++++++++--------- 1 file changed, 41 insertions(+), 28 deletions(-) (limited to 'cadi/client/src/main') diff --git a/cadi/client/src/main/java/org/onap/aaf/cadi/locator/DNSLocator.java b/cadi/client/src/main/java/org/onap/aaf/cadi/locator/DNSLocator.java index 655a0c22..ed60b877 100644 --- a/cadi/client/src/main/java/org/onap/aaf/cadi/locator/DNSLocator.java +++ b/cadi/client/src/main/java/org/onap/aaf/cadi/locator/DNSLocator.java @@ -73,34 +73,7 @@ public class DNSLocator implements Locator { throw new LocatorException("DNSLocator accepts only https or http protocols. (requested URL " + aaf_locate + ')'); } - int colon = aaf_locate.indexOf(':',start); - int slash; - if(colon>0) { - start = colon+1; - int left = aaf_locate.indexOf('[',start); - if(left>0) { - int right = aaf_locate.indexOf(']',left+1); - if(right>0) { - int dash = aaf_locate.indexOf('-',left+1); - if(dash<0) { - startPort = endPort = Integer.parseInt(aaf_locate.substring(left+1,right)); - } else { - startPort = Integer.parseInt(aaf_locate.substring(left+1,dash)); - endPort = Integer.parseInt(aaf_locate.substring(dash + 1,right)); - } - } - - } else { - slash = aaf_locate.indexOf('/',colon+1); - if(slash<0) { - startPort = endPort = Integer.parseInt(aaf_locate.substring(start)); - } else { - startPort = endPort = Integer.parseInt(aaf_locate.substring(start,slash)); - } - } - } else { - startPort = endPort = port; - } + parsePorts(aaf_locate.substring(start), port); } @Override @@ -185,6 +158,46 @@ public class DNSLocator implements Locator { } return false; } + + private void parsePorts(String aaf_locate, int defaultPort) throws LocatorException { + int slash, start; + int colon = aaf_locate.indexOf(':'); + if(colon > 0) { + start = colon + 1; + int left = aaf_locate.indexOf('[', start); + if(left > 0) { + int right = aaf_locate.indexOf(']', left + 1); + if (right < 0) { + throw new LocatorException("Missing closing bracket in DNSLocator constructor. (requested URL " + aaf_locate + ')'); + } else if (right == (left + 1)) { + throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')'); + } + int dash = aaf_locate.indexOf('-', left + 1); + if (dash == (right - 1) || dash == (left + 1)) { + throw new LocatorException("Missing ports in brackets in DNSLocator constructor. (requested URL " + aaf_locate + ')'); + } + if(dash < 0) { + startPort = endPort = Integer.parseInt(aaf_locate.substring(left + 1, right)); + } else { + startPort = Integer.parseInt(aaf_locate.substring(left + 1, dash)); + endPort = Integer.parseInt(aaf_locate.substring(dash + 1, right)); + } + + } else { + slash = aaf_locate.indexOf('/', start); + if (slash == start) { + throw new LocatorException("Missing port before '/' in DNSLocator constructor. (requested URL " + aaf_locate + ')'); + } + if(slash < 0) { + startPort = endPort = Integer.parseInt(aaf_locate.substring(start)); + } else { + startPort = endPort = Integer.parseInt(aaf_locate.substring(start, slash)); + } + } + } else { + startPort = endPort = defaultPort; + } + } private class Host { private URI uri; -- cgit 1.2.3-korg