/*
 * Copyright (c) 2019, PostgreSQL Global Development Group
 * See the LICENSE file in the project root for more information.
 */

package org.postgresql.test.ssl;

import org.postgresql.ssl.LazyKeyManager;
import org.postgresql.ssl.PKCS12KeyManager;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

public class LazyKeyManagerTest {

  @Test
  public void testLoadP12Key() throws Exception {
    String certdir = "../certdir/";
    PKCS12KeyManager pkcs12KeyManager = new PKCS12KeyManager(certdir + "goodclient.p12", new TestCallbackHandler("sslpwd"));
    PrivateKey pk = pkcs12KeyManager.getPrivateKey("user");
    Assert.assertNotNull(pk);
    X509Certificate[] chain = pkcs12KeyManager.getCertificateChain("user");
    Assert.assertNotNull(chain);
  }

  @Test
  public void testLoadKey() throws Exception {
    String certdir = "../certdir/";
    String path = new File("./").getAbsolutePath();
    LazyKeyManager lazyKeyManager = new LazyKeyManager(certdir + "goodclient.crt",
        certdir + "goodclient.pk8", new TestCallbackHandler("sslpwd"), true);
    PrivateKey pk = lazyKeyManager.getPrivateKey("user");
    Assert.assertNotNull(pk);
  }

  public static class TestCallbackHandler implements CallbackHandler {
    char [] password;

    public TestCallbackHandler(String password) {
      if (password != null) {
        this.password = password.toCharArray();
      }
    }

    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
      for (Callback callback : callbacks) {
        if (!(callback instanceof PasswordCallback)) {
          throw new UnsupportedCallbackException(callback);
        }
        PasswordCallback pwdCallback = (PasswordCallback) callback;
        if (password != null) {
          pwdCallback.setPassword(password);
          continue;
        }
        // It is used instead of cons.readPassword(prompt), because the prompt may contain '%'
        // characters
        //pwdCallback.setPassword(cons.readPassword("%s", pwdCallback.getPrompt()));
      }
    }
  }
}

@@ -245,16 +245,53 @@
 Manager;
+%0Aimport org.postgresql.test.TestUtil;
 %0A%0Aimport
@@ -457,16 +457,45 @@
 ificate;
+%0Aimport java.util.Properties;
 %0A%0Aimport
@@ -792,32 +792,175 @@
 rows Exception %7B
+%0A%0A    Properties prop = TestUtil.loadPropertyFiles(%22ssltest.properties%22);%0A    File certDirFile = TestUtil.getFile(prop.getProperty(%22certdir%22));
 %0A    String cert
@@ -961,38 +961,55 @@
 g certdir = 
-%22../
 cert
-dir/%22;
+DirFile.getAbsolutePath();%0A
 %0A    PKCS12K
@@ -1061,32 +1061,33 @@
 ager(certdir + %22
+/
 goodclient.p12%22,
@@ -1386,42 +1386,149 @@
 n %7B%0A
+%0A
     
-String
+Properties prop = TestUtil.loadPropertyFiles(%22ssltest.properties%22);%0A    File
  cert
-dir = %22../
+DirFile = TestUtil.getFile(prop.getProperty(%22
 certdir
-/
 %22
+))
 ;%0A  
@@ -1540,29 +1540,29 @@
 ing 
-path = new File(%22./%22)
+certdir = certDirFile
 .get
@@ -1573,24 +1573,25 @@
 lutePath();%0A
+%0A
     LazyKeyM
@@ -1636,32 +1636,33 @@
 ager(certdir + %22
+/
 goodclient.crt%22,
@@ -1681,16 +1681,17 @@
 tdir + %22
+/
 goodclie

use TestUtil to find path of certdir (#1643)