Skip to content

Commit

Permalink
Added search request & response payload structure
Browse files Browse the repository at this point in the history
  • Loading branch information
xoriole committed Jun 26, 2018
1 parent 0c7d65b commit bfc39e5
Show file tree
Hide file tree
Showing 16 changed files with 1,602 additions and 134 deletions.
Empty file.
15 changes: 15 additions & 0 deletions Tribler/Test/Community/Market/_trial_temp/test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
2018-06-08 11:29:52+0200 [-] Log opened.
2018-06-08 11:29:52+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_cancel <--
2018-06-08 11:29:52+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_counter_trade <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_create_ask <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_create_bid <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_decline_trade <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_e2e_trade <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_failing_payment <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_info_message <--
2018-06-08 11:29:53+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_orderbook_sync <--
2018-06-08 11:29:54+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunity.test_proposed_trade_timeout <--
2018-06-08 11:29:54+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunityTwoNodes.test_churn_matchmaker <--
2018-06-08 11:29:54+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunityTwoNodes.test_e2e_trade <--
2018-06-08 11:29:55+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunityTwoNodes.test_offline_matchmaker <--
2018-06-08 11:29:55+0200 [-] --> Tribler.Test.Community.Market.test_community.TestMarketCommunityTwoNodes.test_partial_trade <--
Empty file.
3 changes: 3 additions & 0 deletions Tribler/Test/Community/Triblerchain/_trial_temp/test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2018-05-18 12:22:51+0200 [-] Log opened.
2018-05-18 12:22:51+0200 [-] --> Tribler.Test.Community.Triblerchain.test_community.TestTriblerChainCrawlerCommunity.test_crawl_request <--
2018-05-18 12:22:51+0200 [-] PythonLoggingObserver hooked up
Empty file.
190 changes: 190 additions & 0 deletions Tribler/Test/Community/Tunnel/FullSession/_trial_temp/test.log

Large diffs are not rendered by default.

Empty file.
456 changes: 456 additions & 0 deletions Tribler/Test/Community/_trial_temp/test.log

Large diffs are not rendered by default.

154 changes: 120 additions & 34 deletions Tribler/Test/Community/popular/test_community.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import random
import string

from Tribler.Test.Core.base_test import MockObject
from Tribler.community.popular.community import PopularCommunity, MSG_TORRENT_HEALTH_RESPONSE, MAX_SUBSCRIBERS, \
MSG_CHANNEL_HEALTH_RESPONSE, ERROR_UNKNOWN_PEER, MSG_POPULAR_CONTENT_SUBSCRIPTION, ERROR_NO_CONTENT, \
ERROR_UNKNOWN_RESPONSE
from Tribler.community.popular.constants import SEARCH_TORRENT_REQUEST
from Tribler.community.popular.payload import SearchResponseItemPayload
from Tribler.community.popular.repository import TYPE_TORRENT_HEALTH
from Tribler.pyipv8.ipv8.test.base import TestBase
from Tribler.pyipv8.ipv8.test.mocking.ipv8 import MockIPv8
Expand All @@ -25,9 +28,62 @@ def load_random_torrents(limit):
]

torrent_db = MockObject()
torrent_db.getTorrent = lambda *args, **kwargs: None
torrent_db.updateTorrent = lambda *args, **kwargs: None
torrent_db.getRecentlyCheckedTorrents = load_random_torrents

return MockIPv8(u"curve25519", PopularCommunity, torrent_db=torrent_db)
channel_db = MockObject()

return MockIPv8(u"curve25519", PopularCommunity, torrent_db=torrent_db, channel_db=channel_db)


class MockRepository(object):

def _random_string(self, size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))

def _random_infohash(self):
return ''.join(random.choice('0123456789abcdef') for _ in range(20))

def search_torrent(self, query):
# sample search response items
query_str = ' '.join(query)
sample_items = []
for _ in range(10):
infohash = self._random_infohash()
name = query_str + " " + self._random_string()
length = random.randint(1000, 9999)
num_files = random.randint(1, 10)
category_list = ['video', 'audio']
creation_date = random.randint(1000000, 111111111)
seeders = random.randint(10, 200)
leechers = random.randint(5, 1000)
cid = self._random_string(size=20)

sample_items.append(SearchResponseItemPayload(infohash, name, length, num_files, category_list,
creation_date, seeders, leechers, cid))
return sample_items

