Skip to content

Commit

Permalink
Merge pull request #793 from yitam/addDriverOption
Browse files Browse the repository at this point in the history
Added the driver option to run functional tests
  • Loading branch information
yitam authored Jun 9, 2018
2 parents 90c6443 + 071e897 commit ea6381c
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 218 deletions.
158 changes: 1 addition & 157 deletions test/functional/pdo_sqlsrv/MsCommon.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@
*/

//
// looks like an additional file (in addition to pdo_test_base.inc) may be needed for these PHPTs
// to be runnable from the MSSQL teams' internal proprietary test running system
//

function IsAEQualified($conn)
{
$msodbcsql_ver = $conn->getAttribute(PDO::ATTR_CLIENT_VERSION)["DriverVer"];
$msodbcsql_maj = explode(".", $msodbcsql_ver)[0];
if ($msodbcsql_maj < 17) {
return false;
}
require 'MsSetup.inc';
if ($daasMode) {
// running against Azure
return true;
}
// if not Azure, check the server version
$server_ver = $conn->getAttribute(PDO::ATTR_SERVER_VERSION);
if (explode('.', $server_ver)[0] < 13)
return false;
return true;
}

// TO BE DELETED
function connect($options=array())
{
Expand All @@ -40,7 +16,7 @@ function connect($options=array())
// simply use $databaseName from MsSetup.inc to facilitate testing in Azure,
// which does not support switching databases
require 'MsSetup.inc';
$conn = new PDO( "sqlsrv:Server=$server;database=$databaseName;ConnectionPooling=false;" , $uid, $pwd, $options);
$conn = new PDO( "sqlsrv:Server=$server;database=$databaseName;Driver=$driver;ConnectionPooling=false;" , $uid, $pwd, $options);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
return $conn;
}
Expand All @@ -58,138 +34,6 @@ function connect($options=array())
}
}


/**
* Connect to the database specified in MsSetup.inc; Column Encryption keywords automatically added when $keystore is not none
* @param string $keywords : string to append to the dsn string in PDO::_construct
* @param array $options : attributes to pass to PDO::_construct
* @param bool $disableCE : flag for disabling column encryption even when keystore is NOT none
* for testing fetching encrypted data when connection column encryption is off
* @return PDO connection object
*/
function ae_connect( $keywords='', $options=array(), $disableCE = false )
{
try
{
// simply use $databaseName from MsSetup.inc to facilitate testing in Azure,
// which does not support switching databases
require 'MsSetup.inc';
$dsn = "sqlsrv:Server=$server;database=$databaseName;ConnectionPooling=false;";
if ( $keystore != "none" && !$disableCE )
{
$dsn .= "ColumnEncryption=Enabled;";
}
if ( $keystore == "ksp" && !$disableCE )
{
require( 'AE_Ksp.inc' );
$ksp_path = getKSPPath();
$dsn .= "CEKeystoreProvider=$ksp_path;CEKeystoreName=$ksp_name;CEKeystoreEncryptKey=$encrypt_key;";
}
if ( $keywords )
{
$dsn .= $keywords;
}
$conn = new PDO( $dsn, $uid, $pwd, $options );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
return $conn;
}
catch( PDOException $e )
{
var_dump( $e );
exit;
}
catch(Exception $e)
{
var_dump( $e );
exit;
}
}


/**
* @return string CEK name depending on the connection keywords
*/
function getCekName()
{
require 'MsSetup.inc';
$cekName = '';
switch ( $keystore ) {
case "none":
$cekName = '';
break;
case "win":
$cekName = 'AEColumnKey';
break;
case "ksp":
$cekName = 'CustomCEK';
break;
case "akv":
$cekName = 'AKVColumnKey';
break;
default:
echo "getCekName: Invalid keystore name.\n";
}
return $cekName;
}


