From cfdb6beb54c64861e1db825a41a7e6bb3f3b83bc Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Tue, 8 May 2018 17:01:20 +0300 Subject: [PATCH] Moved tests from helper_msgcoding into TestCore.test_msgcoding() --- src/helper_msgcoding.py | 46 +++++++++++++++-------------------------- src/tests/core.py | 40 +++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/helper_msgcoding.py b/src/helper_msgcoding.py index ab228834..cc632ffa 100644 --- a/src/helper_msgcoding.py +++ b/src/helper_msgcoding.py @@ -1,4 +1,9 @@ -#!/usr/bin/python2.7 +""" +Message encoding end decoding functions +""" + +import string +import zlib try: import msgpack @@ -7,14 +12,11 @@ except ImportError: import umsgpack as msgpack except ImportError: import fallback.umsgpack.umsgpack as msgpack -import string -import zlib +import messagetypes from bmconfigparser import BMConfigParser from debug import logger -import messagetypes from tr import _translate -import helper_random BITMESSAGE_ENCODING_IGNORE = 0 BITMESSAGE_ENCODING_TRIVIAL = 1 @@ -62,7 +64,7 @@ class MsgEncode(object): self.length = len(self.data) 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) def encodeTrivial(self, message): @@ -75,10 +77,14 @@ class MsgDecode(object): self.encoding = encoding if self.encoding == BITMESSAGE_ENCODING_EXTENDED: 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) 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") def decodeExtended(self, data): @@ -86,7 +92,9 @@ class MsgDecode(object): tmp = "" while len(tmp) <= BMConfigParser().safeGetInt("zlib", "maxsize"): try: - got = dc.decompress(data, BMConfigParser().safeGetInt("zlib", "maxsize") + 1 - len(tmp)) + got = dc.decompress( + data, BMConfigParser().safeGetInt("zlib", "maxsize") + + 1 - len(tmp)) # EOF if got == "": break @@ -138,23 +146,3 @@ class MsgDecode(object): subject = subject.splitlines()[0] self.subject = subject 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 diff --git a/src/tests/core.py b/src/tests/core.py index 3738c32e..f937bc47 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -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 +from helper_msgcoding import MsgEncode, MsgDecode + class TestCore(unittest.TestCase): - """Test case, which runs from main pybitmessage thread""" - def test_pass(self): - pass + """Test case, which runs in main pybitmessage thread""" + + 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():