mirror of
https://github.com/OPSnet/bencode-torrent.git
synced 2026-11-10 03:51:24 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
911e2899f4 | ||
|
|
dd0243afeb | ||
|
|
da95cbacce | ||
|
|
c0e4100135 | ||
|
|
d634179b9b | ||
|
|
007c0fe9f3 | ||
|
|
d95a0c1f8c | ||
|
|
6b9d56bfc7 | ||
|
|
5b94cf1056 | ||
|
|
74929fdac8 |
No files matched your search
@@ -6,8 +6,14 @@ php:
|
||||
- '7.0'
|
||||
- '7.1'
|
||||
- '7.2'
|
||||
- '7.3'
|
||||
- nightly
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
allow_failures:
|
||||
- php: nightly
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.composer/cache
|
||||
|
||||
@@ -1,8 +1,25 @@
|
||||
BEncode Torrent
|
||||
===============
|
||||
|
||||
PHP library for encoding and decoding BitTorrent BEncode data, focused around
|
||||
[Gazelle](https://github.com/ApolloRIP/Gazelle).
|
||||
[[](https://travis-ci.org/OPSnet/bencode-torrent)
|
||||
[](https://packagist.org/packages/orpheusnet/bencode-torrent)
|
||||

|
||||
|
||||
PHP library for encoding and decoding BitTorrent BEncode data, focused around usage within
|
||||
[Gazelle](https://github.com/OPSnet/Gazelle).
|
||||
|
||||
Usage
|
||||
-----
|
||||
`composer require orpheusnet/bencode-torrent`
|
||||
|
||||
```php
|
||||
use OrpheusNET\BencodeTorrent;
|
||||
$bencode = new Bencode();
|
||||
$bencode->decodeFile('path/to/file.torrent');
|
||||
```
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
BEncode is the encoding used by BitTorrent to store and transmitting loosely structured data. It supports
|
||||
* byte strings
|
||||
@@ -23,7 +40,7 @@ As such, this library will make some checks when loading data that these mandato
|
||||
raised. More information on these fields can be found at
|
||||
[BitTorrentSpecification#Metainfo_File_Structure](https://wiki.theory.org/index.php/BitTorrentSpecification#Metainfo_File_Structure).
|
||||
|
||||
Finally, this library is primarily aimed at being used within the [Gazelle](https://github.com/ApolloRIP/Gazelle) so
|
||||
Finally, this library is primarily aimed at being used within the [Gazelle](https://github.com/OPSnet/Gazelle) so
|
||||
we have some utility functions within the library that make sense there to accomplish the following things:
|
||||
* Ensuring torrent files are marked as 'private'
|
||||
* Setting a 'source' on torrents (to ensure unique info hash)
|
||||
@@ -32,5 +49,5 @@ we have some utility functions within the library that make sense there to accom
|
||||
|
||||
This is based (loosely) off the code in the two separate BEncode libraries within WCD's Gazelle
|
||||
([bencodetorrent.class.php](https://github.com/WhatCD/Gazelle/blob/master/classes/bencodetorrent.class.php) and
|
||||
[torrent.class.php](https://github.com/WhatCD/Gazelle/blob/master/classes/torrent.class.php)), but without the
|
||||
necessary 32bit shims as well as make it a unified library used for both uploading and downloading the torrent files.
|
||||
[torrent.class.php](https://github.com/WhatCD/Gazelle/blob/master/classes/torrent.class.php)), but without the now
|
||||
unnecessary 32bit shims as well as making it a unified library used for both uploading and downloading the torrent files.
|
||||
+8
-7
@@ -1,24 +1,25 @@
|
||||
{
|
||||
"type": "library",
|
||||
"name": "apollorip/bencode-torrent",
|
||||
"name": "orpheusnet/bencode-torrent",
|
||||
"description": "PHP Library for decoding and encoding BitTorrent BEncoded data, built for Gazelle",
|
||||
"license": "Unlicense",
|
||||
"authors": [
|
||||
{
|
||||
"name": "itismadness",
|
||||
"email": "[email protected]"
|
||||
"name": "Orpheus",
|
||||
"email": "[email protected]"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"ApolloRIP\\BencodeTorrent\\": "src/"
|
||||
"OrpheusNET\\BencodeTorrent\\": "src/"
|
||||
}
|
||||
},
|
||||
"require": {},
|
||||
"require": {
|
||||
"php": "^7"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6",
|
||||
"phpunit/phpunit": "^6|^7",
|
||||
"squizlabs/php_codesniffer": "3.*",
|
||||
"php": "^7",
|
||||
"ext-mbstring": "^7"
|
||||
}
|
||||
}
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
|
||||
namespace OrpheusNET\BencodeTorrent;
|
||||
|
||||
class Bencode {
|
||||
protected $data = null;
|
||||
|
||||
/**
|
||||
* Sets the internal data array
|
||||
* @param mixed $data
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function setData($data) {
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a BEncoded string and decode it
|
||||
* @param string $data
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function decodeString(string $data) {
|
||||
$this->data = $this->decode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a path to a file, decode the contents of it
|
||||
*
|
||||
* @param string $path
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function decodeFile(string $path) {
|
||||
$this->data = $this->decode(file_get_contents($path, FILE_BINARY));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a BEncoded string to the following values:
|
||||
* - Dictionary (starts with d, ends with e)
|
||||
* - List (starts with l, ends with e
|
||||
* - Integer (starts with i, ends with e
|
||||
* - String (starts with number denoting number of characters followed by : and then the string)
|
||||
*
|
||||
* @see https://wiki.theory.org/index.php/BitTorrentSpecification
|
||||
*
|
||||
* @param string $data
|
||||
* @param int $pos
|
||||
* @return mixed
|
||||
*/
|
||||
protected function decode(string $data, int &$pos = 0) {
|
||||
$start_decode = $pos === 0;
|
||||
if ($data[$pos] === 'd') {
|
||||
$pos++;
|
||||
$return = [];
|
||||
while ($data[$pos] !== 'e') {
|
||||
$key = $this->decode($data, $pos);
|
||||
$value = $this->decode($data, $pos);
|
||||
if ($key === null || $value === null) {
|
||||
break;
|
||||
}
|
||||
if (!is_string($key)) {
|
||||
throw new \RuntimeException('Invalid key type, must be string: '.gettype($key));
|
||||
}
|
||||
$return[$key] = $value;
|
||||
}
|
||||
ksort($return);
|
||||
$pos++;
|
||||
}
|
||||
elseif ($data[$pos] === 'l') {
|
||||
$pos++;
|
||||
$return = [];
|
||||
while ($data[$pos] !== 'e') {
|
||||
$value = $this->decode($data, $pos);
|
||||
$return[] = $value;
|
||||
}
|
||||
$pos++;
|
||||
}
|
||||
elseif ($data[$pos] === 'i') {
|
||||
$pos++;
|
||||
$digits = strpos($data, 'e', $pos) - $pos;
|
||||
$return = substr($data, $pos, $digits);
|
||||
if ($return === '-0') {
|
||||
throw new \RuntimeException('Cannot have integer value -0');
|
||||
}
|
||||
$multiplier = 1;
|
||||
if ($return[0] === '-') {
|
||||
$multiplier = -1;
|
||||
$return = substr($return, 1);
|
||||
}
|
||||
if (!ctype_digit($return)) {
|
||||
$msg = 'Cannot have non-digit values in integer number: '.$return;
|
||||
throw new \RuntimeException($msg);
|
||||
}
|
||||
$return = $multiplier * ((int) $return);
|
||||
$pos += $digits + 1;
|
||||
}
|
||||
else {
|
||||
$digits = strpos($data, ':', $pos) - $pos;
|
||||
$len = (int) substr($data, $pos, $digits);
|
||||
$pos += ($digits + 1);
|
||||
$return = substr($data, $pos, $len);
|
||||
$pos += $len;
|
||||
}
|
||||
if ($start_decode) {
|
||||
if ($pos !== strlen($data)) {
|
||||
throw new \RuntimeException('Could not fully decode bencode string');
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the internal data array
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData() {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
protected function hasData() {
|
||||
if ($this->data === null) {
|
||||
throw new \RuntimeException('Must decode proper bencode string first');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEncode() : string {
|
||||
$this->hasData();
|
||||
return $this->encodeVal($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
protected function encodeVal($data) : string {
|
||||
if (is_array($data)) {
|
||||
$return = '';
|
||||
$check = -1;
|
||||
$list = true;
|
||||
foreach ($data as $key => $value) {
|
||||
if ($key !== ++$check) {
|
||||
$list = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($list) {
|
||||
$return .= 'l';
|
||||
foreach ($data as $value) {
|
||||
$return .= $this->encodeVal($value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$return .= 'd';
|
||||
foreach ($data as $key => $value) {
|
||||
$return .= $this->encodeVal(strval($key));
|
||||
$return .= $this->encodeVal($value);
|
||||
}
|
||||
}
|
||||
$return .= 'e';
|
||||
}
|
||||
elseif (is_integer($data)) {
|
||||
$return = 'i'.$data.'e';
|
||||
}
|
||||
else {
|
||||
$return = strlen($data) . ':' . $data;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
+31
-144
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace ApolloRip\BencodeTorrent;
|
||||
namespace OrpheusNET\BencodeTorrent;
|
||||
|
||||
/**
|
||||
* BEncode service that allows us to encode PHP objects into BEncode and decode
|
||||
@@ -24,12 +24,10 @@ namespace ApolloRip\BencodeTorrent;
|
||||
* For Gazelle, this also acts as a unification of the two original BEncode implementations
|
||||
* which were both used in separate areas of the codebase.
|
||||
*/
|
||||
class BencodeTorrent {
|
||||
class BencodeTorrent extends Bencode {
|
||||
const FILELIST_DELIM = 0xF7;
|
||||
private static $utf8_filelist_delim = null;
|
||||
|
||||
private $data;
|
||||
|
||||
public function __construct() {
|
||||
$this->setDelim();
|
||||
}
|
||||
@@ -50,8 +48,8 @@ class BencodeTorrent {
|
||||
* @param array $data
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function setData(array $data) {
|
||||
$this->data = $data;
|
||||
public function setData($data) {
|
||||
parent::setData($data);
|
||||
$this->validate();
|
||||
}
|
||||
|
||||
@@ -61,7 +59,7 @@ class BencodeTorrent {
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function decodeString(string $data) {
|
||||
$this->data = $this->decode($data);
|
||||
parent::decodeString($data);
|
||||
$this->validate();
|
||||
}
|
||||
|
||||
@@ -72,76 +70,18 @@ class BencodeTorrent {
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function decodeFile(string $path) {
|
||||
$this->data = $this->decode(file_get_contents($path, FILE_BINARY));
|
||||
parent::decodeFile($path);
|
||||
$this->validate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a BEncoded string to the following values:
|
||||
* - Dictionary (starts with d, ends with e)
|
||||
* - List (starts with l, ends with e
|
||||
* - Integer (starts with i, ends with e
|
||||
* - String (starts with number denoting number of characters followed by : and then the string)
|
||||
*
|
||||
* @see https://wiki.theory.org/index.php/BitTorrentSpecification
|
||||
*
|
||||
* @param string $data
|
||||
* @param int $pos
|
||||
* @return array|bool|float|string
|
||||
*/
|
||||
private function decode(string $data, int &$pos = 0) {
|
||||
if ($data[$pos] === 'd') {
|
||||
$pos++;
|
||||
$return = [];
|
||||
while ($data[$pos] !== 'e') {
|
||||
$key = $this->decode($data, $pos);
|
||||
$value = $this->decode($data, $pos);
|
||||
if (empty($key) || empty($value)) {
|
||||
break;
|
||||
}
|
||||
$return[$key] = $value;
|
||||
}
|
||||
ksort($return);
|
||||
$pos++;
|
||||
}
|
||||
elseif ($data[$pos] === 'l') {
|
||||
$pos++;
|
||||
$return = [];
|
||||
while ($data[$pos] !== 'e') {
|
||||
$value = $this->decode($data, $pos);
|
||||
$return[] = $value;
|
||||
}
|
||||
$pos++;
|
||||
}
|
||||
elseif ($data[$pos] === 'i') {
|
||||
$pos++;
|
||||
$digits = strpos($data, 'e', $pos) - $pos;
|
||||
$return = (int) substr($data, $pos, $digits);
|
||||
$pos += $digits + 1;
|
||||
}
|
||||
else {
|
||||
$digits = strpos($data, ':', $pos) - $pos;
|
||||
$len = (int) substr($data, $pos, $digits);
|
||||
$pos += ($digits + 1);
|
||||
$return = substr($data, $pos, $len);
|
||||
$pos += $len;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the internal data array
|
||||
* @return array
|
||||
*/
|
||||
public function getData() : array {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that the internal data array
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function validate() {
|
||||
if (!is_array($this->data)) {
|
||||
throw new \TypeError('Data must be an array');
|
||||
}
|
||||
if (empty($this->data['info'])) {
|
||||
throw new \RuntimeException("Torrent dictionary doesn't have info key");
|
||||
}
|
||||
@@ -163,63 +103,6 @@ class BencodeTorrent {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
private function hasData() {
|
||||
if (empty($this->data) || !is_array($this->data)) {
|
||||
throw new \RuntimeException('Must decode proper bencode string first');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEncode() : string {
|
||||
$this->hasData();
|
||||
return $this->encodeVal($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
private function encodeVal($data) : string {
|
||||
if (is_array($data)) {
|
||||
$return = '';
|
||||
$check = -1;
|
||||
$list = true;
|
||||
foreach ($data as $key => $value) {
|
||||
if ($key !== ++$check) {
|
||||
$list = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($list) {
|
||||
$return .= 'l';
|
||||
foreach ($data as $value) {
|
||||
$return .= $this->encodeVal($value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$return .= 'd';
|
||||
foreach ($data as $key => $value) {
|
||||
$return .= $this->encodeVal(strval($key));
|
||||
$return .= $this->encodeVal($value);
|
||||
}
|
||||
}
|
||||
$return .= 'e';
|
||||
}
|
||||
elseif (is_integer($data)) {
|
||||
$return = 'i'.$data.'e';
|
||||
}
|
||||
else {
|
||||
$return = strlen($data) . ':' . $data;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to clean out keys in the data and info dictionaries that we don't need in
|
||||
* our torrent file when we go to store it in the DB or serve it up to the user (with the
|
||||
@@ -426,27 +309,31 @@ class BencodeTorrent {
|
||||
$this->data['info']['name.utf-8'] :
|
||||
$this->data['info']['name']);
|
||||
$size = $this->data['info']['length'];
|
||||
$files[] = array('name' => $name, 'size' => $size);
|
||||
$files[] = ['path' => $name, 'size' => $size];
|
||||
}
|
||||
else {
|
||||
$path_key = isset($this->data['info']['files'][0]['path.utf-8']) ?
|
||||
'path.utf-8' :
|
||||
'path';
|
||||
$size = 0;
|
||||
foreach ($this->data['info']['files'] as $file) {
|
||||
$tmp_path = array();
|
||||
foreach ($file[$path_key] as $sub_path) {
|
||||
$tmp_path[] = $sub_path;
|
||||
}
|
||||
$files[] = array('name' => implode('/', $tmp_path), 'size' => $file['length']);
|
||||
$size += $file['length'];
|
||||
$path_key = isset($file['path.utf-8']) ? 'path.utf-8' : 'path';
|
||||
$files[] = ['path' => implode('/', $file[$path_key]), 'size' => $file['length']];
|
||||
}
|
||||
uasort(
|
||||
usort(
|
||||
$files,
|
||||
function ($a, $b) {
|
||||
return strnatcasecmp($a['name'], $b['name']);
|
||||
return strnatcasecmp($a['path'], $b['path']);
|
||||
}
|
||||
);
|
||||
}
|
||||
return $files;
|
||||
return array('total_size' => $size, 'files' => $files);
|
||||
}
|
||||
|
||||
public function hasFiles(): bool {
|
||||
return isset($this->data['info']['files']);
|
||||
}
|
||||
|
||||
public function hasEncryptedFiles(): bool {
|
||||
return isset($this->data['encrypted_files']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -461,15 +348,15 @@ class BencodeTorrent {
|
||||
*/
|
||||
public function getGazelleFileList() : array {
|
||||
$files = [];
|
||||
foreach ($this->getFileList() as $file) {
|
||||
$name = $file['name'];
|
||||
foreach ($this->getFileList()['files'] as $file) {
|
||||
$path = $file['path'];
|
||||
$size = $file['size'];
|
||||
$name = $this->makeUTF8(strtr($name, "\n\r\t", ' '));
|
||||
$ext_pos = strrpos($name, '.');
|
||||
$path = $this->makeUTF8(strtr($path, "\n\r\t", ' '));
|
||||
$ext_pos = strrpos($path, '.');
|
||||
// Should not be $ExtPos !== false. Extension-less files that start with a .
|
||||
// should not get extensions
|
||||
$ext = ($ext_pos ? trim(substr($name, $ext_pos + 1)) : '');
|
||||
$files[] = sprintf("%s s%ds %s %s", ".$ext", $size, $name, self::$utf8_filelist_delim);
|
||||
$ext = ($ext_pos ? trim(substr($path, $ext_pos + 1)) : '');
|
||||
$files[] = sprintf("%s s%ds %s %s", ".$ext", $size, $path, self::$utf8_filelist_delim);
|
||||
}
|
||||
return $files;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace OrpheusNET\BencodeTorrent;
|
||||
|
||||
class BencodeTest extends \PHPUnit\Framework\TestCase {
|
||||
public function dataProvider() {
|
||||
return [
|
||||
['i0e', 0],
|
||||
['i-1e', -1],
|
||||
['i20e', 20],
|
||||
['le', []],
|
||||
['d4:item5:valuee', ['item' => 'value']],
|
||||
['0:', ''],
|
||||
['1:a', 'a'],
|
||||
['9:123456789', '123456789']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProvider
|
||||
* @param string $bencoded_string
|
||||
* @param mixed $expected
|
||||
*/
|
||||
public function testDecodeEncode($bencoded_string, $expected) {
|
||||
$bencode = new Bencode();
|
||||
$bencode->decodeString($bencoded_string);
|
||||
$this->assertEquals($expected, $bencode->getData());
|
||||
$this->assertEquals($bencoded_string, $bencode->getEncode());
|
||||
}
|
||||
|
||||
public function testEmptyDict() {
|
||||
$bencode = new Bencode();
|
||||
$bencode->decodeString('de');
|
||||
$this->assertEquals([], $bencode->getData());
|
||||
$this->assertEquals('le', $bencode->getEncode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testInvalidDictionaryKey() {
|
||||
$bencode = new Bencode();
|
||||
$bencode->decodeString('di1e5:valuee');
|
||||
}
|
||||
|
||||
public function invalidIntegers() {
|
||||
return [['-0'], ['a'], ['1.0']];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @dataProvider invalidIntegers
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testInvalidInteger(string $value) {
|
||||
$bencode = new Bencode();
|
||||
$bencode->decodeString("i{$value}e");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testInvalidString() {
|
||||
$bencode = new Bencode();
|
||||
$bencode->decodeString('i1e0e');
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace ApolloRip\BencodeTorrent;
|
||||
namespace OrpheusNET\BencodeTorrent;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class BencodeTorrentTest extends TestCase {
|
||||
class BencodeTorrentTest extends \PHPUnit\Framework\TestCase {
|
||||
public function testLoadTorrent() {
|
||||
$bencode = new BencodeTorrent();
|
||||
try {
|
||||
@@ -92,6 +90,57 @@ class BencodeTorrentTest extends TestCase {
|
||||
$this->assertStringEqualsFile(__DIR__.'/data/test_1.torrent', $bencode->getEncode());
|
||||
$this->assertEquals('1f830103427029a88dd5fde85be74e622ee07951', $bencode->getInfoHash());
|
||||
$this->assertEquals($bencode->getInfoHash(), unpack('H*', $bencode->getHexInfoHash())[1]);
|
||||
$this->assertFalse($bencode->hasEncryptedFiles());
|
||||
$file_list = [
|
||||
'total_size' => 104916260,
|
||||
'files' => [
|
||||
[
|
||||
'size' => 9584196,
|
||||
'path' => '01 Death with Dignity.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 12310347,
|
||||
'path' => '02 Should have known better.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 8871591,
|
||||
'path' => '03 All of me wants all of you.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 7942661,
|
||||
'path' => '04 Drawn to the Blood.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 5878964,
|
||||
'path' => '05 Eugene.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 11175567,
|
||||
'path' => '06 Fourth of July.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 11367829,
|
||||
'path' => '07 The Only Thing.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 7789055,
|
||||
'path' => '08 Carrie & Lowell.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 12197480,
|
||||
'path' => '09 John My Beloved.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 6438044,
|
||||
'path' => '10 No shade in the shadow of the cross.mp3'
|
||||
],
|
||||
[
|
||||
'size' => 11360526,
|
||||
'path' => '11 Blue Bucket of Gold.mp3'
|
||||
]
|
||||
]
|
||||
];
|
||||
$this->assertEquals($file_list, $bencode->getFileList());
|
||||
}
|
||||
|
||||
public function testSetData() {
|
||||
@@ -112,7 +161,18 @@ class BencodeTorrentTest extends TestCase {
|
||||
$this->assertEquals('test', $bencode->getName());
|
||||
$this->assertFalse($bencode->isPrivate());
|
||||
$this->assertEquals(1213134, $bencode->getSize());
|
||||
$this->assertEquals([['name' => 'test', 'size' => 1213134]], $bencode->getFileList());
|
||||
$this->assertEquals(
|
||||
[
|
||||
'total_size' => 1213134,
|
||||
'files' => [
|
||||
[
|
||||
'path' => 'test',
|
||||
'size' => 1213134
|
||||
]
|
||||
]
|
||||
],
|
||||
$bencode->getFileList()
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmptyDictionary() {
|
||||
@@ -121,8 +181,9 @@ class BencodeTorrentTest extends TestCase {
|
||||
'info' => [
|
||||
'name' => 'test',
|
||||
'length' => 1213134,
|
||||
'pieces' => 'fake pieces string'
|
||||
]
|
||||
'pieces' => 'fake pieces string',
|
||||
],
|
||||
'test' => ''
|
||||
];
|
||||
$bencode = new BencodeTorrent();
|
||||
$bencode->decodeString(
|
||||
@@ -310,6 +371,7 @@ class BencodeTorrentTest extends TestCase {
|
||||
['.mp3 s1234s 01 Test??.mp3 ÷', '.mp3 s2345s 03 Test.mp3 ÷'],
|
||||
$bencode->getGazelleFileList()
|
||||
);
|
||||
$this->assertTrue($bencode->hasFiles());
|
||||
}
|
||||
|
||||
public function testGetUtf8Name() {
|
||||
@@ -326,7 +388,18 @@ class BencodeTorrentTest extends TestCase {
|
||||
$bencode->setData($data);
|
||||
$this->assertEquals('test!!', $bencode->getName());
|
||||
$this->assertEquals(12345, $bencode->getSize());
|
||||
$this->assertEquals([['name' => 'test!!', 'size' => 12345]], $bencode->getFileList());
|
||||
$this->assertEquals(
|
||||
[
|
||||
'total_size' => 12345,
|
||||
'files' => [
|
||||
[
|
||||
'path' => 'test!!',
|
||||
'size' => 12345
|
||||
]
|
||||
]
|
||||
],
|
||||
$bencode->getFileList()
|
||||
);
|
||||
$this->assertEquals(['. s12345s test!! ÷'], $bencode->getGazelleFileList());
|
||||
}
|
||||
|
||||
@@ -340,6 +413,7 @@ class BencodeTorrentTest extends TestCase {
|
||||
$bencode = new BencodeTorrent();
|
||||
$bencode->setData($data);
|
||||
$this->assertEquals(['.,!?)óÉ s1234s (test.,!?)óÉ ÷'], $bencode->getGazelleFileList());
|
||||
$this->assertFalse($bencode->hasFiles());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user