Skip to content

Commit

Permalink
to get an understanding on what happens within the loop I added an ad…
Browse files Browse the repository at this point in the history
…ditional test
  • Loading branch information
ata-no-one committed Jul 2, 2024
1 parent e50115f commit 42c969c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
1 change: 1 addition & 0 deletions php/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<phpunit>
<testsuites>
<testsuite name="VaasTesting">
<file>tests/vaas/StreamsInLoopTest.php</file>
<file>tests/vaas/Sha256Test.php</file>
<file>tests/vaas/VaasTest.php</file>
<file>tests/vaas/ProtocolTest.php</file>
Expand Down
52 changes: 52 additions & 0 deletions php/tests/vaas/StreamsInLoopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace VaasTesting;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use React\EventLoop\Loop;
use React\Http\Browser;
use React\Stream\ReadableResourceStream;
use React\Stream\ReadableStreamInterface;
use React\Stream\ThroughStream;
use React\Stream\Util;
use React\Stream\WritableResourceStream;

use function React\Async\await;
use function React\Promise\Stream\buffer;

final class StreamsInLoopTest extends TestCase
{
use ProphecyTrait;

public function testLoopUnexpectedConsumesStreamWithBrowser() {
$browser1 = new Browser();
$browser2 = new Browser();

$response1 = await($browser1->requestStreaming("GET", "https://secure.eicar.org/eicar.com.txt"));
$body1 = $response1->getBody();
$this->assertEquals(true, $body1->isReadable());

$response2 = await($browser2->requestStreaming("GET", "https://secure.eicar.org/eicar.com.txt"));
$this->assertEquals(true, $body1->isReadable());
$body2 = $response2->getBody();
$this->assertEquals(true, $body2->isReadable());

assert($body2 instanceof ReadableStreamInterface);
$string2 = await(buffer($body2));
}

static function random_strings($length_of_string) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$characters_length = strlen($characters);
$random_string = '';

// Generate random characters until the string reaches desired length
for ($i = 0; $i < $length_of_string; $i++) {
$random_index = random_int(0, $characters_length - 1);
$random_string .= $characters[$random_index];
}

return $random_string;
}
}
9 changes: 2 additions & 7 deletions php/tests/vaas/VaasTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use VaasSdk\Vaas;
use Dotenv\Dotenv;
use Exception;
use GuzzleHttp\Promise\PromiseInterface;
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\TestHandler;
Expand All @@ -25,11 +24,8 @@
use Ramsey\Uuid\Generator\RandomBytesGenerator;
use Ramsey\Uuid\Rfc4122\UuidV4;
use React\EventLoop\Loop;
use React\EventLoop\StreamSelectLoop;
use React\Http\Browser;
use React\Http\Io\HttpBodyStream;
use React\Promise\PromiseInterface as PromisePromiseInterface;
use React\Socket\Connector;
use React\Stream\ReadableResourceStream;
use React\Stream\ReadableStreamInterface;
use React\Stream\ThroughStream;
Expand All @@ -39,7 +35,6 @@
use VaasSdk\VaasOptions;
use WebSocket\BadOpcodeException;
use function React\Async\await;
use function React\Promise\Stream\unwrapReadable;

final class VaasTest extends TestCase
{
Expand Down Expand Up @@ -581,7 +576,7 @@ public function testForStream_WithCleanDelayedfor11Seconds_ReturnsClean()
$randomString = $this->random_strings(11000);
$stream = new ThroughStream();

$writeTimer = Loop::addPeriodicTimer(0.00001, function () use ($stream, &$randomString) {
$writeTimer = Loop::addPeriodicTimer(0.001, function () use ($stream, &$randomString) {
if ($randomString === "") {
$stream->end();
return;
Expand Down Expand Up @@ -661,7 +656,7 @@ public function testForStream_WithEicarUrlContentAsStream_ReturnsMaliciousWithDe
$this->assertNotEmpty($verdict->Detection);
}

function random_strings($length_of_string) {
static function random_strings($length_of_string) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$characters_length = strlen($characters);
$random_string = '';
Expand Down
1 change: 1 addition & 0 deletions php/tests/vaas/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="VaasTesting" >
<file>StreamsInLoopTest.php</file>
<file>Sha256Test.php</file>
<file>VaasTest.php</file>
<file>ProtocolTest.php</file>
Expand Down

0 comments on commit 42c969c

Please sign in to comment.