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 2019/01/17 14:11:28 UTC

[incubator-plc4x] 01/02: - Defined a new Annotation "RequirePcapNg" you can annotate Junit5 tests with to automatically disable tests, if the libpcap requirement is not met (Especially on Windows)

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

commit 297b09002aa698735fa6769f98a57e45c97eb2ca
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Thu Jan 17 15:06:10 2019 +0100

    - Defined a new Annotation "RequirePcapNg" you can annotate Junit5 tests with to automatically disable tests, if the libpcap requirement is not met (Especially on Windows)
---
 plc4j/drivers/s7/pom.xml                           |  6 ++
 .../java/s7/connection/S7PlcConnectionIT.java      |  4 ++
 plc4j/utils/wireshark-utils/pom.xml                | 20 +++++++
 .../DisableOnMissingOrTooOldLibpcapVersion.java    | 69 ++++++++++++++++++++++
 .../org/apache/plc4x/java/utils/RequirePcapNg.java | 33 +++++++++++
 5 files changed, 132 insertions(+)

diff --git a/plc4j/drivers/s7/pom.xml b/plc4j/drivers/s7/pom.xml
index a7f5b16..124aa47 100644
--- a/plc4j/drivers/s7/pom.xml
+++ b/plc4j/drivers/s7/pom.xml
@@ -108,6 +108,12 @@
     </dependency>
     <dependency>
       <groupId>org.apache.plc4x</groupId>
+      <artifactId>plc4j-utils-wireshark-utils</artifactId>
+      <version>0.4.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.plc4x</groupId>
       <artifactId>plc4j-utils-test-utils</artifactId>
       <version>0.4.0-SNAPSHOT</version>
       <scope>test</scope>
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 8d07a4a..4cf39e4 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,6 +24,7 @@ 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.apache.plc4x.java.utils.RequirePcapNg;
 import org.junit.Rule;
 import org.junit.jupiter.api.*;
 import org.junit.rules.Timeout;
@@ -63,6 +64,7 @@ public class S7PlcConnectionIT {
     }
 
     @Test
+    @RequirePcapNg
     public void read(TestInfo testInfo) throws Exception {
         SUT.connect();
         EmbeddedChannel channel = (EmbeddedChannel) SUT.getChannel();
@@ -96,6 +98,7 @@ public class S7PlcConnectionIT {
      */
     @Test
     @Disabled
+    @RequirePcapNg
     public void readLargeResponse() throws Exception {
         SUT.connect();
         EmbeddedChannel channel = (EmbeddedChannel) SUT.getChannel();
@@ -105,6 +108,7 @@ public class S7PlcConnectionIT {
     }
 
     @Test
+    @RequirePcapNg
     public void write(TestInfo testInfo) throws Exception {
         SUT.connect();
         EmbeddedChannel channel = (EmbeddedChannel) SUT.getChannel();
diff --git a/plc4j/utils/wireshark-utils/pom.xml b/plc4j/utils/wireshark-utils/pom.xml
index 88cb243..dfd4a02 100644
--- a/plc4j/utils/wireshark-utils/pom.xml
+++ b/plc4j/utils/wireshark-utils/pom.xml
@@ -37,6 +37,26 @@
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.pcap4j</groupId>
+      <artifactId>pcap4j-core</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-artifact</artifactId>
+      <version>3.0.3</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-api</artifactId>
+    </dependency>
   </dependencies>
 
 </project>
\ No newline at end of file
diff --git a/plc4j/utils/wireshark-utils/src/main/java/org/apache/plc4x/java/utils/DisableOnMissingOrTooOldLibpcapVersion.java b/plc4j/utils/wireshark-utils/src/main/java/org/apache/plc4x/java/utils/DisableOnMissingOrTooOldLibpcapVersion.java
new file mode 100644
index 0000000..3fcc046
--- /dev/null
+++ b/plc4j/utils/wireshark-utils/src/main/java/org/apache/plc4x/java/utils/DisableOnMissingOrTooOldLibpcapVersion.java
@@ -0,0 +1,69 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.java.utils;
+
+import org.apache.commons.lang3.SystemUtils;
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
+import org.junit.jupiter.api.extension.ConditionEvaluationResult;
+import org.junit.jupiter.api.extension.ExecutionCondition;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.pcap4j.core.Pcaps;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class DisableOnMissingOrTooOldLibpcapVersion implements ExecutionCondition {
+
+    @Override
+    public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
+
+        // Mac: "libpcap version 1.8.1 -- Apple version 79.200.4";
+        // Linux: ""
+        // Windows: "WinPcap version 4.1.3 (packet.dll version 4.1.0.2980), based on libpcap version 1.0 branch 1_0_rel0b (20091008)"
+        try {
+            String libVersion = Pcaps.libVersion();
+            Pattern pattern = Pattern.compile("^libpcap version (?<version>\\d+\\.\\d+(\\.\\d+)?).*$");
+            Matcher matcher = pattern.matcher(libVersion);
+            if (matcher.matches()) {
+                String versionString = matcher.group("version");
+                DefaultArtifactVersion curVersion = new DefaultArtifactVersion(versionString);
+                DefaultArtifactVersion minVersion = new DefaultArtifactVersion("1.1.0");
+                if (curVersion.compareTo(minVersion) >= 0) {
+                    return ConditionEvaluationResult.enabled("Found libpcap version " + versionString);
+                } else {
+                    if (SystemUtils.IS_OS_WINDOWS) {
+                        return ConditionEvaluationResult.disabled("Test disabled due to too old WinPcap version. Please install at least version 1.1.0 to support all features. Please install from here: https://sourceforge.net/projects/winpcap413-176/ as this version supports all needed freatures.");
+                    } else {
+                        return ConditionEvaluationResult.disabled("Test disabled due to too old libpcap version. Please install at least version 1.1.0 to support all features.");
+                    }
+                }
+            }
+        } catch(Exception e) {
+            e.printStackTrace();
+            // Ignore this ...
+        }
+        if(SystemUtils.IS_OS_WINDOWS) {
+            return ConditionEvaluationResult.disabled("Test disabled due to missing or invalid WinPcap version. Please install from here: https://sourceforge.net/projects/winpcap413-176/ as this version supports all needed freatures.");
+        } else {
+            return ConditionEvaluationResult.disabled("Test disabled due to missing or invalid libpcap version. Please install at least version 1.1.0 to support all features.");
+        }
+    }
+
+}
diff --git a/plc4j/utils/wireshark-utils/src/main/java/org/apache/plc4x/java/utils/RequirePcapNg.java b/plc4j/utils/wireshark-utils/src/main/java/org/apache/plc4x/java/utils/RequirePcapNg.java
new file mode 100644
index 0000000..86a6741
--- /dev/null
+++ b/plc4j/utils/wireshark-utils/src/main/java/org/apache/plc4x/java/utils/RequirePcapNg.java
@@ -0,0 +1,33 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+package org.apache.plc4x.java.utils;
+
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ ElementType.TYPE, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+@ExtendWith(DisableOnMissingOrTooOldLibpcapVersion.class)
+public @interface RequirePcapNg {
+}