// Path: sonar-plugin-agent/src/main/java/jetbrains/buildserver/sonarplugin/util/SSLTools.java
// public final class SSLTools {
//     private static final Logger log = LoggerFactory.getLogger(SQScannerArgsComposer.class);
// 
//     /**
//      * Returns path to temporary created keystore containing TC-trusted certificates with jdk-trusted ones.
//      *
//      * @param javaHome if null, this jvm keystore will be used
//      * @param teamcityCertificateFolderPath path to folder containing TC-trusted certificates
//      * @return path to created keystore
//      */
//     public static Path cloneKeyStoreWithTC(@Nullable String javaHome, String teamcityCertificateFolderPath) {
//         try {
//             KeyStore cacerts = getCacertsKeyStore(javaHome);
//             File f = File.createTempFile("tmpks", ".jks");
//             if (storeCertificates(cacerts, teamcityCertificateFolderPath) > 0) {
//                 try (FileOutputStream fos = new FileOutputStream(f)) {
//                     cacerts.store(fos, "changeit".toCharArray());
//                 }
//                 return f.toPath();
//             }
//             log.error("Error while using certificates from {}", teamcityCertificateFolderPath);
//         } catch (Exception e) {
//             log.error("Error while using certificates from {}", teamcityCertificateFolderPath, e);
//         }
//         return null;
//     }
// 
//     public static KeyStore getCacertsKeyStore(String javaHome) throws Exception {
//         File file = new File(getCacerts(javaHome));
//         if (!file.exists()) {
//             return null;
//         }
//         KeyStore instance = KeyStore.getInstance(KeyStore.getDefaultType());
//         try (InputStream in = new FileInputStream(file)) {
//             instance.load(in, "changeit".toCharArray());
//             return instance;
//         }
//     }
// 
//     public static String getCacerts(String javaHome) {
//         String sep = File.separator;
//         if (javaHome == null)
//             javaHome = System.getProperty("java.home");
//         return javaHome + sep
//                 + "lib" + sep + "security" + sep
//                 + "cacerts";
//     }
// 
//     private static int storeCertificates(KeyStore ks, String syncCertPath) {
//         Path certificateFolder = new File(syncCertPath).toPath();
//         AtomicInteger counter = new AtomicInteger(0);
//         try {
//             Files.walk(certificateFolder, 1)
//                     .filter(Files::isRegularFile)
//                     .map(it -> storeCertificate(ks, it))
//                     .filter(Objects::nonNull)
//                     .forEach(t -> counter.incrementAndGet());
//         } catch (IOException e) {
//             log.error("Error while copying certificates from {}", syncCertPath, e);
//         }
//         return counter.get();
//     }
// 
//     private static Path storeCertificate(KeyStore ks, Path certificatePath) {
//         try (FileInputStream fis = new FileInputStream(certificatePath.toFile())) {
//             Certificate c = CertificateFactory.getInstance("X.509").generateCertificate(fis);
//             ks.setCertificateEntry(certificatePath.toFile().getName(), c);
//             return certificatePath;
//         } catch (IOException | CertificateException | KeyStoreException e) {
//             log.error("Error while reading certificate from {}", certificatePath, e);
//         }
//         return null;
//     }
// 
// }
// 
// Path: sonar-plugin-agent/src/test/java/jetbrains/buildserver/sonarplugin/TestTools.java
// public static Path createTempDirectory() throws IOException {
//     return Files.createTempDirectory("keystoreTemp");
// }
// 
// Path: sonar-plugin-agent/src/test/java/jetbrains/buildserver/sonarplugin/TestTools.java
// public static File writeTo(String prefix, String content) {
//     return writeTo(prefix, content, ".tmp", null);
// }


import jetbrains.buildserver.sonarplugin.util.SSLTools;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import static jetbrains.buildserver.sonarplugin.TestTools.createTempDirectory;
import static jetbrains.buildserver.sonarplugin.TestTools.writeTo;
import static org.assertj.core.api.Assertions.assertThat;
package jetbrains.buildserver.sonarplugin;





