added tests from testing ae types conversion for insertion in SQLSRV

This commit is contained in:
v-kaywon 2018-03-01 17:37:04 -08:00
parent 5eaaa78292
commit 239d71569c
8 changed files with 1533 additions and 2 deletions

View file

@ -157,13 +157,16 @@ class BindParamOption
$type_size = explode("(", $this->sqlType);
$type = $type_size[0];
if (count($type_size) > 1) {
$size = $type_size[1];
$size = rtrim($type_size[1], ")");
$prec_scal = explode(",", $size);
if (count($prec_scal) > 1) {
$prec = $prec_scal[0];
$scal = rtrim($prec_scal[1], ")");
$scal = $prec_scal[1];
$size = null;
}
if (strpos($size, "max") !== false) {
$size = trim($size, "'");
}
}
// get the sqlType constant
try {

View file

@ -0,0 +1,271 @@
--TEST--
Test for inserting encrypted data into binary types columns with different sizes
--DESCRIPTION--
Test implicit conversions between different binary types of different sizes
With Always Encrypted, implicit conversion works if:
1. From input of SQLSRV_SQLTYPE_BINARY(n) to a larger binary(m) column where n <= m
2. From input of SQLSRV_SQLTYPE_BINARY(n) to a larger varbinary(m) column where n <= m (m can be max)
3. From input of SQLSRV_SQLTYPE_VARBINARY(n) to a larger binary(m) column where n <= m
4. From input of SQLSRV_SQLTYPE_VARBINARY(n) to a larger varbinary(m) column where n <= m (m can be max)
Without AlwaysEncrypted, implicit conversion between different binary types and sizes works
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');
$dataTypes = array("binary", "varbinary", "varbinary(max)");
$lengths = array(1, 8, 64, 512, 4000);
$sqlTypes = array("SQLSRV_SQLTYPE_BINARY", "SQLSRV_SQLTYPE_VARBINARY", "SQLSRV_SQLTYPE_VARBINARY('max')");
$sqltypeLengths = $lengths;
$inputValues = array("d", "f");
$conn = AE\connect();
foreach($dataTypes as $dataType) {
$maxcol = strpos($dataType, "(max)");
foreach ($lengths as $m) {
if ($maxcol !== false) {
$typeFull = $dataType;
} else {
$typeFull = "$dataType($m)";
}
echo "\nTesting $typeFull:\n";
// create table containing binary(m) or varbinary(m) columns
$tbname = "test_" . str_replace(array('(', ')'), '', $dataType) . $m;
$colMetaArr = array(new AE\ColumnMeta($typeFull, "c_det"), new AE\ColumnMeta($typeFull, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr);
// insert by specifying SQLSRV_SQLTYPE_BINARY(n) or SQLSRV_SQLTYPE_VARBINARY(n)
// with AE, should be successful as long as the SQLSRV_SQLTYPE length (n) is smaller than the column length (m)
foreach($sqlTypes as $sqlType) {
$maxsqltype = strpos($sqlType, "max");
foreach($sqltypeLengths as $n) {
if ($maxsqltype !== false) {
$sqltypeFull = $sqlType;
} else {
$sqltypeFull = "$sqlType($n)";
}
//insert a row
$inputs = array(new AE\BindParamOption($inputValues[0], null, "SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)", $sqltypeFull),
new AE\BindParamOption($inputValues[1], null, "SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_BINARY)", $sqltypeFull));
$r;
$stmt = AE\insertRow($conn, $tbname, array("c_det" => $inputs[0], "c_rand" => $inputs[1]), $r, AE\INSERT_PREPARE_PARAMS);
// check the case when SQLSRV_SQLTYPE length (n) is greater than the column length (m)
// with AE: should not work
// without AE: should work
if (($n > $m || $maxsqltype) && !$maxcol) {
if (AE\isColEncrypted()) {
if ($r !== false) {
echo "AE: Conversion from $sqltypeFull to $typeFull should not be supported\n";
} else {
if (sqlsrv_errors()[0]['SQLSTATE'] != "22018") {
echo "AE: Conversion from $sqltypeFull to $typeFull expects an operand type clash error, actual error is incorrect\n";
var_dump(sqlsrv_errors());
}
}
} else {
if ($r === false) {
echo "Conversion from $sqltypeFull to $typeFull should be supported\n";
}
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (trim($row['c_det']) != $inputValues[0] || trim($row['c_rand']) != $inputValues[1]) {
echo "Conversion from $sqltypeFull to $typeFull causes data corruption\n";
}
}
// check the case when SQLSRV_SQLTYPE length (n) is less than or equal to the column length (m)
// should work with AE or non AE
} else {
if ($r === false) {
echo "Conversion from $sqltypeFull to $typeFull should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (trim($row['c_det']) == $inputValues[0] || trim($row['c_rand']) == $inputValues[1]) {
echo "****Conversion from $sqltypeFull to $typeFull is supported****\n";
} else {
echo "Conversion from $sqltypeFull to $typeFull causes data corruption\n";
}
}
}
// cleanup
sqlsrv_free_stmt($stmt);
sqlsrv_query($conn, "TRUNCATE TABLE $tbname");
}
}
dropTable($conn, $tbname);
}
}
sqlsrv_close($conn);
?>
--EXPECT--
Testing binary(1):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to binary(1) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to binary(1) is supported****
Testing binary(8):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to binary(8) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to binary(8) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to binary(8) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to binary(8) is supported****
Testing binary(64):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to binary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to binary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to binary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to binary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to binary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to binary(64) is supported****
Testing binary(512):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to binary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to binary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to binary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to binary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to binary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to binary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to binary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to binary(512) is supported****
Testing binary(4000):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(4000) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to binary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(4000) to binary(4000) is supported****
Testing varbinary(1):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(1) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(1) is supported****
Testing varbinary(8):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(8) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(8) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(8) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(8) is supported****
Testing varbinary(64):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to varbinary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to varbinary(64) is supported****
Testing varbinary(512):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to varbinary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to varbinary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to varbinary(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to varbinary(512) is supported****
Testing varbinary(4000):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(4000) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to varbinary(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(4000) to varbinary(4000) is supported****
Testing varbinary(max):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
Testing varbinary(max):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
Testing varbinary(max):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
Testing varbinary(max):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
Testing varbinary(max):
****Conversion from SQLSRV_SQLTYPE_BINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_BINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(1) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(8) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(64) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(512) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY(4000) to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARBINARY('max') to varbinary(max) is supported****

View file

@ -0,0 +1,333 @@
--TEST--
Test for inserting encrypted data of char types with different sizes
--DESCRIPTION--
Test implicit conversions between different char types of different sizes
With Always Encrypted, implicit conversion works if:
1. From input of SQLSRV_SQLTYPE_CHAR(n) to a larger char(m) column where n <= m
2. From input of SQLSRV_SQLTYPE_CHAR(n) to a larger varchar(m) column where n <= m (m can be max)
3. From input of SQLSRV_SQLTYPE_VARCHAR(n) to a larger char(m) column where n <= m
4. From input of SQLSRV_SQLTYPE_VARCHAR(n) to a larger varchar(m) column where n <= m (m can be max)
Without AlwaysEncrypted, implicit conversion between different binary types and sizes works
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');
$dataTypes = array("char", "varchar", "varchar(max)");
$lengths = array(1, 8, 64, 512, 4096, 8000);
$sqlTypes = array("SQLSRV_SQLTYPE_CHAR", "SQLSRV_SQLTYPE_VARCHAR", "SQLSRV_SQLTYPE_VARCHAR('max')");
$sqltypeLengths = $lengths;
$inputValue = "d";
$conn = AE\connect();
foreach($dataTypes as $dataType) {
$maxcol = strpos($dataType, "(max)");
foreach($lengths as $m) {
if ($maxcol !== false) {
$typeFull = $dataType;
} else {
$typeFull = "$dataType($m)";
}
echo "\nTesting $typeFull:\n";
// create table containing char(m) or varchar(m) columns
$tbname = "test_" . str_replace(array('(', ')'), '', $dataType) . $m;
$colMetaArr = array(new AE\ColumnMeta($typeFull, "c1"));
AE\createTable($conn, $tbname, $colMetaArr);
// insert by specifying SQLSRV_SQLTYPE_CHAR(n) or SQLSRV_SQLTYPE_VARCHAR(n)
// with AE, should be successful as long as the SQLSRV_SQLTYPE length (n) is smaller than the column length (m)
foreach($sqlTypes as $sqlType) {
$maxsqltype = strpos($sqlType, "max");
foreach($sqltypeLengths as $n) {
if ($maxsqltype !== false) {
$sqltypeFull = $sqlType;
} else {
$sqltypeFull = "$sqlType($n)";
}
//insert a row
$input = new AE\BindParamOption($inputValue, null, null, $sqltypeFull);
$r;
$stmt = AE\insertRow($conn, $tbname, array("c1" => $input), $r, AE\INSERT_PREPARE_PARAMS);
// check the case when SQLSRV_SQLTYPE length (n) is greater than the column length (m)
// with AE: should not work
// without AE: should work
if (($n > $m || $maxsqltype) && !$maxcol) {
if (AE\isColEncrypted()) {
if ($r !== false) {
echo "AE: Conversion from $sqltypeFull to $typeFull should not be supported\n";
} else {
if (sqlsrv_errors()[0]['SQLSTATE'] != "22018") {
echo "AE: Conversion from $sqltypeFull to $typeFull expects an operand type clash error, actual error is incorrect\n";
var_dump(sqlsrv_errors());
}
}
} else {
if ($r === false) {
echo "Conversion from $sqltypeFull to $typeFull should be supported\n";
}
$sql = "SELECT c1 FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (trim($row['c1']) != $inputValue) {
echo "Conversion from $sqltypeFull to $typeFull causes data corruption\n";
}
}
// check the case when SQLSRV_SQLTYPE length (n) is less than or equal to the column length (m)
// should work with AE or non AE
} else {
if ($r === false) {
echo "Conversion from $sqltypeFull to $typeFull should be supported\n";
} else {
$sql = "SELECT c1 FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (trim($row['c1']) == $inputValue) {
echo "****Conversion from $sqltypeFull to $typeFull is supported****\n";
} else {
echo "Conversion from $sqltypeFull to $typeFull causes data corruption\n";
}
}
}
// cleanup
sqlsrv_free_stmt($stmt);
sqlsrv_query($conn, "TRUNCATE TABLE $tbname");
}
}
dropTable($conn, $tbname);
}
}
sqlsrv_close($conn);
?>
--EXPECT--
Testing char(1):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to char(1) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to char(1) is supported****
Testing char(8):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to char(8) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to char(8) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to char(8) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to char(8) is supported****
Testing char(64):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to char(64) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to char(64) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to char(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to char(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to char(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to char(64) is supported****
Testing char(512):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to char(512) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to char(512) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to char(512) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to char(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to char(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to char(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to char(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to char(512) is supported****
Testing char(4096):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to char(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to char(4096) is supported****
Testing char(8000):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8000) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to char(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8000) to char(8000) is supported****
Testing varchar(1):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(1) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(1) is supported****
Testing varchar(8):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(8) is supported****
Testing varchar(64):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(64) is supported****
Testing varchar(512):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(512) is supported****
Testing varchar(4096):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(4096) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to varchar(4096) is supported****
Testing varchar(8000):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8000) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to varchar(8000) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8000) to varchar(8000) is supported****
Testing varchar(max):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
Testing varchar(max):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
Testing varchar(max):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
Testing varchar(max):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
Testing varchar(max):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
Testing varchar(max):
****Conversion from SQLSRV_SQLTYPE_CHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_CHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(1) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(64) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(512) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(4096) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR(8000) to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_VARCHAR('max') to varchar(max) is supported****

View file

@ -0,0 +1,180 @@
--TEST--
Test for inserting encrypted data of datetime2, datetimeoffset and time datatypes with different precisions
--DESCRIPTION--
Test implicit conversions between different precisions
With Always Encrypted, implicit conversion works if:
1. From input of SQLSRV_SQLTYPE_DATETIME2 to a dateteim2(7) column
2. From input of SQLSRV_SQLTYPE_DATETIMEOFFSET to a datetimeoffset(7) column
3. From input of SQLSRV_SQLTYPE_TIME to a time(7) column
Note: with AE, implicit converion should work as long as the SQLSRV_SQLTYPE has a smaller precision than the one defined in the column. However, the SQLSRV driver does not let the user specify the precision in these SQLSRV_SQLTYPE_* constants and they are all default to a precision of 7. Hence when user specifies SQLSRV_SQLTYPE_DATETIME2, SQLSRV_SQLTYPE_DATETIMEOFFSET or SQLSRV_SQLTYPE_TIME when binding parameter during insertion, only insertion into a column of precision 7 is allowed.
Without AlwaysEncrypted, implicit conversion between different precisions works
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');
function compareDate($dtobj, $dtstr, $dataType, $precision) {
$dtobj_date = $dtobj->format("Y-m-d H:i:s.u");
$dtobj_timezone = $dtobj->getTimezone()->getName();
$dtarr = null;
if ($dataType == "datetimeoffset") {
$dtarr = explode(' ', $dtstr);
}
// php only supports up to 6 decimal places in datetime
// drop the last decimal place before comparing
if ($precision == 7) {
$dtstr = substr($dtstr, 0, -1);
if (!is_null($dtarr)) {
$dtarr[1] = substr($dtarr[1], 0, -1);
}
}
if (strpos($dtobj_date, $dtstr) !== false) {
return true;
}
if ($dataType == "datetimeoffset") {
if (strpos($dtobj_date, $dtarr[0]) !== false && strpos($dtobj_date, $dtarr[1]) !== false && strpos($dtobj_timezone, $dtarr[2]) !== false) {
return true;
}
}
return false;
}
$dataTypes = array("datetime2", "datetimeoffset", "time");
$precisions = array(0, 1, 2, 4, 7);
$inputValuesInit = array("datetime2" => array("0001-01-01 00:00:00", "9999-12-31 23:59:59"),
"datetimeoffset" => array("0001-01-01 00:00:00 -14:00", "9999-12-31 23:59:59 +14:00"),
"time" => array("00:00:00", "23:59:59"));
$conn = AE\connect();
foreach($dataTypes as $dataType) {
foreach($precisions as $m) {
// add $m number of decimal digits to the some input values
$inputValues[0] = $inputValuesInit[$dataType][0];
$inputValues[1] = $inputValuesInit[$dataType][1];
if ($m != 0) {
if ($dataType == "datetime2") {
$inputValues[1] .= "." . str_repeat("9", $m);
} else if ($dataType == "datetimeoffset") {
$dtoffsetPieces = explode(" ", $inputValues[1]);
$inputValues[1] = $dtoffsetPieces[0] . " " . $dtoffsetPieces[1] . "." . str_repeat("9", $m) . " " . $dtoffsetPieces[2];
} else if ($dataType == "time") {
$inputValues[0] .= "." . str_repeat("0", $m);
$inputValues[1] .= "." . str_repeat("9", $m);
}
}
$typeFull = "$dataType($m)";
echo "\nTesting $typeFull:\n";
// create table containing datetime2(m), datetimeoffset(m) or time(m) columns
$tbname = "test_" . $dataType . $m;
$colMetaArr = array(new AE\ColumnMeta($typeFull, "c_det"), new AE\ColumnMeta($typeFull, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr);
// insert by specifying the corresponding SQLSRV_SQLTYPE
$sqlType = "SQLSRV_SQLTYPE_" . strtoupper($dataType);
$inputs = array(new AE\BindParamOption($inputValues[0], null, null, $sqlType),
new AE\BindParamOption($inputValues[1], null, null, $sqlType));
$r;
$stmt = AE\insertRow($conn, $tbname, array("c_det" => $inputs[0], "c_rand" => $inputs[1]), $r, AE\INSERT_PREPARE_PARAMS);
// check the case when the column precision (m) is less than 7
// with AE: should not work
// with AE: should work
if ($m < 7) {
if (AE\isColEncrypted()) {
if ($r !== false) {
echo "AE: Conversion from $sqlType to $typeFull should not be supported\n";
} else {
if (sqlsrv_errors()[0]['SQLSTATE'] != "22018") {
echo "AE: Conversion from $sqlType to $typeFull expects an operand type clash error, actual error is incorrect\n";
var_dump(sqlsrv_errors());
} else {
echo "Test successfully done\n";
}
}
} else {
if ($r === false) {
echo "Conversion from $sqlType to $typeFull should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (!compareDate($row['c_det'], $inputValues[0], $dataType, $m) || !compareDate($row['c_rand'], $inputValues[1], $dataType, $m)) {
echo "Conversion from $sqlType to $typeFull causes data corruption\n";
} else {
echo "Test successfully done\n";
}
}
}
// check the case when the column precision is 7
// should work with AE or non AE
} else {
if ($r === false) {
echo "Conversion from $sqlType to $typeFull should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (compareDate($row['c_det'], $inputValues[0], $dataType, $m) && compareDate($row['c_rand'], $inputValues[1], $dataType, $m)) {
echo "****Conversion from $sqlType to $typeFull is supported****\n";
} else {
echo "Conversion from $sqlType to $typeFull causes data corruption\n";
}
}
}
}
// cleanup
sqlsrv_free_stmt($stmt);
dropTable($conn, $tbname);
}
sqlsrv_close($conn);
?>
--EXPECT--
Testing datetime2(0):
Test successfully done
Testing datetime2(1):
Test successfully done
Testing datetime2(2):
Test successfully done
Testing datetime2(4):
Test successfully done
Testing datetime2(7):
****Conversion from SQLSRV_SQLTYPE_DATETIME2 to datetime2(7) is supported****
Testing datetimeoffset(0):
Test successfully done
Testing datetimeoffset(1):
Test successfully done
Testing datetimeoffset(2):
Test successfully done
Testing datetimeoffset(4):
Test successfully done
Testing datetimeoffset(7):
****Conversion from SQLSRV_SQLTYPE_DATETIMEOFFSET to datetimeoffset(7) is supported****
Testing time(0):
Test successfully done
Testing time(1):
Test successfully done
Testing time(2):
Test successfully done
Testing time(4):
Test successfully done
Testing time(7):
****Conversion from SQLSRV_SQLTYPE_TIME to time(7) is supported****

View file

@ -0,0 +1,247 @@
--TEST--
Test for inserting encrypted data of decimal types with different precisions and scales
--DESCRIPTION--
Test implicit conversions between different precisions and scales
With Always Encrypted, no implicit conversion works for decimal datatypes, the precision and scale specified in the SQLSRV_SQLTYPE must be identical to the precision and scale defined in the column
Without AlwaysEncrypted, implicit conversion between precisions or scales works if:
1. From input of SQLSRV_SQLTYPE_DECIMAL(n1, n2) to a decimal(m1, m2) column where n1 - n2 > m1 - m2 and
2. where n2 != 0 && m1 != m2
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');
$dataTypes = array("decimal", "numeric");
$precisions = array(1 => array(0, 1),
4 => array(0, 1, 4),
16 => array(0, 1, 4, 16),
38 => array(0, 1, 4, 16, 38));
$sqlTypes = array("SQLSRV_SQLTYPE_DECIMAL", "SQLSRV_SQLTYPE_NUMERIC");
$sqltypePrecisions = $precisions;
$inputValuesInit = array(92233720368547758089223372036854775808, -92233720368547758089223372036854775808);
$maxInPrecision = 38;
$conn = AE\connect();
foreach($dataTypes as $dataType) {
foreach($precisions as $m1 => $inScales) {
foreach($inScales as $m2) {
// change the number of integers in the input values to be $m1 - $m2
$precDiff = $maxInPrecision - ($m1 - $m2);
$inputValues = $inputValuesInit;
foreach ($inputValues as &$inputValue) {
$inputValue = $inputValue / pow(10, $precDiff);
}
$typeFull = "$dataType($m1, $m2)";
echo "\nTesting $typeFull:\n";
// create table containing decimal(m1, m2) or numeric(m1, m2) columns
$tbname = "test_" . $dataType . $m1 . $m2;
$colMetaArr = array(new AE\ColumnMeta($typeFull, "c_det"), new AE\ColumnMeta($typeFull, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr);
// insert by specifying SQLSRV_SQLTYPE_DECIMAL(n1, n2) or SQLSRV_SQLTYPE_NUMERIC(n1, n2)
// with AE, should only be successful if the SQLSRV_SQLTYPE precision (n1) and scale (n2) are the same as the column precision (m1) and scale (m2)
foreach($sqlTypes as $sqlType) {
foreach($sqltypePrecisions as $n1 => $sqltypeScales) {
foreach($sqltypeScales as $n2) {
// compute the epsilon for comparing doubles
// float in PHP only has a precision of roughtly 14 digits: http://php.net/manual/en/language.types.float.php
// the smaller precision and scale (n1 and n2 vs m1 and m2) take precedence
$epsilon;
$smallerprec = min($m1, $n1);
$smallerscale = min($m2, $n2);
if ($smallerprec < 14) {
$epsilon = pow(10, $smallerscale * -1);
} else {
$numint = $smallerprec - $smallerscale;
if ($numint < 14) {
$epsilon = pow(10, (14 - $numint) * -1);
} else {
$epsilon = pow(10, $numint - 14);
}
}
$sqltypeFull = "$sqlType($n1, $n2)";
//insert a row
$inputs = array(new AE\BindParamOption((string)$inputValues[0], null, null, $sqltypeFull),
new AE\BindParamOption((string)$inputValues[1], null, null, $sqltypeFull));
$r;
$stmt = AE\insertRow($conn, $tbname, array("c_det" => $inputs[0], "c_rand" => $inputs[1]), $r, AE\INSERT_PREPARE_PARAMS);
// check the case when the SQLSRV_SQLTYPE precision (n1) is not the same as the column precision (m1)
// or the SQLSRV_SQLTYPE scale (n2) is not the same as the column precision (m2)
// with AE: should not work
// without AE: should not work if n1 - n2 < m1 - m2 (Numeric value out of range error)
// or n2 != 0 && $m1 == $m2 (Arithmetic overflow error)
if ($n1 != $m1 || $n2 != $m2) {
if (AE\isColEncrypted()) {
if ($r !== false) {
echo "AE: Conversion from $sqltypeFull to $typeFull should not be supported\n";
} else {
if (sqlsrv_errors()[0]['SQLSTATE'] != "22018") {
echo "AE: Conversion from $sqltypeFull to $typeFull expects an operand type clash error, actual error is incorrect\n";
var_dump(sqlsrv_errors());
}
}
} else {
if ($n1 - $n2 < $m1 - $m2 || ($m1 == $m2 && $n2 == 0)) {
if ($r !== false) {
echo "Conversion from $sqltypeFull to $typeFull should not be supported\n";
}
} else {
if ($r === false) {
echo "Conversion from $sqltypeFull to $typeFull should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (abs($row['c_det'] - $inputValues[0]) > $epsilon || abs($row['c_rand'] - $inputValues[1]) > $epsilon) {
echo "Conversion from $sqltypeFull to $typeFull causes data corruption\n";
}
}
}
}
// check the case when the SQLSRV_SQLTYPE precision (n1) and scale (n2) are the same as the column precision (m1) and scale (m2)
// should work with AE or non AE
} else {
if ($r === false) {
echo "Conversion from $sqltypeFull to $typeFull should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (abs($row['c_det'] - $inputValues[0]) < $epsilon && abs($row['c_rand'] - $inputValues[1]) < $epsilon) {
echo "****Conversion from $sqltypeFull to $typeFull is supported****\n";
} else {
echo "Conversion from $sqltypeFull to $typeFull causes data corruption\n";
}
}
}
// cleanup
sqlsrv_free_stmt($stmt);
sqlsrv_query($conn, "TRUNCATE TABLE $tbname");
}
}
}
dropTable($conn, $tbname);
}
}
}
sqlsrv_close($conn);
?>
--EXPECT--
Testing decimal(1, 0):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(1, 0) to decimal(1, 0) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(1, 0) to decimal(1, 0) is supported****
Testing decimal(1, 1):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(1, 1) to decimal(1, 1) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(1, 1) to decimal(1, 1) is supported****
Testing decimal(4, 0):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(4, 0) to decimal(4, 0) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(4, 0) to decimal(4, 0) is supported****
Testing decimal(4, 1):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(4, 1) to decimal(4, 1) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(4, 1) to decimal(4, 1) is supported****
Testing decimal(4, 4):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(4, 4) to decimal(4, 4) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(4, 4) to decimal(4, 4) is supported****
Testing decimal(16, 0):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(16, 0) to decimal(16, 0) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(16, 0) to decimal(16, 0) is supported****
Testing decimal(16, 1):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(16, 1) to decimal(16, 1) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(16, 1) to decimal(16, 1) is supported****
Testing decimal(16, 4):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(16, 4) to decimal(16, 4) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(16, 4) to decimal(16, 4) is supported****
Testing decimal(16, 16):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(16, 16) to decimal(16, 16) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(16, 16) to decimal(16, 16) is supported****
Testing decimal(38, 0):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 0) to decimal(38, 0) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 0) to decimal(38, 0) is supported****
Testing decimal(38, 1):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 1) to decimal(38, 1) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 1) to decimal(38, 1) is supported****
Testing decimal(38, 4):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 4) to decimal(38, 4) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 4) to decimal(38, 4) is supported****
Testing decimal(38, 16):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 16) to decimal(38, 16) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 16) to decimal(38, 16) is supported****
Testing decimal(38, 38):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 38) to decimal(38, 38) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 38) to decimal(38, 38) is supported****
Testing numeric(1, 0):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(1, 0) to numeric(1, 0) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(1, 0) to numeric(1, 0) is supported****
Testing numeric(1, 1):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(1, 1) to numeric(1, 1) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(1, 1) to numeric(1, 1) is supported****
Testing numeric(4, 0):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(4, 0) to numeric(4, 0) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(4, 0) to numeric(4, 0) is supported****
Testing numeric(4, 1):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(4, 1) to numeric(4, 1) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(4, 1) to numeric(4, 1) is supported****
Testing numeric(4, 4):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(4, 4) to numeric(4, 4) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(4, 4) to numeric(4, 4) is supported****
Testing numeric(16, 0):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(16, 0) to numeric(16, 0) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(16, 0) to numeric(16, 0) is supported****
Testing numeric(16, 1):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(16, 1) to numeric(16, 1) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(16, 1) to numeric(16, 1) is supported****
Testing numeric(16, 4):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(16, 4) to numeric(16, 4) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(16, 4) to numeric(16, 4) is supported****
Testing numeric(16, 16):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(16, 16) to numeric(16, 16) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(16, 16) to numeric(16, 16) is supported****
Testing numeric(38, 0):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 0) to numeric(38, 0) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 0) to numeric(38, 0) is supported****
Testing numeric(38, 1):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 1) to numeric(38, 1) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 1) to numeric(38, 1) is supported****
Testing numeric(38, 4):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 4) to numeric(38, 4) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 4) to numeric(38, 4) is supported****
Testing numeric(38, 16):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 16) to numeric(38, 16) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 16) to numeric(38, 16) is supported****
Testing numeric(38, 38):
****Conversion from SQLSRV_SQLTYPE_DECIMAL(38, 38) to numeric(38, 38) is supported****
****Conversion from SQLSRV_SQLTYPE_NUMERIC(38, 38) to numeric(38, 38) is supported****

