private void EncodeDecode(Message orig)
        {
            orig.Encode(buffer.AsSpan(offset));
            Message dec = PeerMessage.DecodeMessage(buffer.AsSpan(offset, orig.ByteLength), torrentData).message;

            Assert.IsTrue(orig.Equals(dec), $"orig: {orig}, new: {dec}");

            Assert.IsTrue(Toolbox.ByteMatch(orig.Encode(), PeerMessage.DecodeMessage(orig.Encode(), torrentData).message.Encode()));
        }
        public void BitfieldCorrupt()
        {
            Assert.Throws <MessageException> (() => {
                bool[] data    = { true, false, false, true, false, true, false, true, false, true, false, true, false, false, false, true };
                byte[] encoded = new BitfieldMessage(new BitField(data)).Encode();

                PeerMessage.DecodeMessage(encoded, null);
            });
        }
        public void BitFieldEncoding()
        {
            bool[] data = { true,  false, false, true,  false, true,  false, true,  false, true,
                            false, true,  false, false, false, true,  true,  true,  false, false,
                            false, true,  false, true,  false, false, true,  false, true,  false,
                            true,  true,  false, false, true,  false, false, true,  true,  false };

            Assert.AreEqual(data.Length, (int)Math.Ceiling((double)torrentData.Size / torrentData.PieceLength), "#0");
            byte[] encoded = new BitfieldMessage(new BitField(data)).Encode();

            BitfieldMessage m = (BitfieldMessage)PeerMessage.DecodeMessage(encoded, torrentData).message;

            Assert.AreEqual(data.Length, m.BitField.Length, "#1");
            for (int i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], m.BitField[i], "#2." + i);
            }
        }
        public void BitFieldDecoding()
        {
            byte[]          buf = { 0x00, 0x00, 0x00, 0x04, 0x05, 0xff, 0x08, 0xAA, 0xE3, 0x00 };
            BitfieldMessage msg = (BitfieldMessage)PeerMessage.DecodeMessage(buf, torrentData).message;

            for (int i = 0; i < 8; i++)
            {
                Assert.IsTrue(msg.BitField[i], i.ToString());
            }

            for (int i = 8; i < 12; i++)
            {
                Assert.IsFalse(msg.BitField[i], i.ToString());
            }

            Assert.IsTrue(msg.BitField[12], 12.ToString ());
            for (int i = 13; i < 15; i++)
            {
                Assert.IsFalse(msg.BitField[i], i.ToString());
            }
            EncodeDecode(msg);
        }
示例#5
0
 internal Releaser(PeerMessage message)
 => (Message, ReuseId) = (message, message.ReuseId);
示例#6
0
 public void Add(PeerMessage message, Releaser releaser)
 {
     Messages.Add(message);
     Releasers.Add(releaser);
 }