Skip to content

Commit e8fef22

Browse files
authored
Updated skipifs and modified tests with HGS enabled servers (#1111)
1 parent 7214e8d commit e8fef22

File tree

4 files changed

+132
-131
lines changed

4 files changed

+132
-131
lines changed
Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,106 @@
11
--TEST--
22
Test new connection keyword ColumnEncryption
3+
--DESCRIPTION--
4+
Some test cases return errors as expected. For testing purposes, an enclave enabled
5+
SQL Server and the HGS server are the same instance. If the server is HGS enabled,
6+
the error message of one test case is not the same.
37
--SKIPIF--
48
<?php require('skipif_mid-refactor.inc'); ?>
59
--FILE--
610
<?php
711
require_once("MsSetup.inc");
8-
$msodbcsql_maj = "";
12+
$msodbcsqlMaj = "";
13+
$hgsEnabled = true;
914

10-
try
11-
{
12-
$conn = new PDO( "sqlsrv:server = $server", $uid, $pwd );
13-
$msodbcsql_ver = $conn->getAttribute( PDO::ATTR_CLIENT_VERSION )['DriverVer'];
14-
$msodbcsql_maj = explode(".", $msodbcsql_ver)[0];
15-
}
16-
catch( PDOException $e )
17-
{
15+
try {
16+
$conn = new PDO("sqlsrv:server = $server", $uid, $pwd);
17+
$msodbcsqlVer = $conn->getAttribute(PDO::ATTR_CLIENT_VERSION)['DriverVer'];
18+
$version = explode(".", $msodbcsqlVer);
19+
$msodbcsqlMaj = $version[0];
20+
21+
// Next, check if the server is HGS enabled
22+
$serverInfo = $conn->getAttribute(PDO::ATTR_SERVER_INFO);
23+
if (strpos($serverInfo['SQLServerName'], 'PHPHGS') === false) {
24+
$hgsEnabled = false;
25+
}
26+
} catch (PDOException $e) {
1827
echo "Failed to connect\n";
19-
print_r( $e->getMessage() );
28+
print_r($e->getMessage());
2029
echo "\n";
2130
}
2231

23-
test_ColumnEncryption( $server, $uid, $pwd, $msodbcsql_maj );
32+
testColumnEncryption($server, $uid, $pwd, $msodbcsqlMaj);
2433
echo "Done";
2534

2635

27-
function verify_output( $PDOerror, $expected )
36+
function verifyOutput($PDOerror, $expected, $caseNum)
2837
{
29-
if( strpos( $PDOerror->getMessage(), $expected ) === false )
30-
{
31-
print_r( $PDOerror->getMessage() );
38+
if (strpos($PDOerror->getMessage(), $expected) === false) {
39+
echo "Test case $caseNum failed:\n";
40+
print_r($PDOerror->getMessage());
3241
echo "\n";
3342
}
3443
}
3544

36-
function test_ColumnEncryption( $server, $uid, $pwd, $msodbcsql_maj )
45+
function testColumnEncryption($server, $uid, $pwd, $msodbcsqlMaj)
3746
{
47+
global $hgsEnabled;
48+
3849
// Only works for ODBC 17
3950
////////////////////////////////////////
4051
$connectionInfo = "ColumnEncryption = Enabled;";
41-
try
42-
{
43-
$conn = new PDO( "sqlsrv:server = $server ; $connectionInfo", $uid, $pwd );
44-
}
45-
catch( PDOException $e )
46-
{
47-
if($msodbcsql_maj < 17)
48-
{
52+
try {
53+
$conn = new PDO("sqlsrv:server = $server ; $connectionInfo", $uid, $pwd);
54+
} catch (PDOException $e) {
55+
if ($msodbcsqlMaj < 17) {
4956
$expected = "The Always Encrypted feature requires Microsoft ODBC Driver 17 for SQL Server.";
50-
verify_output( $e, $expected );
51-
}
52-
else
53-
{
54-
print_r( $e->getMessage() );
57+
verifyOutput($e, $expected, "1");
58+
} else {
59+
echo "Test case 1 failed:\n";
60+
print_r($e->getMessage());
5561
echo "\n";
5662
}
5763
}
5864

5965
// Works for ODBC 17, ODBC 13
6066
////////////////////////////////////////
6167
$connectionInfo = "ColumnEncryption = Disabled;";
62-
try
63-
{
64-
$conn = new PDO( "sqlsrv:server = $server ; $connectionInfo", $uid, $pwd );
65-
}
66-
catch( PDOException $e )
67-
{
68-
if($msodbcsql_maj < 13)
69-
{
68+
try {
69+
$conn = new PDO("sqlsrv:server = $server ; $connectionInfo", $uid, $pwd);
70+
} catch (PDOException $e) {
71+
if ($msodbcsqlMaj < 13) {
7072
$expected = "Invalid connection string attribute";
71-
verify_output( $e, $expected );
72-
}
73-
else
74-
{
75-
print_r( $e->getMessage() );
73+
verifyOutput($e, $expected, "2");
74+
} else {
75+
echo "Test case 2 failed:\n";
76+
print_r($e->getMessage());
7677
echo "\n";
7778
}
7879
}
7980

8081
// should fail for all ODBC drivers
82+
$expected = "Invalid value specified for connection string attribute 'ColumnEncryption'";
83+
if ($hgsEnabled) {
84+
$expected = "Requested attestation protocol is invalid.";
85+
}
86+
8187
////////////////////////////////////////
8288
$connectionInfo = "ColumnEncryption = false;";
83-
try
84-
{
85-
$conn = new PDO( "sqlsrv:server = $server ; $connectionInfo", $uid, $pwd );
86-
}
87-
catch( PDOException $e )
88-
{
89-
$expected = "Invalid value specified for connection string attribute 'ColumnEncryption'";
90-
verify_output( $e, $expected );
89+
try {
90+
$conn = new PDO("sqlsrv:server = $server ; $connectionInfo", $uid, $pwd);
91+
} catch (PDOException $e) {
92+
verifyOutput($e, $expected, "3");
9193
}
9294

9395
// should fail for all ODBC drivers
9496
////////////////////////////////////////
9597
$connectionInfo = "ColumnEncryption = 1;";
96-
try
97-
{
98-
$conn = new PDO( "sqlsrv:server = $server ; $connectionInfo", $uid, $pwd );
98+
try {
99+
$conn = new PDO("sqlsrv:server = $server ; $connectionInfo", $uid, $pwd);
100+
} catch (PDOException $e) {
101+
verifyOutput($e, $expected, "4");
99102
}
100-
catch( PDOException $e )
101-
{
102-
$expected = "Invalid value specified for connection string attribute 'ColumnEncryption'";
103-
verify_output( $e, $expected );
104-
}
105-
}
103+
}
106104
?>
107105
--EXPECT--
108106
Done

test/functional/pdo_sqlsrv/skipif_not_hgs.inc

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@
33
// SQL Server, and a HGS server. The HGS server and SQL Server
44
// are the same for testing purposes.
55

6-
if (!extension_loaded("sqlsrv")) {
6+
if (!extension_loaded("pdo_sqlsrv")) {
77
die("skip Extension not loaded");
88
}
99

10-
require_once("MsSetup.inc");
10+
require_once('MsSetup.inc');
1111

12-
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Driver" => $driver);
13-
14-
$conn = sqlsrv_connect( $server, $connectionInfo );
15-
if ($conn === false) {
16-
die( "skip Could not connect during SKIPIF." );
12+
$conn = new PDO("sqlsrv:server = $server", $uid, $pwd);
13+
if (!$conn) {
14+
die("skip Could not connect during SKIPIF.");
1715
}
1816

19-
$msodbcsql_ver = sqlsrv_client_info($conn)["DriverVer"];
20-
$msodbcsql_maj = explode(".", $msodbcsql_ver)[0];
21-
$msodbcsql_min = explode(".", $msodbcsql_ver)[1];
17+
$msodbcsqlVer = $conn->getAttribute(PDO::ATTR_CLIENT_VERSION)['DriverVer'];
18+
$version = explode(".", $msodbcsqlVer);
19+
20+
$msodbcsqlMaj = $version[0];
21+
$msodbcsqlMin = $version[1];
2222

23-
if ($msodbcsql_maj < 17) {
23+
if ($msodbcsqlMaj < 17) {
2424
die("skip Unsupported ODBC driver version");
2525
}
2626

27-
if ($msodbcsql_min < 4 and $msodbcsql_maj == 17) {
27+
if ($msodbcsqlMin < 4 and $msodbcsqlMaj == 17) {
2828
die("skip Unsupported ODBC driver version");
2929
}
3030

3131
// Get SQL Server
32-
$server_info = sqlsrv_server_info($conn);
33-
if (strpos($server_info['SQLServerName'], 'PHPHGS') === false) {
32+
$serverInfo = $conn->getAttribute(PDO::ATTR_SERVER_INFO);
33+
if (strpos($serverInfo['SQLServerName'], 'PHPHGS') === false) {
3434
die("skip Server is not HGS enabled");
3535
}
36-
?>
36+
?>

test/functional/sqlsrv/skipif_not_hgs.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ if (!extension_loaded("sqlsrv")) {
99

1010
require_once("MsSetup.inc");
1111

12-
$connectionInfo = array("UID"=>$userName, "PWD"=>$userPassword, "Driver" => $driver);
12+
$connectionInfo = array("UID"=>$userName, "PWD"=>$userPassword);
1313

14-
$conn = sqlsrv_connect( $server, $connectionInfo );
14+
$conn = sqlsrv_connect($server, $connectionInfo);
1515
if ($conn === false) {
16-
die( "skip Could not connect during SKIPIF." );
16+
die("skip Could not connect during SKIPIF.");
1717
}
1818

1919
$msodbcsql_ver = sqlsrv_client_info($conn)["DriverVer"];

test/functional/sqlsrv/sqlsrv_connect_encrypted.phpt

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,101 @@
11
--TEST--
2-
Test new connection keyword ColumnEncryption
2+
Test new connection keyword ColumnEncryption with different input values
3+
--DESCRIPTION--
4+
Some test cases return errors as expected. For testing purposes, an enclave enabled
5+
SQL Server and the HGS server are the same instance. If the server is HGS enabled,
6+
the error message of one test case is not the same.
37
--SKIPIF--
48
<?php require('skipif.inc'); ?>
59
--FILE--
610
<?php
7-
sqlsrv_configure( 'WarningsReturnAsErrors', 0 );
8-
require( 'MsSetup.inc' );
11+
sqlsrv_configure('WarningsReturnAsErrors', 0);
12+
require('MsSetup.inc');
913

1014
$connectionOptions = array("Database"=>$database,"UID"=>$userName, "PWD"=>$userPassword);
11-
test_ColumnEncryption($server, $connectionOptions);
15+
testColumnEncryption($server, $connectionOptions);
1216
echo "Done";
1317

14-
function test_ColumnEncryption($server ,$connectionOptions){
18+
function testColumnEncryption($server, $connectionOptions)
19+
{
1520
$conn = sqlsrv_connect($server, $connectionOptions);
16-
if ($conn === false)
17-
{
21+
if ($conn === false) {
1822
print_r(sqlsrv_errors());
1923
}
2024
$msodbcsql_ver = sqlsrv_client_info($conn)['DriverVer'];
21-
$msodbcsql_maj = explode(".", $msodbcsql_ver)[0];
25+
$msodbcsqlMaj = explode(".", $msodbcsql_ver)[0];
2226

27+
// Next, check if the server is HGS enabled
28+
$hgsEnabled = true;
29+
$serverInfo = sqlsrv_server_info($conn);
30+
if (strpos($serverInfo['SQLServerName'], 'PHPHGS') === false) {
31+
$hgsEnabled = false;
32+
}
33+
2334
// Only works for ODBC 17
24-
$connectionOptions['ColumnEncryption']='Enabled';
25-
$conn = sqlsrv_connect( $server, $connectionOptions );
26-
if( $conn === false )
27-
{
28-
if($msodbcsql_maj < 17){
35+
$connectionOptions['ColumnEncryption'] = 'Enabled';
36+
$conn = sqlsrv_connect($server, $connectionOptions);
37+
if ($conn === false) {
38+
if ($msodbcsqlMaj < 17) {
2939
$expected = "The Always Encrypted feature requires Microsoft ODBC Driver 17 for SQL Server.";
30-
if( strcasecmp(sqlsrv_errors($conn)[0]['message'], $expected ) != 0 )
31-
{
40+
if (strcasecmp(sqlsrv_errors($conn)[0]['message'], $expected) != 0) {
3241
print_r(sqlsrv_errors());
3342
}
34-
}
35-
else
36-
{
43+
} else {
44+
echo "Test case 1 failed:\n";
3745
print_r(sqlsrv_errors());
3846
}
3947
}
40-
48+
4149
// Works for ODBC 17, ODBC 13
4250
$connectionOptions['ColumnEncryption']='Disabled';
43-
$conn = sqlsrv_connect( $server, $connectionOptions );
44-
if( $conn === false )
45-
{
46-
if($msodbcsql_maj < 13)
47-
{
48-
$expected_substr = "Invalid connection string attribute";
49-
if( strpos(sqlsrv_errors($conn)[0]['message'], $expected_substr ) === false )
50-
{
51+
$conn = sqlsrv_connect($server, $connectionOptions);
52+
if ($conn === false) {
53+
if ($msodbcsqlMaj < 13) {
54+
$expected = "Invalid connection string attribute";
55+
if (strpos(sqlsrv_errors($conn)[0]['message'], $expected) === false) {
5156
print_r(sqlsrv_errors());
5257
}
53-
}
54-
else
55-
{
58+
} else {
59+
echo "Test case 2 failed:\n";
5660
print_r(sqlsrv_errors());
5761
}
58-
}
59-
else
60-
{
62+
} else {
6163
sqlsrv_close($conn);
6264
}
65+
66+
// Should fail for all ODBC drivers - but the error message returned depends on the server
67+
$expected = "Invalid value specified for connection string attribute 'ColumnEncryption'";
68+
if ($hgsEnabled) {
69+
$expected = "Requested attestation protocol is invalid.";
70+
}
6371

64-
// should fail for all ODBC drivers
6572
$connectionOptions['ColumnEncryption']='false';
66-
$conn = sqlsrv_connect( $server, $connectionOptions );
67-
if( $conn === false )
68-
{
69-
$expected_substr = "Invalid value specified for connection string attribute 'ColumnEncryption'";
70-
if( strpos(sqlsrv_errors($conn)[0]['message'], $expected_substr ) === false )
71-
{
73+
$conn = sqlsrv_connect($server, $connectionOptions);
74+
if ($conn === false) {
75+
if (strpos(sqlsrv_errors($conn)[0]['message'], $expected) === false) {
76+
echo "Test case 3 failed:\n";
7277
print_r(sqlsrv_errors());
7378
}
7479
}
7580

76-
// should fail for all ODBC drivers
81+
$expected = "Invalid value type for option ColumnEncryption was specified. String type was expected.";
82+
83+
// should fail for all ODBC drivers with the above error message
7784
$connectionOptions['ColumnEncryption']=true;
78-
$conn = sqlsrv_connect( $server, $connectionOptions );
79-
if( $conn === false )
80-
{
81-
$expected_substr = "Invalid value type for option ColumnEncryption was specified. String type was expected.";
82-
if( strpos(sqlsrv_errors($conn)[0]['message'], $expected_substr ) === false )
83-
{
85+
$conn = sqlsrv_connect($server, $connectionOptions);
86+
if ($conn === false) {
87+
if (strpos(sqlsrv_errors($conn)[0]['message'], $expected) === false) {
88+
echo "Test case 4 failed:\n";
8489
print_r(sqlsrv_errors());
8590
}
8691
}
8792

88-
// should fail for all ODBC drivers
93+
// should fail for all ODBC drivers with the above error message
8994
$connectionOptions['ColumnEncryption']=false;
90-
$conn = sqlsrv_connect( $server, $connectionOptions );
91-
if( $conn === false )
92-
{
93-
$expected_substr = "Invalid value type for option ColumnEncryption was specified. String type was expected.";
94-
if( strpos(sqlsrv_errors($conn)[0]['message'], $expected_substr ) === false )
95-
{
95+
$conn = sqlsrv_connect($server, $connectionOptions);
96+
if ($conn === false) {
97+
if (strpos(sqlsrv_errors($conn)[0]['message'], $expected) === false) {
98+
echo "Test case 5 failed:\n";
9699
print_r(sqlsrv_errors());
97100
}
98101
}

0 commit comments

Comments
 (0)