1<?php 2 3// +----------------------------------------------------------------------+ 4// | Decode and Encode data in Bittorrent format | 5// +----------------------------------------------------------------------+ 6// | Copyright (C) 2004-2006 Markus Tacker <m@tacker.org> | 7// +----------------------------------------------------------------------+ 8// | This library is free software; you can redistribute it and/or | 9// | modify it under the terms of the GNU Lesser General Public | 10// | License as published by the Free Software Foundation; either | 11// | version 2.1 of the License, or (at your option) any later version. | 12// | | 13// | This library is distributed in the hope that it will be useful, | 14// | but WITHOUT ANY WARRANTY; without even the implied warranty of | 15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 16// | Lesser General Public License for more details. | 17// | | 18// | You should have received a copy of the GNU Lesser General Public | 19// | License along with this library; if not, write to the | 20// | Free Software Foundation, Inc. | 21// | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 22// +----------------------------------------------------------------------+ 23 24 /** 25 * Test for File_Bittorrent2 26 * 27 * @package File_Bittorrent2 28 * @subpackage Test 29 * @category File 30 * @author Markus Tacker <m@tacker.org> 31 * @author Robin H. Johnson <robbat2@gentoo.org> 32 * @version $Id: FileBittorrent2.php 77 2007-08-26 09:42:22Z m $ 33 */ 34 35 require_once 'PHPUnit/Framework/TestCase.php'; 36 require_once 'File/Bittorrent2/Decode.php'; 37 38 /** 39 * Test for File_Bittorrent2 40 * 41 * @package File_Bittorrent2 42 * @subpackage Test 43 * @category File 44 * @author Markus Tacker <m@tacker.org> 45 * @author Robin H. Johnson <robbat2@gentoo.org> 46 * @version $Id: FileBittorrent2.php 77 2007-08-26 09:42:22Z m $ 47 */ 48 class Tests_FileBittorrent2 extends PHPUnit_Framework_TestCase 49 { 50 public static $torrent = './install-x86-universal-2005.0.iso.torrent'; 51 52 public function testInfoHash() 53 { 54 $File_Bittorrent2_Decode = new File_Bittorrent2_Decode; 55 $File_Bittorrent2_Decode->decodeFile(self::$torrent); 56 exec('torrentinfo-console ' . escapeshellarg(self::$torrent), $bt); 57 $this->assertEquals($File_Bittorrent2_Decode->getInfoHash(), substr($bt[3], strpos($bt[3], ':') + 2)); 58 } 59 60 public function testDecode() 61 { 62 $test_data = array( 63 '0:0:' => false, // data past end of first correct bencoded string 64 'ie' => false, // empty integer 65 'i341foo382e' => false, // malformed integer 66 'i4e' => 4, 67 'i0e' => 0, 68 'i123456789e' => 123456789, 69 'i-10e' => -10, 70 'i-0e' => false, // negative zero integer 71 'i123' => false, // unterminated integer 72 '' => false, // empty string 73 'i6easd' => false, // integer with trailing garbage 74 '35208734823ljdahflajhdf' => false, // garbage looking vaguely like a string, with large count 75 '2:abfdjslhfld' => false, // string with trailing garbage 76 '0:' => '', 77 '3:abc' => 'abc', 78 '10:1234567890' => '1234567890', 79 '02:xy' => false, // string with extra leading zero in count 80 'l' => false, // unclosed empty list 81 'le' => array(), 82 'leanfdldjfh' => false, // empty list with trailing garbage 83 'l0:0:0:e' => array('', '', ''), 84 'relwjhrlewjh' => false, // complete garbage 85 'li1ei2ei3ee' => array( 1, 2, 3 ), 86 'l3:asd2:xye' => array( 'asd', 'xy' ), 87 'll5:Alice3:Bobeli2ei3eee' => array ( array( 'Alice', 'Bob' ), array( 2, 3 ) ), 88 'd' => false, // unclosed empty dict 89 'defoobar' => false, // empty dict with trailing garbage 90 'de' => array(), 91 'd1:a0:e' => array('a'=>''), 92 'd3:agei25e4:eyes4:bluee' => array('age' => 25, 'eyes' => 'blue' ), 93 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee' => array('spam.mp3' => array( 'author' => 'Alice', 'length' => 100000 )), 94 'd3:fooe' => false, // dict with odd number of elements 95 'di1e0:e' => false, // dict with integer key 96 'd1:b0:1:a0:e' => false, // missorted keys 97 'd1:a0:1:a0:e' => false, // duplicate keys 98 'i03e' => false, // integer with leading zero 99 'l01:ae' => false, // list with string with leading zero in count 100 '9999:x' => false, // string shorter than count 101 'l0:' => false, // unclosed list with content 102 'd0:0:' => false, // unclosed dict with content 103 'd0:' => false, // unclosed dict with odd number of elements 104 '00:' => false, // zero-length string with extra leading zero in count 105 'l-3:e' => false, // list with negative-length string 106 'i-03e' => false, // negative integer with leading zero 107 'di0e0:e' => false, // dictionary with integer key 108 'd8:announceldi0e0:eee' => false, // nested dictionary with integer key 109 'd8:announcedi0e0:e18:azureus_propertiesi0ee' => false, // nested dictionary with integer key #2 110 ); 111 // Thanks to IsoHunt.com for the last 3 testcases of bad data seen in their system. 112 113 $File_Bittorrent2_Decode = new File_Bittorrent2_Decode; 114 ini_set('mbstring.internal_encoding','ASCII'); 115 foreach($test_data as $ti => $to) { 116 if ($to === false) { 117 try { 118 $File_Bittorrent2_Decode->decode($ti); 119 $this->fail('File_Bittorrent2 successfully decoded invalid data.'); 120 } catch (File_Bittorrent2_Exception $E) { 121 if ($E->getCode() != File_Bittorrent2_Exception::decode) throw $E; 122 } 123 } else { 124 $this->assertEquals($to, $File_Bittorrent2_Decode->decode($ti)); 125 } 126 } 127 } 128 } 129 130?> 131