9578053 Jan 22 2022 distfiles.gentoo.org/distfiles/gajim-1.3.3-2.tar.gz

This commit is contained in:
emdee 2022-10-19 18:09:31 +00:00
parent a5b3822651
commit 4c1b226bff
1045 changed files with 753037 additions and 18 deletions

View file

@ -0,0 +1,156 @@
'''
Tests for dispatcher.py
'''
import unittest
import lib
lib.setup_env()
from mock import Mock
from nbxmpp import dispatcher
from nbxmpp.namespaces import Namespace
from gajim.common.protocol.bytestream import ConnectionIBBytestream
from gajim.common.protocol.bytestream import ConnectionSocks5Bytestream
from gajim.common.jingle import ConnectionJingle
from gajim.common import app
from gajim.common.socks5 import SocksQueue
session_init = '''
<iq xmlns="jabber:client" to="jingleft@thiessen.im/Gajim" type="set" id="43">
<jingle xmlns="urn:xmpp:jingle:1" action="session-initiate" initiator="jtest@thiessen.im/Gajim" sid="38">
<content name="fileWL1Y2JIPTM5RAD68" creator="initiator">
<security xmlns="urn:xmpp:jingle:security:xtls:0">
<method name="x509" />
</security>
<description xmlns="urn:xmpp:jingle:apps:file-transfer:1">
<offer>
<file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="to" size="2273">
<desc />
</file>
</offer>
</description>
<transport xmlns="urn:xmpp:jingle:transports:s5b:1" sid="39">
<candidate jid="jtest@thiessen.im/Gajim" cid="40" priority="8257536" host="192.168.2.100" type="direct" port="28011" />
<candidate jid="proxy.thiessen.im" cid="41" priority="655360" host="192.168.2.100" type="proxy" port="5000" />
<candidate jid="proxy.jabbim.cz" cid="42" priority="655360" host="192.168.2.100" type="proxy" port="7777" />
</transport>
</content>
</jingle>
</iq>
'''
transport_info = '''
<iq from='jtest@thiessen.im/Gajim'
id='hjdi8'
to='jingleft@thiessen.im/Gajim'
type='set'>
<jingle xmlns='urn:xmpp:jingle:1'
action='transport-info'
initiator='jtest@thiessen.im/Gajim'
sid='38'>
<content creator='initiator' name='fileWL1Y2JIPTM5RAD68'>
<transport xmlns='urn:xmpp:jingle:transports:s5b:1'
sid='vj3hs98y'>
<candidate-used cid='hr65dqyd'/>
</transport>
</content>
</jingle>
</iq>
'''
class Connection(Mock, ConnectionJingle, ConnectionSocks5Bytestream,
ConnectionIBBytestream):
def __init__(self):
Mock.__init__(self)
ConnectionJingle.__init__(self)
ConnectionSocks5Bytestream.__init__(self)
ConnectionIBBytestream.__init__(self)
self.connected = 2 # This tells gajim we are connected
def send(self, stanza=None, when=None):
# Called when gajim wants to send something
print(str(stanza))
class TestJingle(unittest.TestCase):
def setUp(self):
self.dispatcher = dispatcher.XMPPDispatcher()
app.nec = Mock()
app.socks5queue = SocksQueue(Mock())
# Setup mock client
self.client = Connection()
self.client.__str__ = lambda: 'Mock' # FIXME: why do I need this one?
self.client._caller = Connection()
self.client.defaultNamespace = Namespace.CLIENT
self.client.Connection = Connection() # mock transport
self.con = self.client.Connection
self.con.server_resource = None
self.con.connection = Connection()
'''
Fake file_props when we receive a file. Gajim creates a file_props
out of a FileRequestReceive event and from then on it changes in
a lot of places. It is easier to just copy it in here.
If the session_initiate stanza changes, this also must change.
'''
self.receive_file = {'stream-methods':
'http://jabber.org/protocol/bytestreams',
'sender': 'jtest@thiessen.im/Gajim',
'file-name': 'test_received_file',
'request-id': '43', 'sid': '39',
'session-sid': '38', 'session-type': 'jingle',
'transfered_size': [], 'receiver':
'jingleft@thiessen.im/Gajim', 'desc': '',
'size': '2273', 'type': 'r',
'streamhosts': [{'initiator':
'jtest@thiessen.im/Gajim',
'target': 'jingleft@thiessen.im/Gajim',
'cid': '41', 'state': 0, 'host': '192.168.2.100',
'type': 'direct', 'port': '28011'},
{'initiator': 'jtest@thiessen.im/Gajim',
'target': 'jingleft@thiessen.im/Gajim',
'cid': '42', 'state': 0, 'host': '192.168.2.100',
'type': 'proxy', 'port': '5000'}],
'name': 'to'}
def tearDown(self):
# Unplug if needed
if hasattr(self.dispatcher, '_owner'):
self.dispatcher.PlugOut()
def _simulate_connect(self):
self.dispatcher.PlugIn(self.client) # client is owner
# Simulate that we have established a connection
self.dispatcher.StreamInit()
self.dispatcher.ProcessNonBlocking("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client'>")
def _simulate_jingle_session(self):
self.dispatcher.RegisterHandler('iq', self.con._JingleCB, 'set',
Namespace.JINGLE)
self.dispatcher.ProcessNonBlocking(session_init)
session = list(self.con._sessions.values())[0] # The only session we have
jft = list(session.contents.values())[0] # jingleFT object
jft.file_props = self.receive_file # We plug file_props manually
# The user accepts to receive the file
# we have to manually simulate this behavior
session.approve_session()
self.con.send_file_approval(self.receive_file)
self.dispatcher.ProcessNonBlocking(transport_info)
def test_jingle_session(self):
self._simulate_connect()
self._simulate_jingle_session()
if __name__ == '__main__':
unittest.main()

