This is an automated email from the ASF dual-hosted git repository.
panjuan 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 441250e470c Refactor test cases for encrypt (#17310)
441250e470c is described below
commit 441250e470c7d2b16bac60af446e1e239a1f3ef6
Author: Liang Zhang <[email protected]>
AuthorDate: Wed May 4 14:07:31 2022 +0800
Refactor test cases for encrypt (#17310)
---
.../encrypt/algorithm/SM4EncryptAlgorithm.java | 33 +++----
.../encrypt/algorithm/AESEncryptAlgorithmTest.java | 24 +++--
.../encrypt/algorithm/MD5EncryptAlgorithmTest.java | 8 --
.../encrypt/algorithm/RC4EncryptAlgorithmTest.java | 26 +++---
.../encrypt/algorithm/SM3EncryptAlgorithmTest.java | 27 +++---
.../algorithm/SM4EncryptAlgorithmCBCTest.java | 89 ------------------
.../algorithm/SM4EncryptAlgorithmECBTest.java | 86 ------------------
.../encrypt/algorithm/SM4EncryptAlgorithmTest.java | 100 +++++++++++++++++++++
.../ColumnRegexMatchShadowAlgorithmTest.java | 15 ++--
.../query/ShadowTableRuleQueryResultSetTest.java | 10 ++-
...erShardingKeyGeneratorStatementUpdaterTest.java | 27 +++---
.../updatable/SetVariableBackendHandlerTest.java | 37 ++++----
12 files changed, 196 insertions(+), 286 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
index 39b6891f345..c426fb6ac9b 100644
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithm.java
@@ -46,8 +46,6 @@ public final class SM4EncryptAlgorithm implements
EncryptAlgorithm<Object, Strin
Security.addProvider(new BouncyCastleProvider());
}
- private static final String SM4 = "SM4";
-
private static final String SM4_KEY = "sm4-key";
private static final String SM4_IV = "sm4-iv";
@@ -92,21 +90,20 @@ public final class SM4EncryptAlgorithm implements
EncryptAlgorithm<Object, Strin
private byte[] createSm4Key(final Properties props) {
Preconditions.checkArgument(props.containsKey(SM4_KEY), "%s can not be
null.", SM4_KEY);
- String sm4KeyValue = String.valueOf(props.getProperty(SM4_KEY));
- byte[] sm4KeyBytes = ByteUtils.fromHexString(sm4KeyValue);
- Preconditions.checkState(KEY_LENGTH == sm4KeyBytes.length, "Key length
must be " + KEY_LENGTH + " bytes long.");
- return sm4KeyBytes;
+ byte[] result =
ByteUtils.fromHexString(String.valueOf(props.getProperty(SM4_KEY)));
+ Preconditions.checkState(KEY_LENGTH == result.length, "Key length must
be " + KEY_LENGTH + " bytes long.");
+ return result;
}
private byte[] createSm4Iv(final Properties props, final String sm4Mode) {
- if ("CBC".equalsIgnoreCase(sm4Mode)) {
- Preconditions.checkArgument(props.containsKey(SM4_IV), "%s can not
be null.", SM4_IV);
- String sm4IvValue = String.valueOf(props.getProperty(SM4_IV));
- byte[] sm4IvBytes = ByteUtils.fromHexString(sm4IvValue);
- Preconditions.checkState(IV_LENGTH == sm4IvBytes.length, "Iv
length must be " + IV_LENGTH + " bytes long.");
- return sm4IvBytes;
+ if (!"CBC".equalsIgnoreCase(sm4Mode)) {
+ return null;
}
- return null;
+ Preconditions.checkArgument(props.containsKey(SM4_IV), "%s can not be
null.", SM4_IV);
+ String sm4IvValue = String.valueOf(props.getProperty(SM4_IV));
+ byte[] result = ByteUtils.fromHexString(sm4IvValue);
+ Preconditions.checkState(IV_LENGTH == result.length, "Iv length must
be " + IV_LENGTH + " bytes long.");
+ return result;
}
private String createSm4Padding(final Properties props) {
@@ -138,8 +135,8 @@ public final class SM4EncryptAlgorithm implements
EncryptAlgorithm<Object, Strin
@SneakyThrows
private byte[] handle(final byte[] input, final int mode) {
Cipher cipher = Cipher.getInstance(sm4ModePadding,
BouncyCastleProvider.PROVIDER_NAME);
- SecretKeySpec secretKeySpec = new SecretKeySpec(sm4Key, SM4);
- Optional<byte[]> sm4Iv = getSm4Iv();
+ SecretKeySpec secretKeySpec = new SecretKeySpec(sm4Key, "SM4");
+ Optional<byte[]> sm4Iv = Optional.ofNullable(this.sm4Iv);
if (sm4Iv.isPresent()) {
cipher.init(mode, secretKeySpec, new IvParameterSpec(sm4Iv.get()));
} else {
@@ -148,12 +145,8 @@ public final class SM4EncryptAlgorithm implements
EncryptAlgorithm<Object, Strin
return cipher.doFinal(input);
}
- private Optional<byte[]> getSm4Iv() {
- return Optional.ofNullable(sm4Iv);
- }
-
@Override
public String getType() {
- return SM4;
+ return "SM4";
}
}
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithmTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithmTest.java
index d13c95f526e..7921a70925b 100644
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithmTest.java
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithmTest.java
@@ -37,9 +37,13 @@ public final class AESEncryptAlgorithmTest {
@Before
public void setUp() {
- Properties props = new Properties();
- props.setProperty("aes-key-value", "test");
- encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("AES", props));
+ encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("AES", createProperties()));
+ }
+
+ private Properties createProperties() {
+ Properties result = new Properties();
+ result.setProperty("aes-key-value", "test");
+ return result;
}
@Test
@@ -50,17 +54,14 @@ public final class AESEncryptAlgorithmTest {
@Test(expected = IllegalArgumentException.class)
public void assertEncryptWithoutKey() {
- Properties props = new Properties();
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
+ encryptAlgorithm.init(new Properties());
Object actual = encryptAlgorithm.encrypt("test",
mock(EncryptContext.class));
assertThat(actual, is("dSpPiyENQGDUXMKFMJPGWA=="));
}
@Test
public void assertEncryptWithNullPlaintext() {
- Object actual = encryptAlgorithm.encrypt(null,
mock(EncryptContext.class));
- assertNull(actual);
+ assertNull(encryptAlgorithm.encrypt(null, mock(EncryptContext.class)));
}
@Test
@@ -71,16 +72,13 @@ public final class AESEncryptAlgorithmTest {
@Test(expected = IllegalArgumentException.class)
public void assertDecryptWithoutKey() {
- Properties props = new Properties();
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
+ encryptAlgorithm.init(new Properties());
Object actual = encryptAlgorithm.decrypt("dSpPiyENQGDUXMKFMJPGWA==",
mock(EncryptContext.class));
assertThat(actual.toString(), is("test"));
}
@Test
public void assertDecryptWithNullCiphertext() {
- Object actual = encryptAlgorithm.decrypt(null,
mock(EncryptContext.class));
- assertNull(actual);
+ assertNull(encryptAlgorithm.decrypt(null, mock(EncryptContext.class)));
}
}
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/MD5EncryptAlgorithmTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/MD5EncryptAlgorithmTest.java
index 7aad550f876..191a51bdaa0 100644
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/MD5EncryptAlgorithmTest.java
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/MD5EncryptAlgorithmTest.java
@@ -54,12 +54,4 @@ public final class MD5EncryptAlgorithmTest {
public void assertDecode() {
assertThat(encryptAlgorithm.decrypt("test",
mock(EncryptContext.class)).toString(), is("test"));
}
-
- @Test
- public void assertProps() {
- Properties props = new Properties();
- props.setProperty("key1", "value1");
- encryptAlgorithm.setProps(props);
- assertThat(encryptAlgorithm.getProps().getProperty("key1"),
is("value1"));
- }
}
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithmTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithmTest.java
index 391eaf12fb4..f18f1444671 100644
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithmTest.java
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithmTest.java
@@ -38,9 +38,13 @@ public final class RC4EncryptAlgorithmTest {
@Before
public void setUp() {
- Properties props = new Properties();
- props.setProperty("rc4-key-value", "test-sharding");
- encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("Rc4", props));
+ encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("Rc4", createProperties()));
+ }
+
+ private Properties createProperties() {
+ Properties result = new Properties();
+ result.setProperty("rc4-key-value", "test-sharding");
+ return result;
}
@Test
@@ -55,14 +59,17 @@ public final class RC4EncryptAlgorithmTest {
@Test(expected = ShardingSphereException.class)
public void assertKeyIsToLong() {
- Properties props = new Properties();
+ encryptAlgorithm.init(createInvalidProperties());
+ }
+
+ private Properties createInvalidProperties() {
+ Properties result = new Properties();
StringBuilder keyBuffer = new StringBuilder();
for (int i = 0; i < 100; i++) {
keyBuffer.append("test");
}
- props.setProperty("rc4-key-value", keyBuffer.toString());
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
+ result.setProperty("rc4-key-value", keyBuffer.toString());
+ return result;
}
@Test
@@ -74,9 +81,4 @@ public final class RC4EncryptAlgorithmTest {
public void assertDecryptWithNullCiphertext() {
assertNull(encryptAlgorithm.decrypt(null, mock(EncryptContext.class)));
}
-
- @Test
- public void assertGetProperties() {
- assertThat(encryptAlgorithm.getProps().getProperty("rc4-key-value"),
is("test-sharding"));
- }
}
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM3EncryptAlgorithmTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM3EncryptAlgorithmTest.java
index af7a1987b70..ee139ee9a12 100644
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM3EncryptAlgorithmTest.java
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM3EncryptAlgorithmTest.java
@@ -37,9 +37,13 @@ public final class SM3EncryptAlgorithmTest {
@Before
public void setUp() {
- Properties props = new Properties();
- props.setProperty("sm3-salt", "test1234");
- encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM3", props));
+ encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM3", createProperties()));
+ }
+
+ private Properties createProperties() {
+ Properties result = new Properties();
+ result.setProperty("sm3-salt", "test1234");
+ return result;
}
@Test
@@ -50,17 +54,13 @@ public final class SM3EncryptAlgorithmTest {
@Test
public void assertEncryptWithoutSalt() {
- Properties props = new Properties();
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
- Object actual = encryptAlgorithm.encrypt("test1234",
mock(EncryptContext.class));
- assertThat(actual,
is("ab847c6f2f6a53be88808c5221bd6ee0762e1af1def82b21d2061599b6cf5c79"));
+ encryptAlgorithm.init(new Properties());
+ assertThat(encryptAlgorithm.encrypt("test1234",
mock(EncryptContext.class)),
is("ab847c6f2f6a53be88808c5221bd6ee0762e1af1def82b21d2061599b6cf5c79"));
}
@Test
public void assertEncryptWithNullPlaintext() {
- Object actual = encryptAlgorithm.encrypt(null,
mock(EncryptContext.class));
- assertNull(actual);
+ assertNull(encryptAlgorithm.encrypt(null, mock(EncryptContext.class)));
}
@Test
@@ -71,16 +71,13 @@ public final class SM3EncryptAlgorithmTest {
@Test
public void assertDecryptWithoutSalt() {
- Properties props = new Properties();
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
+ encryptAlgorithm.init(new Properties());
Object actual =
encryptAlgorithm.decrypt("ab847c6f2f6a53be88808c5221bd6ee0762e1af1def82b21d2061599b6cf5c79",
mock(EncryptContext.class));
assertThat(actual.toString(),
is("ab847c6f2f6a53be88808c5221bd6ee0762e1af1def82b21d2061599b6cf5c79"));
}
@Test
public void assertDecryptWithNullCiphertext() {
- Object actual = encryptAlgorithm.decrypt(null,
mock(EncryptContext.class));
- assertNull(actual);
+ assertNull(encryptAlgorithm.decrypt(null, mock(EncryptContext.class)));
}
}
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmCBCTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmCBCTest.java
deleted file mode 100644
index 2d164f5198b..00000000000
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmCBCTest.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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.shardingsphere.encrypt.algorithm;
-
-import org.apache.shardingsphere.encrypt.factory.EncryptAlgorithmFactory;
-import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
-import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
-import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-
-public final class SM4EncryptAlgorithmCBCTest {
-
- private EncryptAlgorithm<Object, String> encryptAlgorithm;
-
- @Before
- public void setUp() {
- Properties props = new Properties();
- props.setProperty("sm4-key", "f201326119911788cFd30575b81059ac");
- props.setProperty("sm4-iv", "e166c3391294E69cc4c620f594fe00d7");
- props.setProperty("sm4-mode", "CBC");
- props.setProperty("sm4-padding", "PKCS7Padding");
- encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", props));
- }
-
- @Test
- public void assertEncrypt() {
- assertThat(encryptAlgorithm.encrypt("test",
mock(EncryptContext.class)), is("dca2127b57ba8cac36a0914e0208dc11"));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void assertEncryptWithoutKey() {
- Properties props = new Properties();
- props.setProperty("sm4-mode", "CBC");
- props.setProperty("sm4-iv", "e166c3391294E69cc4c620f594fe00d7");
- props.setProperty("sm4-padding", "PKCS7Padding");
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
- assertThat(encryptAlgorithm.encrypt("test",
mock(EncryptContext.class)), is("028654f2ca4f575dee9e1faae85dadde"));
- }
-
- @Test
- public void assertEncryptWithNullPlaintext() {
- assertNull(encryptAlgorithm.encrypt(null, mock(EncryptContext.class)));
- }
-
- @Test
- public void assertDecrypt() {
-
assertThat(encryptAlgorithm.decrypt("dca2127b57ba8cac36a0914e0208dc11",
mock(EncryptContext.class)).toString(), is("test"));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void assertDecryptWithoutKey() {
- Properties props = new Properties();
- props.setProperty("sm4-mode", "CBC");
- props.setProperty("sm4-iv", "e166c3391294E69cc4c620f594fe00d7");
- props.setProperty("sm4-padding", "PKCS7Padding");
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
-
assertThat(encryptAlgorithm.decrypt("028654f2ca4f575dee9e1faae85dadde",
mock(EncryptContext.class)).toString(), is("test"));
- }
-
- @Test
- public void assertDecryptWithNullCiphertext() {
- assertNull(encryptAlgorithm.decrypt(null, mock(EncryptContext.class)));
- }
-}
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmECBTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmECBTest.java
deleted file mode 100644
index 4d13e6cdd96..00000000000
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmECBTest.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * 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.shardingsphere.encrypt.algorithm;
-
-import org.apache.shardingsphere.encrypt.factory.EncryptAlgorithmFactory;
-import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
-import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
-import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-
-public final class SM4EncryptAlgorithmECBTest {
-
- private EncryptAlgorithm<Object, String> encryptAlgorithm;
-
- @Before
- public void setUp() {
- Properties props = new Properties();
- props.setProperty("sm4-key", "4D744E003D713D054E7E407C350E447E");
- props.setProperty("sm4-mode", "ECB");
- props.setProperty("sm4-padding", "PKCS5Padding");
- encryptAlgorithm = EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", props));
- }
-
- @Test
- public void assertEncrypt() {
- assertThat(encryptAlgorithm.encrypt("test",
mock(EncryptContext.class)), is("028654f2ca4f575dee9e1faae85dadde"));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void assertEncryptWithoutKey() {
- Properties props = new Properties();
- props.setProperty("sm4-mode", "ECB");
- props.setProperty("sm4-padding", "PKCS5Padding");
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
- assertThat(encryptAlgorithm.encrypt("test",
mock(EncryptContext.class)), is("028654f2ca4f575dee9e1faae85dadde"));
- }
-
- @Test
- public void assertEncryptWithNullPlaintext() {
- assertNull(encryptAlgorithm.encrypt(null, mock(EncryptContext.class)));
- }
-
- @Test
- public void assertDecrypt() {
-
assertThat(encryptAlgorithm.decrypt("028654f2ca4f575dee9e1faae85dadde",
mock(EncryptContext.class)).toString(), is("test"));
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void assertDecryptWithoutKey() {
- Properties props = new Properties();
- props.setProperty("sm4-mode", "ECB");
- props.setProperty("sm4-padding", "PKCS5Padding");
- encryptAlgorithm.init(props);
- encryptAlgorithm.setProps(props);
-
assertThat(encryptAlgorithm.decrypt("028654f2ca4f575dee9e1faae85dadde",
mock(EncryptContext.class)).toString(), is("test"));
- }
-
- @Test
- public void assertDecryptWithNullCiphertext() {
- assertNull(encryptAlgorithm.decrypt(null, mock(EncryptContext.class)));
- }
-}
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java
new file mode 100644
index 00000000000..7eb87ba3020
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/SM4EncryptAlgorithmTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.shardingsphere.encrypt.algorithm;
+
+import org.apache.shardingsphere.encrypt.factory.EncryptAlgorithmFactory;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public final class SM4EncryptAlgorithmTest {
+
+ @Test(expected = IllegalArgumentException.class)
+ public void assertInitWithoutKey() {
+ EncryptAlgorithm<Object, String> algorithm =
EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", createECBProperties()));
+ algorithm.init(createInvalidProperties());
+ }
+
+ private Properties createInvalidProperties() {
+ Properties result = new Properties();
+ result.setProperty("sm4-mode", "ECB");
+ result.setProperty("sm4-padding", "PKCS5Padding");
+ return result;
+ }
+
+ @Test
+ public void assertEncryptNullPlaintext() {
+ EncryptAlgorithm<Object, String> algorithm =
EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", createECBProperties()));
+ assertNull(algorithm.encrypt(null, mock(EncryptContext.class)));
+ }
+
+ @Test
+ public void assertEncryptWithECBMode() {
+ EncryptAlgorithm<Object, String> algorithm =
EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", createECBProperties()));
+ assertThat(algorithm.encrypt("test", mock(EncryptContext.class)),
is("028654f2ca4f575dee9e1faae85dadde"));
+ }
+
+ @Test
+ public void assertDecryptNullCiphertext() {
+ EncryptAlgorithm<Object, String> algorithm =
EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", createECBProperties()));
+ assertNull(algorithm.decrypt(null, mock(EncryptContext.class)));
+ }
+
+ @Test
+ public void assertDecryptWithECBMode() {
+ EncryptAlgorithm<Object, String> algorithm =
EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", createECBProperties()));
+ assertThat(algorithm.decrypt("028654f2ca4f575dee9e1faae85dadde",
mock(EncryptContext.class)).toString(), is("test"));
+ }
+
+ private Properties createECBProperties() {
+ Properties result = new Properties();
+ result.setProperty("sm4-key", "4D744E003D713D054E7E407C350E447E");
+ result.setProperty("sm4-mode", "ECB");
+ result.setProperty("sm4-padding", "PKCS5Padding");
+ return result;
+ }
+
+ @Test
+ public void assertEncryptWithCBCMode() {
+ EncryptAlgorithm<Object, String> algorithm =
EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", createCBCProperties()));
+ assertThat(algorithm.encrypt("test", mock(EncryptContext.class)),
is("dca2127b57ba8cac36a0914e0208dc11"));
+ }
+
+ @Test
+ public void assertDecrypt() {
+ EncryptAlgorithm<Object, String> algorithm =
EncryptAlgorithmFactory.newInstance(new
ShardingSphereAlgorithmConfiguration("SM4", createCBCProperties()));
+ assertThat(algorithm.decrypt("dca2127b57ba8cac36a0914e0208dc11",
mock(EncryptContext.class)).toString(), is("test"));
+ }
+
+ private Properties createCBCProperties() {
+ Properties result = new Properties();
+ result.setProperty("sm4-key", "f201326119911788cFd30575b81059ac");
+ result.setProperty("sm4-iv", "e166c3391294E69cc4c620f594fe00d7");
+ result.setProperty("sm4-mode", "CBC");
+ result.setProperty("sm4-padding", "PKCS7Padding");
+ return result;
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithmTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithmTest.java
index b4b471afd21..303e370df99 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithmTest.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/column/ColumnRegexMatchShadowAlgorithmTest.java
@@ -32,13 +32,16 @@ public final class ColumnRegexMatchShadowAlgorithmTest
extends AbstractColumnSha
@Before
public void init() {
- Properties props = new Properties();
- props.setProperty("column", SHADOW_COLUMN);
- props.setProperty("operation", "insert");
- props.setProperty("regex", "[1]");
shadowAlgorithm = new ColumnRegexMatchShadowAlgorithm();
- shadowAlgorithm.init(props);
- shadowAlgorithm.setProps(props);
+ shadowAlgorithm.init(createProperties());
+ }
+
+ private Properties createProperties() {
+ Properties result = new Properties();
+ result.setProperty("column", SHADOW_COLUMN);
+ result.setProperty("operation", "insert");
+ result.setProperty("regex", "[1]");
+ return result;
}
@Test
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowTableRuleQueryResultSetTest.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowTableRuleQueryResultSetTest.java
index 63f25ff642a..22b3e1baa5a 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowTableRuleQueryResultSetTest.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-distsql/shardingsphere-shadow-distsql-handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShadowTableRuleQueryResultSetTest.java
@@ -55,10 +55,14 @@ public final class ShadowTableRuleQueryResultSetTest {
private RuleConfiguration createRuleConfiguration() {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
- Properties props = new Properties();
- props.setProperty("foo", "bar");
result.getTables().put("t_order", new
ShadowTableConfiguration(Collections.emptyList(),
Arrays.asList("shadowAlgorithmName_1", "shadowAlgorithmName_2")));
- result.getShadowAlgorithms().put("shadowAlgorithmName", new
ShardingSphereAlgorithmConfiguration("simple_hint", props));
+ result.getShadowAlgorithms().put("shadowAlgorithmName", new
ShardingSphereAlgorithmConfiguration("simple_hint", createProperties()));
+ return result;
+ }
+
+ private Properties createProperties() {
+ Properties result = new Properties();
+ result.setProperty("foo", "bar");
return result;
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingKeyGeneratorStatementUpdaterTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingKeyGeneratorStatementUpdaterTest.java
index e334cb75561..3e1a22ebb3b 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingKeyGeneratorStatementUpdaterTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingKeyGeneratorStatementUpdaterTest.java
@@ -35,6 +35,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Properties;
import static org.mockito.Mockito.when;
@@ -42,11 +43,11 @@ import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class AlterShardingKeyGeneratorStatementUpdaterTest {
+ private final AlterShardingKeyGeneratorStatementUpdater updater = new
AlterShardingKeyGeneratorStatementUpdater();
+
@Mock
private ShardingSphereMetaData shardingSphereMetaData;
- private final AlterShardingKeyGeneratorStatementUpdater updater = new
AlterShardingKeyGeneratorStatementUpdater();
-
@Before
public void before() {
when(shardingSphereMetaData.getDatabaseName()).thenReturn("test");
@@ -54,33 +55,31 @@ public final class
AlterShardingKeyGeneratorStatementUpdaterTest {
@Test(expected = DuplicateRuleException.class)
public void assertExecuteWithDuplicate() throws DistSQLException {
- Properties props = new Properties();
- props.put("inputKey", "inputValue");
- ShardingKeyGeneratorSegment keyGeneratorSegment = new
ShardingKeyGeneratorSegment("inputAlgorithmName", new
AlgorithmSegment("inputAlgorithmName", props));
- updater.checkSQLStatement(shardingSphereMetaData,
createSQLStatement(keyGeneratorSegment, keyGeneratorSegment), null);
+ ShardingKeyGeneratorSegment keyGeneratorSegment = new
ShardingKeyGeneratorSegment("inputAlgorithmName", new
AlgorithmSegment("inputAlgorithmName", createProperties()));
+ updater.checkSQLStatement(shardingSphereMetaData, new
AlterShardingKeyGeneratorStatement(Arrays.asList(keyGeneratorSegment,
keyGeneratorSegment)), null);
}
@Test(expected = RequiredAlgorithmMissedException.class)
public void assertExecuteWithNotExist() throws DistSQLException {
- Properties props = new Properties();
- props.put("inputKey", "inputValue");
+ Properties props = createProperties();
ShardingKeyGeneratorSegment keyGeneratorSegment = new
ShardingKeyGeneratorSegment("notExistAlgorithmName", new
AlgorithmSegment("inputAlgorithmName", props));
ShardingRuleConfiguration ruleConfig = new ShardingRuleConfiguration();
ruleConfig.getShardingAlgorithms().put("existAlgorithmName", new
ShardingSphereAlgorithmConfiguration("hash_mod", props));
- updater.checkSQLStatement(shardingSphereMetaData,
createSQLStatement(keyGeneratorSegment), ruleConfig);
+ updater.checkSQLStatement(shardingSphereMetaData, new
AlterShardingKeyGeneratorStatement(Collections.singletonList(keyGeneratorSegment)),
ruleConfig);
}
@Test(expected = InvalidAlgorithmConfigurationException.class)
public void assertExecuteWithInvalidAlgorithm() throws DistSQLException {
- Properties props = new Properties();
- props.put("inputKey", "inputValue");
+ Properties props = createProperties();
ShardingKeyGeneratorSegment keyGeneratorSegment = new
ShardingKeyGeneratorSegment("existAlgorithmName", new
AlgorithmSegment("inputAlgorithmName", props));
ShardingRuleConfiguration ruleConfig = new ShardingRuleConfiguration();
ruleConfig.getKeyGenerators().put("existAlgorithmName", new
ShardingSphereAlgorithmConfiguration("InvalidAlgorithm", props));
- updater.checkSQLStatement(shardingSphereMetaData,
createSQLStatement(keyGeneratorSegment), ruleConfig);
+ updater.checkSQLStatement(shardingSphereMetaData, new
AlterShardingKeyGeneratorStatement(Collections.singletonList(keyGeneratorSegment)),
ruleConfig);
}
- private AlterShardingKeyGeneratorStatement createSQLStatement(final
ShardingKeyGeneratorSegment... ruleSegments) {
- return new
AlterShardingKeyGeneratorStatement(Arrays.asList(ruleSegments));
+ private Properties createProperties() {
+ Properties result = new Properties();
+ result.put("inputKey", "inputValue");
+ return result;
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/SetVariableBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/SetVariableBackendHandlerTest.java
index 5f3e63c7abf..70491011bb4 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/SetVariableBackendHandlerTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/SetVariableBackendHandlerTest.java
@@ -68,35 +68,37 @@ public final class SetVariableBackendHandlerTest {
@Before
public void setUp() {
- Properties props = new Properties();
-
props.setProperty(ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE.getKey(),
ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE.getDefaultValue());
- MetaDataContexts metaDataContexts = new
MetaDataContexts(mock(MetaDataPersistService.class), getMetaDataMap(),
- mock(ShardingSphereRuleMetaData.class),
mock(ExecutorEngine.class), mock(OptimizerContext.class), new
ConfigurationProperties(props));
+ MetaDataContexts metaDataContexts = new
MetaDataContexts(mock(MetaDataPersistService.class), createMetaDataMap(),
+ mock(ShardingSphereRuleMetaData.class),
mock(ExecutorEngine.class), mock(OptimizerContext.class), new
ConfigurationProperties(createProperties()));
contextManagerBefore = ProxyContext.getInstance().getContextManager();
ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
- ProxyContext.getInstance().init(contextManager);
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
+ ProxyContext.getInstance().init(contextManager);
connectionSession = new
ConnectionSession(mock(MySQLDatabaseType.class), TransactionType.LOCAL, new
DefaultAttributeMap());
}
- private Map<String, ShardingSphereMetaData> getMetaDataMap() {
+ private Map<String, ShardingSphereMetaData> createMetaDataMap() {
Map<String, ShardingSphereMetaData> result = new HashMap<>(10, 1);
for (int i = 0; i < 10; i++) {
ShardingSphereMetaData metaData =
mock(ShardingSphereMetaData.class);
- ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(metaData.getResource()).thenReturn(new
ShardingSphereResource(Collections.emptyMap(), null, null, new
MySQLDatabaseType()));
when(metaData.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.emptyList(), Collections.emptyList()));
-
when(metaData.getSchemaByName(DefaultSchema.LOGIC_NAME)).thenReturn(schema);
+
when(metaData.getSchemaByName(DefaultSchema.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class));
result.put(String.format(DATABASE_PATTERN, i), metaData);
}
return result;
}
+ private Properties createProperties() {
+ Properties result = new Properties();
+
result.setProperty(ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE.getKey(),
ConfigurationPropertyKey.PROXY_BACKEND_DRIVER_TYPE.getDefaultValue());
+ return result;
+ }
+
@Test
public void assertSwitchTransactionTypeXA() throws SQLException {
connectionSession.setCurrentDatabase(String.format(DATABASE_PATTERN,
0));
- SetVariableHandler handler = new
SetVariableHandler().init(getParameter(new
SetVariableStatement("transaction_type", "XA"), connectionSession));
- ResponseHeader actual = handler.execute();
+ ResponseHeader actual = new SetVariableHandler().init(getParameter(new
SetVariableStatement("transaction_type", "XA"), connectionSession)).execute();
assertThat(actual, instanceOf(UpdateResponseHeader.class));
assertThat(connectionSession.getTransactionStatus().getTransactionType(),
is(TransactionType.XA));
}
@@ -104,8 +106,7 @@ public final class SetVariableBackendHandlerTest {
@Test
public void assertSwitchTransactionTypeBASE() throws SQLException {
connectionSession.setCurrentDatabase(String.format(DATABASE_PATTERN,
0));
- SetVariableHandler handler = new
SetVariableHandler().init(getParameter(new
SetVariableStatement("transaction_type", "BASE"), connectionSession));
- ResponseHeader actual = handler.execute();
+ ResponseHeader actual = new SetVariableHandler().init(getParameter(new
SetVariableStatement("transaction_type", "BASE"), connectionSession)).execute();
assertThat(actual, instanceOf(UpdateResponseHeader.class));
assertThat(connectionSession.getTransactionStatus().getTransactionType(),
is(TransactionType.BASE));
}
@@ -113,8 +114,7 @@ public final class SetVariableBackendHandlerTest {
@Test
public void assertSwitchTransactionTypeLOCAL() throws SQLException {
connectionSession.setCurrentDatabase(String.format(DATABASE_PATTERN,
0));
- SetVariableHandler handler = new
SetVariableHandler().init(getParameter(new
SetVariableStatement("transaction_type", "LOCAL"), connectionSession));
- ResponseHeader actual = handler.execute();
+ ResponseHeader actual = new SetVariableHandler().init(getParameter(new
SetVariableStatement("transaction_type", "LOCAL"),
connectionSession)).execute();
assertThat(actual, instanceOf(UpdateResponseHeader.class));
assertThat(connectionSession.getTransactionStatus().getTransactionType(),
is(TransactionType.LOCAL));
}
@@ -133,8 +133,7 @@ public final class SetVariableBackendHandlerTest {
@Test
public void assertSetAgentPluginsEnabledTrue() throws SQLException {
connectionSession.setCurrentDatabase(String.format(DATABASE_PATTERN,
0));
- SetVariableHandler handler = new
SetVariableHandler().init(getParameter(new
SetVariableStatement(VariableEnum.AGENT_PLUGINS_ENABLED.name(),
Boolean.TRUE.toString()), null));
- ResponseHeader actual = handler.execute();
+ ResponseHeader actual = new SetVariableHandler().init(getParameter(new
SetVariableStatement(VariableEnum.AGENT_PLUGINS_ENABLED.name(),
Boolean.TRUE.toString()), null)).execute();
assertThat(actual, instanceOf(UpdateResponseHeader.class));
assertThat(SystemPropertyUtil.getSystemProperty(VariableEnum.AGENT_PLUGINS_ENABLED.name(),
Boolean.FALSE.toString()), is(Boolean.TRUE.toString()));
}
@@ -142,8 +141,7 @@ public final class SetVariableBackendHandlerTest {
@Test
public void assertSetAgentPluginsEnabledFalse() throws SQLException {
connectionSession.setCurrentDatabase(String.format(DATABASE_PATTERN,
0));
- SetVariableHandler handler = new
SetVariableHandler().init(getParameter(new
SetVariableStatement(VariableEnum.AGENT_PLUGINS_ENABLED.name(),
Boolean.FALSE.toString()), null));
- ResponseHeader actual = handler.execute();
+ ResponseHeader actual = new SetVariableHandler().init(getParameter(new
SetVariableStatement(VariableEnum.AGENT_PLUGINS_ENABLED.name(),
Boolean.FALSE.toString()), null)).execute();
assertThat(actual, instanceOf(UpdateResponseHeader.class));
assertThat(SystemPropertyUtil.getSystemProperty(VariableEnum.AGENT_PLUGINS_ENABLED.name(),
Boolean.FALSE.toString()), is(Boolean.FALSE.toString()));
}
@@ -151,8 +149,7 @@ public final class SetVariableBackendHandlerTest {
@Test
public void assertSetAgentPluginsEnabledFalseWithUnknownValue() throws
SQLException {
connectionSession.setCurrentDatabase(String.format(DATABASE_PATTERN,
0));
- SetVariableHandler handler = new
SetVariableHandler().init(getParameter(new
SetVariableStatement(VariableEnum.AGENT_PLUGINS_ENABLED.name(), "xxx"),
connectionSession));
- ResponseHeader actual = handler.execute();
+ ResponseHeader actual = new SetVariableHandler().init(getParameter(new
SetVariableStatement(VariableEnum.AGENT_PLUGINS_ENABLED.name(), "xxx"),
connectionSession)).execute();
assertThat(actual, instanceOf(UpdateResponseHeader.class));
assertThat(SystemPropertyUtil.getSystemProperty(VariableEnum.AGENT_PLUGINS_ENABLED.name(),
Boolean.FALSE.toString()), is(Boolean.FALSE.toString()));
}