private void EncodeString_ReturnTrue(string testcase) { var testbytes = GetBytes($"{testcase.Length}:{testcase}"); var bytes = BEncoding.Encode(testcase); Debug.Assert(bytes.SequenceEqual(testbytes)); }
private void EncodeInt_ReturnTrue(long testcase) { var resultBytes = Encoding.UTF8.GetBytes($"i{testcase}e"); var bytes = BEncoding.Encode(testcase); Debug.Assert(bytes.SequenceEqual(resultBytes)); }
public Torrent(string name, string location, List <FileItem> files, List <string> trackers, int pieceSize, byte[] pieceHashes = null, int blockSize = 16384, bool?isPrivate = false) { Name = name; DownloadDirectory = location; Files = files; fileWriteLocks = new object[Files.Count]; for (int i = 0; i < this.Files.Count; i++) { fileWriteLocks[i] = new object(); } if (trackers != null) { foreach (string url in trackers) { Tracker tracker = new Tracker(url); Trackers.Add(tracker); tracker.PeerListUpdated += HandlePeerListUpdated; } } PieceSize = pieceSize; BlockSize = blockSize; IsPrivate = isPrivate; int count = Convert.ToInt32(Math.Ceiling(TotalSize / Convert.ToDouble(PieceSize))); PieceHashes = new byte[count][]; IsPieceVerified = new bool[count]; IsBlockAcquired = new bool[count][]; for (int i = 0; i < PieceCount; i++) { IsBlockAcquired[i] = new bool[GetBlockCount(i)]; } if (pieceHashes == null) { // this is a new torrent so we have to create the hashes from the files for (int i = 0; i < PieceCount; i++) { PieceHashes[i] = GetHash(i); } } else { for (int i = 0; i < PieceCount; i++) { PieceHashes[i] = new byte[20]; Buffer.BlockCopy(pieceHashes, i * 20, PieceHashes[i], 0, 20); } } object info = TorrentInfoToBEncodingObject(this); byte[] bytes = BEncoding.Encode(info); Infohash = SHA1.Create().ComputeHash(bytes); for (int i = 0; i < PieceCount; i++) { Verify(i); } }
private void EncodeDictionary_ReturnTrue(IDictionary <string, object> input, string expected) { var encoded = GetString(BEncoding.Encode(input)); Debug.Assert(encoded == expected); }
private void EncodeList_Return_True(IList <object> list, string expected) { var returnResult = GetString(BEncoding.Encode(list)); Debug.Assert(returnResult == expected); }