def search_channels(self, _):
return []

def has_torrent(self, _):
return False

def cleanup(self):
pass

def update_from_search_results(self, results):
pass

def get_torrent(self, _):
return None

def get_top_torrents(self):
return []

def update_from_torrent_search_results(self, search_results):
pass


class TestPopularCommunity(TestPopularCommunityBase):
Expand Down Expand Up @@ -133,9 +189,9 @@ def on_torrent_health_response(peer, source_address, data):

# Add something to queue
health_info = ('a' * 20, random.randint(1, 100), random.randint(1, 10), random.randint(1, 111111))
self.nodes[1].overlay.queue_content(TYPE_TORRENT_HEALTH, health_info)
self.nodes[1].overlay._queue_content(TYPE_TORRENT_HEALTH, health_info)

self.nodes[1].overlay.publish_next_content()
self.nodes[1].overlay._publish_next_content()

yield self.deliver_messages()

Expand All @@ -146,8 +202,8 @@ def test_publish_no_content(self):
"""
Tests publishing next content if no content is available.
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

# Assume a subscribers exist
self.nodes[0].overlay.subscribers = [self.create_node()]
Expand All @@ -156,22 +212,22 @@ def test_publish_no_content(self):

# Try publishing the next available content
self.nodes[0].no_content = False
self.nodes[0].overlay.publish_next_content()
self.nodes[0].overlay._publish_next_content()
yield self.deliver_messages()

# Expect no content found to be logged
self.assertTrue(self.nodes[0].no_content)

# Restore logger
self.nodes[0].overlay._logger = original_logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_send_popular_content_subscribe(self):
"""
Tests sending popular content subscribe request.
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

self.nodes[0].overlay.broadcast_message = lambda packet, peer: \
self.fake_broadcast_message(self.nodes[0], packet, peer)
Expand Down Expand Up @@ -210,15 +266,15 @@ def test_send_popular_content_subscribe(self):
self.assertTrue(default_peers[0] not in self.nodes[0].overlay.publishers)

# Restore logger
self.nodes[0].overlay._logger = original_logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_send_popular_content_subscription(self):
"""
Tests sending popular content subscription response.
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

self.nodes[0].overlay.create_message_packet = lambda _type, _payload: \
self.fake_create_message_packet(self.nodes[0], _type, _payload)
Expand Down Expand Up @@ -253,15 +309,15 @@ def test_send_popular_content_subscription(self):
self.assertEqual(self.nodes[0].receiver, default_peers[0], "Intended receiver is different")

# Restore logger
self.nodes[0].overlay._logger = original_logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_send_torrent_health_response(self):
"""
Tests sending torrent health response.
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

self.nodes[0].overlay.create_message_packet = lambda _type, _payload: \
self.fake_create_message_packet(self.nodes[0], _type, _payload)
Expand Down Expand Up @@ -297,15 +353,15 @@ def test_send_torrent_health_response(self):
self.assertEqual(self.nodes[0].receiver, default_peers[0], "Intended receiver is different")

# Restore logger
self.nodes[0].overlay._logger = original_logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_send_channel_health_response(self):
"""
Tests sending torrent health response.
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

self.nodes[0].overlay.create_message_packet = lambda _type, _payload: \
self.fake_create_message_packet(self.nodes[0], _type, _payload)
Expand Down Expand Up @@ -341,15 +397,15 @@ def test_send_channel_health_response(self):
self.assertEqual(self.nodes[0].receiver, default_peers[0], "Intended receiver is different")

# Restore logger
self.nodes[0].overlay._logger = original_logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_on_torrent_health_response_from_unknown_peer(self):
"""
Tests receiving torrent health response from unknown peer
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

def fake_unpack_auth():
mock_auth = MockObject()
Expand All @@ -360,7 +416,7 @@ def fake_get_peer_from_auth(peer):
return peer

self.nodes[0].overlay._ez_unpack_auth = lambda payload_class, data: fake_unpack_auth()
self.nodes[0].overlay.get_peer_from_auth = lambda auth, address: fake_get_peer_from_auth(self.nodes[1])
self.nodes[0].overlay._get_peer_from_auth = lambda auth, address: fake_get_peer_from_auth(self.nodes[1])

