import java.io.File;
import java.security.KeyStore;

public class KeyStoreCustomLoadParamBug {
    private static final String INVALID_PASSWORD = "invalidpassword";
    private static final String KS_FILENAME = "jck_keystore.pkcs12";
    private static String TEST_DIR = "";


    public static void main(String[] args) throws Exception {
        if (args.length != 2){
            throw new IllegalArgumentException("Please provide the directory of the pkcs12 keystore file");
        }
        TEST_DIR = args[0];
        String runWithLoad = args[1];

        if( !(runWithLoad.equals("load") || runWithLoad.equals("noload")) ) {
            throw new IllegalArgumentException("Illegal argument, please pass either \"load\" or \"noload\" ");
        }



        KeyStore.PasswordProtection protection = new KeyStore.PasswordProtection(INVALID_PASSWORD.toCharArray());
        MyLoadStoreParameter lsp = new MyLoadStoreParameter(protection);
        final File ksf = new File(TEST_DIR + File.separator + KS_FILENAME);
        System.out.println("KeyStore is: "+ksf);

        if(runWithLoad.equals("load")){
            KeyStore.getInstance(ksf, lsp);
            System.err.println("Running KeyStore#getInstance(File,LoadStoreParameter) does not throw IOException "
                      + "when given a wrong password");
        }else{
            KeyStore.getInstance(ksf,INVALID_PASSWORD.toCharArray());
        }

    }

    static class MyLoadStoreParameter implements KeyStore.LoadStoreParameter {
        private KeyStore.ProtectionParameter protection;


        MyLoadStoreParameter(KeyStore.ProtectionParameter protection) {
            this.protection = protection;
        }


        public KeyStore.ProtectionParameter getProtectionParameter() {
            return protection;
        }
    }
}
