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/02/12 14:01:37 UTC

[incubator-plc4x] branch master updated: PLC4X-3 - Implement a testing framework for protocols

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 6f76878  PLC4X-3 - Implement a testing framework for protocols
6f76878 is described below

commit 6f76878e7233867b5ebb696dd1e474d70046e66c
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Feb 12 15:01:33 2018 +0100

    PLC4X-3 - Implement a testing framework for protocols
    
    - Added code for dumping pcap Files and moved the binary request data into binary test resource files
---
 .../java/s7/connection/S7PlcTestConnection.java    | 162 ++++++++++++++++-----
 .../s7/connection/iso-tp-connect-response.pcap     | Bin 0 -> 116 bytes
 .../s7-setup-communication-response.pcap           | Bin 0 -> 121 bytes
 plc4j/utils/raw-sockets/pom.xml                    |   6 -
 pom.xml                                            |  15 +-
 5 files changed, 136 insertions(+), 47 deletions(-)

diff --git a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
index b1e565f..c5fc9ac 100644
--- a/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
+++ b/plc4j/protocols/s7/src/test/java/org/apache/plc4x/java/s7/connection/S7PlcTestConnection.java
@@ -21,12 +21,14 @@ package org.apache.plc4x.java.s7.connection;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.embedded.EmbeddedChannel;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.plc4x.java.base.connection.TestChannelFactory;
-import org.apache.plc4x.java.isotp.netty.model.types.*;
 import org.apache.plc4x.java.netty.events.S7ConnectionEvent;
