Permalink
Browse files

test: add basic tests for SSPI authentication

closes #557
  • Loading branch information...
chrullrich authored and vlsi committed May 10, 2016
1 parent d8117d1 commit 16c27b9e31546f4ebdc23deeb0178ffce9cc2537
@@ -9,6 +9,7 @@ username=test
password=test
privilegedUser=postgres
privilegedPassword=
sspiusername=testsspi
preparethreshold=5
loglevel=0
protocolVersion=0
@@ -133,6 +133,9 @@
<exclude>org/postgresql/sspi/NTDSAPIWrapper.java</exclude>
<exclude>org/postgresql/sspi/SSPIClient.java</exclude>
</excludes>
<testExcludes>
<exclude>org/postgresql/test/sspi/*.java</exclude>
</testExcludes>
</configuration>
</plugin>
</plugins>
@@ -91,6 +91,7 @@ public int hashCode() {
public final static PSQLState TRANSACTION_STATE_INVALID = new PSQLState("25000");
public final static PSQLState ACTIVE_SQL_TRANSACTION = new PSQLState("25001");
public final static PSQLState NO_ACTIVE_SQL_TRANSACTION = new PSQLState("25P01");
public final static PSQLState INVALID_AUTHORIZATION_SPECIFICATION = new PSQLState("28000");
public final static PSQLState STATEMENT_NOT_ALLOWED_IN_FUNCTION_CALL = new PSQLState("2F003");
@@ -120,6 +120,13 @@ public static String getPassword() {
return System.getProperty("password");
}
/*
* Returns the user for SSPI authentication tests
*/
public static String getSSPIUser() {
return System.getProperty("sspiusername");
}
/*
* postgres like user
*/
@@ -266,7 +273,11 @@ public static File getFile(String name) {
public static java.sql.Connection openDB(Properties props) throws Exception {
initDriver();
String user = getUser();
// Allow properties to override the user name.
String user = props.getProperty("username");
if (user == null) {
user = getUser();
}
if (user == null) {
throw new IllegalArgumentException(
"user name is not specified. Please specify 'username' property via -D or build.properties");
@@ -0,0 +1,71 @@
package org.postgresql.test.sspi;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeThat;
import org.postgresql.test.TestUtil;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;
/*
* These tests require a working SSPI authentication setup
* in the database server that allows the executing user
* to authenticate as the "sspiusername" in the build
* configuration.
*/
public class SSPITest {
/*
* SSPI only exists on Windows.
*/
@BeforeClass
public static void checkPlatform() {
assumeThat("SSPI not supported on this platform",
System.getProperty("os.name").toLowerCase(),
containsString("windows"));
}
/*
* Tests that SSPI login succeeds and a query can be run.
*/
@Test
public void testAuthorized() throws Exception {
Properties props = new Properties();
props.setProperty("username", TestUtil.getSSPIUser());
Connection con = TestUtil.openDB(props);
Statement stmt = con.createStatement();
stmt.executeQuery("SELECT 1");
TestUtil.closeDB(con);
}
/*
* Tests that SSPI login fails with an unknown/unauthorized
* user name.
*/
@Test
public void testUnauthorized() throws Exception {
Properties props = new Properties();
props.setProperty("username", "invalid" + TestUtil.getSSPIUser());
try {
Connection con = TestUtil.openDB(props);
TestUtil.closeDB(con);
fail("Expected a PSQLException");
} catch (PSQLException e) {
assertThat(e.getSQLState(), is(PSQLState.INVALID_AUTHORIZATION_SPECIFICATION.getState()));
}
}
}
@@ -0,0 +1,13 @@
package org.postgresql.test.sspi;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/*
* Executes all known tests for SSPI.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({ SSPITest.class })
public class SSPITestSuite {
// Empty.
}

0 comments on commit 16c27b9

Please sign in to comment.