public void StartEndIndices4()
        {
            int pieceLength = 32 * 32;

            ITorrentFile[] files = TorrentFile.Create(pieceLength,
                                                      ("File0", pieceLength - 10),
                                                      ("File1", 11)
                                                      );
            Torrent t = TestRig.CreateMultiFileTorrent(files, pieceLength);

            Assert.AreEqual(0, t.Files[0].StartPieceIndex, "#1");
            Assert.AreEqual(0, t.Files[0].EndPieceIndex, "#2");

            Assert.AreEqual(0, t.Files[1].StartPieceIndex, "#3");
            Assert.AreEqual(1, t.Files[1].EndPieceIndex, "#4");
        }
Example #2
0
        public void StartEndIndices4()
        {
            int pieceLength = 32 * 32;

            TorrentFile[] files = new TorrentFile[] {
                new TorrentFile("File0", pieceLength - 10),
                new TorrentFile("File1", 11)
            };
            Torrent t = MonoTorrent.Client.TestRig.CreateMultiFile(files, pieceLength).Torrent;

            Assert.AreEqual(0, t.Files[0].StartPieceIndex, "#1");
            Assert.AreEqual(0, t.Files[0].EndPieceIndex, "#2");

            Assert.AreEqual(0, t.Files[1].StartPieceIndex, "#3");
            Assert.AreEqual(1, t.Files[1].EndPieceIndex, "#4");
        }
Example #3
0
        public void CreateSingleTest()
        {
            foreach (var v in announces)
            {
                creator.Announces.Add(v);
            }

            TorrentFile f = new TorrentFile(Path.GetFileName(files[0].Path),
                                            files[0].Length,
                                            files[0].StartPieceIndex,
                                            files[0].EndPieceIndex);

            BEncodedDictionary dict    = creator.Create(f.Path, new List <TorrentFile> (new TorrentFile[] { f }));
            Torrent            torrent = Torrent.Load(dict);

            VerifyCommonParts(torrent);
            Assert.AreEqual(1, torrent.Files.Length, "#1");
            Assert.AreEqual(f, torrent.Files[0], "#2");
        }
Example #4
0
        BEncodedValue ToFileInfoDict(TorrentFile file)
        {
            BEncodedDictionary fileDict = new BEncodedDictionary();

            BEncodedList filePath = new BEncodedList();

            string [] splittetPath = file.Path.Split(new char [] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in splittetPath)
            {
                filePath.Add(new BEncodedString(s));
            }

            fileDict ["length"] = new BEncodedNumber(file.Length);
            fileDict ["path"]   = filePath;
            if (file.MD5 != null)
            {
                fileDict ["md5sum"] = (BEncodedString)file.MD5;
            }

            return(fileDict);
        }
Example #5
0
        ///<summary>
        ///this method is used for multi file mode torrents to return a dictionary with
        ///file relevant informations.
        ///<param name="file">the file to report the informations for</param>
        ///<param name="basePath">used to subtract the absolut path information</param>
        ///</summary>
        BEncodedDictionary GetFileInfoDict(TorrentFile file)
        {
            var fileDict = new BEncodedDictionary();

            fileDict.Add("length", new BEncodedNumber(file.Length));

            var filePath     = new BEncodedList();
            var splittetPath = file.Path.Split(System.IO.Path.DirectorySeparatorChar);

            foreach (var s in splittetPath)
            {
                if (s.Length > 0)                 //exclude empties
                {
                    filePath.Add(new BEncodedString(s));
                }
            }

            fileDict.Add("path", filePath);

            return(fileDict);
        }
