You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/02/02 10:17:26 UTC

[incubator-plc4x] branch feature/Beckhoff_ADS_protocol updated: fixed wrong implementation of Length

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

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


The following commit(s) were added to refs/heads/feature/Beckhoff_ADS_protocol by this push:
     new 46fa650  fixed wrong implementation of Length
46fa650 is described below

commit 46fa6504226d71010fe834ab9cfa9c8919e97860
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Feb 2 11:17:22 2018 +0100

    fixed wrong implementation of Length
---
 .../plc4x/java/ads/api/commands/types/Length.java  | 19 +++++++-
 .../plc4x/java/ads/api/generic/types/AMSPort.java  |  4 +-
 .../apache/plc4x/java/ads/api/util/ByteValue.java  |  7 +++
 .../java/ads/api/commands/types/LengthTest.java    | 56 ++++++++++++++++++++++
 4 files changed, 81 insertions(+), 5 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Length.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Length.java
index 0c1ac72..390ef3e 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Length.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/Length.java
@@ -31,11 +31,26 @@ public class Length extends ByteValue {
         assertLength(NUM_BYTES);
     }
 
-    public static Length of(int length) {
-        return new Length(ByteBuffer.allocate(NUM_BYTES).putInt(length).array());
+    public static Length of(long length) {
+        checkUnsignedBounds(length, NUM_BYTES);
+        return new Length(ByteBuffer.allocate(NUM_BYTES)
+            .put((byte) (length >> 24 & 0xff))
+            .put((byte) (length >> 16 & 0xff))
+            .put((byte) (length >> 8 & 0xff))
+            .put((byte) (length & 0xff))
+            .array());
+    }
+
+    public static Length of(String length) {
+        return of(Long.parseLong(length));
     }
 
     public static Length of(byte... values) {
         return new Length(values);
     }
+
+    @Override
+    public String toString() {
+        return "" + (getBytes()[0] << 24 | getBytes()[1] << 16 | getBytes()[2] << 8 | getBytes()[3]);
+    }
 }
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
index 64b30d5..a0439bb 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/generic/types/AMSPort.java
@@ -39,9 +39,7 @@ public class AMSPort extends ByteValue {
     }
 
     public static AMSPort of(int port) {
-        if (port < 0 || port > 65535) {
-            throw new IllegalArgumentException("Value must between 0 and 65535");
-        }
+        checkUnsignedBounds(port, NUM_BYTES);
         return new AMSPort(ByteBuffer.allocate(NUM_BYTES)
             .put((byte) (port >> 8 & 0xff))
             .put((byte) (port & 0xff))
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/ByteValue.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/ByteValue.java
index 13b5ebe..c45c79b 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/ByteValue.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/util/ByteValue.java
@@ -38,6 +38,13 @@ public class ByteValue implements ByteReadable {
         }
     }
 
+    protected static void checkUnsignedBounds(long value, int numberOfBytes) {
+        double upperBound = Math.pow(2, 8 * numberOfBytes);
+        if (value < 0 || value >= upperBound) {
+            throw new IllegalArgumentException("Value must between 0 and " + upperBound + ". Was " + value);
+        }
+    }
+
     @Override
     public byte[] getBytes() {
         return value;
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/LengthTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/LengthTest.java
new file mode 100644
index 0000000..26ca476
--- /dev/null
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/api/commands/types/LengthTest.java
@@ -0,0 +1,56 @@
+/*
+ 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.ads.api.commands.types;
+
+import org.apache.commons.codec.binary.Hex;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class LengthTest {
+
+    byte NULL_BYTE = 0x0;
+
+    @Test
+    void ofBytes() {
+        Assertions.assertEquals("0", Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE).toString());
+        Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE, NULL_BYTE));
+    }
+
+    @Test
+    void ofLong() {
+        assertByte(Length.of(1), "0x00000001");
+        assertByte(Length.of(65535), "0x0000ffff");
+        Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(-1));
+        Assertions.assertThrows(IllegalArgumentException.class, () -> Length.of(Long.valueOf("4294967296")));
+    }
+
+    @Test
+    void ofString() {
+        assertByte(Length.of("1"), "0x00000001");
+    }
+
+    @Test
+    void testToString() {
+        Assertions.assertEquals(Length.of("1").toString(), "1");
+    }
+
+    void assertByte(Length actual, String expected) {
+        Assertions.assertEquals(expected, "0x" + Hex.encodeHexString(actual.getBytes()));
+    }
+}
\ No newline at end of file

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