RDataUtil { public static String getDnsLabelFromRData(String rData) { if(rData == null || rData.trim().isEmpty()) { throw new IllegalArgumentException( "rData cannot be null, empty, or blank" ); } if(rData.contains( Constants.LABEL )) { return rData.substring(0, rData.indexOf(Constants.LABEL)); } Name nameInRData = null; try{ nameInRData = new Name( rData.trim() ); } catch(TextParseException tpe){ throw new IllegalArgumentException( "rData must be valid domain name" ); } if(nameInRData.labels() < 2) { throw new IllegalArgumentException( "rData does not have enough labels to return dns label for a service type" ); } String transportProtocolLabel = nameInRData.getLabelString( 1 ); if(transportProtocolLabel.equals( Constants.TCP ) || transportProtocolLabel.equals( Constants.UDP )) { return nameInRData.getLabelString( 0 ) + Constants.DNS_LABEL_DELIMITER + transportProtocolLabel; } throw new IllegalArgumentException( "Could not extract DNS Label from rData" ); } private  RDataUtil(); static String getDnsLabelFromRData(String rData); static String getServiceTypeNameFromRData(String rData); static String getServiceTypeRData(String rData);  }
@Test public void getDnsFromLabelFromRDataTest () { String testRData = ""; String nullString = null; testRData = "_0._tcp"; Assert.assertTrue("_0._tcp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_0._tcp" + Constants.LABEL; Assert.assertTrue("_0._tcp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_0._tcp" + Constants.NAME; Assert.assertTrue("_0._tcp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_0._tcp.asodweasdfj.com"; Assert.assertTrue("_0._tcp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_0._udp.asodweasdfj.com"; Assert.assertTrue("_0._udp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_0._udp.asodweasdfj.1.com"; Assert.assertTrue("_0._udp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_0._udp.asodweasdfj.1.aa.23.asasfd.com"; Assert.assertTrue("_0._udp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_0._udp.asodweasdfj"; Assert.assertTrue("_0._udp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_65535._tcp.asodweasdfj.com"; Assert.assertTrue("_65535._tcp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_65535._udp.asodweasdfj.com"; Assert.assertTrue("_65535._udp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_65535._udp.asodweasdfj.1.com"; Assert.assertTrue("_65535._udp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_65535._udp.asodweasdfj.1.aa.23.asasfd.com"; Assert.assertTrue("_65535._udp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); testRData = "_65535._udp.asodweasdfj"; Assert.assertTrue("_65535._udp".equals( RDataUtil.getDnsLabelFromRData( testRData ) )); }
@Test(expected = IllegalArgumentException.class) public void nullStringTest(){ RDataUtil.getDnsLabelFromRData( null ); }
@Test(expected = IllegalArgumentException.class) public void emptyStringTest(){ RDataUtil.getDnsLabelFromRData( "" ); }
@Test(expected = IllegalArgumentException.class) public void blankStringTest(){ RDataUtil.getDnsLabelFromRData( " " ); }
@Test(expected = IllegalArgumentException.class) public void oneLabelStringTest(){ RDataUtil.getDnsLabelFromRData( ".com" ); }
@Test(expected = IllegalArgumentException.class) public void oneLabelTCPStringTest(){ RDataUtil.getDnsLabelFromRData( "._tcp" ); }
@Test(expected = IllegalArgumentException.class) public void oneLabelUDPStringTest(){ RDataUtil.getDnsLabelFromRData( "._udp" ); }
@Test(expected = IllegalArgumentException.class) public void emptyLabelUDPStringTest(){ RDataUtil.getDnsLabelFromRData( " ._udp" ); }
@Test(expected = IllegalArgumentException.class) public void emptyLabelTLDStringTest(){ RDataUtil.getDnsLabelFromRData( " .com" ); }
@Test(expected = IllegalArgumentException.class) public void validDomainNoDNSLabelTest(){ RDataUtil.getDnsLabelFromRData( "abc.verisign.com" ); }
@Test(expected = IllegalArgumentException.class) public void longerValidDomainNoDNSLabelTest(){ RDataUtil.getDnsLabelFromRData( "1.abc.xyz.verisign.com" ); }