View file

@ -0,0 +1,167 @@
import unittest
import lib
lib.setup_env()
import notify
import nbxmpp
from gajim.common import app
from gajim.common import nec
from gajim.common import ged
from gajim.common.nec import NetworkEvent
from gajim.session import ChatControlSession
from gajim.roster_window import RosterWindow
from gajim_mocks import *
from data import account1
app.interface = MockInterface()
# name to use for the test account
account_name = account1
class TestChatControlSession(unittest.TestCase):
''' Testclass for session.py '''
@classmethod
def setUpClass(cls):
app.nec = nec.NetworkEventsController()
cls.conn = MockConnection(account_name, {'send_stanza': None})
app.logger = MockLogger()
app.default_session_type = ChatControlSession
def setUp(self):
app.notification = notify.Notification()
# no notifications have been sent
self.assertEqual(0, len(notify.notifications))
def tearDown(self):
app.notification.clean()
def receive_chat_msg(self, jid, msgtxt):
'''simulate receiving a chat message from jid'''
msg = nbxmpp.Message()
msg.setBody(msgtxt)
msg.setType('chat')
xml = """<message from='%s' id='1' type='chat'><body>%s</body>
<thread>123</thread></message>""" % (jid, msgtxt)
stanza = nbxmpp.protocol.Message(node=nbxmpp.simplexml.XML2Node(xml))
self.conn._messageCB(None, stanza)
# ----- custom assertions -----
def assert_new_message_notification(self):
'''a new_message notification has been sent'''
self.assertEqual(1, len(notify.notifications))
notif = notify.notifications[-1]
self.assertEqual('New Message', notif.popup_event_type)
def assert_first_message_notification(self):
'''this message was treated as a first message'''
self.assert_new_message_notification()
notif = notify.notifications[-1]
first = notif.first_unread
self.assertTrue(first,
'message should have been treated as a first message')
def assert_not_first_message_notification(self):
'''this message was not treated as a first message'''
self.assert_new_message_notification()
notif = notify.notifications[-1]
first = notif.first_unread
self.assertTrue(not first,
'message was unexpectedly treated as a first message')
# ----- tests -----
def test_receive_1nocontrol(self):
'''test receiving a message in a blank state'''
jid = 'bct@necronomicorp.com'
fjid = 'bct@necronomicorp.com/Gajim'
msgtxt = 'testing one'
self.receive_chat_msg(fjid, msgtxt)
# session is created
self.assertTrue((jid in self.conn.sessions) and (
'123' in self.conn.sessions[jid]), 'session is not created')
sess = self.conn.sessions[jid]['123']
# message was logged
calls = app.storage.archive.mockGetNamedCalls('insert_into_logs')
self.assertEqual(1, len(calls))
# no ChatControl was open and autopopup was off
# so the message goes into the event queue
self.assertEqual(1, len(app.events.get_events(account_name)))
self.assert_first_message_notification()
# no control is attached to the session
self.assertEqual(None, sess.control)
def test_receive_2already_has_control(self):
'''test receiving a message with a session already attached to a
control'''
jid = 'bct@necronomicorp.com'
fjid = 'bct@necronomicorp.com/Gajim'
msgtxt = 'testing two'
app.interface.roster = RosterWindow(app.app)
sess = self.conn.sessions[jid]['123']
sess.control = MockChatControl(fjid, account_name)
self.receive_chat_msg(fjid, msgtxt)
# message was logged
calls = app.storage.archive.mockGetNamedCalls('insert_into_logs')
self.assertEqual(2, len(calls))
# the message does not go into the event queue
self.assertEqual(1, len(app.events.get_events(account_name)))
self.assert_not_first_message_notification()
# message was printed to the control
calls = sess.control.mockGetNamedCalls('print_conversation')
self.assertEqual(1, len(calls))
app.interface.roster.window.destroy()
#def test_received_3orphaned_control(self):
#'''test receiving a message when a control that doesn't have a session
#attached exists'''
#jid = 'bct@necronomicorp.com'
#fjid = jid + '/Gajim'
#msgtxt = 'testing three'
#ctrl = MockChatControl(jid, account_name)
#gajim.interface.msg_win_mgr = Mock({'get_control': ctrl})
#gajim.interface.msg_win_mgr.mockSetExpectation('get_control',
#expectParams(jid, account_name))
#self.receive_chat_msg(fjid, msgtxt)
## message was logged
#calls = gajim.logger.mockGetNamedCalls('insert_into_logs')
#self.assertEqual(1, len(calls))
## the message does not go into the event queue
#self.assertEqual(0, len(gajim.events.get_events(account_name)))
#self.assert_not_first_message_notification()
## this session is now attached to that control
#self.assertEqual(self.sess, ctrl.session)
#self.assertEqual(ctrl, self.sess.control, 'foo')
## message was printed to the control
#calls = ctrl.mockGetNamedCalls('print_conversation')
#self.assertEqual(1, len(calls))
if __name__ == '__main__':
unittest.main()

