11
11
import java .io .FileReader ;
12
12
import java .io .IOException ;
13
13
import java .math .BigDecimal ;
14
+ import java .sql .Connection ;
14
15
import java .sql .Date ;
15
16
import java .sql .JDBCType ;
16
17
import java .sql .SQLException ;
@@ -62,7 +63,6 @@ public class AESetup extends AbstractTest {
62
63
static String cekWin = Constants .CEK_NAME + "_WIN" ;
63
64
static String cekAkv = Constants .CEK_NAME + "_AKV" ;
64
65
static SQLServerStatementColumnEncryptionSetting stmtColEncSetting = null ;
65
-
66
66
static String AETestConnectionString ;
67
67
static String enclaveProperties = "" ;
68
68
@@ -73,6 +73,8 @@ public class AESetup extends AbstractTest {
73
73
.escapeSingleQuotes (AbstractSQLGenerator .escapeIdentifier (RandomUtil .getIdentifier ("AETest_" )));
74
74
public static final String CHAR_TABLE_AE = TestUtils
75
75
.escapeSingleQuotes (AbstractSQLGenerator .escapeIdentifier (RandomUtil .getIdentifier ("JDBCEncryptedChar" )));
76
+ public static final String CHAR_TABLE_AE_NON_UNICODE = TestUtils
77
+ .escapeSingleQuotes (AbstractSQLGenerator .escapeIdentifier (RandomUtil .getIdentifier ("JDBCEncryptedCharNonUnicode" )));
76
78
public static final String BINARY_TABLE_AE = TestUtils
77
79
.escapeSingleQuotes (AbstractSQLGenerator .escapeIdentifier (RandomUtil .getIdentifier ("JDBCEncryptedBinary" )));
78
80
public static final String DATE_TABLE_AE = TestUtils
@@ -107,6 +109,16 @@ enum ColumnType {
107
109
{"Varchar8000" , "varchar(8000) COLLATE Latin1_General_BIN2" , "CHAR" },
108
110
{"Nvarchar4000" , "nvarchar(4000) COLLATE Latin1_General_BIN2" , "NCHAR" },};
109
111
112
+ static String charTableNonUnicode [][] = {{"Char" , "char(20) COLLATE Latin1_General_BIN2" , "CHAR" },
113
+ {"Varchar" , "varchar(50) COLLATE Latin1_General_BIN2" , "CHAR" },
114
+ {"VarcharMax" , "varchar(max) COLLATE Latin1_General_BIN2" , "LONGVARCHAR" },
115
+ {"Varchar8000" , "varchar(8000) COLLATE Latin1_General_BIN2" , "CHAR" },};
116
+
117
+ static String charTableUTF8Collate [][] = {{"Char" , "char(20) COLLATE Latin1_General_100_BIN2_UTF8" , "CHAR" },
118
+ {"Varchar" , "varchar(50) COLLATE Latin1_General_100_BIN2_UTF8" , "CHAR" },
119
+ {"VarcharMax" , "varchar(max) COLLATE Latin1_General_100_BIN2_UTF8" , "LONGVARCHAR" },
120
+ {"Varchar8000" , "varchar(8000) COLLATE Latin1_General_100_BIN2_UTF8" , "CHAR" },};
121
+
110
122
static String dateTable [][] = {{"Date" , "date" , "DATE" }, {"Datetime2Default" , "datetime2" , "TIMESTAMP" },
111
123
{"DatetimeoffsetDefault" , "datetimeoffset" , "DATETIMEOFFSET" }, {"TimeDefault" , "time" , "TIME" },
112
124
{"Datetime" , "datetime" , "DATETIME" }, {"Smalldatetime" , "smalldatetime" , "SMALLDATETIME" }};
@@ -338,6 +350,25 @@ protected static void createTable(String tableName, String cekName, String table
338
350
}
339
351
}
340
352
353
+ protected static void createTable (String tableName , String cekName , String table [][], SQLServerStatement stmt ) {
354
+ try {
355
+ String sql = "" ;
356
+ for (int i = 0 ; i < table .length ; i ++) {
357
+ sql += ColumnType .PLAIN .name () + table [i ][0 ] + " " + table [i ][1 ] + " NULL," ;
358
+ sql += ColumnType .DETERMINISTIC .name () + table [i ][0 ] + " " + table [i ][1 ]
359
+ + String .format (encryptSql , ColumnType .DETERMINISTIC .name (), cekName ) + ") NULL," ;
360
+ sql += ColumnType .RANDOMIZED .name () + table [i ][0 ] + " " + table [i ][1 ]
361
+ + String .format (encryptSql , ColumnType .RANDOMIZED .name (), cekName ) + ") NULL," ;
362
+ }
363
+ TestUtils .dropTableIfExists (tableName , stmt );
364
+ sql = String .format (createSql , tableName , sql );
365
+ stmt .execute (sql );
366
+ stmt .execute ("DBCC FREEPROCCACHE" );
367
+ } catch (SQLException e ) {
368
+ fail (e .getMessage ());
369
+ }
370
+ }
371
+
341
372
protected static void createPrecisionTable (String tableName , String table [][], String cekName , int floatPrecision ,
342
373
int precision , int scale ) throws SQLException {
343
374
try (SQLServerConnection con = (SQLServerConnection ) PrepUtil .getConnection (AETestConnectionString , AEInfo );
@@ -400,6 +431,22 @@ protected static void createScaleTable(String tableName, String table[][], Strin
400
431
}
401
432
}
402
433
434
+ protected static void createDatabaseWithUtf8Collation (Connection conn , String dbName ) throws SQLException {
435
+ try (SQLServerStatement stmt = (SQLServerStatement ) conn .createStatement ()) {
436
+ String dropDB = "IF EXISTS (SELECT name FROM sys.databases WHERE name = N'" + dbName + "') DROP DATABASE " + dbName + ";" ;
437
+ String createDB = "CREATE DATABASE " + dbName + " COLLATE Latin1_General_100_CS_AS_WS_SC_UTF8" ;
438
+ stmt .execute (dropDB );
439
+ stmt .execute (createDB );
440
+ }
441
+ }
442
+
443
+ protected static void dropDatabaseWithUtf8Collation (Connection conn , String dbName ) throws SQLException {
444
+ try (SQLServerStatement stmt = (SQLServerStatement ) conn .createStatement ()) {
445
+ String dropDB = "IF EXISTS (SELECT name FROM sys.databases WHERE name = N'" + dbName + "') DROP DATABASE " + dbName + ";" ;
446
+ stmt .execute (dropDB );
447
+ }
448
+ }
449
+
403
450
/**
404
451
* Create a list of binary values
405
452
*
@@ -449,6 +496,24 @@ protected static String[] createCharValues(boolean nullable) {
449
496
return values ;
450
497
}
451
498
499
+ /**
500
+ * Create a list of char values for non-unicode data types
501
+ *
502
+ * @param nullable
503
+ */
504
+ protected static String [] createCharValuesNonUnicode (boolean nullable ) {
505
+
506
+ boolean encrypted = true ;
507
+ String char20 = RandomData .generateCharTypes ("20" , nullable , encrypted );
508
+ String varchar50 = RandomData .generateCharTypes ("50" , nullable , encrypted );
509
+ String varcharmax = RandomData .generateCharTypes ("max" , nullable , encrypted );
510
+ String varchar8000 = RandomData .generateCharTypes ("8000" , nullable , encrypted );
511
+
512
+ String [] values = {char20 .trim (), varchar50 , varcharmax , varchar8000 };
513
+
514
+ return values ;
515
+ }
516
+
452
517
/**
453
518
* Create a list of numeric values
454
519
*
@@ -805,11 +870,12 @@ protected static void populateBinaryNullCase() throws SQLException {
805
870
* @param charValues
806
871
* @throws SQLException
807
872
*/
808
- protected static void populateCharNormalCase (String [] charValues ) throws SQLException {
873
+ protected static void populateCharNormalCase (String [] charValues , boolean sendStringParametersAsUnicode ) throws SQLException {
809
874
String sql = "insert into " + CHAR_TABLE_AE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
810
875
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")" ;
811
876
812
- try (SQLServerConnection con = (SQLServerConnection ) PrepUtil .getConnection (AETestConnectionString , AEInfo );
877
+ String connectionString = TestUtils .addOrOverrideProperty (AETestConnectionString , "sendStringParametersAsUnicode" , Boolean .toString (sendStringParametersAsUnicode ));
878
+ try (SQLServerConnection con = (SQLServerConnection ) PrepUtil .getConnection (connectionString , AEInfo );
813
879
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement ) TestUtils .getPreparedStmt (con , sql ,
814
880
stmtColEncSetting )) {
815
881
@@ -866,6 +932,82 @@ protected static void populateCharNormalCase(String[] charValues) throws SQLExce
866
932
}
867
933
}
868
934
935
+ /**
936
+ * Populate char data non-unicode.
937
+ *
938
+ * @param charValues
939
+ * @throws SQLException
940
+ */
941
+ protected static void populateCharNormalCaseNonUnicode (String connectionString , String [] charValues , boolean sendStringParametersAsUnicode ) throws SQLException {
942
+ String sql = "insert into " + CHAR_TABLE_AE_NON_UNICODE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?)" ;
943
+
944
+ String cs = TestUtils .addOrOverrideProperty (connectionString , "sendStringParametersAsUnicode" , Boolean .toString (sendStringParametersAsUnicode ));
945
+ try (SQLServerConnection con = (SQLServerConnection ) PrepUtil .getConnection (cs , AEInfo );
946
+ SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement ) TestUtils .getPreparedStmt (con , sql ,
947
+ stmtColEncSetting )) {
948
+
949
+ // char
950
+ for (int i = 1 ; i <= 3 ; i ++) {
951
+ pstmt .setString (i , charValues [0 ]);
952
+ }
953
+
954
+ // varchar
955
+ for (int i = 4 ; i <= 6 ; i ++) {
956
+ pstmt .setString (i , charValues [1 ]);
957
+ }
958
+
959
+ // varchar(max)
960
+ for (int i = 7 ; i <= 9 ; i ++) {
961
+ pstmt .setString (i , charValues [2 ]);
962
+ }
963
+
964
+ // varchar8000
965
+ for (int i = 10 ; i <= 12 ; i ++) {
966
+ pstmt .setString (i , charValues [3 ]);
967
+ }
968
+
969
+ pstmt .execute ();
970
+ }
971
+ }
972
+
973
+ /**
974
+ * Populate char data using set object.
975
+ *
976
+ * @param charValues
977
+ * @throws SQLException
978
+ */
979
+ protected static void populateCharSetObjectNonUnicode (String connectionString , String [] charValues , boolean sendStringParametersAsUnicode ) throws SQLException {
980
+ String sql = "insert into " + CHAR_TABLE_AE_NON_UNICODE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?)" ;
981
+
982
+ String cs = TestUtils .addOrOverrideProperty (connectionString , "sendStringParametersAsUnicode" , Boolean .toString (sendStringParametersAsUnicode ));
983
+ try (SQLServerConnection con = (SQLServerConnection ) PrepUtil .getConnection (cs , AEInfo );
984
+ SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement ) TestUtils .getPreparedStmt (con , sql ,
985
+ stmtColEncSetting )) {
986
+
987
+ // char
988
+ for (int i = 1 ; i <= 3 ; i ++) {
989
+ pstmt .setObject (i , charValues [0 ]);
990
+ }
991
+
992
+ // varchar
993
+ for (int i = 4 ; i <= 6 ; i ++) {
994
+ pstmt .setObject (i , charValues [1 ]);
995
+ }
996
+
997
+ // varchar(max)
998
+ for (int i = 7 ; i <= 9 ; i ++) {
999
+ pstmt .setObject (i , charValues [2 ], java .sql .Types .LONGVARCHAR );
1000
+ }
1001
+
1002
+ // varchar8000
1003
+ for (int i = 10 ; i <= 12 ; i ++) {
1004
+ pstmt .setObject (i , charValues [3 ]);
1005
+ }
1006
+
1007
+ pstmt .execute ();
1008
+ }
1009
+ }
1010
+
869
1011
/**
870
1012
* Populate char data using set object.
871
1013
*
@@ -876,7 +1018,8 @@ protected static void populateCharSetObject(String[] charValues) throws SQLExcep
876
1018
String sql = "insert into " + CHAR_TABLE_AE + " values( " + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?,"
877
1019
+ "?,?,?," + "?,?,?," + "?,?,?," + "?,?,?" + ")" ;
878
1020
879
- try (SQLServerConnection con = (SQLServerConnection ) PrepUtil .getConnection (AETestConnectionString , AEInfo );
1021
+ String connectionString = TestUtils .addOrOverrideProperty (AETestConnectionString , "sendStringParametersAsUnicode" , "false" );
1022
+ try (SQLServerConnection con = (SQLServerConnection ) PrepUtil .getConnection (connectionString , AEInfo );
880
1023
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement ) TestUtils .getPreparedStmt (con , sql ,
881
1024
stmtColEncSetting )) {
882
1025
0 commit comments