View file

@ -0,0 +1,103 @@
--TEST--
Test for inserting encrypted data of float types with different number of bits
--DESCRIPTION--
Test implicit conversions between different number of bits
With Always Encrypted, implicit conversion works if:
1. From input of SQLSRV_SQLTYPE_FLOAT to a float(m) column where m > 24
Note: with AE, implicit conversion should work as long as the SQLSRV_SQLTYPE has a smaller number of bits than the one defined in the column. However, the SQLSRV driver does not let the user specify the number of bits in the SQLSRV_SQLTYPE_FLOAT constant and it is default to 53. Hence when user specifies SQLSRV_SQLTYPE_FLOAT when binding parameter during insertion, only insertion into a column of > 24 is allowed.
Without AlwaysEncrypted, inplicit conversion between different number of bits works.
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');
$dataType = "float";
$bits = array(1, 12, 24, 36, 53);
$sqlType = "SQLSRV_SQLTYPE_FLOAT";
$inputValues = array(9223372036854775808.9223372036854775808, -9223372036854775808.9223372036854775808);
$epsilon = 100000;
$conn = AE\connect();
foreach($bits as $m) {
$typeFull = "$dataType($m)";
echo "\nTesting $typeFull:\n";
// create table containing float(m) columns
$tbname = "test_" . $dataType . $m;
$colMetaArr = array(new AE\ColumnMeta($typeFull, "c_det"), new AE\ColumnMeta($typeFull, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr);
// insert by specifying SQLSRV_SQLTYPE_FLOAT
$inputs = array(new AE\BindParamOption($inputValues[0], null, null, $sqlType),
new AE\BindParamOption($inputValues[1], null, null, $sqlType));
$r;
$stmt = AE\insertRow($conn, $tbname, array("c_det" => $inputs[0], "c_rand" => $inputs[1]), $r, AE\INSERT_PREPARE_PARAMS);
// check the case when the column number of bits is less than 25
// with AE: should not work
// with AE: should work
if ($m < 25) {
if (AE\isColEncrypted()) {
if ($r !== false) {
echo "AE: Conversion from $sqlType to $typeFull should not be supported\n";
} else {
if (sqlsrv_errors()[0]['SQLSTATE'] != "22018") {
echo "AE: Conversion from $sqlType to $typeFull expects an operand type clash error, actual error is incorrect\n";
var_dump(sqlsrv_errors());
} else {
echo "Test successfully done\n";
}
}
} else {
if ($r === false) {
echo "Conversion from $sqlType to $typeFull should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (abs($row['c_det'] - $inputValues[0]) > $epsilon || abs($row['c_rand'] - $inputValues[1]) > $epsilon) {
echo "Conversion from $sqlType to $typeFull causes data corruption\n";
} else {
echo "Test successfully done\n";
}
}
}
// check the case when the column number of bits 25 or more
// should work with AE or non AE
} else {
if ($r === false) {
echo "Conversion from $sqlType to $typeFull should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (abs($row['c_det'] - $inputValues[0]) < $epsilon && abs($row['c_rand'] - $inputValues[1]) < $epsilon) {
echo "****Conversion from $sqlType to $typeFull is supported****\n";
} else {
echo "Conversion from $sqlType to $typeFull causes data corruption\n";
}
}
}
// cleanup
sqlsrv_free_stmt($stmt);
dropTable($conn, $tbname);
}
sqlsrv_close($conn);
?>
--EXPECT--
Testing float(1):
Test successfully done
Testing float(12):
Test successfully done
Testing float(24):
Test successfully done
Testing float(36):
Conversion from SQLSRV_SQLTYPE_FLOAT to float(36) should be supported
Testing float(53):
Conversion from SQLSRV_SQLTYPE_FLOAT to float(53) should be supported

