-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
ext/sqlite3/tests/sqlite3_trampoline_create_aggregate_no_leak.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--TEST-- | ||
SQLite3::createAggregate() trampoline callback | ||
--EXTENSIONS-- | ||
sqlite3 | ||
--FILE-- | ||
<?php | ||
|
||
require_once(__DIR__ . '/new_db.inc'); | ||
|
||
class TrampolineTest { | ||
public function __call(string $name, array $arguments) { | ||
echo 'Trampoline for ', $name, PHP_EOL; | ||
$context = $arguments[0]; | ||
if ($name === 'finalize') { | ||
return implode(',', $context['values']); | ||
} | ||
if (empty($context)) { | ||
$context = ['total' => 0, 'values' => []]; | ||
} | ||
$context['total'] += (int) $arguments[2]; | ||
$context['values'][] = $context['total']; | ||
return $context; | ||
} | ||
} | ||
$o = new TrampolineTest(); | ||
$step = [$o, 'step']; | ||
$finalize = [$o, 'finalize']; | ||
|
||
echo "Creating Table\n"; | ||
var_dump($db->exec('CREATE TABLE test (a INTEGER, b INTEGER)')); | ||
|
||
echo "INSERT into table\n"; | ||
var_dump($db->exec("INSERT INTO test (a, b) VALUES (1, -1)")); | ||
var_dump($db->exec("INSERT INTO test (a, b) VALUES (2, -2)")); | ||
var_dump($db->exec("INSERT INTO test (a, b) VALUES (3, -3)")); | ||
var_dump($db->exec("INSERT INTO test (a, b) VALUES (4, -4)")); | ||
var_dump($db->exec("INSERT INTO test (a, b) VALUES (4, -4)")); | ||
|
||
$db->createAggregate('S', $step, $finalize, 1); | ||
|
||
print_r($db->querySingle("SELECT S(a), S(b) FROM test", true)); | ||
|
||
echo "Closing database\n"; | ||
var_dump($db->close()); | ||
echo "Done\n"; | ||
?> | ||
--EXPECT-- | ||
Creating Table | ||
bool(true) | ||
INSERT into table | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
bool(true) | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for step | ||
Trampoline for finalize | ||
Trampoline for finalize | ||
Array | ||
( | ||
[S(a)] => 1,3,6,10,14 | ||
[S(b)] => -1,-3,-6,-10,-14 | ||
) | ||
Closing database | ||
bool(true) | ||
Done |
33 changes: 33 additions & 0 deletions
33
ext/sqlite3/tests/sqlite3_trampoline_createcollation_no_leak.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--TEST-- | ||
SQLite3::createFunction use F ZPP for trampoline callback and does not leak | ||
--EXTENSIONS-- | ||
sqlite3 | ||
--FILE-- | ||
<?php | ||
|
||
require_once(__DIR__ . '/new_db.inc'); | ||
|
||
class TrampolineTest { | ||
public function __call(string $name, array $arguments) { | ||
echo 'Trampoline for ', $name, PHP_EOL; | ||
return strtoupper($arguments[0]); | ||
} | ||
} | ||
$o = new TrampolineTest(); | ||
$callback = [$o, 'strtoupper']; | ||
|
||
var_dump($db->createfunction('', $callback)); | ||
|
||
try { | ||
var_dump($db->createfunction('strtoupper', $callback, new stdClass())); | ||
} catch (\Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
var_dump($db->createfunction('strtoupper', $callback)); | ||
|
||
?> | ||
--EXPECT-- | ||
bool(false) | ||
TypeError: SQLite3::createFunction(): Argument #3 ($argCount) must be of type int, stdClass given | ||
bool(true) |
33 changes: 33 additions & 0 deletions
33
ext/sqlite3/tests/sqlite3_trampoline_createfunction_no_leak.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--TEST-- | ||
SQLite3::createFunction use F ZPP for trampoline callback and does not leak | ||
--EXTENSIONS-- | ||
sqlite3 | ||
--FILE-- | ||
<?php | ||
|
||
require_once(__DIR__ . '/new_db.inc'); | ||
|
||
class TrampolineTest { | ||
public function __call(string $name, array $arguments) { | ||
echo 'Trampoline for ', $name, PHP_EOL; | ||
return strtoupper($arguments[0]); | ||
} | ||
} | ||
$o = new TrampolineTest(); | ||
$callback = [$o, 'strtoupper']; | ||
|
||
var_dump($db->createfunction('', $callback)); | ||
|
||
try { | ||
var_dump($db->createfunction('strtoupper', $callback, new stdClass())); | ||
} catch (\Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
var_dump($db->createfunction('strtoupper', $callback)); | ||
|
||
?> | ||
--EXPECT-- | ||
bool(false) | ||
TypeError: SQLite3::createFunction(): Argument #3 ($argCount) must be of type int, stdClass given | ||
bool(true) |
41 changes: 41 additions & 0 deletions
41
ext/sqlite3/tests/sqlite3_trampoline_setauthorizer_no_leak.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--TEST-- | ||
SQLite3 user authorizer trampoline callback | ||
--EXTENSIONS-- | ||
sqlite3 | ||
--FILE-- | ||
<?php | ||
|
||
class TrampolineTest { | ||
public function __call(string $name, array $arguments) { | ||
echo 'Trampoline for ', $name, PHP_EOL; | ||
if ($arguments[0] == SQLite3::SELECT) { | ||
return SQLite3::OK; | ||
} | ||
|
||
return SQLite3::DENY; | ||
} | ||
} | ||
$o = new TrampolineTest(); | ||
$callback = [$o, 'authorizer']; | ||
|
||
$db = new SQLite3(':memory:'); | ||
$db->enableExceptions(true); | ||
|
||
$db->setAuthorizer($callback); | ||
|
||
// This query should be accepted | ||
var_dump($db->querySingle('SELECT 1;')); | ||
|
||
try { | ||
// This one should fail | ||
var_dump($db->querySingle('CREATE TABLE test (a, b);')); | ||
} catch (\Exception $e) { | ||
echo $e->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Trampoline for authorizer | ||
int(1) | ||
Trampoline for authorizer | ||
Unable to prepare statement: not authorized |