Search in sources :

Example 81 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpNotifyReceiverTest method onReceive_異なるセグメントからのメッセージは無視する.

@Test
public void onReceive_異なるセグメントからのメッセージは無視する() throws Exception {
    final SsdpNotifyReceiver receiver = spy(new SsdpNotifyReceiver(Address.IP_V4, NetworkUtils.getAvailableInet4Interfaces().get(0)));
    final InterfaceAddress address = TestUtils.createInterfaceAddress("192.0.2.1", "255.255.0.0", 24);
    doReturn(address).when(receiver).getInterfaceAddress();
    final NotifyListener listener = mock(NotifyListener.class);
    receiver.setNotifyListener(listener);
    final byte[] data = TestUtils.getResourceAsByteArray("ssdp-notify-alive0.bin");
    receiver.onReceive(InetAddress.getByName("192.1.2.2"), data, data.length);
    verify(listener, never()).onReceiveNotify(ArgumentMatchers.any(SsdpRequest.class));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NotifyListener(net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener) Test(org.junit.Test)

Example 82 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpNotifyReceiverTest method onReceive_IOExceptionが発生してもクラッシュしない.

@Test
public void onReceive_IOExceptionが発生してもクラッシュしない() throws Exception {
    final SsdpNotifyReceiver receiver = spy(new SsdpNotifyReceiver(Address.IP_V4, NetworkUtils.getAvailableInet4Interfaces().get(0)));
    final InterfaceAddress address = TestUtils.createInterfaceAddress("192.0.2.1", "255.255.0.0", 24);
    doReturn(address).when(receiver).getInterfaceAddress();
    doThrow(new IOException()).when(receiver).createSsdpRequestMessage(ArgumentMatchers.any(byte[].class), anyInt());
    final NotifyListener listener = mock(NotifyListener.class);
    receiver.setNotifyListener(listener);
    final byte[] data = TestUtils.getResourceAsByteArray("ssdp-notify-alive0.bin");
    receiver.onReceive(InetAddress.getByName("192.0.2.2"), data, data.length);
    verify(listener, never()).onReceiveNotify(ArgumentMatchers.any(SsdpRequest.class));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) IOException(java.io.IOException) NotifyListener(net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener) Test(org.junit.Test)

Example 83 with InterfaceAddress

use of java.net.InterfaceAddress in project mmupnp by ohmae.

the class SsdpSearchServerTest method search_ST_ROOTDEVICEでのサーチ.

@Test
public void search_ST_ROOTDEVICEでのサーチ() throws Exception {
    final NetworkInterface networkInterface = NetworkUtils.getAvailableInet4Interfaces().get(0);
    final SsdpServerDelegate delegate = spy(new SsdpServerDelegate(mock(Receiver.class), Address.IP_V4, networkInterface));
    final InterfaceAddress interfaceAddress = TestUtils.createInterfaceAddress("192.0.2.2", "255.255.255.0", 16);
    doReturn(interfaceAddress).when(delegate).getInterfaceAddress();
    final MockMulticastSocket socket = new MockMulticastSocket();
    doReturn(socket).when(delegate).createMulticastSocket(anyInt());
    final SsdpSearchServer server = spy(new SsdpSearchServer(delegate));
    server.open();
    server.start();
    server.search(SsdpSearchServer.ST_ROOTDEVICE);
    server.stop();
    server.close();
    final DatagramPacket packet = socket.getSendPacket();
    final SsdpRequest message = new SsdpRequest(mock(InterfaceAddress.class), packet.getData(), packet.getLength());
    assertThat(message.getMethod(), is(SsdpMessage.M_SEARCH));
    assertThat(message.getHeader(Http.ST), is(SsdpSearchServer.ST_ROOTDEVICE));
    assertThat(message.getHeader(Http.MAN), is(SsdpMessage.SSDP_DISCOVER));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) DatagramPacket(java.net.DatagramPacket) NetworkInterface(java.net.NetworkInterface) Test(org.junit.Test)

Example 84 with InterfaceAddress

use of java.net.InterfaceAddress in project wildfly by wildfly.

the class SocketsAndInterfacesTestCase method getNic.

private NetworkInterface getNic(String node) throws SocketException, UnknownHostException {
    if (node == null) {
        return null;
    }
    InetAddress node1Address = InetAddress.getByName(node);
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface nic = interfaces.nextElement();
        if (!nic.isUp()) {
            continue;
        }
        for (InterfaceAddress addr : nic.getInterfaceAddresses()) {
            if (addr.getAddress().equals(node1Address)) {
                return nic;
            }
        }
    }
    // no interface found
    return null;
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 85 with InterfaceAddress

use of java.net.InterfaceAddress in project wildfly by wildfly.

the class SocketsAndInterfacesTestCase method getNonDefaultNic.

private NetworkInterface getNonDefaultNic() throws SocketException, UnknownHostException {
    InetAddress defaultAddr = InetAddress.getByName(url.getHost());
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    while (interfaces.hasMoreElements()) {
        NetworkInterface nic = interfaces.nextElement();
        if (!nic.isUp()) {
            continue;
        }
        for (InterfaceAddress addr : nic.getInterfaceAddresses()) {
            if (addr.getAddress().equals(defaultAddr))
                continue;
        }
        // interface found
        return nic;
    }
    // no interface found
    return null;
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Aggregations

InterfaceAddress (java.net.InterfaceAddress)104 NetworkInterface (java.net.NetworkInterface)69 InetAddress (java.net.InetAddress)60 SocketException (java.net.SocketException)27 Inet4Address (java.net.Inet4Address)26 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)9 Inet6Address (java.net.Inet6Address)8 UnknownHostException (java.net.UnknownHostException)7 NotifyListener (net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener)7 Command (com.android.server.NativeDaemonConnector.Command)6 LinkAddress (android.net.LinkAddress)5 DatagramPacket (java.net.DatagramPacket)5 List (java.util.List)5 InetSocketAddress (java.net.InetSocketAddress)4 SharedPreferences (android.content.SharedPreferences)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Enumeration (java.util.Enumeration)3 LinkedList (java.util.LinkedList)3