Skip to content

Instantly share code, notes, and snippets.

View odinuv's full-sized avatar
🐺
Wolf

Ondrej Popelka odinuv

🐺
Wolf
View GitHub Profile
@odinuv
odinuv / kill-connection.php
Created September 27, 2018 15:26
Testing the untestable: Kill connection
<?php
public function killConnection(string $event): void
{
if ($this->killerEnabled && ($this->killerEnabled === $event)) {
$this->serviceConnection->exec('KILL ' . $this->pid);
}
}
@odinuv
odinuv / create-connection.php
Created September 27, 2018 15:24
Testing the untestable: Tainted PDO create
<?php
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
];
$pdo = new TaintedPDO($dsn, $user, $password, $options);
$pdo->setOnEvent([$this, 'killConnection']);
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
$pdo->setAttribute(
@odinuv
odinuv / tainted-pdo-statement.php
Last active September 27, 2018 15:25
Testing the untestable: Tainted PDOStatement
<?php
namespace Keboola\DbExtractor\Tests;
class TaintedPDOStatement extends \PDOStatement
{
/**
* @var callable
*/
private $callback;
@odinuv
odinuv / tainted-pdo.php
Last active September 27, 2018 15:25
Testing the untestable: Tainted PDO
<?php
namespace Keboola\DbExtractor\Tests;
class TaintedPDO extends \PDO
{
private $callback;
public function setOnEvent($callback)
{
@odinuv
odinuv / terminate-connection-test.php
Created September 27, 2018 15:21
Testing the untestable: Terminate connection test
<?php
public function testNetworkKillerQuery(): void
{
ErrorHandler::register(null, true);
$this->pdo->query('KILL SELECT CONNECTION_ID()');
self::expectException(\ErrorException::class);
self::expectExceptionMessage('Warning: PDO::query(): MySQL server has gone away');
$this->pdo->query('SELECT NOW();');
}
@odinuv
odinuv / network-kill-fetch.php
Created September 27, 2018 15:20
Testing the untestable: Test network kill during fetch
<?php
public function testNetworkKillerExecute()
{
$this->setupLargeTable();
$conn = $this->getConnection();
// sleep 5 seconds before killing, then kill it for 10 seconds
exec('php killNetwork.php 5 10 > /dev/null &');
ErrorHandler::register(null, true);
// sleep a while to make sure the connection is terminated
@odinuv
odinuv / network-kill-execute.php
Created September 27, 2018 15:19
Testing the untestable: Test network kill during execute
<?php
public function testNetworkKillerExecute()
{
$conn = $this->getConnection();
// sleep 1 second before killing, then kill it for 3 seconds
exec('php killNetwork.php 1 3 > /dev/null &');
ErrorHandler::register(null, true);
// sleep a while to make sure the connection is terminated
sleep(2);
@odinuv
odinuv / network-kill-query.php
Created September 27, 2018 15:17
Testing the untestable: Test network kill during query
<?php
public function testNetworkKillerQuery()
{
$conn = $this->getConnection();
// sleep 1 second before killing, then kill it for 3 seconds
exec('php killNetwork.php 1 3 > /dev/null &');
ErrorHandler::register(null, true);
// sleep a while to make sure the connection is terminated
sleep(2);
@odinuv
odinuv / kill-server.php
Created September 27, 2018 15:16
Testing the untestable: Kill server
<?php
sleep((int) $argv[1]);
$client = new \Aws\Rds\RdsClient([
'credentials' => [
'key' => getenv('TEST_RDS_ACCESS_KEY'),
'secret' => getenv('TEST_RDS_ACCESS_SECRET'),
],
'region' => 'us-east-1',
'version' => '2014–10–31',
@odinuv
odinuv / killConnection.php
Created September 27, 2018 15:13
Testing the untestable: Kill connection
<?php
sleep((int) $argv[1]);
exec('cports /close * * * 3600');