You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2018/12/10 09:46:48 UTC

[incubator-plc4x] branch develop updated: PLC4X-78 - Write operations seem to fail - Fixed a problem that was causing a test to fail / Extended the S7PlcTestConnection to dump the content of packets not matching the expected packet

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 3d9ba43  PLC4X-78 - Write operations seem to fail - Fixed a problem that was causing a test to fail / Extended the S7PlcTestConnection to dump the content of packets not matching the expected packet
3d9ba43 is described below

commit 3d9ba4326cda6d88d79f58a54323dd54e00dc9ca
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Dec 10 10:46:44 2018 +0100

    PLC4X-78 - Write operations seem to fail
    - Fixed a problem that was causing a test to fail / Extended the S7PlcTestConnection to dump the content of packets not matching the expected packet
---
 .../java/s7/connection/S7PlcConnectionIT.java      |  17 +++++------
 .../java/s7/connection/S7PlcTestConnection.java    |  33 +++++++++++++--------
 .../java/s7/connection/s7-write-var-request.pcapng | Bin 404 -> 404 bytes
 3 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionIT.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionIT.java
index 831d0d3..8d07a4a 100644
--- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionIT.java
+++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcConnectionIT.java
@@ -24,11 +24,8 @@ import org.apache.plc4x.java.api.messages.PlcReadRequest;
 import org.apache.plc4x.java.api.messages.PlcReadResponse;
 import org.apache.plc4x.java.api.messages.PlcWriteRequest;
 import org.apache.plc4x.java.api.messages.PlcWriteResponse;
-import org.junit.After;
-import org.junit.Before;
 import org.junit.Rule;
-import org.junit.Test;
-import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.*;
 import org.junit.rules.Timeout;
 
 import java.util.concurrent.CompletableFuture;
