From a0ffcf02c29e5259f0bdbf5a6f8a5e089c48a751 Mon Sep 17 00:00:00 2001 From: kezhenxu94 Date: Mon, 7 Oct 2019 11:40:54 +0800 Subject: [PATCH] Fix NullPointerException bug (#3176) --- .../shardingsphere/core/rule/EncryptRule.java | 12 +++- .../core/rule/EncryptRuleTest.java | 72 +++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/rule/EncryptRuleTest.java diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java index c586acf723..2da4a0d4dc 100644 --- a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java +++ b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java @@ -231,7 +231,11 @@ public final class EncryptRule implements BaseRule { @Override public Object apply(final Object input) { - return ((ShardingQueryAssistedEncryptor) shardingEncryptor.get()).queryAssistedEncrypt(input.toString()); + if (input == null) { + return null; + } else { + return ((ShardingQueryAssistedEncryptor) shardingEncryptor.get()).queryAssistedEncrypt(input.toString()); + } } }); } @@ -251,7 +255,11 @@ public final class EncryptRule implements BaseRule { @Override public Object apply(final Object input) { - return String.valueOf(shardingEncryptor.get().encrypt(input.toString())); + if (input == null) { + return null; + } else { + return String.valueOf(shardingEncryptor.get().encrypt(input.toString())); + } } }); } diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/rule/EncryptRuleTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/rule/EncryptRuleTest.java new file mode 100644 index 0000000000..d9a7977706 --- /dev/null +++ b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/rule/EncryptRuleTest.java @@ -0,0 +1,72 @@ +/* + * 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.core.rule; + +import org.apache.shardingsphere.api.config.encrypt.EncryptColumnRuleConfiguration; +import org.apache.shardingsphere.api.config.encrypt.EncryptRuleConfiguration; +import org.apache.shardingsphere.api.config.encrypt.EncryptTableRuleConfiguration; +import org.apache.shardingsphere.api.config.encrypt.EncryptorRuleConfiguration; +import org.junit.Before; +import org.junit.Test; + +import java.util.Collections; +import java.util.List; +import java.util.Properties; + +import static org.junit.Assert.assertNull; + +/** + * @author kezhenxu94 + */ +public class EncryptRuleTest { + private final String table = "table"; + private final String column = "column"; + + private EncryptRuleConfiguration encryptRuleConfig; + + @Before + public void before() { + Properties props = new Properties(); + + EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("assistedTest", props); + EncryptColumnRuleConfiguration columnConfig = new EncryptColumnRuleConfiguration("plain_pwd", "cipher_pwd", "", "aes"); + EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(Collections.singletonMap(column, columnConfig)); + + encryptRuleConfig = new EncryptRuleConfiguration(); + encryptRuleConfig.getEncryptors().put("aes", encryptorConfig); + encryptRuleConfig.getTables().put(table, tableConfig); + } + + @Test + public void testGetEncryptAssistedQueryValues() { + List encryptAssistedQueryValues = new EncryptRule(encryptRuleConfig).getEncryptAssistedQueryValues(table, column, Collections.singletonList(null)); + + for (final Object value : encryptAssistedQueryValues) { + assertNull(value); + } + } + + @Test + public void testGetEncryptValues() { + List encryptAssistedQueryValues = new EncryptRule(encryptRuleConfig).getEncryptValues(table, column, Collections.singletonList(null)); + + for (final Object value : encryptAssistedQueryValues) { + assertNull(value); + } + } +} \ No newline at end of file -- GitLab