1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.RawDBReaderTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.lang;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import junit.framework.Test;
import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
import org.apache.derbyTesting.junit.Decorator;
import org.apache.derbyTesting.junit.SecurityManagerSetup;
import org.apache.derbyTesting.junit.SupportFilesSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* <p>
* Test reading of corrupt databases.
* </p>
*/
public class RawDBReaderTest extends GeneratedColumnsHelper
{
///////////////////////////////////////////////////////////////////////////////////
//
// CONSTANTS
//
///////////////////////////////////////////////////////////////////////////////////
private static final String TEST_DBO = "TEST_DBO";
private static final String[] LEGAL_USERS = { TEST_DBO };
private static final String MEMORY_DB = "jdbc:derby:memory:rrt";
private static final String RECOVERY_SCRIPT = "extinout/recovery.sql";
private static final String BOOT_PASSWORD = "fooBarWibble";
private static final String LIST_USER_SCHEMAS =
"select schemaname from sys.sysschemas\n" +
"where schemaname not like 'SYS%'\n" +
"and schemaname != 'APP'\n" +
"and schemaname != 'NULLID'\n" +
"and schemaname != 'SQLJ'\n" +
"order by schemaname\n";
private static final String LIST_USER_TABLES =
"select tablename from sys.systables\n" +
"where tablename not like 'SYS%'\n" +
"order by tablename\n";
private static final String[][] NO_ROWS = {};
private static final String[][] EXPECTED_SCHEMAS =
{
{ "CONTROL11" },
{ "RAW11_APP" },
{ "RAW11_SCHEMA1" },
{ "RAW11_SCHEMA2" },
};
private static final String[][] EXPECTED_TABLES =
{
{ "T1" },
{ "T1" },
{ "T2" },
{ "T2" },
};
///////////////////////////////////////////////////////////////////////////////////
//
// STATE
//
///////////////////////////////////////////////////////////////////////////////////
// location of corrupt database
private File _dbDir;
///////////////////////////////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
///////////////////////////////////////////////////////////////////////////////////
/**
* Create a new instance.
*/
public RawDBReaderTest( String name )
{
super( name );
}
///////////////////////////////////////////////////////////////////////////////////
//
// JUnit BEHAVIOR
//
///////////////////////////////////////////////////////////////////////////////////
/**
* Construct top level suite in this JUnit test
*/
public static Test suite()
{
Test baseTest = TestConfiguration.embeddedSuite(RawDBReaderTest.class);
// We don't expect that users of this tool will run with a security
// manager. The tool is run standalone behind a firewall.
Test wideOpenTest = SecurityManagerSetup.noSecurityManager( baseTest );
// don't teardown the corrupt database. instead, just delete it in our
// own tearDown() method. this is to prevent the corrupt database from
// interfering with later tests which want to use a database with sql
// authorization turned on.
Test authenticatedTest = DatabasePropertyTestSetup.builtinAuthenticationNoTeardown
( wideOpenTest, LEGAL_USERS, "RRT" );
Test authorizedTest = TestConfiguration.sqlAuthorizationDecorator( authenticatedTest );
Test encryptedTest = Decorator.encryptedDatabaseBpw
(
authorizedTest,
"DES/CBC/NoPadding",
BOOT_PASSWORD
);
Test supportFilesTest = new SupportFilesSetup( encryptedTest );
return supportFilesTest;
}
protected void tearDown() throws Exception
{
super.tearDown();
if ( _dbDir != null )
{
// delete the corrupt database so that later tests,
// which require sql authorization, won't bomb because
// they can't open the encrypted database
assertDirectoryDeleted(_dbDir);
_dbDir = null;
}
}
///////////////////////////////////////////////////////////////////////////////////
//
// TESTS
//
///////////////////////////////////////////////////////////////////////////////////
/**
* <p>
* Test the
* </p>
*/
public void test_001_rawDBReader()
throws Exception
{
// create and populate the corrupt database
Connection dboConnection = openUserConnection( TEST_DBO );
populateCorruptDB( dboConnection );
TestConfiguration tc = getTestConfiguration();
// shutdown the database
tc.shutdownDatabase();
String dbName = tc.getDefaultDatabaseName();
File systemDir = new File( "system" );
_dbDir = new File( systemDir, dbName );
String databaseName = _dbDir.getPath();
Connection newDBConn = DriverManager.getConnection( MEMORY_DB + ";create=true" );
// verify that the tool hasn't created any schema objects
vetUnloaded( newDBConn );
// load the tool to recover the corrupt database
String dboPassword = tc.getPassword( TEST_DBO );
goodStatement
(
newDBConn,
"call syscs_util.syscs_register_tool\n" +
"(\n" +
" 'rawDBReader',\n" +
" true,\n" +
" '" + RECOVERY_SCRIPT + "',\n" +
" 'CONTROL11',\n" +
" 'RAW11_',\n" +
" '" + databaseName + "',\n" +
" 'bootPassword=" + BOOT_PASSWORD + "',\n" +
" '" + TEST_DBO + "',\n" +
" '" + dboPassword + "'\n" +
")\n"
);
// verify that the expected schema objects have been created
vetLoaded( newDBConn );
runRecoveryScript( newDBConn );
// now verify that we siphoned the data out of the corrupt database
vetSiphoning( newDBConn );
// drop the tables holding the data we siphoned out of the corrupt database
dropSiphonedData( newDBConn );
// unload the tool
goodStatement
(
newDBConn,
"call syscs_util.syscs_register_tool\n" +
"(\n" +
" 'rawDBReader',\n" +
" false,\n" +
" 'CONTROL11',\n" +
" 'RAW11_'\n" +
")\n"
);
vetUnloaded( newDBConn );
shutdownInMemoryDB();
}
private void populateCorruptDB( Connection dboConnection )
throws Exception
{
goodStatement( dboConnection, "create schema schema1" );
goodStatement( dboConnection, "create schema schema2" );
goodStatement( dboConnection, "create table schema1.t1( id int, tag varchar( 20 ) )" );
goodStatement
( dboConnection,
"insert into schema1.t1 values ( 1, 'schema1.t1:1' ), ( 2, 'schema1.t1:2' )" );
goodStatement( dboConnection, "create table schema1.t2( id int, tag varchar( 20 ) )" );
goodStatement
( dboConnection,
"insert into schema1.t2 values ( 1, 'schema1.t2:1' ), ( 2, 'schema1.t2:2' )" );
goodStatement( dboConnection, "create table schema2.t1( id int, tag varchar( 20 ) )" );
goodStatement
( dboConnection,
"insert into schema2.t1 values ( 1, 'schema2.t1:1' ), ( 2, 'schema2.t1:2' )" );
goodStatement( dboConnection, "create table schema2.t2( id int, tag varchar( 20 ) )" );
goodStatement
( dboConnection,
"insert into schema2.t2 values ( 1, 'schema2.t2:1' ), ( 2, 'schema2.t2:2' )" );
}
private void shutdownInMemoryDB() throws Exception
{
// shutdown and delete the in-memory db
try {
DriverManager.getConnection( MEMORY_DB + ";shutdown=true" );
} catch (SQLException se)
{
assertSQLState( "08006", se );
}
try {
DriverManager.getConnection( MEMORY_DB + ";drop=true" );
} catch (SQLException se)
{
assertSQLState( "08006", se );
}
}
private void runRecoveryScript( Connection conn ) throws Exception
{
try (BufferedReader reader =
new BufferedReader(new FileReader(RECOVERY_SCRIPT)))
{
while ( true )
{
String line = reader.readLine();
if ( line == null ) { break; }
// skip the initial connection statement
// as well as comments and blank lines
if ( line.length() == 0 ) { continue; }
if ( line.startsWith( "connect" ) ) { continue; }
if ( line.startsWith( "--" ) ) { continue; }
// strip off the trailing semi-colon
line = line.substring( 0, line.indexOf( ';' ) );
goodStatement( conn, line );
}
}
}
private void vetSiphoning( Connection conn ) throws Exception
{
assertResults
(
conn,
"select * from schema1.t1 order by id",
new String[][]
{
{ "1", "schema1.t1:1" },
{ "2", "schema1.t1:2" },
},
false
);
assertResults
(
conn,
"select * from schema1.t2 order by id",
new String[][]
{
{ "1", "schema1.t2:1" },
{ "2", "schema1.t2:2" },
},
false
);
assertResults
(
conn,
"select * from schema2.t1 order by id",
new String[][]
{
{ "1", "schema2.t1:1" },
{ "2", "schema2.t1:2" },
},
false
);
assertResults
(
conn,
"select * from schema2.t2 order by id",
new String[][]
{
{ "1", "schema2.t2:1" },
{ "2", "schema2.t2:2" },
},
false
);
}
private void vetUnloaded( Connection conn ) throws Exception
{
assertResults( conn, LIST_USER_SCHEMAS, NO_ROWS, false );
assertResults( conn, LIST_USER_TABLES, NO_ROWS, false );
}
private void vetLoaded( Connection conn ) throws Exception
{
assertResults( conn, LIST_USER_SCHEMAS, EXPECTED_SCHEMAS, false );
assertResults( conn, LIST_USER_TABLES, EXPECTED_TABLES, false );
}
private void dropSiphonedData( Connection conn ) throws Exception
{
goodStatement( conn, "drop table schema1.t1" );
goodStatement( conn, "drop table schema1.t2" );
goodStatement( conn, "drop table schema2.t1" );
goodStatement( conn, "drop table schema2.t2" );
goodStatement( conn, "drop schema schema1 restrict" );
goodStatement( conn, "drop schema schema2 restrict" );
}
}
|