// Path: src/test/java/org/jenkinsci/plugins/credentialsbinding/test/CredentialsTestUtil.java
// public class CredentialsTestUtil {
// 
//     /**
//      * Registers the given value as a {@link StringCredentials} into the default {@link CredentialsProvider}.
//      * Returns the generated credential id for the registered credentials.
//      */
//     public static String registerStringCredentials(ModelObject context, String value) throws IOException {
//         String credentialsId = UUID.randomUUID().toString();
//         setStringCredentials(context, credentialsId, value);
//         return credentialsId;
//     }
// 
//     /**
//      * Registers the given value as a {@link StringCredentials} into the default {@link CredentialsProvider} using the
//      * specified credentials id.
//      */
//     public static void setStringCredentials(ModelObject context, String credentialsId, String value) throws IOException {
//         StringCredentials creds = new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, null, Secret.fromString(value));
//         CredentialsProvider.lookupStores(context).iterator().next().addCredentials(Domain.global(), creds);
//     }
// }
// 
// Path: src/test/java/org/jenkinsci/plugins/credentialsbinding/test/Executables.java
// public class Executables {
//     private static final String LOCATOR = Functions.isWindows() ? "where.exe" : "which";
// 
//     public static @CheckForNull
//     String getPathToExecutable(@NonNull String executable) {
//         try {
//             Process process = new ProcessBuilder(LOCATOR, executable).start();
//             List<String> output = IOUtils.readLines(process.getInputStream(), Charset.defaultCharset());
//             if (process.waitFor() != 0) {
//                 return null;
//             }
//             return output.isEmpty() ? null : output.get(0);
//         } catch (IOException | InterruptedException e) {
//             return null;
//         }
//     }
// 
//     public static Matcher<String> executable() {
//         return new CustomTypeSafeMatcher<String>("executable") {
//             @Override
//             protected boolean matchesSafely(String item) {
//                 try {
//                     return new ProcessBuilder(LOCATOR, item).start().waitFor() == 0;
//                 } catch (InterruptedException | IOException e) {
//                     return false;
//                 }
//             }
//         };
//     }
// }
// 
// Path: src/test/java/org/jenkinsci/plugins/credentialsbinding/test/Executables.java
// public static Matcher<String> executable() {
//     return new CustomTypeSafeMatcher<String>("executable") {
//         @Override
//         protected boolean matchesSafely(String item) {
//             try {
//                 return new ProcessBuilder(LOCATOR, item).start().waitFor() == 0;
//             } catch (InterruptedException | IOException e) {
//                 return false;
//             }
//         }
//     };
// }


