Skip to content

Commit

Permalink
Merge pull request #91 from SimonFrings/tests
Browse files Browse the repository at this point in the history
Clean up test suite, add .gitattributes to exclude dev files from exports and run tests on PHP 7.4 and simplify test matrix
  • Loading branch information
clue authored May 29, 2020
2 parents 3935ff4 + b658d46 commit 319f833
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 66 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/examples/ export-ignore
/phpunit.xml.dist export-ignore
/tests/ export-ignore
19 changes: 10 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
language: php

php:
# - 5.3 # requires old distro, see below
- 5.4
- 5.5
- 5.6
- 7
- hhvm # ignore errors, see below

# lock distro so new future defaults will not break the build
dist: trusty

matrix:
include:
- php: 5.3
dist: precise
- php: 5.4
- php: 5.5
- php: 5.6
- php: 7.0
- php: 7.1
- php: 7.2
- php: 7.3
- php: 7.4
- php: hhvm-3.18
allow_failures:
- php: hhvm
- php: hhvm-3.18

sudo: false

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
"autoload": {
"psr-4": {"Clue\\React\\Socks\\": "src/"}
},
"autoload-dev": {
"psr-4": { "Clue\\Tests\\React\\Socks\\": "tests/" }
},
"require": {
"php": ">=5.3",
"react/promise": "^2.1 || ^1.2",
"react/socket": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^6.0 || ^5.7 || ^4.8.35",
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.7 || ^4.8.35",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"clue/connection-manager-extra": "^1.0 || ^0.7",
"clue/block-react": "^1.1"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" bootstrap="./tests/bootstrap.php">
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Socks Test Suite">
<directory>./tests/</directory>
Expand Down
11 changes: 7 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Socks;

use Clue\React\Socks\Client;
use React\Promise\Promise;
use Clue\React\Socks\Server;
Expand All @@ -16,7 +18,7 @@ class ClientTest extends TestCase

public function setUp()
{
$this->loop = React\EventLoop\Factory::create();
$this->loop = \React\EventLoop\Factory::create();
$this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
$this->client = new Client('127.0.0.1:1080', $this->connector);
}
Expand Down Expand Up @@ -162,7 +164,7 @@ public function testCreateWithInvalidPortDoesNotConnect()

public function testConnectorRejectsWillRejectConnection()
{
$promise = \React\Promise\reject(new RuntimeException());
$promise = \React\Promise\reject(new \RuntimeException());

$this->connector->expects($this->once())->method('connect')->with('127.0.0.1:1080?hostname=google.com')->willReturn($promise);

Expand Down Expand Up @@ -261,7 +263,7 @@ public function testEmitConnectionErrorDuringSessionWillRejectConnection()

$promise = $this->client->connect('google.com:80');

$stream->emit('error', array(new RuntimeException()));
$stream->emit('error', array(new \RuntimeException()));

$promise->then(null, $this->expectCallableOnceWithException(
'RuntimeException',
Expand Down Expand Up @@ -547,9 +549,10 @@ public function testConnectionErrorShouldNotCreateGarbageCycles()
$this->connector->expects($this->once())->method('connect')->willReturn($deferred->promise());

gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on

$promise = $this->client->connect('google.com:80');
$deferred->reject(new RuntimeException());
$deferred->reject(new \RuntimeException());
unset($deferred, $promise);

$this->assertEquals(0, gc_collect_cycles());
Expand Down
30 changes: 16 additions & 14 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Socks;

use Clue\React\Socks\Client;
use Clue\React\Socks\Server;
use Clue\React\Block;
Expand All @@ -20,9 +22,9 @@ class FunctionalTest extends TestCase

public function setUp()
{
$this->loop = React\EventLoop\Factory::create();
$this->loop = \React\EventLoop\Factory::create();

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$address = $socket->getAddress();
if (strpos($address, '://') === false) {
$address = 'tcp://' . $address;
Expand Down Expand Up @@ -196,7 +198,7 @@ public function testConnectionAuthenticationFromUri()
{
$this->server = new Server($this->loop, null, array('name' => 'pass'));

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -219,7 +221,7 @@ public function testConnectionAuthenticationCallback()
return true;
});

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -239,7 +241,7 @@ public function testConnectionAuthenticationCallbackWillNotBeInvokedIfClientsSen
return true;
});

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -254,7 +256,7 @@ public function testConnectionAuthenticationFromUriEncoded()
{
$this->server = new Server($this->loop, null, array('name' => 'p@ss:w0rd'));

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -268,7 +270,7 @@ public function testConnectionAuthenticationFromUriWithOnlyUserAndNoPassword()
{
$this->server = new Server($this->loop, null, array('empty' => ''));

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -282,7 +284,7 @@ public function testConnectionAuthenticationEmptyPassword()
{
$this->server = new Server($this->loop, null, array('user' => ''));

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -303,7 +305,7 @@ public function testConnectionInvalidNoAuthenticationOverLegacySocks4()
{
$this->server = new Server($this->loop, null, array('name' => 'pass'));

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -316,7 +318,7 @@ public function testConnectionInvalidNoAuthentication()
{
$this->server = new Server($this->loop, null, array('name' => 'pass'));

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -329,7 +331,7 @@ public function testConnectionInvalidAuthenticationMismatch()
{
$this->server = new Server($this->loop, null, array('name' => 'pass'));

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -344,7 +346,7 @@ public function testConnectionInvalidAuthenticatorReturnsFalse()
return false;
});

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -359,7 +361,7 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal
return \React\Promise\resolve(false);
});

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand All @@ -374,7 +376,7 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseRejected()
return \React\Promise\reject();
});

$socket = new React\Socket\Server(0, $this->loop);
$socket = new \React\Socket\Server(0, $this->loop);
$this->server->listen($socket);
$this->port = parse_url($socket->getAddress(), PHP_URL_PORT);

Expand Down
20 changes: 11 additions & 9 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Socks;

use Clue\React\Socks\Server;
use React\Promise\Promise;
use React\Promise\Timer\TimeoutException;
Expand Down Expand Up @@ -138,43 +140,43 @@ public function provideConnectionErrors()
{
return array(
array(
new RuntimeException('', SOCKET_EACCES),
new \RuntimeException('', SOCKET_EACCES),
Server::ERROR_NOT_ALLOWED_BY_RULESET
),
array(
new RuntimeException('', SOCKET_ENETUNREACH),
new \RuntimeException('', SOCKET_ENETUNREACH),
Server::ERROR_NETWORK_UNREACHABLE
),
array(
new RuntimeException('', SOCKET_EHOSTUNREACH),
new \RuntimeException('', SOCKET_EHOSTUNREACH),
Server::ERROR_HOST_UNREACHABLE,
),
array(
new RuntimeException('', SOCKET_ECONNREFUSED),
new \RuntimeException('', SOCKET_ECONNREFUSED),
Server::ERROR_CONNECTION_REFUSED
),
array(
new RuntimeException('Connection refused'),
new \RuntimeException('Connection refused'),
Server::ERROR_CONNECTION_REFUSED
),
array(
new RuntimeException('', SOCKET_ETIMEDOUT),
new \RuntimeException('', SOCKET_ETIMEDOUT),
Server::ERROR_TTL
),
array(
new TimeoutException(1.0),
Server::ERROR_TTL
),
array(
new RuntimeException(),
new \RuntimeException(),
Server::ERROR_GENERAL
)
);
}

/**
* @dataProvider provideConnectionErrors
* @param Exception $error
* @param \Exception $error
* @param int $expectedCode
*/
public function testConnectWillReturnMappedSocks5ErrorCodeFromConnector($error, $expectedCode)
Expand Down Expand Up @@ -342,7 +344,7 @@ public function testHandleSocks5ConnectionWithConnectorRefusedWillReturnReturnRe
{
$connection = $this->getMockBuilder('React\Socket\Connection')->disableOriginalConstructor()->setMethods(array('pause', 'end', 'write'))->getMock();

$promise = \React\Promise\reject(new RuntimeException('Connection refused'));
$promise = \React\Promise\reject(new \RuntimeException('Connection refused'));

$this->connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($promise);

Expand Down
18 changes: 10 additions & 8 deletions tests/StreamReaderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Clue\Tests\React\Socks;

use Clue\React\Socks\StreamReader;

class StreamReaderTest extends TestCase
Expand All @@ -13,7 +15,7 @@ public function setUp()

public function testReadByteAssertCorrect()
{
$this->reader->readByteAssert(0x01)->then($this->expectCallableOnce(0x01));
$this->reader->readByteAssert(0x01)->then($this->expectCallableOnceWith(0x01));

$this->reader->write("\x01");
}
Expand All @@ -27,14 +29,14 @@ public function testReadByteAssertInvalid()

public function testReadStringNull()
{
$this->reader->readStringNull()->then($this->expectCallableOnce('hello'));
$this->reader->readStringNull()->then($this->expectCallableOnceWith('hello'));

$this->reader->write("hello\x00");
}

public function testReadStringLength()
{
$this->reader->readLength(5)->then($this->expectCallableOnce('hello'));
$this->reader->readLength(5)->then($this->expectCallableOnceWith('hello'));

$this->reader->write('he');
$this->reader->write('ll');
Expand All @@ -47,17 +49,17 @@ public function testReadBuffered()
{
$this->reader->write('hello');

$this->reader->readLength(5)->then($this->expectCallableOnce('hello'));
$this->reader->readLength(5)->then($this->expectCallableOnceWith('hello'));

$this->assertEquals('', $this->reader->getBuffer());
}

public function testSequence()
{
$this->reader->readByte()->then($this->expectCallableOnce(ord('h')));
$this->reader->readByteAssert(ord('e'))->then($this->expectCallableOnce(ord('e')));
$this->reader->readLength(4)->then($this->expectCallableOnce('llo '));
$this->reader->readBinary(array('w'=>'C', 'o' => 'C'))->then($this->expectCallableOnce(array('w' => ord('w'), 'o' => ord('o'))));
$this->reader->readByte()->then($this->expectCallableOnceWith(ord('h')));
$this->reader->readByteAssert(ord('e'))->then($this->expectCallableOnceWith(ord('e')));
$this->reader->readLength(4)->then($this->expectCallableOnceWith('llo '));
$this->reader->readBinary(array('w'=>'C', 'o' => 'C'))->then($this->expectCallableOnceWith(array('w' => ord('w'), 'o' => ord('o'))));

$this->reader->write('hello world');

Expand Down
Loading

0 comments on commit 319f833

Please sign in to comment.