View file

@ -0,0 +1,124 @@
--TEST--
Test for inserting encrypted data of int types
--DESCRIPTION--
Test implicit conversions between different integer types
With Always Encrypted, implicit conversion works if:
1. From input SQLSRV_SQLTYPE_BIT to a bit column
2. From input SQLSRV_SQLTYPE_BIT to a tinyint column
3. From input SQLSRV_SQLTYPE_BIT to a smallint column
4. From input SQLSRV_SQLTYPE_BIT to an int column
5. From input SQLSRV_SQLTYPE_BIT to a bigint column
6. From input SQLSRV_SQLTYPE_TINYINT to a tinyint column
7. From input SQLSRV_SQLTYPE_TINYINT to a smallint column
8. From input SQLSRV_SQLTYPE_TINYINT to an int column
9. From input SQLSRV_SQLTYPE_TINYINT to a bigint column
10. From input SQLSRV_SQLTYPE_SMALLINT to a smallint column
11. From input SQLSRV_SQLTYPE_SMALLINT to an int column
12. From input SQLSRV_SQLTYPE_SMALLINT to a bigint column
13. From input SQLSRV_SQLTYPE_INT to an int column
14. From input SQLSRV_SQLTYPE_INT to a bigint column
15. From input SQLSRV_SQLTYPE_BIGINT to a bigint column
Without AlwaysEncrypted, inplicit conversion between different integer types works
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');
$dataTypes = array("bit", "tinyint", "smallint", "int", "bigint");
$sqlTypes = array("SQLSRV_SQLTYPE_BIT", "SQLSRV_SQLTYPE_TINYINT", "SQLSRV_SQLTYPE_SMALLINT", "SQLSRV_SQLTYPE_INT", "SQLSRV_SQLTYPE_BIGINT");
$inputValues = array(1, 0);
// this is a list of implicit datatype conversion that AE supports
$aeConvList = array("bit" => array("SQLSRV_SQLTYPE_BIT"),
"tinyint" => array("SQLSRV_SQLTYPE_BIT", "SQLSRV_SQLTYPE_TINYINT"),
"smallint" => array("SQLSRV_SQLTYPE_BIT", "SQLSRV_SQLTYPE_TINYINT", "SQLSRV_SQLTYPE_SMALLINT"),
"int" => array("SQLSRV_SQLTYPE_BIT", "SQLSRV_SQLTYPE_TINYINT", "SQLSRV_SQLTYPE_SMALLINT", "SQLSRV_SQLTYPE_INT"),
"bigint" => array("SQLSRV_SQLTYPE_BIT", "SQLSRV_SQLTYPE_TINYINT", "SQLSRV_SQLTYPE_SMALLINT", "SQLSRV_SQLTYPE_INT", "SQLSRV_SQLTYPE_BIGINT"));
$conn = AE\connect();
foreach ($dataTypes as $dataType) {
echo "\nTesting $dataType:\n";
// create table containing bit, tinyint, smallint, int, or bigint columns
$tbname = "test_" . $dataType;
$colMetaArr = array( new AE\ColumnMeta($dataType, "c_det"), new AE\ColumnMeta($dataType, "c_rand", null, false));
AE\createTable($conn, $tbname, $colMetaArr);
// insert by specifying different SQLSRV_SQLTYPE integer constants
// with AE, should only be successful if the SQLSRV_SQLTYPE is smaller in size than the column datatype
foreach($sqlTypes as $sqlType) {
$inputs = array(new AE\BindParamOption($inputValues[0], null, null, $sqlType), new AE\BindParamOption($inputValues[1], null, null, $sqlType));
$r;
$stmt = AE\insertRow($conn, $tbname, array($colMetaArr[0]->colName => $inputs[0], $colMetaArr[1]->colName => $inputs[1]), $r, AE\INSERT_PREPARE_PARAMS);
// check the case if the type conversion is not listed in $aeConvList
if (!in_array($sqlType, $aeConvList["$dataType"])) {
if (AE\isColEncrypted()) {
if ($r !== false) {
echo "AE: Conversion from $sqlType to $dataType should not be supported\n";
} else {
if (sqlsrv_errors()[0]['SQLSTATE'] != "22018") {
echo "AE: Conversion from $sqlType to $dataType expects an operand type clash error, actual error is incorrect\n";
var_dump(sqlsrv_errors());
}
}
} else {
if ($r === false) {
echo "Conversion from $sqlType to $dataType should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if ($row['c_det'] != $inputValues[0] || $row['c_rand'] != $inputValues[1]) {
echo "Conversion from $sqlType to $dataType causes data corruption\n";
}
}
}
} else {
if ($r === false) {
echo "Conversion from $sqlType to $dataType should be supported\n";
} else {
$sql = "SELECT c_det, c_rand FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if ($row['c_det'] == $inputValues[0] && $row['c_rand'] == $inputValues[1]) {
echo "****Conversion from $sqlType to $dataType is supported****\n";
} else {
echo "Conversion from $sqlType to $dataType causes data corruption\n";
}
}
}
// cleanup
sqlsrv_free_stmt($stmt);
sqlsrv_query($conn, "TRUNCATE TABLE $tbname");
}
dropTable($conn, $tbname);
}
sqlsrv_close($conn);
?>
--EXPECT--
Testing bit:
****Conversion from SQLSRV_SQLTYPE_BIT to bit is supported****
Testing tinyint:
****Conversion from SQLSRV_SQLTYPE_BIT to tinyint is supported****
****Conversion from SQLSRV_SQLTYPE_TINYINT to tinyint is supported****
Testing smallint:
****Conversion from SQLSRV_SQLTYPE_BIT to smallint is supported****
****Conversion from SQLSRV_SQLTYPE_TINYINT to smallint is supported****
****Conversion from SQLSRV_SQLTYPE_SMALLINT to smallint is supported****
Testing int:
****Conversion from SQLSRV_SQLTYPE_BIT to int is supported****
****Conversion from SQLSRV_SQLTYPE_TINYINT to int is supported****
****Conversion from SQLSRV_SQLTYPE_SMALLINT to int is supported****
****Conversion from SQLSRV_SQLTYPE_INT to int is supported****
Testing bigint:
****Conversion from SQLSRV_SQLTYPE_BIT to bigint is supported****
****Conversion from SQLSRV_SQLTYPE_TINYINT to bigint is supported****
****Conversion from SQLSRV_SQLTYPE_SMALLINT to bigint is supported****
****Conversion from SQLSRV_SQLTYPE_INT to bigint is supported****
****Conversion from SQLSRV_SQLTYPE_BIGINT to bigint is supported****