/**
* class for encapsulating column metadata needed for creating a table
*/
class columnMeta {
public $colName;
public $dataType; //a string that includes the size of the type if necessary (e.g., decimal(10,5))
public $encType; //randomized or deterministic; default is deterministic
public $options; //a string that is null by default (e.g. NOT NULL Identity (1,1) )

function __construct( $dataType, $colName = null, $options = null, $encType = "deterministic" )
{
if ( is_null( $colName ))
{
$this->colName = get_default_colname( $dataType );
}
else
{
$this->colName = $colName;
}
$this->dataType = $dataType;
$this->encType = $encType;
$this->options = $options;
}
/**
* @return string column definition for creating a table
*/
function getColDef()
{
require 'MsSetup.inc';
$append = " ";

// an identity column is not encrypted because a select query with identity column as the where clause is often run and the user want to have to bind parameter every time
if ( $keystore != "none" && stripos( $this->options, "identity" ) === false )
{
$cekName = getCekName();
if ( stripos( $this->dataType, "char" ) !== false )
$append .= "COLLATE Latin1_General_BIN2 ";
$append .= sprintf( "ENCRYPTED WITH (ENCRYPTION_TYPE = %s, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256', COLUMN_ENCRYPTION_KEY = $cekName) ", $this->encType );
}
$append .= $this->options;
$colDef = "[" . $this->colName . "] " . $this->dataType . $append;
return $colDef;
}
}


/**
* @return string default column name when a name is not provided in the columnMeta class
*/
function get_default_colname( $dataType )
{
$colName = "c_" . str_replace( ",", "_", str_replace( "(", "_", $dataType ));
$colName = rtrim( $colName, ")" );
return $colName;
}