@@ -45,13 +42,13 @@ public class S7PlcConnectionIT {
 
     private S7PlcTestConnection SUT;
 
-    @Before
+    @BeforeEach
     public void setUp() {
         SUT = new S7PlcTestConnection(1, 2,
             "pdu-size=128&max-amq-caller=2&max-amq-callee=3&unknown=parameter&unknown-flag");
     }
 
-    @After
+    @AfterEach
     public void tearDown() throws PlcConnectionException{
         if(SUT.isConnected()) {
             SUT.close();
@@ -66,7 +63,7 @@ public class S7PlcConnectionIT {
     }
 
     @Test
-    public void read() throws Exception {
+    public void read(TestInfo testInfo) throws Exception {
         SUT.connect();
         EmbeddedChannel channel = (EmbeddedChannel) SUT.getChannel();
         assertThat("No outbound messages should exist.", channel.outboundMessages().size(), equalTo(0));
@@ -76,7 +73,7 @@ public class S7PlcConnectionIT {
         // Check that one message has been sent.
         assertThat("Exactly one outbound message should exist after sending.",
             channel.outboundMessages().size(), equalTo(1));
-        SUT.verifyPcapFile("org/apache/plc4x/java/s7/connection/s7-read-var-request.pcapng");
+        SUT.verifyPcapFile("org/apache/plc4x/java/s7/connection/s7-read-var-request.pcapng", testInfo);
 
         // Manually feed a packet response into the channel.
         SUT.sendPcapFile("org/apache/plc4x/java/s7/connection/s7-read-var-response.pcapng");
@@ -108,7 +105,7 @@ public class S7PlcConnectionIT {
     }
 
     @Test
-    public void write() throws Exception {
+    public void write(TestInfo testInfo) throws Exception {
         SUT.connect();
         EmbeddedChannel channel = (EmbeddedChannel) SUT.getChannel();
         assertThat("No outbound messages should exist.", channel.outboundMessages().size(), equalTo(0));
@@ -118,7 +115,7 @@ public class S7PlcConnectionIT {
         // Check that one message has been sent.
         assertThat("Exactly one outbound message should exist after sending.",
             channel.outboundMessages().size(), equalTo(1));
-        SUT.verifyPcapFile("org/apache/plc4x/java/s7/connection/s7-write-var-request.pcapng");
+        SUT.verifyPcapFile("org/apache/plc4x/java/s7/connection/s7-write-var-request.pcapng", testInfo);
 
         // Manually feed a packet response into the channel.
         SUT.sendPcapFile("org/apache/plc4x/java/s7/connection/s7-write-var-response.pcapng");
diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
index 15d5770..d8f250e 100644
--- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
+++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
@@ -24,16 +24,16 @@ import io.netty.channel.embedded.EmbeddedChannel;
 import org.apache.commons.io.IOUtils;
 import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
 import org.apache.plc4x.java.base.connection.TestChannelFactory;
-import org.pcap4j.core.NotOpenException;
-import org.pcap4j.core.PcapHandle;
-import org.pcap4j.core.PcapNativeException;
-import org.pcap4j.core.Pcaps;
+import org.junit.jupiter.api.TestInfo;
+import org.pcap4j.core.*;
 import org.pcap4j.packet.Packet;
+import org.pcap4j.packet.namednumber.DataLinkType;
 
 import java.io.EOFException;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Method;
 import java.util.Arrays;
 import java.util.Objects;
 import java.util.concurrent.TimeoutException;
@@ -160,34 +160,43 @@ public class S7PlcTestConnection extends S7PlcConnection {
         }
     }
 
-    public void verifyPcapFile(String filename) {
+    public void verifyPcapFile(String filename, TestInfo testInfo) {
         try {
             ClassLoader classLoader = getClass().getClassLoader();
             File file = new File(Objects.requireNonNull(classLoader.getResource(filename)).getFile());
             PcapHandle responsePcap = Pcaps.openOffline(file.getAbsolutePath());
             Packet packet = responsePcap.getNextPacketEx();
-            byte[] actData = packet.getPayload().getPayload().getPayload().getRawData();
+            byte[] refData = packet.getPayload().getPayload().getPayload().getRawData();
 
             // Get the systems output.
             EmbeddedChannel channel = (EmbeddedChannel) getChannel();
             ByteBuf request = channel.readOutbound();
 
             // Check the sizes are equal.
-            assertThat(actData.length, equalTo(request.readableBytes()));
+            assertThat(refData.length, equalTo(request.readableBytes()));
 
             // Read the raw data sent to the output.
-            byte[] refData = new byte[request.readableBytes()];
-            request.readBytes(refData);
+            byte[] actData = new byte[request.readableBytes()];
+            request.readBytes(actData);
 
             // Compare the actual output to the reference output
             if(!Arrays.equals(actData, refData)) {
-                for(int i = 0; i < actData.length; i++) {
-                    if(actData[i] != refData[i]) {
+                String currentWorkingDir = System.getProperty("user.dir");
+                Class<?> testClass = testInfo.getTestClass().orElse(Object.class);
+                Method testMethod = testInfo.getTestMethod().orElse(null);
+                String fileName = currentWorkingDir + "/target/failsafe-reports/failure-" + testClass.getSimpleName() + "-"  + testMethod.getName() + ".pcapng";
+                try (PcapHandle handle = Pcaps.openDead(DataLinkType.EN10MB, 65536)) {
+                    PcapDumper dumper = handle.dumpOpen(fileName);
+                    dumper.dumpRaw(actData);
+                    dumper.flush();
+                }
+                for (int i = 0; i < actData.length; i++) {
+                    if (actData[i] != refData[i]) {
                         fail("Mismatch at position " + i);
                     }
                 }
             }
-        } catch (PcapNativeException | EOFException | TimeoutException | NotOpenException e) {
+        } catch (Exception e) {
             throw new RuntimeException("Error sending pacap file " + filename, e);
         }
     }
diff --git a/plc4j/drivers/s7/src/test/resources/org/apache/plc4x/java/s7/connection/s7-write-var-request.pcapng b/plc4j/drivers/s7/src/test/resources/org/apache/plc4x/java/s7/connection/s7-write-var-request.pcapng
index 981c9ee..1cace06 100644
Binary files a/plc4j/drivers/s7/src/test/resources/org/apache/plc4x/java/s7/connection/s7-write-var-request.pcapng and b/plc4j/drivers/s7/src/test/resources/org/apache/plc4x/java/s7/connection/s7-write-var-request.pcapng differ