提交 723696ee authored 作者: Thomas Mueller's avatar Thomas Mueller

Improved tests

上级 7740acbb
...@@ -175,6 +175,15 @@ public class ConnectionInfo implements Cloneable { ...@@ -175,6 +175,15 @@ public class ConnectionInfo implements Cloneable {
} }
String normalizedName = FileUtils.unwrap(FileUtils.toRealPath(n)); String normalizedName = FileUtils.unwrap(FileUtils.toRealPath(n));
if (normalizedName.equals(absDir) || !normalizedName.startsWith(absDir)) { if (normalizedName.equals(absDir) || !normalizedName.startsWith(absDir)) {
// database name matches the baseDir or
// database name is clearly outside of the baseDir
throw DbException.get(ErrorCode.IO_EXCEPTION_1, normalizedName + " outside " +
absDir);
}
if (normalizedName.charAt(absDir.length()) != '/') {
// database must be within the directory
// (with baseDir=/test, the database name must not be
// /test2/x and not /test2)
throw DbException.get(ErrorCode.IO_EXCEPTION_1, normalizedName + " outside " + throw DbException.get(ErrorCode.IO_EXCEPTION_1, normalizedName + " outside " +
absDir); absDir);
} }
......
...@@ -119,12 +119,6 @@ public class TcpServerThread implements Runnable { ...@@ -119,12 +119,6 @@ public class TcpServerThread implements Runnable {
} }
db = server.checkKeyAndGetDatabaseName(db); db = server.checkKeyAndGetDatabaseName(db);
ConnectionInfo ci = new ConnectionInfo(db); ConnectionInfo ci = new ConnectionInfo(db);
if (baseDir != null) {
ci.setBaseDir(baseDir);
}
if (server.getIfExists()) {
ci.setProperty("IFEXISTS", "TRUE");
}
ci.setOriginalURL(originalURL); ci.setOriginalURL(originalURL);
ci.setUserName(transfer.readString()); ci.setUserName(transfer.readString());
ci.setUserPasswordHash(transfer.readBytes()); ci.setUserPasswordHash(transfer.readBytes());
...@@ -133,6 +127,13 @@ public class TcpServerThread implements Runnable { ...@@ -133,6 +127,13 @@ public class TcpServerThread implements Runnable {
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
ci.setProperty(transfer.readString(), transfer.readString()); ci.setProperty(transfer.readString(), transfer.readString());
} }
// override client's requested properties with server settings
if (baseDir != null) {
ci.setBaseDir(baseDir);
}
if (server.getIfExists()) {
ci.setProperty("IFEXISTS", "TRUE");
}
session = Engine.getInstance().createSession(ci); session = Engine.getInstance().createSession(ci);
transfer.setSession(session); transfer.setSession(session);
transfer.writeInt(SessionRemote.STATUS_OK); transfer.writeInt(SessionRemote.STATUS_OK);
......
...@@ -180,7 +180,7 @@ public abstract class TestBase { ...@@ -180,7 +180,7 @@ public abstract class TestBase {
* @param password the password to use * @param password the password to use
* @return the connection * @return the connection
*/ */
protected Connection getConnection(String name, String user, String password) throws SQLException { public Connection getConnection(String name, String user, String password) throws SQLException {
return getConnectionInternal(getURL(name, false), user, password); return getConnectionInternal(getURL(name, false), user, password);
} }
......
...@@ -872,18 +872,30 @@ public class TestTools extends TestBase { ...@@ -872,18 +872,30 @@ public class TestTools extends TestBase {
"-tcpAllowOthers").start(); "-tcpAllowOthers").start();
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", ""); conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
conn.close(); conn.close();
// must not be able to use a different base dir
new AssertThrows(ErrorCode.IO_EXCEPTION_1) {
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:9192/../test", "sa", "");
}};
new AssertThrows(ErrorCode.IO_EXCEPTION_1) {
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:9192/../test2/test", "sa", "");
}};
tcpServer.stop(); tcpServer.stop();
Server.createTcpServer( Server.createTcpServer(
"-ifExists", "-ifExists",
"-tcpPassword", "abc", "-tcpPassword", "abc",
"-baseDir", getBaseDir(), "-baseDir", getBaseDir(),
"-tcpPort", "9192").start(); "-tcpPort", "9192").start();
// should not be able to create new db // must not be able to create new db
try { new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", ""); public void test() throws SQLException {
} catch (SQLException e) { getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", "");
assertKnownException(e); }};
} new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:9192/test2;ifexists=false", "sa", "");
}};
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", ""); conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
conn.close(); conn.close();
new AssertThrows(ErrorCode.WRONG_USER_OR_PASSWORD) { new AssertThrows(ErrorCode.WRONG_USER_OR_PASSWORD) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论