public void AnnouncePeerQuery_WithImpliedPort_Zero()
 {
     byte[] encoded = Encoding.UTF8.GetBytes(TestBencodedMessage.Replace("porti1e", "porti0e"));
     Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(encoded, out BDictionary message));
     Assert.Equal(QueryType.announce_peer, KademliaRPC.TryGetQuery(message, out Query query));
     Assert.False(((AnnouncePeerQuery)query).ImpliedPort);
 }
Beispiel #2
0
        public void PingQuery_Decoding()
        {
            byte[] bencodedMessageBytes = Encoding.UTF8.GetBytes(TestBencodedMessage);

            Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(bencodedMessageBytes, out BDictionary message));
            Assert.Equal(QueryType.ping, KademliaRPC.TryGetQuery(message, out Query query));
            Assert.True(query is PingQuery);
        }
 public void AnnouncePeerQuery_WithoutImpliedPort()
 {
     byte[] encoded = Encoding.UTF8.GetBytes(
         "d1:ad2:id20:abcdefghij01234567899:info_hash20:mnopqrstuvwxyz1234564:porti6881e5:token8:aoeusnthe1:q13:announce_peer1:t2:aa1:y1:qe");
     Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(encoded, out BDictionary message));
     Assert.Equal(QueryType.announce_peer, KademliaRPC.TryGetQuery(message, out Query query));
     Assert.False(((AnnouncePeerQuery)query).ImpliedPort);
 }
Beispiel #4
0
        public void PingQuery_Encoding()
        {
            TransactionID transactionId = new TransactionID(Encoding.UTF8.GetBytes("aa"));
            NodeID        senderId      = new NodeID(Encoding.UTF8.GetBytes("abcdefghij0123456789"));
            PingQuery     testQuery     = new PingQuery(transactionId, senderId);

            byte[] bencodedMessageBytes = testQuery.Construct().Encode();

            Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(bencodedMessageBytes, out BDictionary message));
            Assert.Equal(QueryType.ping, KademliaRPC.TryGetQuery(message, out Query query));
            Assert.True(query is PingQuery);
            Assert.Equal(testQuery.TransactionID, query.TransactionID);
            Assert.Equal(testQuery.SenderNode, ((PingQuery)query).SenderNode);

            Assert.Equal(TestBencodedMessage, Encoding.UTF8.GetString(bencodedMessageBytes));
        }
Beispiel #5
0
        public void GetPeersQuery_Encoding()
        {
            TransactionID transactionId = new TransactionID(Encoding.UTF8.GetBytes("aa"));
            NodeID        senderId      = new NodeID(Encoding.UTF8.GetBytes("abcdefghij0123456789"));
            InfoHash      infoHash      = new InfoHash(Encoding.UTF8.GetBytes("mnopqrstuvwxyz123456"));
            GetPeersQuery testQuery     = new GetPeersQuery(transactionId, senderId, infoHash);

            byte[] bencodedMessageBytes = testQuery.Construct().Encode();

            Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(bencodedMessageBytes, out BDictionary message));
            Assert.Equal(QueryType.get_peers, KademliaRPC.TryGetQuery(message, out Query query));
            Assert.True(query is GetPeersQuery);
            Assert.Equal(testQuery.TransactionID, query.TransactionID);
            Assert.Equal(testQuery.SenderNode, ((GetPeersQuery)query).SenderNode);
            Assert.Equal(testQuery.InfoHash, ((GetPeersQuery)query).InfoHash);

            Assert.Equal(TestBencodedMessage, Encoding.UTF8.GetString(bencodedMessageBytes));
        }
Beispiel #6
0
        public void FindNodeQuery_Encoding()
        {
            TransactionID transactionId = new TransactionID(Encoding.UTF8.GetBytes("aa"));
            NodeID        senderId      = new NodeID(Encoding.UTF8.GetBytes("abcdefghij0123456789"));
            NodeID        targetId      = new NodeID(Encoding.UTF8.GetBytes("mnopqrstuvwxyz123456"));
            FindNodeQuery testQuery     = new FindNodeQuery(transactionId, senderId, targetId);

            byte[] bencodedMessageBytes = testQuery.Construct().Encode();

            Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(bencodedMessageBytes, out BDictionary message));
            Assert.Equal(QueryType.find_node, KademliaRPC.TryGetQuery(message, out Query query));
            Assert.True(query is FindNodeQuery);
            Assert.Equal(testQuery.TransactionID, query.TransactionID);
            Assert.Equal(testQuery.SenderNode, ((FindNodeQuery)query).SenderNode);
            Assert.Equal(testQuery.TargetNode, ((FindNodeQuery)query).TargetNode);

            Assert.Equal(TestBencodedMessage, Encoding.UTF8.GetString(bencodedMessageBytes));
        }
        public void AnnouncePeerQuery_Encoding()
        {
            TransactionID     transactionId = new TransactionID(Encoding.UTF8.GetBytes("aa"));
            NodeID            senderId      = new NodeID(Encoding.UTF8.GetBytes("abcdefghij0123456789"));
            InfoHash          infoHash      = new InfoHash(Encoding.UTF8.GetBytes("mnopqrstuvwxyz123456"));
            PeerToken         token         = new PeerToken(Encoding.UTF8.GetBytes("aoeusnth"));
            int               port          = 6881;
            bool              impliedPort   = true;
            AnnouncePeerQuery testQuery     = new AnnouncePeerQuery(transactionId, senderId, infoHash, token, port, impliedPort);

            byte[] bencodedMessageBytes = testQuery.Construct().Encode();

            Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(bencodedMessageBytes, out BDictionary message));
            Assert.Equal(QueryType.announce_peer, KademliaRPC.TryGetQuery(message, out Query query));
            Assert.True(query is AnnouncePeerQuery);
            Assert.Equal(testQuery.TransactionID, query.TransactionID);
            Assert.Equal(testQuery.SenderNode, ((AnnouncePeerQuery)query).SenderNode);
            Assert.Equal(testQuery.InfoHash, ((AnnouncePeerQuery)query).InfoHash);
            Assert.Equal(testQuery.Token, ((AnnouncePeerQuery)query).Token);
            Assert.Equal(testQuery.Port, ((AnnouncePeerQuery)query).Port);
            Assert.Equal(testQuery.ImpliedPort, ((AnnouncePeerQuery)query).ImpliedPort);

            Assert.Equal(TestBencodedMessage, Encoding.UTF8.GetString(bencodedMessageBytes));
        }
Beispiel #8
0
 private bool AssertInvalidQuery(string bencodedMessage)
 {
     byte[] bencodedMessageBytes = Encoding.UTF8.GetBytes(bencodedMessage);
     return(MessageType.Query == KademliaRPC.TryParseMessage(bencodedMessageBytes, out BDictionary message) &&
            QueryType.Invalid == KademliaRPC.TryGetQuery(message, out Query query));
 }
Beispiel #9
0
 public void PingQuery_InvalidQueryName()
 {
     byte[] encoded = Encoding.UTF8.GetBytes(TestBencodedMessage.Replace("ping", "Ping"));
     Assert.Equal(MessageType.Query, KademliaRPC.TryParseMessage(encoded, out BDictionary message));
     Assert.Equal(QueryType.Unknown, KademliaRPC.TryGetQuery(message, out Query query));
 }