From 14d67ad49f6efc232ac0cfe7e1cdcd9a7a4f2981 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 4 Oct 2020 13:42:58 -0400 Subject: [PATCH] Add fuzz test for backwards seeking Test data is 1025 random bytes; gb18030 still fails --- tests/cases/Encoding/TestBig5.php | 8 ++++++++ tests/cases/Encoding/TestEUCKR.php | 8 ++++++++ tests/cases/Encoding/TestGB18030.php | 8 ++++++++ tests/cases/Encoding/TestSingleByte.php | 10 ++++++++++ tests/cases/Encoding/TestUTF8.php | 8 ++++++++ tests/lib/DecoderTest.php | 17 +++++++++++++++++ 6 files changed, 59 insertions(+) diff --git a/tests/cases/Encoding/TestBig5.php b/tests/cases/Encoding/TestBig5.php index 8bfeef3..b7b1fce 100644 --- a/tests/cases/Encoding/TestBig5.php +++ b/tests/cases/Encoding/TestBig5.php @@ -136,6 +136,14 @@ class TestBig5 extends \MensBeam\Intl\Test\CoderDecoderTest { return parent::testIterateThroughAStringAllowingSurrogates($input, $strictExp, $relaxedExp); } + + /** + * @covers MensBeam\Intl\Encoding\Big5::seekBack + */ + public function testSeekBackOverRandomData() { + return parent::testSeekBackOverRandomData(); + } + public function provideCodePoints() { return [ 'U+0064 (HTML)' => [false, 0x64, "64"], diff --git a/tests/cases/Encoding/TestEUCKR.php b/tests/cases/Encoding/TestEUCKR.php index 1ea5f94..55c1268 100644 --- a/tests/cases/Encoding/TestEUCKR.php +++ b/tests/cases/Encoding/TestEUCKR.php @@ -136,6 +136,14 @@ class TestEUCKR extends \MensBeam\Intl\Test\CoderDecoderTest { return parent::testIterateThroughAStringAllowingSurrogates($input, $strictExp, $relaxedExp); } + + /** + * @covers MensBeam\Intl\Encoding\EUCKR::seekBack + */ + public function testSeekBackOverRandomData() { + return parent::testSeekBackOverRandomData(); + } + public function provideCodePoints() { return [ 'U+0064 (HTML)' => [false, 0x64, "64"], diff --git a/tests/cases/Encoding/TestGB18030.php b/tests/cases/Encoding/TestGB18030.php index 63de0ed..e151ce5 100644 --- a/tests/cases/Encoding/TestGB18030.php +++ b/tests/cases/Encoding/TestGB18030.php @@ -196,6 +196,14 @@ class TestGB18030 extends \MensBeam\Intl\Test\CoderDecoderTest { } } + + /** + * @covers MensBeam\Intl\Encoding\GB18030::seekBack + */ + public function testSeekBackOverRandomData() { + return parent::testSeekBackOverRandomData(); + } + public function provideStrings() { return [ 'empty string' => ["", []], diff --git a/tests/cases/Encoding/TestSingleByte.php b/tests/cases/Encoding/TestSingleByte.php index b0cdb4d..598c2ad 100644 --- a/tests/cases/Encoding/TestSingleByte.php +++ b/tests/cases/Encoding/TestSingleByte.php @@ -214,6 +214,16 @@ class TestSingleByte extends \MensBeam\Intl\Test\CoderDecoderTest { return parent::testIterateThroughAStringAllowingSurrogates($input, $exp, $exp); } + + /** + * @dataProvider provideClasses + * @coversNothing + */ + public function testSeekBackOverRandomData($class = null) { + $this->testedClass = $class; + return parent::testSeekBackOverRandomData(); + } + public function provideClasses() { foreach (self::$classes as $name => $class) { yield $name => [$class]; diff --git a/tests/cases/Encoding/TestUTF8.php b/tests/cases/Encoding/TestUTF8.php index 61d74be..262a67c 100644 --- a/tests/cases/Encoding/TestUTF8.php +++ b/tests/cases/Encoding/TestUTF8.php @@ -136,6 +136,14 @@ class TestUTF8 extends \MensBeam\Intl\Test\CoderDecoderTest { return parent::testIterateThroughAStringAllowingSurrogates($input, $strictExp, $relaxedExp); } + + /** + * @covers MensBeam\Intl\Encoding\UTF8::seekBack + */ + public function testSeekBackOverRandomData() { + return parent::testSeekBackOverRandomData(); + } + public function provideCodePoints() { return [ 'U+007A (HTML)' => [false, 0x7A, "7A"], diff --git a/tests/lib/DecoderTest.php b/tests/lib/DecoderTest.php index 4730420..5f74450 100644 --- a/tests/lib/DecoderTest.php +++ b/tests/lib/DecoderTest.php @@ -9,6 +9,7 @@ namespace MensBeam\Intl\Test; use MensBeam\Intl\Encoding\DecoderException; abstract class DecoderTest extends \PHPUnit\Framework\TestCase { + protected $random = "L51yGwEFuatjbZi7wgNC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F/GEbwqOf55/p1ewaZMrunFKd/Mj89qyYU5bp6mwmXSwj10psAA+qtXYm3XzRrLHKfCuiukyPEtvI+RdjbQDtMP1vF5qkmjlQLHXvEDpviJMaqvIPkjGrZkvAej1JX5yka50z0od9LLz8TIernjLLoVZ+cWtpd3kchO6w+zTpIOups4HdD66zaiPJrXIrJwi5bIgwTOWLhVs3ufZ0loFjlWWUh5FlTW+oWl1AD4h/yPBHWglqfMaTTqH75B4XEriy+Bw9k="; protected $lowerA = "a"; public function testDecodeMultipleCharactersAsCodePoints(string $input, array $exp) { @@ -289,6 +290,22 @@ abstract class DecoderTest extends \PHPUnit\Framework\TestCase { $this->iterateThroughAString($input, $exp, true); } + public function testSeekBackOverRandomData() { + $class = $this->testedClass; + $bytes = base64_decode($this->random); + $i = new $class($bytes); + $fwd = []; + do { + $fwd[] = [$i->posByte(), $i->nextCode()]; + } while ($i->posByte() < strlen($bytes)); + while (sizeof($fwd)) { + list($expPos, $expCode) = array_pop($fwd); + $this->assertSame(0, $i->seek(-1), "Start of string reached prematureley"); + $this->assertSame($expPos, $i->posByte(), "Position desynchronized"); + $this->assertSame($expCode, $i->peekCode(1)[0], "Incorrect character decoded at byte position $expPos"); + } + } + protected function iterateThroughAString(string $input, array $exp, bool $allowSurrogates) { $class = $this->testedClass; $input = $this->prepString($input);