|
1 | 1 | package com.microsoft.sqlserver.jdbc.callablestatement;
|
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | +import static org.junit.Assert.fail; |
3 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 6 |
|
5 | 7 | import java.sql.CallableStatement;
|
6 | 8 | import java.sql.Connection;
|
7 | 9 | import java.sql.ResultSet;
|
| 10 | +import java.sql.SQLException; |
8 | 11 | import java.sql.Statement;
|
| 12 | +import java.util.UUID; |
9 | 13 |
|
10 | 14 | import org.junit.jupiter.api.Tag;
|
11 | 15 | import org.junit.jupiter.api.Test;
|
12 | 16 | import org.junit.platform.runner.JUnitPlatform;
|
13 | 17 | import org.junit.runner.RunWith;
|
14 | 18 |
|
15 | 19 | import com.microsoft.sqlserver.jdbc.RandomUtil;
|
| 20 | +import com.microsoft.sqlserver.jdbc.TestResource; |
16 | 21 | import com.microsoft.sqlserver.jdbc.TestUtils;
|
17 | 22 | import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
|
18 | 23 | import com.microsoft.sqlserver.testframework.AbstractTest;
|
@@ -79,4 +84,42 @@ public void datatypestest() throws Exception {
|
79 | 84 | }
|
80 | 85 | }
|
81 | 86 | }
|
| 87 | + |
| 88 | + @Test |
| 89 | + @Tag("xAzureSQLDB") |
| 90 | + @Tag("xAzureSQLDW") |
| 91 | + @Tag("xAzureSQLMI") |
| 92 | + public void noPrivilegeTest() throws SQLException { |
| 93 | + try (Connection c = getConnection(); Statement stmt = c.createStatement()) { |
| 94 | + String tableName = RandomUtil.getIdentifier("jdbc_priv"); |
| 95 | + String procName = RandomUtil.getIdentifier("priv_proc"); |
| 96 | + String user = "priv_user" + UUID.randomUUID(); |
| 97 | + String pass = "priv_pass" + UUID.randomUUID(); |
| 98 | + |
| 99 | + stmt.execute( |
| 100 | + "CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableName) + " (id int, name varchar(50))"); |
| 101 | + stmt.execute("CREATE PROC " + AbstractSQLGenerator.escapeIdentifier(procName) |
| 102 | + + " @id int, @str varchar(50) as INSERT INTO " + AbstractSQLGenerator.escapeIdentifier(tableName) |
| 103 | + + " values(@id,@str)"); |
| 104 | + stmt.execute( |
| 105 | + "CREATE LOGIN " + AbstractSQLGenerator.escapeIdentifier(user) + " WITH password='" + pass + "'"); |
| 106 | + stmt.execute("CREATE USER " + AbstractSQLGenerator.escapeIdentifier(user) + ""); |
| 107 | + try { |
| 108 | + stmt.execute("EXECUTE AS USER='" + user + "';EXECUTE " + AbstractSQLGenerator.escapeIdentifier(procName) |
| 109 | + + " 1,'hi';"); |
| 110 | + fail(TestResource.getResource("R_shouldThrowException")); |
| 111 | + } catch (SQLException e) { |
| 112 | + assertTrue(e.getMessage().matches(TestResource.formatErrorMsg("R_NoPrivilege"))); |
| 113 | + } finally { |
| 114 | + TestUtils.dropProcedureIfExists(procName, stmt); |
| 115 | + TestUtils.dropTableIfExists(tableName, stmt); |
| 116 | + stmt.close(); |
| 117 | + c.close(); |
| 118 | + try (Connection c2 = getConnection(); Statement stmt2 = c2.createStatement()) { |
| 119 | + stmt2.execute("DROP USER " + AbstractSQLGenerator.escapeIdentifier(user)); |
| 120 | + stmt2.execute("DROP LOGIN " + AbstractSQLGenerator.escapeIdentifier(user)); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
82 | 125 | }
|
0 commit comments