-import org.apache.plc4x.java.s7.netty.S7Protocol;
-import org.apache.plc4x.java.s7.netty.model.types.MessageType;
-import org.apache.plc4x.java.s7.netty.model.types.ParameterType;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 
 public class S7PlcTestConnection extends S7PlcConnection {
 
@@ -34,6 +36,31 @@ public class S7PlcTestConnection extends S7PlcConnection {
         super(new TestChannelFactory(), rack, slot, params);
     }
 
+    /*
+            byte[] setupCommunicationResponse = toByteArray(
+            new int[] {
+                // ISO on TCP packet
+                0x03, 0x00,
+                0x00, 0x1B,
+                // ISO TP packet
+                0x02,
+                TpduCode.DATA.getCode(),
+                0x80,
+                S7Protocol.S7_PROTOCOL_MAGIC_NUMBER,
+                MessageType.ACK_DATA.getCode(), 0x00, 0x00,
+                0x00, 0x01,
+                // Parameter Length
+                0x00, 0x08,
+                // Data Length
+                0x00, 0x00,
+                // Error codes
+                0x00, 0x00,
+                // Parameters:
+                ParameterType.SETUP_COMMUNICATION.getCode(), 0x00, 0x00, 0x08, 0x00, 0x08, 0x01, 0x00
+            });
+        dumpArrayToPcapFile("s7-setup-communication-response.pcap", setupCommunicationResponse);
+     */
+
     @Override
     protected void sendChannelCreatedEvent() {
         EmbeddedChannel channel = (EmbeddedChannel) getChannel();
@@ -47,20 +74,8 @@ public class S7PlcTestConnection extends S7PlcConnection {
         // TODO: Check the content of the Iso TP connection request.
 
         // Send an Iso TP connection response back to the pipeline.
-        byte[] connectionConfirm = toByteArray(
-            new int[] {
-                // ISO on TCP packet
-                0x03, 0x00, 0x00, 0x16,
-                // ISO TP packet
-                0x11,
-                TpduCode.CONNECTION_CONFIRM.getCode(),
-                0x00, 0x01, 0x00, 0x02,
-                ProtocolClass.CLASS_0.getCode(),
-
-                ParameterCode.CALLED_TSAP.getCode(), 0x02, DeviceGroup.PG_OR_PC.getCode(), 0x01,
-                ParameterCode.CALLING_TSAP.getCode(), 0x02, DeviceGroup.OTHERS.getCode(), 0x12,
-                ParameterCode.TPDU_SIZE.getCode(), 0x01, TpduSize.SIZE_512.getCode()
-            });
+        byte[] connectionConfirm = readPcapFile(
+            "org/apache/plc4x/java/s7/connection/iso-tp-connect-response.pcap");
         channel.writeInbound(Unpooled.wrappedBuffer(connectionConfirm));
 
         // Read a S7 Setup Communication request.
@@ -70,27 +85,8 @@ public class S7PlcTestConnection extends S7PlcConnection {
         // TODO: Check the content of the S7 Setup Communication connection request.
 
         // Send an S7 Setup Communication response back to the pipeline.
-        byte[] setupCommunicationResponse = toByteArray(
-            new int[] {
-                // ISO on TCP packet
-                0x03, 0x00,
-                0x00, 0x1B,
-                // ISO TP packet
-                0x02,
-                TpduCode.DATA.getCode(),
-                0x80,
-                S7Protocol.S7_PROTOCOL_MAGIC_NUMBER,
-                MessageType.ACK_DATA.getCode(), 0x00, 0x00,
-                0x00, 0x01,
-                // Parameter Length
-                0x00, 0x08,
-                // Data Length
-                0x00, 0x00,
-                // Error codes
-                0x00, 0x00,
-                // Parameters:
-                ParameterType.SETUP_COMMUNICATION.getCode(), 0x00, 0x00, 0x08, 0x00, 0x08, 0x01, 0x00
-            });
+        byte[] setupCommunicationResponse = readPcapFile(
+            "org/apache/plc4x/java/s7/connection/s7-setup-communication-response.pcap");
         channel.writeInbound(Unpooled.wrappedBuffer(setupCommunicationResponse));
     }
 
@@ -102,4 +98,92 @@ public class S7PlcTestConnection extends S7PlcConnection {
         return out;
     }
 
+    public static void dumpArrayToPcapFile(String filename, byte[] data) {
+        byte[] pcapHeader = toByteArray(
+            new int[]{
+                // Magic Bytes (Big Endian)
+                0xa1, 0xb2, 0xc3, 0xd4,
+                // Version (2.4)
+                0x00, 0x02, 0x00, 0x04,
+                // Timestamp
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                // Max Length
+                0x00, 0x00, 0xFF, 0xFF,
+                // Link Layer: Ethernet
+                0x00, 0x00, 0x00, 0x01,
+                // Timestamp (Seconds)
+                0x00, 0x00, 0x00, 0x00,
+                // Timestamp (Microseconds)
+                0x00, 0x00, 0x00, 0x00,
+                // Packet Length
+                0x00, 0x00, 0x00, 0x00,
+                // Packet Length (duplicate)
+                0x00, 0x00, 0x00, 0x00, // 40
+                /////////////////////////////////////
+                // Ethernet Header
+                // Destination Address (Mac Address)
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                // Source Address (Mac Address)
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                // IPv4
+                0x08, 0x00,
+                /////////////////////////////////////
+                // IPv4 Header (type + 20 byte length)
+                0x45, 0x00,
+                // Length
+                0x00, 0x00, // 40 + payload
+                0x00, 0x00,
+                0x00, 0x00,
+                0x1e,
+                // Protocol (TCP)
+                0x06,
+                // Header Checksum
+                0x00, 0x00,
+                // Source IP
+                0x00, 0x00, 0x00, 0x00,
+                // Dest IP
+                0x00, 0x00, 0x00, 0x00, // 34
+                /////////////////////////////////////
+                // TCP Header
+                // Source Port
+                0x00, 0x66,
+                // Dest Port
+                0x10, 0xd1,
+                0x00, 0x00, 0x00, 0x00,
+                0x00, 0x00, 0x00, 0x00,
+                // Header Length
+                0x50, 0x18,
+                0xFA, 0xDA,
+                0x00, 0x00,
+                0x00, 0x00 // 54
+            });
+
+        byte[] total = new byte[pcapHeader.length + data.length];
+        System.arraycopy(pcapHeader, 0, total, 0, pcapHeader.length);
+        System.arraycopy(data, 0, total, pcapHeader.length, data.length);
+        total[35] = (byte) (data.length + 54);
+        total[39] = (byte) (data.length + 54);
+        total[39+18] = (byte) (data.length + 40);
+
+        try {
+            File file = new File(filename);
+            System.out.println("Dumping to file: "  + file.getAbsolutePath());
+            FileUtils.writeByteArrayToFile(file, total);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public byte[] readPcapFile(String filename) {
+        try {
+            InputStream in = getClass().getClassLoader().getResourceAsStream(filename);
+            byte[] pcap = IOUtils.toByteArray(in);
+            byte[] data = new byte[pcap.length - 94];
+            System.arraycopy(pcap, 94, data, 0, pcap.length - 94);
+            return data;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
 }
diff --git a/plc4j/protocols/s7/src/test/resources/org/apache/plc4x/java/s7/connection/iso-tp-connect-response.pcap b/plc4j/protocols/s7/src/test/resources/org/apache/plc4x/java/s7/connection/iso-tp-connect-response.pcap
new file mode 100644
index 0000000..d0a56c9
Binary files /dev/null and b/plc4j/protocols/s7/src/test/resources/org/apache/plc4x/java/s7/connection/iso-tp-connect-response.pcap differ
diff --git a/plc4j/protocols/s7/src/test/resources/org/apache/plc4x/java/s7/connection/s7-setup-communication-response.pcap b/plc4j/protocols/s7/src/test/resources/org/apache/plc4x/java/s7/connection/s7-setup-communication-response.pcap
new file mode 100644
index 0000000..bc79c91
Binary files /dev/null and b/plc4j/protocols/s7/src/test/resources/org/apache/plc4x/java/s7/connection/s7-setup-communication-response.pcap differ
diff --git a/plc4j/utils/raw-sockets/pom.xml b/plc4j/utils/raw-sockets/pom.xml
index 01966ec..f22a424 100644
--- a/plc4j/utils/raw-sockets/pom.xml
+++ b/plc4j/utils/raw-sockets/pom.xml
@@ -34,10 +34,6 @@
   <name>PLC4J: Utils: Raw-Sockets</name>
   <description>An implementation of a Netty Channel that allows implementing protocols below the TCP and UCP level.</description>
 
-  <properties>
-    <pcap4j.version>1.7.3</pcap4j.version>
-  </properties>
-
   <dependencies>
     <dependency>
       <groupId>io.netty</groupId>
@@ -47,12 +43,10 @@
     <dependency>
       <groupId>org.pcap4j</groupId>
       <artifactId>pcap4j-core</artifactId>
-      <version>${pcap4j.version}</version>
     </dependency>
     <dependency>
       <groupId>org.pcap4j</groupId>
       <artifactId>pcap4j-packetfactory-static</artifactId>
-      <version>${pcap4j.version}</version>
       <scope>runtime</scope>
     </dependency>
 
diff --git a/pom.xml b/pom.xml
index 59baab9..f742afc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -92,11 +92,12 @@
     <assertj.version>3.8.0</assertj.version>
     <commons-io.version>2.6</commons-io.version>
     <commons-lang.version>2.6</commons-lang.version>
+    <gson.version>2.8.0</gson.version>
     <junit.version>4.12</junit.version>
     <logback.version>1.2.3</logback.version>
-    <gson.version>2.8.0</gson.version>
-    <netty.version>4.1.17.Final</netty.version>
     <mockito.version>2.13.0</mockito.version>
+    <netty.version>4.1.17.Final</netty.version>
+    <pcap4j.version>1.7.3</pcap4j.version>
     <slf4j.version>1.7.25</slf4j.version>
   </properties>
 
@@ -184,6 +185,16 @@
         <artifactId>commons-lang3</artifactId>
         <version>3.7</version>
       </dependency>
+      <dependency>
+        <groupId>org.pcap4j</groupId>
+        <artifactId>pcap4j-core</artifactId>
+        <version>${pcap4j.version}</version>
+      </dependency>
+      <dependency>
+        <groupId>org.pcap4j</groupId>
+        <artifactId>pcap4j-packetfactory-static</artifactId>
+        <version>${pcap4j.version}</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 

-- 
To stop receiving notification emails like this one, please contact
cdutz@apache.org.