Example #6
0
        public void StartEndIndices()
        {
            int pieceLength = 32 * 32;

            TorrentFile[] files = new TorrentFile[] {
                new TorrentFile("File0", 0),
                new TorrentFile("File1", pieceLength),
                new TorrentFile("File2", 0),
                new TorrentFile("File3", pieceLength - 1),
                new TorrentFile("File4", 1),
                new TorrentFile("File5", 236),
                new TorrentFile("File6", pieceLength * 7)
            };
            Torrent t = MonoTorrent.Client.TestRig.CreateMultiFile(files, pieceLength).Torrent;

            Assert.AreEqual(0, t.Files[0].StartPieceIndex, "#0a");
            Assert.AreEqual(0, t.Files[0].EndPieceIndex, "#0b");

            Assert.AreEqual(0, t.Files[1].StartPieceIndex, "#1");
            Assert.AreEqual(0, t.Files[1].EndPieceIndex, "#2");

            Assert.AreEqual(0, t.Files[2].StartPieceIndex, "#3");
            Assert.AreEqual(0, t.Files[2].EndPieceIndex, "#4");

            Assert.AreEqual(1, t.Files[3].StartPieceIndex, "#5");
            Assert.AreEqual(1, t.Files[3].EndPieceIndex, "#6");

            Assert.AreEqual(1, t.Files[4].StartPieceIndex, "#7");
            Assert.AreEqual(1, t.Files[4].EndPieceIndex, "#8");

            Assert.AreEqual(2, t.Files[5].StartPieceIndex, "#9");
            Assert.AreEqual(2, t.Files[5].EndPieceIndex, "#10");

            Assert.AreEqual(2, t.Files[6].StartPieceIndex, "#11");
            Assert.AreEqual(9, t.Files[6].EndPieceIndex, "#12");
        }
Example #7
0
        /// <summary>
        /// This method is called internally to load the information found within the "Info" section
        /// of the .torrent file
        /// </summary>
        /// <param name="dictionary">The dictionary representing the Info section of the .torrent file</param>
        void ProcessInfo(BEncodedDictionary dictionary)
        {
            metadata    = dictionary.Encode();
            pieceLength = int.Parse(dictionary["piece length"].ToString());
            LoadHashPieces(((BEncodedString)dictionary["pieces"]).TextBytes);

            foreach (var keypair in dictionary)
            {
                switch (keypair.Key.Text)
                {
                case ("source"):
                    source = keypair.Value.ToString();
                    break;

                case ("sha1"):
                    sha1 = ((BEncodedString)keypair.Value).TextBytes;
                    break;

                case ("ed2k"):
                    ed2k = ((BEncodedString)keypair.Value).TextBytes;
                    break;

                case ("publisher-url.utf-8"):
                    if (keypair.Value.ToString().Length > 0)
                    {
                        publisherUrl = keypair.Value.ToString();
                    }
                    break;

                case ("publisher-url"):
                    if ((String.IsNullOrEmpty(publisherUrl)) && (keypair.Value.ToString().Length > 0))
                    {
                        publisherUrl = keypair.Value.ToString();
                    }
                    break;

                case ("publisher.utf-8"):
                    if (keypair.Value.ToString().Length > 0)
                    {
                        publisher = keypair.Value.ToString();
                    }
                    break;

                case ("publisher"):
                    if ((String.IsNullOrEmpty(publisher)) && (keypair.Value.ToString().Length > 0))
                    {
                        publisher = keypair.Value.ToString();
                    }
                    break;

                case ("files"):
                    LoadTorrentFiles(((BEncodedList)keypair.Value));
                    break;

                case ("name.utf-8"):
                    if (keypair.Value.ToString().Length > 0)
                    {
                        name = keypair.Value.ToString();
                    }
                    break;

                case ("name"):
                    if ((String.IsNullOrEmpty(name)) && (keypair.Value.ToString().Length > 0))
                    {
                        name = keypair.Value.ToString();
                    }
                    break;

                case ("piece length"):                         // Already handled
                    break;

                case ("length"):
                    break;                             // This is a singlefile torrent

                case ("private"):
                    isPrivate = (keypair.Value.ToString() == "1") ? true : false;
                    break;

                default:
                    break;
                }
            }

            if (torrentFiles == null)             // Not a multi-file torrent
            {
                var length = long.Parse(dictionary["length"].ToString());
                size = length;
                var path = name;
                var md5  = (dictionary.ContainsKey("md5")) ? ((BEncodedString)dictionary["md5"]).TextBytes : null;
                var ed2k = (dictionary.ContainsKey("ed2k")) ? ((BEncodedString)dictionary["ed2k"]).TextBytes : null;
                var sha1 = (dictionary.ContainsKey("sha1")) ? ((BEncodedString)dictionary["sha1"]).TextBytes : null;

                torrentFiles = new TorrentFile[1];
                var endPiece = Math.Min(Pieces.Count - 1, (int)((size + (pieceLength - 1)) / pieceLength));
                torrentFiles[0] = new TorrentFile(path, length, 0, endPiece, md5, ed2k, sha1);
            }
        }