Skip to content

Commit

Permalink
Merge pull request #119 from clue-labs/flags
Browse files Browse the repository at this point in the history
Igore excessive fopen() mode flags for WritableResourceStream
  • Loading branch information
WyriHaximus authored Nov 10, 2017
2 parents 1e8c392 + 293828e commit a179ab5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/WritableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public function __construct($stream, LoopInterface $loop, $writeBufferSoftLimit
throw new \InvalidArgumentException('First parameter must be a valid stream resource');
}

// ensure resource is opened for writing (fopen mode must contain either of "waxc+")
$meta = stream_get_meta_data($stream);
if (isset($meta['mode']) && str_replace(array('b', 't'), '', $meta['mode']) === 'r') {
if (isset($meta['mode']) && $meta['mode'] !== '' && strtr($meta['mode'], 'waxc+', '.....') === $meta['mode']) {
throw new \InvalidArgumentException('Given stream resource is not opened in write mode');
}

Expand Down
30 changes: 30 additions & 0 deletions tests/DuplexResourceStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public function testConstructor()
$conn = new DuplexResourceStream($stream, $loop);
}

/**
* @covers React\Stream\DuplexResourceStream::__construct
*/
public function testConstructorWithExcessiveMode()
{
// excessive flags are ignored for temp streams, so we have to use a file stream
$name = tempnam(sys_get_temp_dir(), 'test');
$stream = @fopen($name, 'r+eANYTHING');
unlink($name);

$loop = $this->createLoopMock();
$buffer = new DuplexResourceStream($stream, $loop);
$buffer->close();
}

/**
* @covers React\Stream\DuplexResourceStream::__construct
*/
Expand All @@ -45,6 +60,21 @@ public function testConstructorThrowsExceptionOnWriteOnlyStream()
new DuplexResourceStream(STDOUT, $loop);
}

/**
* @covers React\Stream\DuplexResourceStream::__construct
* @expectedException InvalidArgumentException
*/
public function testConstructorThrowsExceptionOnWriteOnlyStreamWithExcessiveMode()
{
// excessive flags are ignored for temp streams, so we have to use a file stream
$name = tempnam(sys_get_temp_dir(), 'test');
$stream = fopen($name, 'weANYTHING');
unlink($name);

$loop = $this->createLoopMock();
new DuplexResourceStream($stream, $loop);
}

/**
* @covers React\Stream\DuplexResourceStream::__construct
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/ReadableResourceStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ public function testConstructor()
new ReadableResourceStream($stream, $loop);
}

/**
* @covers React\Stream\ReadableResourceStream::__construct
*/
public function testConstructorWithExcessiveMode()
{
// excessive flags are ignored for temp streams, so we have to use a file stream
$name = tempnam(sys_get_temp_dir(), 'test');
$stream = @fopen($name, 'r+eANYTHING');
unlink($name);

$loop = $this->createLoopMock();
$buffer = new ReadableResourceStream($stream, $loop);
$buffer->close();
}

/**
* @covers React\Stream\ReadableResourceStream::__construct
*/
Expand All @@ -44,6 +59,21 @@ public function testConstructorThrowsExceptionOnWriteOnlyStream()
new ReadableResourceStream(STDOUT, $loop);
}

/**
* @covers React\Stream\ReadableResourceStream::__construct
* @expectedException InvalidArgumentException
*/
public function testConstructorThrowsExceptionOnWriteOnlyStreamWithExcessiveMode()
{
// excessive flags are ignored for temp streams, so we have to use a file stream
$name = tempnam(sys_get_temp_dir(), 'test');
$stream = fopen($name, 'weANYTHING');
unlink($name);

$loop = $this->createLoopMock();
new ReadableResourceStream($stream, $loop);
}

/**
* @covers React\Stream\ReadableResourceStream::__construct
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/WritableStreamResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public function testConstructor()
$buffer->on('error', $this->expectCallableNever());
}

/**
* @covers React\Stream\WritableResourceStream::__construct
*/
public function testConstructorWithExcessiveMode()
{
// excessive flags are ignored for temp streams, so we have to use a file stream
$name = tempnam(sys_get_temp_dir(), 'test');
$stream = @fopen($name, 'w+eANYTHING');
unlink($name);

$loop = $this->createLoopMock();
$buffer = new WritableResourceStream($stream, $loop);
$buffer->close();
}

/**
* @covers React\Stream\WritableResourceStream::__construct
* @expectedException InvalidArgumentException
Expand All @@ -43,6 +58,21 @@ public function testConstructorThrowsExceptionOnReadOnlyStream()
new WritableResourceStream($stream, $loop);
}

/**
* @covers React\Stream\WritableResourceStream::__construct
* @expectedException InvalidArgumentException
*/
public function testConstructorThrowsExceptionOnReadOnlyStreamWithExcessiveMode()
{
// excessive flags are ignored for temp streams, so we have to use a file stream
$name = tempnam(sys_get_temp_dir(), 'test');
$stream = fopen($name, 'reANYTHING');
unlink($name);

$loop = $this->createLoopMock();
new WritableResourceStream($stream, $loop);
}

/**
* @covers React\Stream\WritableResourceStream::__construct
*/
Expand Down

0 comments on commit a179ab5

Please sign in to comment.