Fixed the test to work when data not encrypted

This commit is contained in:
Jenny Tam 2017-11-30 14:25:57 -08:00
parent 794279b9cc
commit 581a98085f
2 changed files with 108 additions and 47 deletions

View file

@ -4,15 +4,49 @@ Fetch data from a prepopulated test table given a custom keystore provider
<?php require('skipif_not_ksp.inc'); ?>
--FILE--
<?php
function verifyData($row, $num)
{
$c1 = $num * 10 + $num + 1;
if (AE\isColEncrypted()) {
$c2 = "Sample data $num for column 2";
$c3 = '';
for ($i = 0; $i < 3; $i++) {
// add to letter 'a'
$c3 .= chr(97 + $num + $i);
}
$c4 = "2017-08-" . ($num + 10);
// need to trim the third value because it is a char(5)
if ($row[0] !== $c1 || $row[1] !== $c2 || trim($row[2]) !== $c3 || $row[3] !== $c4) {
echo "Expected the following\n";
echo "c1=$c1\nc2=$c2\nc3=$c3\nc4=$c4\n";
echo "But got these instead\n";
echo "c1=" . $row[0] . "\nc2=" . $row[1] . "\nc3=" . $row[2] . "\nc4=" . $row[3] . "\n" ;
return false;
}
} else {
if ($row[0] !== $c1) {
echo "Expected $c1 but got $row[0]\n";
}
// should expect binary values for the other columns
for ($i = 1; $i <= 3; $i++) {
if (ctype_print($row[1])) {
print "Error: expected a binary array for column $i!\n";
}
}
}
return true;
}
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";
print_r(sqlsrv_errors());
} else {
if ($conn !== false) {
echo "Connected successfully with ColumnEncryption enabled.\n";
}
@ -20,13 +54,15 @@ Fetch data from a prepopulated test table given a custom keystore provider
$tsql = "SELECT * FROM $ksp_test_table";
$stmt = sqlsrv_prepare($conn, $tsql);
if (!sqlsrv_execute($stmt)) {
echo "Failed to fetch data.\n";
print_r(sqlsrv_errors());
fatalError("Failed to fetch data.\n");
}
// fetch data
$id = 0;
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
echo "c1=" . $row[0] . "\tc2=" . $row[1] . "\tc3=" . $row[2] . "\tc4=" . $row[3] . "\n" ;
if (!verifyData($row, $id++)) {
break;
}
}
sqlsrv_free_stmt($stmt);
@ -36,14 +72,4 @@ Fetch data from a prepopulated test table given a custom keystore provider
?>
--EXPECT--
Connected successfully with ColumnEncryption enabled.
c1=1 c2=Sample data 0 for column 2 c3=abc c4=2017-08-10
c1=12 c2=Sample data 1 for column 2 c3=bcd c4=2017-08-11
c1=23 c2=Sample data 2 for column 2 c3=cde c4=2017-08-12
c1=34 c2=Sample data 3 for column 2 c3=def c4=2017-08-13
c1=45 c2=Sample data 4 for column 2 c3=efg c4=2017-08-14
c1=56 c2=Sample data 5 for column 2 c3=fgh c4=2017-08-15
c1=67 c2=Sample data 6 for column 2 c3=ghi c4=2017-08-16
c1=78 c2=Sample data 7 for column 2 c3=hij c4=2017-08-17
c1=89 c2=Sample data 8 for column 2 c3=ijk c4=2017-08-18
c1=100 c2=Sample data 9 for column 2 c3=jkl c4=2017-08-19
Done

View file

