Platform { String produceLinkLocalIp() { try { Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); while (nics.hasMoreElements()) { NetworkInterface nic = nics.nextElement(); Enumeration<InetAddress> addresses = nic.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = addresses.nextElement(); if (address.isSiteLocalAddress()) return address.getHostAddress(); } } } catch (Exception e) { log("error reading nics", e); } return null; }  @Nullable abstract String getHostString(InetSocketAddress socket); @Nullable String linkLocalIp(); AssertionError assertionError(String message, Throwable cause); static Platform get(); void log(String msg, @Nullable Throwable thrown); void log(String msg, Object param1, @Nullable Throwable thrown); abstract long randomLong(); abstract long nextTraceIdHigh(); Clock clock();  }
@Test public void produceLinkLocalIp_exceptionReadingNics() throws Exception { mockStatic(NetworkInterface.class); when(NetworkInterface.getNetworkInterfaces()).thenThrow(SocketException.class); assertThat(platform.produceLinkLocalIp()) .isNull(); }
@Test public void produceLinkLocalIp_noAddresses() throws Exception { nicWithAddress(null); assertThat(platform.produceLinkLocalIp()) .isNull(); }
@Test public void produceLinkLocalIp_siteLocal_ipv4() throws Exception { nicWithAddress(InetAddress.getByAddress("local", new byte[] {(byte) 192, (byte) 168, 0, 1})); assertThat(platform.produceLinkLocalIp()) .isEqualTo("192.168.0.1"); }
@Test public void produceLinkLocalIp_siteLocal_ipv6() throws Exception { InetAddress ipv6 = Inet6Address.getByName("fec0:db8::c001"); nicWithAddress(ipv6); assertThat(platform.produceLinkLocalIp()) .isEqualTo(ipv6.getHostAddress()); }
@Test public void produceLinkLocalIp_notSiteLocal_ipv4() throws Exception { nicWithAddress(InetAddress.getByAddress("external", new byte[] {1, 2, 3, 4})); assertThat(platform.produceLinkLocalIp()) .isNull(); }
@Test public void produceLinkLocalIp_notSiteLocal_ipv6() throws Exception { nicWithAddress(Inet6Address.getByName("2001:db8::c001")); assertThat(platform.produceLinkLocalIp()) .isNull(); }