use of com.mysql.cj.MysqlConnection in project JavaSegundasQuintas by ecteruel.
the class ConnectionRegressionTest method testBug88242.
/**
* Tests fix for Bug#88242 - autoReconnect and socketTimeout JDBC option makes wrong order of client packet.
*
* The wrong behavior may not be observed in all systems or configurations. It seems to be easier to reproduce when SSL is enabled. Without it, the data
* packets flow faster and desynchronization occurs rarely, which is the root cause for this problem.
*
* @throws Exception
*/
@Test
public void testBug88242() 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");
Properties props = new Properties();
props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "false");
props.setProperty(PropertyKey.autoReconnect.getKeyName(), "true");
props.setProperty(PropertyKey.socketTimeout.getKeyName(), "1500");
Connection testConn = getConnectionWithProps(props);
PreparedStatement ps = testConn.prepareStatement("SELECT ?, SLEEP(?)");
int key = 0;
for (int i = 0; i < 5; i++) {
// Execute a query that runs faster than the socket timeout limit.
ps.setInt(1, ++key);
ps.setInt(2, 0);
try {
ResultSet rset = ps.executeQuery();
assertTrue(rset.next());
assertEquals(key, rset.getInt(1));
} catch (SQLException e) {
e.printStackTrace();
fail("Exception [" + e.getClass().getName() + ": " + e.getMessage() + "] caught when no exception was expected.");
}
// Execute a query that runs slower than the socket timeout limit.
ps.setInt(1, ++key);
ps.setInt(2, 2);
final PreparedStatement localPstmt = ps;
assertThrows("Communications link failure.*", SQLException.class, new Callable<Void>() {
public Void call() throws Exception {
localPstmt.executeQuery();
return null;
}
});
}
testConn.close();
}
use of com.mysql.cj.MysqlConnection in project JavaSegundasQuintas by ecteruel.
the class ConnectionRegressionTest method testBug25545.
/**
* Tests fix for BUG#25545 - Client flags not sent correctly during handshake when using SSL.
*
* Requires test certificates from testsuite/ssl-test-certs to be installed on the server being tested.
*
* @throws Exception
*/
@Test
public void testBug25545() 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");
createProcedure("testBug25545", "() BEGIN SELECT 1; END");
String trustStorePath = "src/test/config/ssl-test-certs/ca-truststore";
System.setProperty("javax.net.ssl.keyStore", trustStorePath);
System.setProperty("javax.net.ssl.keyStorePassword", "password");
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
System.setProperty("javax.net.ssl.trustStorePassword", "password");
Connection sslConn = null;
try {
Properties props = new Properties();
props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
props.setProperty(PropertyKey.requireSSL.getKeyName(), "true");
sslConn = getConnectionWithProps(props);
sslConn.prepareCall("{ call testBug25545()}").execute();
} finally {
if (sslConn != null) {
sslConn.close();
}
}
}
use of com.mysql.cj.MysqlConnection in project JavaSegundasQuintas by ecteruel.
the class ConnectionRegressionTest method testBug27102307.
/**
* Tests fix for BUG#27102307, CHANGE USESSL AND VERIFYSERVERCERTIFICATE TO SSLMODE OPTION.
*
* @throws Exception
*/
@Test
public void testBug27102307() throws Exception {
assumeTrue(supportsTestCertificates(this.stmt), "This test requires the server configured with SSL certificates from ConnectorJ/src/test/config/ssl-test-certs");
// Basic SSL properties translation is tested in testBug21947042(). Testing only missing variants here.
System.setProperty("javax.net.ssl.trustStore", "");
System.setProperty("javax.net.ssl.trustStorePassword", "");
Connection sslConn = null;
Properties props = new Properties();
// 1. Explicit sslMode, no explicit useSSL
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.toString());
sslConn = getConnectionWithProps(props);
assertTrue(((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).isExplicitlySet());
assertEquals(SslMode.DISABLED, ((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).getValue());
assertFalse(((MysqlConnection) sslConn).getSession().isSSLEstablished());
sslConn.close();
// 2. Explicit sslMode, explicit useSSL=false
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.REQUIRED.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
sslConn = getConnectionWithProps(props);
assertTrue(((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).isExplicitlySet());
assertEquals(SslMode.REQUIRED, ((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).getValue());
assertTrue(((MysqlConnection) sslConn).getSession().isSSLEstablished());
sslConn.close();
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.REQUIRED.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "no");
sslConn = getConnectionWithProps(props);
assertTrue(((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).isExplicitlySet());
assertEquals(SslMode.REQUIRED, ((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).getValue());
assertTrue(((MysqlConnection) sslConn).getSession().isSSLEstablished());
sslConn.close();
// 3. Explicit sslMode, explicit useSSL=true
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
sslConn = getConnectionWithProps(props);
assertTrue(((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).isExplicitlySet());
assertEquals(SslMode.DISABLED, ((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).getValue());
assertFalse(((MysqlConnection) sslConn).getSession().isSSLEstablished());
sslConn.close();
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "yes");
sslConn = getConnectionWithProps(props);
assertTrue(((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).isExplicitlySet());
assertEquals(SslMode.DISABLED, ((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).getValue());
assertFalse(((MysqlConnection) sslConn).getSession().isSSLEstablished());
sslConn.close();
// 4. Explicit sslMode=REQUIRED, explicit useSSL=true, verifyServerCertificate=true, no trust store
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.REQUIRED.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "true");
sslConn = getConnectionWithProps(props);
assertTrue(((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).isExplicitlySet());
assertEquals(SslMode.REQUIRED, ((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).getValue());
assertTrue(((MysqlConnection) sslConn).getSession().isSSLEstablished());
sslConn.close();
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.REQUIRED.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "yes");
sslConn = getConnectionWithProps(props);
assertTrue(((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).isExplicitlySet());
assertEquals(SslMode.REQUIRED, ((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).getValue());
assertTrue(((MysqlConnection) sslConn).getSession().isSSLEstablished());
sslConn.close();
// 5. Explicit sslMode=VERIFY_CA, explicit useSSL=true, verifyServerCertificate=false, no trust store
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.VERIFY_CA.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "false");
assertThrows(SQLException.class, new Callable<Void>() {
public Void call() throws Exception {
getConnectionWithProps(props);
return null;
}
});
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.VERIFY_CA.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "no");
assertThrows(SQLException.class, new Callable<Void>() {
public Void call() throws Exception {
getConnectionWithProps(props);
return null;
}
});
// 5. Explicit sslMode=VERIFY_CA, explicit useSSL=false, verifyServerCertificate=false, with trust store
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.VERIFY_CA.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "false");
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");
sslConn = getConnectionWithProps(props);
assertTrue(((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).isExplicitlySet());
assertEquals(SslMode.VERIFY_CA, ((JdbcConnection) sslConn).getPropertySet().getEnumProperty(PropertyKey.sslMode).getValue());
assertTrue(((MysqlConnection) sslConn).getSession().isSSLEstablished());
sslConn.close();
// 6. Explicit sslMode=VERIFY_IDENTITY, explicit useSSL=true, verifyServerCertificate=false, no trust store
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.VERIFY_IDENTITY.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "true");
props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "false");
assertThrows(SQLException.class, new Callable<Void>() {
public Void call() throws Exception {
getConnectionWithProps(props);
return null;
}
});
// 7. Explicit sslMode=VERIFY_IDENTITY, explicit useSSL=false, verifyServerCertificate=false, with trust store
// The server certificate used in this test has "CN=MySQL Connector/J Server" and several SAN entries that don't match thus identity check failure
props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.VERIFY_IDENTITY.toString());
props.setProperty(PropertyKey.useSSL.getKeyName(), "false");
props.setProperty(PropertyKey.verifyServerCertificate.getKeyName(), "false");
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");
Throwable cause = null;
try {
sslConn = getConnectionWithProps(props);
fail("CertificateException expected but not thrown.");
} catch (Throwable t) {
cause = t.getCause();
while (cause != null && !(cause instanceof CertificateException)) {
cause = cause.getCause();
}
assertTrue(cause != null && cause instanceof CertificateException, "CertificateException expected");
String errMsg = cause.getMessage();
if (errMsg.startsWith("java.security.cert.CertificateException: ")) {
errMsg = errMsg.substring("java.security.cert.CertificateException: ".length());
}
assertEquals("Server identity verification failed. None of the DNS or IP Subject Alternative Name " + "entries matched the server hostname/IP '" + getHostFromTestsuiteUrl() + "'.", errMsg);
}
}
use of com.mysql.cj.MysqlConnection in project JavaSegundasQuintas by ecteruel.
the class ConnectionRegressionTest method testBug45419.
/**
* Tests fix for BUG#45419, ensure that time is not converted to seconds before being reported as milliseconds.
*
* @throws Exception
*/
@Test
public void testBug45419() throws Exception {
Exception e = null;
String msg = ExceptionFactory.createLinkFailureMessageBasedOnHeuristics(((MysqlConnection) this.conn).getPropertySet(), ((MysqlConnection) this.conn).getSession().getServerSession(), new PacketSentTimeHolder() {
@Override
public long getPreviousPacketSentTime() {
return System.currentTimeMillis() - 1000;
}
@Override
public long getLastPacketSentTime() {
return System.currentTimeMillis() - 1000;
}
}, new PacketReceivedTimeHolder() {
@Override
public long getLastPacketReceivedTime() {
return System.currentTimeMillis() - 2000;
}
}, e);
Matcher m = Pattern.compile("\\d.?\\d{3}", Pattern.MULTILINE).matcher(msg);
assertTrue(m.find());
assertTrue(Long.parseLong(m.group().replaceAll("[^\\d]", "")) >= 2000);
assertTrue(m.find());
assertTrue(Long.parseLong(m.group().replaceAll("[^\\d]", "")) >= 1000);
}
use of com.mysql.cj.MysqlConnection in project JavaSegundasQuintas by ecteruel.
the class ConnectionRegressionTest method testAuthCleartextPlugin.
@Test
public void testAuthCleartextPlugin() throws Exception {
assumeTrue(versionMeetsMinimum(5, 5, 7), "MySQL 5.5.7+ is required to run this test.");
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");
boolean installPluginInRuntime = false;
try {
// Install plugin if required.
this.rs = this.stmt.executeQuery("SELECT (PLUGIN_LIBRARY LIKE 'auth_test_plugin%') as `TRUE` FROM INFORMATION_SCHEMA.PLUGINS " + "WHERE PLUGIN_NAME='cleartext_plugin_server'");
if (this.rs.next()) {
installPluginInRuntime = !this.rs.getBoolean(1);
} else {
installPluginInRuntime = true;
}
if (installPluginInRuntime) {
try {
String ext = isServerRunningOnWindows() ? ".dll" : ".so";
this.stmt.executeUpdate("INSTALL PLUGIN cleartext_plugin_server SONAME 'auth_test_plugin" + ext + "'");
} catch (SQLException e) {
if (e.getErrorCode() == MysqlErrorNumbers.ER_CANT_OPEN_LIBRARY) {
// to disable plugin deinstallation attempt in a finally block
installPluginInRuntime = false;
assumeTrue(false, "This test requires the server installed with the test package.");
} else {
throw e;
}
}
}
String dbname = getPropertiesFromTestsuiteUrl().getProperty(PropertyKey.DBNAME.getKeyName());
assertFalse(StringUtils.isNullOrEmpty(dbname), "No database selected");
createUser("'wl5735user'@'%'", "identified WITH cleartext_plugin_server AS ''");
this.stmt.executeUpdate("DELETE FROM mysql.db WHERE user='wl5735user'");
this.stmt.executeUpdate("INSERT INTO mysql.db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, " + "Drop_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Create_tmp_table_priv, Lock_tables_priv, Create_view_priv, " + "Show_view_priv, Create_routine_priv, Alter_routine_priv, Execute_priv, Event_priv, Trigger_priv) VALUES ('%', '" + dbname + "', 'wl5735user', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'N', 'N')");
this.stmt.executeUpdate("FLUSH PRIVILEGES");
Properties props = new Properties();
props.setProperty(PropertyKey.USER.getKeyName(), "wl5735user");
props.setProperty(PropertyKey.PASSWORD.getKeyName(), "");
props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
assertThrows(SQLException.class, "SSL connection required for plugin \"mysql_clear_password\"\\. Check if 'sslMode' is enabled\\.", () -> getConnectionWithProps(props));
String trustStorePath = "src/test/config/ssl-test-certs/ca-truststore";
System.setProperty("javax.net.ssl.trustStore", trustStorePath);
System.setProperty("javax.net.ssl.trustStorePassword", "password");
props.setProperty(PropertyKey.sslMode.getKeyName(), "REQUIRED");
try (Connection testConn = getConnectionWithProps(props)) {
assertTrue(((MysqlConnection) testConn).getSession().isSSLEstablished(), "SSL connection isn't actually established!");
Statement testSt = testConn.createStatement();
ResultSet testRs = testSt.executeQuery("SELECT USER(), CURRENT_USER()");
assertTrue(testRs.next());
assertEquals("wl5735user", testRs.getString(1).split("@")[0]);
assertEquals("wl5735user", testRs.getString(2).split("@")[0]);
}
} finally {
if (installPluginInRuntime) {
this.stmt.executeUpdate("UNINSTALL PLUGIN cleartext_plugin_server");
}
}
}
Aggregations