diff --git a/build.properties b/build.properties index e23f96a..14c0e9e 100644 --- a/build.properties +++ b/build.properties @@ -9,6 +9,7 @@ username=test password=test privilegedUser=postgres privilegedPassword= +sspiusername=testsspi preparethreshold=5 loglevel=0 protocolVersion=0 diff --git a/pgjdbc/pom.xml b/pgjdbc/pom.xml index dab5308..2e5ab56 100644 --- a/pgjdbc/pom.xml +++ b/pgjdbc/pom.xml @@ -133,6 +133,9 @@ org/postgresql/sspi/NTDSAPIWrapper.java org/postgresql/sspi/SSPIClient.java + + org/postgresql/test/sspi/*.java + diff --git a/pgjdbc/src/main/java/org/postgresql/util/PSQLState.java b/pgjdbc/src/main/java/org/postgresql/util/PSQLState.java index 21bfc82..3b40229 100644 --- a/pgjdbc/src/main/java/org/postgresql/util/PSQLState.java +++ b/pgjdbc/src/main/java/org/postgresql/util/PSQLState.java @@ -91,6 +91,7 @@ public class PSQLState implements java.io.Serializable { 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"); diff --git a/pgjdbc/src/test/java/org/postgresql/test/TestUtil.java b/pgjdbc/src/test/java/org/postgresql/test/TestUtil.java index 2e12cc8..417be9c 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/TestUtil.java +++ b/pgjdbc/src/test/java/org/postgresql/test/TestUtil.java @@ -121,6 +121,13 @@ public class TestUtil { } /* + * Returns the user for SSPI authentication tests + */ + public static String getSSPIUser() { + return System.getProperty("sspiusername"); + } + + /* * postgres like user */ public static String getPrivilegedUser() { @@ -266,7 +273,11 @@ public class TestUtil { 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"); diff --git a/pgjdbc/src/test/java/org/postgresql/test/sspi/SSPITest.java b/pgjdbc/src/test/java/org/postgresql/test/sspi/SSPITest.java new file mode 100644 index 0000000..9940fe2 --- /dev/null +++ b/pgjdbc/src/test/java/org/postgresql/test/sspi/SSPITest.java @@ -0,0 +1,53 @@ +package org.postgresql.test.sspi; + +import org.postgresql.test.TestUtil; +import org.postgresql.util.PSQLException; +import org.postgresql.util.PSQLState; + +import junit.framework.TestCase; + +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 extends TestCase { + + /* + * Tests that SSPI login succeeds and a query can be run. + */ + 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. + */ + 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) { + assertEquals(PSQLState.INVALID_AUTHORIZATION_SPECIFICATION.getState(), e.getSQLState()); + } + } + +} + diff --git a/pgjdbc/src/test/java/org/postgresql/test/sspi/SSPITestSuite.java b/pgjdbc/src/test/java/org/postgresql/test/sspi/SSPITestSuite.java new file mode 100644 index 0000000..6f4c706 --- /dev/null +++ b/pgjdbc/src/test/java/org/postgresql/test/sspi/SSPITestSuite.java @@ -0,0 +1,33 @@ +/*------------------------------------------------------------------------- +* +* Copyright (c) 2004-2014, PostgreSQL Global Development Group +* +* +*------------------------------------------------------------------------- +*/ + +package org.postgresql.test.sspi; + +import junit.framework.JUnit4TestAdapter; +import junit.framework.TestSuite; + +/* + * Executes all known tests for SSPI and includes some utility methods. + */ +public class SSPITestSuite extends TestSuite { + + /* + * The main entry point for JUnit + */ + public static TestSuite suite() throws Exception { + TestSuite suite = new TestSuite(); + + // + // Add one line per class in our test cases. These should be in order of + // complexity. + suite.addTestSuite(SSPITest.class); + + // That's all folks + return suite; + } +}