import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.For;
import org.jvnet.hudson.test.JenkinsRule;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.credentialsbinding.test.Executables.executable;
import static org.junit.Assume.assumeThat;
import hudson.tasks.Shell;
import org.jenkinsci.plugins.credentialsbinding.test.CredentialsTestUtil;
import org.jenkinsci.plugins.credentialsbinding.test.Executables;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
/*
 * The MIT License
 *
 * Copyright (c) 2019 CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.jenkinsci.plugins.credentialsbinding.masking;



@For(BashSecretPatternFactory.class)
public class BashSecretPatternFactory2Test {

    public @Rule JenkinsRule j = new JenkinsRule();

    @Before
    public void setUp() {
        assumeThat("bash", is(executable()));
        // due to https://github.com/jenkinsci/durable-task-plugin/blob/e75123eda986f20a390d92cc892c3d206e60aefb/src/main/java/org/jenkinsci/plugins/durabletask/BourneShellScript.java#L149
        // on Windows
        assumeThat("nohup", is(executable()));
        j.jenkins.getDescriptorByType(Shell.DescriptorImpl.class).setShell(Executables.getPathToExecutable("bash"));
    }

    @Test
    // DO NOT DO THIS IN PRODUCTION; IT IS QUOTED WRONG
    public void testSecretsWithBackslashesStillMaskedWhenUsedWithoutProperQuoting() throws Exception {
        WorkflowJob project = j.createProject(WorkflowJob.class);
// Path: src/test/java/org/jenkinsci/plugins/credentialsbinding/test/CredentialsTestUtil.java
// public class CredentialsTestUtil {
// 
//     /**
//      * Registers the given value as a {@link StringCredentials} into the default {@link CredentialsProvider}.
//      * Returns the generated credential id for the registered credentials.
//      */
//     public static String registerStringCredentials(ModelObject context, String value) throws IOException {
//         String credentialsId = UUID.randomUUID().toString();
//         setStringCredentials(context, credentialsId, value);
//         return credentialsId;
//     }
// 
//     /**
//      * Registers the given value as a {@link StringCredentials} into the default {@link CredentialsProvider} using the
//      * specified credentials id.
//      */
//     public static void setStringCredentials(ModelObject context, String credentialsId, String value) throws IOException {
//         StringCredentials creds = new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, null, Secret.fromString(value));
//         CredentialsProvider.lookupStores(context).iterator().next().addCredentials(Domain.global(), creds);
//     }
// }
// 
// Path: src/test/java/org/jenkinsci/plugins/credentialsbinding/test/Executables.java
// public class Executables {
//     private static final String LOCATOR = Functions.isWindows() ? "where.exe" : "which";
// 
//     public static @CheckForNull
//     String getPathToExecutable(@NonNull String executable) {
//         try {
//             Process process = new ProcessBuilder(LOCATOR, executable).start();
//             List<String> output = IOUtils.readLines(process.getInputStream(), Charset.defaultCharset());
//             if (process.waitFor() != 0) {
//                 return null;
//             }
//             return output.isEmpty() ? null : output.get(0);
//         } catch (IOException | InterruptedException e) {
//             return null;
//         }
//     }
// 
//     public static Matcher<String> executable() {
//         return new CustomTypeSafeMatcher<String>("executable") {
//             @Override
//             protected boolean matchesSafely(String item) {
//                 try {
//                     return new ProcessBuilder(LOCATOR, item).start().waitFor() == 0;
//                 } catch (InterruptedException | IOException e) {
//                     return false;
//                 }
//             }
//         };
//     }
// }
// 
// Path: src/test/java/org/jenkinsci/plugins/credentialsbinding/test/Executables.java
// public static Matcher<String> executable() {
//     return new CustomTypeSafeMatcher<String>("executable") {
//         @Override
//         protected boolean matchesSafely(String item) {
//             try {
//                 return new ProcessBuilder(LOCATOR, item).start().waitFor() == 0;
//             } catch (InterruptedException | IOException e) {
//                 return false;
//             }
//         }
//     };
// }


// Path: src/test/java/org/jenkinsci/plugins/credentialsbinding/masking/BashSecretPatternFactory2Test.java
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.For;
import org.jvnet.hudson.test.JenkinsRule;
import static org.hamcrest.Matchers.is;
import static org.jenkinsci.plugins.credentialsbinding.test.Executables.executable;
import static org.junit.Assume.assumeThat;
import hudson.tasks.Shell;
import org.jenkinsci.plugins.credentialsbinding.test.CredentialsTestUtil;
import org.jenkinsci.plugins.credentialsbinding.test.Executables;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;

/*
 * The MIT License
 *
 * Copyright (c) 2019 CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.jenkinsci.plugins.credentialsbinding.masking;



@For(BashSecretPatternFactory.class)
public class BashSecretPatternFactory2Test {

    public @Rule JenkinsRule j = new JenkinsRule();

    @Before
    public void setUp() {
        assumeThat("bash", is(executable()));
        // due to https://github.com/jenkinsci/durable-task-plugin/blob/e75123eda986f20a390d92cc892c3d206e60aefb/src/main/java/org/jenkinsci/plugins/durabletask/BourneShellScript.java#L149
        // on Windows
        assumeThat("nohup", is(executable()));
        j.jenkins.getDescriptorByType(Shell.DescriptorImpl.class).setShell(Executables.getPathToExecutable("bash"));
    }

    @Test
    // DO NOT DO THIS IN PRODUCTION; IT IS QUOTED WRONG
    public void testSecretsWithBackslashesStillMaskedWhenUsedWithoutProperQuoting() throws Exception {
        WorkflowJob project = j.createProject(WorkflowJob.class);
        String password = "foo\\bar\\";