Skip to content

Commit

Permalink
[test] ensure HostAddress data cannot be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Jun 19, 2024
1 parent 24dd582 commit 53145a7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 23 deletions.
26 changes: 26 additions & 0 deletions src/main/java/org/mariadb/jdbc/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,21 @@ private Configuration(
} else if (this.pipe != null) {
addresses.add(HostAddress.pipe(this.pipe));
}
} else {
if (this.localSocket != null) {
List<HostAddress> newAddresses = new ArrayList<>();
for (HostAddress host : addresses) {
newAddresses.add(host.withLocalSocket(this.localSocket));
}
this.addresses = newAddresses;
}
if (this.pipe != null) {
List<HostAddress> newAddresses = new ArrayList<>();
for (HostAddress host : addresses) {
newAddresses.add(host.withPipe(this.pipe));
}
this.addresses = newAddresses;
}
}

// *************************************************************
Expand Down Expand Up @@ -2397,6 +2412,17 @@ public Builder addresses(HostAddress... hostAddress) {
return this;
}

/**
* add host addresses
*
* @param hostAddress host addresses
* @return this {@link Builder}
*/
public Builder addresses(List<HostAddress> hostAddress) {
this._addresses.addAll(hostAddress);
return this;
}

/**
* Socket factory
*
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/org/mariadb/jdbc/HostAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public class HostAddress {
public final String host;

/** port */
public int port;
public final int port;

public String pipe;
public final String pipe;

public SslMode sslMode;
public String localSocket;
public final SslMode sslMode;
public final String localSocket;

