Skip to content

Commit ac56844

Browse files
author
Emil Gustafsson
committed
format all files
Signed-off-by: Emil Gustafsson <[email protected]>
1 parent 1eb92d0 commit ac56844

File tree

120 files changed

+10027
-10243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+10027
-10243
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ build/
66
.project
77
.settings/
88
/bin/
9+
target/
Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,36 @@
11
package com.cisco.trex.stateless;
22

33
import com.cisco.trex.stateless.exception.TRexConnectionException;
4-
import com.cisco.trex.stateless.exception.TRexTimeoutException;
54
import com.cisco.trex.stateless.model.*;
5+
import java.util.List;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9-
import java.util.Collections;
10-
import java.util.HashMap;
11-
import java.util.List;
12-
import java.util.Map;
13-
149
public class TRexClientApp {
15-
public static TRexClient client = null;
16-
public static final String client_name = "trex-test-user";
17-
public static final String trex_host = "trex.mycompany.com";
18-
public static final String trex_port = "4501";
19-
private static Logger logger = LoggerFactory.getLogger(TRexClientApp.class);
20-
21-
// API usage example. see also:
22-
// https://github.com/cisco-system-traffic-generator/trex-java-sdk/blob/master/src/test/java/com/cisco/trex/stateless/TRexClientTest.java
10+
public static TRexClient client = null;
11+
public static final String client_name = "trex-test-user";
12+
public static final String trex_host = "trex.mycompany.com";
13+
public static final String trex_port = "4501";
14+
private static Logger logger = LoggerFactory.getLogger(TRexClientApp.class);
2315

24-
public static void main(String[] args) {
25-
client = new TRexClient(trex_host, trex_port, client_name);
26-
try {
27-
client.connect();
28-
List<Port> ports = client.getPorts();
29-
System.out.println("Found " + ports.size() + " ports");
30-
List<String> cmds = client.getSupportedCommands();
31-
logger.info("List of available commands:");
32-
for (String cmd : cmds) {
33-
logger.info(" - " + cmd);
34-
}
35-
} catch (TRexConnectionException e) {
36-
e.printStackTrace();
37-
} finally {
38-
client.disconnect();
39-
}
16+
// API usage example. see also:
17+
// https://github.com/cisco-system-traffic-generator/trex-java-sdk/blob/master/src/test/java/com/cisco/trex/stateless/TRexClientTest.java
18+
19+
public static void main(String[] args) {
20+
client = new TRexClient(trex_host, trex_port, client_name);
21+
try {
22+
client.connect();
23+
List<Port> ports = client.getPorts();
24+
System.out.println("Found " + ports.size() + " ports");
25+
List<String> cmds = client.getSupportedCommands();
26+
logger.info("List of available commands:");
27+
for (String cmd : cmds) {
28+
logger.info(" - " + cmd);
29+
}
30+
} catch (TRexConnectionException e) {
31+
e.printStackTrace();
32+
} finally {
33+
client.disconnect();
4034
}
35+
}
4136
}
42-
Lines changed: 128 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,151 @@
11
package com.cisco.trex.stateless;
22

3+
import static java.lang.Thread.sleep;
34

45
import com.cisco.trex.stateless.exception.TRexConnectionException;
56
import com.cisco.trex.stateless.exception.TRexTimeoutException;
67
import com.cisco.trex.stateless.model.*;
78
import com.cisco.trex.stateless.model.capture.*;
9+
import java.util.*;
10+
import java.util.stream.Collectors;
811
import org.junit.*;
912
import org.pcap4j.packet.EthernetPacket;
1013
import org.pcap4j.packet.IllegalRawDataException;
11-
import org.pcap4j.packet.Packet;
12-
13-
import java.util.*;
14-
import java.util.stream.Collectors;
15-
16-
import static java.lang.Thread.sleep;
1714

1815
public class TRexClientBPFTest {
19-
public static final String CLIENT_USER = "unit-tests-user";
20-
private static final Integer STREAM_ID = 100500;
21-
public static TRexClient client;
22-
23-
@BeforeClass
24-
public static void setUp() throws TRexConnectionException, TRexTimeoutException {
25-
client = new TRexClient("trex-host", "4501", CLIENT_USER);
26-
client.connect();
27-
client.getPorts().forEach(p -> {
28-
client.acquirePort(p.getIndex(), true);
29-
client.serviceMode(p.getIndex(), true);
16+
public static final String CLIENT_USER = "unit-tests-user";
17+
private static final Integer STREAM_ID = 100500;
18+
public static TRexClient client;
19+
20+
@BeforeClass
21+
public static void setUp() throws TRexConnectionException, TRexTimeoutException {
22+
client = new TRexClient("trex-host", "4501", CLIENT_USER);
23+
client.connect();
24+
client
25+
.getPorts()
26+
.forEach(
27+
p -> {
28+
client.acquirePort(p.getIndex(), true);
29+
client.serviceMode(p.getIndex(), true);
30+
});
31+
}
32+
33+
@Test
34+
public void captureMonitorFilterTest() throws InterruptedException, IllegalRawDataException {
35+
filterTestBase(
36+
(f) -> {
37+
List<Integer> rxPorts =
38+
client.getPorts().stream().map(Port::getIndex).collect(Collectors.toList());
39+
List<Integer> txPorts =
40+
client.getPorts().stream().map(Port::getIndex).collect(Collectors.toList());
41+
return client.captureMonitorStart(rxPorts, txPorts, f);
3042
});
31-
}
32-
33-
@Test
34-
public void captureMonitorFilterTest() throws InterruptedException, IllegalRawDataException {
35-
filterTestBase((f) -> {
36-
List<Integer> rxPorts = client.getPorts().stream().map(Port::getIndex).collect(Collectors.toList());
37-
List<Integer> txPorts = client.getPorts().stream().map(Port::getIndex).collect(Collectors.toList());
38-
return client.captureMonitorStart(rxPorts, txPorts, f);
43+
}
44+
45+
@Test
46+
public void captureRecorderFilterTest() throws InterruptedException, IllegalRawDataException {
47+
filterTestBase(
48+
(f) -> {
49+
List<Integer> rxPorts =
50+
client.getPorts().stream().map(Port::getIndex).collect(Collectors.toList());
51+
List<Integer> txPorts =
52+
client.getPorts().stream().map(Port::getIndex).collect(Collectors.toList());
53+
return client.captureRecorderStart(rxPorts, txPorts, f, 1000);
3954
});
55+
}
56+
57+
private void filterTestBase(CaptureMonitorStarter monitorStarter)
58+
throws InterruptedException, IllegalRawDataException {
59+
List<Port> ports = client.getPorts();
60+
Port port = ports.get(0);
61+
client.acquirePort(port.getIndex(), true);
62+
client.serviceMode(port.getIndex(), true);
63+
client.removeAllCaptures();
64+
client.removeAllStreams(port.getIndex());
65+
66+
final String srcMacA = "11:00:00:00:00:11";
67+
final String srcMacB = "33:00:00:00:00:33";
68+
69+
client.addStream(
70+
port.getIndex(), TRexClientTest.buildStream(TRexClientTest.buildArpPkt(srcMacA)));
71+
client.addStream(
72+
port.getIndex(), TRexClientTest.buildStream(TRexClientTest.buildArpPkt(srcMacB)));
73+
74+
Map<String, Object> mul = new HashMap<>();
75+
mul.put("op", "abs");
76+
mul.put("type", "pps");
77+
mul.put("value", 1.0);
78+
79+
CaptureTester unfiltered = new CaptureTester(monitorStarter, "");
80+
CaptureTester filteredA = new CaptureTester(monitorStarter, "ether src " + srcMacA);
81+
CaptureTester filteredB = new CaptureTester(monitorStarter, "ether src " + srcMacB);
82+
Assert.assertEquals(3, client.getActiveCaptures().get().length);
83+
84+
client.startTraffic(port.getIndex(), -1.0, true, mul, 1);
85+
86+
sleep(3000);
87+
88+
client.stopTraffic(port.getIndex());
89+
unfiltered.StopAndFetch();
90+
filteredA.StopAndFetch();
91+
filteredB.StopAndFetch();
92+
93+
Assert.assertTrue(unfiltered.PacketsCount() != 0);
94+
Assert.assertTrue(filteredA.PacketsCount() != 0);
95+
Assert.assertTrue(filteredB.PacketsCount() != 0);
96+
97+
Assert.assertEquals(filteredA.PacketsWithSrcCount(srcMacA), filteredA.PacketsCount());
98+
Assert.assertEquals(filteredB.PacketsWithSrcCount(srcMacB), filteredB.PacketsCount());
99+
Assert.assertEquals(0, filteredA.PacketsWithSrcCount(srcMacB));
100+
Assert.assertEquals(0, filteredB.PacketsWithSrcCount(srcMacA));
101+
102+
int ABInUnfiltered =
103+
unfiltered.PacketsWithSrcCount(srcMacA) + unfiltered.PacketsWithSrcCount(srcMacB);
104+
Assert.assertEquals(ABInUnfiltered, filteredA.PacketsCount() + filteredB.PacketsCount());
105+
}
106+
107+
@FunctionalInterface
108+
private interface CaptureMonitorStarter {
109+
TRexClientResult<CaptureMonitor> start(String filter);
110+
}
111+
112+
private class CaptureTester {
113+
CaptureMonitor monitor;
114+
CaptureInfo info;
115+
CapturedPackets packets;
116+
117+
public CaptureTester(CaptureMonitorStarter s, String filter) {
118+
TRexClientResult<CaptureMonitor> startResult = s.start(filter);
119+
Assert.assertFalse(startResult.getError(), startResult.isFailed());
120+
monitor = startResult.get();
40121
}
41122

42-
@Test
43-
public void captureRecorderFilterTest() throws InterruptedException, IllegalRawDataException {
44-
filterTestBase((f) -> {
45-
List<Integer> rxPorts = client.getPorts().stream().map(Port::getIndex).collect(Collectors.toList());
46-
List<Integer> txPorts = client.getPorts().stream().map(Port::getIndex).collect(Collectors.toList());
47-
return client.captureRecorderStart(rxPorts, txPorts, f, 1000);
48-
});
123+
public void StopAndFetch() {
124+
client.captureMonitorStop(monitor.getCaptureId());
125+
info =
126+
Arrays.stream(client.getActiveCaptures().get())
127+
.filter(cap -> cap.getId() == monitor.getCaptureId())
128+
.findFirst()
129+
.get();
130+
TRexClientResult<CapturedPackets> result =
131+
client.captureFetchPkts(info.getId(), info.getCount());
132+
Assert.assertFalse(result.isFailed());
133+
packets = result.get();
49134
}
50135

51-
private void filterTestBase(CaptureMonitorStarter monitorStarter) throws InterruptedException, IllegalRawDataException {
52-
List<Port> ports = client.getPorts();
53-
Port port = ports.get(0);
54-
client.acquirePort(port.getIndex(), true);
55-
client.serviceMode(port.getIndex(), true);
56-
client.removeAllCaptures();
57-
client.removeAllStreams(port.getIndex());
58-
59-
final String srcMacA = "11:00:00:00:00:11";
60-
final String srcMacB = "33:00:00:00:00:33";
61-
62-
client.addStream(port.getIndex(), TRexClientTest.buildStream(TRexClientTest.buildArpPkt(srcMacA)));
63-
client.addStream(port.getIndex(), TRexClientTest.buildStream(TRexClientTest.buildArpPkt(srcMacB)));
64-
65-
Map<String, Object> mul = new HashMap<>();
66-
mul.put("op", "abs");
67-
mul.put("type", "pps");
68-
mul.put("value", 1.0);
69-
70-
CaptureTester unfiltered = new CaptureTester(monitorStarter, "");
71-
CaptureTester filteredA = new CaptureTester(monitorStarter, "ether src " + srcMacA);
72-
CaptureTester filteredB = new CaptureTester(monitorStarter, "ether src " + srcMacB);
73-
Assert.assertEquals(3, client.getActiveCaptures().get().length);
74-
75-
client.startTraffic(port.getIndex(), -1.0, true, mul, 1);
76-
77-
sleep(3000);
78-
79-
client.stopTraffic(port.getIndex());
80-
unfiltered.StopAndFetch();
81-
filteredA.StopAndFetch();
82-
filteredB.StopAndFetch();
83-
84-
Assert.assertTrue(unfiltered.PacketsCount() != 0);
85-
Assert.assertTrue(filteredA.PacketsCount() != 0);
86-
Assert.assertTrue(filteredB.PacketsCount() != 0);
87-
88-
Assert.assertEquals(filteredA.PacketsWithSrcCount(srcMacA), filteredA.PacketsCount());
89-
Assert.assertEquals(filteredB.PacketsWithSrcCount(srcMacB), filteredB.PacketsCount());
90-
Assert.assertEquals(0, filteredA.PacketsWithSrcCount(srcMacB));
91-
Assert.assertEquals(0, filteredB.PacketsWithSrcCount(srcMacA));
92-
93-
int ABInUnfiltered = unfiltered.PacketsWithSrcCount(srcMacA) + unfiltered.PacketsWithSrcCount(srcMacB);
94-
Assert.assertEquals(ABInUnfiltered, filteredA.PacketsCount() + filteredB.PacketsCount());
95-
}
136+
public int PacketsWithSrcCount(String srcAddr) throws IllegalRawDataException {
137+
int total = 0;
138+
for (CapturedPkt p : packets.getPkts()) {
139+
byte[] pktBin = Base64.getDecoder().decode(p.getBinary());
140+
EthernetPacket etherPkt = EthernetPacket.newPacket(pktBin, 0, pktBin.length);
96141

97-
@FunctionalInterface
98-
private interface CaptureMonitorStarter {
99-
TRexClientResult<CaptureMonitor> start(String filter);
142+
if (etherPkt.getHeader().getSrcAddr().toString().equalsIgnoreCase(srcAddr)) total++;
143+
}
144+
return total;
100145
}
101146

102-
private class CaptureTester {
103-
CaptureMonitor monitor;
104-
CaptureInfo info;
105-
CapturedPackets packets;
106-
107-
public CaptureTester(CaptureMonitorStarter s, String filter) {
108-
TRexClientResult<CaptureMonitor> startResult = s.start(filter);
109-
Assert.assertFalse(startResult.getError(), startResult.isFailed());
110-
monitor = startResult.get();
111-
}
112-
113-
public void StopAndFetch() {
114-
client.captureMonitorStop(monitor.getCaptureId());
115-
info = Arrays.stream(client.getActiveCaptures().get())
116-
.filter(cap -> cap.getId() == monitor.getCaptureId()).findFirst().get();
117-
TRexClientResult<CapturedPackets> result = client.captureFetchPkts(info.getId(), info.getCount());
118-
Assert.assertFalse(result.isFailed());
119-
packets = result.get();
120-
}
121-
122-
public int PacketsWithSrcCount(String srcAddr) throws IllegalRawDataException {
123-
int total = 0;
124-
for(CapturedPkt p : packets.getPkts()) {
125-
byte[] pktBin = Base64.getDecoder().decode(p.getBinary());
126-
EthernetPacket etherPkt = EthernetPacket.newPacket(pktBin, 0, pktBin.length);
127-
128-
if(etherPkt.getHeader().getSrcAddr().toString().equalsIgnoreCase(srcAddr))
129-
total++;
130-
}
131-
return total;
132-
}
133-
134-
public int PacketsCount() {
135-
return info.getCount();
136-
}
147+
public int PacketsCount() {
148+
return info.getCount();
137149
}
150+
}
138151
}

0 commit comments

Comments
 (0)