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));
}
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));
}
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));
}
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;
}
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;
}
Aggregations