Skip to content

Commit

Permalink
Add simple test to make sure we can load a key (#1588)
Browse files Browse the repository at this point in the history
* Add simple test to make sure we can load a key
  • Loading branch information
davecramer committed Oct 28, 2019
1 parent ad73457 commit 7c59126
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.junit.Assert;
import org.junit.Test;

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

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 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()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CommonNameVerifierTest.class,
LibPQFactoryHostNameTest.class,
SslTest.class,
LazyKeyManagerTest.class,
})
public class SslTestSuite {
}

0 comments on commit 7c59126

Please sign in to comment.