Search in sources :

Example 21 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class ConnectionRegressionTest method testEnableTLSVersion.

/**
 * Tests fix for Bug#87379. This allows TLS version to be overridden through a new configuration option - enabledTLSProtocols. When set to some combination
 * of TLSv1, TLSv1.1, or TLSv1.2 (comma-separated, no spaces), the default behavior restricting the TLS version based on JRE and MySQL Server version is
 * bypassed to enable or restrict specific TLS versions.
 *
 * @throws Exception
 */
@Test
public void testEnableTLSVersion() throws Exception {
    assumeTrue((((MysqlConnection) this.conn).getSession().getServerSession().getCapabilities().getCapabilityFlags() & NativeServerSession.CLIENT_SSL) != 0, "This test requires server with SSL support.");
    assumeTrue(supportsTestCertificates(this.stmt), "This test requires the server configured with SSL certificates from ConnectorJ/src/test/config/ssl-test-certs");
    // Find out which TLS protocol versions are supported by this JVM.
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, null, null);
    List<String> jvmSupportedProtocols = Arrays.asList(sslContext.createSSLEngine().getSupportedProtocols());
    Properties props = new Properties();
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
    props.setProperty(PropertyKey.requireSSL.getKeyName(), "true");
    props.setProperty(PropertyKey.trustCertificateKeyStoreUrl.getKeyName(), "file:src/test/config/ssl-test-certs/ca-truststore");
    props.setProperty(PropertyKey.trustCertificateKeyStoreType.getKeyName(), "JKS");
    props.setProperty(PropertyKey.trustCertificateKeyStorePassword.getKeyName(), "password");
    System.out.println(dbUrl);
    System.out.println("JVM version: " + System.getProperty(PropertyDefinitions.SYSP_java_version));
    System.out.println("JVM supports TLS protocols: " + jvmSupportedProtocols);
    Connection sslConn = getConnectionWithProps(dbUrl, props);
    assertTrue(((MysqlConnection) sslConn).getSession().isSSLEstablished());
    System.out.println("MySQL version: " + ((MysqlConnection) sslConn).getSession().getServerSession().getServerVersion());
    List<String> commonSupportedProtocols = new ArrayList<>();
    if (((JdbcConnection) sslConn).getSession().versionMeetsMinimum(5, 7, 10)) {
        this.rs = sslConn.createStatement().executeQuery("SHOW GLOBAL VARIABLES LIKE 'tls_version'");
        assertTrue(this.rs.next());
        List<String> serverSupportedProtocols = Arrays.asList(this.rs.getString(2).trim().split("\\s*,\\s*"));
        System.out.println("Server supports TLS protocols: " + serverSupportedProtocols);
        commonSupportedProtocols.addAll(serverSupportedProtocols);
        commonSupportedProtocols.retainAll(jvmSupportedProtocols);
    } else if (((JdbcConnection) sslConn).getSession().versionMeetsMinimum(5, 6, 46)) {
        commonSupportedProtocols.add("TLSv1");
        commonSupportedProtocols.add("TLSv1.1");
        commonSupportedProtocols.add("TLSv1.2");
    } else {
        commonSupportedProtocols.add("TLSv1");
    }
    String[] testingProtocols = { "TLSv1.2", "TLSv1.1", "TLSv1" };
    for (String protocol : testingProtocols) {
        Properties testProps = new Properties();
        testProps.putAll(props);
        testProps.setProperty(PropertyKey.enabledTLSProtocols.getKeyName(), protocol);
        System.out.println("Testing " + protocol + " expecting connection: " + commonSupportedProtocols.contains(protocol));
        try {
            Connection tlsConn = getConnectionWithProps(dbUrl, testProps);
            assertTrue(commonSupportedProtocols.contains(protocol), "Expected to fail connection with " + protocol + " due to lack of jvm/server support.");
            ResultSet rset = tlsConn.createStatement().executeQuery("SHOW STATUS LIKE 'ssl_version'");
            assertTrue(rset.next());
            String tlsVersion = rset.getString(2);
            assertEquals(protocol, tlsVersion);
            tlsConn.close();
        } catch (Exception e) {
            if (commonSupportedProtocols.contains(protocol)) {
                e.printStackTrace();
                fail("Expected to be able to connect with " + protocol + " protocol, but failed.");
            }
        }
    }
    System.out.println();
    sslConn.close();
}
Also used : ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) MysqlConnection(com.mysql.cj.MysqlConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) SSLContext(javax.net.ssl.SSLContext) Properties(java.util.Properties) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLTransientException(java.sql.SQLTransientException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XAException(javax.transaction.xa.XAException) SocketException(java.net.SocketException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) PasswordExpiredException(com.mysql.cj.exceptions.PasswordExpiredException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) CommunicationsException(com.mysql.cj.jdbc.exceptions.CommunicationsException) CertificateException(java.security.cert.CertificateException) ClosedOnExpiredPasswordException(com.mysql.cj.exceptions.ClosedOnExpiredPasswordException) PropertyNotModifiableException(com.mysql.cj.exceptions.PropertyNotModifiableException) Test(org.junit.jupiter.api.Test)

Example 22 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class MetaDataRegressionTest method testBug17248345.

/**
 * Tests fix for BUG#17248345 - GETFUNCTIONCOLUMNS() METHOD RETURNS COLUMNS OF PROCEDURE. (this happens when functions and procedures have a common name)
 *
 * @throws Exception
 */
@Test
public void testBug17248345() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.nullDatabaseMeansCurrent.getKeyName(), "true");
    Connection testConn;
    // create one stored procedure and one function with same name
    createProcedure("testBug17248345", "(IN proccol INT) SELECT 1");
    createFunction("testBug17248345", "(funccol INT) RETURNS INT DETERMINISTIC RETURN 1");
    // test with standard connection (getProceduresReturnsFunctions=true & useInformationSchema=false)
    props.setProperty(PropertyKey.useInformationSchema.getKeyName(), "false");
    testConn = getConnectionWithProps(props);
    assertFalse(((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.useInformationSchema).getValue(), "Property useInformationSchema should be false");
    assertTrue(((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.getProceduresReturnsFunctions).getValue(), "Property getProceduresReturnsFunctions should be true");
    checkMetaDataInfoForBug17248345(testConn);
    testConn.close();
    // test with property useInformationSchema=true (getProceduresReturnsFunctions=true)
    props.setProperty(PropertyKey.useInformationSchema.getKeyName(), "true");
    testConn = getConnectionWithProps(props);
    assertTrue(((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.useInformationSchema).getValue(), "Property useInformationSchema should be true");
    assertTrue(((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.getProceduresReturnsFunctions).getValue(), "Property getProceduresReturnsFunctions should be true");
    checkMetaDataInfoForBug17248345(testConn);
    testConn.close();
    // test with property getProceduresReturnsFunctions=false (useInformationSchema=false)
    props.setProperty(PropertyKey.useInformationSchema.getKeyName(), "false");
    props.setProperty(PropertyKey.getProceduresReturnsFunctions.getKeyName(), "false");
    testConn = getConnectionWithProps(props);
    assertFalse(((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.useInformationSchema).getValue(), "Property useInformationSchema should be false");
    assertFalse(((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.getProceduresReturnsFunctions).getValue(), "Property getProceduresReturnsFunctions should be false");
    checkMetaDataInfoForBug17248345(testConn);
    testConn.close();
    // test with property useInformationSchema=true & getProceduresReturnsFunctions=false
    props.setProperty(PropertyKey.useInformationSchema.getKeyName(), "true");
    props.setProperty(PropertyKey.getProceduresReturnsFunctions.getKeyName(), "false");
    testConn = getConnectionWithProps(props);
    assertTrue(((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.useInformationSchema).getValue(), "Property useInformationSchema should be true");
    assertFalse(((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.getProceduresReturnsFunctions).getValue(), "Property getProceduresReturnsFunctions should be false");
    checkMetaDataInfoForBug17248345(testConn);
    testConn.close();
}
Also used : Connection(java.sql.Connection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 23 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class MetaDataRegressionTest method testBug61332.

/**
 * Tests fix for BUG#61332 - Check if "LIKE" or "=" is sent to server in I__S query when no wildcards are supplied for schema parameter.
 *
 * @throws Exception
 */
@Test
public void testBug61332() throws Exception {
    Properties props = new Properties();
    props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
    props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
    props.setProperty(PropertyKey.useInformationSchema.getKeyName(), "true");
    props.setProperty(PropertyKey.queryInterceptors.getKeyName(), QueryInterceptorBug61332.class.getName());
    createDatabase("dbbug61332");
    Connection testConn = getConnectionWithProps(props);
    try {
        createTable("dbbug61332.bug61332", "(c1 char(1))");
        DatabaseMetaData metaData = testConn.getMetaData();
        this.rs = ((JdbcConnection) this.conn).getPropertySet().<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue() == DatabaseTerm.SCHEMA ? metaData.getColumns(null, "dbbug61332", "bug61332", null) : metaData.getColumns("dbbug61332", null, "bug61332", null);
        this.rs.next();
    } finally {
    }
}
Also used : DatabaseTerm(com.mysql.cj.conf.PropertyDefinitions.DatabaseTerm) Connection(java.sql.Connection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) Properties(java.util.Properties) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.jupiter.api.Test)

Example 24 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class MetaDataRegressionTest method checkGetProceduresForBug69298.

private void checkGetProceduresForBug69298(String stepDescription, Connection testConn) throws Exception {
    DatabaseMetaData testDbMetaData = testConn.getMetaData();
    boolean dbMapsToSchema = ((JdbcConnection) testConn).getPropertySet().<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue() == DatabaseTerm.SCHEMA;
    ResultSet proceduresMD = testDbMetaData.getProcedures(null, null, "testBug69298_%");
    String sd = stepDescription + " getProcedures() ";
    boolean isGetProceduresReturnsFunctions = ((JdbcConnection) testConn).getPropertySet().getBooleanProperty(PropertyKey.getProceduresReturnsFunctions).getValue();
    if (isGetProceduresReturnsFunctions) {
        assertTrue(proceduresMD.next(), sd + "1st of 2 rows expected.");
        // function: testBug69298_func
        assertEquals(dbMapsToSchema ? "def" : testConn.getCatalog(), proceduresMD.getString("PROCEDURE_CAT"), sd + "-> PROCEDURE_CAT");
        assertEquals(dbMapsToSchema ? testConn.getSchema() : null, proceduresMD.getString("PROCEDURE_SCHEM"), sd + "-> PROCEDURE_SCHEM");
        assertEquals("testBug69298_func", proceduresMD.getString("PROCEDURE_NAME"), sd + "-> PROCEDURE_NAME");
        assertEquals("testBug69298_func comment", proceduresMD.getString("REMARKS"), sd + "-> REMARKS");
        assertEquals(DatabaseMetaData.procedureReturnsResult, proceduresMD.getShort("PROCEDURE_TYPE"), sd + "-> PROCEDURE_TYPE");
        assertEquals("testBug69298_func", proceduresMD.getString("SPECIFIC_NAME"), sd + "-> SPECIFIC_NAME");
        assertTrue(proceduresMD.next(), sd + "2nd of 2 rows expected.");
    } else {
        assertTrue(proceduresMD.next(), sd + "one row expected.");
    }
    // procedure: testBug69298_proc
    assertEquals(dbMapsToSchema ? "def" : testConn.getCatalog(), proceduresMD.getString("PROCEDURE_CAT"), sd + "-> PROCEDURE_CAT");
    assertEquals(dbMapsToSchema ? testConn.getSchema() : null, proceduresMD.getString("PROCEDURE_SCHEM"), sd + "-> PROCEDURE_SCHEM");
    assertEquals("testBug69298_proc", proceduresMD.getString("PROCEDURE_NAME"), sd + "-> PROCEDURE_NAME");
    assertEquals("testBug69298_proc comment", proceduresMD.getString("REMARKS"), sd + "-> REMARKS");
    assertEquals(DatabaseMetaData.procedureNoResult, proceduresMD.getShort("PROCEDURE_TYPE"), sd + "-> PROCEDURE_TYPE");
    assertEquals("testBug69298_proc", proceduresMD.getString("SPECIFIC_NAME"), sd + "-> SPECIFIC_NAME");
    assertFalse(proceduresMD.next(), stepDescription + "no more rows expected.");
}
Also used : DatabaseTerm(com.mysql.cj.conf.PropertyDefinitions.DatabaseTerm) ResultSet(java.sql.ResultSet) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 25 with JdbcConnection

use of com.mysql.cj.jdbc.JdbcConnection in project JavaSegundasQuintas by ecteruel.

the class MetaDataRegressionTest method testBug20504139.

/**
 * Tests fix for BUG#20504139 - GETFUNCTIONCOLUMNS() AND GETPROCEDURECOLUMNS() RETURNS ERROR FOR VALID INPUTS.
 *
 * @throws Exception
 */
@Test
public void testBug20504139() throws Exception {
    createFunction("testBug20504139f", "(namef CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN CONCAT('Hello, ', namef, '!')");
    createFunction("`testBug20504139``f`", "(namef CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN CONCAT('Hello, ', namef, '!')");
    createProcedure("testBug20504139p", "(INOUT namep CHAR(50)) SELECT  CONCAT('Hello, ', namep, '!') INTO namep");
    createProcedure("`testBug20504139``p`", "(INOUT namep CHAR(50)) SELECT  CONCAT('Hello, ', namep, '!') INTO namep");
    for (int testCase = 0; testCase < 8; testCase++) {
        // 3 props, 8 combinations: 2^3 = 8
        boolean usePedantic = (testCase & 1) == 1;
        boolean useInformationSchema = (testCase & 2) == 2;
        boolean useFuncsInProcs = (testCase & 4) == 4;
        String connProps = String.format("pedantic=%s,useInformationSchema=%s,getProceduresReturnsFunctions=%s", usePedantic, useInformationSchema, useFuncsInProcs);
        System.out.printf("testBug20504139_%d: %s%n", testCase, connProps);
        Properties props = new Properties();
        props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
        props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
        props.setProperty(PropertyKey.pedantic.getKeyName(), "" + usePedantic);
        props.setProperty(PropertyKey.useInformationSchema.getKeyName(), "" + useInformationSchema);
        props.setProperty(PropertyKey.getProceduresReturnsFunctions.getKeyName(), "" + useFuncsInProcs);
        Connection testConn = getConnectionWithProps(props);
        DatabaseMetaData dbmd = testConn.getMetaData();
        boolean dbMapsToSchema = ((JdbcConnection) testConn).getPropertySet().<DatabaseTerm>getEnumProperty(PropertyKey.databaseTerm).getValue() == DatabaseTerm.SCHEMA;
        ResultSet testRs = null;
        try {
            /*
                 * test DatabaseMetadata.getProcedureColumns for function
                 */
            int i = 1;
            try {
                for (String name : new String[] { "testBug20504139f", "testBug20504139`f" }) {
                    testRs = dbMapsToSchema ? dbmd.getProcedureColumns("", this.dbName, name, "%") : dbmd.getProcedureColumns(this.dbName, "", name, "%");
                    if (useFuncsInProcs) {
                        assertTrue(testRs.next());
                        assertEquals("", testRs.getString(4), testCase + "." + i + ". expected function column name (empty)");
                        assertEquals(DatabaseMetaData.procedureColumnReturn, testRs.getInt(5), testCase + "." + i + ". expected function column type (empty)");
                        assertTrue(testRs.next());
                        assertEquals("namef", testRs.getString(4), testCase + "." + i + ". expected function column name");
                        assertEquals(DatabaseMetaData.procedureColumnIn, testRs.getInt(5), testCase + "." + i + ". expected function column type (empty)");
                        assertFalse(testRs.next());
                    } else {
                        assertFalse(testRs.next());
                    }
                    testRs.close();
                    i++;
                }
            } catch (SQLException e) {
                assertFalse(e.getMessage().matches("FUNCTION `testBug20504139(:?`{2})?[fp]` does not exist"), testCase + "." + i + ". failed to retrieve function columns, with getProcedureColumns(), from database meta data.");
                throw e;
            }
            /*
                 * test DatabaseMetadata.getProcedureColumns for procedure
                 */
            i = 1;
            try {
                for (String name : new String[] { "testBug20504139p", "testBug20504139`p" }) {
                    testRs = dbMapsToSchema ? dbmd.getProcedureColumns("", this.dbName, name, "%") : dbmd.getProcedureColumns(this.dbName, "", name, "%");
                    assertTrue(testRs.next());
                    assertEquals("namep", testRs.getString(4), testCase + ". expected procedure column name");
                    assertEquals(DatabaseMetaData.procedureColumnInOut, testRs.getInt(5), testCase + ". expected procedure column type (empty)");
                    assertFalse(testRs.next());
                    testRs.close();
                    i++;
                }
            } catch (SQLException e) {
                assertFalse(e.getMessage().matches("PROCEDURE `testBug20504139(:?`{2})?[fp]` does not exist"), testCase + "." + i + ". failed to retrieve prodedure columns, with getProcedureColumns(), from database meta data.");
                throw e;
            }
            /*
                 * test DatabaseMetadata.getFunctionColumns for function
                 */
            i = 1;
            try {
                for (String name : new String[] { "testBug20504139f", "testBug20504139`f" }) {
                    testRs = dbMapsToSchema ? dbmd.getFunctionColumns("", this.dbName, name, "%") : dbmd.getFunctionColumns(this.dbName, "", name, "%");
                    assertTrue(testRs.next());
                    assertEquals("", testRs.getString(4), testCase + ". expected function column name (empty)");
                    assertEquals(DatabaseMetaData.functionReturn, testRs.getInt(5), testCase + ". expected function column type (empty)");
                    assertTrue(testRs.next());
                    assertEquals("namef", testRs.getString(4), testCase + ". expected function column name");
                    assertEquals(DatabaseMetaData.functionColumnIn, testRs.getInt(5), testCase + ". expected function column type (empty)");
                    assertFalse(testRs.next());
                    testRs.close();
                    i++;
                }
            } catch (SQLException e) {
                assertFalse(e.getMessage().matches("FUNCTION `testBug20504139(:?`{2})?[fp]` does not exist"), testCase + "." + i + ". failed to retrieve function columns, with getFunctionColumns(), from database meta data.");
                throw e;
            }
            /*
                 * test DatabaseMetadata.getFunctionColumns for procedure
                 */
            i = 1;
            try {
                for (String name : new String[] { "testBug20504139p", "testBug20504139`p" }) {
                    testRs = dbMapsToSchema ? dbmd.getFunctionColumns("", this.dbName, name, "%") : dbmd.getFunctionColumns(this.dbName, "", name, "%");
                    assertFalse(testRs.next());
                    testRs.close();
                    i++;
                }
            } catch (SQLException e) {
                assertFalse(e.getMessage().matches("PROCEDURE `testBug20504139(:?`{2})?[fp]` does not exist"), testCase + "." + i + ". failed to retrieve procedure columns, with getFunctionColumns(), from database meta data.");
                throw e;
            }
        } finally {
            testConn.close();
        }
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseTerm(com.mysql.cj.conf.PropertyDefinitions.DatabaseTerm) Connection(java.sql.Connection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)280 Properties (java.util.Properties)216 Test (org.junit.jupiter.api.Test)207 Connection (java.sql.Connection)175 MysqlConnection (com.mysql.cj.MysqlConnection)149 XAConnection (javax.sql.XAConnection)100 SQLException (java.sql.SQLException)97 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)96 PooledConnection (javax.sql.PooledConnection)90 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)81 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)79 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)78 Statement (java.sql.Statement)77 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)66 PreparedStatement (java.sql.PreparedStatement)66 ResultSet (java.sql.ResultSet)65 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)60 CommunicationsException (com.mysql.cj.jdbc.exceptions.CommunicationsException)60 IOException (java.io.IOException)54 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)45