// Path: src/main/java/com/sonymobile/tools/gerrit/gerritevents/ssh/SshUtil.java
// public final class SshUtil {
// 
//     /**
//      * Default hidden constructor.
//      */
//     private SshUtil() {
//     }
// 
//     /**
//      * Does a series of checks in the file to see if it is a valid private key file.
//      * @param keyFile the file
//      * @return true if valid.
//      */
//     public static boolean isPrivateKeyFileValid(File keyFile) {
//         return parsePrivateKeyFile(keyFile) != null;
//     }
// 
//     /**
//      * Parses the keyFile hiding any Exceptions that might occur.
//      * @param keyFile the file.
//      * @return the "parsed" file.
//      */
//     private static KeyPair parsePrivateKeyFile(File keyFile) {
//         if (keyFile == null) {
//           return null;
//         }
// 
//         try {
//             JSch jsch = new JSch();
//             KeyPair key = KeyPair.load(jsch, keyFile.getAbsolutePath());
//             return key;
//         } catch (JSchException ex) {
//             return null;
//         }
//     }
// 
//     /**
//      * Checks to see if the passPhrase is valid for the private key file.
//      * @param keyFilePath the private key file.
//      * @param passPhrase the password for the file.
//      * @return true if it is valid.
//      */
//     public static boolean checkPassPhrase(File keyFilePath, String passPhrase) {
//         KeyPair key = parsePrivateKeyFile(keyFilePath);
//         boolean isValidPhrase = passPhrase != null && !passPhrase.trim().isEmpty();
//         if (key == null) {
//             return false;
//         } else if (key.isEncrypted() != isValidPhrase) {
//             return false;
//         } else if (key.isEncrypted()) {
//             return key.decrypt(passPhrase);
//         }
//         return true;
//     }
// }


import java.io.File;
import java.net.URL;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.sonymobile.tools.gerrit.gerritevents.ssh.SshUtil;
package com.sonymobile.tools.gerrit.gerritevents;





/**
 * Test that SSH keys are correctly handled for keys with and without passphrases
 * Created by svanoort on 9/11/15.
 */
public class SshUtilTest {

    static final String PASSPHRASE = "letmein";

    /**
     * Test basic key file loading.
     * @throws Exception If test fails.
     */
    @Test
    public void testBasicKeyFileLoading() throws Exception {

        boolean tested = SshUtil.isPrivateKeyFileValid(null);
        assertFalse("Key file validation failed with null key file", tested);

        tested = SshUtil.isPrivateKeyFileValid(new File("missing_file"));
        assertFalse("Key file validation failed with missing key file", tested);

        URL url = Thread.currentThread().getContextClassLoader().getResource(
                "com/sonymobile/tools/gerrit/gerritevents/id_rsa_corrupted");
        tested = SshUtil.isPrivateKeyFileValid(new File(url.getPath()));
        assertFalse("Key file validation failed with invalid key file", tested);

        url = Thread.currentThread().getContextClassLoader().getResource(
                "com/sonymobile/tools/gerrit/gerritevents/id_rsa");
        tested = SshUtil.isPrivateKeyFileValid(new File(url.getPath()));
        assertTrue("Key file validation pass with vallid key file", tested);
    }

    /**
     * Test handling of keys with no passphrase set.
     * @throws Exception If test fails.
     */
    @Test
    public void testInvalidKeyFile() throws Exception {
        File file = new File("missing_file");

        boolean tested = SshUtil.checkPassPhrase(file, null);
        assertFalse("Passphrase validation failed with missing key file and null passphrase", tested);

        tested = SshUtil.checkPassPhrase(file, "");
        assertFalse("Passphrase validation failed with missing key file and empty passphrase", tested);

// Path: src/main/java/com/sonymobile/tools/gerrit/gerritevents/ssh/SshUtil.java
// public final class SshUtil {
// 
//     /**
//      * Default hidden constructor.
//      */
//     private SshUtil() {
//     }
// 
//     /**
//      * Does a series of checks in the file to see if it is a valid private key file.
//      * @param keyFile the file
//      * @return true if valid.
//      */
//     public static boolean isPrivateKeyFileValid(File keyFile) {
//         return parsePrivateKeyFile(keyFile) != null;
//     }
// 
//     /**
//      * Parses the keyFile hiding any Exceptions that might occur.
//      * @param keyFile the file.
//      * @return the "parsed" file.
//      */
//     private static KeyPair parsePrivateKeyFile(File keyFile) {
//         if (keyFile == null) {
//           return null;
//         }
// 
//         try {
//             JSch jsch = new JSch();
//             KeyPair key = KeyPair.load(jsch, keyFile.getAbsolutePath());
//             return key;
//         } catch (JSchException ex) {
//             return null;
//         }
//     }
// 
//     /**
//      * Checks to see if the passPhrase is valid for the private key file.
//      * @param keyFilePath the private key file.
//      * @param passPhrase the password for the file.
//      * @return true if it is valid.
//      */
//     public static boolean checkPassPhrase(File keyFilePath, String passPhrase) {
//         KeyPair key = parsePrivateKeyFile(keyFilePath);
//         boolean isValidPhrase = passPhrase != null && !passPhrase.trim().isEmpty();
//         if (key == null) {
//             return false;
//         } else if (key.isEncrypted() != isValidPhrase) {
//             return false;
//         } else if (key.isEncrypted()) {
//             return key.decrypt(passPhrase);
//         }
//         return true;
//     }
// }


// Path: src/test/java/com/sonymobile/tools/gerrit/gerritevents/SshUtilTest.java
import java.io.File;
import java.net.URL;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.sonymobile.tools.gerrit.gerritevents.ssh.SshUtil;

package com.sonymobile.tools.gerrit.gerritevents;





/**
 * Test that SSH keys are correctly handled for keys with and without passphrases
 * Created by svanoort on 9/11/15.
 */
public class SshUtilTest {

    static final String PASSPHRASE = "letmein";

    /**
     * Test basic key file loading.
     * @throws Exception If test fails.
     */
    @Test
    public void testBasicKeyFileLoading() throws Exception {

        boolean tested = SshUtil.isPrivateKeyFileValid(null);
        assertFalse("Key file validation failed with null key file", tested);

        tested = SshUtil.isPrivateKeyFileValid(new File("missing_file"));
        assertFalse("Key file validation failed with missing key file", tested);

        URL url = Thread.currentThread().getContextClassLoader().getResource(
                "com/sonymobile/tools/gerrit/gerritevents/id_rsa_corrupted");
        tested = SshUtil.isPrivateKeyFileValid(new File(url.getPath()));
        assertFalse("Key file validation failed with invalid key file", tested);

        url = Thread.currentThread().getContextClassLoader().getResource(
                "com/sonymobile/tools/gerrit/gerritevents/id_rsa");
        tested = SshUtil.isPrivateKeyFileValid(new File(url.getPath()));
        assertTrue("Key file validation pass with vallid key file", tested);
    }

    /**
     * Test handling of keys with no passphrase set.
     * @throws Exception If test fails.
     */
    @Test
    public void testInvalidKeyFile() throws Exception {
        File file = new File("missing_file");

        boolean tested = SshUtil.checkPassPhrase(file, null);
        assertFalse("Passphrase validation failed with missing key file and null passphrase", tested);

        tested = SshUtil.checkPassPhrase(file, "");
        assertFalse("Passphrase validation failed with missing key file and empty passphrase", tested);

        tested = SshUtil.checkPassPhrase(file, "nope");