/**
* Create a table
* @param object $conn : PDO connection object
Expand Down
11 changes: 5 additions & 6 deletions test/functional/pdo_sqlsrv/MsCommon_mid-refactor.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
// to be runnable from the MSSQL teams' internal proprietary test running system
//

const KSP_NAME = 'MyCustomKSPName';
const ENCRYPT_KEY = 'LPKCWVD07N3RG98J0MBLG4H2';
const KSP_TEST_TABLE = 'CustomKSPTestTable';

function isAEQualified($conn)
{
$msodbcsql_ver = $conn->getAttribute(PDO::ATTR_CLIENT_VERSION)["DriverVer"];
Expand Down Expand Up @@ -52,7 +48,7 @@ function connect($keywords = '', $options=array(), $errmode = PDO::ERRMODE_EXCEP
// simply use $databaseName from MsSetup.inc to facilitate testing in Azure,
// which does not support switching databases
require("MsSetup.inc");
$dsn = getDSN($server, $databaseName, $keywords, $disableCE);
$dsn = getDSN($server, $databaseName, $driver, $keywords, $disableCE);
$conn = new PDO($dsn, $uid, $pwd, $options);
if ($errmode == PDO::ERRMODE_EXCEPTION || $errmode == PDO::ERRMODE_WARNING || $errmode == PDO::ERRMODE_SILENT) {
$conn->setAttribute(PDO::ATTR_ERRMODE, $errmode);
Expand All @@ -76,7 +72,7 @@ function connect($keywords = '', $options=array(), $errmode = PDO::ERRMODE_EXCEP
* @param bool $disableCE : flag for disabling column encryption even when keystore is NOT none
* @return string dsn string used for PDO constructor
*/
function getDSN($sqlsrvserver, $database, $keywords = '', $disableCE = false)
function getDSN($sqlsrvserver, $database, $driver, $keywords = '', $disableCE = false)
{
require("MsSetup.inc");
$dsn = "";
Expand All @@ -89,6 +85,9 @@ function getDSN($sqlsrvserver, $database, $keywords = '', $disableCE = false)
if ($database) {
$dsn .= "database=$database;";
}
if ($driver) {
$dsn .= "driver=$driver;";
}
if ($keystore != "none" && !$disableCE) {
$dsn .= "ColumnEncryption=Enabled;";
}
Expand Down
4 changes: 1 addition & 3 deletions test/functional/pdo_sqlsrv/MsSetup.inc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ if (isset($_ENV['MSSQL_SERVER']) || isset($_ENV['MSSQL_USER']) || isset($_ENV['M
$uid = 'TARGET_USERNAME';
$pwd = 'TARGET_PASSWORD';
$databaseName = 'TARGET_DATABASE';
$DriverName = "ODBC Driver 11 for SQL Server";
}

$adServer = 'TARGET_AD_SERVER';
Expand All @@ -27,13 +26,12 @@ $adUser = 'TARGET_AD_USERNAME';
$adPassword = 'TARGET_AD_PASSWORD';

$driverType = true;
$PhpDriver = "ODBC Driver 11 for SQL Server";
$driver = "ODBC Driver 17 for SQL Server";

$tableName = 'pdo_test_table';
$tableIndex = 'php_test_table_idx';
$procName = 'php_test_proc';
$fileName = 'php_test_file.dat';
$dsn = "odbc:Driver={$DriverName};Server=$server";
$connectionOptions = array();
$daasMode = false;
$marsMode = true;
Expand Down
2 changes: 1 addition & 1 deletion test/functional/pdo_sqlsrv/PDO21_Connection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ try {
// Invalid connection attempt => errors are expected
$serverName="InvalidServerName";

$dsn = getDSN($serverName, $databaseName);
$dsn = getDSN($serverName, $databaseName, $driver);
$conn1 = new PDO($dsn, $uid, $pwd, $connectionOptions);
if ($conn1) {
printf("Invalid connection attempt should have failed.\n");
Expand Down
2 changes: 2 additions & 0 deletions test/functional/pdo_sqlsrv/pdo_020_bind_params_array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ try {

// Create table
$tableName = 'bindParams';
dropTable($conn, $tableName);

$sql = "CREATE TABLE $tableName (ID TINYINT, SID CHAR(5))";
$stmt = $conn->exec($sql);

Expand Down
2 changes: 2 additions & 0 deletions test/functional/pdo_sqlsrv/pdo_040_error_information.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ try {

// Create table
$tableName = 'pdo_040test';
dropTable($conn, $tableName);

// common function insertRow() is not used here since the test deliberately
// executes an invalid insertion statement
// thus it's not necessary to create an encrypted column for testing column encryption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require_once("MsCommon_mid-refactor.inc");
try {
echo "Testing a connection with ATTR_PERSISTENT...\n";
// setting PDO::ATTR_PERSISTENT in PDO constructor returns an exception
$dsn = getDSN($server, $databaseName);
$dsn = getDSN($server, $databaseName, $driver);
$attr = array(PDO::ATTR_PERSISTENT => true);
$conn = new PDO($dsn, $uid, $pwd, $attr);
//free the connection
Expand Down
2 changes: 1 addition & 1 deletion test/functional/pdo_sqlsrv/pdo_065_construct_prefetch.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require_once("MsSetup.inc");
require_once("MsCommon_mid-refactor.inc");
try {
echo "Testing a connection with ATTR_PREFETCH before ERRMODE_EXCEPTION...\n";
$dsn = getDSN($server, $databaseName);
$dsn = getDSN($server, $databaseName, $driver);

$attr = array(PDO::ATTR_PREFETCH => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$conn = new PDO($dsn, $uid, $pwd, $attr);
Expand Down
1 change: 1 addition & 0 deletions test/functional/pdo_sqlsrv/pdo_ae_output_param_all.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ $colMetaArr = array("c1_int" => "int",
createTable($conn, $tbname, $colMetaArr);
// Create a Store Procedure
$spname = 'selectAllColumns';
dropProc($conn, $spname);
$spSql = "CREATE PROCEDURE $spname (
@c1_int int OUTPUT, @c2_smallint smallint OUTPUT,
@c3_tinyint tinyint OUTPUT, @c4_bit bit OUTPUT,
Expand Down
3 changes: 2 additions & 1 deletion test/functional/pdo_sqlsrv/pdo_utf8_conn.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ UTF-8 connection strings
<?php require('skipif_mid-refactor.inc'); ?>
--FILE--
<?php
require_once("MsSetup.inc");
require_once("MsCommon_mid-refactor.inc");

$server = 'localhost';
$databaseName = 'test';
$uid = 'sa';
$pwd = 'Sunshine4u';

$dsn = getDSN($server, $databaseName);
$dsn = getDSN($server, $databaseName, $driver);
// test an invalid connection credentials
$c = new PDO($dsn, $uid, $pwd);

Expand Down
5 changes: 5 additions & 0 deletions test/functional/pdo_sqlsrv/skipif_not_akv.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ if (!extension_loaded("pdo") || !extension_loaded('pdo_sqlsrv')) {
}

require_once("MsSetup.inc");
if ($driver != "ODBC Driver 17 for SQL Server") {
// the testing is not set to use ODBC 17
die("skip - AE feature not supported in the current environment.");
}

require_once("MsCommon_mid-refactor.inc");

$dsn = getDSN($server, null);
Expand Down
Loading

0 comments on commit ea6381c

Please sign in to comment.