View file

@ -0,0 +1,270 @@
--TEST--
Test for inserting encrypted data of nchar types with different sizes
--DESCRIPTION--
Test implicit conversions between different nchar types of different sizes
With Always Encrypted, implicit conversion works if:
1. From input of SQLSRV_SQLTYPE_NCHAR(n) to a larger nchar(m) column where n <= m
2. From input of SQLSRV_SQLTYPE_NCHAR(n) to a larger nvarchar(m) column where n <= m (m can be max)
3. From input of SQLSRV_SQLTYPE_NVARCHAR(n) to a larger nchar(m) column where n <= m
4. From input of SQLSRV_SQLTYPE_NVARCHAR(n) to a larger nvarchar(m) column where n <= m (m can be max)
Without AlwaysEncrypted, implicit conversion between different binary types and sizes works
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
--FILE--
<?php
require_once('MsCommon.inc');
$dataTypes = array("nchar", "nvarchar", "nvarchar(max)");
$lengths = array(1, 8, 64, 512, 4000);
$sqlTypes = array("SQLSRV_SQLTYPE_NCHAR", "SQLSRV_SQLTYPE_NVARCHAR", "SQLSRV_SQLTYPE_NVARCHAR('max')");
$sqltypeLengths = $lengths;
$inputValue = "d";
$conn = AE\connect();
foreach($dataTypes as $dataType) {
$maxcol = strpos($dataType, "(max)");
foreach($lengths as $m) {
if ($maxcol !== false) {
$typeFull = $dataType;
} else {
$typeFull = "$dataType($m)";
}
echo "\nTesting $typeFull:\n";
// create table containing nchar(m) or nvarchar(m) columns
$tbname = "test_" . str_replace(array('(', ')'), '', $dataType) . $m;
$colMetaArr = array(new AE\ColumnMeta($typeFull, "c1", null, false));
AE\createTable($conn, $tbname, $colMetaArr);
// insert by specifying SQLSRV_SQLTYPE_NCHAR(n) or SQLSRV_SQLTYPE_NVARCHAR(n)
// with AE, should be successful as long as the SQLSRV_SQLTYPE length (n) is smaller than the column length (m)
foreach($sqlTypes as $sqlType) {
$maxsqltype = strpos($sqlType, "max");
foreach($sqltypeLengths as $n) {
if ($maxsqltype !== false) {
$sqltypeFull = $sqlType;
} else {
$sqltypeFull = "$sqlType($n)";
}
//insert a row
$input = new AE\BindParamOption($inputValue, null, null, $sqltypeFull);
$r;
$stmt = AE\insertRow($conn, $tbname, array("c1" => $input), $r, AE\INSERT_PREPARE_PARAMS);
// check the case when SQLSRV_SQLTYPE length (n) is greater than the column length (m)
// with AE: should not works
// without AE: should work
if (($n > $m || $maxsqltype) && !$maxcol) {
if (AE\isColEncrypted()) {
if ($r !== false) {
echo "AE: Conversion from $sqltypeFull to $typeFull should not be supported\n";
} else {
if (sqlsrv_errors()[0]['SQLSTATE'] != "22018") {
echo "AE: Conversion from $sqltypeFull to $typeFull expects an operand type clash error, actual error is incorrect\n";
var_dump(sqlsrv_errors());
}
}
} else {
if ($r === false) {
echo "Conversions from $sqltypeFull to $typeFull should be supported\n";
}
$sql = "SELECT c1 FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (trim($row['c1']) != $inputValue) {
echo "Conversion from $sqltypeFull to $typeFull causes data corruption\n";
}
}
// check the case when SQLSRV_SQLTYPE length (n) is less than or equal to the column length (m)
// should work with AE or non AE
} else {
if ($r === false) {
echo "Conversion from $sqltypeFull to $typeFull should be supported\n";
} else {
$sql = "SELECT c1 FROM $tbname";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
if (trim($row['c1']) == $inputValue) {
echo "****Conversion from $sqltypeFull to $typeFull is supported****\n";
} else {
echo "Conversion from $sqltypeFull to $typeFull causes data corruption\n";
}
}
}
// cleanup
sqlsrv_free_stmt($stmt);
sqlsrv_query($conn, "TRUNCATE TABLE $tbname");
}
}
dropTable($conn, $tbname);
}
}
sqlsrv_close($conn);
?>
--EXPECT--
Testing nchar(1):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nchar(1) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nchar(1) is supported****
Testing nchar(8):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nchar(8) is supported****
Testing nchar(64):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nchar(64) is supported****
Testing nchar(512):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nchar(512) is supported****
Testing nchar(4000):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(4000) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(4000) to nchar(4000) is supported****
Testing nvarchar(1):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(1) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(1) is supported****
Testing nvarchar(8):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(8) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(8) is supported****
Testing nvarchar(64):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nvarchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(64) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nvarchar(64) is supported****
Testing nvarchar(512):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nvarchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nvarchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nvarchar(512) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nvarchar(512) is supported****
Testing nvarchar(4000):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(4000) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nvarchar(4000) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(4000) to nvarchar(4000) is supported****
Testing nvarchar(max):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
Testing nvarchar(max):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
Testing nvarchar(max):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
Testing nvarchar(max):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
Testing nvarchar(max):
****Conversion from SQLSRV_SQLTYPE_NCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(1) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(8) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(64) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(512) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR(4000) to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****
****Conversion from SQLSRV_SQLTYPE_NVARCHAR('max') to nvarchar(max) is supported****