Skip to content

Commit fd38a07

Browse files
committed
extra tests
1 parent dddbec3 commit fd38a07

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,8 @@ public Connection getConnection(InterpreterContext context)
596596
return connection;
597597
}
598598

599-
private void validateConnectionUrl(String url) {
599+
// package private for testing purposes
600+
static void validateConnectionUrl(String url) {
600601
final String decodedUrl = urlDecode(url, url, 0);
601602
final Map<String, String> params = parseUrlParameters(decodedUrl);
602603

@@ -655,7 +656,7 @@ private static Map<String, String> parseUrlParameters(final String url) {
655656
if (parts.length > 1) {
656657
// The first part is the base URL, so we start from the second part
657658
for (int i = 1; i < parts.length; i++) {
658-
splitNameValue(parts[i], parameters);
659+
splitNameValue(parts[i], parameters, true);
659660
}
660661
}
661662
return parameters;
@@ -691,7 +692,7 @@ private static int extractFromParens(final String input,
691692
String params = input.substring(startIndex + 1, endIndex);
692693
String[] keyValuePairs = params.split(",");
693694
for (String pair : keyValuePairs) {
694-
splitNameValue(pair, parameters);
695+
splitNameValue(pair, parameters, false);
695696
}
696697
}
697698
return endIndex;
@@ -703,12 +704,14 @@ private static int extractFromParens(final String input,
703704
*
704705
* @param nameValue the name-value pair as a string
705706
* @param parameters the map to store the extracted key-value pair
707+
* @param allowEmptyValue whether to allow empty values
706708
*/
707-
private static void splitNameValue(String nameValue, Map<String, String> parameters) {
709+
private static void splitNameValue(String nameValue, Map<String, String> parameters,
710+
boolean allowEmptyValue) {
708711
String[] keyValue = nameValue.split("=");
709712
if (keyValue.length >= 2) {
710713
parameters.put(keyValue[0].trim(), keyValue[1].trim());
711-
} else {
714+
} else if (allowEmptyValue) {
712715
// Handle cases where there might not be a value
713716
parameters.put(keyValue[0].trim(), "");
714717
}

jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@
5858
import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_URL;
5959
import static org.apache.zeppelin.jdbc.JDBCInterpreter.DEFAULT_USER;
6060
import static org.apache.zeppelin.jdbc.JDBCInterpreter.PRECODE_KEY_TEMPLATE;
61-
62-
import static org.junit.jupiter.api.Assertions.assertTrue;
63-
import static org.junit.jupiter.api.Assertions.assertFalse;
64-
import static org.junit.jupiter.api.Assertions.fail;
65-
import static org.junit.jupiter.api.Assertions.assertEquals;
61+
import static org.junit.jupiter.api.Assertions.*;
6662

6763

6864
/**
@@ -784,11 +780,24 @@ void testValidateConnectionMySQLProps() throws IOException, InterpreterException
784780

785781
@Test
786782
void testValidateConnectionMySQLProps2() throws IOException, InterpreterException {
783+
testBannedURL("com.mysql.cj.jdbc.Driver",
784+
"jdbc:mysql://[(host=myhost,port=1111,allowLoadLocalInfile=true),(host=myhost2)]/db");
785+
}
786+
787+
@Test
788+
void testValidateConnectionMySQLProps3() throws IOException, InterpreterException {
787789
testBannedURL("com.mysql.cj.jdbc.Driver",
788790
"jdbc:mysql://address=(host=172.18.0.1)(port=3309)" +
789791
"(%2561llowLoadLocalInfile=true),localhost:3306/test");
790792
}
791793

794+
@Test
795+
void testValidateConnectionMySQLWeirdPassword() throws IOException, InterpreterException {
796+
// we strongly discourage putting passwords in the URL
797+
assertDoesNotThrow(() -> JDBCInterpreter.validateConnectionUrl
798+
("jdbc:mysql://localhost:3306/test?user=xyz&password=(allowLoadLocalInfile)"));
799+
}
800+
792801
private void testBannedH2QueryParam(String param) throws IOException, InterpreterException {
793802
testBannedURL("org.h2.Driver", getJdbcConnection() + ";" + param);
794803
}

0 commit comments

Comments
 (0)