AddressObserverUsingIpv4 implements Ipv4PacketListener { @Override public void onIpv4PacketReceived(Ipv4PacketReceived packetReceived) { if (packetReceived == null || packetReceived.getPacketChain() == null) { return; } RawPacket rawPacket = null; EthernetPacket ethernetPacket = null; Ipv4Packet ipv4Packet = null; for (PacketChain packetChain : packetReceived.getPacketChain()) { if (packetChain.getPacket() instanceof RawPacket) { rawPacket = (RawPacket) packetChain.getPacket(); } else if (packetChain.getPacket() instanceof EthernetPacket) { ethernetPacket = (EthernetPacket) packetChain.getPacket(); } else if (packetChain.getPacket() instanceof Ipv4Packet) { ipv4Packet = (Ipv4Packet) packetChain.getPacket(); } } if (rawPacket == null || ethernetPacket == null || ipv4Packet == null) { return; } if (!IPV4_IP_TO_IGNORE.equals(ipv4Packet.getSourceIpv4().getValue())) { addressObservationWriter.addAddress(ethernetPacket.getSourceMac(), new IpAddress(ipv4Packet.getSourceIpv4().getValue().toCharArray()), rawPacket.getIngress()); } } AddressObserverUsingIpv4(AddressObservationWriter addressObservationWriter); @Override void onIpv4PacketReceived(Ipv4PacketReceived packetReceived);  }
@Test public void onIpv4PacketReceivedTest() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder() .setPacket(new EthernetPacketBuilder().setSourceMac(new MacAddress("aa:bb:cc:dd:ee:ff")).build()) .build()); packetChainList.add(new PacketChainBuilder() .setPacket(new Ipv4PacketBuilder().setSourceIpv4(new Ipv4Address("1.2.3.4")).build()).build()); Ipv4PacketReceived ipv4PktReceived = new Ipv4PacketReceivedBuilder().setPacketChain(packetChainList).build(); AddressObserverUsingIpv4 addressObserverIpv4 = new AddressObserverUsingIpv4(addressObservationWriterMock); addressObserverIpv4.onIpv4PacketReceived(ipv4PktReceived); verify(addressObservationWriterMock, times(1)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }
@Test public void onIpv4PacketReceivedNullInputTest1() throws Exception { Ipv4PacketReceived ipv4PktReceived = new Ipv4PacketReceivedBuilder().setPacketChain(null).build(); AddressObserverUsingIpv4 addressObserverIpv4 = new AddressObserverUsingIpv4(addressObservationWriterMock); addressObserverIpv4.onIpv4PacketReceived(ipv4PktReceived); verify(addressObservationWriterMock, times(0)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }
@Test public void onIpv4PacketReceivedNullInputTest2() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder() .setPacket(new EthernetPacketBuilder().setSourceMac(new MacAddress("aa:bb:cc:dd:ee:ff")).build()) .build()); Ipv4PacketReceived ipv4PktReceived = new Ipv4PacketReceivedBuilder().setPacketChain(packetChainList).build(); AddressObserverUsingIpv4 addressObserverIpv4 = new AddressObserverUsingIpv4(addressObservationWriterMock); addressObserverIpv4.onIpv4PacketReceived(ipv4PktReceived); verify(addressObservationWriterMock, times(0)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }
@Test public void onIpv4PacketReceivedNullInputTest3() throws Exception { ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>(); packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build()); packetChainList.add(new PacketChainBuilder() .setPacket(new EthernetPacketBuilder().setSourceMac(new MacAddress("aa:bb:cc:dd:ee:ff")).build()) .build()); packetChainList.add(new PacketChainBuilder() .setPacket(new Ipv4PacketBuilder().setSourceIpv4(new Ipv4Address("0.0.0.0")).build()).build()); Ipv4PacketReceived ipv4PktReceived = new Ipv4PacketReceivedBuilder().setPacketChain(packetChainList).build(); AddressObserverUsingIpv4 addressObserverIpv4 = new AddressObserverUsingIpv4(addressObservationWriterMock); addressObserverIpv4.onIpv4PacketReceived(ipv4PktReceived); verify(addressObservationWriterMock, times(0)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }