Skip to content

Commit a6da1ae

Browse files
rene-yelilgreenbirdpeterbae
authored
Feature | Cleanup Always Encrypted with secure enclaves tests (#1262)
* Fix AEv2 tests exclude for reqExternalSetup and cleanup (#1247) * skip AKV test properly * removed enclave properties string to failed errors as enclave tests could be skipped * Fix | Add null check for getObject() with LocalTime and LocalDate (#1250) * added all AKV tests to use reqExternalSetup tag so they will be skipped by default (#1254) * skip AKV test properly * removed enclave properties string to failed errors as enclave tests could be skipped * Optimize callablestatement test * stop checking AE all the time, also add some tags * some changes * test * Revert "test" This reverts commit e05c67e. * Revert "some changes" This reverts commit 62d2e64. * delete everythign * add aev2 stuff * fix index out of bounds * fix errors * Remove test that doesn't belong * revert surefire plugin * fix exclude * revert table creation logic * fixes * alter * z * z * zz * temp * zzz * zzz * re-add print * address comments * Don't use pstmt * put expected values first Co-authored-by: lilgreenbird <[email protected]> Co-authored-by: Peter Bae <[email protected]> Co-authored-by: rene-ye <[email protected]>
1 parent 947b537 commit a6da1ae

File tree

10 files changed

+261
-844
lines changed

10 files changed

+261
-844
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerEnclaveProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ private byte[] adjustBigInt(byte[] b) throws IOException {
317317
if (b.length < BIG_INTEGER_SIZE) {
318318
ByteArrayOutputStream output = new ByteArrayOutputStream();
319319
for (int i = 0; i < BIG_INTEGER_SIZE - b.length; i++) {
320-
output.write(new byte[] {0});
320+
output.write(0);
321321
}
322322
output.write(b);
323323
b = output.toByteArray();

src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ public class AESetup extends AbstractTest {
7373
// test that only run on Windows will be skipped
7474
static boolean isWindows = System.getProperty("os.name").startsWith("Windows");
7575

76-
protected static boolean isAEv2 = false;
77-
7876
public static final String tableName = TestUtils
7977
.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("AETest_")));
8078
public static final String CHAR_TABLE_AE = TestUtils
@@ -126,6 +124,10 @@ enum ColumnType {
126124
{"SmallMoney", "smallmoney", "SMALLMONEY"}, {"Money", "money", "MONEY"},
127125
{"Decimal2", "decimal(28,4)", "DECIMAL"}, {"Numeric2", "numeric(28,4)", "DECIMAL"},};
128126

127+
static String numericTableSimple[][] = {{"Int", "int", "INT"}};
128+
129+
static String varcharTableSimple[][] = {{"Varchar", "varchar(20) COLLATE LATIN1_GENERAL_BIN2", "VARCHAR"}};
130+
129131
// CREATE TABLE tableName (columns) NULL"
130132
static String createSql = "CREATE TABLE %s (%s)";
131133

@@ -191,26 +193,6 @@ static void setAEConnectionString(String serverName, String url, String protocol
191193
}
192194
}
193195

194-
/**
195-
* Setup AE connection string and check setup
196-
*
197-
* @param serverName
198-
* @param url
199-
* @param protocol
200-
* @throws SQLException
201-
*/
202-
void checkAESetup(String serverName, String url, String protocol) throws Exception {
203-
setAEConnectionString(serverName, url, protocol);
204-
205-
try (SQLServerConnection con = PrepUtil.getConnection(AETestConnectionString, AEInfo)) {
206-
isAEv2 = TestUtils.isAEv2(con);
207-
} catch (SQLException e) {
208-
isAEv2 = false;
209-
} catch (Exception e) {
210-
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
211-
}
212-
}
213-
214196
@BeforeAll
215197
public static void setupAETest() throws Exception {
216198
readFromFile(Constants.JAVA_KEY_STORE_FILENAME, "Alias name");
@@ -1959,4 +1941,37 @@ private static void dropCMK(String cmkName, Statement stmt) throws SQLException
19591941
+ " drop column master key " + cmkName + " end";
19601942
stmt.execute(cekSql);
19611943
}
1944+
1945+
/**
1946+
* Alter Column encryption on deterministic columns to randomized - this will trigger enclave to re-encrypt
1947+
*
1948+
* @param stmt
1949+
* @param tableName
1950+
* @param table
1951+
* @param cekName
1952+
* @throws SQLException
1953+
*/
1954+
protected void testAlterColumnEncryption(SQLServerStatement stmt, String tableName, String table[][],
1955+
String cekName) throws SQLException {
1956+
try (SQLServerConnection con = PrepUtil.getConnection(AETestConnectionString, AEInfo)) {
1957+
for (int i = 0; i < table.length; i++) {
1958+
// alter deterministic to randomized
1959+
String sql = "ALTER TABLE " + tableName + " ALTER COLUMN " + ColumnType.DETERMINISTIC.name()
1960+
+ table[i][0] + " " + table[i][1]
1961+
+ String.format(encryptSql, ColumnType.RANDOMIZED.name(), cekName) + ")";
1962+
try {
1963+
stmt.execute(sql);
1964+
if (!TestUtils.isAEv2(con)) {
1965+
fail(TestResource.getResource("R_expectedExceptionNotThrown"));
1966+
}
1967+
} catch (SQLException e) {
1968+
if (!TestUtils.isAEv2(con)) {
1969+
fail(e.getMessage());
1970+
} else {
1971+
fail(TestResource.getResource("R_AlterAEv2Error") + e.getMessage() + "Query: " + sql);
1972+
}
1973+
}
1974+
}
1975+
}
1976+
}
19621977
}

src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/CallableStatementTest.java

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,11 @@ public class CallableStatementTest extends AESetup {
127127
*/
128128
@BeforeAll
129129
public static void initValues() throws Exception {
130-
dropAll();
131-
132130
numericValues = createNumericValues(nullable);
133131
byteValues = createBinaryValues(nullable);
134132
dateValues = createTemporalTypesCallableStatement(nullable);
135133
charValues = createCharValues(nullable);
136-
}
137134

138-
void initCallableStatementTest() throws Exception {
139135
dropAll();
140136

141137
createSPTables(cekJks);
@@ -165,39 +161,27 @@ public static void dropAll() throws Exception {
165161
@ParameterizedTest
166162
@MethodSource("enclaveParams")
167163
public void testMultiInsertionSelection(String serverName, String url, String protocol) throws Exception {
168-
checkAESetup(serverName, url, protocol);
169-
initCallableStatementTest();
170-
171164
createMultiInsertionSelection();
172165
MultiInsertionSelection();
173166
}
174167

175168
@ParameterizedTest
176169
@MethodSource("enclaveParams")
177170
public void testInputProcedureNumeric(String serverName, String url, String protocol) throws Exception {
178-
checkAESetup(serverName, url, protocol);
179-
initCallableStatementTest();
180-
181171
createInputProcedure();
182172
testInputProcedure("{call " + inputProcedure + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}", numericValues);
183173
}
184174

185175
@ParameterizedTest
186176
@MethodSource("enclaveParams")
187177
public void testInputProcedureChar(String serverName, String url, String protocol) throws Exception {
188-
checkAESetup(serverName, url, protocol);
189-
initCallableStatementTest();
190-
191178
createInputProcedure2();
192179
testInputProcedure2("{call " + inputProcedure2 + "(?,?,?,?,?,?,?,?)}");
193180
}
194181

195182
@ParameterizedTest
196183
@MethodSource("enclaveParams")
197184
public void testEncryptedOutputNumericParams(String serverName, String url, String protocol) throws Exception {
198-
checkAESetup(serverName, url, protocol);
199-
initCallableStatementTest();
200-
201185
createOutputProcedure();
202186
testOutputProcedureRandomOrder("{call " + outputProcedure + "(?,?,?,?,?,?,?)}", numericValues);
203187
testOutputProcedureInorder("{call " + outputProcedure + "(?,?,?,?,?,?,?)}", numericValues);
@@ -209,9 +193,6 @@ public void testEncryptedOutputNumericParams(String serverName, String url, Stri
209193
@MethodSource("enclaveParams")
210194
public void testUnencryptedAndEncryptedNumericOutputParams(String serverName, String url,
211195
String protocol) throws Exception {
212-
checkAESetup(serverName, url, protocol);
213-
initCallableStatementTest();
214-
215196
createOutputProcedure2();
216197
testOutputProcedure2RandomOrder("{call " + outputProcedure2 + "(?,?,?,?,?,?,?,?,?,?)}", numericValues);
217198
testOutputProcedure2Inorder("{call " + outputProcedure2 + "(?,?,?,?,?,?,?,?,?,?)}", numericValues);
@@ -222,9 +203,6 @@ public void testUnencryptedAndEncryptedNumericOutputParams(String serverName, St
222203
@MethodSource("enclaveParams")
223204
public void testEncryptedOutputParamsFromDifferentTables(String serverName, String url,
224205
String protocol) throws Exception {
225-
checkAESetup(serverName, url, protocol);
226-
initCallableStatementTest();
227-
228206
createOutputProcedure3();
229207
testOutputProcedure3RandomOrder("{call " + outputProcedure3 + "(?,?)}");
230208
testOutputProcedure3Inorder("{call " + outputProcedure3 + "(?,?)}");
@@ -234,9 +212,6 @@ public void testEncryptedOutputParamsFromDifferentTables(String serverName, Stri
234212
@ParameterizedTest
235213
@MethodSource("enclaveParams")
236214
public void testInOutProcedure(String serverName, String url, String protocol) throws Exception {
237-
checkAESetup(serverName, url, protocol);
238-
initCallableStatementTest();
239-
240215
createInOutProcedure();
241216
testInOutProcedure("{call " + inoutProcedure + "(?)}");
242217
testInOutProcedure("exec " + inoutProcedure + " ?");
@@ -245,19 +220,13 @@ public void testInOutProcedure(String serverName, String url, String protocol) t
245220
@ParameterizedTest
246221
@MethodSource("enclaveParams")
247222
public void testMixedProcedure(String serverName, String url, String protocol) throws Exception {
248-
checkAESetup(serverName, url, protocol);
249-
initCallableStatementTest();
250-
251223
createMixedProcedure();
252224
testMixedProcedure("{ ? = call " + mixedProcedure + "(?,?,?)}");
253225
}
254226

255227
@ParameterizedTest
256228
@MethodSource("enclaveParams")
257229
public void testUnencryptedAndEncryptedIOParams(String serverName, String url, String protocol) throws Exception {
258-
checkAESetup(serverName, url, protocol);
259-
initCallableStatementTest();
260-
261230
// unencrypted input and output parameter
262231
// encrypted input and output parameter
263232
createMixedProcedure2();
@@ -268,9 +237,6 @@ public void testUnencryptedAndEncryptedIOParams(String serverName, String url, S
268237
@ParameterizedTest
269238
@MethodSource("enclaveParams")
270239
public void testUnencryptedIOParams(String serverName, String url, String protocol) throws Exception {
271-
checkAESetup(serverName, url, protocol);
272-
initCallableStatementTest();
273-
274240
createMixedProcedure3();
275241
testMixedProcedure3RandomOrder("{call " + mixedProcedure3 + "(?,?,?,?)}");
276242
testMixedProcedure3Inorder("{call " + mixedProcedure3 + "(?,?,?,?)}");
@@ -280,9 +246,6 @@ public void testUnencryptedIOParams(String serverName, String url, String protoc
280246
@ParameterizedTest
281247
@MethodSource("enclaveParams")
282248
public void testVariousIOParams(String serverName, String url, String protocol) throws Exception {
283-
checkAESetup(serverName, url, protocol);
284-
initCallableStatementTest();
285-
286249
createmixedProcedureNumericPrecisionScale();
287250
testmixedProcedureNumericPrecisionScaleInorder("{call " + mixedProcedureNumericPrecisionScale + "(?,?,?,?)}");
288251
testmixedProcedureNumericPrecisionScaleParameterName(
@@ -292,9 +255,6 @@ public void testVariousIOParams(String serverName, String url, String protocol)
292255
@ParameterizedTest
293256
@MethodSource("enclaveParams")
294257
public void testOutputProcedureChar(String serverName, String url, String protocol) throws Exception {
295-
checkAESetup(serverName, url, protocol);
296-
initCallableStatementTest();
297-
298258
createOutputProcedureChar();
299259
testOutputProcedureCharInorder("{call " + outputProcedureChar + "(?,?,?,?,?,?,?,?,?)}");
300260
testOutputProcedureCharInorderObject("{call " + outputProcedureChar + "(?,?,?,?,?,?,?,?,?)}");
@@ -303,9 +263,6 @@ public void testOutputProcedureChar(String serverName, String url, String protoc
303263
@ParameterizedTest
304264
@MethodSource("enclaveParams")
305265
public void testOutputProcedureNumeric(String serverName, String url, String protocol) throws Exception {
306-
checkAESetup(serverName, url, protocol);
307-
initCallableStatementTest();
308-
309266
createOutputProcedureNumeric();
310267
testOutputProcedureNumericInorder("{call " + outputProcedureNumeric + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
311268
testcoerctionsOutputProcedureNumericInorder(
@@ -315,9 +272,6 @@ public void testOutputProcedureNumeric(String serverName, String url, String pro
315272
@ParameterizedTest
316273
@MethodSource("enclaveParams")
317274
public void testOutputProcedureBinary(String serverName, String url, String protocol) throws Exception {
318-
checkAESetup(serverName, url, protocol);
319-
initCallableStatementTest();
320-
321275
createOutputProcedureBinary();
322276
testOutputProcedureBinaryInorder("{call " + outputProcedureBinary + "(?,?,?,?,?)}");
323277
testOutputProcedureBinaryInorderObject("{call " + outputProcedureBinary + "(?,?,?,?,?)}");
@@ -327,9 +281,6 @@ public void testOutputProcedureBinary(String serverName, String url, String prot
327281
@ParameterizedTest
328282
@MethodSource("enclaveParams")
329283
public void testOutputProcedureDate(String serverName, String url, String protocol) throws Exception {
330-
checkAESetup(serverName, url, protocol);
331-
initCallableStatementTest();
332-
333284
createOutputProcedureDate();
334285
testOutputProcedureDateInorder("{call " + outputProcedureDate + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
335286
testOutputProcedureDateInorderObject("{call " + outputProcedureDate + "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
@@ -338,9 +289,6 @@ public void testOutputProcedureDate(String serverName, String url, String protoc
338289
@ParameterizedTest
339290
@MethodSource("enclaveParams")
340291
public void testMixedProcedureDateScale(String serverName, String url, String protocol) throws Exception {
341-
checkAESetup(serverName, url, protocol);
342-
initCallableStatementTest();
343-
344292
createMixedProcedureDateScale();
345293
testMixedProcedureDateScaleInorder("{call " + outputProcedureDateScale + "(?,?,?,?,?,?)}");
346294
testMixedProcedureDateScaleWithParameterName("{call " + outputProcedureDateScale + "(?,?,?,?,?,?)}");
@@ -349,19 +297,13 @@ public void testMixedProcedureDateScale(String serverName, String url, String pr
349297
@ParameterizedTest
350298
@MethodSource("enclaveParams")
351299
public void testOutputProcedureBatch(String serverName, String url, String protocol) throws Exception {
352-
checkAESetup(serverName, url, protocol);
353-
initCallableStatementTest();
354-
355300
createOutputProcedureBatch();
356301
testOutputProcedureBatchInorder("{call " + outputProcedureBatch + "(?,?,?,?)}");
357302
}
358303

359304
@ParameterizedTest
360305
@MethodSource("enclaveParams")
361306
public void testOutputProcedure4(String serverName, String url, String protocol) throws Exception {
362-
checkAESetup(serverName, url, protocol);
363-
initCallableStatementTest();
364-
365307
createOutputProcedure4();
366308
}
367309

0 commit comments

Comments
 (0)