|
1 | 1 | package com.cisco.trex.stateless; |
2 | 2 |
|
| 3 | +import static java.lang.Thread.sleep; |
3 | 4 |
|
4 | 5 | import com.cisco.trex.stateless.exception.TRexConnectionException; |
5 | 6 | import com.cisco.trex.stateless.exception.TRexTimeoutException; |
6 | 7 | import com.cisco.trex.stateless.model.*; |
7 | 8 | import com.cisco.trex.stateless.model.capture.*; |
| 9 | +import java.util.*; |
| 10 | +import java.util.stream.Collectors; |
8 | 11 | import org.junit.*; |
9 | 12 | import org.pcap4j.packet.EthernetPacket; |
10 | 13 | 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; |
17 | 14 |
|
18 | 15 | 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); |
30 | 42 | }); |
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); |
39 | 54 | }); |
| 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(); |
40 | 121 | } |
41 | 122 |
|
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(); |
49 | 134 | } |
50 | 135 |
|
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); |
96 | 141 |
|
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; |
100 | 145 | } |
101 | 146 |
|
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(); |
137 | 149 | } |
| 150 | + } |
138 | 151 | } |
0 commit comments