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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.DboPowersTest
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.jdbcapi;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.JDBCDataSource;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* This JUnit tests enforcement of dbo (=database owner) powers, cf.
* DERBY-2264.
*
* The tests are run in the cross product (cardinality 10) of contexts:
*
* {client/server, embedded} x
* {no authentication, authentication and authentication/sqlAuthorization} x
* {data base owner, other user }
*
* One could consider removing the client/server suites to speed up
* this test as it does not add much value given the nature of the changes.
*
*/
public class DboPowersTest extends BaseJDBCTestCase
{
/* internal state */
final private int _authLevel;
final private String _dbo;
final private String _dboPassword;
/* test execution security context: one of three below */
final private static int NOAUTHENTICATION=0;
final private static int AUTHENTICATION=1;
final private static int SQLAUTHORIZATION=2;
final private static String[] secLevelNames = {
"noAuthentication",
"authentication",
"authentication + sqlAuthorization"};
final private static boolean ENCRYPT = true;
final private static boolean DECRYPT = false;
/**
* Create a new instance of DboPowersTest (for shutdown test)
*
* @param name Fixture name
* @param authLevel authentication level with which test is run
*/
public DboPowersTest(String name, int authLevel)
{
super(name);
this._authLevel = authLevel;
this._dbo = null;
this._dboPassword = null;
}
/**
* Creates a new instance of DboPowersTest for cryptographic operations
* and hard upgrade tests.
* <p>
* The database owner credentials is needed to always be able to perform
* the restricted operations (when they are not under test, but used as
* part of a test fixture for another operation).
*
* @param name Fixture name
* @param authLevel authentication level with which test is run
* @param dbo Database owner
* @param dboPassword Database owner's password
*/
public DboPowersTest(String name, int authLevel,
String dbo, String dboPassword)
{
super(name);
this._authLevel = authLevel;
this._dbo = dbo;
this._dboPassword = dboPassword;
}
/**
* Construct top level suite in this JUnit test
*
* @return A suite containing embedded and client suites
*/
public static Test suite()
{
BaseTestSuite suite = new BaseTestSuite("DboPowersTest");
/* Database shutdown powers */
suite.addTest(dboShutdownSuite("suite: shutdown powers, embedded"));
suite.addTest(
TestConfiguration.clientServerDecorator(
dboShutdownSuite("suite: shutdown powers, client")));
// Database (re)encryption powers and decryption powers.
// The cryptographic power tests are not run for JSR169, since Derby
// does not support database encryption for that platform, cf.
// the specification for JSR169 support in DERBY-97.
if (!JDBC.vmSupportsJSR169()) {
suite.addTest(
dboCryptoSuite("suite: cryptographic powers, embedded"));
suite.addTest(
TestConfiguration.clientServerDecorator(
dboCryptoSuite("suite: cryptographic powers, client")));
}
/* Database hard upgrade powers */
suite.addTest(
dboHardUpgradeSuite("suite: hard upgrade powers, embedded"));
suite.addTest(
TestConfiguration.clientServerDecorator(
dboHardUpgradeSuite("suite: hard upgrade powers, client")));
return suite;
}
/**
* Users used by both dboShutdownSuite and dboCryptoSuite
*/
final static String[][] users = {
/* authLevel == AUTHENTICATION: dbo is APP/APP for db 'wombat',
* so use that as first user.
*/
{"APP", "U1"},
/* authLevel == SQLAUTHORIZATION: sqlAuthorizationDecorator
* decorator presumes TEST_DBO as dbo, so add it to set of
* valid users. Uses a fresh db 'dbsqlauth', not 'wombat'.
*/
{"TEST_DBO", "U1"}};
final static String pwSuffix = "pwSuffix";
/**
*
* Construct suite of tests for shutdown database action
*
* @param framework Derby framework name
* @return A suite containing the test case for shutdown
* incarnated for the three security levels no authentication,
* authentication, and authentication plus sqlAuthorization, The
* latter two has an instance for dbo, and one for an ordinary user,
* so there are in all five incarnations of tests.
*/
private static Test dboShutdownSuite(String framework)
{
Test tests[] = new Test[SQLAUTHORIZATION+1]; // one per authLevel
/* Tests without any authorization active (level ==
* NOAUTHENTICATION).
*/
BaseTestSuite noauthSuite =
new BaseTestSuite("suite: security level=" +
secLevelNames[NOAUTHENTICATION]);
noauthSuite.addTest(new DboPowersTest("testShutDown",
NOAUTHENTICATION));
tests[NOAUTHENTICATION] = noauthSuite;
/* First decorate with users, then with authentication. Do this
* twice, once for authentication only, and once for
* authentication + sqlAuthorization (see extra decorator
* added below).
*/
for (int autLev = AUTHENTICATION;
autLev <= SQLAUTHORIZATION ; autLev++) {
tests[autLev] = wrapShutdownUserTests(autLev);
}
BaseTestSuite suite = new BaseTestSuite("dboPowers:"+framework);
/* run tests with no authentication enabled */
suite.addTest(tests[NOAUTHENTICATION]);
/* run test for all users with only authentication enabled */
suite.addTest(tests[AUTHENTICATION]);
/* run test for all users with authentication and
* sqlAuthorization enabled
*/
suite.addTest(
TestConfiguration.
sqlAuthorizationDecorator(tests[SQLAUTHORIZATION]));
return suite;
}
/**
* Wraps the shutdown fixture in decorators to run with data
* base owner and one other valid user.
*
* @param autLev security context to use
*/
private static Test wrapShutdownUserTests(int autLev)
{
// add decorator for different users authenticated
BaseTestSuite usersSuite =
new BaseTestSuite("usersSuite: security level=" +
secLevelNames[autLev]);
// First decorate with users, then with
for (int userNo = 0; userNo < users.length; userNo++) {
usersSuite.addTest
(TestConfiguration.changeUserDecorator
(new DboPowersTest("testShutDown", autLev),
users[autLev-1][userNo],
users[autLev-1][userNo].concat(pwSuffix)));
}
return DatabasePropertyTestSetup.
builtinAuthentication(usersSuite, users[autLev-1], pwSuffix);
}
/**
* Test database shutdown power enforcement
*
* @throws SQLException
*/
public void testShutDown() throws SQLException
{
println("testShutDown: auth=" + this._authLevel +
" user="+getTestConfiguration().getUserName());
// make sure db is booted
getConnection().close();
String user = getTestConfiguration().getUserName();
String password = getTestConfiguration().getUserPassword();
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(
ds, "connectionAttributes", "shutdown=true");
JDBCDataSource.setBeanProperty(ds, "user", user);
JDBCDataSource.setBeanProperty(ds, "password", password);
try {
ds.getConnection();
fail("shutdown failed: no exception");
} catch (SQLException e) {
if ("08006".equals(e.getSQLState())) {
// reboot if shutdown succeeded
JDBCDataSource.setBeanProperty(ds, "connectionAttributes", "");
ds.getConnection().close();
}
vetShutdownException(user, e);
}
}
/**
* Decide if the result of trying to shut down the database is
* compliant with the semantics introduced by DERBY-2264.
*
*/
private void vetShutdownException (String user, SQLException e)
{
switch (_authLevel) {
case NOAUTHENTICATION:
assertSQLState("database shutdown, no authentication",
"08006", e);
break;
case AUTHENTICATION:
/* We don't enforce dbo powers if only connection level
* authentication is used, for now. This leniency was
* introduced late in 10.3 release cycle for compatibility
* reasons.
*/
assertSQLState("database shutdown, authentication",
"08006", e);
break;
case SQLAUTHORIZATION:
if ("TEST_DBO".equals(user)) {
assertSQLState("database shutdown, SQL authorization, db owner",
"08006", e);
} else {
assertSQLState("database shutdown restriction, " +
"SQL authorization, not db owner",
"08004", e);
}
break;
default:
fail("test error: invalid authLevel: " + _authLevel);
break;
}
}
/**
*
* Constructs suite of tests for cryptographic actions, that is database
* encryption, re-encryption, and decryption.
*
* @param framework Derby framework name
* @return A suite containing the test cases for cryptographic operations
* incarnated for the three security levels no authentication,
* authentication, and authentication plus sqlAuthorization, The
* latter two has an instance for dbo, and one for an ordinary user,
* so there are in all five incarnations of tests.
*/
private static Test dboCryptoSuite(String framework)
{
Test tests[] = new Test[SQLAUTHORIZATION+1]; // one per authLevel
/* Tests without any authorization active (level ==
* NOAUTHENTICATION). Note use of no shutdown decorator
* variants: Necessary since framework doesn't know
* bootPassword.
*/
BaseTestSuite noauthSuite =
new BaseTestSuite("suite: security level=" +
secLevelNames[NOAUTHENTICATION]);
for (int tNo = 0; tNo < cryptoTests.length; tNo++) {
noauthSuite.addTest(
TestConfiguration.singleUseDatabaseDecoratorNoShutdown(
new DboPowersTest(cryptoTests[tNo], NOAUTHENTICATION,
"foo", "bar")));
}
tests[NOAUTHENTICATION] = noauthSuite;
/* Tests with authentication and sql authorization
*/
for (int autLev = AUTHENTICATION;
autLev <= SQLAUTHORIZATION ; autLev++) {
tests[autLev] = wrapCryptoUserTests(autLev);
}
BaseTestSuite suite = new BaseTestSuite("dboPowers:"+framework);
/* run tests with no authentication enabled */
suite.addTest(tests[NOAUTHENTICATION]);
/* run test for all users with only authentication enabled */
suite.addTest(tests[AUTHENTICATION]);
/* run test for all users with authentication and
* sqlAuthorization enabled
*/
suite.addTest(tests[SQLAUTHORIZATION]);
return suite;
}
/**
* Wraps the encryption fixtures in decorators to run with data
* base owner and one other valid user.
*
* @param autLev security context to use
*/
private static Test wrapCryptoUserTests(int autLev)
{
// add decorator for different users authenticated
BaseTestSuite usersSuite =
new BaseTestSuite("usersSuite: security level=" +
secLevelNames[autLev]);
// First decorate with users, then with authentication. Note
// use of no teardown / no shutdown decorator variants:
// Necessary since framework doesnt know bootPassword
for (int userNo = 0; userNo < users.length; userNo++) {
for (int tNo = 0; tNo < cryptoTests.length; tNo++) {
Test test = TestConfiguration.changeUserDecorator
(new DboPowersTest(cryptoTests[tNo],
autLev,
users[autLev-1][0], // dbo
users[autLev-1][0].concat(pwSuffix)),
users[autLev-1][userNo],
users[autLev-1][userNo].concat(pwSuffix));
test = DatabasePropertyTestSetup.builtinAuthenticationNoTeardown
(test, users[autLev-1], pwSuffix);
if (autLev == AUTHENTICATION) {
test = TestConfiguration.
singleUseDatabaseDecoratorNoShutdown(test);
} else {
test = TestConfiguration.
sqlAuthorizationDecoratorSingleUse(test);
}
usersSuite.addTest(test);
}
}
return usersSuite;
}
/**
* Enumerates the cryptographic tests.
*/
final static String[] cryptoTests = {
"testEncrypt", "testReEncrypt", "testDecrypt"};
/**
* Test database encryption for an already created
* database. Note: The test needs to shut down the database for
* the single use decorators to work.
*
* @throws SQLException
*/
public void testEncrypt() throws SQLException
{
println("testEncrypt: auth=" + this._authLevel +
" user="+getTestConfiguration().getUserName());
// make sure db is created
getConnection().close();
// shut down database in preparation for encryption
bringDbDown();
// make encryption attempt
String user = getTestConfiguration().getUserName();
String password = getTestConfiguration().getUserPassword();
String bootPassword="12345678";
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(ds, "connectionAttributes",
"dataEncryption=true;bootPassword=" +
bootPassword);
JDBCDataSource.setBeanProperty(ds, "user", user);
JDBCDataSource.setBeanProperty(ds, "password", password);
Connection con;
try {
con = ds.getConnection();
vetCryptoAttempt(ENCRYPT, user, null);
} catch (SQLException e) {
vetCryptoAttempt(ENCRYPT, user, e);
bringDbDown();
return;
}
try {
derby3038(con);
} catch (SQLException e) {
fail("derby3038 regression: " + e);
}
// we managed to encrypt: bring db down and up again to verify
bringDbDown();
bringDbUp(bootPassword);
bringDbDown();
}
/**
* Test database re-encryption for an already encrypted
* database. Note: The test needs to shut down database for the
* single use decorators to work.
*
* @throws SQLException
*/
public void testReEncrypt() throws SQLException
{
println("testReEncrypt: auth=" + this._authLevel +
" user="+getTestConfiguration().getUserName());
// make sure db is created
getConnection().close();
// shut down database in preparation for encryption
bringDbDown();
String bootPassword="12345678";
doEncrypt(bootPassword);
bringDbDown();
// make re-encryption attempt
String user = getTestConfiguration().getUserName();
String password = getTestConfiguration().getUserPassword();
String newBootPassword="87654321";
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(ds, "connectionAttributes",
"bootPassword=" + bootPassword +
";newBootPassword=" + newBootPassword);
JDBCDataSource.setBeanProperty(ds, "user", user);
JDBCDataSource.setBeanProperty(ds, "password", password);
try {
ds.getConnection();
vetCryptoAttempt(ENCRYPT, user, null);
} catch (SQLException e) {
vetCryptoAttempt(ENCRYPT, user, e);
bringDbDown();
return;
}
// we managed to encrypt: bring db down and up again to verify
bringDbDown();
bringDbUp(newBootPassword);
bringDbDown();
}
/**
* Tests that only the DBO can decrypt a database.
*/
public void testDecrypt()
throws SQLException {
println("testDecrypt: auth=" + this._authLevel +
" user=" + getTestConfiguration().getUserName());
// make sure db is created
getConnection().close();
// shut down database in preparation for encryption
bringDbDown();
String bootPassword = "conHippo08";
doEncrypt(bootPassword);
// shut down database in preparation for decryption
bringDbDown();
String user = getTestConfiguration().getUserName();
String password = getTestConfiguration().getUserPassword();
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(ds, "connectionAttributes",
"bootPassword=" + bootPassword +
";decryptDatabase=true");
JDBCDataSource.setBeanProperty(ds, "user", user);
JDBCDataSource.setBeanProperty(ds, "password", password);
try {
ds.getConnection();
vetCryptoAttempt(DECRYPT, user, null);
} catch (SQLException sqle) {
vetCryptoAttempt(DECRYPT, user, sqle);
return;
} finally {
bringDbDown();
}
// we managed to decrypt: bring db up again to verify
bringDbUp(null);
bringDbDown();
}
/**
* Encrypt database, as owner (not testing encryption power here)
* @param bootPassword
* @throws SQLException
*/
private void doEncrypt(String bootPassword) throws SQLException
{
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(ds, "connectionAttributes",
"dataEncryption=true;bootPassword=" +
bootPassword);
JDBCDataSource.setBeanProperty(ds, "user", _dbo);
JDBCDataSource.setBeanProperty(ds, "password", _dboPassword);
ds.getConnection();
}
/**
* Shut down database, as db owner (not testing that power here)
*/
private void bringDbDown()
{
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(
ds, "connectionAttributes", "shutdown=true");
JDBCDataSource.setBeanProperty(ds, "user", _dbo);
JDBCDataSource.setBeanProperty(ds, "password", _dboPassword);
try {
ds.getConnection();
fail("shutdown failed: expected exception");
} catch (SQLException e) {
assertSQLState("database shutdown", "08006", e);
}
}
/**
* Boots database back up after cryptographic operation using current user,
* should succeed.
*
* @param bootPassword boot using this bootPassword, may be {@code null}
* @throws SQLException
*/
private void bringDbUp(String bootPassword) throws SQLException
{
String user = getTestConfiguration().getUserName();
String password = getTestConfiguration().getUserPassword();
DataSource ds = JDBCDataSource.getDataSource();
if (bootPassword != null) {
JDBCDataSource.setBeanProperty(
ds, "connectionAttributes", "bootPassword=" + bootPassword);
}
JDBCDataSource.setBeanProperty(ds, "user", user);
JDBCDataSource.setBeanProperty(ds, "password", password);
ds.getConnection().close();
}
/**
* Decides if the result of trying to (re-)encrypt or decrypt the database
* is compliant with the semantics introduced by DERBY-2264.
*
* @param encrypt whether we are (re-)encrypting or decrypting
* @param user The db user under which we tried to encrypt
* @param e Exception caught during attempt, if any
*/
private void vetCryptoAttempt(boolean encrypt, String user, SQLException e)
{
vetAttempt(user, e, "08004", encrypt ? "(re)encryption" : "decrypt");
}
/**
*
* Construct suite of tests for hard upgrade database action
*
* NOTE: there is no real upgrade going on here since the
* database is created with the same version, but the checking
* is performed nonetheless, which is what we are testing
* here. This saves us from having to create a database with
* an old version of Derby to test this power.
*
* @param framework Derby framework name
* @return A suite containing the test case for hard upgrade
* incarnated for the three security levels no authentication,
* authentication, and authentication plus sqlAuthorization, The
* latter two has an instance for dbo, and one for an ordinary user,
* so there are in all five incarnations of tests.
*/
private static Test dboHardUpgradeSuite(String framework)
{
Test tests[] = new Test[SQLAUTHORIZATION+1]; // one per authLevel
/* Tests without any authorization active (level ==
* NOAUTHENTICATION).
*/
BaseTestSuite noauthSuite =
new BaseTestSuite("suite: security level=" +
secLevelNames[NOAUTHENTICATION]);
noauthSuite.addTest(new DboPowersTest("testHardUpgrade",
NOAUTHENTICATION,
"foo", "bar"));
tests[NOAUTHENTICATION] = noauthSuite;
/* First decorate with users, then with authentication. Do this
* twice, once for authentication only, and once for
* authentication + sqlAuthorization (see extra decorator
* added below).
*/
for (int autLev = AUTHENTICATION;
autLev <= SQLAUTHORIZATION ; autLev++) {
tests[autLev] = wrapHardUpgradeUserTests(autLev);
}
BaseTestSuite suite = new BaseTestSuite("dboPowers:"+framework);
// A priori, doing a hard upgrade is a no-op here; we are only
// interested in checking if we have the powers to do it. However,
// sometimes the regression suite is run against a default database
// (system/wombat) created by an earlier release to check soft upgrade
// modes. To avoid interfering with such usage, we use a
// singleUseDatabaseDecorator below, so we avoid accidentally hard
// upgrading system/wombat in such runs. The SQLAUTHORIZATION run takes
// care of itself since it uses another database anyway.
/* run tests with no authentication enabled */
suite.addTest(TestConfiguration.singleUseDatabaseDecorator(
tests[NOAUTHENTICATION]));
/* run test for all users with only authentication enabled */
suite.addTest(TestConfiguration.singleUseDatabaseDecorator(
tests[AUTHENTICATION]));
/* run test for all users with authentication and
* sqlAuthorization enabled
*/
suite.addTest(
TestConfiguration.
sqlAuthorizationDecorator(tests[SQLAUTHORIZATION]));
return suite;
}
/**
* Wraps the shutdown fixture in decorators to run with data
* base owner and one other valid user.
*
* @param autLev security context to use
*/
private static Test wrapHardUpgradeUserTests(int autLev)
{
// add decorator for different users authenticated
BaseTestSuite usersSuite =
new BaseTestSuite("usersSuite: security level=" +
secLevelNames[autLev]);
// First decorate with users, then with
for (int userNo = 0; userNo < users.length; userNo++) {
usersSuite.addTest
(TestConfiguration.changeUserDecorator
(new DboPowersTest("testHardUpgrade",
autLev,
users[autLev-1][0], // dbo
users[autLev-1][0].concat(pwSuffix)),
users[autLev-1][userNo],
users[autLev-1][userNo].concat(pwSuffix)));
}
return DatabasePropertyTestSetup.
builtinAuthentication(usersSuite, users[autLev-1], pwSuffix);
}
/**
* Test database upgrade power enforcement
*
* @throws SQLException
*/
public void testHardUpgrade() throws SQLException
{
println("testHardUpgrade: auth=" + this._authLevel +
" user="+getTestConfiguration().getUserName());
// make sure db is created
getConnection().close();
// shut it down in preparation for upgrade boot
bringDbDown();
String user = getTestConfiguration().getUserName();
String password = getTestConfiguration().getUserPassword();
DataSource ds = JDBCDataSource.getDataSource();
JDBCDataSource.setBeanProperty(
ds, "connectionAttributes", "upgrade=true");
JDBCDataSource.setBeanProperty(ds, "user", user);
JDBCDataSource.setBeanProperty(ds, "password", password);
try {
ds.getConnection();
vetHardUpgradeAttempt(user, null);
} catch (SQLException e) {
vetHardUpgradeAttempt(user, e);
}
bringDbDown();
}
/**
* Decide if the result of trying to hard upgrade the database is
* compliant with the semantics introduced by DERBY-2264.
*
* @param user The db user under which we tried to upgrade
* @param e Exception caught during attempt, if any
*/
private void vetHardUpgradeAttempt (String user, SQLException e)
{
vetAttempt(user, e, "08004", "hard upgrade");
}
/**
* Decide if the result of trying operation yields expected result.
*
* @param user The db user under which we tried to upgrade
* @param e Exception caught during attempted operation, if any
* @param state The expected SQL state if this operation fails due to
* insufficient power
* @param operation string describing the operation attempted
*/
private void vetAttempt (String user, SQLException e,
String state, String operation)
{
switch (_authLevel) {
case NOAUTHENTICATION:
assertEquals(operation + ", no authentication", null, e);
break;
case AUTHENTICATION:
/* We don't enforce dbo powers if only connection level
* authentication is used, for now. This leniency was
* introduced late in 10.3 release cycle for compatibility
* reasons.
*/
assertEquals(operation + ", authentication", null, e);
break;
case SQLAUTHORIZATION:
if ("TEST_DBO".equals(user)) {
assertEquals(operation + ", SQL authorization, db owner",
null, e);
} else {
String msg = operation + ", SQL authorization, not db owner";
assertNotNull(
msg + ": succeeded unexpectedly without exeption", e);
assertSQLState(msg, state, e);
}
break;
default:
fail("test error: invalid authLevel: " + _authLevel);
break;
}
}
/**
* Make and call a stored procedure which opens a nested
* connection to expose DERBY-3038.
*/
private void derby3038(Connection con) throws SQLException {
java.sql.Statement s = con.createStatement();
try {
s.executeUpdate
("CREATE PROCEDURE DERBY3038PROC () " +
"LANGUAGE JAVA PARAMETER STYLE JAVA EXTERNAL NAME '" +
DboPowersTest.class.getName() + ".derby3038Proc' " +
"READS SQL DATA");
s.executeUpdate("CALL DERBY3038PROC()");
} finally {
s.close();
}
}
public static void derby3038Proc()
throws SQLException {
// Before fixing DERBY-3038 this connect would fail.
Connection con = java.sql.DriverManager.
getConnection("jdbc:default:connection");
con.close();
}
}
|