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 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.RolesConferredPrivilegesTest
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.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* This tests that SQL roles actually confer the correct privileges, that is,
* when privileges are granted to one or more roles and those roles are granted
* to other roles, to one or more users or to PUBLIC, sessions can make use of
* them correctly.
*/
public class RolesConferredPrivilegesTest extends BaseJDBCTestCase
{
private final static String pwSuffix = "pwSuffix";
/* SQL states */
private final static String NOEXECUTEPERMISSION = "42504";
private final static String NOTABLEPERMISSION = "42500";
private final static String NOCOLUMNPERMISSION = "42502";
private final static String TABLENOTFOUND = "42X05";
private final static String OBJECTNOTFOUND = "42X94";
private final static String FKVIOLATION = "23503";
private final static String CHECKCONSTRAINTVIOLATED = "23513";
private final static String ALREADYCLOSED = "XJ012";
private final static String CONSTRAINTDROPPED = "01500";
private final static String VIEWDROPPED = "01501";
private final static String TRIGGERDROPPED = "01502";
private final static String UNRELIABLE = "42Y39";
private final static String[] users = {"test_dbo", "DonaldDuck", "MickeyMouse"};
/**
* Create a new instance of RolesConferredPrivilegesTest.
*
* @param name Fixture name
*/
public RolesConferredPrivilegesTest(String name)
{
super(name);
}
/**
* Construct top level suite in this JUnit test
*
* @return A suite containing embedded and client suites.
*/
public static Test suite()
{
BaseTestSuite suite =
new BaseTestSuite("RolesConferredPrivilegesTest");
suite.addTest(makeSuite());
// suite.addTest(
// TestConfiguration.clientServerDecorator(makeSuite()));
return suite;
}
/**
* Construct suite of tests
*
* @return A suite containing the test cases.
*/
private static Test makeSuite()
{
/* Tests running with sql authorization set. First decorate
* with clean database, then with authentication +
* sqlAuthorization.
*/
Test clean = new CleanDatabaseTestSetup(
new BaseTestSuite(RolesConferredPrivilegesTest.class)) {
protected void decorateSQL(Statement s)
throws SQLException {
/*
* a1 a2 a3
* / | \ | |
* / b +--------> c d
* j | \ /
* e---+ \ /
* \ \ \ /
* \ \---------+ \ /
* \ \_ f
* \ /
* \ /
* \ /
* \ /
* \ /
* \ /
* h
*/
s.execute("create role a1");
s.execute("create role j");
s.execute("create role b");
s.execute("create role e");
s.execute("create role h");
s.execute("create role a2");
s.execute("create role c");
s.execute("create role f");
s.execute("create role a3");
s.execute("create role d");
s.execute("grant a1 to j");
s.execute("grant a1 TO b");
s.execute("grant b TO e");
s.execute("grant e TO h");
s.execute("grant a1 TO c");
s.execute("grant e TO f");
s.execute("grant a2 TO c");
s.execute("grant c TO f");
s.execute("grant f TO h");
s.execute("grant a3 TO d");
s.execute("grant d TO f");
s.execute("create schema s1");
s.execute
("create function s1.f1( ) returns int " +
"language java parameter style java external name " +
"'org.apache.derbyTesting.functionTests.tests.lang." +
"RolesConferredPrivilegesTest.s1f1' " +
"no sql called on null input");
s.execute
("create function s1.f2( ) returns int " +
"language java parameter style java external name " +
"'org.apache.derbyTesting.functionTests.tests.lang." +
"RolesConferredPrivilegesTest.s1f1' " +
"no sql called on null input");
s.execute
("create table s1.t1(" +
"c1 int unique, c2 int unique, c3 int unique, " +
"primary key (c1,c2,c3))");
// We made columns all unique so we can test references
// privilege for all columns.
s.execute(
"create procedure s1.calledNested()" +
" language java parameter style java" +
" external name " +
"'org.apache.derbyTesting.functionTests.tests.lang." +
"RolesConferredPrivilegesTest.calledNested' " +
" modifies sql data");
s.execute
("create function s1.getCurrentRole() " +
"returns varchar(30)" +
"language java parameter style java external name " +
"'org.apache.derbyTesting.functionTests.tests.lang." +
"RolesConferredPrivilegesTest.getCurrentRole' " +
" reads sql data");
}
};
return
TestConfiguration.sqlAuthorizationDecorator(
DatabasePropertyTestSetup.singleProperty(
DatabasePropertyTestSetup.builtinAuthentication(
clean, users, pwSuffix),
// Increase default statementCacheSize since we compile a
// lot:
"derby.language.statementCacheSize", "1000"));
}
private final static int GRANT = 0;
private final static int REVOKE = 1;
// Action type for assert methods.
private final static int NOPRIV = 0;
private final static int VIAUSER = 1;
private final static int VIAROLE = 2;
// The "guts" of GRANT/REVOKE privilege strings. Prepend GRANT/REVOKE and
// append TO/FROM authorizationId [RESTRICT] as the case may be.
private final static String g_r = "references on s1.t1 ";
private final static String g_r_c1 = "references (c1) on s1.t1 ";
private final static String g_r_c2 = "references (c2) on s1.t1 ";
private final static String g_r_c3 = "references (c3) on s1.t1 ";
private final static String g_u = "update on s1.t1 ";
private final static String g_u_c1 = "update (c1) on s1.t1 ";
private final static String g_u_c2 = "update (c2) on s1.t1 ";
private final static String g_u_c3 = "update (c3) on s1.t1 ";
private final static String g_u_c1_c2_c3 = "update (c1,c2,c3) on s1.t1 ";
private final static String g_i = "insert on s1.t1 ";
private final static String g_s = "select on s1.t1 ";
private final static String g_s_c1 = "select (c1) on s1.t1 ";
private final static String g_s_c2 = "select (c2) on s1.t1 ";
private final static String g_s_c3 = "select (c3) on s1.t1 ";
private final static String g_d = "delete on s1.t1 ";
private final static String g_t = "trigger on s1.t1 ";
private final static String g_e = "execute on function s1.f1 ";
private final static String g_e_f2 = "execute on function s1.f2 ";
// Collections of privileges
private final static String[] g_all =
new String[] {g_r, g_r_c1, g_r_c2, g_r_c3,
g_u, g_u_c1, g_u_c2, g_u_c3,
g_i,
g_s, g_s_c1, g_s_c2, g_s_c3,
g_d,
g_t,
g_e};
// column level (for privilege types applicable)
private final static String[] g_all_col =
new String[] {g_r_c1, g_r_c2, g_r_c3,
g_u_c1, g_u_c2, g_u_c3,
g_i,
g_s_c1, g_s_c2, g_s_c3,
g_d,
g_t,
g_e};
// table level
private final static String[] g_all_tab =
new String[] {g_r, g_u, g_i, g_s, g_d, g_t, g_e};
private final static String[][]grantRevokes =
new String[][] {g_all, g_all_col, g_all_tab};
/**
* Basic test that checks that privileges granted to a role are applicable
* when the user sets the current role to that role (or to a role that
* inherits that role).
*
* @throws SQLException
*/
public void atestConferredPrivileges() throws SQLException
{
Connection dboConn = getConnection();
Statement s = dboConn.createStatement();
// try with role granted to both a user and to public
String[] grantees = new String[] {"DonaldDuck", "public"};
Connection c = openUserConnection("DonaldDuck");
for (int gNo = 0; gNo < grantees.length; gNo++ ) {
/*
* Grant a role to a session's user and test that privileges apply
* when granted to any applicable role.
*/
s.executeUpdate("grant h to " + grantees[gNo]);
String[] applicableFor_h =
new String[] {"h", "a1", "a2", "a3", "b", "e", "c", "f", "d"};
String[] notApplicableFor_h =
new String[] {"j"};
setRole(c, "h");
// Test that all privileges apply when granted to the current role
// or to an inherited role of the current role, cf graph above.
for (int i=0; i < applicableFor_h.length; i++) {
assertAllforRole(VIAROLE, c, applicableFor_h[i]);
}
// Test that no privileges apply when granted to a role NOT
// inherited by the current role.
for (int i=0; i < notApplicableFor_h.length; i++) {
assertAllforRole(NOPRIV, c, notApplicableFor_h[i]);
}
/*
* Test that no privileges apply when we set role to none.
*/
setRole(c, "none");
for (int i=0; i < applicableFor_h.length; i++) {
assertAllforRole(NOPRIV, c, applicableFor_h[i]);
}
/*
* Test that when one link in the graph "cycle" is broken,
* privileges still apply.
*/
s.executeUpdate("grant f to " + grantees[gNo]);
setRole(c, "f");
assertAllforRole(VIAROLE, c, "a1");
// one arc gone
s.executeUpdate("revoke a1 from c");
assertAllforRole(VIAROLE, c, "a1");
// both arcs gone
s.executeUpdate("revoke a1 from b");
assertAllforRole(NOPRIV, c, "a1");
// resurrect other arc
s.executeUpdate("grant a1 to b");
assertAllforRole(VIAROLE, c, "a1");
// restore things
s.executeUpdate("grant a1 to c");
s.executeUpdate("revoke f from " + grantees[gNo]);
/*
* Revoke the role from the current session's user and verify that
* privileges no longer apply when granted to any heretofore
* applicable role.
*/
setRole(c, "h");
s.executeUpdate("revoke h from " + grantees[gNo]);
for (int i=0; i < applicableFor_h.length; i++) {
assertAllforRole(NOPRIV, c, applicableFor_h[i]);
}
/*
* Test when role is dropped when still a current role
*/
s.executeUpdate("grant h to " + grantees[gNo]);
setRole(c, "h");
// Test that all privileges apply when granted to the current role
// or to an inherited role of the current role, cf graph above.
for (int i=0; i < applicableFor_h.length; i++) {
assertAllforRole(VIAROLE, c, applicableFor_h[i]);
}
s.executeUpdate("drop role h");
for (int i=0; i < applicableFor_h.length; i++) {
assertAllforRole(NOPRIV, c, applicableFor_h[i]);
}
// restore the dropped role
s.executeUpdate("create role h");
s.executeUpdate("grant e to h");
s.executeUpdate("grant f to h");
}
c.close();
s.close();
dboConn.close();
}
/**
* When a view, a trigger or a constraint requires a privilege by way of
* the current role (or by way of a role inherited by the current role) at
* creation time (SELECT, TRIGGER or REFERENCES privilege respectively), a
* dependency is also registered against the current role. Whenever that
* role (it need no longer be current) - or indeed one of its inherited
* roles - is revoked (from the current user or from a role in the closure
* of the original current role) or dropped, the dependent view, trigger or
* constraint is potentially invalidated (the single dependency is against
* the original current role, not against the potentially n-ary set of
* roles in the closure of the current role used to find the required
* privileges). Due to DERBY-1632, currently the objects are dropped
* instead of being potentially revalidated.
*
* These tests check that invalidation actually happens and leads to a
* dropping of the dependent view (there are also no revalidation
* possibilities in play here, so even when DERBY-1632 is fixed these tests
* should work).
*/
public void testViewInvalidation() throws SQLException {
Connection dboConn = getConnection();
Statement s = dboConn.createStatement();
Connection c = openUserConnection("DonaldDuck");
Statement cStmt = c.createStatement();
SQLWarning w;
/*
* 3-dimensional search space:
*
* Which role we grant the role to (direct to a role or to a role it
* inherits)
* X
* Whether the role is granted directly to the session user or to PUBLIC.
* X
* Whether we grant the entire underlying table or just the column
* needed.
*/
String[] grantToThisRole = new String[] {"a2", "h"};
String[] roleGrantees = new String[] {"DonaldDuck", "public"};
String[] tabAndColSelectsPerms = new String[] {g_s, g_s_c1};
String createViewString = "create view v as select c1 from s1.t1";
for (int r = 0; r < grantToThisRole.length; r++) {
for (int gNo = 0; gNo < roleGrantees.length; gNo++ ) {
for (int i = 0; i < tabAndColSelectsPerms.length; i++) {
/*
* Create a view on the basis of a select privilege via a
* role.
*/
s.executeUpdate("grant h to " + roleGrantees[gNo]);
doGrantRevoke(GRANT, "test_dbo", tabAndColSelectsPerms[i],
grantToThisRole[r]);
setRole(c, "h");
cStmt.executeUpdate(createViewString);
assertViewExists(true, c, "v");
/*
* Setting another role does not affect the view once
* defined.
*/
setRole(c, "none");
assertViewExists(true, c, "v");
/*
* Remove privileges from role, and the view should be
* gone.
*/
doGrantRevoke(REVOKE, "test_dbo", tabAndColSelectsPerms[i],
grantToThisRole[r], VIEWDROPPED);
assertViewExists(false, c, "v");
/*
* Revoking the role should also invalidate view
*/
doGrantRevoke(GRANT, "test_dbo", tabAndColSelectsPerms[i],
grantToThisRole[r]);
setRole(c, "h");
cStmt.executeUpdate(createViewString);
assertViewExists(true, c, "v");
s.executeUpdate("revoke h from " + roleGrantees[gNo]);
w = s.getWarnings();
assertSQLState(VIEWDROPPED, w);
assertViewExists(false, c, "v");
/*
* Check that user privilege and/or PUBLIC privilege is
* preferred over role privilege if available. This is not
* standard SQL, but useful behavior IMHO as long as Derby
* can't revalidate via another path (DERBY-1632) - lest a
* role revoke or drop causes an invalidation when user has
* discretionary privilege. Cf. also comment on priority of
* user vs public in DERBY-1611.
*/
String[] directGrantee = roleGrantees;
for (int u = 0; u < directGrantee.length; u++) {
s.executeUpdate("grant h to " + roleGrantees[gNo]);
doGrantRevoke(GRANT, "test_dbo",
tabAndColSelectsPerms[i],
directGrantee[u]);
setRole(c, "h");
// Now we have select privilege two ways, via role and
// via user.
cStmt.executeUpdate(createViewString);
// Now revoke role priv and see that view is still
// unaffected.
s.executeUpdate("revoke h from " + roleGrantees[gNo]);
assertViewExists(true, c, "v");
// Take away user privilege, too.
doGrantRevoke(REVOKE, "test_dbo",
tabAndColSelectsPerms[i],
directGrantee[u],
VIEWDROPPED);
assertViewExists(false, c, "v");
}
// clean up
doGrantRevoke(REVOKE, "test_dbo",tabAndColSelectsPerms[i],
grantToThisRole[r]);
}
}
}
/*
* Dropping a role should also invalidate a dependent view.
*
* (We do this test outside the loop above for simplicity of
* reestablish role graph after the drop..)
*/
// drop the current role
doGrantRevoke(GRANT, "test_dbo", g_s, "h");
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
cStmt.executeUpdate(createViewString);
assertViewExists(true, c, "v");
s.executeUpdate("drop role h");
w = s.getWarnings();
assertSQLState(VIEWDROPPED, w);
assertViewExists(false, c, "v");
doGrantRevoke(REVOKE, "test_dbo", g_s, "h");
// re-establish role graph
s.executeUpdate("create role h");
s.executeUpdate("grant e to h");
s.executeUpdate("grant f to h");
// drop an inherited role needed
doGrantRevoke(GRANT, "test_dbo", g_s, "a3");
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
cStmt.executeUpdate(createViewString);
assertViewExists(true, c, "v");
s.executeUpdate("drop role a3");
w = s.getWarnings();
assertSQLState(VIEWDROPPED, w);
assertViewExists(false, c, "v");
doGrantRevoke(REVOKE, "test_dbo", g_s, "h");
// re-establish role graph
s.executeUpdate("create role a3");
s.executeUpdate("grant a3 to d");
cStmt.close();
c.close();
s.close();
dboConn.close();
}
/**
* @see #testViewInvalidation
*/
public void testTriggerInvalidation() throws SQLException {
Connection dboConn = getConnection();
Statement s = dboConn.createStatement();
Connection c = openUserConnection("DonaldDuck");
Statement cStmt = c.createStatement();
SQLWarning w;
/*
* 2-dimensional search space:
*
* Which role we grant the role to (direct to a role or to a role it
* inherits)
* X
* Whether the role is granted directly to the session user or to PUBLIC.
*/
String[] grantToThisRole = new String[] {"a2", "h"};
String[] roleGrantees = new String[] {"DonaldDuck", "public"};
String createTriggerString =
"create trigger t after insert on s1.t1 values 1";
for (int r = 0; r < grantToThisRole.length; r++) {
for (int gNo = 0; gNo < roleGrantees.length; gNo++ ) {
/*
* Create a trigger on the basis of a trigger privilege via a
* role.
*/
s.executeUpdate("grant h to " + roleGrantees[gNo]);
doGrantRevoke(GRANT, "test_dbo", g_t, grantToThisRole[r]);
setRole(c, "h");
cStmt.executeUpdate(createTriggerString);
assertTriggerExists(true, c, "t");
cStmt.executeUpdate(createTriggerString);
/*
* Setting another role does not affect the trigger once
* defined.
*/
setRole(c, "none");
assertTriggerExists(true, c, "t");
setRole(c, "h");
cStmt.executeUpdate(createTriggerString);
// Remove privileges from role, and the trigger should be
// gone.
doGrantRevoke(REVOKE, "test_dbo", g_t, grantToThisRole[r],
TRIGGERDROPPED);
assertTriggerExists(false, c, "t");
/*
* Revoking the role should also invalidate trigger
*/
doGrantRevoke(GRANT, "test_dbo", g_t, grantToThisRole[r]);
setRole(c, "h");
cStmt.executeUpdate(createTriggerString);
assertTriggerExists(true, c, "t");
cStmt.executeUpdate(createTriggerString);
s.executeUpdate("revoke h from " + roleGrantees[gNo]);
w = s.getWarnings();
assertSQLState(TRIGGERDROPPED, w);
assertTriggerExists(false, c, "t");
/*
* Check that user privilege and/or PUBLIC privilege is
* preferred over role privilege if available. This is not
* standard SQL, but useful behavior IMHO as long as Derby
* can't revalidate via another path (DERBY-1632) - lest a
* role revoke or drop causes an invalidation when user has
* discretionary privilege. Cf. also comment on priority of
* user vs public in DERBY-1611.
*/
String[] directGrantee = roleGrantees;
for (int u = 0; u < directGrantee.length; u++) {
s.executeUpdate("grant h to " + roleGrantees[gNo]);
doGrantRevoke(GRANT, "test_dbo", g_t, directGrantee[u]);
setRole(c, "h");
// Now we have trigger privilege two ways,a via role and
// via user.
cStmt.executeUpdate(createTriggerString);
// Now revoke role priv and see that trigger is still
// unaffected.
s.executeUpdate("revoke h from " + roleGrantees[gNo]);
assertTriggerExists(true, c, "t");
cStmt.executeUpdate(createTriggerString);
// take away user privilege, too
doGrantRevoke(REVOKE, "test_dbo",g_t,directGrantee[u],
TRIGGERDROPPED);
assertTriggerExists(false, c, "t");
}
// clean up
doGrantRevoke(REVOKE, "test_dbo", g_t, grantToThisRole[r]);
}
}
/*
* Dropping a role should also invalidate a dependent trigger.
*
* (We do this test outside the loop above for simplicity of
* reestablish role graph after the drop..)
*/
doGrantRevoke(GRANT, "test_dbo", g_t, "h");
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
cStmt.executeUpdate(createTriggerString);
assertTriggerExists(true, c, "t");
cStmt.executeUpdate(createTriggerString);
s.executeUpdate("drop role h");
w = s.getWarnings();
assertSQLState(TRIGGERDROPPED, w);
assertTriggerExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_t, "h");
// re-establish role graph
s.executeUpdate("create role h");
s.executeUpdate("grant e to h");
s.executeUpdate("grant f to h");
/*
* Dropping an EXECUTE privilege used in a trigger body will not drop
* the trigger if the EXECUTE privilege is revoked from a user
* directly, since this currently requires the RESTRICT
* keyword. However, revoking a role does not carry the RESTRICT
* keyword, so any execution privilege conferred through a role is
* revoked, too, and any dependent object, for example a trigger in
* example below, will be dropped.
*/
doGrantRevoke(GRANT, "test_dbo", g_t, "h");
doGrantRevoke(GRANT, "test_dbo", g_e, "h");
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
cStmt.executeUpdate
("create trigger t after insert on s1.t1 values s1.f1()");
assertTriggerExists(true, c, "t");
cStmt.executeUpdate
("create trigger t after insert on s1.t1 values s1.f1()");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(TRIGGERDROPPED, w);
assertTriggerExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_t, "h");
doGrantRevoke(REVOKE, "test_dbo", g_e, "h");
/*
* Check that dependency on role and subsequent invalidation happens
* for a mix of column SELECT privileges granted to user, public and
* role (due to tricky logic in this implementation,
* cf. DDLConstantAction#storeViewTriggerDependenciesOnPrivileges
*/
// SELECT privileges to {public, role} x
// TRIGGER privilege to {user, role}
String triggerPrivGrantees[] = new String[] {"h", "DonaldDuck"};
for (int i=0; i < triggerPrivGrantees.length; i++) {
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_t, triggerPrivGrantees[i]);
doGrantRevoke(GRANT, "test_dbo", g_s_c1, "public");
doGrantRevoke(GRANT, "test_dbo", g_s_c2, "h");
cStmt.executeUpdate
("create trigger t after insert on s1.t1 " +
"select c1,c2 from s1.t1");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(TRIGGERDROPPED, w);
assertTriggerExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_t, triggerPrivGrantees[i]);
doGrantRevoke(REVOKE, "test_dbo", g_s_c1, "public");
doGrantRevoke(REVOKE, "test_dbo", g_s_c2, "h");
}
// SELECT privileges to {user, role} x
// TRIGGER privilege to {user, role}
for (int i=0; i < triggerPrivGrantees.length; i++) {
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_t, triggerPrivGrantees[i]);
doGrantRevoke(GRANT, "test_dbo", g_s_c1, "DonaldDuck");
doGrantRevoke(GRANT, "test_dbo", g_s_c2, "h");
cStmt.executeUpdate
("create trigger t after insert on s1.t1 " +
"select c1,c2 from s1.t1");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(TRIGGERDROPPED, w);
assertTriggerExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_t, triggerPrivGrantees[i]);
doGrantRevoke(REVOKE, "test_dbo", g_s_c1, "DonaldDuck");
doGrantRevoke(REVOKE, "test_dbo", g_s_c2, "h");
}
// SELECT privileges to {user, public, role} x
// TRIGGER privilege to {user, role}
for (int i=0; i < triggerPrivGrantees.length; i++) {
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_t, triggerPrivGrantees[i]);
doGrantRevoke(GRANT, "test_dbo", g_s_c1, "DonaldDuck");
doGrantRevoke(GRANT, "test_dbo", g_s_c2, "public");
doGrantRevoke(GRANT, "test_dbo", g_s_c3, "h");
cStmt.executeUpdate
("create trigger t after insert on s1.t1 " +
"select c1,c2,c3 from s1.t1");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(TRIGGERDROPPED, w);
assertTriggerExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_t, triggerPrivGrantees[i]);
doGrantRevoke(REVOKE, "test_dbo", g_s_c1, "DonaldDuck");
doGrantRevoke(REVOKE, "test_dbo", g_s_c2, "public");
doGrantRevoke(REVOKE, "test_dbo", g_s_c3, "h");
}
cStmt.close();
c.close();
s.close();
dboConn.close();
}
/**
* @see #testViewInvalidation
*/
public void testConstraintInvalidation() throws SQLException {
Connection dboConn = getConnection();
Statement s = dboConn.createStatement();
Connection c = openUserConnection("DonaldDuck");
Statement cStmt = c.createStatement();
SQLWarning w;
/*
* 3-dimensional search space:
*
* Which role we grant the role to (direct to a role or to a role it
* inherits)
* X
* Whether the role is granted directly to the session user or to PUBLIC.
* X
* Whether we grant the entire underlying table or just the column
* needed.
*/
String[] grantToThisRole = new String[] {"a2", "h"};
String[] roleGrantees = new String[] {"DonaldDuck", "public"};
String[][] tabAndColReferencesPerms =
new String[][] {{g_r}, {g_r_c1, g_r_c2, g_r_c3}};
String createTableString =
"create table t (i int not null, j int, k int)";
String dropTableString = "drop table t";
String addConstraintString =
"alter table t add constraint fk " +
"foreign key(i,j,k) references s1.t1";
cStmt.executeUpdate(createTableString);
for (int r = 0; r < grantToThisRole.length; r++) {
for (int gNo = 0; gNo < roleGrantees.length; gNo++ ) {
for (int i = 0; i < tabAndColReferencesPerms.length; i++) {
/*
* Create a foreign key constraint on the basis of a
* references privilege via a role.
*/
s.executeUpdate("grant h to " + roleGrantees[gNo]);
doGrantRevoke(GRANT, "test_dbo",
tabAndColReferencesPerms[i],
grantToThisRole[r]);
setRole(c, "h");
cStmt.executeUpdate(addConstraintString);
assertFkConstraintExists(true, c, "t");
/*
* Setting another role does not affect the constraint once
* defined.
*/
setRole(c, "none");
assertFkConstraintExists(true, c, "t");
// Remove privileges from role, and the constraint should be
// gone.
doGrantRevoke
(REVOKE, "test_dbo",
tabAndColReferencesPerms[i],
grantToThisRole[r],
new String[]{CONSTRAINTDROPPED, null, null});
assertFkConstraintExists(false, c, "t");
/*
* Revoking the role should also invalidate constraint
*/
doGrantRevoke(GRANT, "test_dbo",
tabAndColReferencesPerms[i],
grantToThisRole[r]);
setRole(c, "h");
cStmt.executeUpdate(addConstraintString);
assertFkConstraintExists(true, c, "t");
s.executeUpdate("revoke h from " + roleGrantees[gNo]);
assertFkConstraintExists(false, c, "t");
/*
* Check that user privilege and/or PUBLIC privilege is
* preferred over role privilege if available. This is not
* standard SQL, but useful behavior IMHO as long as Derby
* can't revalidate via another path (DERBY-1632) - lest a
* role revoke or drop causes an invalidation when user has
* discretionary privilege. Cf. also comment on priority of
* user vs public in DERBY-1611.
*/
String[] directGrantee = roleGrantees;
for (int u = 0; u < directGrantee.length; u++) {
s.executeUpdate("grant h to " + roleGrantees[gNo]);
doGrantRevoke(GRANT, "test_dbo",
tabAndColReferencesPerms[i],
directGrantee[u]);
setRole(c, "h");
// Now we have references privilege two ways, via role
// and via user.
cStmt.executeUpdate(addConstraintString);
// Now revoke role priv and see that constraints is
// still unaffected.
s.executeUpdate("revoke h from " + roleGrantees[gNo]);
assertFkConstraintExists(true, c, "t");
// take away user privilege, too
doGrantRevoke
(REVOKE, "test_dbo",
tabAndColReferencesPerms[i],
directGrantee[u],
new String[]{CONSTRAINTDROPPED, null, null});
assertFkConstraintExists(false, c, "t");
}
// clean up
doGrantRevoke
(REVOKE, "test_dbo",
tabAndColReferencesPerms[i],
grantToThisRole[r]);
}
}
}
/*
* Dropping a role should also invalidate a dependent constraint.
*
* (We do this test outside the loop above for simplicity of
* reestablish role graph after the drop..)
*/
doGrantRevoke(GRANT, "test_dbo", g_r, "h");
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
cStmt.executeUpdate(addConstraintString);
assertFkConstraintExists(true, c, "t");
s.executeUpdate("drop role h");
w = s.getWarnings();
assertSQLState(CONSTRAINTDROPPED, w);
assertFkConstraintExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_s, "h");
// re-establish role graph
s.executeUpdate("create role h");
s.executeUpdate("grant e to h");
s.executeUpdate("grant f to h");
/*
* For FOREIGN KEY constraint, check that dependency on role and
* subesquent invalidation happens for a mix of column privileges
* granted to user, public and role (due to tricky logic in this
* implementation,
* cf. DDLConstantAction#storeConstraintDependenciesOnPrivileges
*/
// {role, role}
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo",
new String[] {g_r_c1, g_r_c2, g_r_c3}, "h");
cStmt.executeUpdate
("alter table t add constraint fk foreign key(i,j,k) " +
"references s1.t1");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(CONSTRAINTDROPPED, w);
assertFkConstraintExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo",
new String[] {g_r_c1, g_r_c2, g_r_c3}, "h");
// {public, role}
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_r_c1, "public");
doGrantRevoke(GRANT, "test_dbo", g_r_c2, "h");
doGrantRevoke(GRANT, "test_dbo", g_r_c3, "h");
cStmt.executeUpdate("alter table t add constraint fk " +
"foreign key(i,j,k) references s1.t1");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(CONSTRAINTDROPPED, w);
assertFkConstraintExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_r_c1, "public");
doGrantRevoke(REVOKE, "test_dbo", g_r_c2, "h");
doGrantRevoke(REVOKE, "test_dbo", g_r_c3, "h");
// {user, role}
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_r_c1, "DonaldDuck");
doGrantRevoke(GRANT, "test_dbo", g_r_c2, "h");
doGrantRevoke(GRANT, "test_dbo", g_r_c3, "h");
cStmt.executeUpdate("alter table t add constraint fk " +
"foreign key(i,j,k) references s1.t1");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(CONSTRAINTDROPPED, w);
assertFkConstraintExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_r_c1, "DonaldDuck");
doGrantRevoke(REVOKE, "test_dbo", g_r_c2, "h");
doGrantRevoke(REVOKE, "test_dbo", g_r_c3, "h");
// {user, public, role}
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_r_c1, "DonaldDuck");
doGrantRevoke(GRANT, "test_dbo", g_r_c2, "public");
doGrantRevoke(GRANT, "test_dbo", g_r_c3, "h");
cStmt.executeUpdate("alter table t add constraint fk " +
"foreign key(i,j,k) references s1.t1");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(CONSTRAINTDROPPED, w);
assertFkConstraintExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_r_c1, "DonaldDuck");
doGrantRevoke(REVOKE, "test_dbo", g_r_c2, "public");
doGrantRevoke(REVOKE, "test_dbo", g_r_c3, "h");
// Try the same as above but with EXECUTE privilege instead of
// REFERENCES for a CHECK constraint
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_e, "h");
cStmt.executeUpdate("alter table t add constraint ch " +
"check(i < s1.f1())");
assertCheckConstraintExists(true, c, "t");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(CONSTRAINTDROPPED, w);
assertCheckConstraintExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_e, "h");
// Try the same as above but with two EXECUTE privileges
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_e, "h");
doGrantRevoke(GRANT, "test_dbo", g_e_f2, "DonaldDuck");
cStmt.executeUpdate("alter table t add constraint ch " +
"check(i < (s1.f1() + s1.f2()))");
assertCheckConstraintExists(true, c, "t");
s.executeUpdate("revoke h from DonaldDuck");
w = s.getWarnings();
assertSQLState(CONSTRAINTDROPPED, w);
assertCheckConstraintExists(false, c, "t");
doGrantRevoke(REVOKE, "test_dbo", g_e, "h");
doGrantRevoke(REVOKE, "test_dbo", g_e_f2, "DonaldDuck");
// Try the same as above but with multiple CHECK constraints to verify
// that only those affected by a revoke are impacted.
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
doGrantRevoke(GRANT, "test_dbo", g_e, "h");
doGrantRevoke(GRANT, "test_dbo", g_e_f2, "DonaldDuck");
cStmt.executeUpdate
("create table tmp(i int constraint ct1 check(i < s1.f1())," +
" j int constraint ct2 check(j < s1.f2()))");
s.executeUpdate("revoke h from DonaldDuck");
// This should only impact ct1
try {
cStmt.executeUpdate("insert into tmp values (6, -1)");
} catch (SQLException e) {
fail("expected success", e);
}
try {
cStmt.executeUpdate("insert into tmp values (6, 6)");
fail("ct2 should remain");
} catch (SQLException e) {
assertSQLState(CHECKCONSTRAINTVIOLATED, e);
}
cStmt.executeUpdate("alter table tmp drop constraint ct2");
doGrantRevoke(REVOKE, "test_dbo", g_e, "h");
doGrantRevoke(REVOKE, "test_dbo", g_e_f2, "DonaldDuck");
cStmt.executeUpdate("drop table tmp");
cStmt.executeUpdate(dropTableString);
cStmt.close();
c.close();
s.close();
dboConn.close();
}
/**
* DERBY-4191
* There are times when no column is selected from a table in the from
* list. At such a time, we should make sure that we make sure there
* is atleast some kind of select privilege available on that table for
* the query to succeed. eg of such queries
* select count(*) from t1
* select count(1) from t1
* select 1 from t1
* select t1.c1 from t1, t2
*
* In addition, the subquery inside of a NON-select query should require
* select privilege on the tables involved in the subquery eg
* update dbo.t set a = ( select max(a1) + 2 from dbo.t1 )
* update dbo.t set a = ( select max(b1) + 2 from dbo.t2 )
* For both the queries above, in addition to update privilege requirement
* on dbo.t(a), we need to require select privileges on columns/tables
* within the select list. So for first query, the user should have select
* privilege on dbo.t1 or dbo.t1(a1). Similarly, for 2nd query, the user
* should have select privilege on dbo.t2 or dbo.t2(b1)
* @throws SQLException
*/
public void testMinimumSelectPrivilege() throws SQLException {
Connection dboConn = getConnection();
Statement stmtDBO = dboConn.createStatement();
Connection cDD = openUserConnection("DonaldDuck");
Statement stmtDD = cDD.createStatement();
Connection cMM = openUserConnection("MickeyMouse");
Statement stmtMM = cMM.createStatement();
stmtDBO.executeUpdate("create role role1");
stmtDBO.executeUpdate("grant role1 to MickeyMouse");
stmtDD.executeUpdate("create table DDtable1(c11 int, c12 int)");
stmtDD.executeUpdate("insert into DDtable1 values(1, 2)");
stmtDD.executeUpdate("create table DDtable2(c21 int, c22 int)");
stmtDD.executeUpdate("insert into DDtable2 values(3, 4)");
stmtMM.executeUpdate("set role role1");
try {
stmtMM.executeQuery("select c11 from DonaldDuck.DDtable1");
fail("select should have failed");
} catch (SQLException e) {
assertSQLState("42502", e);
}
try {
stmtMM.executeUpdate("update DonaldDuck.DDtable1 set c11 = " +
" (select c21 from DonaldDuck.DDtable2)");
fail("select should have failed");
} catch (SQLException e) {
assertSQLState("42502", e);
}
stmtDD.executeUpdate("grant select(c12) on DDtable1 to role1");
stmtDD.executeUpdate("grant update on DDtable1 to role1");
stmtMM.executeQuery("select c12 from DonaldDuck.DDtable1");
try {
stmtMM.executeQuery("select c11 from DonaldDuck.DDtable1");
fail("select should have failed");
} catch (SQLException e) {
assertSQLState("42502", e);
}
try {
stmtMM.executeUpdate("update DonaldDuck.DDtable1 set c11 = " +
" (select c21 from DonaldDuck.DDtable2)");
fail("select should have failed");
} catch (SQLException e) {
assertSQLState("42502", e);
}
stmtDD.executeUpdate("grant select(c11) on DDtable1 to role1");
stmtMM.executeQuery("select c12 from DonaldDuck.DDtable1");
stmtMM.executeQuery("select c11 from DonaldDuck.DDtable1");
try {
stmtMM.executeQuery("select c11 from DonaldDuck.DDtable1, " +
"DonaldDuck.DDtable2");
fail("select should have failed");
} catch (SQLException e) {
assertSQLState("42500", e);
}
try {
stmtMM.executeQuery("update DonaldDuck.DDtable1 set c11 = " +
" (select c21 from DonaldDuck.DDtable2)");
fail("select should have failed");
} catch (SQLException e) {
assertSQLState("42502", e);
}
stmtDD.executeUpdate("grant select(c21) on DDtable2 to role1");
stmtMM.executeQuery("select c12 from DonaldDuck.DDtable1");
stmtMM.executeQuery("select c11 from DonaldDuck.DDtable1");
stmtMM.executeQuery("select c11 from DonaldDuck.DDtable1, " +
"DonaldDuck.DDtable2");
stmtMM.executeUpdate("update DonaldDuck.DDtable1 set c11 = " +
" (select c21 from DonaldDuck.DDtable2)");
}
/**
* Test that a prepared statement can no longer execute after its required
* privileges acquired via the current role are no longer applicable.
*/
public void testPSInvalidation() throws SQLException {
Connection dboConn = getConnection();
Statement s = dboConn.createStatement();
Connection c = openUserConnection("DonaldDuck");
Statement cStmt = c.createStatement();
/*
* 3-dimensional search space:
*
* Which role we grant the role to (direct to a role or to a role it
* inherits)
* X
* Whether the role is granted directly to the session user or to PUBLIC.
* X
* Whether we grant the entire underlying table or just the column
* needed.
*/
String[] grantToThisRole = new String[] {"a2", "h"};
String[] roleGrantees = new String[] {"DonaldDuck", "public"};
String[][] privilegeStmts =
new String[][] {{g_s, "select c1 from s1.t1"},
{g_s_c1, "select c1 from s1.t1"},
{g_e, "values s1.f1()"},
{g_u, "update s1.t1 set c1=0"},
{g_u_c1_c2_c3, "update s1.t1 set c1=0"},
{g_i, "insert into s1.t1 values (5,5,5)"}};
PreparedStatement ps = null;
for (int r = 0; r < grantToThisRole.length; r++) {
for (int gNo = 0; gNo < roleGrantees.length; gNo++ ) {
for (int i = 0; i < privilegeStmts.length; i++) {
/*
* Create a ps on the basis of a select privilege via a
* role.
*/
s.executeUpdate("grant h to " + roleGrantees[gNo]);
doGrantRevoke(GRANT, "test_dbo", privilegeStmts[i][0],
grantToThisRole[r]);
setRole(c, "h");
ps = c.prepareStatement(privilegeStmts[i][1]);
assertPsWorks(true, ps);
/*
* Setting another role should make the ps fail, since we
* no longer have the privilege.
*/
setRole(c, "none");
assertPsWorks(false, ps);
// set it back:
setRole(c, "h");
assertPsWorks(true, ps);
/*
* Remove privileges from role, and the execute should
* fail.
*/
doGrantRevoke(REVOKE, "test_dbo", privilegeStmts[i][0],
grantToThisRole[r]);
assertPsWorks(false, ps);
doGrantRevoke(GRANT, "test_dbo", privilegeStmts[i][0],
grantToThisRole[r]);
/*
* Revoking the role should also make the ps fail, since we
* no longer have the privilege.
*/
setRole(c, "h");
assertPsWorks(true, ps);
s.executeUpdate("revoke h from " + roleGrantees[gNo]);
assertPsWorks(false, ps);
/*
* Check that prepared statements are reprepared if there
* is another applicable privilege, when the privilege
* granted via a role is used first and that role is
* revoked.
*/
String[] directGrantee = roleGrantees;
// iterate over granting role h to {user, PUBLIC}
for (int u = 0; u < directGrantee.length; u++) {
s.executeUpdate("grant h to " + roleGrantees[gNo]);
setRole(c, "h");
assertPsWorks(true, ps);
doGrantRevoke(GRANT, "test_dbo",
privilegeStmts[i][0],
directGrantee[u]);
// Now we have select privilege two ways, via role and
// via user or PUBLIC.
// Now revoke role priv and see that ps is still
// unaffected.
s.executeUpdate("revoke h from " + roleGrantees[gNo]);
assertPsWorks(true, ps);
// Take away user privilege, too.
doGrantRevoke(REVOKE, "test_dbo",
privilegeStmts[i][0],
directGrantee[u]);
assertPsWorks(false, ps);
}
// clean up
doGrantRevoke(REVOKE, "test_dbo",privilegeStmts[i][0],
grantToThisRole[r]);
}
}
}
/*
* Dropping a role should also cause a dependent ps fail.
*
* (We do this test outside the loop above for simplicity of
* reestablish role graph after the drop..)
*
*/
for (int i=0; i < privilegeStmts.length; i++) {
doGrantRevoke(GRANT, "test_dbo", privilegeStmts[i][0], "h");
s.executeUpdate("grant h to DonaldDuck");
setRole(c, "h");
ps = c.prepareStatement(privilegeStmts[i][1]);
assertPsWorks(true, ps);
s.executeUpdate("drop role h");
assertPsWorks(false, ps);
doGrantRevoke(REVOKE, "test_dbo", privilegeStmts[i][0], "h");
// re-establish role graph
s.executeUpdate("create role h");
s.executeUpdate("grant e to h");
s.executeUpdate("grant f to h");
}
cStmt.close();
c.close();
s.close();
dboConn.close();
}
/**
* Test behavior for when there are open result sets on prepared statements
* that require privileges obtained via the current role and something
* changes in the middle of accessing the result set. We should be able to
* finish using the result set.
*/
public void testOpenRs() throws SQLException {
Connection dboConn = getConnection();
Statement s = dboConn.createStatement();
Connection c = openUserConnection("DonaldDuck");
Statement cStmt = c.createStatement();
ResultSet rs = null;
String select = "select * from s1.t1";
PreparedStatement ps = dboConn.prepareStatement(
"insert into s1.t1 values (?,?,?)");
for (int i=0; i < 5; i++) {
ps.setInt(1, i);
ps.setInt(2, i);
ps.setInt(3, i);
ps.execute();
}
/*
* Select privilege revoked
*/
// Auto-commit on
doGrantRevoke(GRANT, "test_dbo", g_s, "h");
s.execute("grant h to DonaldDuck");
setRole(c, "h");
rs = cStmt.executeQuery(select);
rs.next();
// Now remove privilege in middle of rs reading
doGrantRevoke(REVOKE, "test_dbo", g_s, "h");
// check that we can read the next row
rs.next();
rs.close();
// Auto-commit off
c.setAutoCommit(false);
doGrantRevoke(GRANT, "test_dbo", g_s, "h");
setRole(c, "h");
rs = cStmt.executeQuery(select);
rs.next();
c.commit();
// Now remove privilege in middle of rs reading
doGrantRevoke(REVOKE, "test_dbo", g_s, "h");
// check that we can read the next row
rs.next();
rs.close();
c.setAutoCommit(true);
/*
* Role privilege revoked
*/
// Auto-commit on
doGrantRevoke(GRANT, "test_dbo", g_s, "h");
s.execute("grant h to DonaldDuck");
setRole(c, "h");
rs = cStmt.executeQuery(select);
rs.next();
// Now remove privilege in middle of rs reading
s.execute("revoke h from DonaldDuck");
// check that we can read the next row
rs.next();
rs.close();
// Auto-commit off
c.setAutoCommit(false);
s.execute("grant h to DonaldDuck");
setRole(c, "h");
rs = cStmt.executeQuery(select);
rs.next();
c.commit();
// Now remove privilege in middle of rs reading
s.execute("revoke h from DonaldDuck");
// check that we can read the next row
rs.next();
rs.close();
c.setAutoCommit(true);
doGrantRevoke(REVOKE, "test_dbo", g_s, "h");
/*
* Current role changed
*/
// Auto-commit on
doGrantRevoke(GRANT, "test_dbo", g_s, "h");
s.execute("grant h to DonaldDuck");
setRole(c, "h");
c.setAutoCommit(true);
rs = cStmt.executeQuery(select);
rs.next();
// Now change role in middle of rs reading
setRole(c, "none");
// check that we can read the next row
rs.next();
rs.close();
// Auto-commit off
c.setAutoCommit(false);
setRole(c, "h");
rs = cStmt.executeQuery(select);
rs.next();
// Now remove privilege in middle of rs reading
c.commit();
setRole(c, "none");
// check that we can read the next row
rs.next();
rs.close();
c.setAutoCommit(true);
doGrantRevoke(REVOKE, "test_dbo", g_s, "h");
// clean up
s.executeUpdate("delete from s1.t1");
c.close();
dboConn.close();
}
/**
* Test that DEFAULT CURRENT_ROLE works as expected
* See DERBY-3897.
*/
public void testDefaultCurrentRole() throws SQLException {
Connection dboConn = getConnection();
Statement s = dboConn.createStatement();
s.execute("grant h to DonaldDuck");
Connection c = openUserConnection("DonaldDuck");
Statement cStmt = c.createStatement();
setRole(c, "h");
// CREATE TABLE
cStmt.executeUpdate
("create table t(role varchar(128) default current_role)");
cStmt.executeUpdate("insert into t values default");
ResultSet rs = cStmt.executeQuery("select * from t");
JDBC.assertSingleValueResultSet(rs, "\"H\"");
rs.close();
cStmt.executeUpdate("drop table t");
// ALTER TABLE
cStmt.executeUpdate("create table t(i int)");
cStmt.executeUpdate("insert into t values 1");
cStmt.executeUpdate
("alter table t " +
"add column role varchar(10) default current_role");
rs = cStmt.executeQuery("select * from t");
JDBC.assertFullResultSet(rs, new String[][]{{"1", "\"H\""}});
rs.close();
cStmt.executeUpdate("drop table t");
// do the same from within a stored procedure
s.execute("grant execute on procedure s1.calledNested to DonaldDuck");
if (!JDBC.vmSupportsJSR169()) {
// JSR169 cannot run with tests with stored procedures
// that do database access - for they require a
// DriverManager connection to jdbc:default:connection;
// DriverManager is not supported with JSR169.
cStmt.executeUpdate("call s1.calledNested()");
}
setRole(c, "none");
cStmt.close();
s.execute("revoke h from DonaldDuck");
s.execute("revoke execute on procedure s1.calledNested " +
"from DonaldDuck restrict");
s.close();
c.close();
dboConn.close();
}
/**
* Test that CURRENT_ROLE works as expected in some miscellaneous contexts.
* See DERBY-3897.
*/
public void testCurrentRoleInWeirdContexts() throws SQLException {
if (JDBC.vmSupportsJSR169()) {
// JSR169 cannot run with tests with stored procedures
// that do database access - for they require a
// DriverManager connection to jdbc:default:connection;
// DriverManager is not supported with JSR169.
return;
}
Connection dboConn = getConnection();
Statement s = dboConn.createStatement();
setRole(dboConn, "a1");
s.execute("create table trackCreds(usr varchar(30), role varchar(30))");
s.executeUpdate("create table t(i int)");
s.execute("grant insert on t to DonaldDuck");
s.execute("grant h to DonaldDuck");
// From within a trigger body:
s.execute("create trigger tr after insert on t " +
"insert into trackCreds values (current_user, current_role)");
Connection c = openUserConnection("DonaldDuck");
Statement cStmt = c.createStatement();
setRole(c, "h");
cStmt.executeUpdate("insert into test_dbo.t values 1");
ResultSet rs = s.executeQuery("select * from trackCreds");
JDBC.assertFullResultSet(rs, new String[][]{{"DONALDDUCK", "\"H\""}});
rs.close();
setRole(c, "none");
cStmt.close();
// From within a CHECK constraint, that we get an error
try {
s.execute("create table strange(role varchar(30) " +
"check (role = current_role))");
fail("current_role inside a check constraint should be denied");
} catch (SQLException e) {
assertSQLState(UNRELIABLE, e);
}
// From within a function, called from a CHECK constraint
// executed as a substatement as part of ALTER TABLE.
// In this case, the session context stack contains two elements
// referenced from the three activations thus:
//
// top level "alter table" act. -> top level session context
// substatement (check constraint act.) -> top level session context
// nested connnection in getCurrentRole,
// "values current_role" act. -> pushed session context
//
// Before DERBY-3897 the call to s1.getCurrentRole would yield null
// because the pushed session context for getCurrentRole would inherit
// a wrong (newly created) session context from the CHECK constraint
// substatement's activation. After DERBY-3897, the substatement
// correctly inherits the session context of "alter table"s
// activation, so the pushed session context for getCurrentRole will
// be correct, too.
//
s.execute("create table strange(i int)");
s.execute("insert into strange values null");
s.execute("alter table strange " +
"add constraint s check (s1.getCurrentRole() = '\"A1\"')");
s.execute("revoke h from DonaldDuck");
s.execute("revoke insert on t from DonaldDuck");
setRole(dboConn, "none");
s.execute("drop table t");
s.execute("drop table strange");
s.execute("drop table trackCreds");
s.close();
c.close();
dboConn.close();
}
/**
* stored function: s1.f1
*/
public static int s1f1() {
return 0;
}
private void assertAllforRole(int hasPrivilege,
Connection c,
String grantee) throws SQLException {
for (int i=0; i < grantRevokes.length; i++) {
doGrantRevoke(GRANT, "test_dbo", grantRevokes[i], grantee);
assertEverything(hasPrivilege, c, null);
doGrantRevoke(REVOKE, "test_dbo", grantRevokes[i], grantee);
// check that when priv is revoked we no longer have it
if (hasPrivilege == VIAROLE) {
assertEverything(NOPRIV, c, null);
}
}
}
private void assertEverything(int hasPrivilege,
String user,
String role) throws SQLException {
Connection c = openUserConnection(user);
assertEverything(hasPrivilege, c, role);
c.close();
}
private void assertEverything(int hasPrivilege,
Connection c,
String role) throws SQLException {
if (role != null) {
setRole(c, role);
}
String[] columns = new String[] {"c1", "c2"};
String schema = "s1";
String table = "t1";
String function = "f1";
assertSelectPrivilege
(hasPrivilege, c, schema, table, columns);
assertSelectPrivilege
(hasPrivilege, c, schema, table, null);
assertInsertPrivilege
(hasPrivilege, c, schema, table, null);
assertUpdatePrivilege
(hasPrivilege, c, schema, table, columns);
assertUpdatePrivilege
(hasPrivilege, c, schema, table, null);
assertDeletePrivilege
(hasPrivilege, c, schema, table);
assertReferencesPrivilege
(hasPrivilege, c, schema, table, columns);
assertReferencesPrivilege
(hasPrivilege, c, schema, table, null);
assertTriggerPrivilege
(hasPrivilege, c, schema, table);
assertExecutePrivilege(hasPrivilege, c, schema, function);
}
/**
* Assert that a user has execute privilege on a given function
*
* @param hasPrivilege whether or not the user has the privilege
* @param user the user to check
* @param role to use, or null if we do not want to set the role
* @param schema the schema to check
* @param function the name of the function to check
* @throws SQLException throws all exceptions
*/
private void assertExecutePrivilege(int hasPrivilege,
String user,
String role,
String schema,
String function) throws SQLException {
Connection c = openUserConnection(user);
if (role != null) {
setRole(c, role);
}
assertExecutePrivilege(hasPrivilege, c, schema, function);
c.close();
}
/**
* Assert that a user has execute privilege on a given function
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param function the name of the function to check
* @throws SQLException throws all exceptions
*/
private void assertExecutePrivilege(int hasPrivilege,
Connection c,
String schema,
String function) throws SQLException {
Statement stm = c.createStatement();
try {
ResultSet rs =
stm.executeQuery("values " + schema + "." + function + "()");
rs.next();
rs.close();
stm.close();
if (hasPrivilege == NOPRIV) {
fail("expected no EXECUTE privilege on function. " +
formatArgs(c, schema, function));
}
} catch (SQLException e) {
if (stm != null) {
stm.close();
}
if (hasPrivilege == NOPRIV)
assertSQLState(NOEXECUTEPERMISSION, e);
else {
fail("Unexpected lack of execute privilege. " +
formatArgs(c, schema, function), e);
}
}
}
/**
* Assert that a user has trigger privilege on a given table
*
* @param hasPrivilege whether or not the user has the privilege
* @param user the user to check
* @param role to use, or null if we do not want to set the role
* @param schema the schema to check
* @param table the name of the table to check
* @throws SQLException throws all exceptions
*/
private void assertTriggerPrivilege(int hasPrivilege,
String user,
String role,
String schema,
String table) throws SQLException {
Connection c = openUserConnection(user);
if (role != null) {
setRole(c, role);
}
assertTriggerPrivilege(hasPrivilege, c, schema, table);
c.close();
}
/**
* Assert that a user has trigger execute privilege on a given table /
* column set.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @throws SQLException throws all exceptions
*/
private void assertTriggerPrivilege(int hasPrivilege,
Connection c,
String schema,
String table) throws SQLException {
Statement s = c.createStatement();
String triggerName = table + "Trigger";
try {
int i = s.executeUpdate
("create trigger " + triggerName + " after insert on " +
schema + "." + table + " for each row values 1");
if (hasPrivilege != NOPRIV) {
assertEquals(0, i);
}
s.execute("drop trigger " + triggerName);
if (hasPrivilege == NOPRIV) {
fail("expected no TRIGGER privilege on table. " +
formatArgs(c, schema, table));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(NOTABLEPERMISSION, e);
} else {
fail("Unexpected lack of trigger privilege. " +
formatArgs(c, schema, table), e);
}
}
s.close();
assertPrivilegeMetadata
(hasPrivilege, c, "TRIGGER", schema, table, null);
}
/**
* Assert that a user has references privilege on a given table
*
* @param hasPrivilege whether or not the user has the privilege
* @param user the user to check
* @param role to use, or null if we do not want to set the role
* @param schema the schema to check
* @param table the name of the table to check
* @param columns the name of the columns to check, or null
* @throws SQLException throws all exceptions
*/
private void assertReferencesPrivilege(int hasPrivilege,
String user,
String role,
String schema,
String table,
String[] columns)
throws SQLException {
Connection c = openUserConnection(user);
if (role != null) {
setRole(c, role);
}
assertReferencesPrivilege(hasPrivilege, c, schema, table, columns);
c.close();
}
/**
* Assert that a user has references privilege on a given table / column
* set.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @param columns the set of columns to check
* @throws SQLException throws all exceptions
*/
private void assertReferencesPrivilege(int hasPrivilege,
Connection c,
String schema,
String table,
String[] columns)
throws SQLException {
Statement s = c.createStatement();
columns = ((columns == null)
? getAllColumns(schema, table)
: columns);
for (int i = 0; i < columns.length; i++) {
try {
s.execute("create table referencestest (c1 int" +
" references " + schema + "." +
table + "(" + columns[i] + "))" );
s.execute("drop table referencestest");
if (hasPrivilege == NOPRIV) {
fail("Unexpected references privilege. " +
formatArgs(c, schema, table,
new String[]{columns[i]}));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(NOCOLUMNPERMISSION, e);
} else {
fail("Unexpected lack of references privilege. " +
formatArgs(c, schema, table,
new String[]{columns[i]}),
e);
}
}
}
s.close();
assertPrivilegeMetadata
(hasPrivilege, c, "REFERENCES", schema, table, columns);
}
/**
* Assert that a user has update privilege on a given table
*
* @param hasPrivilege whether or not the user has the privilege
* @param user the user to check
* @param role to use, or null if we do not want to set the role
* @param schema the schema to check
* @param table the name of the table to check
* @param columns the name of the columns to check, or null
* @throws SQLException throws all exceptions
*/
private void assertUpdatePrivilege(int hasPrivilege,
String user,
String role,
String schema,
String table,
String[] columns)
throws SQLException {
Connection c = openUserConnection(user);
if (role != null) {
setRole(c, role);
}
assertUpdatePrivilege(hasPrivilege, c, schema, table, columns);
c.close();
}
/**
* Assert that a user has update privilege on a given table / column set.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @param columns the set of columns to check
* @throws SQLException throws all exceptions
*/
private void assertUpdatePrivilege(int hasPrivilege,
Connection c,
String schema,
String table,
String[] columns)
throws SQLException {
String[] checkColumns =
(columns == null) ? getAllColumns(schema, table) : columns;
Statement s = c.createStatement();
int columnCount = 0;
boolean checkCount;
for (int i = 0; i < checkColumns.length; i++) {
checkCount = false;
try {
// Try to get count of rows to verify update rows. We may not
// have select privilege on the column, in which case, we
// simply don't verify the count.
try {
ResultSet countRS =
s.executeQuery("select count(" + checkColumns[i] +
") from " + schema + "." + table);
if (!countRS.next()) {
fail("Could not get count on " + checkColumns[i] +
" to verify update");
}
columnCount = countRS.getInt(1);
checkCount = true;
} catch (SQLException e) {
assertSQLState(NOCOLUMNPERMISSION, e);
}
int actualCount =
s.executeUpdate("update " + schema + "." + table +
" set " + checkColumns[i] + "= 0");
if (hasPrivilege != NOPRIV && checkCount) {
// update count should equal select count
assertEquals(columnCount, actualCount);
}
if (hasPrivilege == NOPRIV) {
fail("expected no UPDATE privilege on " +
formatArgs(c, schema, table,
new String[]{checkColumns[i]}));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(NOCOLUMNPERMISSION, e);
} else {
fail("Unexpected lack of privilege to update. " +
formatArgs(c, schema, table,
new String[]{checkColumns[i]}));
}
}
}
s.close();
assertPrivilegeMetadata
(hasPrivilege, c, "UPDATE", schema, table, columns);
}
/**
* Assert that a user has insert privilege on a given table
*
* @param hasPrivilege whether or not the user has the privilege
* @param user the user to check
* @param role to use, or null if we do not want to set the role
* @param schema the schema to check
* @param table the name of the table to check
* @param columns the name of the columns to check, or null
* @throws SQLException throws all exceptions
*/
private void assertInsertPrivilege(int hasPrivilege,
String user,
String role,
String schema,
String table,
String[] columns)
throws SQLException {
Connection c = openUserConnection(user);
if (role != null) {
setRole(c, role);
}
assertInsertPrivilege(hasPrivilege, c, schema, table, columns);
c.close();
}
/**
* Assert that a user has insert privilege on a given table / column set.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @param columns the set of columns to check
* @throws SQLException throws all exceptions
*/
private void assertInsertPrivilege(int hasPrivilege,
Connection c,
String schema,
String table,
String[] columns)
throws SQLException {
Statement s = c.createStatement();
try {
int i = s.executeUpdate("insert into " + schema + "." +
table + " values (0,0,0)");
if (hasPrivilege == NOPRIV) {
fail("expected no INSERT privilege on table, " +
formatArgs(c, schema, table, columns));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(NOTABLEPERMISSION, e);
} else {
fail("Unexpected lack of insert privilege. " +
formatArgs(c, schema, table, columns), e);
}
}
s.close();
assertPrivilegeMetadata
(hasPrivilege, c, "INSERT", schema, table, columns);
}
/**
* Assert that a user has select privilege on a given table
*
* @param hasPrivilege whether or not the user has the privilege
* @param user the user to check
* @param role to use, or null if we do not want to set the role
* @param schema the schema to check
* @param table the name of the table to check
* @param columns the name of the columns to check, or null
* @throws SQLException throws all exceptions
*/
private void assertSelectPrivilege(int hasPrivilege,
String user,
String role,
String schema,
String table,
String[] columns)
throws SQLException {
Connection c = openUserConnection(user);
if (role != null) {
setRole(c, role);
}
assertSelectPrivilege(hasPrivilege, c, schema, table, columns);
c.close();
}
/**
* Assert that a user has select privilege on a given table / column set.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @param columns the set of columns to check
* @throws SQLException throws all exceptions
*/
private void assertSelectPrivilege(int hasPrivilege,
Connection c,
String schema,
String table,
String[] columns) throws SQLException {
assertSelectPrivilege(hasPrivilege, c, schema,
table, columns, NOCOLUMNPERMISSION);
assertSelectConstantPrivilege(hasPrivilege, c, schema,
table, NOTABLEPERMISSION);
assertSelectCountPrivilege(hasPrivilege, c, schema,
table, columns, NOTABLEPERMISSION);
}
/**
* Assert that a user has select privilege at the table(s) level or
* atleast on one column from each of the tables involved in the
* query when running a select query which selects count(*) or
* count(constant) from the tables.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @param columns used for error handling if ran into exception
* @param sqlState expected state if hasPrivilege == NOPRIV
* @throws SQLException throws all exceptions
*/
private void assertSelectCountPrivilege(int hasPrivilege,
Connection c,
String schema,
String table,
String[] columns,
String sqlState) throws SQLException {
Statement s = c.createStatement();
try {
s.execute("select count(*) from " + schema + "." + table);
if (hasPrivilege == NOPRIV) {
fail("expected no SELECT privilege on table " +
formatArgs(c, schema, table, columns));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(sqlState, e);
} else {
fail("Unexpected lack of select privilege. " +
formatArgs(c, schema, table, columns), e);
}
}
try {
s.execute("select count('a') from " + schema + "." + table);
if (hasPrivilege == NOPRIV) {
fail("expected no SELECT privilege on table " +
formatArgs(c, schema, table, columns));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(sqlState, e);
} else {
fail("Unexpected lack of select privilege. " +
formatArgs(c, schema, table, columns), e);
}
}
s.close();
}
/**
* Assert that a user has select privilege at the table(s) level or
* atleast on one column from each of the tables involved in the
* query when running a select query which only selects constants from
* the tables.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @param sqlState expected state if hasPrivilege == NOPRIV
* @throws SQLException throws all exceptions
*/
private void assertSelectConstantPrivilege(int hasPrivilege,
Connection c,
String schema,
String table,
String sqlState) throws SQLException {
Statement s = c.createStatement();
try {
s.execute("select 1 from " + schema + "." + table);
if (hasPrivilege == NOPRIV) {
fail("expected no SELECT privilege on table " +
formatArgs(c, schema, table));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(sqlState, e);
} else {
fail("Unexpected lack of select privilege. " +
formatArgs(c, schema, table), e);
}
}
s.close();
}
/**
* Assert that a user has select privilege on a given table / column set.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @param columns the set of columns to check
* @param sqlState expected state if hasPrivilege == NOPRIV
* @throws SQLException throws all exceptions
*/
private void assertSelectPrivilege(int hasPrivilege,
Connection c,
String schema,
String table,
String[] columns,
String sqlState) throws SQLException {
Statement s = c.createStatement();
try {
s.execute("select " + columnListAsString(columns) +
" from " + schema + "." + table);
if (hasPrivilege == NOPRIV) {
fail("expected no SELECT privilege on table " +
formatArgs(c, schema, table, columns));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(sqlState, e);
} else {
fail("Unexpected lack of select privilege. " +
formatArgs(c, schema, table, columns), e);
}
}
s.close();
assertPrivilegeMetadata
(hasPrivilege, c, "SELECT", schema, table, columns);
}
/**
* Check that a given view exists (select privilege assumed) or not by
* selecting from it. The connection user must supposed to be the owner for
* this to work.
*/
private void assertViewExists(boolean exists,
Connection c,
String table) throws SQLException {
Statement s = c.createStatement();
try {
s.execute("select * from " + table);
if (!exists) {
fail("Table expected not to exist: " + table);
}
} catch (SQLException e) {
if (exists) {
fail("Table expected to exist: " + table, e);
}
assertSQLState(TABLENOTFOUND, e);
}
s.close();
}
/**
* Check that a given trigger exists (select privilege assumed) or not.
* NOTE: It is destructive, since the test is by dropping the trigger. The
* connection user must supposed to be the owner for this to work.
*/
private void assertTriggerExists(boolean exists,
Connection c,
String trigger) throws SQLException {
Statement s = c.createStatement();
try {
s.execute("drop trigger " + trigger);
if (!exists) {
fail("Trigger expected not to exist: " + trigger);
}
} catch (SQLException e) {
if (exists) {
fail("Trigger expected to exist: " + trigger, e);
}
assertSQLState(OBJECTNOTFOUND, e);
}
s.close();
}
/**
* Check that a given foregin key constraint exists by the following
* method: We insert a value that is not present in the referenced table so
* the foreign key constraint will fail if the constraint is present. The
* connection user must be the owner for this to work.
*/
private void assertFkConstraintExists(boolean exists,
Connection c,
String table)
throws SQLException {
assertConstraintExists(exists, c, table, FKVIOLATION);
}
/**
* Check that a given check constraint exists by the following method: We
* insert a value that does not satify the check constraint. The connection
* user must be the owner for this to work.
*/
private void assertCheckConstraintExists(boolean exists,
Connection c,
String table)
throws SQLException {
assertConstraintExists(exists, c, table, CHECKCONSTRAINTVIOLATED);
}
private void assertConstraintExists(boolean exists,
Connection c,
String table,
String sqlState)
throws SQLException {
Statement s = c.createStatement();
try {
s.execute("insert into " + table + " values (6,6,6)");
s.execute("delete from " + table);
if (exists) {
fail("Table expected to have a constraint: " + table);
}
} catch (SQLException e) {
if (!exists) {
fail("Table expected not to have a constraint: " + table, e);
}
assertSQLState(sqlState, e);
}
s.close();
}
/**
* Check that a given prepared statement can be executed.
*/
private void assertPsWorks(boolean works,
PreparedStatement ps) throws SQLException {
ps.getConnection().setAutoCommit(false);
try {
boolean b = ps.execute();
ResultSet rs = ps.getResultSet();
if (rs != null) {
rs.next();
rs.close();
}
ps.getConnection().rollback();
ps.getConnection().setAutoCommit(true);
if (!works) {
fail("Prepared statement expected to fail.");
}
} catch (SQLException e) {
ps.getConnection().setAutoCommit(true);
if (works) {
fail("Prepared statement expected to work.", e);
}
assertSQLState
(new String[]{NOCOLUMNPERMISSION,
NOEXECUTEPERMISSION,
NOTABLEPERMISSION},
e);
}
}
/**
* Assert that a user has delete privilege on a given table
*
* @param hasPrivilege whether or not the user has the privilege
* @param user the user to check
* @param role to use, or null if we do not want to set the role
* @param schema the schema to check
* @param table the name of the table to check
* @throws SQLException throws all exceptions
*/
private void assertDeletePrivilege(int hasPrivilege,
String user,
String role,
String schema,
String table)
throws SQLException {
Connection c = openUserConnection(user);
if (role != null) {
setRole(c, role);
}
assertDeletePrivilege(hasPrivilege, c, schema, table);
c.close();
}
/**
* Assert that a user has delete privilege on a given table.
*
* @param hasPrivilege whether or not the user has the privilege
* @param c connection to use
* @param schema the schema to check
* @param table the table to check
* @throws SQLException throws all exceptions
*/
private void assertDeletePrivilege(int hasPrivilege,
Connection c,
String schema,
String table) throws SQLException {
Statement s = c.createStatement();
try {
s.execute("delete from " + schema + "." + table);
if (hasPrivilege == NOPRIV) {
fail("expected no DELETE privilege on table " +
formatArgs(c, schema, table));
}
} catch (SQLException e) {
if (hasPrivilege == NOPRIV) {
assertSQLState(NOTABLEPERMISSION, e);
} else {
fail("Unexpected lack of delete privilege. " +
formatArgs(c, schema, table), e);
}
}
s.close();
assertPrivilegeMetadata
(hasPrivilege, c, "DELETE", schema, table, null);
}
/**
* Assert that a specific privilege exists by checking the
* database metadata available to a user.
*
* @param hasPrivilege Is != NOPRIV if we expect the caller to have the
* privilege
* @param c user connection
* @param type type of privilege, e.g. SELECT, INSERT, DELETE, etc.
* @param schema the schema to check
* @param table the table to check
* @param columns the set of columns to check, or all columns if null
* @throws SQLException
*/
private void assertPrivilegeMetadata(int hasPrivilege,
Connection c,
String type,
String schema,
String table,
String[] columns) throws SQLException {
ResultSet rs;
Statement stm = c.createStatement();
rs = stm.executeQuery("values current_user");
rs.next();
String user = rs.getString(1);
rs.close();
stm.close();
if (isOwner(schema, user)) {
// NOTE: Does not work for table owner, who has no manifest entry
// corresponding to the privilege in SYSTABLEPERMS.
return;
}
if (hasPrivilege == VIAROLE) {
// No DatabaseMetaData for roles. We could of course check
// SYS.SYSROLES and the privilege tables but then we would have to
// essentially rebuild the whole role privilege computation
// machinery in this test.. ;)
return;
}
DatabaseMetaData dm = c.getMetaData();
rs = dm.getTablePrivileges
(null, JDBC.identifierToCNF(schema), JDBC.identifierToCNF(table));
boolean found = false;
// check getTablePrivileges
if (columns == null) {
while (rs.next())
{
// Also verify that grantor and is_grantable can be
// obtained Derby doesn't currently support the for grant
// option, the grantor is always the object owner - in this
// test, test_dbo, and is_grantable is always 'NO'.
assertEquals(JDBC.identifierToCNF("test_dbo"),
rs.getString(4));
assertEquals("NO", rs.getString(7));
if (rs.getString(6).equals(type)) {
String privUser = rs.getString(5);
if (privUser.equals(user) ||
privUser.equals(
JDBC.identifierToCNF("public"))) {
found = true;
}
}
}
assertEquals(hasPrivilege == VIAUSER, found);
rs.close();
}
// check getColumnPrivileges()
ResultSet cp = null;
if (columns == null) {
/*
* Derby does not record table level privileges in SYSCOLPERMS, so
* the following does not work. If it is ever changed so that
* getColumnPrivileges returns proper results for table level
* privileges, this(*) can be reenabled.
*
* (*) See GrantRevokeTest.
*/
} else {
// or, check that all given columns have privilege or not as the
// case may be
int noFound = 0;
for (int i = 0; i < columns.length; i++) {
cp = dm.getColumnPrivileges(null,
JDBC.identifierToCNF(schema),
JDBC.identifierToCNF(table),
JDBC.identifierToCNF(columns[i]));
while (cp.next()) {
// also verify that grantor and is_grantable are valid
// Derby doesn't currently support for grant, so
// grantor is always the object owner - in this test,
// test_dbo, and getColumnPrivileges casts 'NO' for
// is_grantable for supported column-related privileges
assertEquals(JDBC.identifierToCNF("test_dbo"),
cp.getString(5));
assertEquals("NO", cp.getString(8));
if (cp.getString(7).equals(type)) {
String privUser = cp.getString(6);
if (privUser.equals(user) ||
privUser.equals(
JDBC.identifierToCNF("public"))) {
noFound++;
}
}
}
}
if (hasPrivilege == VIAUSER) {
assertEquals(columns.length, noFound);
} else {
assertEquals(0, noFound);
}
}
if (cp != null) {
cp.close();
}
}
private boolean isOwner(String schema, String user) throws SQLException {
Connection c = getConnection();
Statement stm = c.createStatement();
ResultSet rs = stm.executeQuery
("select schemaname, authorizationid from sys.sysschemas " +
"where schemaname='" + JDBC.identifierToCNF(schema) + "'");
rs.next();
boolean result = rs.getString(2).equals(JDBC.identifierToCNF(user));
rs.close();
stm.close();
return result;
}
/**
* Get all the columns in a given schema / table
*
* @return an array of Strings with the column names
* @throws SQLException
*/
private String[] getAllColumns(String schema, String table)
throws SQLException
{
Connection c = getConnection();
DatabaseMetaData dbmd = c.getMetaData();
ArrayList<String> columnList = new ArrayList<String>();
ResultSet rs =
dbmd.getColumns( (String) null, schema, table, (String) null);
while(rs.next())
{
columnList.add(rs.getString(4));
}
return columnList.toArray(new String[columnList.size()]);
}
/**
* Return the given String array as a comma separated String
*
* @param columns an array of columns to format
* @return a comma separated String of the column names
*/
private static String columnListAsString(String[] columns) {
if (columns == null) {
return "*";
}
StringBuffer sb = new StringBuffer(columns[0]);
for (int i = 1; i < columns.length; i++ ) {
sb.append("," + columns[i]);
}
return sb.toString();
}
/**
* Format the table arguments used by the various assert* methods for
* printing.
*/
private static String formatArgs(Connection c,
String schema,
String table,
String[] columns) throws SQLException {
return
formatArgs(c, schema, table) +
"(" + columnListAsString(columns) + ")";
}
/**
* Format the dbObject arguments used by the various assert* methods for
* printing.
*/
private static String formatArgs(Connection c,
String schema,
String dbObject) throws SQLException {
ResultSet rs;
Statement stm = c.createStatement();
rs = stm.executeQuery("values current_user");
rs.next();
String user = rs.getString(1);
rs = c.createStatement().executeQuery("values current_role");
rs.next();
String role = rs.getString(1);
rs.close();
stm.close();
return
"User: " + user +
(role == null ? "" : " Role: " + role) +
" Object: " +
schema + "." +
dbObject;
}
/**
* Set the given role for the current session.
*/
private void setRole(Connection c, String role) throws SQLException {
PreparedStatement ps;
if (role.toUpperCase().equals("NONE")) {
ps = c.prepareStatement("set role none");
} else {
ps = c.prepareStatement("set role ?");
ps.setString(1, role);
}
ps.execute();
ps.close();
}
/**
* Perform a bulk grant or revoke action for grantee
*/
private void doGrantRevoke(int action,
String grantor,
String[] actionStrings,
String grantee,
String[] warningExpected)
throws SQLException {
Connection c = openUserConnection(grantor);
Statement s = c.createStatement();
for (int i=0; i < actionStrings.length; i++) {
s.execute(
(action == GRANT ? "grant " : "revoke ") +
actionStrings[i] +
(action == GRANT ? " to " : " from ") +
grantee +
(action == REVOKE && actionStrings[i].startsWith
("execute") ? " restrict" : ""));
if (warningExpected[i] != null) {
assertSQLState(warningExpected[i], s.getWarnings());
}
}
s.close();
c.close();
}
/**
* Perform a bulk grant or revoke action for grantee
*/
private void doGrantRevoke(int action,
String grantor,
String[] actionStrings,
String grantee)
throws SQLException {
String[] warns = new String[actionStrings.length];
doGrantRevoke(action, grantor, actionStrings, grantee, warns);
}
/**
* Perform a bulk grant or revoke action for grantee
*/
private void doGrantRevoke(int action,
String grantor,
String actionString,
String grantee,
String warningExpected) throws SQLException {
doGrantRevoke(action, grantor, new String[] {actionString}, grantee,
new String[]{warningExpected});
}
/**
* Perform a bulk grant or revoke action for grantee
*/
private void doGrantRevoke(int action,
String grantor,
String actionString,
String grantee) throws SQLException {
doGrantRevoke(action, grantor, new String[] {actionString}, grantee);
}
private String CNFUser2user(String CNFUser) {
for (int i = 0; i < users.length; i++) {
if (JDBC.identifierToCNF(users[i]).equals(CNFUser)) {
return users[i];
}
}
fail("test error");
return null;
}
private void assertSQLState(String[] ok_states, SQLException e) {
String state = e.getSQLState();
boolean found = false;
for (int i = 0; i < ok_states.length; i++) {
if (ok_states[i].equals(state)) {
found = true;
}
}
if (!found) {
StringBuffer b = new StringBuffer();
b.append("Exception ");
b.append(state);
b.append(" found, one of ");
for (int i = 0; i < ok_states.length; i++) {
b.append(ok_states[i]);
if (i != ok_states.length - 1) {
b.append('|');
}
}
b.append(" expected");
fail(b.toString());
}
}
public static void calledNested()
throws SQLException
{
Connection c = null;
try {
c = DriverManager.getConnection("jdbc:default:connection");
Statement cStmt = c.createStatement();
// CREATE TABLE
cStmt.executeUpdate
("create table t(role varchar(128) default current_role)");
cStmt.executeUpdate("insert into t values default");
ResultSet rs = cStmt.executeQuery("select * from t");
JDBC.assertSingleValueResultSet(rs, "\"H\"");
rs.close();
cStmt.executeUpdate("drop table t");
// ALTER TABLE
cStmt.executeUpdate("create table t(i int)");
cStmt.executeUpdate("insert into t values 1");
cStmt.executeUpdate
("alter table t " +
"add column role varchar(10) default current_role");
rs = cStmt.executeQuery("select * from t");
JDBC.assertFullResultSet(rs, new String[][]{{"1", "\"H\""}});
rs.close();
cStmt.executeUpdate("drop table t");
cStmt.close();
} finally {
if (c != null) {
try {
c.close();
} catch (Exception e) {
}
}
}
}
public static String getCurrentRole()
throws SQLException
{
Connection c = null;
try {
c = DriverManager.getConnection("jdbc:default:connection");
Statement cStmt = c.createStatement();
ResultSet rs = cStmt.executeQuery("values current_role");
rs.next();
String result = rs.getString(1);
rs.close();
cStmt.close();
return result;
} finally {
if (c != null) {
try {
c.close();
} catch (Exception e) {
}
}
}
}
}
|