Skip to content

Commit

Permalink
fix issue 214, add test case (#216)
Browse files Browse the repository at this point in the history
Signed-off-by: Nicklas Körtge <[email protected]>
  • Loading branch information
n1ckl0sk0rtge authored Feb 5, 2025
1 parent ef0dcd6 commit c9347a1
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 2 deletions.
9 changes: 9 additions & 0 deletions java/src/test/files/rules/issues/Issue214TestFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.security.NoSuchAlgorithmException;
import java.security.Signature;

public class JcaSignatureGetInstanceTestFile {

public void test() throws NoSuchAlgorithmException {
Signature signature = Signature.getInstance("SHA1withRSA"); // Noncompliant {{(Signature) SHA1withRSA}}
}
}
120 changes: 120 additions & 0 deletions java/src/test/java/com/ibm/plugin/rules/issues/Issue214Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* SonarQube Cryptography Plugin
* Copyright (C) 2025 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.plugin.rules.issues;

import com.ibm.engine.detection.DetectionStore;
import com.ibm.engine.model.Algorithm;
import com.ibm.engine.model.IValue;
import com.ibm.engine.model.context.SignatureContext;
import com.ibm.mapper.model.BlockSize;
import com.ibm.mapper.model.DigestSize;
import com.ibm.mapper.model.INode;
import com.ibm.mapper.model.KeyLength;
import com.ibm.mapper.model.MessageDigest;
import com.ibm.mapper.model.Oid;
import com.ibm.mapper.model.Signature;
import com.ibm.mapper.model.functionality.Digest;
import com.ibm.plugin.TestBase;
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.Tree;

import javax.annotation.Nonnull;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

// https://github.com/IBM/sonar-cryptography/issues/214
class Issue214Test extends TestBase {

@Test
void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/rules/issues/Issue214TestFile.java")
.withChecks(this)
.verifyIssues();
}

@Override
public void asserts(
int findingId,
@Nonnull DetectionStore<JavaCheck, Tree, Symbol, JavaFileScannerContext> detectionStore,
@Nonnull List<INode> nodes) {
/*
* Detection Store
*/

assertThat(detectionStore.getDetectionValues()).hasSize(1);
assertThat(detectionStore.getDetectionValueContext()).isInstanceOf(SignatureContext.class);
IValue<Tree> value0 = detectionStore.getDetectionValues().get(0);
assertThat(value0).isInstanceOf(Algorithm.class);
assertThat(value0.asString()).isEqualTo("SHA1withRSA");

/*
* Translation
*/
assertThat(nodes).hasSize(1);

// Signature
INode signatureNode = nodes.get(0);
assertThat(signatureNode.getKind()).isEqualTo(Signature.class);
assertThat(signatureNode.getChildren()).hasSize(3);
assertThat(signatureNode.asString()).isEqualTo("SHA1withRSA");

// Oid under Signature
INode oidNode = signatureNode.getChildren().get(Oid.class);
assertThat(oidNode).isNotNull();
assertThat(oidNode.getChildren()).isEmpty();
assertThat(oidNode.asString()).isEqualTo("1.2.840.113549.1.1.5");

// MessageDigest under Signature
INode messageDigestNode = signatureNode.getChildren().get(MessageDigest.class);
assertThat(messageDigestNode).isNotNull();
assertThat(messageDigestNode.getChildren()).hasSize(3);
assertThat(messageDigestNode.asString()).isEqualTo("SHA1");

// DigestSize under MessageDigest under Signature
INode digestSizeNode = messageDigestNode.getChildren().get(DigestSize.class);
assertThat(digestSizeNode).isNotNull();
assertThat(digestSizeNode.getChildren()).isEmpty();
assertThat(digestSizeNode.asString()).isEqualTo("160");

// Digest under MessageDigest under Signature
INode digestNode = messageDigestNode.getChildren().get(Digest.class);
assertThat(digestNode).isNotNull();
assertThat(digestNode.getChildren()).isEmpty();
assertThat(digestNode.asString()).isEqualTo("DIGEST");

// BlockSize under MessageDigest under Signature
INode blockSizeNode = messageDigestNode.getChildren().get(BlockSize.class);
assertThat(blockSizeNode).isNotNull();
assertThat(blockSizeNode.getChildren()).isEmpty();
assertThat(blockSizeNode.asString()).isEqualTo("512");

// KeyLength under Signature
INode keyLengthNode = signatureNode.getChildren().get(KeyLength.class);
assertThat(keyLengthNode).isNotNull();
assertThat(keyLengthNode.getChildren()).isEmpty();
assertThat(keyLengthNode.asString()).isEqualTo("2048");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class JcaAlgorithmMapper implements IMapper {
new JcaMGFMapper(),
new JcaPasswordBasedEncryptionMapper(),
new JcaPBKDFMapper(),
new JcaPRNGMapper(),
new JcaKeyAgreementMapper(),
new JcaSignatureMapper());
new JcaSignatureMapper(),
new JcaPRNGMapper());

@Nonnull
@Override
Expand Down

0 comments on commit c9347a1

Please sign in to comment.