public void EncodeDictionary() { BDict testDict = new BDict(); BString testString = new BString("Hello World"); BInt testInt = new BInt(5); testDict.Add("a", testString); testDict.Add("b", testInt); string test = BencodingUtils.EncodeString(testDict); Assert.AreEqual("d1:a11:Hello World1:bi5ee", test); }
public void BDictSetNullValue() { BDict dict = new BDict(); dict.Add("a", new BInt(0)); dict["a"] = null; }
public void BDictSetValue() { BDict dict = new BDict(); dict.Add("a", new BInt(0)); BInt newInt = new BInt(1); dict["a"] = newInt; Assert.AreEqual(newInt, dict["a"]); }
public void DecodeDict() { const string testString = "d1:a11:Hello Worlde"; BDict testExpected = new BDict(); testExpected.Add("a", new BString("Hello World")); IBencodingType testResult = BencodingUtils.Decode(testString); Assert.AreEqual(testExpected, testResult); }
public void SendMessage(IPeerWireClient peerWireClient, IPEndPoint[] addedEndPoints, byte[] flags, IPEndPoint[] droppedEndPoints) { if (addedEndPoints == null && droppedEndPoints == null) { return; } BDict d = new BDict(); if (addedEndPoints != null) { byte[] added = new byte[addedEndPoints.Length * 6]; for (int x = 0; x < addedEndPoints.Length; x++) { addedEndPoints[x].Address.GetAddressBytes().CopyTo(added, x * 6); BitConverter.GetBytes((ushort)addedEndPoints[x].Port).CopyTo(added, (x * 6) + 4); } d.Add("added", new BString { ByteValue = added }); } if (droppedEndPoints != null) { byte[] dropped = new byte[droppedEndPoints.Length * 6]; for (int x = 0; x < droppedEndPoints.Length; x++) { droppedEndPoints[x].Address.GetAddressBytes().CopyTo(dropped, x * 6); dropped.SetValue((ushort)droppedEndPoints[x].Port, (x * 6) + 2); } d.Add("dropped", new BString { ByteValue = dropped }); } _parent.SendExtended(peerWireClient, _parent.GetOutgoingMessageID(peerWireClient, this), BencodingUtils.EncodeBytes(d)); }
public void EqualityDict() { // Make initial dict BDict testDict = new BDict(); BString testString = new BString("Hello World"); BInt testInt = new BInt(5); testDict.Add("a", testString); testDict.Add("b", testInt); // Make test dict BDict testDict2 = new BDict(); BString testString2 = new BString("Hello World"); BInt testInt2 = new BInt(5); testDict2.Add("a", testString2); testDict2.Add("b", testInt2); // Test equality recursive Assert.AreEqual(testDict, testDict2); // Test null dict BDict nullDict = null; Assert.IsFalse(testDict.Equals(nullDict)); // Test different counts testDict2.Add("c", new BInt(10)); Assert.IsFalse(testDict.Equals(testDict2)); // Test different values testDict.Add("c", new BInt(9)); Assert.IsFalse(testDict.Equals(testDict2)); // Test missing keys testDict2.Remove("c"); testDict2.Add("d", new BInt(9)); Assert.IsFalse(testDict.Equals(testDict2)); }
public bool OnHandshake(IPeerWireClient client) { BDict handshakeDict = new BDict(); BDict mDict = new BDict(); byte i = 1; foreach (IBTExtension extension in _protocolExtensions) { _extOutgoing.Add(new ClientProtocolIDMap(client, extension.Protocol, i)); mDict.Add(extension.Protocol, new BInt(i)); i++; } handshakeDict.Add("m", mDict); String handshakeEncoded = BencodingUtils.EncodeString(handshakeDict); byte[] handshakeBytes = Encoding.ASCII.GetBytes(handshakeEncoded); Int32 length = 2 + handshakeBytes.Length; client.SendBytes((new byte[0]).Cat(Pack.Int32(length, Pack.Endianness.Big).Cat(new[] { (byte)20 }).Cat(new[] { (byte)0 }).Cat(handshakeBytes))); return true; }
public bool OnHandshake(IPeerWireClient client) { var handshakeDict = new BDict(); var mDict = new BDict(); byte i = 1; foreach (var extension in _protocolExtensions) { _extOutgoing.Add(new ClientProtocolIDMap(client, extension.Protocol, i)); mDict.Add(extension.Protocol, new BInt(i)); i++; } handshakeDict.Add("m", mDict); var handshakeEncoded = BencodingUtils.EncodeString(handshakeDict); var handshakeBytes = Encoding.ASCII.GetBytes(handshakeEncoded); var length = 2 + handshakeBytes.Length; client.SendBytes((new byte[0]).Cat(Pack.Int32(length, Pack.Endianness.Big).Cat(new[] { (byte)20 }).Cat(new[] { (byte)0 }).Cat(handshakeBytes))); return(true); }
public bool OnHandshake(IPeerWireClient client) { BDict handshakeDict = new BDict(); BDict mDict = new BDict(); byte i = 1; foreach (IBTExtension extension in this._protocolExtensions) { this._extOutgoing.Add(new ClientProtocolIDMap(client, extension.Protocol, i)); mDict.Add(extension.Protocol, new BInt(i)); i++; } handshakeDict.Add("m", mDict); string handshakeEncoded = BencodingUtils.EncodeString(handshakeDict); byte[] handshakeBytes = Encoding.ASCII.GetBytes(handshakeEncoded); Int32 length = 2 + handshakeBytes.Length; client.SendBytes((new byte[0]).Cat(PackHelper.Int32(length).Cat(new[] { (byte)20 }).Cat(new[] { (byte)0 }).Cat(handshakeBytes))); return(true); }
public void SendMessage(IPeerWireClient peerWireClient, IPEndPoint[] addedEndPoints, byte[] flags, IPEndPoint[] droppedEndPoints) { if (addedEndPoints == null && droppedEndPoints == null) return; BDict d = new BDict(); if (addedEndPoints != null) { byte[] added = new byte[addedEndPoints.Length * 6]; for (int x = 0; x < addedEndPoints.Length; x++) { addedEndPoints[x].Address.GetAddressBytes().CopyTo(added, x * 6); BitConverter.GetBytes((ushort)addedEndPoints[x].Port).CopyTo(added, (x * 6)+4); } d.Add("added", new BString { ByteValue = added }); } if (droppedEndPoints != null) { byte[] dropped = new byte[droppedEndPoints.Length * 6]; for (int x = 0; x < droppedEndPoints.Length; x++) { droppedEndPoints[x].Address.GetAddressBytes().CopyTo(dropped, x * 6); dropped.SetValue((ushort)droppedEndPoints[x].Port, (x * 6) + 2); } d.Add("dropped", new BString { ByteValue = dropped }); } _parent.SendExtended(peerWireClient, _parent.GetOutgoingMessageID(peerWireClient, this), BencodingUtils.EncodeBytes(d)); }
public void BDictAddNullValue() { BDict dict = new BDict(); dict.Add("a", null); }
public void BDictAddNullKey() { BDict dict = new BDict(); dict.Add(null, new BInt(0)); }