AddressObserverUsingIpv6 implements Ipv6PacketListener { @Override public void onIpv6PacketReceived(Ipv6PacketReceived packetReceived) { if (packetReceived == null || packetReceived.getPacketChain() == null) { return; } RawPacket rawPacket = null; EthernetPacket ethernetPacket = null; Ipv6Packet ipv6Packet = 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 Ipv6Packet) { ipv6Packet = (Ipv6Packet) packetChain.getPacket(); } } if (rawPacket == null || ethernetPacket == null || ipv6Packet == null) { return; } if (!IPV6_IP_TO_IGNORE.equals(ipv6Packet.getSourceIpv6().getValue())) { addressObservationWriter.addAddress(ethernetPacket.getSourceMac(), new IpAddress(ipv6Packet.getSourceIpv6().getValue().toCharArray()), rawPacket.getIngress()); } } AddressObserverUsingIpv6(AddressObservationWriter addressObservationWriter); @Override void onIpv6PacketReceived(Ipv6PacketReceived packetReceived);  }
@Test public void onIpv6PacketReceivedTest() { 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 Ipv6PacketBuilder() .setSourceIpv6(new Ipv6Address("123:4567:89ab:cdef:fedc:ba98:7654:3210")).build()) .build()); Ipv6PacketReceived ipv6PktReceived = new Ipv6PacketReceivedBuilder().setPacketChain(packetChainList).build(); AddressObserverUsingIpv6 addressObserverIpv6 = new AddressObserverUsingIpv6(addressObservationWriterMock); addressObserverIpv6.onIpv6PacketReceived(ipv6PktReceived); verify(addressObservationWriterMock, times(1)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }
@Test public void onIpv6PacketReceivedNullInputTest1() throws Exception { Ipv6PacketReceived ipv6PktReceived = new Ipv6PacketReceivedBuilder().setPacketChain(null).build(); AddressObserverUsingIpv6 addressObserverIpv6 = new AddressObserverUsingIpv6(addressObservationWriterMock); addressObserverIpv6.onIpv6PacketReceived(ipv6PktReceived); verify(addressObservationWriterMock, times(0)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }
@Test public void onIpv6PacketReceivedNullInputTest2() 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()); Ipv6PacketReceived ipv6PktReceived = new Ipv6PacketReceivedBuilder().setPacketChain(packetChainList).build(); AddressObserverUsingIpv6 addressObserverIpv6 = new AddressObserverUsingIpv6(addressObservationWriterMock); addressObserverIpv6.onIpv6PacketReceived(ipv6PktReceived); verify(addressObservationWriterMock, times(0)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }
@Test public void onIpv6PacketReceivedNullInputTest3() 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 Ipv6PacketBuilder().setSourceIpv6(new Ipv6Address("0:0:0:0:0:0:0:0")).build()).build()); Ipv6PacketReceived ipv6PktReceived = new Ipv6PacketReceivedBuilder().setPacketChain(packetChainList).build(); AddressObserverUsingIpv6 addressObserverIpv6 = new AddressObserverUsingIpv6(addressObservationWriterMock); addressObserverIpv6.onIpv6PacketReceived(ipv6PktReceived); verify(addressObservationWriterMock, times(0)).addAddress(any(MacAddress.class), any(IpAddress.class), any(NodeConnectorRef.class)); }