<?xml version="1.0" encoding="UTF-8"?>
<test-data
    xmlns="http://pmd.sourceforge.net/rule-tests"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">

    <test-code>
        <description>Hard coded inline cryptographic key, bad</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import javax.crypto.spec.SecretKeySpec;

public class Foo {

    void encrypt() {
        SecretKeySpec keySpec =  new SecretKeySpec("hard coded key here".getBytes("UTF-8"), "AES");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Key stored in a property, good</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import javax.crypto.spec.SecretKeySpec;

public class SecureProperty {
    byte[] getKeyBytes() {return null;}
    void encrypt(SecureProperty secureProperty) {
        SecretKeySpec keySpec =  new SecretKeySpec(secureProperty.getKeyBytes(), "AES");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Hard coded in field cryptographic key, bad</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>5</expected-linenumbers>
        <code><![CDATA[
import javax.crypto.spec.SecretKeySpec;

public class Foo {

    static private String key = "0123456789ABCDEF";

    void encrypt() {
        byte[] keyData = key.getBytes("UTF-8");

        SecretKeySpec keySpec =  new SecretKeySpec(keyData, "AES");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Hard coded crypto key byte[], bad</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
import javax.crypto.spec.SecretKeySpec;

public class Foo {

    static private byte[] encryptionKey = new byte[] { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 };

    void encrypt() {
        SecretKeySpec keySpec =  new SecretKeySpec(encryptionKey, "AES");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Key fetched from a property, good</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import javax.crypto.spec.SecretKeySpec;
import my.SecureUtils;
import my.Struct;
public class Foo {

    void encrypt(Struct data) {
        byte[] computedSecretKey = SecureUtils.computeSecretKey(data.getSecretKeyAlgorithm(),
            data.getAccessKey(), data.getClientIp(), data.getTimeStamp(), data.getEnv());
        SecretKeySpec secretKeySpec = new SecretKeySpec(computedSecretKey, data.getSecretKeyAlgorithm().getName());
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>[java] HardcodedCryptoKey false negative with variable assignments #3368</description>
        <expected-problems>3</expected-problems>
        <expected-linenumbers>7,9,16</expected-linenumbers>
        <code><![CDATA[
import javax.crypto.spec.SecretKeySpec;

public class Foo {
    public void testHardCodedCryptoKey(boolean tag) {
       String str;
       if (tag) {
          str = "Hardcoded Crypto Key1"; // should report a warning here
       } else {
          str = "Hardcoded Crypto Key2"; // should report a warning here
       }
       SecretKeySpec secretKeySpec = new SecretKeySpec(str.getBytes(), "AES");
    }

    void encrypt() {
        final String var0;
        var0 = "hard coded key here";
        SecretKeySpec keySpec =  new SecretKeySpec(var0.getBytes(), "AES");
    }

    void encrypt2(String prefix, String key) {
        final String var0 = prefix;
        var0 = var0 + key;
        SecretKeySpec keySpec =  new SecretKeySpec(var0.getBytes(), "AES");
    }
}
        ]]></code>
    </test-code>
</test-data>
