/**
 * *****************************************************************************
 * Copyright (c) 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************
 */
package com.exalttech.trex.packets;

import com.exalttech.trex.ui.models.PacketInfo;
import com.exalttech.trex.util.PacketUtil;
import java.io.IOException;
import javax.xml.bind.DatatypeConverter;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.pcap4j.packet.IllegalRawDataException;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.namednumber.EtherType;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

/**
 *
 * Ethernet packet tests class
 *
 * @author Georgekh
 */
public class TrexEthernetPacketTest {

    private static final Logger LOG = Logger.getLogger(TrexEthernetPacketTest.class.getName());
    PacketUtil packetUtil = new PacketUtil();

    public TrexEthernetPacketTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @BeforeClass
    public void setUp() {
    }

    @AfterClass
    public void tearDown() {
    }

    /**
     * Test of buildPacket method, of class TrexEthernetPacket without vlan
     */
    @Test
    @Parameters({"macSrcAddress", "macDstAddress", "packetLength", "expectedEthernetWithoutVlanHex"})
    public void testEthernetPacketWithoutVlan(String macSrcAddress, String macDstAddress, int packetLength, String expectedEthernetWithoutVlanHex) throws IOException, IllegalRawDataException {

        LOG.info("------------Testing Ethernet packet");
        // build ethernet packet

        LOG.info("Building Ethernet packet");
        TrexEthernetPacket instance = new TrexEthernetPacket();
        instance.setSrcAddr(macSrcAddress);
        instance.setDstAddr(macDstAddress);
        instance.setLength(packetLength);
        instance.buildPacket(null);

        LOG.info("Encoding packet data");

        // Encode packet data
        String encodedBinaryPacket = packetUtil.getEncodedPacket(instance.getPacket().getRawData());
        LOG.info("Decoding packets and returning packet data information");

        // Decode and return packet info
        PacketInfo packetInfo = packetUtil.getPacketInfoData(encodedBinaryPacket);
        LOG.info("Verifying packet data");

        // Assert mac src/destination address
        Assert.assertEquals(macSrcAddress, packetInfo.getSrcMac(), "Invalid MAC source address. ");
        Assert.assertEquals(macDstAddress, packetInfo.getDestMac(), "Invalid MAC destination address. ");

        // Verify packet length
        Packet packet = packetUtil.getPacketFromEncodedString(encodedBinaryPacket);
        Assert.assertEquals(packetLength, packetUtil.getPacketLength(packet), "Invalid Packet length. ");

        // Verify packet data
        String packetHex = DatatypeConverter.printHexBinary(packet.getRawData());
        Assert.assertEquals(expectedEthernetWithoutVlanHex.toLowerCase(), packetHex.toLowerCase(), "Invalid Packet hex. ");

    }

    /**
     * Test of buildPacket method, of class TrexEthernetPacket with VLAN
     */
    @Test
    @Parameters({"macSrcAddress", "macDstAddress", "packetLength", "expectedEthernetWithVlanHex"})
    public void testEthernetPacketWithVlan(String macSrcAddress, String macDstAddress, int packetLength, String expectedEthernetWithVlanHex) throws IOException, IllegalRawDataException {

        LOG.info("------------Testing Ethernet packet");
        // build ethernet packet

        LOG.info("Building Ethernet packet");
        TrexEthernetPacket instance = new TrexEthernetPacket();
        instance.setSrcAddr(macSrcAddress);
        instance.setDstAddr(macDstAddress);
        instance.setLength(packetLength);
        instance.setType(EtherType.DOT1Q_VLAN_TAGGED_FRAMES.value());

        // build VLAN packet
        TrexVlanPacket vlanPacket = new TrexVlanPacket();
        vlanPacket.setType((short) 0xFFFF);
        vlanPacket.buildPacket(null);

        LOG.info("Building VLAN packet");
        instance.buildPacket(vlanPacket.getBuilder());

        LOG.info("Encoding packet data");

        // Encode packet data
        String encodedBinaryPacket = packetUtil.getEncodedPacket(instance.getPacket().getRawData());
        LOG.info("Decoding packets and returning packet data information");

        // Decode and return packet info
        PacketInfo packetInfo = packetUtil.getPacketInfoData(encodedBinaryPacket);
        LOG.info("Verifying packet data");

        // Assert mac src/destination address
        Assert.assertEquals(macSrcAddress, packetInfo.getSrcMac(), "Invalid MAC source address. ");
        Assert.assertEquals(macDstAddress, packetInfo.getDestMac(), "Invalid MAC destination address. ");

        // Verify packet length
        Packet packet = packetUtil.getPacketFromEncodedString(encodedBinaryPacket);
        Assert.assertEquals(packetLength, packetUtil.getPacketLength(packet), "Invalid Packet length. ");

        // Verify packet data
        String packetHex = DatatypeConverter.printHexBinary(packet.getRawData());
        Assert.assertEquals(expectedEthernetWithVlanHex.toLowerCase(), packetHex.toLowerCase(), "Invalid Packet hex. ");

    }
}

