File_Bittorrent
[ class tree: File_Bittorrent ] [ index: File_Bittorrent ] [ all elements ]

Source for file FileBittorrent.php

Documentation is available at FileBittorrent.php

  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.     /**
  26.     * Test for File_Bittorrent
  27.     *
  28.     * @package File_Bittorrent
  29.     * @subpackage Test
  30.     * @category File
  31.     * @author Markus Tacker <m@tacker.org>
  32.     * @author Robin H. Johnson <robbat2@gentoo.org>
  33.     * @version $Id: FileBittorrent.php 72 2007-03-13 14:53:28Z m $
  34.     */
  35.  
  36.     require_once 'PHPUnit/Framework/TestCase.php';
  37.     require_once 'File/Bittorrent/Decode.php';
  38.  
  39.     /**
  40.     * Test for File_Bittorrent
  41.     *
  42.     * @package File_Bittorrent
  43.     * @subpackage Test
  44.     * @category File
  45.     * @author Markus Tacker <m@tacker.org>
  46.     * @author Robin H. Johnson <robbat2@gentoo.org>
  47.     * @version $Id: FileBittorrent.php 72 2007-03-13 14:53:28Z m $
  48.     */
  49.     class Tests_FileBittorrent extends PHPUnit_Framework_TestCase
  50.     {
  51.         public static $torrent './install-x86-universal-2005.0.iso.torrent';
  52.  
  53.         public function testInfoHash()
  54.         {
  55.             $File_Bittorrent_Decode = new File_Bittorrent_Decode;
  56.             $File_Bittorrent_Decode->decodeFile(self::$torrent);
  57.             exec('torrentinfo-console ' escapeshellarg(self::$torrent)$bt);
  58.             $this->assertEquals($File_Bittorrent_Decode->info_hashsubstr($bt[3]strpos($bt[3]':'+ 2));
  59.         }
  60.  
  61.         public function testDecode()
  62.         {
  63.             $test_data = array(
  64.                 '0:0:'                     => false// data past end of first correct bencoded string
  65.                 'ie'                       => false// empty integer
  66.                 'i341foo382e'              => false// malformed integer
  67.                 'i4e'                      => 4,
  68.                 'i0e'                      => 0,
  69.                 'i123456789e'              => 123456789,
  70.                 'i-10e'                    => -10,
  71.                 'i-0e'                     => false// negative zero integer
  72.                 'i123'                     => false// unterminated integer
  73.                 ''                         => false// empty string
  74.                 'i6easd'                   => false// integer with trailing garbage
  75.                 '35208734823ljdahflajhdf'  => false// garbage looking vaguely like a string, with large count
  76.                 '2:abfdjslhfld'            => false// string with trailing garbage
  77.                 '0:'                       => '',
  78.                 '3:abc'                    => 'abc',
  79.                 '10:1234567890'            => '1234567890',
  80.                 '02:xy'                    => false// string with extra leading zero in count
  81.                 'l'                        => false// unclosed empty list
  82.                 'le'                       => array(),
  83.                 'leanfdldjfh'              => false// empty list with trailing garbage
  84.                 'l0:0:0:e'                 => array(''''''),
  85.                 'relwjhrlewjh'             => false// complete garbage
  86.                 'li1ei2ei3ee'              => array12),
  87.                 'l3:asd2:xye'              => array'asd''xy' ),
  88.                 'll5:Alice3:Bobeli2ei3eee' => array array'Alice''Bob' )array2) ),
  89.                 'd'                        => false// unclosed empty dict
  90.                 'defoobar'                 => false// empty dict with trailing garbage
  91.                 'de'                       => array(),
  92.                 'd1:a0:e'                  => array('a'=>''),
  93.                 'd3:agei25e4:eyes4:bluee'  => array('age' => 25'eyes' => 'blue' ),
  94.                 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee' => array('spam.mp3' => array'author' => 'Alice''length' => 100000 )),
  95.                 'd3:fooe'                  => false// dict with odd number of elements
  96.                 'di1e0:e'                  => false// dict with integer key
  97.                 'd1:b0:1:a0:e'             => false// missorted keys
  98.                 'd1:a0:1:a0:e'             => false// duplicate keys
  99.                 'i03e'                     => false// integer with leading zero
  100.                 'l01:ae'                   => false// list with string with leading zero in count
  101.                 '9999:x'                   => false// string shorter than count
  102.                 'l0:'                      => false// unclosed list with content
  103.                 'd0:0:'                    => false// unclosed dict with content
  104.                 'd0:'                      => false// unclosed dict with odd number of elements
  105.                 '00:'                      => false// zero-length string with extra leading zero in count
  106.                 'l-3:e'                    => false// list with negative-length string
  107.                 'i-03e'                    => false// negative integer with leading zero
  108.                 'di0e0:e'                  => false// dictionary with integer key
  109.                 'd8:announceldi0e0:eee'    => false// nested dictionary with integer key
  110.                 'd8:announcedi0e0:e18:azureus_propertiesi0ee' => false// nested dictionary with integer key #2
  111.             );
  112.             // Thanks to IsoHunt.com for the last 3 testcases of bad data seen in their system.
  113.  
  114.             $File_Bittorrent_Decode = new File_Bittorrent_Decode;
  115.             ini_set('mbstring.internal_encoding','ASCII');
  116.             foreach($test_data as $ti => $to{
  117.                 $this->assertEquals($to$File_Bittorrent_Decode->decode($ti));
  118.             }
  119.         }
  120.     }
  121.  
  122. ?>

Documentation generated on Tue, 13 Mar 2007 10:00:13 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.