Search in sources :

Example 76 with InterfaceAddress

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

the class SsdpMessageDelegateTest method getScopeId_インターフェースに紐付くIPv6ならその値.

@Test
public void getScopeId_インターフェースに紐付くIPv6ならその値() throws Exception {
    final int scopeId = 1;
    final byte[] data = TestUtils.getResourceAsByteArray("ssdp-notify-alive0.bin");
    final HttpRequest request = new HttpRequest().readData(new ByteArrayInputStream(data, 0, data.length));
    final Inet6Address address = Inet6Address.getByAddress(null, InetAddress.getByName("fe80::a831:801b:8dc6:421f").getAddress(), scopeId);
    final InterfaceAddress ifa = TestUtils.createInterfaceAddress(address, "255.255.255.0", 0);
    final SsdpMessageDelegate delegate = new SsdpMessageDelegate(request, ifa);
    assertThat(delegate.getScopeId(), is(scopeId));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InterfaceAddress(java.net.InterfaceAddress) Inet6Address(java.net.Inet6Address) Test(org.junit.Test)

Example 77 with InterfaceAddress

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

the class SsdpNotifyReceiverTest method setNotifyListener_受信メッセージが通知されること.

@Test
public void setNotifyListener_受信メッセージが通知されること() 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 SsdpNotifyReceiver receiver = spy(new SsdpNotifyReceiver(delegate));
    final ArgumentCaptor<SsdpRequest> captor = ArgumentCaptor.forClass(SsdpRequest.class);
    final NotifyListener listener = mock(NotifyListener.class);
    doNothing().when(listener).onReceiveNotify(captor.capture());
    receiver.setNotifyListener(listener);
    final byte[] data = TestUtils.getResourceAsByteArray("ssdp-notify-alive0.bin");
    final InetAddress address = InetAddress.getByName("192.0.2.2");
    receiver.onReceive(address, data, data.length);
    assertThat(captor.getValue().getUuid(), is("uuid:01234567-89ab-cdef-0123-456789abcdef"));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) NotifyListener(net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener) Test(org.junit.Test)

Example 78 with InterfaceAddress

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

the class SsdpNotifyReceiverTest method onReceive_M_SEARCHパケットは無視する.

@Test
public void onReceive_M_SEARCHパケットは無視する() 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 SsdpRequest message = new SsdpRequest();
    message.setMethod(SsdpMessage.M_SEARCH);
    message.setUri("*");
    message.setHeader(Http.HOST, Address.IP_V4.getAddressString());
    message.setHeader(Http.MAN, SsdpMessage.SSDP_DISCOVER);
    message.setHeader(Http.MX, "1");
    message.setHeader(Http.ST, SsdpSearchServer.ST_ALL);
    final byte[] data = message.getMessage().getMessageString().getBytes();
    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) NotifyListener(net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener) Test(org.junit.Test)

Example 79 with InterfaceAddress

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

the class SsdpNotifyReceiverTest method onReceive_Listenerがnullでもクラッシュしない.

@Test
public void onReceive_Listenerがnullでもクラッシュしない() 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 byte[] data = TestUtils.getResourceAsByteArray("ssdp-notify-alive0.bin");
    receiver.onReceive(InetAddress.getByName("192.0.2.2"), data, data.length);
}
Also used : InterfaceAddress(java.net.InterfaceAddress) Test(org.junit.Test)

Example 80 with InterfaceAddress

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

the class SsdpNotifyReceiverTest method onReceive_ByeByeパケットは通知する.

@Test
public void onReceive_ByeByeパケットは通知する() 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-byebye0.bin");
    receiver.onReceive(InetAddress.getByName("192.0.2.2"), data, data.length);
    verify(listener).onReceiveNotify(ArgumentMatchers.any(SsdpRequest.class));
}
Also used : InterfaceAddress(java.net.InterfaceAddress) NotifyListener(net.mm2d.upnp.SsdpNotifyReceiver.NotifyListener) Test(org.junit.Test)

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