View file

@ -0,0 +1,172 @@
'''
Tests for dispatcher.py
'''
import unittest
import lib
lib.setup_env()
from mock import Mock
import sys
import socket
from gajim.common.socks5 import *
from gajim.common import jingle_xtls
class fake_sock(Mock):
def __init__(self, sockobj):
Mock.__init__(self)
self.sockobj = sockobj
def setup_stream(self):
sha1 = self.sockobj._get_sha1_auth()
self.incoming = []
self.incoming.append(self.sockobj._get_auth_response())
self.incoming.append(self.sockobj._get_request_buff(sha1, 0x00))
self.outgoing = []
self.outgoing.append(self.sockobj._get_auth_buff())
self.outgoing.append(self.sockobj._get_request_buff(sha1))
def switch_stream(self):
# Roles are reversed, client will be expecting server stream
# and server will be expecting client stream
temp = self.incoming
self.incoming = self.outgoing
self.outgoing = temp
def _recv(self, foo):
return self.incoming.pop(0)
def _send(self, data):
# This method is surrounded by a try block,
# we can't use assert here
if data != self.outgoing[0]:
print('FAILED SENDING TEST')
self.outgoing.pop(0)
class fake_idlequeue(Mock):
def __init__(self):
Mock.__init__(self)
def plug_idle(self, obj, writable=True, readable=True):
if readable:
obj.pollin()
if writable:
obj.pollout()
class TestSocks5(unittest.TestCase):
'''
Test class for Socks5
'''
def setUp(self):
streamhost = { 'host': None,
'port': 1,
'initiator' : None,
'target' : None}
queue = Mock()
queue.file_props = {}
#self.sockobj = Socks5Receiver(fake_idlequeue(), streamhost, None)
self.sockobj = Socks5Sender(fake_idlequeue(), None, 'server', Mock() ,
None, None, True, file_props={})
sock = fake_sock(self.sockobj)
self.sockobj._sock = sock
self.sockobj._recv = sock._recv
self.sockobj._send = sock._send
self.sockobj.state = 1
self.sockobj.connected = True
self.sockobj.pollend = self._pollend
# Something that the receiver needs
#self.sockobj.file_props['type'] = 'r'
# Something that the sender needs
self.sockobj.file_props = {}
self.sockobj.file_props['type'] = 'r'
self.sockobj.file_props['paused'] = ''
self.sockobj.queue = Mock()
self.sockobj.queue.process_result = self._pollend
def _pollend(self, foo = None, duu = None):
# This is a disconnect function
sys.exit("end of the road")
def _check_inout(self):
# Check if there isn't anything else to receive or send
sock = self.sockobj._sock
assert(sock.incoming == [])
assert(sock.outgoing == [])
def test_connection_server(self):
return
mocksock = self.sockobj._sock
mocksock.setup_stream()
#self.sockobj._sock.switch_stream()
s = socket.socket(2, 1, 6)
server = ('127.0.0.1', 28000)
s.connect(server)
s.send(mocksock.outgoing.pop(0))
self.assertEqual(s.recv(64), mocksock.incoming.pop(0))
s.send(mocksock.outgoing.pop(0))
self.assertEqual(s.recv(64), mocksock.incoming.pop(0))
def test_connection_client(self):
mocksock = self.sockobj._sock
mocksock.setup_stream()
mocksock.switch_stream()
s = socket.socket(10, 1, 6)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
netadd = ('::', 28000, 0, 0)
s.bind(netadd)
s.listen(socket.SOMAXCONN)
(s, address) = s.accept()
self.assertEqual(s.recv(64), mocksock.incoming.pop(0))
s.send(mocksock.outgoing.pop(0))
buff = s.recv(64)
inco = mocksock.incoming.pop(0)
#self.assertEqual(s.recv(64), mocksock.incoming.pop(0))
s.send(mocksock.outgoing.pop(0))
def test_client_negoc(self):
return
self.sockobj._sock.setup_stream()
try:
self.sockobj.pollout()
except SystemExit:
pass
self._check_inout()
def test_server_negoc(self):
return
self.sockobj._sock.setup_stream()
self.sockobj._sock.switch_stream()
try:
self.sockobj.idlequeue.plug_idle(self.sockobj, False, True)
except SystemExit:
pass
self._check_inout()
if __name__ == '__main__':
unittest.main()