@ -4,6 +4,22 @@ Test using sqlsrv_query for binding parameters with column encryption and a cust
<?php require('skipif_not_ksp.inc'); ?>
--FILE--
<?php
class Patient
{
public $SSN;
public $FirstName;
public $LastName;
public $BirthDate;
public function __construct($ssn, $fname, $lname, $bdate)
{
$this->SSN = $ssn;
$this->FirstName = $fname;
$this->LastName = $lname;
$this->BirthDate = $bdate;
}
}
function createPatientsTable()
{
global $conn;
@ -23,26 +39,49 @@ Test using sqlsrv_query for binding parameters with column encryption and a cust
function selectData()
{
global $conn, $tableName;
global $conn, $tableName, $numRows, $patient;
$stmt = sqlsrv_query($conn, "SELECT * FROM $tableName");
$id = 1;
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";
if ($obj->PatientId !== $id) {
echo "Expected $id but got $obj->PatientId\n";
}
if ($obj->SSN !== $patient->SSN) {
echo "Expected $patient->SSN but got $obj->SSN\n";
}
if ($obj->FirstName !== $patient->FirstName) {
echo "Expected $patient->FirstName but got $obj->FirstName\n";
}
if ($obj->LastName !== $patient->LastName) {
echo "Expected $patient->LastName but got $obj->LastName\n";
}
if ($obj->BirthDate !== $patient->BirthDate) {
echo "Expected $patient->BirthDate but got $obj->BirthDate\n";
}
$id++;
}
$rowFetched = $id - 1;
if ($rowFetched != $numRows){
echo "Expected $numRows rows but got $rowFetched\n";
}
}
function printError()
{
global $AEQueryError;
$errors = sqlsrv_errors();
foreach ($errors as $error) {
echo " SQLSTATE: " . $error['SQLSTATE'] . "\n";
echo " code: " . $error['code'] . "\n";
echo " message: " . $error['message'] . "\n\n";
if (AE\isColEncrypted()) {
verifyError($errors[0], 'IMSSP', $AEQueryError);
} else {
foreach ($errors as $error) {
echo " SQLSTATE: " . $error['SQLSTATE'] . "\n";
echo " code: " . $error['code'] . "\n";
echo " message: " . $error['message'] . "\n\n";
}
}
}
@ -51,24 +90,28 @@ Test using sqlsrv_query for binding parameters with column encryption and a cust
require_once('MsHelper.inc');
$conn = AE\connect(array('ReturnDatesAsStrings'=>true));
if ($conn === false) {
echo "Failed to connect.\n";
printError();
} else {
if ($conn !== false) {
echo "Connected successfully with ColumnEncryption enabled.\n\n";
}
$AEQueryError = 'Must specify the SQL type for each parameter in a parameterized query when using sqlsrv_query in a column encryption enabled connection.';
$tableName = createPatientsTable();
$numRows = 0;
$tsql = "INSERT INTO $tableName (SSN, FirstName, LastName, BirthDate) VALUES (?, ?, ?, ?)";
$inputs = array('748-68-0245', 'Jeannette', 'McDonald', '2002-11-28');
$patient = new Patient('748-68-0245', 'Jeannette', 'McDonald', '2002-11-28');
$inputs = array($patient->SSN, $patient->FirstName, $patient->LastName, $patient->BirthDate);
// 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();
} else {
$numRows++;
}
// expects an error in Column Encryption enabled connection
@ -79,7 +122,10 @@ Test using sqlsrv_query for binding parameters with column encryption and a cust
array($inputs[3], SQLSRV_PARAM_IN)));
if (!$stmt) {
printError();
} else {
$numRows++;
}
// 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)),
@ -88,7 +134,10 @@ Test using sqlsrv_query for binding parameters with column encryption and a cust
array($inputs[3], null, null, SQLSRV_SQLTYPE_DATE)));
if (!$stmt) {
printError();
} else {
$numRows++;
}
selectData();
echo "Done\n";
@ -97,20 +146,6 @@ Test using sqlsrv_query for binding parameters with column encryption and a cust
Connected successfully with ColumnEncryption enabled.
Using sqlsrv_query and binding parameters with literal values:
SQLSTATE: IMSSP
code: -63
message: Must specify the SQL type for each parameter in a parameterized query when using sqlsrv_query in a column encryption enabled connection.
Using sqlsrv_query and binding parameters with parameter arrays and no sqltypes provided:
SQLSTATE: IMSSP
code: -63
message: Must specify the SQL type for each parameter in a parameterized query when using sqlsrv_query in a column encryption enabled connection.
Using sqlsrv_query and binding parameters with parameter arrays and sqltypes provided:
1
748-68-0245
Jeannette
McDonald
2002-11-28
Done