/** primary node */
public Boolean primary;
Expand All @@ -37,13 +37,13 @@ public class HostAddress {
* @param primary is primary
*/
private HostAddress(
String host, int port, Boolean primary, String pipe, String localSocket, String sslMode) {
String host, int port, Boolean primary, String pipe, String localSocket, SslMode sslMode) {
this.host = host;
this.port = port;
this.primary = primary;
this.pipe = pipe;
this.localSocket = localSocket;
this.sslMode = sslMode == null ? null : SslMode.from(sslMode);
this.sslMode = sslMode;
}

/**
Expand Down Expand Up @@ -86,7 +86,8 @@ public static HostAddress from(String host, int port, boolean primary) {
* @return host
*/
public static HostAddress from(String host, int port, String sslMode) {
return new HostAddress(host, port, null, null, null, sslMode);
return new HostAddress(
host, port, null, null, null, sslMode == null ? null : SslMode.from(sslMode));
}

/**
Expand All @@ -99,7 +100,8 @@ public static HostAddress from(String host, int port, String sslMode) {
* @return host
*/
public static HostAddress from(String host, int port, boolean primary, String sslMode) {
return new HostAddress(host, port, primary, null, null, sslMode);
return new HostAddress(
host, port, primary, null, null, sslMode == null ? null : SslMode.from(sslMode));
}

/**
Expand Down Expand Up @@ -222,7 +224,8 @@ private static HostAddress parseParameterHostAddress(String str, HaMode haMode,
}
}

return new HostAddress(host, port, primary, pipe, localsocket, sslMode);
return new HostAddress(
host, port, primary, pipe, localsocket, sslMode == null ? null : SslMode.from(sslMode));
}

@Override
Expand Down Expand Up @@ -267,6 +270,21 @@ public void forceThreadsConnected(long threadsConnected, long threadConnectedTim
this.threadConnectedTimeout = threadConnectedTimeout;
}

public HostAddress withPipe(String pipe) {
return new HostAddress(
this.host, this.port, this.primary, pipe, this.localSocket, this.sslMode);
}

public HostAddress withLocalSocket(String localSocket) {
return new HostAddress(
this.host, this.port, this.primary, this.pipe, localSocket, this.sslMode);
}

public HostAddress withPort(int port) {
return new HostAddress(
this.host, port, this.primary, this.pipe, this.localSocket, this.sslMode);
}

public Long getThreadConnectedTimeout() {
return threadConnectedTimeout;
}
Expand Down
20 changes: 13 additions & 7 deletions src/test/java/org/mariadb/jdbc/integration/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.util.Locale;
import java.util.Optional;
import java.util.Properties;
import java.util.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.function.Executable;
Expand Down Expand Up @@ -223,9 +221,13 @@ public static Connection createCon(String option) throws SQLException {
public static Connection createCon(String option, Integer sslPort) throws SQLException {
Configuration conf = Configuration.parse(mDefUrl + "&" + option);
if (sslPort != null) {
for (HostAddress hostAddress : conf.addresses()) {
hostAddress.port = sslPort;
Configuration.Builder builder = conf.toBuilder();
List<HostAddress> newAddresses = new ArrayList<>();
for (HostAddress host : conf.addresses()) {
newAddresses.add(host.withPort(sslPort));
}
builder.addresses(newAddresses);
return Driver.connect(builder.build());
}
return Driver.connect(conf);
}
Expand All @@ -234,9 +236,13 @@ public static Connection createBasicCon(String option, Integer sslPort) throws S
Configuration conf =
Configuration.parse(mDefUrl.substring(0, mDefUrl.indexOf("?")) + "?" + option);
if (sslPort != null) {
for (HostAddress hostAddress : conf.addresses()) {
hostAddress.port = sslPort;
Configuration.Builder builder = conf.toBuilder();
List<HostAddress> newAddresses = new ArrayList<>();
for (HostAddress host : conf.addresses()) {
newAddresses.add(host.withPort(sslPort));
}
builder.addresses(newAddresses);
return Driver.connect(builder.build());
}
return Driver.connect(conf);
}
Expand Down
5 changes: 1 addition & 4 deletions src/test/java/org/mariadb/jdbc/integration/SslTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ void ensureUnixSocketSsl() throws SQLException {
+ ")(sslMode=verify-full)(type=primary)/");
try (Connection con = (Connection) DriverManager.getConnection(url)) {
assertNotNull(getSslVersion(con));
Assertions.assertEquals(
String.format(
"address=(host=%s)(port=%s)(sslMode=verify-full)(type=primary)", hostname, port),
con.__test_host());
Assertions.assertEquals("address=(localSocket=/run/mysqld/mysqld.sock)", con.__test_host());
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/mariadb/jdbc/integration/UnixsocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,15 @@ public void testConnectWithUnixSocketWhenDBNotUp() throws IOException {
+ " but ending with "
+ finalLines);
}

@Test
public void unixSocketErrorOnWindows() throws IOException {
Assumptions.assumeTrue(isWindows());
String url = mDefUrl + "&localSocket=/tmp/not_valid_socket&localSocketAddress=localhost";
java.sql.Driver driver = new org.mariadb.jdbc.Driver();
assertThrowsContains(
SQLNonTransientConnectionException.class,
() -> driver.connect(url, new Properties()),
"Unix domain sockets are not supported on Windows");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,6 @@ public void builder() throws SQLException {
.socketFactory("someSocketFactory")
.connectTimeout(22)
.restrictedAuth("mysql_native_password,client_ed25519")
.pipe("pipeName")
.localSocket("localSocket")
.tcpKeepAlive(false)
.uuidAsString(true)
.tcpAbortiveClose(true)
Expand Down Expand Up @@ -909,7 +907,7 @@ public void builder() throws SQLException {
.build();
String expected =
"jdbc:mariadb://host1:3305,address=(host=host2)(port=3307)(type=replica)/db?user=me&password=***&timezone=UTC&connectionTimeZone=SERVER&forceConnectionTimeZoneToSession=false&preserveInstants=true&autocommit=false&nullDatabaseMeansCurrent=true&useCatalogTerm=SCHEMA&createDatabaseIfNotExist=true&useLocalSessionState=true&returnMultiValuesGeneratedIds=true&permitRedirect=false&transactionIsolation=REPEATABLE_READ&defaultFetchSize=10&maxQuerySizeToLog=100&maxAllowedPacket=8000&geometryDefaultType=default&restrictedAuth=mysql_native_password,client_ed25519&initSql=SET"
+ " @@a='10'&socketFactory=someSocketFactory&connectTimeout=22&pipe=pipeName&localSocket=localSocket&uuidAsString=true&tcpKeepAlive=false&tcpKeepIdle=10&tcpKeepCount=50&tcpKeepInterval=50&tcpAbortiveClose=true&localSocketAddress=localSocketAddress&socketTimeout=1000&useReadAheadInput=true&tlsSocketType=TLStype&sslMode=TRUST&serverSslCert=mycertPath&keyStore=/tmp&keyStorePassword=MyPWD&keyStoreType=JKS&trustStoreType=JKS&enabledSslCipherSuites=myCipher,cipher2&enabledSslProtocolSuites=TLSv1.2&fallbackToSystemKeyStore=false&fallbackToSystemTrustStore=false&allowMultiQueries=true&allowLocalInfile=false&useCompression=true&useAffectedRows=true&useBulkStmts=true&disablePipeline=true&cachePrepStmts=false&prepStmtCacheSize=2&useServerPrepStmts=true&credentialType=ENV&sessionVariables=blabla&connectionAttributes=bla=bla&servicePrincipalName=SPN&blankTableNameMeta=true&tinyInt1isBit=false&yearIsDateType=false&dumpQueriesOnException=true&includeInnodbStatusInDeadlockExceptions=true&includeThreadDumpInDeadlockExceptions=true&retriesAllDown=10&galeraAllowedState=A,B&transactionReplay=true&pool=true&poolName=myPool&maxPoolSize=16&minPoolSize=12&maxIdleTime=25000&registerJmxPool=false&poolValidMinDelay=260&useResetConnection=true&serverRsaPublicKeyFile=RSAPath&allowPublicKeyRetrieval=true";
+ " @@a='10'&socketFactory=someSocketFactory&connectTimeout=22&uuidAsString=true&tcpKeepAlive=false&tcpKeepIdle=10&tcpKeepCount=50&tcpKeepInterval=50&tcpAbortiveClose=true&localSocketAddress=localSocketAddress&socketTimeout=1000&useReadAheadInput=true&tlsSocketType=TLStype&sslMode=TRUST&serverSslCert=mycertPath&keyStore=/tmp&keyStorePassword=MyPWD&keyStoreType=JKS&trustStoreType=JKS&enabledSslCipherSuites=myCipher,cipher2&enabledSslProtocolSuites=TLSv1.2&fallbackToSystemKeyStore=false&fallbackToSystemTrustStore=false&allowMultiQueries=true&allowLocalInfile=false&useCompression=true&useAffectedRows=true&useBulkStmts=true&disablePipeline=true&cachePrepStmts=false&prepStmtCacheSize=2&useServerPrepStmts=true&credentialType=ENV&sessionVariables=blabla&connectionAttributes=bla=bla&servicePrincipalName=SPN&blankTableNameMeta=true&tinyInt1isBit=false&yearIsDateType=false&dumpQueriesOnException=true&includeInnodbStatusInDeadlockExceptions=true&includeThreadDumpInDeadlockExceptions=true&retriesAllDown=10&galeraAllowedState=A,B&transactionReplay=true&pool=true&poolName=myPool&maxPoolSize=16&minPoolSize=12&maxIdleTime=25000&registerJmxPool=false&poolValidMinDelay=260&useResetConnection=true&serverRsaPublicKeyFile=RSAPath&allowPublicKeyRetrieval=true";
assertEquals(expected, conf.toString());
assertEquals(expected, conf.toBuilder().build().toString());
}
Expand Down

0 comments on commit 53145a7

Please sign in to comment.