<commit_before>package hudson.util

import com.trilead.ssh2.crypto.Base64
import hudson.FilePath
import jenkins.security.ConfidentialStoreRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

import javax.crypto.Cipher

/**
 *
 *
 * @author <NAME>
 */
class SecretRewriterTest {
    @Rule
    public MockSecretRule mockSecretRule = new MockSecretRule()

    @Rule
    public ConfidentialStoreRule confidentialStoreRule = new ConfidentialStoreRule();

    @Rule public TemporaryFolder tmp = new TemporaryFolder()

    @Test
    void singleFileRewrite() {
        def o = encryptOld('foobar') // old
        def n = encryptNew('foobar') // new
        roundtrip "<foo>${o}</foo>",
                  "<foo>${n}</foo>"

        roundtrip "<foo>${o}</foo><foo>${o}</foo>",
                  "<foo>${n}</foo><foo>${n}</foo>"

        roundtrip "<foo>${n}</foo>",
                  "<foo>${n}</foo>"

        roundtrip "  <foo>thisIsLegalBase64AndLongEnoughThatItCouldLookLikeSecret</foo>  ",
                  "  <foo>thisIsLegalBase64AndLongEnoughThatItCouldLookLikeSecret</foo>  "

        // to be rewritten, it needs to be between a tag
        roundtrip "<foo>$o", "<foo>$o"
        roundtrip "$o</foo>", "$o</foo>"

        //
        roundtrip "<abc>\n<foo>$o</foo>\n</abc>", "<abc>\n<foo>$n</foo>\n</abc>"
    }

    void roundtrip(String before, String after) {
        def sr = new SecretRewriter(null);
        def f = File.createTempFile("test", "xml", tmp.root)
        f.text = before
        sr.rewrite(f,null)
        assert after.trim()==f.text.trim()
    }

    String encryptOld(str) {
        def cipher = Secret.getCipher("AES");
        cipher.init(Cipher.ENCRYPT_MODE, Secret.legacyKey);<commit_msg>Fix for JENKINS-16942<commit_after>package hudson.util

import com.trilead.ssh2.crypto.Base64
import hudson.FilePath
import jenkins.security.ConfidentialStoreRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

import javax.crypto.Cipher

/**
 *
 *
 * @author <NAME>
 */
class SecretRewriterTest {
    @Rule
    public MockSecretRule mockSecretRule = new MockSecretRule()

    @Rule
    public ConfidentialStoreRule confidentialStoreRule = new ConfidentialStoreRule();

    @Rule public TemporaryFolder tmp = new TemporaryFolder()

    @Test
    void singleFileRewrite() {
        def o = encryptOld('foobar') // old
        def n = encryptNew('foobar') // new
        roundtrip "<foo>${o}</foo>",
                  "<foo>${n}</foo>"

        roundtrip "<foo>${o}</foo><foo>${o}</foo>",
                  "<foo>${n}</foo><foo>${n}</foo>"

        roundtrip "<foo>${n}</foo>",
                  "<foo>${n}</foo>"

        roundtrip "  <foo>thisIsLegalBase64AndLongEnoughThatItCouldLookLikeSecret</foo>  ",
                  "  <foo>thisIsLegalBase64AndLongEnoughThatItCouldLookLikeSecret</foo>  "

        // to be rewritten, it needs to be between a tag
        roundtrip "<foo>$o", "<foo>$o"
        roundtrip "$o</foo>", "$o</foo>"

        //
        roundtrip "<abc>\n<foo>$o</foo>\n</abc>", "<abc>\n<foo>$n</foo>\n</abc>"
    }

    void roundtrip(String before, String after) {
        def sr = new SecretRewriter(null);
        def f = File.createTempFile("test", "xml", tmp.root)
        f.text = before
        sr.rewrite(f,null)
        assert after.replaceAll("\r\n", "\n").trim()==f.text.replaceAll("\r\n", "\n").trim()
    }

    String encryptOld(str) {
        def cipher = Secret.getCipher("AES");
        cipher.init(Cipher.ENCRYPT_MODE, Secret.legacyKey);
{'fraction_non_alphanumeric': 0.15814606741573034, 'fraction_numerical': 0.0053370786516853935, 'mean_word_length': 2.7171189979123174, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4437978', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1106, 'n_words': 215}