|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, PostgreSQL Global Development Group |
| 3 | + * See the LICENSE file in the project root for more information. |
| 4 | + */ |
| 5 | + |
| 6 | +package org.postgresql.test.util; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 11 | + |
| 12 | +import org.postgresql.PGProperty; |
| 13 | +import org.postgresql.jdbc.SslMode; |
| 14 | +import org.postgresql.test.TestUtil; |
| 15 | +import org.postgresql.util.ObjectFactory; |
| 16 | +import org.postgresql.util.PSQLState; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.Assertions; |
| 20 | +import org.opentest4j.MultipleFailuresError; |
| 21 | + |
| 22 | +import java.sql.SQLException; |
| 23 | +import java.util.Properties; |
| 24 | + |
| 25 | +import javax.net.SocketFactory; |
| 26 | + |
| 27 | +public class ObjectFactoryTest { |
| 28 | + Properties props = new Properties(); |
| 29 | + |
| 30 | + static class BadObject { |
| 31 | + static boolean wasInstantiated = false; |
| 32 | + |
| 33 | + BadObject() { |
| 34 | + wasInstantiated = true; |
| 35 | + throw new RuntimeException("I should not be instantiated"); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private void testInvalidInstantiation(PGProperty prop, PSQLState expectedSqlState) { |
| 40 | + prop.set(props, BadObject.class.getName()); |
| 41 | + |
| 42 | + BadObject.wasInstantiated = false; |
| 43 | + SQLException ex = assertThrows(SQLException.class, () -> { |
| 44 | + TestUtil.openDB(props); |
| 45 | + }); |
| 46 | + |
| 47 | + try { |
| 48 | + Assertions.assertAll( |
| 49 | + () -> assertFalse(BadObject.wasInstantiated, "ObjectFactory should not have " |
| 50 | + + "instantiated bad object for " + prop), |
| 51 | + () -> assertEquals(expectedSqlState.getState(), ex.getSQLState(), () -> "#getSQLState()"), |
| 52 | + () -> { |
| 53 | + assertThrows( |
| 54 | + ClassCastException.class, |
| 55 | + () -> { |
| 56 | + throw ex.getCause(); |
| 57 | + }, |
| 58 | + () -> "Wrong class specified for " + prop.name() |
| 59 | + + " => ClassCastException is expected in SQLException#getCause()" |
| 60 | + ); |
| 61 | + } |
| 62 | + ); |
| 63 | + } catch (MultipleFailuresError e) { |
| 64 | + // Add the original exception so it is easier to understand the reason for the test to fail |
| 65 | + e.addSuppressed(ex); |
| 66 | + throw e; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testInvalidSocketFactory() { |
| 72 | + testInvalidInstantiation(PGProperty.SOCKET_FACTORY, PSQLState.CONNECTION_FAILURE); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testInvalidSSLFactory() { |
| 77 | + TestUtil.assumeSslTestsEnabled(); |
| 78 | + // We need at least "require" to trigger SslSockerFactory instantiation |
| 79 | + PGProperty.SSL_MODE.set(props, SslMode.REQUIRE.value); |
| 80 | + testInvalidInstantiation(PGProperty.SSL_FACTORY, PSQLState.CONNECTION_FAILURE); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testInvalidAuthenticationPlugin() { |
| 85 | + testInvalidInstantiation(PGProperty.AUTHENTICATION_PLUGIN_CLASS_NAME, |
| 86 | + PSQLState.INVALID_PARAMETER_VALUE); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testInvalidSslHostnameVerifier() { |
| 91 | + TestUtil.assumeSslTestsEnabled(); |
| 92 | + // Hostname verification is done at verify-full level only |
| 93 | + PGProperty.SSL_MODE.set(props, SslMode.VERIFY_FULL.value); |
| 94 | + PGProperty.SSL_ROOT_CERT.set(props, TestUtil.getSslTestCertPath("goodroot.crt")); |
| 95 | + testInvalidInstantiation(PGProperty.SSL_HOSTNAME_VERIFIER, PSQLState.CONNECTION_FAILURE); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testInstantiateInvalidSocketFactory() { |
| 100 | + Properties props = new Properties(); |
| 101 | + assertThrows(ClassCastException.class, () -> { |
| 102 | + ObjectFactory.instantiate(SocketFactory.class, BadObject.class.getName(), props, |
| 103 | + false, null); |
| 104 | + }); |
| 105 | + } |
| 106 | +} |
0 commit comments