<commit_before>package com.cisco.trex.stateless;


import com.cisco.trex.stateless.exception.TRexConnectionException;
import com.cisco.trex.stateless.exception.TRexTimeoutException;
import com.cisco.trex.stateless.model.*;
import org.junit.*;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.ArpPacket;
import org.pcap4j.packet.EthernetPacket;
import org.pcap4j.packet.namednumber.ArpHardwareType;
import org.pcap4j.packet.namednumber.ArpOperation;
import org.pcap4j.packet.namednumber.EtherType;
import org.pcap4j.util.ByteArrays;
import org.pcap4j.util.MacAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

public class TRexClientTest {
    public static final String CLIENT_USER = "unit-tests-user";
    private static final Packet SIMPLE_PACKET = buildArpPkt();
    private static final Integer STREAM_ID = 100500;
    public static TRexClient client;

    @BeforeClass
    public static void setUp() throws TRexConnectionException, TRexTimeoutException {
        client = new TRexClient("tcp", "trex-host", "4501", CLIENT_USER);
        client.connect();
    }

    @Test
    public void getPortsTest() {
        List<Port> ports = client.getPorts();
        Assert.assertTrue(ports.size() > 0);
    }

    @Test
    public void getSupportedCommandsTest() {
        List<String> cmds = client.getSupportedCommands();
        Assert.assertTrue(cmds.size() > 0);
    }

    @Test
    public void getPortStatusTest() {
        PortStatus portStatus = client.getPortStatus(0);
        Assert.assertTrue(portStatus.linkUp);
    }

