Removed assert and added a new test

This commit is contained in:
Jenny Tam 2020-10-21 18:28:46 -07:00
parent cbd3c18cb6
commit dff66a7982
3 changed files with 272 additions and 60 deletions

View file

@ -892,8 +892,9 @@ SQLRETURN binary_to_string( _Inout_ SQLCHAR* field_data, _Inout_ SQLLEN& read_so
// Set the amount of space necessary for null characters at the end of the data.
SQLSMALLINT extra = sizeof(Char);
SQLSRV_ASSERT( ((buffer_length - extra) % (extra * 2)) == 0, "Must be multiple of 2 for binary to system string or "
"multiple of 4 for binary to wide string" );
// TO convert a binary to a system string or a binary to a wide string, the buffer size minur extra
// is expected to be multiples of 2 or 4 (depending on Char), but calculating to_copy_hex below
// takes care of this.
// all fields will be treated as ODBC returns varchar(max) fields:
// the entire length of the string is returned the first
@ -921,8 +922,8 @@ SQLRETURN binary_to_string( _Inout_ SQLCHAR* field_data, _Inout_ SQLLEN& read_so
Char* h = reinterpret_cast<Char*>( buffer );
BYTE* b = reinterpret_cast<BYTE*>( field_data + read_so_far );
// to_copy contains the number of bytes to copy, so we divide the number in half (or quarter)
// to get the number of hex digits we can copy
SQLLEN to_copy_hex = to_copy / (2 * extra);
// to get the maximum number of hex digits we can copy
SQLLEN to_copy_hex = static_cast<SQLLEN>(floor(to_copy / (2 * extra)));
for( SQLLEN i = 0; i < to_copy_hex; ++i ) {
*h = hex_chars[(*b & 0xf0) >> 4];
h++;

View file

@ -1,30 +1,29 @@
--TEST--
Test fetching varchar max and varbinary fields with client buffer
Test fetching varchar and nvarchar max fields
--DESCRIPTION--
Test fetching varbinary and varchar max fields as streams or strings with client buffer
Test fetching varchar and nvarchar max fields as streams or strings with or without client buffer
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--ENV--
PHPT_EXEC=true
--FILE--
<?php
require_once('MsCommon.inc');
$conn = AE\connect();
$tableName = "test_max_fields";
$tableName = "char_max_fields";
$columns = array(new AE\ColumnMeta("varchar(max)", "varchar_max_col"),
new AE\ColumnMeta("varbinary(max)", "varbinary_max_col"));
new AE\ColumnMeta("nvarchar(max)", "nvarchar_max_col"));
AE\createTable($conn, $tableName, $columns);
$strValue = str_repeat("ÃÜðßZZýA©", 600);
$strValue = str_repeat("SimpleTest", 450);
$nstrValue = str_repeat("ÃÜðßZZýA©", 600);
$input = strtoupper(bin2hex('abcdefghijklmnopqrstuvwxyz'));
$binaryValue = str_repeat($input, 100);
$insertSql = "INSERT INTO $tableName (varchar_max_col, varbinary_max_col) VALUES (?, ?)";
$params = array($strValue, array($binaryValue, null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARBINARY('max')));
$insertSql = "INSERT INTO $tableName (varchar_max_col, nvarchar_max_col) VALUES (?, ?)";
$params = array(array($strValue, null, null, SQLSRV_SQLTYPE_VARCHAR('max')),
array($nstrValue, null, SQLSRV_PHPTYPE_STRING('UTF-8'), SQLSRV_SQLTYPE_NVARCHAR('max')));
$stmt = sqlsrv_prepare($conn, $insertSql, $params);
if ($stmt) {
@ -36,65 +35,92 @@ if ($stmt) {
fatalError("Failed to prepare insert statement");
}
$query = "SELECT * FROM $tableName";
$stmt = sqlsrv_prepare($conn, $query, null, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED));
if ($stmt) {
$res = sqlsrv_execute($stmt);
if (!$res) {
fatalError("Failed to fetch data");
}
} else {
fatalError("Failed to prepare select statement");
}
runTest($conn, false);
runTest($conn, true);
if (!sqlsrv_fetch($stmt)) {
fatalError("Failed to fetch row");
}
$stream = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));
$success = false;
if ($stream !== false) {
$value = '';
$num = 0;
while (!feof($stream)) {
$value .= fread($stream, 8192);
}
fclose($stream);
if (checkData($value, $strValue)) { // compare the data to see if they match!
$success = true;
}
}
if (!$success) {
fatalError("Failed to fetch stream ");
}
$value = sqlsrv_get_field($stmt, 1, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
if (!checkData($value, $binaryValue)) { // compare the data to see if they match!
echo("Expected:\n$binaryValue\nActual:\n$value\n");
}
echo "Done.\n";
dropTable($conn, $tableName);
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
echo "Done\n";
///////////////////////////////////////////////////////////////////////////////////////////////
function runTest($conn, $buffered)
{
global $tableName, $strValue, $nstrValue;
trace("runTest ($buffered)\n");
$query = "SELECT * FROM $tableName";
if ($buffered) {
$stmt = sqlsrv_prepare($conn, $query, null, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED));
} else {
$stmt = sqlsrv_prepare($conn, $query);
}
if (!$stmt) {
fatalError("runTest ($buffered): failed to prepare select statement");
}
if (!sqlsrv_execute($stmt)) {
fatalError("runTest ($buffered): failed to execute select");
}
if (!sqlsrv_fetch($stmt)) {
fatalError("runTest ($buffered): failed to fetch data");
}
fetchAsString($stmt, 0, $strValue);
fetchAsString($stmt, 1, $nstrValue);
if (!sqlsrv_execute($stmt)) {
fatalError("runTest ($buffered): failed to execute select");
}
if (!sqlsrv_fetch($stmt)) {
fatalError("runTest ($buffered): failed to fetch data");
}
fetchAsStream($stmt, 0, $strValue);
fetchAsStream($stmt, 1, $nstrValue);
}
function fetchAsString($stmt, $index, $expected)
{
trace("fetchAsString ($index):\n");
$sqltype = ($index > 0) ? SQLSRV_PHPTYPE_STRING('UTF-8') : SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR);
$value = sqlsrv_get_field($stmt, $index, $sqltype);
if (!checkData($value, $expected)) {
echo("fetchAsString ($index) expected:\n$expected\nActual:\n$value\n");
}
}
function fetchAsStream($stmt, $index, $expected)
{
trace("fetchAsStream ($index):\n");
$sqltype = ($index > 0) ? SQLSRV_PHPTYPE_STREAM('UTF-8') : SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR);
$stream = sqlsrv_get_field($stmt, $index, $sqltype);
if ($stream !== false) {
$value = '';
while (!feof($stream)) {
$value .= fread($stream, 8192);
}
fclose($stream);
if (!checkData($value, $expected)) {
echo("fetchAsStream ($index) expected:\n$expected\nActual:\n$value\n");
}
}
}
function checkData($actual, $expected)
{
$success = true;
$pos = strpos($actual, $expected);
if (($pos === false) || ($pos > 1)) {
$success = false;
$success = false;
}
if (!$success) {
trace("\nData error\nExpected:\n$expected\nActual:\n$actual\n");
}
return ($success);
}
?>
--EXPECT--
Done.
Done

