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

Made Azure AD tests more robust #973

Merged
merged 2 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 36 additions & 5 deletions test/functional/pdo_sqlsrv/pdo_azure_ad_access_token.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,24 @@ function simpleTest($conn)
dropTable($conn, $tableName);
}

function connectAzureDB($accToken, $showException)
{
global $adServer, $adDatabase, $maxAttempts;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is $maxAttempts needed here?


$conn = false;
try {
$connectionInfo = "Database = $adDatabase; AccessToken = $accToken;";
$conn = new PDO("sqlsrv:server = $adServer; $connectionInfo");
} catch (PDOException $e) {
if ($showException) {
print_r($e->getMessage());
echo PHP_EOL;
}
}

return $conn;
}

// First test some error conditions
require_once('MsSetup.inc');
connectWithInvalidOptions($server);
Expand All @@ -138,13 +156,26 @@ connectWithEmptyAccessToken($server);

// Next, test with a valid access token and perform some simple tasks
require_once('access_token.inc');
$maxAttempts = 3;

try {
if ($adServer != 'TARGET_AD_SERVER' && $accToken != 'TARGET_ACCESS_TOKEN') {
$connectionInfo = "Database = $adDatabase; AccessToken = $accToken;";
$conn = new PDO("sqlsrv:server = $adServer; $connectionInfo");
$conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true);
simpleTest($conn);
unset($conn);
$conn = false;
$numAttempts = 0;
do {
$conn = connectAzureDB($accToken, ($numAttempts == ($maxAttempts - 1)));
if ($conn === false) {
$numAttempts++;
sleep(10);
}
} while ($conn === false && $numAttempts < $maxAttempts);

// Proceed when successfully connected
if ($conn) {
$conn->setAttribute(PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true);
simpleTest($conn);
unset($conn);
}
}
} catch(PDOException $e) {
print_r( $e->getMessage() );
Expand Down
36 changes: 27 additions & 9 deletions test/functional/pdo_sqlsrv/pdo_azure_ad_authentication.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,39 @@ try {
// your credentials to test, or this part is skipped.
//
$azureServer = $adServer;
$azureDatabase = $adDatabase;
$azureUsername = $adUser;
$azurePassword = $adPassword;
$maxAttempts = 3;

if ($azureServer != 'TARGET_AD_SERVER') {
function connectAzureDB($showException)
{
global $adServer, $adUser, $adPassword, $maxAttempts;

$connectionInfo = "Authentication = ActiveDirectoryPassword; TrustServerCertificate = false";


$conn = false;
try {
$conn = new PDO("sqlsrv:server = $azureServer ; $connectionInfo", $azureUsername, $azurePassword);
$conn = new PDO("sqlsrv:server = $adServer; $connectionInfo", $adUser, $adPassword);
echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n";
} catch (PDOException $e) {
echo "Could not connect with ActiveDirectoryPassword.\n";
print_r($e->getMessage());
echo "\n";
if ($showException) {
echo "Could not connect with ActiveDirectoryPassword after $maxAttempts retries.\n";
print_r($e->getMessage());
echo "\n";
}
}

return $conn;
}

if ($azureServer != 'TARGET_AD_SERVER') {
$conn = false;
$numAttempts = 0;
do {
$conn = connectAzureDB($numAttempts == ($maxAttempts - 1));
if ($conn === false) {
$numAttempts++;
sleep(10);
}
} while ($conn === false && $numAttempts < $maxAttempts);
} else {
echo "Not testing with Authentication=ActiveDirectoryPassword.\n";
}
Expand Down
42 changes: 32 additions & 10 deletions test/functional/sqlsrv/sqlsrv_azure_ad_access_token.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,47 @@ function simpleTest($conn)
dropTable($conn, $tableName);
}

// First test some error conditions
connectWithInvalidOptions($server);

// Then, test with an empty access token
connectWithEmptyAccessToken($server);

// Next, test with a valid access token and perform some simple tasks
require_once('access_token.inc');
if ($adServer != 'TARGET_AD_SERVER' && $accToken != 'TARGET_ACCESS_TOKEN') {
function connectAzureDB($accToken, $showException)
{
global $adServer, $adDatabase, $maxAttempts;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$maxAttempts here too - I think you forgot the error message that includes it.


$conn = false;
$connectionInfo = array("Database"=>$adDatabase, "AccessToken"=>$accToken);

$conn = sqlsrv_connect($adServer, $connectionInfo);
if ($conn === false) {
fatalError("Could not connect with Azure AD AccessToken.\n");
if ($showException) {
fatalError("Could not connect with Azure AD AccessToken.\n");
}
} else {
simpleTest($conn);

sqlsrv_close($conn);
}

return $conn;
}

// First test some error conditions
connectWithInvalidOptions($server);

// Then, test with an empty access token
connectWithEmptyAccessToken($server);

// Next, test with a valid access token and perform some simple tasks
require_once('access_token.inc');
$maxAttempts = 3;

if ($adServer != 'TARGET_AD_SERVER' && $accToken != 'TARGET_ACCESS_TOKEN') {
$conn = false;
$numAttempts = 0;
do {
$conn = connectAzureDB($accToken, ($numAttempts == ($maxAttempts - 1)));
if ($conn === false) {
$numAttempts++;
sleep(10);
}
} while ($conn === false && $numAttempts < $maxAttempts);
}

echo "Done\n";
Expand Down
45 changes: 33 additions & 12 deletions test/functional/sqlsrv/sqlsrv_azure_ad_authentication.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,44 @@ if ($conn === false) {
// Test Azure AD on an Azure database instance. Replace $azureServer, etc with
// your credentials to test, or this part is skipped.
//
$azureServer = $adServer;
$azureDatabase = $adDatabase;
$azureUsername = $adUser;
$azurePassword = $adPassword;

if ($azureServer != 'TARGET_AD_SERVER') {
$connectionInfo = array( "UID"=>$azureUsername, "PWD"=>$azurePassword,
"Authentication"=>'ActiveDirectoryPassword', "TrustServerCertificate"=>false );

$conn = sqlsrv_connect($azureServer, $connectionInfo);
function connectAzureDB($showException)
{
global $adServer, $adUser, $adPassword, $maxAttempts;

$connectionInfo = array("UID"=>$adUser,
"PWD"=>$adPassword,
"Authentication"=>'ActiveDirectoryPassword',
"TrustServerCertificate"=>false );

$conn = false;
$conn = sqlsrv_connect($adServer, $connectionInfo);
if ($conn === false) {
echo "Could not connect with ActiveDirectoryPassword.\n";
print_r(sqlsrv_errors());
if ($showException) {
echo "Could not connect with ActiveDirectoryPassword after $maxAttempts retries.\n";
print_r(sqlsrv_errors());
}
} else {
echo "Connected successfully with Authentication=ActiveDirectoryPassword.\n";
sqlsrv_close($conn);
}

return $conn;
}

$azureServer = $adServer;
$maxAttempts = 3;

if ($azureServer != 'TARGET_AD_SERVER') {
$conn = false;
$numAttempts = 0;
do {
$conn = connectAzureDB($numAttempts == ($maxAttempts - 1));
if ($conn === false) {
$numAttempts++;
sleep(10);
}
} while ($conn === false && $numAttempts < $maxAttempts);

} else {
echo "Not testing with Authentication=ActiveDirectoryPassword.\n";
}
Expand Down