@@ -991,16 +991,57 @@
 Assert;%0A
+import org.pcap4j.packet.AbstractPacket;%0A
 import o
@@ -1119,56 +1119,8 @@
 et;%0A
-import org.pcap4j.packet.namednumber.EtherType;%0A
 impo
@@ -1525,16 +1525,19 @@
 Util();%0A
+   
 %0A    pub
@@ -1876,21 +1876,9 @@
 cket
- without vlan
+.
 %0A   
@@ -1968,35 +1968,16 @@
 expected
-EthernetWithoutVlan
 Hex%22%7D)%0A 
@@ -2098,35 +2098,16 @@
 expected
-EthernetWithoutVlan
 Hex) thr
@@ -2279,32 +2279,87 @@
 ernet packet%22);%0A
+        AbstractPacket.AbstractBuilder builder = null;%0A
         TrexEthe
@@ -2557,36 +2557,39 @@
 nce.buildPacket(
-null
+builder
 );%0A%0A        LOG.
@@ -2610,32 +2610,40 @@
  packet data%22);%0A
+        
 %0A        // Enco
@@ -3470,32 +3470,40 @@
 ket length. %22);%0A
+        
 %0A        // Veri
@@ -3639,2219 +3639,8 @@
 cted
-EthernetWithoutVlanHex.toLowerCase(), packetHex.toLowerCase(), %22Invalid Packet hex. %22);%0A%0A    %7D%0A%0A    /**%0A     * Test of buildPacket method, of class TrexEthernetPacket with VLAN%0A     */%0A    @Test%0A    @Parameters(%7B%22macSrcAddress%22, %22macDstAddress%22, %22packetLength%22, %22expectedEthernetWithVlanHex%22%7D)%0A    public void testEthernetPacketWithVlan(String macSrcAddress, String macDstAddress, int packetLength, String expectedEthernetWithVlanHex) throws IOException, IllegalRawDataException %7B%0A%0A        LOG.info(%22------------Testing Ethernet packet%22);%0A        // build ethernet packet%0A%0A        LOG.info(%22Building Ethernet packet%22);%0A        TrexEthernetPacket instance = new TrexEthernetPacket();%0A        instance.setSrcAddr(macSrcAddress);%0A        instance.setDstAddr(macDstAddress);%0A        instance.setLength(packetLength);%0A        instance.setType(EtherType.DOT1Q_VLAN_TAGGED_FRAMES.value());%0A%0A        // build VLAN packet%0A        TrexVlanPacket vlanPacket = new TrexVlanPacket();%0A        vlanPacket.setType((short) 0xFFFF);%0A        vlanPacket.buildPacket(null);%0A%0A        LOG.info(%22Building VLAN packet%22);%0A        instance.buildPacket(vlanPacket.getBuilder());%0A%0A        LOG.info(%22Encoding packet data%22);%0A%0A        // Encode packet data%0A        String encodedBinaryPacket = packetUtil.getEncodedPacket(instance.getPacket().getRawData());%0A        LOG.info(%22Decoding packets and returning packet data information%22);%0A%0A        // Decode and return packet info%0A        PacketInfo packetInfo = packetUtil.getPacketInfoData(encodedBinaryPacket);%0A        LOG.info(%22Verifying packet data%22);%0A%0A        // Assert mac src/destination address%0A        Assert.assertEquals(macSrcAddress, packetInfo.getSrcMac(), %22Invalid MAC source address. %22);%0A        Assert.assertEquals(macDstAddress, packetInfo.getDestMac(), %22Invalid MAC destination address. %22);%0A%0A        // Verify packet length%0A        Packet packet = packetUtil.getPacketFromEncodedString(encodedBinaryPacket);%0A        Assert.assertEquals(packetLength, packetUtil.getPacketLength(packet), %22Invalid Packet length. %22);%0A%0A        // Verify packet data%0A        String packetHex = DatatypeConverter.printHexBinary(packet.getRawData());%0A        Assert.assertEquals(expectedEthernetWithVlan
 Hex.
@@ -3711,10 +3711,11 @@
 %0A%0A    %7D%0A
+%0A
 %7D%0A

Remove test