Skip to content

Commit 0b684d5

Browse files
committed
misc - test coverage addition
1 parent 98d8c84 commit 0b684d5

File tree

11 files changed

+124
-56
lines changed

11 files changed

+124
-56
lines changed

src/main/java/org/mariadb/jdbc/Connection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ public void setCatalog(String catalog) throws SQLException {
288288
try {
289289
getContext().addStateFlag(ConnectionState.STATE_DATABASE);
290290
client.execute(new ChangeDbPacket(catalog));
291+
client.getContext().setDatabase(catalog);
291292
} finally {
292293
lock.unlock();
293294
}

src/main/java/org/mariadb/jdbc/client/ConnectionHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import org.mariadb.jdbc.util.ConfigurableSocketFactory;
5656
import org.mariadb.jdbc.util.constants.Capabilities;
5757

58-
public class ConnectionHelper {
58+
public final class ConnectionHelper {
5959

6060
private static final SocketHandlerFunction socketHandler;
6161

@@ -349,7 +349,7 @@ public static SSLSocket sslWrapper(
349349
.create(
350350
"SSL hostname verification failed : "
351351
+ ex.getMessage()
352-
+ "\nThis verification can be disabled using the option \"disableSslHostnameVerification\" "
352+
+ "\nThis verification can be disabled using the sslMode to VERIFY_CA "
353353
+ "but won't prevent man-in-the-middle attacks anymore",
354354
"08006");
355355
}

src/main/java/org/mariadb/jdbc/client/MultiPrimaryClient.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,7 @@ protected void reConnect() throws SQLException {
159159
syncNewState(oldClient);
160160

161161
if (conf.transactionReplay()) {
162-
if (executeTransactionReplay(oldClient)) {
163-
// transaction cannot be replayed, but connection is now up again.
164-
// changing exception to SQLTransientConnectionException
165-
throw new SQLTransientConnectionException(
166-
String.format(
167-
"Driver has reconnect connection after a "
168-
+ "communications "
169-
+ "link "
170-
+ "failure with %s, but wasn't able to replay transaction",
171-
oldClient.getHostAddress()),
172-
"25S03");
173-
}
162+
executeTransactionReplay(oldClient);
174163
} else if ((oldClient.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
175164
// transaction is lost, but connection is now up again.
176165
// changing exception to SQLTransientConnectionException
@@ -191,14 +180,12 @@ protected void reConnect() throws SQLException {
191180
}
192181
}
193182

194-
protected boolean executeTransactionReplay(Client oldCli) throws SQLException {
183+
protected void executeTransactionReplay(Client oldCli) throws SQLException {
195184
// transaction replay
196185
if ((oldCli.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
197186
RedoContext ctx = (RedoContext) oldCli.getContext();
198-
if (!ctx.getTransactionSaver().isCleanState()) return true;
199187
currentClient.transactionReplay(ctx.getTransactionSaver());
200188
}
201-
return false;
202189
}
203190

204191
public void syncNewState(Client oldCli) throws SQLException {

src/main/java/org/mariadb/jdbc/client/MultiPrimaryReplicaClient.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,7 @@ protected void reConnect() throws SQLException {
173173
// if reconnect primary, then replay transaction / throw exception if was in transaction.
174174
if (!requestReadOnly) {
175175
if (conf.transactionReplay()) {
176-
if (executeTransactionReplay(oldClient)) {
177-
// transaction cannot be replayed, but connection is now up again.
178-
// changing exception to SQLTransientConnectionException
179-
throw new SQLTransientConnectionException(
180-
String.format(
181-
"Driver has reconnect connection after a "
182-
+ "communications "
183-
+ "link "
184-
+ "failure with %s, but wasn't able to replay transaction",
185-
oldClient.getHostAddress()),
186-
"25S03");
187-
}
176+
executeTransactionReplay(oldClient);
188177
} else if ((oldClient.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
189178
// transaction is lost, but connection is now up again.
190179
// changing exception to SQLTransientConnectionException
@@ -295,6 +284,7 @@ public void setReadOnly(boolean readOnly) throws SQLException {
295284
if (!requestReadOnly) {
296285
if (replicaClient != null) {
297286
currentClient = replicaClient;
287+
syncNewState(primaryClient);
298288
} else if (nextTryReplica < System.currentTimeMillis()) {
299289
try {
300290
replicaClient = connectHost(true, true);
@@ -315,6 +305,7 @@ public void setReadOnly(boolean readOnly) throws SQLException {
315305
try {
316306
primaryClient = connectHost(false, false);
317307
nextTryPrimary = -1;
308+
syncNewState(replicaClient);
318309
} catch (SQLException e) {
319310
nextTryPrimary = System.currentTimeMillis() + waitTimeout;
320311
throw new SQLNonTransientConnectionException(

src/main/java/org/mariadb/jdbc/client/TransactionSaver.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,16 @@
66

77
public class TransactionSaver {
88
private final List<RedoableClientMessage> buffers = new ArrayList<>();
9-
private transient boolean cleanState = true;
109

1110
public void add(RedoableClientMessage clientMessage) {
1211
buffers.add(clientMessage);
1312
}
1413

15-
public void dirty() {
16-
buffers.clear();
17-
cleanState = false;
18-
}
19-
2014
public void clear() {
2115
buffers.clear();
22-
cleanState = true;
2316
}
2417

2518
public List<RedoableClientMessage> getBuffers() {
2619
return buffers;
2720
}
28-
29-
public boolean isCleanState() {
30-
return cleanState;
31-
}
3221
}

src/test/java/org/mariadb/jdbc/integration/ConnectionTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,5 +976,23 @@ public void socketFactoryTest() throws SQLException {
976976
try (Connection conn = createCon("socketFactory=" + SocketFactoryTest.class.getName())) {
977977
conn.isValid(1);
978978
}
979+
assertThrowsContains(
980+
SQLNonTransientConnectionException.class,
981+
() -> createCon("socketFactory=wrongClass"),
982+
"Socket factory failed to initialized with option \"socketFactory\" set to \"wrongClass\"");
983+
}
984+
985+
@Test
986+
public void socketOption() throws SQLException {
987+
try (Connection con = createCon("tcpKeepAlive=true&tcpAbortiveClose=true")) {
988+
con.isValid(1);
989+
}
990+
}
991+
992+
@Test
993+
public void sslNotSet() throws SQLException {
994+
Assumptions.assumeFalse(haveSsl());
995+
assertThrowsContains(
996+
SQLException.class, () -> createCon("sslMode=trust"), "ssl not enabled in the server");
979997
}
980998
}

src/test/java/org/mariadb/jdbc/integration/MultiHostTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ public void failoverReadonlyToMaster() throws Exception {
5555
}
5656
}
5757

58+
@Test
59+
public void syncState() throws Exception {
60+
61+
try (Connection con = createProxyConKeep("assureReadOnly=true")) {
62+
Statement stmt = con.createStatement();
63+
stmt.execute("CREATE DATABASE IF NOT EXISTS sync");
64+
con.setCatalog("sync");
65+
con.setTransactionIsolation(java.sql.Connection.TRANSACTION_SERIALIZABLE);
66+
con.setReadOnly(true);
67+
assertEquals("sync", con.getCatalog());
68+
assertEquals(java.sql.Connection.TRANSACTION_SERIALIZABLE, con.getTransactionIsolation());
69+
con.setReadOnly(true);
70+
con.setReadOnly(false);
71+
assertEquals(java.sql.Connection.TRANSACTION_SERIALIZABLE, con.getTransactionIsolation());
72+
con.setTransactionIsolation(java.sql.Connection.TRANSACTION_READ_COMMITTED);
73+
con.setReadOnly(true);
74+
assertEquals(java.sql.Connection.TRANSACTION_READ_COMMITTED, con.getTransactionIsolation());
75+
con.setTransactionIsolation(java.sql.Connection.TRANSACTION_READ_UNCOMMITTED);
76+
con.setReadOnly(false);
77+
assertEquals(java.sql.Connection.TRANSACTION_READ_UNCOMMITTED, con.getTransactionIsolation());
78+
con.setTransactionIsolation(java.sql.Connection.TRANSACTION_REPEATABLE_READ);
79+
con.setReadOnly(true);
80+
assertEquals(java.sql.Connection.TRANSACTION_REPEATABLE_READ, con.getTransactionIsolation());
81+
} finally {
82+
sharedConn.createStatement().execute("DROP DATABASE IF EXISTS sync");
83+
}
84+
}
85+
5886
@Test
5987
public void masterFailover() throws Exception {
6088
Configuration conf = Configuration.parse(mDefUrl);
@@ -69,7 +97,7 @@ public void masterFailover() throws Exception {
6997
mDefUrl.replaceAll(
7098
"//([^/]*)/",
7199
String.format(
72-
"//address=(host=localhost)(port=%s)(type=master),address=(host=%s)(port=%s)(type=master)/",
100+
"//address=(host=localhost)(port=9999)(type=master),address=(host=localhost)(port=%s)(type=master),address=(host=%s)(port=%s)(type=master)/",
73101
proxy.getLocalPort(), hostAddress.host, hostAddress.port));
74102
url = url.replaceAll("jdbc:mariadb:", "jdbc:mariadb:sequential:");
75103
if (conf.sslMode() == SslMode.VERIFY_FULL) {

src/test/java/org/mariadb/jdbc/integration/SslTest.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
import java.sql.*;
2929
import java.util.Locale;
3030
import org.junit.jupiter.api.*;
31-
import org.mariadb.jdbc.Common;
31+
import org.mariadb.jdbc.*;
3232
import org.mariadb.jdbc.Connection;
3333
import org.mariadb.jdbc.Statement;
34+
import org.mariadb.jdbc.integration.tools.TcpProxy;
3435

3536
@DisplayName("SSL tests")
3637
public class SslTest extends Common {
@@ -121,6 +122,42 @@ public void mandatorySsl() throws SQLException {
121122
() -> createCon(baseMutualOptions + "&sslMode=trust", sslPort));
122123
}
123124

125+
@Test
126+
public void enabledSslProtocolSuites() throws SQLException {
127+
Assumptions.assumeTrue(
128+
!"maxscale".equals(System.getenv("srv")) && !"skysql-ha".equals(System.getenv("srv")));
129+
try (Connection con =
130+
createCon(
131+
baseOptions + "&sslMode=trust&enabledSslProtocolSuites=TLSv1.2,TLSv1.3", sslPort)) {
132+
assertNotNull(getSslVersion(con));
133+
}
134+
assertThrowsContains(
135+
SQLNonTransientConnectionException.class,
136+
() ->
137+
createCon(baseMutualOptions + "&sslMode=trust&enabledSslProtocolSuites=SSLv3", sslPort),
138+
"No appropriate protocol");
139+
}
140+
141+
@Test
142+
public void enabledSslCipherSuites() throws SQLException {
143+
Assumptions.assumeTrue(
144+
!"maxscale".equals(System.getenv("srv")) && !"skysql-ha".equals(System.getenv("srv")));
145+
try (Connection con =
146+
createCon(
147+
baseOptions
148+
+ "&sslMode=trust&enabledSslCipherSuites=TLS_DHE_RSA_WITH_AES_256_CBC_SHA,TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
149+
sslPort)) {
150+
assertNotNull(getSslVersion(con));
151+
}
152+
assertThrowsContains(
153+
SQLException.class,
154+
() ->
155+
createCon(
156+
baseMutualOptions + "&sslMode=trust&enabledSslCipherSuites=UNKNOWN_CIPHER",
157+
sslPort),
158+
"Unsupported SSL cipher");
159+
}
160+
124161
@Test
125162
public void mutualAuthSsl() throws SQLException {
126163
Assumptions.assumeTrue(
@@ -206,6 +243,22 @@ public void certificateMandatorySsl() throws Throwable {
206243
baseOptions + "&sslMode=VERIFY_FULL&serverSslCert=" + serverCertPath, sslPort)) {
207244
assertNotNull(getSslVersion(con));
208245
}
246+
247+
Configuration conf = Configuration.parse(mDefUrl);
248+
HostAddress hostAddress = conf.addresses().get(0);
249+
try {
250+
proxy = new TcpProxy(hostAddress.host, sslPort == null ? hostAddress.port : sslPort);
251+
} catch (IOException i) {
252+
throw new SQLException("proxy error", i);
253+
}
254+
255+
String url = mDefUrl.replaceAll("//([^/]*)/", "//localhost:" + proxy.getLocalPort() + "/");
256+
assertThrowsContains(
257+
SQLException.class,
258+
() ->
259+
DriverManager.getConnection(
260+
url + "&sslMode=VERIFY_FULL&serverSslCert=" + serverCertPath),
261+
"DNS host \"localhost\" doesn't correspond to certificate");
209262
}
210263

211264
String urlPath = Paths.get(serverCertPath).toUri().toURL().toString();

src/test/java/org/mariadb/jdbc/unit/util/ConfigurationTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*
2020
*/
2121

22-
2322
package org.mariadb.jdbc.unit.util;
2423

2524
import static org.junit.jupiter.api.Assertions.*;

src/test/java/org/mariadb/jdbc/unit/util/constant/HaModeTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.jupiter.api.Assertions;
77
import org.junit.jupiter.api.Test;
88
import org.mariadb.jdbc.HostAddress;
9+
import org.mariadb.jdbc.client.ConnectionHelper;
910
import org.mariadb.jdbc.plugin.authentication.AuthenticationPluginLoader;
1011
import org.mariadb.jdbc.plugin.credential.CredentialPluginLoader;
1112
import org.mariadb.jdbc.plugin.tls.TlsSocketPluginLoader;
@@ -32,6 +33,7 @@ public void instantiateStaticOnlyClass() {
3233
AuthenticationPluginLoader ap = new AuthenticationPluginLoader();
3334
TlsSocketPluginLoader tp = new TlsSocketPluginLoader();
3435
LoggerHelper lh = new LoggerHelper();
36+
ConnectionHelper ch = new ConnectionHelper();
3537
}
3638

3739
@Test

0 commit comments

Comments
 (0)