Coverage Summary for Class: InetAddressUtils (co.rsk.scoring)

Class Class, % Method, % Line, %
InetAddressUtils 0% (0/1) 0% (0/4) 0% (0/25)


1 package co.rsk.scoring; 2  3 import javax.annotation.CheckForNull; 4 import java.net.InetAddress; 5 import java.net.UnknownHostException; 6  7 /** 8  * InetAddressUtils has static methods 9  * to perform operations on InetAddress and InetAddressBlock 10  * given an String representation 11  * <p> 12  * Created by ajlopez on 15/07/2017. 13  */ 14 public final class InetAddressUtils { 15  private InetAddressUtils() {} 16  17  /** 18  * Returns <tt>true</tt> if the specified texts represent an address with mask 19  * ie "192.168.51.1/16" has a mask 20  * "192.168.51.1" has no mask 21  * 22  * @param text the address 23  * 24  * @return <tt>true</tt> or <tt>false</tt> 25  */ 26  public static boolean hasMask(String text) { 27  if (text == null) { 28  return false; 29  } 30  31  String[] parts = text.split("/"); 32  33  return parts.length == 2 && parts[0].length() != 0 && parts[1].length() != 0; 34  } 35  36  /** 37  * Convert a text representation to an InetAddress 38  * It supports IPV4 and IPV6 formats 39  * 40  * @param hostname the address 41  * 42  * @return the text converted to an InetAddress 43  */ 44  public static InetAddress getAddressForBan(@CheckForNull String hostname) throws InvalidInetAddressException { 45  if (hostname == null) { 46  throw new InvalidInetAddressException("null address", null); 47  } 48  49  String name = hostname.trim(); 50  if (name.length() == 0) { 51  throw new InvalidInetAddressException("empty address", null); 52  } 53  54  //TODO(mmarquez): should we validate address format ?? 55  try { 56  InetAddress address = InetAddress.getByName(name); 57  58  if (address.isLoopbackAddress() || address.isAnyLocalAddress()) { 59  throw new InvalidInetAddressException("local address: '" + name + "'", null); 60  } 61  62  return address; 63  } 64  catch (UnknownHostException ex) { 65  throw new InvalidInetAddressException("unknown host: '" + name + "'", ex); 66  } 67  } 68  69  /** 70  * Convert a text representation to an InetAddressBlock 71  * (@see InetAddressBlock) 72  * It supports IPV4 and IPV6 formats 73  * ie "192.168.51.1/16" is a valid text 74  * 75  * @param text the address with mask 76  * 77  * @return the text converted to an InetAddressBlock 78  * @throws InvalidInetAddressException if the text is invalid 79  */ 80  public static InetAddressBlock parse(String text) throws InvalidInetAddressException { 81  //TODO(mmarquez): should we validate address format ?? 82  String[] parts = text.split("/"); 83  84  InetAddress address; 85  86  address = InetAddressUtils.getAddressForBan(parts[0]); 87  88  int nbits; 89  90  try { 91  nbits = Integer.parseInt(parts[1]); 92  } 93  catch (NumberFormatException ex) { 94  throw new InvalidInetAddressBlockException("Invalid mask", ex); 95  } 96  97  if (nbits <= 0 || nbits > address.getAddress().length * 8) { 98  throw new InvalidInetAddressBlockException("Invalid mask", null); 99  } 100  101  return new InetAddressBlock(address, nbits); 102  } 103 }