@Test public void createPacketTest(){ service = (PacketServiceImpl) context.getBean(IPacketService.class); ReflectionTestUtils.setField(service, "pcapngHelper", helper); byte[] data = new byte[64]; data[0] = 0; data[1] = 0; data[2] = 0; data[3] = 0; ByteBuffer buffer = ByteBuffer.allocate(2); short network = 0x0800; buffer.putShort(network); buffer.flip(); byte[] netarr = buffer.array(); data[12] = netarr[0]; data[13] = netarr[1]; data[14] = netarr[0]; data[15] = netarr[1]; ByteBuffer datawrap = ByteBuffer.wrap(data); short value = datawrap.getShort(12); assertEquals(network, value); Packet packet = service.createPacketFromPcap(12, 1, 1, 1, data, null); packet = service.createPacketFromPcap(1, 1, 1, 1, data, null); packet = service.createPacketFromPcap(113, 1, 1, 1, data, null); packet = service.createPacketFromPcap(0, 1, 1, 1, data, file.getAbsolutePath()); packet = service.createPacketFromNetmon(0xe000, 1, 1, 1, data); packet = service.createPacketFromNetmon(9, 1, 1, 1, data); packet = service.createPacketFromNetmon(8, 1, 1, 1, data); packet = service.createPacketFromNetmon(1, 1, 1, 1, data); packet = service.createPacketFromNetmon(6, 1, 1, 1, data); data[1] = 0x2; data[2] = 0x8; packet = service.createPacketFromNetmon(6, 1, 1, 1, data); data[6] = 0x6; packet = service.createPacket((short) 0x86DD, 1, 1, 1, 0, data); boolean ok = false; if(packet instanceof TCPPacket){ ok = true; } assertEquals(true, ok); data[6] = 0x11; packet = service.createPacket((short) 0x86DD, 1, 1, 1, 0, data); if(packet instanceof UDPPacket){ ok = true; } assertEquals(true, ok); data[6] = 0x8; packet = service.createPacket((short) 0x86DD, 1, 1, 1, 0, data); data[9] = 0x6; packet = service.createPacket((short) 0x0800, 1, 1, 1, 0, data); data[9] = 0x11; packet = service.createPacket((short) 0x0800, 1, 1, 1, 0, data); data[9] = 0x8; packet = service.createPacket((short) 0x0800, 1, 1, 1, 0, data); assertNotNull(packet); }
@Override public Packet createPacket(short network, long seconds, long microSeconds, int len, int datalinkHdrLen, byte[] data) { Packet packet = null; ByteBuffer bytes = ByteBuffer.wrap(data); if (network == IPV6 && data.length >= datalinkHdrLen + 40) { byte protocol = bytes.get(datalinkHdrLen + 6); switch (protocol) { case 6: packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; case 17: packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; } } else if (network == IPV4 && data.length >= datalinkHdrLen + 20) { byte iphlen = (byte) ((bytes.get(datalinkHdrLen) & 0x0f) << 2); if (data.length < datalinkHdrLen + iphlen) { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } else { byte protocol = bytes.get(datalinkHdrLen + 9); switch (protocol) { case 6: if (data.length >= datalinkHdrLen + iphlen + 20) { packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; case 17: if (data.length >= datalinkHdrLen + iphlen + 6) { packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } } } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } return packet; }
PacketServiceImpl implements IPacketService { @Override public Packet createPacket(short network, long seconds, long microSeconds, int len, int datalinkHdrLen, byte[] data) { Packet packet = null; ByteBuffer bytes = ByteBuffer.wrap(data); if (network == IPV6 && data.length >= datalinkHdrLen + 40) { byte protocol = bytes.get(datalinkHdrLen + 6); switch (protocol) { case 6: packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; case 17: packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; } } else if (network == IPV4 && data.length >= datalinkHdrLen + 20) { byte iphlen = (byte) ((bytes.get(datalinkHdrLen) & 0x0f) << 2); if (data.length < datalinkHdrLen + iphlen) { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } else { byte protocol = bytes.get(datalinkHdrLen + 9); switch (protocol) { case 6: if (data.length >= datalinkHdrLen + iphlen + 20) { packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; case 17: if (data.length >= datalinkHdrLen + iphlen + 6) { packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } } } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } return packet; } }
PacketServiceImpl implements IPacketService { @Override public Packet createPacket(short network, long seconds, long microSeconds, int len, int datalinkHdrLen, byte[] data) { Packet packet = null; ByteBuffer bytes = ByteBuffer.wrap(data); if (network == IPV6 && data.length >= datalinkHdrLen + 40) { byte protocol = bytes.get(datalinkHdrLen + 6); switch (protocol) { case 6: packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; case 17: packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; } } else if (network == IPV4 && data.length >= datalinkHdrLen + 20) { byte iphlen = (byte) ((bytes.get(datalinkHdrLen) & 0x0f) << 2); if (data.length < datalinkHdrLen + iphlen) { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } else { byte protocol = bytes.get(datalinkHdrLen + 9); switch (protocol) { case 6: if (data.length >= datalinkHdrLen + iphlen + 20) { packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; case 17: if (data.length >= datalinkHdrLen + iphlen + 6) { packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } } } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } return packet; }  }
PacketServiceImpl implements IPacketService { @Override public Packet createPacket(short network, long seconds, long microSeconds, int len, int datalinkHdrLen, byte[] data) { Packet packet = null; ByteBuffer bytes = ByteBuffer.wrap(data); if (network == IPV6 && data.length >= datalinkHdrLen + 40) { byte protocol = bytes.get(datalinkHdrLen + 6); switch (protocol) { case 6: packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; case 17: packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; } } else if (network == IPV4 && data.length >= datalinkHdrLen + 20) { byte iphlen = (byte) ((bytes.get(datalinkHdrLen) & 0x0f) << 2); if (data.length < datalinkHdrLen + iphlen) { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } else { byte protocol = bytes.get(datalinkHdrLen + 9); switch (protocol) { case 6: if (data.length >= datalinkHdrLen + iphlen + 20) { packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; case 17: if (data.length >= datalinkHdrLen + iphlen + 6) { packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } } } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } return packet; }  @Override Packet createPacketFromPcap(int datalink, long seconds, long microSeconds, int len, byte[] data, String pcapfile); @Override Packet createPacketFromNetmon(int datalink, long seconds, long microSeconds, int len, byte[] data); @Override Packet createPacket(short network, long seconds, long microSeconds, int len, int datalinkHdrLen, byte[] data); }
PacketServiceImpl implements IPacketService { @Override public Packet createPacket(short network, long seconds, long microSeconds, int len, int datalinkHdrLen, byte[] data) { Packet packet = null; ByteBuffer bytes = ByteBuffer.wrap(data); if (network == IPV6 && data.length >= datalinkHdrLen + 40) { byte protocol = bytes.get(datalinkHdrLen + 6); switch (protocol) { case 6: packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; case 17: packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); break; } } else if (network == IPV4 && data.length >= datalinkHdrLen + 20) { byte iphlen = (byte) ((bytes.get(datalinkHdrLen) & 0x0f) << 2); if (data.length < datalinkHdrLen + iphlen) { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } else { byte protocol = bytes.get(datalinkHdrLen + 9); switch (protocol) { case 6: if (data.length >= datalinkHdrLen + iphlen + 20) { packet = new TCPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; case 17: if (data.length >= datalinkHdrLen + iphlen + 6) { packet = createUDPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } break; default: packet = new IPPacket(seconds, microSeconds, len, datalinkHdrLen, data); } } } else { packet = new Packet(seconds, microSeconds, len, datalinkHdrLen, data); } return packet; }  @Override Packet createPacketFromPcap(int datalink, long seconds, long microSeconds, int len, byte[] data, String pcapfile); @Override Packet createPacketFromNetmon(int datalink, long seconds, long microSeconds, int len, byte[] data); @Override Packet createPacket(short network, long seconds, long microSeconds, int len, int datalinkHdrLen, byte[] data);  }