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 2026/08/19 20:45:23 UTC

(plc4x) 04/07: fix(plc4j): replay driver testsuite messages in the declared byte order

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

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

commit 32caefd826db8b86ac16e9ff10be07350c48342c
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Wed Aug 19 21:38:43 2026 +0200

    fix(plc4j): replay driver testsuite messages in the declared byte order
    
    Two independent bugs in the driver-testsuite replay path, both of which made
    little-endian testsuites unusable and are the real reason EIPDriverIT was
    disabled (its comment blamed an XML array-wrapper problem that does not
    exist).
    
    IncomingPlcMessageHandler took a byteOrder parameter, documented it, and then
    never passed it to the WriteBufferByteBased - while OutgoingPlcMessageHandler
    did pass it to its read buffer. Every injected message therefore went out
    big-endian, so a little-endian driver read the encapsulation length
    byte-swapped (0x0019 as 0x1900) and waited forever for a packet that never
    came.
    
    ReadBufferXmlBased did not mirror the writer's list-item naming.
    FieldWriterArray.writeSimpleTypeArrayField serializes each simple list item
    as <value>, discarding the field's own name, so reading a list back failed
    with "unexpected element 'value'. Expected 'classId'". Applied to scalar
    reads only, leaving byte arrays (written as one element) and complex list
    items (which push their own type name) alone.
---
 .../spi/buffers/xmlbased/ReadBufferXmlBased.java   | 46 +++++++++++++++-------
 .../handlers/IncomingPlcMessageHandler.java        |  4 +-
 2 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/plc4j/spi/buffers/xml/src/main/java/org/apache/plc4x/java/spi/buffers/xmlbased/ReadBufferXmlBased.java b/plc4j/spi/buffers/xml/src/main/java/org/apache/plc4x/java/spi/buffers/xmlbased/ReadBufferXmlBased.java
index 6876646590..0c116cd9b5 100644
--- a/plc4j/spi/buffers/xml/src/main/java/org/apache/plc4x/java/spi/buffers/xmlbased/ReadBufferXmlBased.java
+++ b/plc4j/spi/buffers/xml/src/main/java/org/apache/plc4x/java/spi/buffers/xmlbased/ReadBufferXmlBased.java
@@ -108,35 +108,35 @@ public class ReadBufferXmlBased extends AbstractBuffer implements ReadBuffer, Xm
 
     @Override
     public byte readUnsignedByte(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Byte.parseByte(decode(name, rwUintKey, numBits));
     }
 
     @Override
     public short readUnsignedShort(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Short.parseShort(decode(name, rwUintKey, numBits));
     }
 
     @Override
     public int readUnsignedInt(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Integer.parseInt(decode(name, rwUintKey, numBits));
     }
 
     @Override
     public long readUnsignedLong(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Long.parseLong(decode(name, rwUintKey, numBits));
     }
 
     @Override
     public BigInteger readUnsignedBigInteger(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         String bigIntString = decode(name, rwUintKey, numBits);
         return new BigInteger(bigIntString);
@@ -144,63 +144,63 @@ public class ReadBufferXmlBased extends AbstractBuffer implements ReadBuffer, Xm
 
     @Override
     public byte readSignedByte(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Byte.parseByte(decode(name, rwIntKey, numBits));
     }
 
     @Override
     public short readSignedShort(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Short.parseShort(decode(name, rwIntKey, numBits));
     }
 
     @Override
     public int readSignedInt(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Integer.parseInt(decode(name, rwIntKey, numBits));
     }
 
     @Override
     public long readSignedLong(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Long.parseLong(decode(name, rwIntKey, numBits));
     }
 
     @Override
     public BigInteger readSignedBigInteger(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return new BigInteger(decode(name, rwIntKey, numBits));
     }
 
     @Override
     public float readFloat(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Float.parseFloat(decode(name, rwFloatKey, numBits));
     }
 
     @Override
     public double readDouble(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return Double.parseDouble(decode(name, rwFloatKey, numBits));
     }
 
     @Override
     public BigDecimal readBigDecimal(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return new BigDecimal(decode(name, rwFloatKey, numBits));
     }
 
     @Override
     public String readString(int numBits, WithOption... options) throws BufferException {
-        String name = getName(options);
+        String name = getScalarName(options);
         move(numBits);
         return decode(name, rwStringKey, numBits);
     }
@@ -328,6 +328,24 @@ public class ReadBufferXmlBased extends AbstractBuffer implements ReadBuffer, Xm
         }
     }
 
+    /**
+     * Resolves the element name for a scalar read.
+     *
+     * <p>Mirrors {@code FieldWriterArray.writeSimpleTypeArrayField}, which serializes every simple
+     * list item as {@code <value>} and discards the field's own name. Reads carrying the
+     * render-as-list marker are exactly those items, so without this a list serialized as
+     * {@code <classId isList="true"><value/>...} fails to read back with
+     * "unexpected element 'value'. Expected 'classId'". Only scalar reads go through here:
+     * byte arrays are written as a single element, and complex list items push their own type
+     * name as context.
+     */
+    private String getScalarName(WithOption... options) throws BufferException {
+        if (WithOption.extractRenderAsList(options).orElse(false)) {
+            return "value";
+        }
+        return getName(options);
+    }
+
     protected String getName(WithOption... options) throws BufferException {
         Optional<String> name = WithOption.extractName(options, getContext());
         if (name.isEmpty()) {
diff --git a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/java/utils/testutils/driver/internal/handlers/IncomingPlcMessageHandler.java b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/java/utils/testutils/driver/internal/handlers/IncomingPlcMessageHandler.java
index 099c62e08a..ea9a62dba4 100644
--- a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/java/utils/testutils/driver/internal/handlers/IncomingPlcMessageHandler.java
+++ b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/java/utils/testutils/driver/internal/handlers/IncomingPlcMessageHandler.java
@@ -22,6 +22,7 @@ import org.apache.plc4x.java.spi.buffers.api.Message;
 import org.apache.plc4x.java.spi.buffers.api.ReadBuffer;
 import org.apache.plc4x.java.spi.buffers.api.WithOption;
 import org.apache.plc4x.java.spi.buffers.api.exceptions.BufferException;
+import org.apache.plc4x.java.spi.buffers.bytebased.WithByteBasedOption;
 import org.apache.plc4x.java.spi.buffers.bytebased.WriteBufferByteBased;
 import org.apache.plc4x.java.spi.buffers.xmlbased.ReadBufferXmlBased;
 import org.apache.plc4x.java.spi.transports.api.TransportInstance;
@@ -136,7 +137,8 @@ public class IncomingPlcMessageHandler {
                 WriteBufferByteBased writeBuffer = new WriteBufferByteBased(new byte[message.getLengthInBytes()],
                     WithOption.WithUnsignedIntegerEncoding("unsigned-binary"),
                     WithOption.WithSignedIntegerEncoding("twos-complement"),
-                    WithOption.WithFloatEncoding("IEEE754"));
+                    WithOption.WithFloatEncoding("IEEE754"),
+                    WithByteBasedOption.WithByteOrder(byteOrder));
                 writeBuffer.writeMessage(message);
                 byte[] bytes = writeBuffer.getBytes();
                 LOGGER.info("Sending serialized message: {}", StaticHelper.ENCODE_HEX(bytes));