Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit d8d8ad2

Browse files
committed
Merge branch 'hotfix/148'
Close #148
2 parents 57cfe9e + 1ba673c commit d8d8ad2

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ All notable changes to this project will be documented in this file, in reverse
2828
- [#142](https://github.com/zendframework/zend-diactoros/pull/142) updates the
2929
exceptions thrown by `HeaderSecurity` to include the header name and/or
3030
value.
31+
- [#148](https://github.com/zendframework/zend-diactoros/pull/148) fixes several
32+
stream operations to ensure they raise exceptions when the internal pointer
33+
is at an invalid position.
3134

3235
## 1.3.3 - 2016-01-04
3336

src/RelativeStream.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Zend\Diactoros;
1111

1212
use Psr\Http\Message\StreamInterface;
13+
use RuntimeException;
1314

1415
/**
1516
* Class RelativeStream
@@ -131,6 +132,9 @@ public function isWritable()
131132
*/
132133
public function write($string)
133134
{
135+
if ($this->tell() < 0) {
136+
throw new RuntimeException('Invalid pointer position');
137+
}
134138
return $this->decoratedStream->write($string);
135139
}
136140

@@ -147,6 +151,9 @@ public function isReadable()
147151
*/
148152
public function read($length)
149153
{
154+
if ($this->tell() < 0) {
155+
throw new RuntimeException('Invalid pointer position');
156+
}
150157
return $this->decoratedStream->read($length);
151158
}
152159

@@ -155,6 +162,9 @@ public function read($length)
155162
*/
156163
public function getContents()
157164
{
165+
if ($this->tell() < 0) {
166+
throw new RuntimeException('Invalid pointer position');
167+
}
158168
return $this->decoratedStream->getContents();
159169
}
160170

test/RelativeStreamTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class RelativeStreamTest extends TestCase
2121
public function testToString()
2222
{
2323
$decorated = $this->prophesize('Zend\Diactoros\Stream');
24+
$decorated->tell()->willReturn(100);
2425
$decorated->seek(100, SEEK_SET)->shouldBeCalled();
2526
$decorated->getContents()->shouldBeCalled()->willReturn('foobarbaz');
2627

@@ -112,6 +113,7 @@ public function testRewind()
112113
public function testWrite()
113114
{
114115
$decorated = $this->prophesize('Zend\Diactoros\Stream');
116+
$decorated->tell()->willReturn(100);
115117
$decorated->write("foobaz")->shouldBeCalled()->willReturn(6);
116118
$stream = new RelativeStream($decorated->reveal(), 100);
117119
$ret = $stream->write("foobaz");
@@ -121,6 +123,7 @@ public function testWrite()
121123
public function testRead()
122124
{
123125
$decorated = $this->prophesize('Zend\Diactoros\Stream');
126+
$decorated->tell()->willReturn(100);
124127
$decorated->read(3)->shouldBeCalled()->willReturn("foo");
125128
$stream = new RelativeStream($decorated->reveal(), 100);
126129
$ret = $stream->read(3);
@@ -130,6 +133,7 @@ public function testRead()
130133
public function testGetContents()
131134
{
132135
$decorated = $this->prophesize('Zend\Diactoros\Stream');
136+
$decorated->tell()->willReturn(100);
133137
$decorated->getContents()->shouldBeCalled()->willReturn("foo");
134138
$stream = new RelativeStream($decorated->reveal(), 100);
135139
$ret = $stream->getContents();
@@ -144,4 +148,34 @@ public function testGetMetadata()
144148
$ret = $stream->getMetadata("bar");
145149
$this->assertEquals("foo", $ret);
146150
}
151+
152+
public function testWriteRaisesExceptionWhenPointerIsBehindOffset()
153+
{
154+
$this->setExpectedException('RuntimeException', 'Invalid pointer position');
155+
$decorated = $this->prophesize('Zend\Diactoros\Stream');
156+
$decorated->tell()->shouldBeCalled()->willReturn(0);
157+
$decorated->write("foobaz")->shouldNotBeCalled();
158+
$stream = new RelativeStream($decorated->reveal(), 100);
159+
$stream->write("foobaz");
160+
}
161+
162+
public function testReadRaisesExceptionWhenPointerIsBehindOffset()
163+
{
164+
$this->setExpectedException('RuntimeException', 'Invalid pointer position');
165+
$decorated = $this->prophesize('Zend\Diactoros\Stream');
166+
$decorated->tell()->shouldBeCalled()->willReturn(0);
167+
$decorated->read(3)->shouldNotBeCalled();
168+
$stream = new RelativeStream($decorated->reveal(), 100);
169+
$stream->read(3);
170+
}
171+
172+
public function testGetContentsRaisesExceptionWhenPointerIsBehindOffset()
173+
{
174+
$this->setExpectedException('RuntimeException', 'Invalid pointer position');
175+
$decorated = $this->prophesize('Zend\Diactoros\Stream');
176+
$decorated->tell()->shouldBeCalled()->willReturn(0);
177+
$decorated->getContents()->shouldNotBeCalled();
178+
$stream = new RelativeStream($decorated->reveal(), 100);
179+
$stream->getContents();
180+
}
147181
}

0 commit comments

Comments
 (0)