Fix String length checker 31/95331/5
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 24 Feb 2021 00:11:08 +0000 (01:11 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 24 Feb 2021 16:48:37 +0000 (16:48 +0000)
We are enforcing length restrictions on String.length(), which is
the wrong measure, as it returns the number of code units in the
String. We actually want to compare the number of code points, i.e.
we need to use String.codePointCount().

JIRA: MDSAL-661
Change-Id: I7291aabb67ec9d002cdeb52befd1723707113121
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/LengthGenerator.java
binding/mdsal-binding-test-model/src/main/yang/mdsal661.yang [new file with mode: 0644]
binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal661Test.java [new file with mode: 0644]

index 4684e3dfe541e444e16eadb68aa630907789c1f6..379f5f808acfbd6fb8f53f8810f7c19bc4e2c238 100644 (file)
@@ -97,7 +97,7 @@ final class LengthGenerator {
         sb.append("private static void ").append(lengthCheckerName(member)).append("(final String value) {\n");
 
         if (!expressions.isEmpty()) {
-            sb.append("    final int length = value.length();\n");
+            sb.append("    final int length = value.codePointCount(0, value.length());\n");
 
             for (String exp : expressions) {
                 sb.append("    if (").append(exp).append(") {\n");
diff --git a/binding/mdsal-binding-test-model/src/main/yang/mdsal661.yang b/binding/mdsal-binding-test-model/src/main/yang/mdsal661.yang
new file mode 100644 (file)
index 0000000..2032d86
--- /dev/null
@@ -0,0 +1,10 @@
+module mdsal661 {
+  namespace mdsal661;
+  prefix mdsal661;
+
+  typedef foo {
+    type string {
+      length 1;
+    }
+  }
+}
diff --git a/binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal661Test.java b/binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal661Test.java
new file mode 100644 (file)
index 0000000..1ba4fcd
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.test.model;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.mdsal661.norev.Foo;
+
+public class Mdsal661Test {
+    @Test
+    public void testLengthEnforcerReject() {
+        assertThrows(IllegalArgumentException.class, () -> new Foo(""));
+        assertThrows(IllegalArgumentException.class, () -> new Foo("ab"));
+    }
+
+    @Test
+    public void testLengthEnforcerAccept() {
+        assertNotNull(new Foo("a"));
+        // U+1F31E, encodes to UTF-16 as "\uD83C\uDF1E", i.e. two code units
+        assertNotNull(new Foo("🌞"));
+    }
+}