2016-09-30 06:06:07 +02:00
|
|
|
import datetime
|
|
|
|
import time
|
2017-02-18 01:59:57 +01:00
|
|
|
import binascii
|
2017-09-29 12:44:22 +02:00
|
|
|
import os
|
2017-07-27 06:11:12 +02:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2016-09-30 06:06:07 +02:00
|
|
|
import mock
|
2018-02-20 19:46:17 +01:00
|
|
|
import logging
|
2016-09-30 06:06:07 +02:00
|
|
|
|
2018-02-20 19:46:17 +01:00
|
|
|
from lbrynet.dht.encoding import Bencode
|
|
|
|
from lbrynet.dht.error import DecodeError
|
|
|
|
from lbrynet.dht.msgformat import DefaultFormat
|
|
|
|
from lbrynet.dht.msgtypes import ResponseMessage, RequestMessage, ErrorMessage
|
2016-09-30 06:06:07 +02:00
|
|
|
|
2018-02-20 19:46:17 +01:00
|
|
|
_encode = Bencode()
|
|
|
|
_datagram_formatter = DefaultFormat()
|
2016-09-30 06:06:07 +02:00
|
|
|
DEFAULT_TIMESTAMP = datetime.datetime(2016, 1, 1)
|
|
|
|
DEFAULT_ISO_TIME = time.mktime(DEFAULT_TIMESTAMP.timetuple())
|
|
|
|
|
2018-02-20 19:46:17 +01:00
|
|
|
log = logging.getLogger("lbrynet.tests.util")
|
|
|
|
|
2016-09-30 06:06:07 +02:00
|
|
|
|
2017-07-27 06:11:12 +02:00
|
|
|
def mk_db_and_blob_dir():
|
|
|
|
db_dir = tempfile.mkdtemp()
|
|
|
|
blob_dir = tempfile.mkdtemp()
|
|
|
|
return db_dir, blob_dir
|
|
|
|
|
2018-02-20 19:46:17 +01:00
|
|
|
|
2017-07-27 06:11:12 +02:00
|
|
|
def rm_db_and_blob_dir(db_dir, blob_dir):
|
|
|
|
shutil.rmtree(db_dir, ignore_errors=True)
|
|
|
|
shutil.rmtree(blob_dir, ignore_errors=True)
|
|
|
|
|
2018-02-20 19:46:17 +01:00
|
|
|
|
2017-02-18 01:59:57 +01:00
|
|
|
def random_lbry_hash():
|
|
|
|
return binascii.b2a_hex(os.urandom(48))
|
|
|
|
|
2018-02-20 19:46:17 +01:00
|
|
|
|
2016-09-30 06:06:07 +02:00
|
|
|
def resetTime(test_case, timestamp=DEFAULT_TIMESTAMP):
|
|
|
|
iso_time = time.mktime(timestamp.timetuple())
|
|
|
|
patcher = mock.patch('time.time')
|
|
|
|
patcher.start().return_value = iso_time
|
|
|
|
test_case.addCleanup(patcher.stop)
|
|
|
|
|
|
|
|
patcher = mock.patch('lbrynet.core.utils.now')
|
|
|
|
patcher.start().return_value = timestamp
|
|
|
|
test_case.addCleanup(patcher.stop)
|
|
|
|
|
|
|
|
patcher = mock.patch('lbrynet.core.utils.utcnow')
|
|
|
|
patcher.start().return_value = timestamp
|
|
|
|
test_case.addCleanup(patcher.stop)
|
2017-10-17 04:11:20 +02:00
|
|
|
|
2018-02-20 19:46:17 +01:00
|
|
|
|
2017-10-17 04:11:20 +02:00
|
|
|
def is_android():
|
|
|
|
return 'ANDROID_ARGUMENT' in os.environ # detect Android using the Kivy way
|
2018-02-20 19:46:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def debug_kademlia_packet(data, source, destination, node):
|
|
|
|
if log.level != logging.DEBUG:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
packet = _datagram_formatter.fromPrimitive(_encode.decode(data))
|
|
|
|
if isinstance(packet, RequestMessage):
|
|
|
|
log.debug("request %s --> %s %s (node time %s)", source[0], destination[0], packet.request,
|
|
|
|
node.clock.seconds())
|
|
|
|
elif isinstance(packet, ResponseMessage):
|
|
|
|
if isinstance(packet.response, (str, unicode)):
|
|
|
|
log.debug("response %s <-- %s %s (node time %s)", destination[0], source[0], packet.response,
|
|
|
|
node.clock.seconds())
|
|
|
|
else:
|
2018-03-27 23:35:31 +02:00
|
|
|
log.debug("response %s <-- %s %i contacts (node time %s)", destination[0], source[0],
|
|
|
|
len(packet.response), node.clock.seconds())
|
2018-02-20 19:46:17 +01:00
|
|
|
elif isinstance(packet, ErrorMessage):
|
|
|
|
log.error("error %s <-- %s %s (node time %s)", destination[0], source[0], packet.exceptionType,
|
|
|
|
node.clock.seconds())
|
|
|
|
except DecodeError:
|
|
|
|
log.exception("decode error %s --> %s (node time %s)", source[0], destination[0], node.clock.seconds())
|