From 1daa4f0623c3268ea6708604f7ec4f0131ec9959 Mon Sep 17 00:00:00 2001 From: Jenny Tam Date: Thu, 9 Nov 2017 09:29:13 -0800 Subject: [PATCH] Modified files as per review comments --- test/functional/sqlsrv/0052.phpt | 4 +- test/functional/sqlsrv/MsHelper.inc | 41 +++-- test/functional/sqlsrv/skipif_not_ksp.inc | 4 +- .../sqlsrv/sqlsrv_connect_encrypted_ksp.phpt | 4 +- ...qlsrv_connect_encrypted_ksp_encrypted.phpt | 2 +- .../sqlsrv_connect_encrypted_ksp_errors.phpt | 16 +- .../sqlsrv/sqlsrv_encrypted_patients_ksp.phpt | 14 +- .../sqlsrv_encrypted_query_nosqltype.phpt | 164 ++++++++---------- 8 files changed, 115 insertions(+), 134 deletions(-) diff --git a/test/functional/sqlsrv/0052.phpt b/test/functional/sqlsrv/0052.phpt index 11ced488..ad412d12 100644 --- a/test/functional/sqlsrv/0052.phpt +++ b/test/functional/sqlsrv/0052.phpt @@ -31,7 +31,7 @@ this test is very similar to test_scrollable.phpt... might consider removing thi $options = array('Scrollable' => 'static'); } - $stmt = AE\executeQueryEx($conn, $query, $options); + $stmt = sqlsrv_query($conn, $query, array(), $options); $rows = sqlsrv_has_rows($stmt); if ($rows != false) { fatalError("Should be no rows present"); @@ -46,7 +46,7 @@ this test is very similar to test_scrollable.phpt... might consider removing thi print_r(sqlsrv_errors(), true); } - $stmt = AE\selectFromTable($conn, $tableName); + $stmt = sqlsrv_query($conn, $query); $rows = sqlsrv_has_rows($stmt); if ($rows != false) { fatalError("Should be no rows present"); diff --git a/test/functional/sqlsrv/MsHelper.inc b/test/functional/sqlsrv/MsHelper.inc index 617a6906..a96a3155 100644 --- a/test/functional/sqlsrv/MsHelper.inc +++ b/test/functional/sqlsrv/MsHelper.inc @@ -74,10 +74,10 @@ class ColumnMeta // https://docs.microsoft.com/en-us/sql/relational-databases/security/encryption/always-encrypted-database-engine $unsupported = array("xml", "timestamp", "image", "ntext", "text", "sql_variant", "hierarchyid", "geography", "geometry", "alias"); - + if (stripos($this->options, "identity") !== false) { $this->encryptable = false; - } elseif (in_array(strtolower($this->dataType), $unsupported)) { + } elseif (in_array(strtolower($this->dataType), $unsupported)) { $this->encryptable = false; } else { $this->encryptable = true; @@ -222,17 +222,19 @@ function getKSPpath() $dir_name = realpath(dirname(__FILE__)); $ksp = $dir_name . DIRECTORY_SEPARATOR . $name; - if ( strtoupper( substr( php_uname( 's' ), 0, 3 ) ) == 'WIN' ) { + if (strtoupper(substr(php_uname('s'), 0, 3)) == 'WIN') { $arch = 'x64'; - if ( PHP_INT_SIZE == 4 ) // running 32 bit + if (PHP_INT_SIZE == 4) { + // running 32 bit $arch = ''; + } $ksp .= $arch . '.dll'; - } - else + } else { $ksp .= '.so'; - + } + return $ksp; -} +} /** * @return string default column name when a name is not provided in the ColumnMeta class @@ -429,7 +431,7 @@ function insertRow($conn, $tbname, $inputs, &$r = null, $api = INSERT_QUERY) $stmt = sqlsrv_prepare($conn, $insertSql); if ($stmt) { $r = sqlsrv_execute($stmt); - } + } break; } } else { @@ -443,20 +445,20 @@ function insertRow($conn, $tbname, $inputs, &$r = null, $api = INSERT_QUERY) array_push($params, $inputs[$key]); } } - + // use prepare for inserts when AE is enabled if (isColEncrypted() || $api == INSERT_PREPARE_PARAMS) { $stmt = sqlsrv_prepare($conn, $insertSql, $params); if ($stmt) { $r = sqlsrv_execute($stmt); } else { - fatalError("insertRow: failed to prepare insert query!"); + fatalError("insertRow: failed to prepare insert query!"); } } else { $stmt = sqlsrv_query($conn, $insertSql, $params); } } - + return $stmt; } @@ -501,18 +503,19 @@ function executeQuery($conn, $sql, $conds = null, $values = null, $options = nul $sql = $sql . " WHERE $clause "; } elseif (!empty($conds)) { $sql = $sql . " WHERE $conds "; - } + } $stmt = sqlsrv_query($conn, $sql, null, $options); } else { - // with AE enabled, use sqlsrv_prepare() in case there are + // with AE enabled, use sqlsrv_prepare() in case there are // fields with unlimited size - if (empty($conds) || empty($values)) { + if (empty($conds)) { $stmt = sqlsrv_prepare($conn, $sql, null, $options); } else { $sql = $sql . " WHERE $conds "; + // pass $values to sqlsrv_prepare whether the array is null, empty or filled $stmt = sqlsrv_prepare($conn, $sql, $values, $options); - } + } if ($stmt) { $r = sqlsrv_execute($stmt); if (!$r) { @@ -681,7 +684,7 @@ function insertTestRow($conn, $tbname, $index) if (empty($inputArray)) { fatalError("getInsertSqlComplete: inputs for inserting a row cannot be empty"); } - + $result = null; if (isColEncrypted()) { $stmt = insertRow($conn, $tbname, $inputArray, $result); @@ -693,14 +696,14 @@ function insertTestRow($conn, $tbname, $index) $col = 1; foreach ($inputArray as $key => $value) { $colStr .= $key . ", "; - + if (is_array($value)) { $value = $value[0]; // this might be an input to a decimal, a numeric or a binary field if (isBinary($col)) { $value = "0x" . $value; // annotate the input string as a hex string } - } + } if (is_null($value)) { $valStr .= "null, "; } elseif (is_string($value) && !isBinary($col)) { diff --git a/test/functional/sqlsrv/skipif_not_ksp.inc b/test/functional/sqlsrv/skipif_not_ksp.inc index 38cd153f..08e25179 100644 --- a/test/functional/sqlsrv/skipif_not_ksp.inc +++ b/test/functional/sqlsrv/skipif_not_ksp.inc @@ -1,7 +1,7 @@ true)); - if($conn === false) { + if ($conn === false) { echo "Failed to connect.\n"; print_r(sqlsrv_errors()); } else { @@ -19,7 +19,7 @@ Fetch data from a prepopulated test table given a custom keystore provider $ksp_test_table = AE\KSP_TEST_TABLE; $tsql = "SELECT * FROM $ksp_test_table"; $stmt = sqlsrv_prepare($conn, $tsql); - if (! sqlsrv_execute($stmt)) { + if (!sqlsrv_execute($stmt)) { echo "Failed to fetch data.\n"; print_r(sqlsrv_errors()); } diff --git a/test/functional/sqlsrv/sqlsrv_connect_encrypted_ksp_encrypted.phpt b/test/functional/sqlsrv/sqlsrv_connect_encrypted_ksp_encrypted.phpt index bf6463c7..b2ea6c77 100644 --- a/test/functional/sqlsrv/sqlsrv_connect_encrypted_ksp_encrypted.phpt +++ b/test/functional/sqlsrv/sqlsrv_connect_encrypted_ksp_encrypted.phpt @@ -9,7 +9,7 @@ Fetch encrypted data from a prepopulated test table given a custom keystore prov require_once('MsHelper.inc'); $conn = AE\connect(array('ReturnDatesAsStrings'=>true)); - if($conn === false) { + if ($conn === false) { fatalError("Failed to connect.\n"); } else { echo "Connected successfully with ColumnEncryption disabled.\n"; diff --git a/test/functional/sqlsrv/sqlsrv_connect_encrypted_ksp_errors.phpt b/test/functional/sqlsrv/sqlsrv_connect_encrypted_ksp_errors.phpt index 1f799a3b..7083d4a3 100644 --- a/test/functional/sqlsrv/sqlsrv_connect_encrypted_ksp_errors.phpt +++ b/test/functional/sqlsrv/sqlsrv_connect_encrypted_ksp_errors.phpt @@ -8,7 +8,7 @@ Connect using a custom keystore provider with some required inputs missing function connect($server, $connectionInfo) { $conn = sqlsrv_connect($server, $connectionInfo); - if($conn === false) { + if ($conn === false) { echo "Failed to connect.\n"; $errors = sqlsrv_errors(); foreach ($errors[0] as $key => $error) { @@ -31,20 +31,20 @@ Connect using a custom keystore provider with some required inputs missing $ksp_name = AE\KSP_NAME; $encrypt_key = AE\ENCRYPT_KEY; - echo("Connecting... with column encryption\n"); + echo "Connecting... with column encryption\n"; $connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd, "ColumnEncryption"=>"enabled"); connect($server, $connectionInfo); - echo("Connecting... with an invalid input to CEKeystoreProvider\n"); + echo "Connecting... with an invalid input to CEKeystoreProvider\n"; $connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd, "ColumnEncryption"=>"enabled", "CEKeystoreProvider"=>1); connect($server, $connectionInfo); - echo("Connecting... with an empty path\n"); + echo "Connecting... with an empty path\n"; $connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd, "ColumnEncryption"=>"enabled", "CEKeystoreProvider"=>"", @@ -53,7 +53,7 @@ Connect using a custom keystore provider with some required inputs missing connect($server, $connectionInfo); - echo("Connecting... without a name\n"); + echo "Connecting... without a name\n"; $connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd, "ColumnEncryption"=>"enabled", "CEKeystoreProvider"=>$ksp_path, @@ -61,7 +61,7 @@ Connect using a custom keystore provider with some required inputs missing connect($server, $connectionInfo); - echo("Connecting... with an empty name\n"); + echo "Connecting... with an empty name\n"; $connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd, "ColumnEncryption"=>"enabled", "CEKeystoreProvider"=>$ksp_path, @@ -70,7 +70,7 @@ Connect using a custom keystore provider with some required inputs missing connect($server, $connectionInfo); - echo("Connecting... without a key\n"); + echo "Connecting... without a key\n"; $connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd, "ColumnEncryption"=>"enabled", "CEKeystoreProvider"=>$ksp_path, @@ -78,7 +78,7 @@ Connect using a custom keystore provider with some required inputs missing connect($server, $connectionInfo); - echo("Connecting... with all required inputs\n"); + echo "Connecting... with all required inputs\n"; $connectionInfo = array("Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd, "ColumnEncryption"=>"enabled", "CEKeystoreProvider"=>$ksp_path, diff --git a/test/functional/sqlsrv/sqlsrv_encrypted_patients_ksp.phpt b/test/functional/sqlsrv/sqlsrv_encrypted_patients_ksp.phpt index 3926d0ea..cdc8e62a 100644 --- a/test/functional/sqlsrv/sqlsrv_encrypted_patients_ksp.phpt +++ b/test/functional/sqlsrv/sqlsrv_encrypted_patients_ksp.phpt @@ -4,7 +4,7 @@ Test simple insert, fetch and update with ColumnEncryption enabled and a custome --FILE-- true)); - if($conn === false) { + if ($conn === false) { fatalError( "Failed to connect.\n"); } else { echo "Connected successfully with ColumnEncryption enabled.\n"; } - $tableName = CreatePatientsTable(); + $tableName = createPatientsTable(); insertData('748-68-0245', 'Jeannette', 'McDonald', '2002-11-28'); insertData('795-73-9838', 'John', 'Doe', '2001-05-29'); @@ -103,7 +103,7 @@ Test simple insert, fetch and update with ColumnEncryption enabled and a custome array('Chang', null, null, SQLSRV_SQLTYPE_NVARCHAR(50)), array('456-12-5486', null, null, SQLSRV_SQLTYPE_CHAR(11))); - $tsql = "UPDATE Patients SET BirthDate = ?, LastName = ? WHERE SSN = ?"; + $tsql = "UPDATE $tableName SET BirthDate = ?, LastName = ? WHERE SSN = ?"; $stmt = sqlsrv_query($conn, $tsql, $params); if (!$stmt) { @@ -112,7 +112,7 @@ Test simple insert, fetch and update with ColumnEncryption enabled and a custome echo "Update his birthdate too...\n"; $params = array(array('456-12-5486', null, null, SQLSRV_SQLTYPE_CHAR(11))); - $tsql = "SELECT SSN, FirstName, LastName, BirthDate FROM Patients WHERE SSN = ?"; + $tsql = "SELECT SSN, FirstName, LastName, BirthDate FROM $tableName WHERE SSN = ?"; $stmt = sqlsrv_query($conn, $tsql, $params); if (!$stmt) { fatalError("Failed to select with a WHERE clause\n"); @@ -128,7 +128,7 @@ Test simple insert, fetch and update with ColumnEncryption enabled and a custome /////////////////////////////////////////// $procName = '#phpAEProc1'; $spArgs = "@p1 INT, @p2 DATE OUTPUT"; - $spCode = "SET @p2 = (SELECT [BirthDate] FROM Patients WHERE [PatientId] = @p1)"; + $spCode = "SET @p2 = (SELECT [BirthDate] FROM $tableName WHERE [PatientId] = @p1)"; $stmt = sqlsrv_query($conn, "CREATE PROC [$procName] ($spArgs) AS BEGIN $spCode END"); sqlsrv_free_stmt($stmt); @@ -147,7 +147,7 @@ Test simple insert, fetch and update with ColumnEncryption enabled and a custome /////////////////////////////////////////// $procName = '#phpAEProc2'; $spArgs = "@p1 INT, @p2 CHAR(11) OUTPUT"; - $spCode = "SET @p2 = (SELECT [SSN] FROM Patients WHERE [PatientId] = @p1)"; + $spCode = "SET @p2 = (SELECT [SSN] FROM $tableName WHERE [PatientId] = @p1)"; $stmt = sqlsrv_query($conn, "CREATE PROC [$procName] ($spArgs) AS BEGIN $spCode END"); sqlsrv_free_stmt($stmt); diff --git a/test/functional/sqlsrv/sqlsrv_encrypted_query_nosqltype.phpt b/test/functional/sqlsrv/sqlsrv_encrypted_query_nosqltype.phpt index d68c3731..5df1ad65 100644 --- a/test/functional/sqlsrv/sqlsrv_encrypted_query_nosqltype.phpt +++ b/test/functional/sqlsrv/sqlsrv_encrypted_query_nosqltype.phpt @@ -1,118 +1,96 @@ --TEST-- -Test using sqlserv_query for binding parameters with ColumnEncryption enabled and a custome keystore provider +Test using sqlsrv_query for binding parameters with column encryption and a custom keystore provider --SKIPIF-- --FILE-- PatientId . "\n"; echo $obj->SSN . "\n"; echo $obj->FirstName . "\n"; echo $obj->LastName . "\n"; - echo $obj->BirthDate . "\n\n"; - } - } - - function PrintError() - { - $errors = sqlsrv_errors(); - foreach ( $errors as $error ) - { - echo " SQLSTATE: " . $error['SQLSTATE'] . "\n"; - echo " code: " . $error['code'] . "\n"; - echo " message: " . $error['message'] . "\n\n"; + echo $obj->BirthDate . "\n\n"; } } - - sqlsrv_configure( 'WarningsReturnAsErrors', 1 ); - sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL ); - require_once( 'MsSetup.inc' ); - require_once( 'AE_Ksp.inc' ); - - $ksp_path = getKSPpath(); - - $connectionInfo = array( "Database"=>$databaseName, "UID"=>$uid, "PWD"=>$pwd, - "ReturnDatesAsStrings"=>true, "ColumnEncryption"=>'Enabled', - "CEKeystoreProvider"=>$ksp_path, - "CEKeystoreName"=>$ksp_name, - "CEKeystoreEncryptKey"=>$encrypt_key); - - $conn = sqlsrv_connect( $server, $connectionInfo ); - if( $conn === false ) + function printError() { - echo "Failed to connect.\n"; - PrintError(); + $errors = sqlsrv_errors(); + foreach ($errors as $error) { + echo " SQLSTATE: " . $error['SQLSTATE'] . "\n"; + echo " code: " . $error['code'] . "\n"; + echo " message: " . $error['message'] . "\n\n"; + } } - else - { + + sqlsrv_configure('WarningsReturnAsErrors', 1); + sqlsrv_configure('LogSeverity', SQLSRV_LOG_SEVERITY_ALL); + + require_once('MsHelper.inc'); + $conn = AE\connect(array('ReturnDatesAsStrings'=>true)); + + if ($conn === false) { + echo "Failed to connect.\n"; + printError(); + } else { echo "Connected successfully with ColumnEncryption enabled.\n\n"; } - - $tablename = CreatePatientsTable(); - - $tsql = "INSERT INTO $tablename (SSN, FirstName, LastName, BirthDate) VALUES (?, ?, ?, ?)"; - $inputs = array( '748-68-0245', 'Jeannette', 'McDonald', '2002-11-28' ); - - //expects an error in Column Encryption enabled connection - print_r( "Using sqlsrv_query and binding parameters with literal values:\n" ); - $stmt = sqlsrv_query( $conn, $tsql, $inputs ); - if ( !$stmt) - PrintError(); - - //expects an error in Column Encryption enabled connection - print_r( "Using sqlsrv_query and binding parameters with parameter arrays and no sqltypes provided:\n" ); - $stmt = sqlsrv_query( $conn, $tsql, array( array( $inputs[0], SQLSRV_PARAM_IN ), - array( $inputs[1], SQLSRV_PARAM_IN ), - array( $inputs[2], SQLSRV_PARAM_IN ), - array( $inputs[3], SQLSRV_PARAM_IN ))); - if ( !$stmt) - PrintError(); - - //no error is expected - print_r( "Using sqlsrv_query and binding parameters with parameter arrays and sqltypes provided:\n" ); - $stmt = sqlsrv_query( $conn, $tsql, array( array( $inputs[0], null, null, SQLSRV_SQLTYPE_CHAR(11) ), - array( $inputs[1], null, null, SQLSRV_SQLTYPE_NVARCHAR(50) ), - array( $inputs[2], null, null, SQLSRV_SQLTYPE_NVARCHAR(50) ), - array( $inputs[3], null, null, SQLSRV_SQLTYPE_DATE ) )); - if ( !$stmt) - PrintError(); - - SelectData(); - - + + $tableName = createPatientsTable(); + + $tsql = "INSERT INTO $tableName (SSN, FirstName, LastName, BirthDate) VALUES (?, ?, ?, ?)"; + $inputs = array('748-68-0245', 'Jeannette', 'McDonald', '2002-11-28'); + + // expects an error in Column Encryption enabled connection + print_r("Using sqlsrv_query and binding parameters with literal values:\n"); + $stmt = sqlsrv_query($conn, $tsql, $inputs); + if (!$stmt) { + printError(); + } + + // expects an error in Column Encryption enabled connection + print_r("Using sqlsrv_query and binding parameters with parameter arrays and no sqltypes provided:\n"); + $stmt = sqlsrv_query($conn, $tsql, array(array($inputs[0], SQLSRV_PARAM_IN), + array($inputs[1], SQLSRV_PARAM_IN), + array($inputs[2], SQLSRV_PARAM_IN), + array($inputs[3], SQLSRV_PARAM_IN))); + if (!$stmt) { + printError(); + } + // no error is expected + print_r("Using sqlsrv_query and binding parameters with parameter arrays and sqltypes provided:\n"); + $stmt = sqlsrv_query($conn, $tsql, array(array($inputs[0], null, null, SQLSRV_SQLTYPE_CHAR(11)), + array($inputs[1], null, null, SQLSRV_SQLTYPE_NVARCHAR(50)), + array($inputs[2], null, null, SQLSRV_SQLTYPE_NVARCHAR(50)), + array($inputs[3], null, null, SQLSRV_SQLTYPE_DATE))); + if (!$stmt) { + printError(); + } + selectData(); + echo "Done\n"; ?> --EXPECT--