Skip to content

Commit

Permalink
More tests for slskmessages
Browse files Browse the repository at this point in the history
  • Loading branch information
Lev Gorodetskiy authored and Lene Preuss committed Apr 27, 2020
1 parent 46db5b0 commit 06cc8f2
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pynicotine/slskmessages.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ def packObject(self, object):
log.addwarning(_("Warning: unknown object type %s") % type(object) + " " + "in message %(type)s" % {'type': self.__class__})
return b""

def packObjects(self, *objects) -> bytes:
"""Helper method to pack each object in sequence and concatenate resulting bytes"""
bytes_ = b''
for obj in objects:
bytes_ += self.packObject(obj)
return bytes_

def makeNetworkMessage(self):
""" Returns binary array, that can be sent over the network"""
log.addwarning(_("Empty message made, class %s") % (self.__class__,))
Expand All @@ -367,6 +374,7 @@ def parseNetworkMessage(self, message):
in an object"""
log.addwarning(_("Can't parse incoming messages, class %s") % (self.__class__,))

# FIXME: Unresolved attribute reference
def doubleParseNetworkMessage(self, message):
"""Calls self._parseNetworkMessage first with a NetworkLongLongType, if that fails with NetworkIntType."""
messagename = str(self)
Expand Down Expand Up @@ -430,7 +438,7 @@ def __repr__(self):
def makeNetworkMessage(self):
payload = self.username + self.passwd
md5hash = hashlib.md5(payload.encode()).hexdigest()
message = self.packObject(self.username) + self.packObject(self.passwd) + self.packObject(self.version) + self.packObject(md5hash) + self.packObject(17)
message = self.packObjects(self.username, self.passwd, self.version, md5hash, 17)
return message

def parseNetworkMessage(self, message):
Expand All @@ -440,9 +448,8 @@ def parseNetworkMessage(self, message):

else:
pos, self.banner = self.getObject(message, bytes, pos)
if len(message[pos:]) > 0:
if message[pos:]:
try:
import socket
pos, self.ip = pos + 4, socket.inet_ntoa(message[pos:pos + 4][::-1])
# Unknown number
except Exception as error:
Expand Down Expand Up @@ -493,7 +500,6 @@ def makeNetworkMessage(self):

def parseNetworkMessage(self, message):
pos, self.user = self.getObject(message, bytes)
import socket
pos, self.ip = pos + 4, socket.inet_ntoa(message[pos:pos + 4][::-1])
pos, self.port = self.getObject(message, int, pos, 1)

Expand Down Expand Up @@ -568,6 +574,7 @@ def parseNetworkMessage(self, message):
pass


# NOTE: int attribute converted to string
class SetStatus(ServerMessage):
""" We send our new status to the server """
def __init__(self, status=None):
Expand All @@ -580,15 +587,15 @@ def makeNetworkMessage(self):
class NotifyPrivileges(ServerMessage):
""" Server tells us something about privileges"""
def __init__(self, token=None, user=None):
self.token = token
self.token: Optional[int] = token
self.user = _str(user)

def parseNetworkMessage(self, message):
pos, self.token = self.getObject(message, int)
pos, self.user = self.getObject(message, bytes, pos)

def makeNetworkMessage(self):
return self.packObject(self.token) + self.packObject(self.user)
return self.packObjects(self.token, self.user)


class AckNotifyPrivileges(ServerMessage):
Expand Down
151 changes: 151 additions & 0 deletions tests/test_pynicotine/test_slskmessages.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import unittest

from pynicotine.slskmessages import AddUser
from pynicotine.slskmessages import ChangePassword
from pynicotine.slskmessages import GetPeerAddress
from pynicotine.slskmessages import GetUserStatus
from pynicotine.slskmessages import JustDecoded
from pynicotine.slskmessages import Login
from pynicotine.slskmessages import NetworkIntType
from pynicotine.slskmessages import NetworkLongLongType
from pynicotine.slskmessages import NetworkSignedIntType
from pynicotine.slskmessages import NotifyPrivileges
from pynicotine.slskmessages import RemoveUser
from pynicotine.slskmessages import SetStatus
from pynicotine.slskmessages import SetWaitPort
from pynicotine.slskmessages import SlskMessage
from pynicotine.slskmessages import ToBeEncoded
from pynicotine.slskmessages import Unknown6


class ToBeEncodedTest(unittest.TestCase):
Expand Down Expand Up @@ -78,3 +88,144 @@ def test_packObject(self):
self.assertEqual(b'{\x00\x00\x00', net_int_message)
self.assertEqual(b'{\x00\x00\x00', net_signed_int_message)
self.assertEqual(b'{\x00\x00\x00\x00\x00\x00\x00', net_long_long_message)


class LoginMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = Login(username='test', passwd='s33cr3t', version=157)

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'\x04\x00\x00\x00test\x07\x00\x00\x00s33cr3t\x9d\x00\x00\x00 \x00\x00\x00d'
b'bc93f24d8f3f109deed23c3e2f8b74c\x11\x00\x00\x00',
message)


class ChangePasswordMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = ChangePassword(password='s33cr3t')

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'\x07\x00\x00\x00s33cr3t',
message)


class SetWaitPortMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = SetWaitPort(port=1337)

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'9\x05\x00\x00',
message)


class GetPeerAddressMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = GetPeerAddress(user='test')

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'\x04\x00\x00\x00test',
message)


class AddUserMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = AddUser(user='test')

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'\x04\x00\x00\x00test',
message)


class Unknown6MessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = Unknown6(user='test')

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'\x04\x00\x00\x00test',
message)


class RemoveUserMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = RemoveUser(user='test')

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'\x04\x00\x00\x00test',
message)


class GetUserStatusMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = GetUserStatus(user='test')

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'\x04\x00\x00\x00test',
message)


class SetStatusMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = SetStatus(status=1)

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'\x01\x00\x00\x00',
message)


class NotifyPrivilegesMessageTest(unittest.TestCase):
def test_makeNetworkMessage(self):
# Arrange
obj = NotifyPrivileges(token=123456, user='test')

# Act
message = obj.makeNetworkMessage()

# Assert
self.assertEqual(
b'@\xe2\x01\x00\x04\x00\x00\x00test',
message)

0 comments on commit 06cc8f2

Please sign in to comment.