EthernetFramer implements Framer<PCapPacket, MACPacket> { @Override public MACPacket frame(final PCapPacket parent, final Buffer buffer) throws IOException, FramingException { if (parent == null) { throw new IllegalArgumentException("The parent frame cannot be null"); } if (buffer.getReadableBytes() < 14) { throw new FramingException("not enough bytes for header", getProtocol()); } Buffer headers; try { EtherType etherType = getEtherType(buffer.getByte(12), buffer.getByte(13)); if (etherType == EtherType.Dot1Q) { getEtherType(buffer.getByte(16), buffer.getByte(17)); headers = buffer.readBytes(18); } else { headers = buffer.readBytes(14); } } catch (IndexOutOfBoundsException e) { throw new FramingException("not enough bytes for header", getProtocol()); } catch (final UnknownEtherType e) { throw new FramingException(String.format("unknown ether type: 0x%02x%02x", e.getB1(), e.getB2()), getProtocol()); } final Buffer payload = buffer.slice(buffer.capacity()); return new MACPacketImpl(Protocol.ETHERNET_II, parent, headers, payload); } EthernetFramer(); @Override Protocol getProtocol(); @Override MACPacket frame(final PCapPacket parent, final Buffer buffer); static EtherType getEtherType(final byte b1, final byte b2); static EtherType getEtherTypeSafe(final byte b1, final byte b2); @Override boolean accept(final Buffer data);  }
@Test(expected = IllegalArgumentException.class) public void testEthernetFramerNoParent() throws Exception { this.framer.frame(null, this.ethernetFrameBuffer); }
@Test public void testAcceptsAllEtherTypes() throws Exception { for (EthernetFramer.EtherType t : EthernetFramer.EtherType.values()) { Buffer frame = this.ethernetFrameBuffer.slice(); frame.setByte(12, t.getB1()); frame.setByte(13, t.getB2()); PCapPacket parent = mock(PCapPacket.class); this.framer.frame(parent, frame); } }