View file

@ -0,0 +1,185 @@
--TEST--
Test fetching varbinary max fields with client buffer
--DESCRIPTION--
Test fetching varbinary max fields as streams or strings using client buffer
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--ENV--
PHPT_EXEC=true
--FILE--
<?php
require_once('MsCommon.inc');
function fetchNull($stmt, $sqltype, $message)
{
$result = sqlsrv_get_field($stmt, 1, $sqltype);
if (!is_null($result)) {
echo("$message: expected NULL\n");
}
}
function fetchStream($stmt, $test)
{
global $binaryValue, $hexValue;
if (!sqlsrv_execute($stmt)) {
fatalError("fetchStream: failed to execute select");
}
if (!sqlsrv_fetch($stmt)) {
fatalError("fetchStream: failed to fetch row");
}
switch ($test) {
case 1:
$sqltype = SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR);
trace("fetchStream (char string):\n");
$expected = $hexValue;
break;
case 2:
$sqltype = SQLSRV_PHPTYPE_STREAM('UTF-8');
trace("fetchStream (char string):\n");
$expected = $hexValue;
break;
case 3:
$sqltype = SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY);
trace("fetchStream (binary string):\n");
$expected = $binaryValue;
break;
default:
echo "fetchStream: something went wrong\n";
break;
}
$stream = sqlsrv_get_field($stmt, 0, $sqltype);
if ($stream !== false) {
$value = '';
while (!feof($stream)) {
$value .= fread($stream, 8192);
}
fclose($stream);
if (!checkData($value, $expected)) {
echo("Expected:\n$expected\nActual:\n$value\n");
}
} else {
fatalError("fetchStream ($test) failed");
}
}
function fetchData($stmt, $test)
{
global $binaryValue, $hexValue;
if (!sqlsrv_execute($stmt)) {
fatalError("fetchData: failed to execute select");
}
if (!sqlsrv_fetch($stmt)) {
fatalError("fetchData: failed to fetch row");
}
switch ($test) {
case 1:
$sqltype = SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR);
trace("fetchData (char string):\n");
$expected = $hexValue;
break;
case 2:
$sqltype = SQLSRV_PHPTYPE_STRING('UTF-8');
trace("fetchData (char string):\n");
$expected = $hexValue;
break;
case 3:
$sqltype = SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY);
trace("fetchData (binary string):\n");
$expected = $binaryValue;
break;
default:
echo "fetchData: something went wrong\n";
break;
}
$value = sqlsrv_get_field($stmt, 0, $sqltype);
if (!checkData($value, $expected)) {
echo("Expected:\n$expected\nActual:\n$value\n");
}
fetchNull($stmt, $sqltype, "fetchData ($test)\n");
}
function runTest($conn, $buffered)
{
global $tableName, $binaryValue, $hexValue;
$query = "SELECT * FROM $tableName";
if ($buffered) {
trace("Test using a client buffer\n");
$stmt = sqlsrv_prepare($conn, $query, null, array("Scrollable"=>SQLSRV_CURSOR_CLIENT_BUFFERED));
} else {
trace("Test without using a client buffer\n");
$stmt = sqlsrv_prepare($conn, $query);
}
if (!$stmt) {
fatalError("runTest: failed to prepare select statement");
}
fetchData($stmt, 1);
fetchData($stmt, 2);
fetchData($stmt, 3);
fetchStream($stmt, 1);
fetchStream($stmt, 2);
fetchStream($stmt, 3);
}
function checkData($actual, $expected)
{
$success = true;
$pos = strpos($actual, $expected);
if (($pos === false) || ($pos > 1)) {
$success = false;
}
return ($success);
}
$conn = AE\connect();
$tableName = "binary_max_fields";
$columns = array(new AE\ColumnMeta("varbinary(max)", "varbinary_max_col"),
new AE\ColumnMeta("varbinary(max)", "varbinary_null_col"));
AE\createTable($conn, $tableName, $columns);
$bin = 'abcdefghijk';
$binaryValue = str_repeat($bin, 40);
$hexValue = strtoupper(bin2hex($binaryValue));
$insertSql = "INSERT INTO $tableName (varbinary_max_col, varbinary_null_col) VALUES (?, ?)";
$params = array(array($binaryValue, null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max')),
array(null, null, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARBINARY('max')));
$stmt = sqlsrv_prepare($conn, $insertSql, $params);
if ($stmt) {
$res = sqlsrv_execute($stmt);
if (!$res) {
fatalError("Failed to insert data");
}
} else {
fatalError("Failed to prepare insert statement");
}
runTest($conn, false);
runTest($conn, true);
echo "Done\n";
dropTable($conn, $tableName);
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
--EXPECT--
Done