Skip to content

Commit

Permalink
Add test cases on EncryptInsertSelectProjectionSupportedChecker (#33646)
Browse files Browse the repository at this point in the history
* Add test cases on EncryptInsertSelectProjectionSupportedChecker

* Add test cases on EncryptInsertSelectProjectionSupportedChecker
  • Loading branch information
terrymanu authored Nov 13, 2024
1 parent 569efa2 commit cd360cc
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ private boolean containsOrderByItem(final SelectStatementContext sqlStatementCon
}

@Override
public void check(final EncryptRule encryptRule, final ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) {
public void check(final EncryptRule rule, final ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) {
for (OrderByItem each : getOrderByItems(sqlStatementContext)) {
if (each.getSegment() instanceof ColumnOrderByItemSegment) {
checkColumnOrderByItem(encryptRule, schema, sqlStatementContext, ((ColumnOrderByItemSegment) each.getSegment()).getColumn());
checkColumnOrderByItem(rule, schema, sqlStatementContext, ((ColumnOrderByItemSegment) each.getSegment()).getColumn());
}
}
}
Expand All @@ -79,10 +79,10 @@ private Collection<OrderByItem> getOrderByItems(final SelectStatementContext sql
return result;
}

private void checkColumnOrderByItem(final EncryptRule encryptRule, final ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext, final ColumnSegment columnSegment) {
private void checkColumnOrderByItem(final EncryptRule rule, final ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext, final ColumnSegment columnSegment) {
Map<String, String> columnTableNames = sqlStatementContext.getTablesContext().findTableNames(Collections.singleton(columnSegment), schema);
String tableName = columnTableNames.getOrDefault(columnSegment.getExpression(), "");
Optional<EncryptTable> encryptTable = encryptRule.findEncryptTable(tableName);
Optional<EncryptTable> encryptTable = rule.findEncryptTable(tableName);
String columnName = columnSegment.getIdentifier().getValue();
ShardingSpherePreconditions.checkState(!encryptTable.isPresent() || !encryptTable.get().isEncryptColumn(columnName), () -> new UnsupportedEncryptSQLException("ORDER BY"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
}

@Override
public void check(final EncryptRule encryptRule, final ShardingSphereSchema schema, final InsertStatementContext sqlStatementContext) {
checkSelect(encryptRule, sqlStatementContext.getInsertSelectContext().getSelectStatementContext());
public void check(final EncryptRule rule, final ShardingSphereSchema schema, final InsertStatementContext sqlStatementContext) {
checkSelect(rule, sqlStatementContext.getInsertSelectContext().getSelectStatementContext());
for (SelectStatementContext each : sqlStatementContext.getInsertSelectContext().getSelectStatementContext().getSubqueryContexts().values()) {
checkSelect(encryptRule, each);
checkSelect(rule, each);
}
}

private void checkSelect(final EncryptRule encryptRule, final SelectStatementContext selectStatementContext) {
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(encryptRule, selectStatementContext);
private void checkSelect(final EncryptRule rule, final SelectStatementContext selectStatementContext) {
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(rule, selectStatementContext);
for (ProjectionSegment each : selectStatementContext.getSqlStatement().getProjections().getProjections()) {
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptShorthandExpandWithSubqueryStatement(selectStatementContext, each);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ public final class EncryptProjectionRewriteSupportedChecker {
/**
* Check not contain encrypt projection in combine segment.
*
* @param encryptRule encrypt rule
* @param rule encrypt rule
* @param selectStatementContext select statement context
*/
public static void checkNotContainEncryptProjectionInCombineSegment(final EncryptRule encryptRule, final SelectStatementContext selectStatementContext) {
ShardingSpherePreconditions.checkState(!containsEncryptProjectionInCombineSegment(encryptRule, selectStatementContext),
public static void checkNotContainEncryptProjectionInCombineSegment(final EncryptRule rule, final SelectStatementContext selectStatementContext) {
ShardingSpherePreconditions.checkState(!containsEncryptProjectionInCombineSegment(rule, selectStatementContext),
() -> new UnsupportedSQLOperationException("Can not support encrypt projection in combine statement."));
}

private static boolean containsEncryptProjectionInCombineSegment(final EncryptRule encryptRule, final SelectStatementContext selectStatementContext) {
private static boolean containsEncryptProjectionInCombineSegment(final EncryptRule rule, final SelectStatementContext selectStatementContext) {
if (!selectStatementContext.getSqlStatement().getCombine().isPresent()) {
return false;
}
Expand All @@ -62,18 +62,18 @@ private static boolean containsEncryptProjectionInCombineSegment(final EncryptRu
List<Projection> rightProjections = selectStatementContext.getSubqueryContexts().get(combineSegment.getRight().getStartIndex()).getProjectionsContext().getExpandProjections();
ShardingSpherePreconditions.checkState(leftProjections.size() == rightProjections.size(), () -> new UnsupportedSQLOperationException("Column projections must be same for combine statement"));
for (int i = 0; i < leftProjections.size(); i++) {
if (containsEncryptProjectionInCombineSegment(encryptRule, leftProjections.get(i), rightProjections.get(i))) {
if (containsEncryptProjectionInCombineSegment(rule, leftProjections.get(i), rightProjections.get(i))) {
return true;
}
}
return false;
}

private static boolean containsEncryptProjectionInCombineSegment(final EncryptRule encryptRule, final Projection leftProjection, final Projection rightProjection) {
private static boolean containsEncryptProjectionInCombineSegment(final EncryptRule rule, final Projection leftProjection, final Projection rightProjection) {
ColumnSegmentBoundInfo leftColumnInfo = getColumnSegmentBoundInfo(leftProjection);
EncryptAlgorithm leftColumnEncryptor = encryptRule.findQueryEncryptor(leftColumnInfo.getOriginalTable().getValue(), leftColumnInfo.getOriginalColumn().getValue()).orElse(null);
EncryptAlgorithm leftColumnEncryptor = rule.findQueryEncryptor(leftColumnInfo.getOriginalTable().getValue(), leftColumnInfo.getOriginalColumn().getValue()).orElse(null);
ColumnSegmentBoundInfo rightColumnInfo = getColumnSegmentBoundInfo(rightProjection);
EncryptAlgorithm rightColumnEncryptor = encryptRule.findQueryEncryptor(rightColumnInfo.getOriginalTable().getValue(), rightColumnInfo.getOriginalColumn().getValue()).orElse(null);
EncryptAlgorithm rightColumnEncryptor = rule.findQueryEncryptor(rightColumnInfo.getOriginalTable().getValue(), rightColumnInfo.getOriginalColumn().getValue()).orElse(null);
return null != leftColumnEncryptor || null != rightColumnEncryptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public boolean isCheck(final SQLStatementContext sqlStatementContext) {
}

@Override
public void check(final EncryptRule encryptRule, final ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) {
checkSelect(encryptRule, sqlStatementContext);
public void check(final EncryptRule rule, final ShardingSphereSchema schema, final SelectStatementContext sqlStatementContext) {
checkSelect(rule, sqlStatementContext);
for (SelectStatementContext each : sqlStatementContext.getSubqueryContexts().values()) {
checkSelect(encryptRule, each);
checkSelect(rule, each);
}
}

private void checkSelect(final EncryptRule encryptRule, final SelectStatementContext selectStatementContext) {
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(encryptRule, selectStatementContext);
private void checkSelect(final EncryptRule rule, final SelectStatementContext selectStatementContext) {
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptProjectionInCombineSegment(rule, selectStatementContext);
for (ProjectionSegment each : selectStatementContext.getSqlStatement().getProjections().getProjections()) {
EncryptProjectionRewriteSupportedChecker.checkNotContainEncryptShorthandExpandWithSubqueryStatement(selectStatementContext, each);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.checker.sql.projection;

import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.binder.context.segment.insert.values.InsertSelectContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class EncryptInsertSelectProjectionSupportedCheckerTest {

@Test
void assertIsCheckWithNotInsertStatementContext() {
assertFalse(new EncryptInsertSelectProjectionSupportedChecker().isCheck(mock(SQLStatementContext.class)));
}

@Test
void assertIsCheckWithoutInsertSelect() {
InsertStatementContext sqlStatementContext = mock(InsertStatementContext.class);
when(sqlStatementContext.getInsertSelectContext()).thenReturn(null);
assertFalse(new EncryptInsertSelectProjectionSupportedChecker().isCheck(sqlStatementContext));
}

@Test
void assertIsCheckWithInsertSelect() {
InsertStatementContext sqlStatementContext = mock(InsertStatementContext.class);
when(sqlStatementContext.getInsertSelectContext()).thenReturn(mock(InsertSelectContext.class));
assertTrue(new EncryptInsertSelectProjectionSupportedChecker().isCheck(sqlStatementContext));
}

@Test
void assertCheckSuccess() {
InsertStatementContext sqlStatementContext = mock(InsertStatementContext.class);
when(sqlStatementContext.getInsertSelectContext()).thenReturn(mock(InsertSelectContext.class, RETURNS_DEEP_STUBS));
when(sqlStatementContext.getInsertSelectContext().getSelectStatementContext().getSubqueryContexts().values())
.thenReturn(Collections.singletonList(mock(SelectStatementContext.class, RETURNS_DEEP_STUBS)));
assertDoesNotThrow(() -> new EncryptInsertSelectProjectionSupportedChecker().check(mock(EncryptRule.class), mock(ShardingSphereSchema.class), sqlStatementContext));
}
}

0 comments on commit cd360cc

Please sign in to comment.