Skip to content

Commit

Permalink
Feature/update python tests (#122)
Browse files Browse the repository at this point in the history
* update rsa signature tests

Signed-off-by: Nicklas Körtge <[email protected]>

* update tests

Signed-off-by: Nicklas Körtge <[email protected]>

* update tests

Signed-off-by: Nicklas Körtge <[email protected]>

---------

Signed-off-by: Nicklas Körtge <[email protected]>
  • Loading branch information
n1ckl0sk0rtge authored Sep 2, 2024
1 parent 4700a0a commit 80f7bb8
Show file tree
Hide file tree
Showing 27 changed files with 673 additions and 686 deletions.
1 change: 0 additions & 1 deletion enricher/src/main/java/com/ibm/enricher/Enricher.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public INode enrich(@Nonnull INode node) {
if (node instanceof Signature) {
node = new SignatureEnricher().enrich(node);
}

if (node instanceof MessageDigest) {
node = new MacOrDigestEnricher().enrich(node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.ibm.enricher.IEnricher;
import com.ibm.mapper.model.INode;
import com.ibm.mapper.model.Signature;
import com.ibm.mapper.model.algorithms.RSA;
import org.jetbrains.annotations.NotNull;

Expand All @@ -30,9 +29,6 @@ public class RSAEnricher implements IEnricher, IEnrichWithDefaultKeySize {
@NotNull @Override
public INode enrich(@NotNull INode node) {
if (node instanceof RSA rsa) {
if (rsa.is(Signature.class)) {
return enrichSignature(rsa);
}
return enrich(rsa);
}
return node;
Expand All @@ -42,8 +38,4 @@ public INode enrich(@NotNull INode node) {
this.applyDefaultKeySizeForJca(rsa, 2048);
return rsa;
}

@NotNull private RSA enrichSignature(@NotNull RSA rsa) {
return rsa;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class SignatureEnricher implements IEnricher {
@Override
public @NotNull INode enrich(@NotNull INode node) {
if (node instanceof Signature signature) {
if (node instanceof Signature signature && signature.is(Signature.class)) {
return enrich(signature);
}
return node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.ibm.mapper.reorganizer.rules.BlockCipherReorganizer;
import com.ibm.mapper.reorganizer.rules.CipherParameterReorganizer;
import com.ibm.mapper.reorganizer.rules.MacReorganizer;
import com.ibm.mapper.reorganizer.rules.SignerReorganizer;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
Expand All @@ -42,8 +41,7 @@ public static List<IReorganizerRule> rules() {
AsymmetricBlockCipherReorganizer.rules().stream(),
BlockCipherReorganizer.rules().stream(),
CipherParameterReorganizer.rules().stream(),
MacReorganizer.rules().stream(),
SignerReorganizer.rules().stream())
MacReorganizer.rules().stream())
.flatMap(i -> i)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* 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 com.ibm.mapper.reorganizer;

import com.ibm.mapper.model.INode;
import com.ibm.mapper.utils.Function3;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public interface IFunctionDetectionCondition extends Function3<INode, INode, List<INode>, Boolean> {
@Override
@Nonnull
Boolean apply(@Nonnull INode node, @Nullable INode parent, @Nonnull List<INode> roots);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2024 IBM
*
* 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 com.ibm.mapper.reorganizer;

import com.ibm.mapper.model.INode;
import com.ibm.mapper.utils.Function3;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public interface IFunctionPerformReorganization
extends Function3<INode, INode, List<INode>, List<INode>> {

@Override
@Nullable List<INode> apply(@Nonnull INode node, @Nullable INode parent, @Nonnull List<INode> roots);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
package com.ibm.mapper.reorganizer;

import com.ibm.mapper.model.INode;
import com.ibm.mapper.utils.Function3;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public interface IReorganizerRule {

Expand All @@ -44,8 +44,7 @@ public interface IReorganizerRule {
* @param roots - The list of root nodes for the translation tree
* @return An updated list of root nodes for the translation tree
*/
@Nonnull
List<INode> applyReorganization(
@Nullable List<INode> applyReorganization(
@Nonnull INode node, @Nonnull INode parent, @Nonnull List<INode> roots);

@Nonnull
Expand All @@ -71,6 +70,9 @@ List<INode> applyReorganization(
interface IReorganizerRuleBuilder {
@Nonnull
KindBuilder createReorganizerRule();

@Nonnull
KindBuilder createReorganizerRule(@Nonnull String ruleName);
}

interface KindBuilder {
Expand All @@ -90,11 +92,10 @@ interface ValueBuilder {

@Nonnull
PerformBuilder withDetectionCondition(
Function3<INode, INode, List<INode>, Boolean> detectionConditionFunction);
@Nonnull IFunctionDetectionCondition detectionConditionFunction);

@Nonnull
IReorganizerRule perform(
@Nonnull Function3<INode, INode, List<INode>, List<INode>> performFunction);
IReorganizerRule perform(@Nonnull IFunctionPerformReorganization performFunction);

@Nonnull
IReorganizerRule noAction();
Expand All @@ -109,11 +110,10 @@ interface ChildrenBuilder {

@Nonnull
PerformBuilder withDetectionCondition(
Function3<INode, INode, List<INode>, Boolean> detectionConditionFunction);
@Nonnull IFunctionDetectionCondition detectionConditionFunction);

@Nonnull
IReorganizerRule perform(
@Nonnull Function3<INode, INode, List<INode>, List<INode>> performFunction);
IReorganizerRule perform(@Nonnull IFunctionPerformReorganization performFunction);

@Nonnull
IReorganizerRule noAction();
Expand All @@ -122,20 +122,18 @@ IReorganizerRule perform(
interface DetectionConditionBuilder {
@Nonnull
PerformBuilder withDetectionCondition(
Function3<INode, INode, List<INode>, Boolean> detectionConditionFunction);
@Nonnull IFunctionDetectionCondition detectionConditionFunction);

@Nonnull
IReorganizerRule perform(
@Nonnull Function3<INode, INode, List<INode>, List<INode>> performFunction);
IReorganizerRule perform(@Nonnull IFunctionPerformReorganization performFunction);

@Nonnull
IReorganizerRule noAction();
}

interface PerformBuilder {
@Nonnull
IReorganizerRule perform(
@Nonnull Function3<INode, INode, List<INode>, List<INode>> performFunction);
IReorganizerRule perform(@Nonnull IFunctionPerformReorganization performFunction);

@Nonnull
IReorganizerRule noAction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class Reorganizer implements IReorganizer {

private final List<IReorganizerRule> rules;

public Reorganizer(List<IReorganizerRule> rules) {
public Reorganizer(@Nonnull List<IReorganizerRule> rules) {
this.rules = rules;
}

Expand Down Expand Up @@ -132,7 +132,7 @@ private Optional<List<INode>> applyReorganizerRules(
"[reorganizer] MATCH: Node '%s' & Rule %s",
node.asString(), reorganizerRule.asString());
LOGGER.debug(message);
return Optional.of(
return Optional.ofNullable(
reorganizerRule.applyReorganization(node, parent, rootNodes)); // new root
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.annotation.Nullable;

public record ReorganizerRule(
@Nullable String name,
@Nonnull Class<? extends INode> kind,
@Nullable String value,
@Nonnull List<IReorganizerRule> children,
Expand Down Expand Up @@ -82,7 +83,8 @@ public String asString() {
String[] kindStrings = kind.toString().split("\\.");
String kindName = kindStrings[kindStrings.length - 1];
return String.format(
"[kind: %s | value: %s | %d subrule(s)]", kindName, value, children.size());
"[name: %s | kind: %s | value: %s | %d subrule(s)]",
name, kindName, value, children.size());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -42,7 +43,7 @@ private UsualPerformActions() {
* move them to the same level (i.e., under the node's parent)
*/
@Nonnull
public static final Function3<INode, INode, List<INode>, List<INode>> performMovingChildrenUp =
public static final IFunctionPerformReorganization performMovingChildrenUp =
(node, parent, roots) -> {
if (parent == null) {
// Do nothing
Expand All @@ -68,7 +69,7 @@ private UsualPerformActions() {
* @return The {@code Function3} returning the updated list of root nodes
*/
@Nonnull
public static Function3<INode, INode, List<INode>, List<INode>> performReplacingNode(
public static IFunctionPerformReorganization performReplacingNode(
@Nonnull Function3<INode, INode, List<INode>, INode> perform) {
return (node, parent, roots) -> {
INode newNode = perform.apply(node, parent, roots);
Expand All @@ -80,7 +81,7 @@ public static Function3<INode, INode, List<INode>, List<INode>> performReplacing
private static List<INode> replaceNode(
@Nonnull INode newNode,
@NotNull INode originalNode,
INode parent,
@Nullable INode parent,
@Nonnull List<INode> roots) {
// Add all the children to the new node
for (Map.Entry<Class<? extends INode>, INode> childKeyValue :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@
import com.ibm.mapper.reorganizer.IReorganizerRule;
import com.ibm.mapper.reorganizer.IReorganizerRule.KindBuilder;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.NotNull;

public final class ReorganizerRuleBuilder implements IReorganizerRule.IReorganizerRuleBuilder {

@Override
public @Nonnull KindBuilder createReorganizerRule() {
return new ReorganizerRuleBuilderImpl();
}

@Override
public @NotNull KindBuilder createReorganizerRule(@NotNull String ruleName) {
return new ReorganizerRuleBuilderImpl(ruleName);
}
}
Loading

0 comments on commit 80f7bb8

Please sign in to comment.