@Before public void before() throws Exception { inputPath = Files.createTempFile("proc-net-udp", "txt"); ResourceUtil.writeDataFile("proc_net_udp_sample.txt", inputPath); monitor = new UdpSocketMonitor(lifecycleListener, inputPath); }
@Test public void shouldBeAbleToSubscribeToAddressAndPortAndInode() throws Exception { monitor.beginMonitoringOf(getSocketAddress("192.168.122.2", 53).getAddress(), 15294); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "192.168.122.2", 53, 9, 0, 0, 15294); }
private void pollMonitors() { try { udpSocketMonitor.poll(changeLoggingUdpSocketStatisticsHandler); softnetStatsMonitor.poll(changeLoggingSoftnetStatsHandler); systemNetworkManagementMonitor.poll(changeLoggingSnmpUdpStatisticsHandler); System.out.println(""); } catch(RuntimeException e) { e.printStackTrace(); } }
private void run() throws Exception { try(final DatagramChannel c0 = createListeningChannelOnPort(new InetSocketAddress(InetAddress.getLoopbackAddress(), 12769)); final DatagramChannel c1 = createListeningChannelOnPort(new InetSocketAddress(InetAddress.getLoopbackAddress(), 12770)); final DatagramChannel c2 = multicastListener(InetAddress.getByName("239.192.45.3"), 5000)) { Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(this::pollMonitors, 0L, 1L, TimeUnit.SECONDS); udpSocketMonitor.beginMonitoringOf((InetSocketAddress) c0.getLocalAddress()); udpSocketMonitor.beginMonitoringOf((InetSocketAddress) c1.getLocalAddress()); udpSocketMonitor.beginMonitoringOf((InetSocketAddress) c2.getLocalAddress()); waitForMonitoringToStart(); Thread.sleep(TimeUnit.SECONDS.toMillis(1L)); final DatagramChannel writer = DatagramChannel.open().connect(c0.getLocalAddress()); while(!Thread.currentThread().isInterrupted()) { // overflow receive buffers writer.write(ByteBuffer.wrap("deadcod".getBytes(StandardCharsets.UTF_8))); Thread.sleep(200L); } } }
@Test public void shouldMonitorIpAddressWhoseFirstOctetJavaSignedByteValueIsNegative() throws Exception { beginMonitoring(requestSpecFor(getSocketAddress("239.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); ResourceUtil.writeDataFile("proc_net_udp_signed_first_octet_sample.txt", inputPath); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "239.168.122.1", 53, 166, 0, 0, 15292); }
@Test public void shouldNotifyLifecycleListenerWhenMonitoredSocketBecomesUnavailable() throws Exception { beginMonitoring(requestSpecFor(getSocketAddress("0.0.0.0", 20048))); monitor.poll(recordingUdpSocketStatisticsHandler); ResourceUtil.writeDataFile("proc_net_udp_socket_removed_sample.txt", inputPath); monitor.poll(recordingUdpSocketStatisticsHandler); final List<InetSocketAddress> monitoringStoppedList = lifecycleListener.getMonitoringStoppedList(); assertThat(monitoringStoppedList.size(), is(1)); assertThat(monitoringStoppedList.get(0), is(getSocketAddress("0.0.0.0", 20048))); }
@Test public void shouldBeAbleToSubscribeToAddressAndPortAndInode() throws Exception { monitor.beginMonitoringOf(getSocketAddress("192.168.122.2", 53), 15294); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "192.168.122.2", 53, 9, 0, 0, 15294); } }
@Test public void shouldMonitorIpAddressOnLocalhost() throws Exception { beginMonitoring(requestSpecFor(getSocketAddress("127.0.0.1", 32770))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "127.0.0.1", 32770, 0, 0, 0, 15293); }
@Test public void shouldNotNotifyHandlerOfUnchangedEntries() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150), getSocketAddress("192.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); recordingUdpSocketStatisticsHandler.getRecordedEntries().clear(); ResourceUtil.writeDataFile("proc_net_udp_updated_sample.txt", inputPath); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(2)); assertEntry(recordedEntries.get(0), "0.0.0.0", 56150, 1, 0, 4, 13597); assertEntry(recordedEntries.get(1), "192.168.122.1", 53, 166, 0, 2, 15292); }
@Test public void shouldNotifyLifecycleListener() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150))); monitor.poll(recordingUdpSocketStatisticsHandler); endMonitoring(requestSpecFor(getSocketAddress("0.0.0.0", 56150))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<InetSocketAddress> monitoringStartedList = lifecycleListener.getMonitoringStartedList(); assertThat(monitoringStartedList.size(), is(2)); assertThat(monitoringStartedList.get(0), is(getSocketAddress("0.0.0.0", 20048))); assertThat(monitoringStartedList.get(1), is(getSocketAddress("0.0.0.0", 56150))); final List<InetSocketAddress> monitoringStoppedList = lifecycleListener.getMonitoringStoppedList(); assertThat(monitoringStoppedList.size(), is(1)); assertThat(monitoringStoppedList.get(0), is(getSocketAddress("0.0.0.0", 56150))); }
@Test public void shouldSampleMonitoredSockets() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150), getSocketAddress("192.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(3)); assertEntry(recordedEntries.get(0), "0.0.0.0", 20048, 0, 0, 0, 21682); assertEntry(recordedEntries.get(1), "0.0.0.0", 56150, 0, 0, 4, 13597); assertEntry(recordedEntries.get(2), "192.168.122.1", 53, 166, 144, 0, 15292); }
@Test public void shouldNotifyLifecycleListener() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150))); monitor.poll(recordingUdpSocketStatisticsHandler); endMonitoring(requestSpecFor(getSocketAddress("0.0.0.0", 56150))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<InetSocketAddress> monitoringStartedList = lifecycleListener.getMonitoringStartedList(); assertThat(monitoringStartedList.size(), is(2)); assertThat(monitoringStartedList.get(0), is(getSocketAddress("0.0.0.0", 20048))); assertThat(monitoringStartedList.get(1), is(getSocketAddress("0.0.0.0", 56150))); final List<InetSocketAddress> monitoringStoppedList = lifecycleListener.getMonitoringStoppedList(); assertThat(monitoringStoppedList.size(), is(2)); assertThat(monitoringStoppedList.get(0), is(getSocketAddress("0.0.0.0", 56150))); assertThat(monitoringStoppedList.get(1), is(getSocketAddress("0.0.0.0", 20048))); }
@Test public void shouldNotNotifyHandlerOfChangeWhenSocketIsNoLongerMonitored() throws Exception { beginMonitoring(requestSpecFor( getSocketAddress("0.0.0.0", 20048), getSocketAddress("0.0.0.0", 56150), getSocketAddress("192.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); recordingUdpSocketStatisticsHandler.getRecordedEntries().clear(); ResourceUtil.writeDataFile("proc_net_udp_updated_sample.txt", inputPath); endMonitoring(requestSpecFor(getSocketAddress("192.168.122.1", 53))); monitor.poll(recordingUdpSocketStatisticsHandler); final List<MonitoredEntry> recordedEntries = recordingUdpSocketStatisticsHandler.getRecordedEntries(); assertThat(recordedEntries.size(), is(1)); assertEntry(recordedEntries.get(0), "0.0.0.0", 56150, 1, 0, 4, 13597); }