Modify sqlsrv tests to handle the new error message with stream data

This commit is contained in:
Jenny Tam 2017-12-12 15:32:39 -08:00
parent a8c43e8d20
commit 0de708e3ab
20 changed files with 190 additions and 65 deletions

View file

@ -23,19 +23,26 @@ function runTest($fieldType)
sqlsrv_fetch($stmt)
|| die(print_r(sqlsrv_errors(), true));
($stream = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STREAM("char")))
|| die(print_r(sqlsrv_errors(), true));
stream_filter_append($originalStream, "convert.base64-encode")
|| die(print_r(error_get_last()));
while (($originalLine = fread($originalStream, 80)) &&
($dbLine = fread($stream, 80))) {
if ($originalLine != $dbLine) {
die("Not identical");
// Do not support getting stream if AE enabled, so expect
// it to fail with the correct error message
$stream = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STREAM("char"));
if ($stream) {
stream_filter_append($originalStream, "convert.base64-encode")
|| die(print_r(error_get_last()));
while (($originalLine = fread($originalStream, 80)) &&
($dbLine = fread($stream, 80))) {
if ($originalLine != $dbLine) {
die("Not identical");
}
}
} else {
if (AE\isColEncrypted()) {
verifyError(sqlsrv_errors()[0], 'IMSSP', 'Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.');
} else {
fatalError('Fetching data stream failed!');
}
}
dropTable($conn, $params['tableName']);
sqlsrv_free_stmt($stmt) || die(print_r(sqlsrv_errors(), true));

View file

@ -38,14 +38,17 @@ inserting and retrieving UTF-8 text.
$u = sqlsrv_get_field($s, 1, SQLSRV_PHPTYPE_STREAM('utf-8'));
if ($u === false) {
die(print_r(sqlsrv_errors(), true));
if (AE\isColEncrypted()) {
verifyError(sqlsrv_errors()[0], 'IMSSP', 'Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.');
} else {
die(print_r(sqlsrv_errors(), true));
}
} else {
$utf8_2 = fread($u, 10000);
if ($utf8 != $utf8_2) {
fatalError("round trip failed");
}
}
$utf8_2 = fread($u, 10000);
if ($utf8 != $utf8_2) {
fatalError("round trip failed");
}
dropTable($c, 'utf8test');
echo "Test succeeded\n";

View file

@ -90,7 +90,8 @@ class ColumnMeta
{
$append = " ";
if (isColEncrypted() && $this->encryptable) {
if ($this->encryptable && isDataEncrypted()) {
$cekName = getCekName();
if (stripos($this->dataType, "char") !== false) {
$append .= "COLLATE Latin1_General_BIN2 ";
@ -325,13 +326,26 @@ function getSeqPlaceholders($num)
}
/**
* @return bool false if $keystore specified in MsSetup.inc is none or data not encrypted,
* otherwise return true
* @return bool false if $keystore specified in MsSetup.inc is none; return true otherwise
*/
function isColEncrypted()
{
global $keystore, $dataEncrypted;
if ($keystore == KEYSTORE_NONE || !$dataEncrypted) {
if ($keystore === KEYSTORE_NONE) {
return false;
} else {
return true;
}
}
/**
* @return bool false if $keystore specified in MsSetup.inc is none or data not encrypted;
* return true otherwise
*/
function isDataEncrypted()
{
global $keystore, $dataEncrypted;
if ($keystore === KEYSTORE_NONE || !$dataEncrypted) {
return false;
} else {
return true;
@ -1065,6 +1079,23 @@ function getColName($k)
return ("");
}
function isStreamData($k)
{
switch ($k) {
case 14: return (true); // varchar(max)
case 17: return (true); // nvarchar(max)
case 18: return (true); // text
case 19: return (true); // ntext
case 20: return (true); // binary
case 21: return (true); // varbinary(512)
case 22: return (true); // varbinary(max)
case 23: return (true); // image
case 27: return (true); // timestamp
case 28: return (true); // xml
default: break;
}
return (false);
}
function getColSize($k)
{

View file

@ -33,6 +33,9 @@ function fetchFields()
$stmt1 = AE\selectFromTable($conn1, $tableName);
$numFields = sqlsrv_num_fields($stmt1);
$errState = 'IMSSP';
$errMessage = 'Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.';
trace("Retrieving $noRowsInserted rows with $numFields fields each ...");
for ($i = 0; $i < $noRowsInserted; $i++) {
$row = sqlsrv_fetch($stmt1);
@ -41,8 +44,16 @@ function fetchFields()
}
for ($j = 0; $j < $numFields; $j++) {
$fld = sqlsrv_get_field($stmt1, $j);
// With AE enabled, those fields that sqlsrv_get_field() will fetch
// as stream data will return a specific error message
$col = $j+1;
if ($fld === false) {
fatalError("Field $j of Row $i is missing\n", true);
if (AE\isColEncrypted() && isStreamData($col)) {
verifyError(sqlsrv_errors()[0], $errState, $errMessage);
} else {
fatalError("Field $j of Row $i is missing\n", true);
}
}
}
}

View file

@ -69,7 +69,7 @@ function verifyStream($stmt, $row, $colIndex)
}
}
if ($stream === false) {
fatalError("Failed to read field $col: $type");
verifyStreamError("Failed to read field $col: $type");
} else {
$value = '';
if ($stream) {
@ -88,6 +88,15 @@ function verifyStream($stmt, $row, $colIndex)
}
}
function verifyStreamError($message)
{
global $errState, $errMessage;
if (AE\isColEncrypted()) {
verifyError(sqlsrv_errors()[0], $errState, $errMessage);
} else {
fatalError($message);
}
}
function checkData($col, $actual, $expected)
{
@ -123,6 +132,10 @@ if (!isWindows()) {
global $testName;
$testName = "Stream - Read";
// error message expected with AE enabled
$errState = 'IMSSP';
$errMessage = 'Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.';
// test ansi only if windows or non-UTF8 locales are supported (ODBC 17 and above)
startTest($testName);
if (isWindows() || isLocaleSupported()) {

View file

@ -89,6 +89,16 @@ function streamScroll($noRows, $startRow)
sqlsrv_close($conn1);
}
function verifyStreamError($message)
{
global $errState, $errMessage;
if (AE\isColEncrypted()) {
verifyError(sqlsrv_errors()[0], $errState, $errMessage);
} else {
fatalError($message);
}
}
function verifyStream($stmt, $row, $colIndex)
{
$col = $colIndex + 1;
@ -104,7 +114,7 @@ function verifyStream($stmt, $row, $colIndex)
}
}
if ($stream === false) {
fatalError("Failed to read field $col: $type");
verifyStreamError("Failed to read field $col: $type");
} else {
$value = '';
if ($stream) {
@ -159,6 +169,10 @@ if (!isWindows()) {
global $testName;
$testName = "Stream - Scrollable";
// error message expected with AE enabled
$errState = 'IMSSP';
$errMessage = 'Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.';
// test ansi only if windows or non-UTF8 locales are supported (ODBC 17 and above)
startTest($testName);
if (isWindows() || isLocaleSupported()) {

View file

@ -272,14 +272,23 @@ function runTest($noPasses, $noRows, $tableName, $conn, $prepared, $release, $mo
break;
case 5: // fetch fields
$errState = 'IMSSP';
$errMessage = 'Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.';
$stmt = execQuery($conn, $tableName, $prepared);
$numFields = sqlsrv_num_fields($stmt);
while (sqlsrv_fetch($stmt)) {
$rowCount++;
for ($i = 0; $i < $numFields; $i++) {
$fld = sqlsrv_get_field($stmt, $i);
$col = $i + 1;
if ($fld === false) {
die("Field $i of row $rowCount is missing");
if (AE\isColEncrypted() && isStreamData($col)) {
verifyError(sqlsrv_errors()[0], $errState, $errMessage);
} else {
fatalError("Field $i of row $rowCount is missing");
}
}
unset($fld);
}

View file

@ -28,7 +28,7 @@ foreach ($dataTypes as $dataType) {
$r;
$stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputValues[0], $colMetaArr[1]->colName => $inputValues[1] ), $r);
if (!AE\isColEncrypted()) {
if (!AE\isDataEncrypted()) {
if ($r === false) {
echo "Default type should be compatible with $dataType.\n";
$success = false;

View file

@ -44,7 +44,7 @@ sqlsrv_free_stmt($stmt);
//for AE only
echo "\nChecking ciphertext data:\n";
if (AE\isColEncrypted()) {
if (AE\isDataEncrypted()) {
$conn1 = connect(null, true);
$selectSql = "SELECT SSN, FirstName, LastName, BirthDate FROM $tbname";
$stmt = sqlsrv_query($conn1, $selectSql);

View file

@ -43,7 +43,7 @@ AE\fetchAll($conn, $tbname);
sqlsrv_free_stmt($stmt);
// for AE only
if (AE\isColEncrypted()) {
if (AE\isDataEncrypted()) {
$conn1 = connect(null, true);
$selectSql = "SELECT * FROM $tbname";

View file

@ -14,7 +14,6 @@ $tbname = 'NVarcharAnalysis';
$colMetaArr = array( new AE\ColumnMeta("int", "CharCount", "IDENTITY(0,1)"), new AE\ColumnMeta("nvarchar(1000)"));
AE\createTable($conn, $tbname, $colMetaArr);
// insert 1000 rows
for ($i = 0; $i < 1000; $i++) {
$data = str_repeat("*", $i);
@ -33,7 +32,7 @@ while ($decrypted_row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
sqlsrv_free_stmt($stmt);
// for AE only
if (AE\isColEncrypted()) {
if (AE\isDataEncrypted()) {
$conn1 = connect(null, true);
$stmt = sqlsrv_query($conn1, $selectSql);
while ($encrypted_row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {

View file

@ -33,7 +33,7 @@ while ($decrypted_row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
sqlsrv_free_stmt($stmt);
// for AE only
if (AE\isColEncrypted()) {
if (AE\isDataEncrypted()) {
$conn1 = connect(null, true);
$stmt = sqlsrv_query($conn1, $selectSql);
while ($encrypted_row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {

View file

@ -40,7 +40,7 @@ foreach ($dataTypes as $dataType) {
$r;
$stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputs[0], $colMetaArr[1]->colName => $inputs[1] ), $r, AE\INSERT_PREPARE_PARAMS);
if (!AE\isColEncrypted()) {
if (!AE\isDataEncrypted()) {
if ($r === false) {
$isCompatible = false;
foreach ($compatList[$dataType] as $compatType) {

View file

@ -35,7 +35,7 @@ foreach ($dataTypes as $dataType) {
$r;
$stmt = AE\insertRow($conn, $tbname, array($colMetaArr[0]->colName => $inputs[0], $colMetaArr[1]->colName => $inputs[1]), $r, AE\INSERT_PREPARE_PARAMS);
if (!AE\isColEncrypted()) {
if (!AE\isDataEncrypted()) {
if ($r === false) {
$isCompatible = false;
foreach ($compatList[$dataType] as $compatType) {

View file

@ -43,7 +43,7 @@ foreach ($dataTypes as $dataType) {
$r;
$stmt = AE\insertRow($conn, $tbname, array( $colMetaArr[0]->colName => $inputs[0], $colMetaArr[1]->colName => $inputs[1] ), $r, AE\INSERT_PREPARE_PARAMS);
if (!AE\isColEncrypted()) {
if (!AE\isDataEncrypted()) {
if ($r === false) {
$isCompatible = false;
foreach ($compatList[$dataType] as $compatType) {

View file

@ -76,9 +76,17 @@ binding parameters, including output parameters, using the simplified syntax.
$name = sqlsrv_get_field($stmt, 2, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
echo "$name\n";
$stream = sqlsrv_get_field($stmt, 3, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
while (!feof($stream)) {
$str = fread($stream, 10000);
echo $str;
if (!$stream) {
if (AE\isColEncrypted()) {
verifyError(sqlsrv_errors()[0], 'IMSSP', 'Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.');
} else {
fatalError('Fetching data stream failed!');
}
} else {
while (!feof($stream)) {
$str = fread($stream, 10000);
echo $str;
}
}
echo "\n";
}
@ -121,14 +129,14 @@ binding parameters, including output parameters, using the simplified syntax.
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
--EXPECTF--
--EXPECTREGEX--
1
12.0
12\.0
testtestte
This is some text meant to test binding parameters to streams
(This is some text meant to test binding parameters to streams)?
2
13.0
13\.0
testtestte
This is some more text meant to test binding parameters to streams
(This is some more text meant to test binding parameters to streams)?
3
4

View file

@ -42,17 +42,25 @@ sqlsrv_query test. Performs same tasks as 0006.phpt, using sqlsrv_query.
while (sqlsrv_fetch($stmt)) {
$id = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
echo "$id <br/>";
echo "$id\n";
$double = sqlsrv_get_field($stmt, 1, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
echo "$double <br/>";
echo "$double\n";
$name = sqlsrv_get_field($stmt, 2, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
echo "$name <br/>";
echo "$name\n";
$stream = sqlsrv_get_field($stmt, 3, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
while (!feof($stream)) {
$str = fread($stream, 4000);
echo $str;
if (!$stream) {
if (AE\isColEncrypted()) {
echo sqlsrv_errors()[0]['message'];
} else {
fatalError('Fetching data stream failed!');
}
} else {
while (!feof($stream)) {
$str = fread($stream, 4000);
echo $str;
}
}
echo "<br/>";
echo "\n";
}
sqlsrv_query($conn, "DROP TABLE test_params");
@ -61,5 +69,8 @@ sqlsrv_query test. Performs same tasks as 0006.phpt, using sqlsrv_query.
sqlsrv_close($conn);
?>
--EXPECTF--
1 <br/>12.0 <br/>testtestte <br/>This is some text meant to test binding parameters to streams<br/>%A
--EXPECTREGEX--
1
12\.0
testtestte
(This is some text meant to test binding parameters to streams|Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.)

View file

@ -113,9 +113,17 @@ binding streams using full syntax.
$name = sqlsrv_get_field($stmt, 2, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
echo "$name\n";
$stream = sqlsrv_get_field($stmt, 3, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
while (!feof($stream)) {
$str = fread($stream, 10000);
echo $str;
if (!$stream) {
if (AE\isColEncrypted()) {
echo sqlsrv_errors()[0]['message'];
} else {
fatalError('Fetching data stream failed!');
}
} else {
while (!feof($stream)) {
$str = fread($stream, 10000);
echo $str;
}
}
echo "\n";
}
@ -153,11 +161,11 @@ binding streams using full syntax.
dropTable($conn, $tableName);
sqlsrv_close($conn);
?>
--EXPECT--
sqlsrv_query(2) failed.
sqlsrv_query(3) failed.
--EXPECTREGEX--
sqlsrv_query\(2\) failed\.
sqlsrv_query\(3\) failed\.
1
12.0
12\.0
testtestte
This is some text meant to test binding parameters to streams
(This is some text meant to test binding parameters to streams|Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.)
Done

View file

@ -47,6 +47,7 @@ function execProc($conn, $tableName, $columnNames, $k, $data, $sqlType)
$spCode = "SET @p2 = ( SELECT c2 FROM $tableName WHERE c1 = @p1 )";
$procName = "testBindOutSp";
dropProc($conn, $procName);
$stmt1 = sqlsrv_query($conn, "CREATE PROC [$procName] ($spArgs) AS BEGIN $spCode END");
sqlsrv_free_stmt($stmt1);

View file

@ -82,9 +82,19 @@ error messages when trying to retrieve past the end of a result set and when no
$name = sqlsrv_get_field($stmt, 2, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
echo "$name\n";
$stream = sqlsrv_get_field($stmt, 3, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
while (!feof($stream)) {
$str = fread($stream, 10000);
echo $str;
if (!$stream) {
if (AE\isColEncrypted()) {
verifyError(sqlsrv_errors()[0], 'IMSSP', 'Connection with Column Encryption enabled does not support fetching stream. Please fetch the data as a string.');
} else {
fatalError('Fetching data stream failed!');
}
} else {
while (!feof($stream)) {
$str = fread($stream, 10000);
if ($str !== "This is some text meant to test binding parameters to streams") {
fatalError("Incorrect data: \'$str\'!\n");
}
}
}
echo "\n";
}
@ -127,11 +137,11 @@ Array
1
12.0
testtestte
This is some text meant to test binding parameters to streams
2
13.0
testtestte
This is some more text meant to test binding parameters to streams
Array
(
[0] => Array