Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesigned some tests based on recent test results #992

Merged
merged 2 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/shared/core_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ void core_sqlsrv_sensitivity_metadata( _Inout_ sqlsrv_stmt* stmt TSRMLS_DC )
throw core::CoreException();
}

CHECK_CUSTOM_ERROR(true, stmt, SQLSRV_ERROR_DATA_CLASSIFICATION_FAILED, "Unexpected SQL Error state") {
CHECK_CUSTOM_ERROR(true, stmt, SQLSRV_ERROR_DATA_CLASSIFICATION_FAILED, "Check if ODBC driver or the server supports the Data Classification feature.") {
throw core::CoreException();
}
}
Expand Down
17 changes: 12 additions & 5 deletions test/functional/pdo_sqlsrv/pdo_data_classification.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function testConnAttrCases()
}
}

function testNotAvailable($conn, $tableName, $isSupported)
function testNotAvailable($conn, $tableName, $isSupported, $driverCapable)
{
// If supported, the query should return a column with no classification
$options = array(PDO::SQLSRV_ATTR_DATA_CLASSIFICATION => true);
Expand All @@ -66,25 +66,31 @@ function testNotAvailable($conn, $tableName, $isSupported)
$stmt->execute();

$notAvailableErr = '*Failed to retrieve Data Classification Sensitivity Metadata. If the driver and the server both support the Data Classification feature, check whether the query returns columns with classification information.';

$unexpectedErrorState = '*Failed to retrieve Data Classification Sensitivity Metadata: Check if ODBC driver or the server supports the Data Classification feature.';

$error = ($driverCapable) ? $notAvailableErr : $unexpectedErrorState;
try {
$metadata = $stmt->getColumnMeta(0);
echo "testNotAvailable: expected getColumnMeta to fail\n";
} catch (PDOException $e) {
if (!fnmatch($notAvailableErr, $e->getMessage())) {
if (!fnmatch($error, $e->getMessage())) {
echo "testNotAvailable: exception unexpected\n";
var_dump($e->getMessage());
}
}
}

function isDataClassSupported($conn)
function isDataClassSupported($conn, &$driverCapable)
{
// Check both SQL Server version and ODBC driver version
$msodbcsqlVer = $conn->getAttribute(PDO::ATTR_CLIENT_VERSION)["DriverVer"];
$version = explode(".", $msodbcsqlVer);

// ODBC Driver must be 17.2 or above
$driverCapable = true;
if ($version[0] < 17 || $version[1] < 2) {
$driverCapable = false;
return false;
}

Expand Down Expand Up @@ -238,7 +244,8 @@ try {
testConnAttrCases();

$conn = connect();
$isSupported = isDataClassSupported($conn);
$driverCapable = true;
$isSupported = isDataClassSupported($conn, $driverCapable);

// Create a test table
$tableName = 'pdoPatients';
Expand Down Expand Up @@ -274,7 +281,7 @@ try {
}

// Test another error condition
testNotAvailable($conn, $tableName, $isSupported);
testNotAvailable($conn, $tableName, $isSupported, $driverCapable);

// Run the query without data classification metadata
$tsql = "SELECT * FROM $tableName";
Expand Down
17 changes: 12 additions & 5 deletions test/functional/sqlsrv/sqlsrv_data_classification.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PHPT_EXEC=true
<?php
$dataClassKey = 'Data Classification';

function testErrorCases($conn, $tableName, $isSupported)
function testErrorCases($conn, $tableName, $isSupported, $driverCapable)
{
// This function will check two error cases:
// (1) if supported, the query should return a column with no classification
Expand All @@ -22,13 +22,17 @@ function testErrorCases($conn, $tableName, $isSupported)
}

$notAvailableErr = '*Failed to retrieve Data Classification Sensitivity Metadata. If the driver and the server both support the Data Classification feature, check whether the query returns columns with classification information.';

$unexpectedErrorState = '*Failed to retrieve Data Classification Sensitivity Metadata: Check if ODBC driver or the server supports the Data Classification feature.';

$error = ($driverCapable) ? $notAvailableErr : $unexpectedErrorState;

$metadata = sqlsrv_field_metadata($stmt);
if ($metadata) {
echo "testErrorCases (1): expected sqlsrv_field_metadata to fail\n";
}

if (!fnmatch($notAvailableErr, sqlsrv_errors()[0]['message'])) {
if (!fnmatch($error, sqlsrv_errors()[0]['message'])) {
var_dump(sqlsrv_errors());
}

Expand All @@ -49,14 +53,16 @@ function testErrorCases($conn, $tableName, $isSupported)
}
}

function isDataClassSupported($conn)
function isDataClassSupported($conn, &$driverCapable)
{
// Check both SQL Server version and ODBC driver version
$msodbcsqlVer = sqlsrv_client_info($conn)['DriverVer'];
$version = explode(".", $msodbcsqlVer);

// ODBC Driver must be 17.2 or above
$driverCapable = true;
if ($version[0] < 17 || $version[1] < 2) {
$driverCapable = false;
return false;
}

Expand Down Expand Up @@ -221,7 +227,8 @@ if (!$conn) {
fatalError("Failed to connect.\n");
}

$isSupported = isDataClassSupported($conn);
$driverCapable = true;
$isSupported = isDataClassSupported($conn, $driverCapable);

// Create a test table
$tableName = 'srvPatients';
Expand Down Expand Up @@ -262,7 +269,7 @@ if ($isSupported) {
}
}

testErrorCases($conn, $tableName, $isSupported);
testErrorCases($conn, $tableName, $isSupported, $driverCapable);

// Run the query without data classification metadata
$tsql = "SELECT * FROM $tableName";
Expand Down
49 changes: 33 additions & 16 deletions test/functional/sqlsrv/srv_007_login_timeout.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,42 @@ Intentionally provide an invalid server name and set LoginTimeout. Verify the ti

$serverName = "WRONG_SERVER_NAME";

$t0 = microtime(true);

// Based on the following reference, a login timeout of less than approximately 10 seconds
// Based on the following reference, a login timeout of less than approximately 10 seconds
// is not reliable. The defaut is 15 seconds so we fix it at 20 seconds.
// https://docs.microsoft.com/sql/connect/odbc/windows/features-of-the-microsoft-odbc-driver-for-sql-server-on-windows

$timeout = 20;
$conn = sqlsrv_connect($serverName , array("LoginTimeout" => $timeout));

$t1 = microtime(true);

$elapsed = $t1 - $t0;
$diff = abs($elapsed - $timeout);

if ($elapsed < $timeout || $diff > 1.0) {
echo "Connection failed at $elapsed secs. Leeway is 1.0 sec but the difference is $diff\n";
}

print "Done";
$timeout = 20;
$maxAttempts = 3;
$numAttempts = 0;
$leeway = 1.0;
$missed = false;

do {
$t0 = microtime(true);

$conn = sqlsrv_connect($serverName , array("LoginTimeout" => $timeout));
$numAttempts++;

$t1 = microtime(true);

// Sometimes time elapsed might be less than expected timeout, such as 19.99*
// something, but 1.0 second leeway should be reasonable
$elapsed = $t1 - $t0;
$diff = abs($elapsed - $timeout);

$missed = ($diff > $leeway);
if ($missed) {
if ($numAttempts == $maxAttempts) {
echo "Connection failed at $elapsed secs. Leeway is $leeway sec but the difference is $diff\n";
} else {
// The test will fail but this helps us decide if this test should be redesigned
echo "$numAttempts\t";
sleep(5);
}
}
} while ($missed && $numAttempts < $maxAttempts);

print "Done\n";
?>
--EXPECT--
Done