public class SSLToolsTest {
    @Test
    public void addOneCertificateToCacerts() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {
        String certificateContent = "-----BEGIN CERTIFICATE-----\n" +
                "MIIBaTCCAQ6gAwIBAgIBATAKBggqhkjOPQQDAjAvMS0wKwYDVQQDDCQ0NTJiNmMx\n" +
                "OS1lYjY4LTRlNzAtYjE3Ny0zZTU1M2U1YzZkMzYwHhcNMjExMTEyMjAwMDQ4WhcN\n" +
                "MjExMTEzMjAwMDQ4WjAvMS0wKwYDVQQDDCQ0NTJiNmMxOS1lYjY4LTRlNzAtYjE3\n" +
                "Ny0zZTU1M2U1YzZkMzYwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAStwNVdUQln\n" +
                "31aEA7C8pLHZqFF7eVrjruZSxlLiFGaszL5ht0IwP1C2xpV4zqn8W4if/FSjPUr9\n" +
                "cG+gjucetp1noxswGTAXBgNVHREBAf8EDTALgglsb2NhbGhvc3QwCgYIKoZIzj0E\n" +
                "AwIDSQAwRgIhANhI7Uq9V/l1bnNKunOhbYG9Yi6Td2abEZaI700gKUhqAiEA1prc\n" +
                "5nDPIDtcYOgQnxkPA0mrZ57oRPnjJ8/KDUyb6aE=\n" +
                "-----END CERTIFICATE-----\n";
// Path: sonar-plugin-agent/src/main/java/jetbrains/buildserver/sonarplugin/util/SSLTools.java
// public final class SSLTools {
//     private static final Logger log = LoggerFactory.getLogger(SQScannerArgsComposer.class);
// 
//     /**
//      * Returns path to temporary created keystore containing TC-trusted certificates with jdk-trusted ones.
//      *
//      * @param javaHome if null, this jvm keystore will be used
//      * @param teamcityCertificateFolderPath path to folder containing TC-trusted certificates
//      * @return path to created keystore
//      */
//     public static Path cloneKeyStoreWithTC(@Nullable String javaHome, String teamcityCertificateFolderPath) {
//         try {
//             KeyStore cacerts = getCacertsKeyStore(javaHome);
//             File f = File.createTempFile("tmpks", ".jks");
//             if (storeCertificates(cacerts, teamcityCertificateFolderPath) > 0) {
//                 try (FileOutputStream fos = new FileOutputStream(f)) {
//                     cacerts.store(fos, "changeit".toCharArray());
//                 }
//                 return f.toPath();
//             }
//             log.error("Error while using certificates from {}", teamcityCertificateFolderPath);
//         } catch (Exception e) {
//             log.error("Error while using certificates from {}", teamcityCertificateFolderPath, e);
//         }
//         return null;
//     }
// 
//     public static KeyStore getCacertsKeyStore(String javaHome) throws Exception {
//         File file = new File(getCacerts(javaHome));
//         if (!file.exists()) {
//             return null;
//         }
//         KeyStore instance = KeyStore.getInstance(KeyStore.getDefaultType());
//         try (InputStream in = new FileInputStream(file)) {
//             instance.load(in, "changeit".toCharArray());
//             return instance;
//         }
//     }
// 
//     public static String getCacerts(String javaHome) {
//         String sep = File.separator;
//         if (javaHome == null)
//             javaHome = System.getProperty("java.home");
//         return javaHome + sep
//                 + "lib" + sep + "security" + sep
//                 + "cacerts";
//     }
// 
//     private static int storeCertificates(KeyStore ks, String syncCertPath) {
//         Path certificateFolder = new File(syncCertPath).toPath();
//         AtomicInteger counter = new AtomicInteger(0);
//         try {
//             Files.walk(certificateFolder, 1)
//                     .filter(Files::isRegularFile)
//                     .map(it -> storeCertificate(ks, it))
//                     .filter(Objects::nonNull)
//                     .forEach(t -> counter.incrementAndGet());
//         } catch (IOException e) {
//             log.error("Error while copying certificates from {}", syncCertPath, e);
//         }
//         return counter.get();
//     }
// 
//     private static Path storeCertificate(KeyStore ks, Path certificatePath) {
//         try (FileInputStream fis = new FileInputStream(certificatePath.toFile())) {
//             Certificate c = CertificateFactory.getInstance("X.509").generateCertificate(fis);
//             ks.setCertificateEntry(certificatePath.toFile().getName(), c);
//             return certificatePath;
//         } catch (IOException | CertificateException | KeyStoreException e) {
//             log.error("Error while reading certificate from {}", certificatePath, e);
//         }
//         return null;
//     }
// 
// }
// 
// Path: sonar-plugin-agent/src/test/java/jetbrains/buildserver/sonarplugin/TestTools.java
// public static Path createTempDirectory() throws IOException {
//     return Files.createTempDirectory("keystoreTemp");
// }
// 
// Path: sonar-plugin-agent/src/test/java/jetbrains/buildserver/sonarplugin/TestTools.java
// public static File writeTo(String prefix, String content) {
//     return writeTo(prefix, content, ".tmp", null);
// }


// Path: sonar-plugin-agent/src/test/java/jetbrains/buildserver/sonarplugin/SSLToolsTest.java
import jetbrains.buildserver.sonarplugin.util.SSLTools;
import org.assertj.core.api.Assertions;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import static jetbrains.buildserver.sonarplugin.TestTools.createTempDirectory;
import static jetbrains.buildserver.sonarplugin.TestTools.writeTo;
import static org.assertj.core.api.Assertions.assertThat;

package jetbrains.buildserver.sonarplugin;





public class SSLToolsTest {
    @Test
    public void addOneCertificateToCacerts() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {
        String certificateContent = "-----BEGIN CERTIFICATE-----\n" +
                "MIIBaTCCAQ6gAwIBAgIBATAKBggqhkjOPQQDAjAvMS0wKwYDVQQDDCQ0NTJiNmMx\n" +
                "OS1lYjY4LTRlNzAtYjE3Ny0zZTU1M2U1YzZkMzYwHhcNMjExMTEyMjAwMDQ4WhcN\n" +
                "MjExMTEzMjAwMDQ4WjAvMS0wKwYDVQQDDCQ0NTJiNmMxOS1lYjY4LTRlNzAtYjE3\n" +
                "Ny0zZTU1M2U1YzZkMzYwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAStwNVdUQln\n" +
                "31aEA7C8pLHZqFF7eVrjruZSxlLiFGaszL5ht0IwP1C2xpV4zqn8W4if/FSjPUr9\n" +
                "cG+gjucetp1noxswGTAXBgNVHREBAf8EDTALgglsb2NhbGhvc3QwCgYIKoZIzj0E\n" +
                "AwIDSQAwRgIhANhI7Uq9V/l1bnNKunOhbYG9Yi6Td2abEZaI700gKUhqAiEA1prc\n" +
                "5nDPIDtcYOgQnxkPA0mrZ57oRPnjJ8/KDUyb6aE=\n" +
                "-----END CERTIFICATE-----\n";
        Path tmpDir = createTempDirectory();