Moved tests from helper_msgcoding into TestCore.test_msgcoding()

This commit is contained in:
Dmitri Bogomolov 2018-05-08 17:01:20 +03:00
parent ce6e9804ad
commit cfdb6beb54
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
2 changed files with 53 additions and 33 deletions

View File

@ -1,4 +1,9 @@
#!/usr/bin/python2.7 """
Message encoding end decoding functions
"""
import string
import zlib
try: try:
import msgpack import msgpack
@ -7,14 +12,11 @@ except ImportError:
import umsgpack as msgpack import umsgpack as msgpack
except ImportError: except ImportError:
import fallback.umsgpack.umsgpack as msgpack import fallback.umsgpack.umsgpack as msgpack
import string
import zlib
import messagetypes
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
from debug import logger from debug import logger
import messagetypes
from tr import _translate from tr import _translate
import helper_random
BITMESSAGE_ENCODING_IGNORE = 0 BITMESSAGE_ENCODING_IGNORE = 0
BITMESSAGE_ENCODING_TRIVIAL = 1 BITMESSAGE_ENCODING_TRIVIAL = 1
@ -62,7 +64,7 @@ class MsgEncode(object):
self.length = len(self.data) self.length = len(self.data)
def encodeSimple(self, message): def encodeSimple(self, message):
self.data = 'Subject:' + message['subject'] + '\n' + 'Body:' + message['body'] self.data = 'Subject:%(subject)s\nBody:%(body)s' % message
self.length = len(self.data) self.length = len(self.data)
def encodeTrivial(self, message): def encodeTrivial(self, message):
@ -75,10 +77,14 @@ class MsgDecode(object):
self.encoding = encoding self.encoding = encoding
if self.encoding == BITMESSAGE_ENCODING_EXTENDED: if self.encoding == BITMESSAGE_ENCODING_EXTENDED:
self.decodeExtended(data) self.decodeExtended(data)
elif self.encoding in [BITMESSAGE_ENCODING_SIMPLE, BITMESSAGE_ENCODING_TRIVIAL]: elif self.encoding in (
BITMESSAGE_ENCODING_SIMPLE, BITMESSAGE_ENCODING_TRIVIAL):
self.decodeSimple(data) self.decodeSimple(data)
else: else:
self.body = _translate("MsgDecode", "The message has an unknown encoding.\nPerhaps you should upgrade Bitmessage.") self.body = _translate(
"MsgDecode",
"The message has an unknown encoding.\n"
"Perhaps you should upgrade Bitmessage.")
self.subject = _translate("MsgDecode", "Unknown encoding") self.subject = _translate("MsgDecode", "Unknown encoding")
def decodeExtended(self, data): def decodeExtended(self, data):
@ -86,7 +92,9 @@ class MsgDecode(object):
tmp = "" tmp = ""
while len(tmp) <= BMConfigParser().safeGetInt("zlib", "maxsize"): while len(tmp) <= BMConfigParser().safeGetInt("zlib", "maxsize"):
try: try:
got = dc.decompress(data, BMConfigParser().safeGetInt("zlib", "maxsize") + 1 - len(tmp)) got = dc.decompress(
data, BMConfigParser().safeGetInt("zlib", "maxsize") +
1 - len(tmp))
# EOF # EOF
if got == "": if got == "":
break break
@ -138,23 +146,3 @@ class MsgDecode(object):
subject = subject.splitlines()[0] subject = subject.splitlines()[0]
self.subject = subject self.subject = subject
self.body = body self.body = body
if __name__ == '__main__':
messageData = {
"subject": ''.join(helper_random.randomchoice(string.ascii_lowercase + string.digits) for _ in range(40)),
"body": ''.join(helper_random.randomchoice(string.ascii_lowercase + string.digits) for _ in range(10000))
}
obj1 = MsgEncode(messageData, 1)
obj2 = MsgEncode(messageData, 2)
obj3 = MsgEncode(messageData, 3)
print "1:%i 2:%i 3:%i" %(len(obj1.data), len(obj2.data), len(obj3.data))
obj1e = MsgDecode(1, obj1.data)
# no subject in trivial encoding
assert messageData["body"] == obj1e.body
obj2e = MsgDecode(2, obj2.data)
assert messageData["subject"] == obj2e.subject
assert messageData["body"] == obj2e.body
obj3e = MsgDecode(3, obj3.data)
assert messageData["subject"] == obj3e.subject
assert messageData["body"] == obj3e.body

View File

@ -1,14 +1,46 @@
""" """
Tests for core. Tests for core and those that do not work outside
(because of import error for example)
""" """
import random
import string
import unittest import unittest
from helper_msgcoding import MsgEncode, MsgDecode
class TestCore(unittest.TestCase): class TestCore(unittest.TestCase):
"""Test case, which runs from main pybitmessage thread""" """Test case, which runs in main pybitmessage thread"""
def test_pass(self):
pass def test_msgcoding(self):
"""test encoding and decoding (originally from helper_msgcoding)"""
msg_data = {
'subject': ''.join(
random.choice(string.ascii_lowercase + string.digits)
for _ in range(40)),
'body': ''.join(
random.choice(string.ascii_lowercase + string.digits)
for _ in range(10000))
}
obj1 = MsgEncode(msg_data, 1)
obj2 = MsgEncode(msg_data, 2)
obj3 = MsgEncode(msg_data, 3)
# print "1: %i 2: %i 3: %i" % (
# len(obj1.data), len(obj2.data), len(obj3.data))
obj1e = MsgDecode(1, obj1.data)
# no subject in trivial encoding
self.assertEqual(msg_data['body'], obj1e.body)
obj2e = MsgDecode(2, obj2.data)
self.assertEqual(msg_data['subject'], obj2e.subject)
self.assertEqual(msg_data['body'], obj2e.body)
obj3e = MsgDecode(3, obj3.data)
self.assertEqual(msg_data['subject'], obj3e.subject)
self.assertEqual(msg_data['body'], obj3e.body)
def run(): def run():