TCP extends BasePacket { private static short checksum(byte[] message, int length, int offset) { int sum = 0; while (offset < length - 1) { sum += (int) integralFromBytes(message, offset, 2); offset += 2; } if (offset == length - 1) { sum += (message[offset] >= 0 ? message[offset] : message[offset] ^ 0xffffff00) << 8; } sum = (sum >>> 16) + (sum & 0xffff); sum += sum >>> 16; return (short) ~sum; }  TransportPort getSourcePort(); TCP setSourcePort(TransportPort sourcePort); TCP setSourcePort(int sourcePort); TransportPort getDestinationPort(); TCP setDestinationPort(TransportPort destinationPort); TCP setDestinationPort(int destinationPort); short getChecksum(); int getSequence(); TCP setSequence(int seq); int getAcknowledge(); TCP setAcknowledge(int ack); byte getDataOffset(); TCP setDataOffset(byte offset); short getFlags(); TCP setFlags(short flags); short getWindowSize(); TCP setWindowSize(short windowSize); short getTcpChecksum(); TCP setTcpChecksum(short checksum); @Override void resetChecksum(); short getUrgentPointer(short urgentPointer); TCP setUrgentPointer(short urgentPointer); byte[] getOptions(); TCP setOptions(byte[] options); TCP setChecksum(short checksum); byte[] serialize(); @Override int hashCode(); @Override boolean equals(Object obj); @Override IPacket deserialize(byte[] data, int offset, int length);  }
@Test public void testChecksum() throws PacketParsingException{ Ethernet packet = new Ethernet(); IPv4 ip = new IPv4(); ip.deserialize(fea8, 0, fea8.length); TCP payload = (TCP)ip.getPayload(); payload.resetChecksum(); payload.serialize(); assertTrue(payload.getChecksum() == (short)0xfea8); packet.deserialize(checksumTestPacket1, 0, checksumTestPacket1.length); payload = (TCP)packet.getPayload().getPayload(); payload.resetChecksum(); payload.serialize(); assertTrue(payload.getChecksum() == (short)0xff03); packet = new Ethernet(); packet.deserialize(checksumTestPacket2, 0, checksumTestPacket2.length); payload = (TCP)packet.getPayload().getPayload(); payload.resetChecksum(); payload.serialize(); assertTrue(payload.getChecksum() == (short)0xff09); }