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

sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 19de005e57c Fix wrong mask result when config same value of from-x and 
to-y with KEEP_FROM_X_TO_Y (#25619)
19de005e57c is described below

commit 19de005e57ce0c9be35e77d21a5252c40ed93b18
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Fri May 12 19:55:44 2023 +0800

    Fix wrong mask result when config same value of from-x and to-y with 
KEEP_FROM_X_TO_Y (#25619)
---
 .../algorithm/cover/KeepFromXToYMaskAlgorithm.java |  8 ++---
 .../algorithm/cover/MaskFromXToYMaskAlgorithm.java |  5 ++-
 .../cover/KeepFirstNLastMMaskAlgorithmTest.java    | 42 ++++++++++++++++++++--
 .../cover/KeepFromXToYMaskAlgorithmTest.java       | 37 +++++++++++++++----
 .../cover/MaskFirstNLastMMaskAlgorithmTest.java    | 38 +++++++++++++++++++-
 .../cover/MaskFromXToYMaskAlgorithmTest.java       | 33 ++++++++++++++---
 6 files changed, 144 insertions(+), 19 deletions(-)

diff --git 
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
index 93a153ba6cf..d4e936e5d73 100644
--- 
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
+++ 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
@@ -18,7 +18,9 @@
 package org.apache.shardingsphere.mask.algorithm.cover;
 
 import com.google.common.base.Strings;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmPropsChecker;
+import 
org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Properties;
@@ -45,6 +47,7 @@ public final class KeepFromXToYMaskAlgorithm implements 
MaskAlgorithm<Object, St
         fromX = createFromX(props);
         toY = createToY(props);
         replaceChar = createReplaceChar(props);
+        ShardingSpherePreconditions.checkState(fromX <= toY, () -> new 
MaskAlgorithmInitializationException(getType(), "fromX must be less than or 
equal to toY"));
     }
     
     private Integer createFromX(final Properties props) {
@@ -68,11 +71,8 @@ public final class KeepFromXToYMaskAlgorithm implements 
MaskAlgorithm<Object, St
         if (Strings.isNullOrEmpty(result)) {
             return result;
         }
-        if (result.length() <= fromX || toY <= fromX) {
-            return result;
-        }
         char[] chars = result.toCharArray();
-        for (int i = 0; i < fromX; i++) {
+        for (int i = 0; i < Math.min(fromX, result.length()); i++) {
             chars[i] = replaceChar;
         }
         for (int i = toY + 1; i < chars.length; i++) {
diff --git 
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
index d69ca7c1bf4..8b44d910dcd 100644
--- 
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
+++ 
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
@@ -18,7 +18,9 @@
 package org.apache.shardingsphere.mask.algorithm.cover;
 
 import com.google.common.base.Strings;
+import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmPropsChecker;
+import 
org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Properties;
@@ -45,6 +47,7 @@ public final class MaskFromXToYMaskAlgorithm implements 
MaskAlgorithm<Object, St
         fromX = createFromX(props);
         toY = createToY(props);
         replaceChar = createReplaceChar(props);
+        ShardingSpherePreconditions.checkState(fromX <= toY, () -> new 
MaskAlgorithmInitializationException(getType(), "fromX must be less than or 
equal to toY"));
     }
     
     private Integer createFromX(final Properties props) {
@@ -68,7 +71,7 @@ public final class MaskFromXToYMaskAlgorithm implements 
MaskAlgorithm<Object, St
         if (Strings.isNullOrEmpty(result)) {
             return result;
         }
-        if (result.length() <= fromX || toY < fromX) {
+        if (result.length() <= fromX) {
             return result;
         }
         char[] chars = result.toCharArray();
diff --git 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithmTest.java
 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithmTest.java
index 7dfc0b2d52b..8210aadeb0c 100644
--- 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithmTest.java
+++ 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithmTest.java
@@ -31,20 +31,56 @@ class KeepFirstNLastMMaskAlgorithmTest {
     
     private KeepFirstNLastMMaskAlgorithm maskAlgorithm;
     
+    private KeepFirstNLastMMaskAlgorithm sameFirstNLastMMaskAlgorithm;
+    
     @BeforeEach
     void setUp() {
         maskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
-        maskAlgorithm.init(PropertiesBuilder.build(new Property("first-n", 
"2"), new Property("last-m", "5"), new Property("replace-char", "*")));
+        maskAlgorithm.init(PropertiesBuilder.build(new Property("first-n", 
"3"), new Property("last-m", "5"), new Property("replace-char", "*")));
+        sameFirstNLastMMaskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
+        sameFirstNLastMMaskAlgorithm.init(PropertiesBuilder.build(new 
Property("first-n", "5"), new Property("last-m", "5"), new 
Property("replace-char", "*")));
     }
     
     @Test
     void assertMask() {
-        assertThat(maskAlgorithm.mask("abc123456"), is("ab**23456"));
+        assertThat(maskAlgorithm.mask("abc123456"), is("abc*23456"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc123456789"), 
is("abc12**56789"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthLessThanFirstN() {
+        assertThat(maskAlgorithm.mask("ab"), is("ab"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc"), is("abc"));
     }
     
     @Test
-    void assertMaskWhenPlainValueLengthLessThenFirstNLastMSum() {
+    void assertMaskWhenPlainValueLengthEqualsFirstN() {
         assertThat(maskAlgorithm.mask("abc"), is("abc"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc12"), is("abc12"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthLessThanLastM() {
+        assertThat(maskAlgorithm.mask("abc1"), is("abc1"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc1"), is("abc1"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthEqualsLastM() {
+        assertThat(maskAlgorithm.mask("abc12"), is("abc12"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc12"), is("abc12"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthLessThanFirstNPlusLastM() {
+        assertThat(maskAlgorithm.mask("abc1234"), is("abc1234"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc123456"), 
is("abc123456"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthEqualsFirstNPlusLastM() {
+        assertThat(maskAlgorithm.mask("abc12345"), is("abc12345"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc1234567"), 
is("abc1234567"));
     }
     
     @Test
diff --git 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithmTest.java
 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithmTest.java
index 47e3784fa67..71340210a5c 100644
--- 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithmTest.java
+++ 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithmTest.java
@@ -31,25 +31,44 @@ class KeepFromXToYMaskAlgorithmTest {
     
     private KeepFromXToYMaskAlgorithm maskAlgorithm;
     
+    private KeepFromXToYMaskAlgorithm sameFromXToYMaskAlgorithm;
+    
     @BeforeEach
     void setUp() {
         maskAlgorithm = new KeepFromXToYMaskAlgorithm();
-        maskAlgorithm.init(PropertiesBuilder.build(new Property("from-x", 
"2"), new Property("to-y", "5"), new Property("replace-char", "*")));
+        maskAlgorithm.init(PropertiesBuilder.build(new Property("from-x", 
"3"), new Property("to-y", "5"), new Property("replace-char", "*")));
+        sameFromXToYMaskAlgorithm = new KeepFromXToYMaskAlgorithm();
+        sameFromXToYMaskAlgorithm.init(PropertiesBuilder.build(new 
Property("from-x", "5"), new Property("to-y", "5"), new 
Property("replace-char", "*")));
     }
     
     @Test
     void assertMask() {
-        assertThat(maskAlgorithm.mask("abc123456"), is("**c123***"));
+        assertThat(maskAlgorithm.mask("abc123456"), is("***123***"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc123456"), 
is("*****3***"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthLessThanFromXPlusOne() {
+        assertThat(maskAlgorithm.mask("abc"), is("***"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc"), is("***"));
     }
     
     @Test
-    void assertMaskWhenPlainValueLengthLessThanToY() {
-        assertThat(maskAlgorithm.mask("abc"), is("**c"));
+    void assertMaskWhenPlainValueLengthEqualsFromXPlusOne() {
+        assertThat(maskAlgorithm.mask("abc1"), is("***1"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc123"), is("*****3"));
     }
     
     @Test
-    void assertMaskWhenPlainValueLengthLessThanFromX() {
-        assertThat(maskAlgorithm.mask("a"), is("a"));
+    void assertMaskWhenPlainValueLengthLessThanToYPlusOne() {
+        assertThat(maskAlgorithm.mask("abc12"), is("***12"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc12"), is("*****"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthEqualsToYPlusOne() {
+        assertThat(maskAlgorithm.mask("abc123"), is("***123"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc123"), is("*****3"));
     }
     
     @Test
@@ -69,4 +88,10 @@ class KeepFromXToYMaskAlgorithmTest {
         assertThrows(MaskAlgorithmInitializationException.class,
                 () -> new 
KeepFirstNLastMMaskAlgorithm().init(PropertiesBuilder.build(new 
Property("from-x", "2"), new Property("to-y", "5"), new 
Property("replace-char", ""))));
     }
+    
+    @Test
+    void assertInitWhenFromXGreaterThanToY() {
+        assertThrows(MaskAlgorithmInitializationException.class,
+                () -> new 
KeepFirstNLastMMaskAlgorithm().init(PropertiesBuilder.build(new 
Property("from-x", "5"), new Property("to-y", "2"), new 
Property("replace-char", ""))));
+    }
 }
diff --git 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
index 085d1e33f53..6b0ecd4afe7 100644
--- 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
+++ 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
@@ -31,20 +31,56 @@ class MaskFirstNLastMMaskAlgorithmTest {
     
     private MaskFirstNLastMMaskAlgorithm maskAlgorithm;
     
+    private MaskFirstNLastMMaskAlgorithm sameFirstNLastMMaskAlgorithm;
+    
     @BeforeEach
     void setUp() {
         maskAlgorithm = new MaskFirstNLastMMaskAlgorithm();
         maskAlgorithm.init(PropertiesBuilder.build(new Property("first-n", 
"3"), new Property("last-m", "5"), new Property("replace-char", "*")));
+        sameFirstNLastMMaskAlgorithm = new MaskFirstNLastMMaskAlgorithm();
+        sameFirstNLastMMaskAlgorithm.init(PropertiesBuilder.build(new 
Property("first-n", "5"), new Property("last-m", "5"), new 
Property("replace-char", "*")));
     }
     
     @Test
     void assertMask() {
-        assertThat(maskAlgorithm.mask("abc12345678"), is("***123*****"));
+        assertThat(maskAlgorithm.mask("abc123456"), is("***1*****"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc123456789"), 
is("*****34*****"));
     }
     
     @Test
     void assertMaskWhenPlainValueLengthLessThanFirstN() {
         assertThat(maskAlgorithm.mask("ab"), is("**"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc"), is("***"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthEqualsFirstN() {
+        assertThat(maskAlgorithm.mask("abc"), is("***"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc12"), is("*****"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthLessThanLastM() {
+        assertThat(maskAlgorithm.mask("abc1"), is("****"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc1"), is("****"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthEqualsLastM() {
+        assertThat(maskAlgorithm.mask("abc12"), is("*****"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc12"), is("*****"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthLessThanFirstNPlusLastM() {
+        assertThat(maskAlgorithm.mask("abc1234"), is("*******"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc123456"), 
is("*********"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthEqualsFirstNPlusLastM() {
+        assertThat(maskAlgorithm.mask("abc12345"), is("********"));
+        assertThat(sameFirstNLastMMaskAlgorithm.mask("abc1234567"), 
is("**********"));
     }
     
     @Test
diff --git 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
index db01850e052..7eff89ceb16 100644
--- 
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
+++ 
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
@@ -31,25 +31,44 @@ class MaskFromXToYMaskAlgorithmTest {
     
     private MaskFromXToYMaskAlgorithm maskAlgorithm;
     
+    private MaskFromXToYMaskAlgorithm sameFromXToYMaskAlgorithm;
+    
     @BeforeEach
     void setUp() {
         maskAlgorithm = new MaskFromXToYMaskAlgorithm();
         maskAlgorithm.init(PropertiesBuilder.build(new Property("from-x", 
"3"), new Property("to-y", "5"), new Property("replace-char", "*")));
+        sameFromXToYMaskAlgorithm = new MaskFromXToYMaskAlgorithm();
+        sameFromXToYMaskAlgorithm.init(PropertiesBuilder.build(new 
Property("from-x", "5"), new Property("to-y", "5"), new 
Property("replace-char", "*")));
     }
     
     @Test
     void assertMask() {
-        assertThat(maskAlgorithm.mask("abc12345"), is("abc***45"));
+        assertThat(maskAlgorithm.mask("abc123456"), is("abc***456"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc123456"), 
is("abc12*456"));
     }
     
     @Test
-    void assertMaskWhenPlainValueLengthLessThanFromX() {
-        assertThat(maskAlgorithm.mask("ab"), is("ab"));
+    void assertMaskWhenPlainValueLengthLessThanFromXPlusOne() {
+        assertThat(maskAlgorithm.mask("abc"), is("abc"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc"), is("abc"));
     }
     
     @Test
-    void assertMaskWhenPlainValueLengthLessThanToY() {
+    void assertMaskWhenPlainValueLengthEqualsFromXPlusOne() {
         assertThat(maskAlgorithm.mask("abc1"), is("abc*"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc123"), is("abc12*"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthLessThanToYPlusOne() {
+        assertThat(maskAlgorithm.mask("abc12"), is("abc**"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc12"), is("abc12"));
+    }
+    
+    @Test
+    void assertMaskWhenPlainValueLengthEqualsToYPlusOne() {
+        assertThat(maskAlgorithm.mask("abc123"), is("abc***"));
+        assertThat(sameFromXToYMaskAlgorithm.mask("abc123"), is("abc12*"));
     }
     
     @Test
@@ -69,4 +88,10 @@ class MaskFromXToYMaskAlgorithmTest {
         assertThrows(MaskAlgorithmInitializationException.class,
                 () -> new 
MaskFromXToYMaskAlgorithm().init(PropertiesBuilder.build(new Property("from-x", 
"3"), new Property("to-y", "5"), new Property("replace-char", ""))));
     }
+    
+    @Test
+    void assertInitWhenFromXGreaterThanToY() {
+        assertThrows(MaskAlgorithmInitializationException.class,
+                () -> new 
KeepFirstNLastMMaskAlgorithm().init(PropertiesBuilder.build(new 
Property("from-x", "5"), new Property("to-y", "2"), new 
Property("replace-char", ""))));
+    }
 }

Reply via email to