Skip to content

Commit 1ba1f21

Browse files
authored
Added more checks for error conditions (#965)
1 parent 7d389e0 commit 1ba1f21

File tree

4 files changed

+133
-14
lines changed

4 files changed

+133
-14
lines changed

test/functional/sqlsrv/0075.phpt

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
Fix for output string parameter truncation error
3+
--DESCRIPTION--
4+
This test includes calling sqlsrv_query with an array of parameters with a named key, which should result in an error.
35
--SKIPIF--
46
<?php require('skipif_versions_old.inc'); ?>
57
--FILE--
@@ -23,10 +25,25 @@ if ($s === false) {
2325

2426
$inValue1 = "Some data";
2527
$outValue1 = "";
28+
$tsql = '{CALL [test_output] (?, ?)}';
29+
2630
$s = sqlsrv_query(
2731
$conn,
28-
"{CALL [test_output] (?, ?)}",
29-
array(array($inValue1, SQLSRV_PARAM_IN, null, SQLSRV_SQLTYPE_VARCHAR(512)),
32+
$tsql,
33+
array("k1" => array($inValue1, SQLSRV_PARAM_IN, null, SQLSRV_SQLTYPE_VARCHAR(512)),
34+
array(&$outValue1, SQLSRV_PARAM_OUT, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR(512)))
35+
);
36+
37+
if ($s !== false) {
38+
echo "Expect this to fail!\n";
39+
} else {
40+
print_r(sqlsrv_errors());
41+
}
42+
43+
$s = sqlsrv_query(
44+
$conn,
45+
$tsql,
46+
array(array($inValue1, SQLSRV_PARAM_IN, null, SQLSRV_SQLTYPE_VARCHAR(512)),
3047
array(&$outValue1, SQLSRV_PARAM_OUT, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR), SQLSRV_SQLTYPE_VARCHAR(512)))
3148
);
3249

@@ -45,5 +62,18 @@ sqlsrv_close($conn);
4562

4663
?>
4764
--EXPECT--
65+
Array
66+
(
67+
[0] => Array
68+
(
69+
[0] => IMSSP
70+
[SQLSTATE] => IMSSP
71+
[1] => -57
72+
[code] => -57
73+
[2] => String keys are not allowed in parameters arrays.
74+
[message] => String keys are not allowed in parameters arrays.
75+
)
76+
77+
)
4878
512
4979
Some data

test/functional/sqlsrv/sqlsrv_378_out_param_error.phpt

+67-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
This test verifies that GitHub issue #378 is fixed.
33
--DESCRIPTION--
44
GitHub issue #378 - output parameters appends garbage info when variable is initialized with different data type
5-
steps to reproduce the issue:
5+
Steps to reproduce the issue:
66
1- create a store procedure with print and output parameter
77
2- initialize output parameters to a different data type other than the type declared in sp.
88
3- set the WarningsReturnAsErrors to true
99
4- call sp.
10+
Also check error conditions by passing output parameters NOT by reference.
11+
--ENV--
12+
PHPT_EXEC=true
1013
--SKIPIF--
1114
<?php require('skipif_versions_old.inc'); ?>
1215
--FILE--
@@ -19,11 +22,8 @@ $conn = AE\connect();
1922
$procName = 'test_378';
2023
createSP($conn, $procName);
2124

22-
sqlsrv_configure('WarningsReturnAsErrors', true);
23-
executeSP($conn, $procName);
24-
25-
sqlsrv_configure('WarningsReturnAsErrors', false);
26-
executeSP($conn, $procName);
25+
runTests($conn, $procName, true);
26+
runTests($conn, $procName, false);
2727

2828
dropProc($conn, $procName);
2929
echo "Done\n";
@@ -46,22 +46,79 @@ function createSP($conn, $procName)
4646
}
4747
}
4848