source_address = MockObject()
data = MockObject()
Expand All @@ -372,15 +428,15 @@ def fake_get_peer_from_auth(peer):
self.assertTrue(self.nodes[0].unknown_response)

# Restore logger
self.nodes[0].overlay._logger = original_logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper
def test_on_popular_content_subscribe_unknown_peer(self):
"""
Tests receiving torrent health response from unknown peer
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

def fake_unpack_auth():
mock_auth = MockObject()
Expand All @@ -394,9 +450,9 @@ def fake_get_peer_from_auth(peer):
def fake_publish_latest_torrents(my_peer, _peer):
my_peer.publish_latest_torrents_called = True

self.nodes[0].overlay.publish_latest_torrents = lambda peer: fake_publish_latest_torrents(self.nodes[1], peer)
self.nodes[0].overlay._publish_latest_torrents = lambda peer: fake_publish_latest_torrents(self.nodes[1], peer)
self.nodes[0].overlay._ez_unpack_auth = lambda payload_class, data: fake_unpack_auth()
self.nodes[0].overlay.get_peer_from_auth = lambda auth, address: fake_get_peer_from_auth(self.nodes[1])
self.nodes[0].overlay._get_peer_from_auth = lambda auth, address: fake_get_peer_from_auth(self.nodes[1])

source_address = MockObject()
data = MockObject()
Expand All @@ -408,15 +464,15 @@ def fake_publish_latest_torrents(my_peer, _peer):
self.assertTrue(self.nodes[0].unknown_peer_found)

# Restore logger
self.nodes[0].overlay._logger = original_logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper(500)
@twisted_wrapper(5)
def test_on_popular_content_subscribe_ok(self):
"""
Tests receiving torrent health response from unknown peer
"""
original_logger = self.nodes[0].overlay._logger
self.nodes[0].overlay._logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)
original_logger = self.nodes[0].overlay.logger
self.nodes[0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(self.nodes[0], *args)

def fake_unpack_auth():
mock_auth = MockObject()
Expand All @@ -433,9 +489,9 @@ def fake_publish_latest_torrents(my_peer, _peer):
def fake_send_popular_content_subscription(my_peer):
my_peer.send_content_subscription_called = True

self.nodes[0].overlay.publish_latest_torrents = lambda peer: fake_publish_latest_torrents(self.nodes[0], peer)
self.nodes[0].overlay._publish_latest_torrents = lambda peer: fake_publish_latest_torrents(self.nodes[0], peer)
self.nodes[0].overlay._ez_unpack_auth = lambda payload_class, data: fake_unpack_auth()
self.nodes[0].overlay.get_peer_from_auth = lambda auth, address: fake_get_peer_from_auth(self.nodes[1])
self.nodes[0].overlay._get_peer_from_auth = lambda auth, address: fake_get_peer_from_auth(self.nodes[1])
self.nodes[0].overlay.send_popular_content_subscription = lambda peer, subscribed: \
fake_send_popular_content_subscription(self.nodes[1])

Expand All @@ -449,7 +505,37 @@ def fake_send_popular_content_subscription(my_peer):
self.assertTrue(self.nodes[0].publish_latest_torrents_called)

# Restore logger
self.nodes[0].overlay._logger = original_logger
self.nodes[0].overlay.logger = original_logger

@twisted_wrapper(5)
def test_search_request_response(self):

self.nodes[0].overlay.content_repository = MockRepository()
self.nodes[1].overlay.content_repository = MockRepository()

yield self.introduce_nodes()
self.nodes[0].overlay.subscribe_peers()
yield self.deliver_messages()

# Create a search request
query = "ubuntu"
self.nodes[0].overlay.send_torrent_search_request(query)

yield self.deliver_messages()

@twisted_wrapper(5)
def test_send_content_info_request(self):
self.nodes[0].overlay.content_repository = MockRepository()
self.nodes[1].overlay.content_repository = MockRepository()

yield self.introduce_nodes()
self.nodes[0].overlay.subscribe_peers()
yield self.deliver_messages()

content_type = SEARCH_TORRENT_REQUEST
request_list = ["ubuntu"]
self.nodes[0].overlay.send_content_info_request(content_type, request_list, limit=5, peer=None)
yield self.deliver_messages()

def fake_logger_error(self, my_peer, *args):
if ERROR_UNKNOWN_PEER in args[0]:
Expand Down
Loading

0 comments on commit bfc39e5

Please sign in to comment.