2020-04-20 18:27:53 +00:00
|
|
|
{
|
|
|
|
writeScriptBin,
|
|
|
|
writeText,
|
|
|
|
python3,
|
|
|
|
connectTo ? "localhost",
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
dummyFile = writeText "dummy-file" ''
|
|
|
|
Dear dog,
|
|
|
|
|
|
|
|
Please find this *really* important attachment.
|
|
|
|
|
|
|
|
Yours truly,
|
2022-08-05 11:27:03 +00:00
|
|
|
Bob
|
2020-04-20 18:27:53 +00:00
|
|
|
'';
|
|
|
|
in
|
|
|
|
writeScriptBin "send-message" ''
|
|
|
|
#!${(python3.withPackages (ps: [ ps.slixmpp ])).interpreter}
|
|
|
|
import logging
|
|
|
|
import sys
|
2022-11-23 10:01:04 +00:00
|
|
|
import signal
|
2020-04-20 18:27:53 +00:00
|
|
|
from types import MethodType
|
|
|
|
|
|
|
|
from slixmpp import ClientXMPP
|
|
|
|
from slixmpp.exceptions import IqError, IqTimeout
|
|
|
|
|
|
|
|
|
|
|
|
class CthonTest(ClientXMPP):
|
|
|
|
|
|
|
|
def __init__(self, jid, password):
|
|
|
|
ClientXMPP.__init__(self, jid, password)
|
|
|
|
self.add_event_handler("session_start", self.session_start)
|
2021-02-11 17:46:07 +00:00
|
|
|
self.test_succeeded = False
|
2020-04-20 18:27:53 +00:00
|
|
|
|
|
|
|
async def session_start(self, event):
|
2021-02-11 17:46:07 +00:00
|
|
|
try:
|
|
|
|
# Exceptions in event handlers are printed to stderr but not
|
|
|
|
# propagated, they do not make the script terminate with a non-zero
|
|
|
|
# exit code. We use the `test_succeeded` flag as a workaround and
|
|
|
|
# check it later at the end of the script to exit with a proper
|
|
|
|
# exit code.
|
|
|
|
# Additionally, this flag ensures that this event handler has been
|
|
|
|
# actually run by ClientXMPP, which may well not be the case.
|
|
|
|
await self.test_xmpp_server()
|
|
|
|
self.test_succeeded = True
|
|
|
|
finally:
|
|
|
|
# Even if an exception happens in `test_xmpp_server()`, we still
|
|
|
|
# need to disconnect explicitly, otherwise the process will hang
|
|
|
|
# forever.
|
|
|
|
self.disconnect(wait=True)
|
|
|
|
|
|
|
|
async def test_xmpp_server(self):
|
2020-04-20 18:27:53 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
self.send_presence()
|
|
|
|
self.get_roster()
|
|
|
|
# Sending a test message
|
|
|
|
self.send_message(mto="azurediamond@example.com", mbody="Hello, this is dog.", mtype="chat")
|
|
|
|
log.info('Message sent')
|
|
|
|
|
|
|
|
# Test http upload (XEP_0363)
|
2020-09-09 10:38:57 +00:00
|
|
|
try:
|
2022-04-22 14:33:38 +00:00
|
|
|
url = await self['xep_0363'].upload_file("${dummyFile}",timeout=10)
|
2020-09-09 10:38:57 +00:00
|
|
|
except:
|
|
|
|
log.error("ERROR: Cannot run upload command. XEP_0363 seems broken")
|
|
|
|
sys.exit(1)
|
2020-04-20 18:27:53 +00:00
|
|
|
log.info('Upload success!')
|
2021-02-11 17:46:07 +00:00
|
|
|
|
2020-04-20 18:27:53 +00:00
|
|
|
# Test MUC
|
2021-02-11 17:46:07 +00:00
|
|
|
# TODO: use join_muc_wait() after slixmpp 1.8.0 is released.
|
|
|
|
self.plugin['xep_0045'].join_muc('testMucRoom', 'cthon98')
|
2020-04-20 18:27:53 +00:00
|
|
|
log.info('MUC join success!')
|
|
|
|
log.info('XMPP SCRIPT TEST SUCCESS')
|
|
|
|
|
2022-11-23 10:01:04 +00:00
|
|
|
def timeout_handler(signalnum, stackframe):
|
|
|
|
print('ERROR: xmpp-sendmessage timed out')
|
|
|
|
sys.exit(1)
|
2020-04-20 18:27:53 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-11-23 10:01:04 +00:00
|
|
|
signal.signal(signal.SIGALRM, timeout_handler)
|
|
|
|
signal.alarm(120)
|
2020-04-20 18:27:53 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
|
|
format='%(levelname)-8s %(message)s')
|
|
|
|
|
|
|
|
ct = CthonTest('cthon98@example.com', 'nothunter2')
|
|
|
|
ct.register_plugin('xep_0071')
|
|
|
|
ct.register_plugin('xep_0128')
|
|
|
|
# HTTP Upload
|
|
|
|
ct.register_plugin('xep_0363')
|
|
|
|
# MUC
|
|
|
|
ct.register_plugin('xep_0045')
|
2022-11-23 10:05:12 +00:00
|
|
|
ct.connect(("${connectTo}", 5222))
|
2020-04-20 18:27:53 +00:00
|
|
|
ct.process(forever=False)
|
2021-02-11 17:46:07 +00:00
|
|
|
|
|
|
|
if not ct.test_succeeded:
|
|
|
|
sys.exit(1)
|
2019-04-17 21:36:07 +00:00
|
|
|
''
|