49-
function executeSP($conn, $procName)
49+
//-------------------functions-------------------
50+
function runTests($conn, $procName, $warningAsErrors)
51+
{
52+
sqlsrv_configure('WarningsReturnAsErrors', $warningAsErrors);
53+
54+
trace("\nWarningsReturnAsErrors: $warningAsErrors\n");
55+
56+
executeSP($conn, $procName, true, false);
57+
executeSP($conn, $procName, true, true);
58+
executeSP($conn, $procName, false, false);
59+
executeSP($conn, $procName, false, true);
60+
}
61+
62+
function compareErrors()
63+
{
64+
$message = 'Variable parameter 3 not passed by reference (prefaced with an &). Output or bidirectional variable parameters (SQLSRV_PARAM_OUT and SQLSRV_PARAM_INOUT) passed to sqlsrv_prepare or sqlsrv_query should be passed by reference, not by value.';
65+
66+
$error = sqlsrv_errors()[0]['message'];
67+
68+
if ($error !== $message) {
69+
print_r(sqlsrv_errors(), true);
70+
return;
71+
}
72+
73+
trace("Comparing errors: matched!\n");
74+
}
75+
76+
function executeSP($conn, $procName, $noRef, $prepare)
5077
{
5178
$expected = 3;
5279
$v1 = 1;
5380
$v2 = 2;
5481
$v3 = 'str';
5582

5683
$res = true;
57-
if (AE\isColEncrypted()) {
58-
$stmt = sqlsrv_prepare($conn, "{call $procName( ?, ?, ?)}", array($v1, $v2, array(&$v3, SQLSRV_PARAM_OUT)));
84+
$tsql = "{call $procName( ?, ?, ?)}";
85+
86+
if ($noRef) {
87+
$params = array($v1, $v2, array($v3, SQLSRV_PARAM_OUT));
88+
} else {
89+
$params = array($v1, $v2, array(&$v3, SQLSRV_PARAM_OUT));
90+
}
91+
92+
trace("No reference: $noRef\n");
93+
trace("Use prepared stmt: $prepare\n");
94+
95+
if (AE\isColEncrypted() || $prepare) {
96+
$stmt = sqlsrv_prepare($conn, $tsql, $params);
5997
if ($stmt) {
6098
$res = sqlsrv_execute($stmt);
99+
} else {
100+
fatalError("executeSP: failed in preparing statement with reference($noRef)");
61101
}
102+
if ($noRef) {
103+
if ($res !== false) {
104+
echo "Expect this to fail!\n";
105+
}
106+
compareErrors();
107+
return;
108+
}
62109
} else {
63-
$stmt = sqlsrv_query($conn, "{call $procName( ?, ?, ?)}", array($v1, $v2, array(&$v3, SQLSRV_PARAM_OUT)));
110+
$stmt = sqlsrv_query($conn, $tsql, $params);
111+
if ($noRef) {
112+
if ($stmt !== false) {
113+
echo "Expect this to fail!\n";
114+
}
115+
compareErrors();
116+
return;
117+
}
64118
}
119+
120+
trace("No errors: $v3 and $expected\n");
121+
// No errors expected
65122
if ($stmt === false || !$res) {
66123
print_r(sqlsrv_errors(), true);
67124
}

test/functional/sqlsrv/sqlsrv_data_to_str.phpt

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
large types to strings of 1MB size.
3+
--DESCRIPTION--
4+
This includes a test by providing an invalid php type.
35
--SKIPIF--
46
<?php require('skipif_azure_dw.inc'); ?>
57
--FILE--
@@ -8,7 +10,7 @@ large types to strings of 1MB size.
810
sqlsrv_configure( 'WarningsReturnAsErrors', 0 );
911
sqlsrv_configure( 'LogSeverity', SQLSRV_LOG_SEVERITY_ALL );
1012

11-
require( 'MsCommon.inc' );
13+
require_once( 'MsCommon.inc' );
1214

1315
$conn = Connect();
1416
if( !$conn ) {
@@ -59,6 +61,16 @@ large types to strings of 1MB size.
5961
die( "sqlsrv_get_field(6) failed." );
6062
}
6163

64+
$str = sqlsrv_get_field( $stmt, 0, SQLSRV_PHPTYPE_STRING("UTF") );
65+
if ($str === false) {
66+
$error = sqlsrv_errors()[0]['message'];
67+
if ($error !== 'Invalid type') {
68+
fatalError('Unexpected error returned');
69+
}
70+
} else {
71+
echo "Expect sqlsrv_get_field(7) to fail!\n";
72+
}
73+
6274
sqlsrv_free_stmt( $stmt );
6375
sqlsrv_close( $conn );
6476

test/functional/sqlsrv/test_scrollable.phpt

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
scrollable result sets.
2+
Scrollable result sets with a simple test for an expected error.
33
--SKIPIF--
44
<?php require('skipif_versions_old.inc'); ?>
55
--FILE--
@@ -69,6 +69,13 @@ for ($i = 1; $i <= $numRows; $i++) {
6969
}
7070

7171
$query = "SELECT * FROM $tableName";
72+
$options = array('Scrollable' => 'dummy');
73+
$stmt = sqlsrv_query($conn, $query, array(), $options);
74+
if ($stmt !== false) {
75+
fatalError("Expect dummy scrollable to fail!\n");
76+
}
77+
print_r(sqlsrv_errors());
78+
7279
$options = array('Scrollable' => SQLSRV_CURSOR_FORWARD);
7380
$stmt = sqlsrv_query($conn, $query, array(), $options);
7481

@@ -205,4 +212,17 @@ echo "Test succeeded.\n";
205212

206213
?>
207214
--EXPECT--
215+
Array
216+
(
217+
[0] => Array
218+
(
219+
[0] => IMSSP
220+
[SQLSTATE] => IMSSP
221+
[1] => -54
222+
[code] => -54
223+
[2] => The value passed for the 'Scrollable' statement option is invalid. Please use 'static', 'dynamic', 'keyset', 'forward', or 'buffered'.
224+
[message] => The value passed for the 'Scrollable' statement option is invalid. Please use 'static', 'dynamic', 'keyset', 'forward', or 'buffered'.
225+
)
226+
227+
)
208228
Test succeeded.

0 commit comments

Comments
 (0)