    @Test
    public void acquirePortTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        PortStatus portStatus = client.acquirePort(port.getIndex(), true);
        Assert.assertEquals(CLIENT_USER, portStatus.owner);
    }

    @Test
    public void releasePortTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        client.acquirePort(port.getIndex(), true);
        PortStatus releasedPortStatus = client.releasePort(port.getIndex());
        Assert.assertNotEquals(CLIENT_USER, releasedPortStatus.owner);
    }

    @Test
    public void serviceModeOnOffTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        
        client.acquirePort(port.getIndex(), true);
        PortStatus serviceModePortStatus = client.serviceMode(port.getIndex(), true);
        Assert.assertTrue(serviceModePortStatus.service);
        
        client.serviceMode(port.getIndex(), false);
        PortStatus normalModePortStatus = client.serviceMode(port.getIndex(), false);
        Assert.assertFalse(normalModePortStatus.service);
    }

    @Test
    public void addStreamTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        Stream stream = buildStream(SIMPLE_PACKET);
        client.acquirePort(port.getIndex(), true);
        client.addStream(port.getIndex(), stream);
        
        List<Integer> streamIds = client.getStreamIds(port.getIndex());
        Assert.assertTrue(streamIds.contains(stream.getId()));
        client.releasePort(port.getIndex());
    }
    

    @Test
    public void removeStreamTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        Stream stream = buildStream(SIMPLE_PACKET);
        
        client.acquirePort(port.getIndex(), true);
        client.addStream(port.getIndex(), stream);
        client.removeStream(port.getIndex(), stream.getId());
        
        List<Integer> streamIds = client.getStreamIds(port.getIndex());
        Assert.assertTrue(!streamIds.contains(stream.getId()));
        
        client.releasePort(port.getIndex());
    }

    @Test
    public void removeAllStreamsTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        
        Stream s = buildStream(SIMPLE_PACKET);
        
        client.acquirePort(port.getIndex(), true);
        client.addStream(port.getIndex(), s);
        client.removeAllStreams(port.getIndex());
        
        List<Integer> streamIds = client.getStreamIds(port.getIndex());
        Assert.assertTrue(streamIds.isEmpty());
        
        client.releasePort(port.getIndex());
    }

    @Test
    public void getStreamTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        
        Stream s = buildStream(SIMPLE_PACKET);
        client.acquirePort(port.getIndex(), true);
        client.addStream(port.getIndex(), s);
        
        Stream streamFromTrex = client.getStream(port.getIndex(), s.getId());
        Assert.assertTrue(s.equals(streamFromTrex));
        
        client.releasePort(port.getIndex());
    }

    @Test
    public void startStopTrafficTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        client.acquirePort(port.getIndex(), true);

        Stream s = buildStream(SIMPLE_PACKET);
        client.addStream(port.getIndex(), s);

        Map<String, Object> mul = new HashMap<>();
        mul.put("op", "abs");
        mul.put("type", "pps");
        mul.put("value", 1.0);
        client.startTraffic(port.getIndex(), -1.0, true, mul, 1);
        
        PortStatus portStatusTX = client.getPortStatus(port.getIndex());
        Assert.assertTrue(portStatusTX.state.equals("TX"));
        client.stopTraffic(port.getIndex());

        PortStatus portStatusStream = client.getPortStatus(port.getIndex());
        Assert.assertTrue(portStatusStream.state.equals("STREAMS"));
        
        client.releasePort(port.getIndex());
    }

    @Test
    @Ignore
    public void arpTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        Port port1 = ports.get(1);
        client.acquirePort(port.getIndex(), true);
        client.acquirePort(port1.getIndex(), true);
        
        client.serviceMode(port.getIndex(), true);
        client.serviceMode(port1.getIndex(), true);

        client.removeRxQueue(port.getIndex());
        client.setRxQueue(port.getIndex(), 5);
            
        EthernetPacket pkt = buildArpPkt();
        client.sendPacket(port.getIndex(), pkt);

        Predicate<EthernetPacket> arpReplyFilter = etherPkt -> {
            if(etherPkt.contains(ArpPacket.class)) {<commit_msg>Add tests for setL3Mode and send icmpEcho<commit_after>package com.cisco.trex.stateless;


import com.cisco.trex.stateless.exception.TRexConnectionException;
import com.cisco.trex.stateless.exception.TRexTimeoutException;
import com.cisco.trex.stateless.model.*;
import org.junit.*;
import org.pcap4j.packet.ArpPacket;
import org.pcap4j.packet.EthernetPacket;
import org.pcap4j.packet.Packet;
import org.pcap4j.packet.namednumber.ArpHardwareType;
import org.pcap4j.packet.namednumber.ArpOperation;
import org.pcap4j.packet.namednumber.EtherType;
import org.pcap4j.util.ByteArrays;
import org.pcap4j.util.MacAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

public class TRexClientTest {
    public static final String CLIENT_USER = "unit-tests-user";
    private static final Packet SIMPLE_PACKET = buildArpPkt();
    private static final Integer STREAM_ID = 100500;
    public static TRexClient client;

    @BeforeClass
    public static void setUp() throws TRexConnectionException, TRexTimeoutException {
        client = new TRexClient("tcp", "trex-host", "4501", CLIENT_USER);
        client.connect();
    }

    @Test
    public void getPortsTest() {
        List<Port> ports = client.getPorts();
        Assert.assertTrue(ports.size() > 0);
    }

    @Test
    public void getSupportedCommandsTest() {
        List<String> cmds = client.getSupportedCommands();
        Assert.assertTrue(cmds.size() > 0);
    }

    @Test
    public void getPortStatusTest() {
        PortStatus portStatus = client.getPortStatus(0);
        Assert.assertTrue(portStatus.linkUp);
    }

    @Test
    public void acquirePortTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        PortStatus portStatus = client.acquirePort(port.getIndex(), true);
        Assert.assertEquals(CLIENT_USER, portStatus.owner);
    }

    @Test
    public void releasePortTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        client.acquirePort(port.getIndex(), true);
        PortStatus releasedPortStatus = client.releasePort(port.getIndex());
        Assert.assertNotEquals(CLIENT_USER, releasedPortStatus.owner);
    }

    @Test
    public void serviceModeOnOffTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        
        client.acquirePort(port.getIndex(), true);
        PortStatus serviceModePortStatus = client.serviceMode(port.getIndex(), true);
        Assert.assertTrue(serviceModePortStatus.service);
        
        client.serviceMode(port.getIndex(), false);
        PortStatus normalModePortStatus = client.serviceMode(port.getIndex(), false);
        Assert.assertFalse(normalModePortStatus.service);
    }

    @Test
    public void addStreamTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        Stream stream = buildStream(SIMPLE_PACKET);
        client.acquirePort(port.getIndex(), true);
        client.addStream(port.getIndex(), stream);
        
        List<Integer> streamIds = client.getStreamIds(port.getIndex());
        Assert.assertTrue(streamIds.contains(stream.getId()));
        client.releasePort(port.getIndex());
    }
    

    @Test
    public void removeStreamTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        Stream stream = buildStream(SIMPLE_PACKET);
        
        client.acquirePort(port.getIndex(), true);
        client.addStream(port.getIndex(), stream);
        client.removeStream(port.getIndex(), stream.getId());
        
        List<Integer> streamIds = client.getStreamIds(port.getIndex());
        Assert.assertTrue(!streamIds.contains(stream.getId()));
        
        client.releasePort(port.getIndex());
    }

    @Test
    public void removeAllStreamsTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        
        Stream s = buildStream(SIMPLE_PACKET);
        
        client.acquirePort(port.getIndex(), true);
        client.addStream(port.getIndex(), s);
        client.removeAllStreams(port.getIndex());
        
        List<Integer> streamIds = client.getStreamIds(port.getIndex());
        Assert.assertTrue(streamIds.isEmpty());
        
        client.releasePort(port.getIndex());
    }

    @Test
    public void getStreamTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        
        Stream s = buildStream(SIMPLE_PACKET);
        client.acquirePort(port.getIndex(), true);
        client.addStream(port.getIndex(), s);
        
        Stream streamFromTrex = client.getStream(port.getIndex(), s.getId());
        Assert.assertTrue(s.equals(streamFromTrex));
        
        client.releasePort(port.getIndex());
    }

    /**
     * This is a specific test which is not related to standalone Java SDK
     */
    @Test
    @Ignore
    public void pingTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        try {
            client.acquirePort(port.getIndex(), true);
            client.serviceMode(port.getIndex(), true);
            String host = "192.168.9.1";
            EthernetPacket icmpReply = client.sendIcmpEcho(port.getIndex(), host, 10, 10, 1000);
            Assert.assertNotNull(icmpReply);
        } catch (UnknownHostException ignored) {
        } finally {
            client.serviceMode(port.getIndex(), false);
            client.releasePort(port.getIndex());
        }
    }

    /**
     * This is a specific test which is not related to standalone Java SDK
     */
    @Test
    @Ignore
    public void setL3Test() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        try {
            client.acquirePort(port.getIndex(), true);
            client.serviceMode(port.getIndex(), true);
            client.setL3Mode(port.getIndex(), null, "192.168.9.27", "192.168.9.28");
            String nextHopMac = client.resolveArp(port.getIndex(), "192.168.9.27", "192.168.9.28");
            boolean result = client.setL3Mode(port.getIndex(), nextHopMac, "192.168.9.27", "192.168.9.28");
            Assert.assertTrue(result);
        } finally {
            client.serviceMode(port.getIndex(), false);
            client.releasePort(port.getIndex());
        }
    }
    
    @Test
    public void startStopTrafficTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        client.acquirePort(port.getIndex(), true);

        Stream s = buildStream(SIMPLE_PACKET);
        client.addStream(port.getIndex(), s);

        Map<String, Object> mul = new HashMap<>();
        mul.put("op", "abs");
        mul.put("type", "pps");
        mul.put("value", 1.0);
        client.startTraffic(port.getIndex(), -1.0, true, mul, 1);
        
        PortStatus portStatusTX = client.getPortStatus(port.getIndex());
        Assert.assertTrue(portStatusTX.state.equals("TX"));
        client.stopTraffic(port.getIndex());

        PortStatus portStatusStream = client.getPortStatus(port.getIndex());
        Assert.assertTrue(portStatusStream.state.equals("STREAMS"));
        
        client.releasePort(port.getIndex());
    }

    /**
     * This is a specific test which is not related to standalone Java SDK
     */
    @Test
    @Ignore
    public void arpTest() {
        List<Port> ports = client.getPorts();
        Port port = ports.get(0);
        Port port1 = ports.get(1);
        client.acquirePort(port.getIndex(), true);
        client.acquirePort(port1.getIndex(), true);
        
        client.serviceMode(port.getIndex(), true);
        client.serviceMode(port1.getIndex(), true);

        client.removeRxQueue(port.getIndex());
        client.setRxQueue(port.getIndex(), 5);
            
        EthernetPacket pkt = buildArpPkt();
        client.sendPacket(port.getIndex(), pkt);

        Predicate<EthernetPacket> arpReplyFilter = etherPkt -> {
            if(etherPkt.contains(ArpPacket.class)) {
{'fraction_non_alphanumeric': 0.12559726962457338, 'fraction_numerical': 0.010648464163822525, 'mean_word_length': 2.75089605734767, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4227750', 'n_tokens_mistral': 4417, 'n_tokens_neox': 4323, 'n_words': 779}