Modified files as per review comments

This commit is contained in:
Jenny Tam 2017-11-09 09:29:13 -08:00
parent 41eec918c9
commit 1daa4f0623
8 changed files with 115 additions and 134 deletions

View file

@ -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");

View file

@ -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)) {

View file

@ -1,7 +1,7 @@
<?php
if(! extension_loaded( "sqlsrv" ) )
die( "skip extension not loaded" );
if (!extension_loaded("sqlsrv"))
die("skip extension not loaded");
require_once('MsCommon.inc');

View file

@ -9,7 +9,7 @@ Fetch data from a prepopulated test table given a custom keystore provider
require_once('MsHelper.inc');
$conn = AE\connect(array('ReturnDatesAsStrings'=>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());
}

View file

@ -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";

View file

@ -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,

View file

@ -4,7 +4,7 @@ Test simple insert, fetch and update with ColumnEncryption enabled and a custome
<?php require('skipif_not_ksp.inc'); ?>
--FILE--
<?php
function CreatePatientsTable()
function createPatientsTable()
{
global $conn;
$tableName = 'Patients';
@ -82,13 +82,13 @@ Test simple insert, fetch and update with ColumnEncryption enabled and a custome
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 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);

View file

@ -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--
<?php require('skipif_not_ksp.inc'); ?>
--FILE--
<?php
function CreatePatientsTable()
function createPatientsTable()
{
global $conn;
$tablename = 'Patients';
$stmt = sqlsrv_query( $conn, "IF OBJECT_ID('$tablename', 'U') IS NOT NULL DROP TABLE $tablename" );
sqlsrv_free_stmt( $stmt );
$tableName = 'Patients';
$tsql = "CREATE TABLE $tablename (
[PatientId] [int] IDENTITY(1,1) NOT NULL,
[SSN] [char](11) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CustomCEK, ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL,
[FirstName] [nvarchar](50) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CustomCEK, ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL,
[LastName] [nvarchar](50) COLLATE Latin1_General_BIN2 ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CustomCEK, ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NULL,
[BirthDate] [date] ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = CustomCEK, ENCRYPTION_TYPE = Randomized, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL)";
$stmt = sqlsrv_query( $conn, $tsql );
if (! $stmt )
{
echo "Failed to create test table!\n";
die( print_r( sqlsrv_errors(), true ));
$columns = array(new AE\ColumnMeta('int', 'PatientId', 'IDENTITY(1,1) NOT NULL'),
new AE\ColumnMeta('char(11)', 'SSN'),
new AE\ColumnMeta('nvarchar(50)', 'FirstName'),
new AE\ColumnMeta('nvarchar(50)', 'LastName'),
new AE\ColumnMeta('date', 'BirthDate'));
$stmt = AE\createTable($conn, $tableName, $columns);
if (!$stmt) {
fatalError("Failed to create test table!\n");
}
return $tablename;
return $tableName;
}
function SelectData()
{
global $conn, $tablename;
$stmt = sqlsrv_query($conn, "SELECT * FROM $tablename");
while ($obj = sqlsrv_fetch_object( $stmt ))
{
function selectData()
{
global $conn, $tableName;
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
while ($obj = sqlsrv_fetch_object($stmt)) {
echo $obj->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--