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

Improved tests

上级 7740acbb
......@@ -175,6 +175,15 @@ public class ConnectionInfo implements Cloneable {
}
String normalizedName = FileUtils.unwrap(FileUtils.toRealPath(n));
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 " +
absDir);
}
......
......@@ -119,12 +119,6 @@ public class TcpServerThread implements Runnable {
}
db = server.checkKeyAndGetDatabaseName(db);
ConnectionInfo ci = new ConnectionInfo(db);
if (baseDir != null) {
ci.setBaseDir(baseDir);
}
if (server.getIfExists()) {
ci.setProperty("IFEXISTS", "TRUE");
}
ci.setOriginalURL(originalURL);
ci.setUserName(transfer.readString());
ci.setUserPasswordHash(transfer.readBytes());
......@@ -133,6 +127,13 @@ public class TcpServerThread implements Runnable {
for (int i = 0; i < len; i++) {
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);
transfer.setSession(session);
transfer.writeInt(SessionRemote.STATUS_OK);
......
......@@ -180,7 +180,7 @@ public abstract class TestBase {
* @param password the password to use
* @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);
}
......
......@@ -872,18 +872,30 @@ public class TestTools extends TestBase {
"-tcpAllowOthers").start();
conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9192/test", "sa", "");
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();
Server.createTcpServer(
"-ifExists",
"-tcpPassword", "abc",
"-baseDir", getBaseDir(),
"-tcpPort", "9192").start();
// should not be able to create new db
try {
getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", "");
} catch (SQLException e) {
assertKnownException(e);
}
// must not be able to create new db
new AssertThrows(ErrorCode.DATABASE_NOT_FOUND_1) {
public void test() throws SQLException {
getConnection("jdbc:h2:tcp://localhost:9192/test2", "sa", "");
}};
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.close();
new AssertThrows(ErrorCode.WRONG_USER_OR_PASSWORD) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论