From 917cf865a94f672d8b83af68ce693de3309ee3e5 Mon Sep 17 00:00:00 2001 From: itismadness Date: Mon, 12 Oct 2020 00:27:44 +0000 Subject: [PATCH] allow empty components on file path so long as not totally empty --- src/BencodeTorrent.php | 4 ++-- tests/BencodeTorrentTest.php | 41 ++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/BencodeTorrent.php b/src/BencodeTorrent.php index 311fe5a..2a91e87 100644 --- a/src/BencodeTorrent.php +++ b/src/BencodeTorrent.php @@ -105,8 +105,8 @@ function ($element) { return strlen($element) === 0; } ); - if (count($filter) > 0) { - throw new \RuntimeException('Cannot have empty path for a file'); + if (count($filter) === count($file[$path_key]) && $file['length'] !== 0) { + throw new \RuntimeException('Cannot have a file with no path'); } } } diff --git a/tests/BencodeTorrentTest.php b/tests/BencodeTorrentTest.php index 1c6b617..e9eaa95 100644 --- a/tests/BencodeTorrentTest.php +++ b/tests/BencodeTorrentTest.php @@ -465,7 +465,7 @@ public function testGetEncodeNoData(): void $bencode->getEncode(); } - public function testInvalidPath(): void + public function testEmptyPathPart(): void { $data = [ 'encoding' => 'UTF8', @@ -473,7 +473,44 @@ public function testInvalidPath(): void 'files' => [ 0 => [ 'length' => 1234, - 'path' => [''] + 'path' => [ + '', + '01 - test test', + ] + ] + ], + 'name' => 'test', + 'length' => 1213134, + 'pieces' => 'fake pieces string' + ] + ]; + $bencode = new BencodeTorrent(); + $bencode->setData($data); + $this->assertSame( + [ + 'total_size' => 1234, + 'files' => [ + 0 => [ + 'path' => '/01 - test test', + 'size' => 1234, + ], + ], + ], + $bencode->getFileList() + ); + } + + public function testEmptyPath(): void + { + $data = [ + 'encoding' => 'UTF8', + 'info' => [ + 'files' => [ + 0 => [ + 'length' => 1234, + 'path' => [ + '', + ] ] ], 'name' => 'test',