Skip to content

Commit 6f303ed

Browse files
committed
Fix invalid connection strings test
1 parent 66c1231 commit 6f303ed

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

src/main/java/io/github/utplsql/cli/ConnectionInfo.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public ConnectionInfo parseConnectionString(String connectionString)
4747
private void parseCredentials(String str) throws ParameterException, IllegalArgumentException {
4848
int barIdx = str.indexOf("/");
4949

50-
if (barIdx == -1 || str.length() == 1)
50+
if (barIdx == -1 || barIdx == 0 || barIdx == str.length() - 1)
5151
throw invalidConnectionString();
5252

5353
this.setUser(str.substring(0, barIdx));
@@ -61,22 +61,21 @@ private void parseHost(String str) throws ParameterException, IllegalArgumentExc
6161
int colonIdx = str.indexOf(":");
6262
int barIdx = str.indexOf("/");
6363

64-
if (colonIdx != -1 && barIdx != -1) {
64+
if ((colonIdx != -1 && barIdx == -1) || barIdx == 0) // @host:port or // @/db
65+
throw invalidConnectionString();
66+
67+
if (colonIdx != -1) { // @host:port/db
6568
setHost(str.substring(0, colonIdx));
6669
setPort(Integer.parseInt(str.substring(colonIdx + 1, barIdx)));
6770
setDatabase(str.substring(barIdx + 1));
6871
}
6972
else
70-
if (colonIdx == -1 && barIdx != -1) {
73+
if (barIdx != -1) { // @host/db
7174
setHost(str.substring(0, barIdx));
7275
setPort(DEFAULT_PORT);
7376
setDatabase(str.substring(barIdx + 1));
7477
}
75-
else
76-
if (colonIdx != -1) {
77-
throw invalidConnectionString();
78-
}
79-
else {
78+
else { // @db
8079
setHost(DEFAULT_HOST);
8180
setPort(DEFAULT_PORT);
8281
setDatabase(str);

src/test/java/io/github/utplsql/cli/ConnectionInfoTest.java

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ConnectionInfoTest {
1717
*/
1818

1919
@Test
20-
public void connectionStr_Full() {
20+
public void valid_Full() {
2121
try {
2222
ConnectionInfo ci = new ConnectionInfo()
2323
.parseConnectionString("my_user/p@ss!@some.server.123-abc.com:3000/db_1.acme.com");
@@ -34,7 +34,7 @@ public void connectionStr_Full() {
3434
}
3535

3636
@Test
37-
public void connectionStr_WithoutPort() {
37+
public void valid_WithoutPort() {
3838
try {
3939
ConnectionInfo ci = new ConnectionInfo()
4040
.parseConnectionString("my_user/p@ss!@some.server.123-abc.com/db_1.acme.com");
@@ -51,7 +51,7 @@ public void connectionStr_WithoutPort() {
5151
}
5252

5353
@Test
54-
public void connectionStr_WithoutHostAndPort() {
54+
public void valid_WithoutHostAndPort() {
5555
try {
5656
ConnectionInfo ci = new ConnectionInfo()
5757
.parseConnectionString("my_user/p@ss!@127.0.0.1/db_1.acme.com");
@@ -68,14 +68,64 @@ public void connectionStr_WithoutHostAndPort() {
6868
}
6969

7070
@Test
71-
public void connectionStr_Invalid() {
71+
public void invalid_WithoutDatabase_1() {
7272
try {
7373
new ConnectionInfo().parseConnectionString("user/pass@");
74+
Assert.fail();
75+
} catch (ParameterException ignored) {}
76+
}
77+
78+
@Test
79+
public void invalid_WithoutDatabase_2() {
80+
try {
7481
new ConnectionInfo().parseConnectionString("user/pass");
75-
new ConnectionInfo().parseConnectionString("user/@");
76-
new ConnectionInfo().parseConnectionString("/pass@");
77-
new ConnectionInfo().parseConnectionString("/@");
78-
new ConnectionInfo().parseConnectionString("@");
82+
Assert.fail();
83+
} catch (ParameterException ignored) {}
84+
}
85+
86+
@Test
87+
public void invalid_WithoutDatabase_3() {
88+
try {
89+
new ConnectionInfo().parseConnectionString("user/pass@localhost:1521");
90+
Assert.fail();
91+
} catch (ParameterException ignored) {}
92+
}
93+
94+
@Test
95+
public void invalid_WithoutHost() {
96+
try {
97+
new ConnectionInfo().parseConnectionString("user/pass@/db");
98+
Assert.fail();
99+
} catch (ParameterException ignored) {}
100+
}
101+
102+
@Test
103+
public void invalid_WithoutPassword() {
104+
try {
105+
new ConnectionInfo().parseConnectionString("user/@db");
106+
Assert.fail();
107+
} catch (ParameterException ignored) {}
108+
}
109+
110+
@Test
111+
public void invalid_WithoutUsername() {
112+
try {
113+
new ConnectionInfo().parseConnectionString("/pass@db");
114+
Assert.fail();
115+
} catch (ParameterException ignored) {}
116+
}
117+
118+
@Test
119+
public void invalid_WithoutUserPassDb_1() {
120+
try {
121+
new ConnectionInfo().parseConnectionString("/@db");
122+
Assert.fail();
123+
} catch (ParameterException ignored) {}
124+
}
125+
126+
@Test
127+
public void invalid_WithoutUserPass() {
128+
try {
79129
new ConnectionInfo().parseConnectionString("@db");
80130
Assert.fail();
81131
} catch (ParameterException ignored) {}

0 commit comments

Comments
 (0)