Changeset - efd14b776ccc
[Not reviewed]
0 4 2
Jan Kaluza - 10 years ago 2015-12-17 10:07:56
jkaluza@redhat.com
Fix libcommuni PMs. Add test for PMs and test also non-single-irc-network mode
6 files changed with 205 insertions and 66 deletions:
0 comments (0 inline, 0 general)
backends/libcommuni/ircnetworkplugin.cpp
Show inline comments
 
@@ -173,6 +173,7 @@ void IRCNetworkPlugin::handleMessageSendRequest(const std::string &user, const s
 
		return;
 
	}
 

	
 
	LOG4CXX_INFO(logger, user << ": XXX name: " << session << ", message to " << legacyName);
 
	std::string target = getTargetName(legacyName);
 
	// We are sending PM message. On XMPP side, user is sending PM using the particular channel,
 
	// for example #room@irc.freenode.org/hanzz. On IRC side, we are forwarding this message
backends/libcommuni/session.cpp
Show inline comments
 
@@ -81,6 +81,11 @@ void MyIrcSession::on_connected() {
 
	}
 
}
 

	
 
void MyIrcSession::addPM(const std::string &name, const std::string &room) {
 
	LOG4CXX_INFO(logger, user << ": Adding PM " << name << " " << room);
 
	m_pms[name] = room;
 
}
 

	
 
void MyIrcSession::on_socketError(QAbstractSocket::SocketError error) {
 
	std::string reason;
 
	switch(error) {
 
@@ -253,13 +258,18 @@ void MyIrcSession::on_messageReceived(IrcMessage *message) {
 
	else {
 
		std::string nickname = TO_UTF8(m->nick());
 
		correctNickname(nickname);
 
		LOG4CXX_INFO(logger, user << ": Corrected nickname " << nickname);
 
		if (m_pms.find(nickname) != m_pms.end()) {
 
			if (hasIRCBuddy(m_pms[nickname], nickname)) {
 
			std::string room = m_pms[nickname].substr(0, m_pms[nickname].find("/"));
 
			room = room.substr(0, room.find("@"));
 
			if (hasIRCBuddy(room, nickname)) {
 
				LOG4CXX_INFO(logger, nickname);
 
				np->handleMessage(user, m_pms[nickname] + suffix, TO_UTF8(msg), nickname, TO_UTF8(html), "", false, true);
 
				LOG4CXX_INFO(logger, room << " " << suffix);
 
				np->handleMessage(user, room + suffix, TO_UTF8(msg), nickname, TO_UTF8(html), "", false, true);
 
				return;
 
			}
 
			else {
 
				LOG4CXX_INFO(logger, user << ": nickname not found " << nickname);
 
				nickname = nickname + suffix;
 
			}
 
		}
backends/libcommuni/session.h
Show inline comments
 
@@ -95,9 +95,7 @@ public:
 
	// for example #room@irc.freenode.org/hanzz. On IRC side, we are forwarding this message
 
	// just to "hanzz". Therefore we have to somewhere store, that message from "hanzz" should
 
	// be mapped to #room@irc.freenode.org/hanzz.
 
	void addPM(const std::string &name, const std::string &room) {
 
		m_pms[name] = room;
 
	}
 
	void addPM(const std::string &name, const std::string &room);
 

	
 
	void setIdentify(const std::string &identify) {
 
		m_identify = identify;
spectrum/src/tests/irc_test2.cfg
Show inline comments
 
new file 100644
 
[service]
 
jid = localhost
 
password = secret
 
server = 127.0.0.1
 
port = 5222
 
server_mode = 1
 
backend_host=127.0.0.1
 
pidfile=./test.pid
 
# < this option doesn't work yet
 
#backend_port=10001
 
#admin_jid=admin@localhost
 
admin_password=test
 
#cert=server.pfx #patch to PKCS#12 certificate
 
#cert_password=test #password to that certificate if any
 
users_per_backend=10
 
#backend=../..//backends/swiften/spectrum2_swiften_backend
 
#backend=../../../backends/twitter/spectrum2_twitter_backend
 
backend=../../../backends/libcommuni/spectrum2_libcommuni_backend
 
protocol=prpl-jabber
 
#protocol=prpl-msn
 
#protocol=any
 
#protocol=prpl-icq
 
working_dir=./
 
portfile=./$jid.port
 

	
 
[backend]
 
#default_avatar=catmelonhead.jpg
 
#no_vcard_fetch=true
 

	
 
[logging]
 
#config=logging.cfg # log4cxx/log4j logging configuration file
 
#backend_config=/home/hanzz/code/libtransport/spectrum/src/backend-logging.cfg # log4cxx/log4j logging configuration file for backends
 

	
 
[database]
 
type=sqlite3 # or "none" without database backend
 
database=users.sqlite
 
prefix=twitter
 
#type = mysql # or "none" without database backend.......................................................................................................................
 
#database = test
 
#prefix=
 
#user=root
 
#password=yourrootsqlpassword
 
#encryption_key=hanzzik
spectrum/src/tests/muc_pm.py
Show inline comments
 
new file 100644
 
import optparse
 
import sys
 
import time
 
import subprocess
 
import os
 

	
 
import sleekxmpp
 

	
 

	
 
class Responder(sleekxmpp.ClientXMPP):
 
	def __init__(self, jid, password, room, nick):
 
		sleekxmpp.ClientXMPP.__init__(self, jid, password)
 
		self.room = room
 
		self.nick = nick
 
		self.finished = False
 
		self.add_event_handler("session_start", self.start)
 
		self.add_event_handler("message", self.message)
 

	
 
		self.tests = {}
 

	
 
	def message(self, msg):
 
			self.send_message(mto=self.room + "/client",
 
							mbody="echo %s" % msg['body'],
 
							mtype='chat')
 

	
 
	def start(self, event):
 
		self.plugin['xep_0045'].joinMUC(self.room, self.nick, wait=True)
 

	
 
class Client(sleekxmpp.ClientXMPP):
 
	def __init__(self, jid, password, room, nick):
 
		sleekxmpp.ClientXMPP.__init__(self, jid, password)
 
		self.room = room
 
		self.nick = nick
 
		self.add_event_handler("session_start", self.start)
 
		self.add_event_handler("message", self.message)
 
		self.finished = False
 

	
 
		self.tests = {}
 
		self.tests["echo_received"] = ["libcommuni: Send and receive private messages", False]
 

	
 
	def message(self, msg):
 
		if msg['body'] == "echo abc" and msg['from'] == self.room + "/responder":
 
			self.tests["echo_received"][1] = True
 
			self.finished = True
 

	
 
	def start(self, event):
 
		self.getRoster()
 
		self.sendPresence()
 
		self.plugin['xep_0045'].joinMUC(self.room, self.nick, wait=True)
 
		self.send_message(mto=self.room + "/responder", mbody="abc", mtype='chat')
spectrum/src/tests/start.py
Show inline comments
 
@@ -7,73 +7,110 @@ import os
 
import sleekxmpp
 
import imp
 

	
 
def single_test(Client, Responder):
 
	os.system("../spectrum2 -n ./irc_test.cfg > spectrum2.log &")
 
	os.system("ngircd -f ngircd.conf &")
 
	time.sleep(1)
 
	responder = Responder("responder@localhost", "password", "#channel@localhost", "responder")
 
	responder.register_plugin('xep_0030')  # Service Discovery
 
	responder.register_plugin('xep_0045')  # Multi-User Chat
 
	responder.register_plugin('xep_0199')  # XMPP Ping
 
	responder['feature_mechanisms'].unencrypted_plain = True
 

	
 
	if responder.connect():
 
		responder.process(block=False)
 
	else:
 
		print "connect() failed"
 
		sys.exit(1)
 

	
 
	client = Client("client@localhost", "password", "#channel@localhost", "client")
 
	client.register_plugin('xep_0030')  # Service Discovery
 
	client.register_plugin('xep_0045')  # Multi-User Chat
 
	client.register_plugin('xep_0199')  # XMPP Ping
 
	client['feature_mechanisms'].unencrypted_plain = True
 

	
 
	time.sleep(2)
 

	
 
	if client.connect():
 
		client.process(block=False)
 
	else:
 
		print "connect() failed"
 
		sys.exit(1)
 

	
 
	max_time = 60
 
	while not client.finished and not responder.finished and max_time > 0:
 
class BaseTest:
 
	def __init__(self, config, server_mode, room):
 
		self.config = config
 
		self.server_mode = server_mode
 
		self.room = room
 

	
 
	def start(self, Client, Responder):
 
		self.pre_test()
 
		os.system("../spectrum2 -n ./" + self.config + " > spectrum2.log &")
 
		time.sleep(1)
 
		max_time -= 1
 
	client.disconnect()
 
	responder.disconnect()
 

	
 
	os.system("killall spectrum2")
 
	os.system("killall ngircd")
 
	os.system("killall spectrum2_libcommuni_backend 2>/dev/null")
 

	
 
	ret = True
 
	tests = []
 
	tests += client.tests.values()
 
	tests += responder.tests.values()
 
	for v in tests:
 
		if v[1]:
 
			print v[0] + ": PASSED"
 

	
 
		responder = Responder("responder@localhost", "password", self.room, "responder")
 
		responder.register_plugin('xep_0030')  # Service Discovery
 
		responder.register_plugin('xep_0045')  # Multi-User Chat
 
		responder.register_plugin('xep_0199')  # XMPP Ping
 
		responder['feature_mechanisms'].unencrypted_plain = True
 

	
 
		if responder.connect():
 
			responder.process(block=False)
 
		else:
 
			print v[0] + ": FAILED"
 
			ret = False
 
			print "connect() failed"
 
			sys.exit(1)
 

	
 
		client = Client("client@localhost", "password", self.room, "client")
 
		client.register_plugin('xep_0030')  # Service Discovery
 
		client.register_plugin('xep_0045')  # Multi-User Chat
 
		client.register_plugin('xep_0199')  # XMPP Ping
 
		client['feature_mechanisms'].unencrypted_plain = True
 

	
 
		time.sleep(2)
 

	
 
		if client.connect():
 
			client.process(block=False)
 
		else:
 
			print "connect() failed"
 
			sys.exit(1)
 

	
 
		max_time = 60
 
		while not client.finished and not responder.finished and max_time > 0:
 
			time.sleep(1)
 
			max_time -= 1
 
		client.disconnect()
 
		responder.disconnect()
 

	
 
		os.system("killall spectrum2")
 
		self.post_test()
 

	
 
		ret = True
 
		tests = []
 
		tests += client.tests.values()
 
		tests += responder.tests.values()
 
		for v in tests:
 
			if v[1]:
 
				print v[0] + ": PASSED"
 
			else:
 
				print v[0] + ": FAILED"
 
				ret = False
 

	
 
	if not ret:
 
		os.system("cat spectrum2.log")
 
		if not ret:
 
			os.system("cat spectrum2.log")
 

	
 
	return ret
 
		return ret
 

	
 
class LibcommuniServerModeSingleServerConf(BaseTest):
 
	def __init__(self):
 
		BaseTest.__init__(self, "irc_test.cfg", True, "#channel@localhost")
 

	
 
	def pre_test(self):
 
		os.system("ngircd -f ngircd.conf &")
 

	
 
	def post_test(self):
 
		os.system("killall ngircd 2>/dev/null")
 
		os.system("killall spectrum2_libcommuni_backend 2>/dev/null")
 

	
 
class LibcommuniServerModeConf(BaseTest):
 
	def __init__(self):
 
		BaseTest.__init__(self, "irc_test2.cfg", True, "#channel%localhost@localhost")
 

	
 
	def pre_test(self):
 
		os.system("ngircd -f ngircd.conf &")
 

	
 
	def post_test(self):
 
		os.system("killall ngircd 2>/dev/null")
 
		os.system("killall spectrum2_libcommuni_backend 2>/dev/null")
 

	
 
configurations = []
 
configurations.append(LibcommuniServerModeSingleServerConf())
 
configurations.append(LibcommuniServerModeConf())
 

	
 
exitcode = 0
 
for f in os.listdir("."):
 
	if not f.endswith(".py") or f == "start.py":
 
		continue
 

	
 
	print "Starting " + f + " test ..."
 
	test = imp.load_source('test', './' + f)
 
	ret = single_test(test.Client, test.Responder)
 
	if not ret:
 
		exitcode = -1
 

	
 
for conf in configurations:
 
	for f in os.listdir("."):
 
		if not f.endswith(".py") or f == "start.py":
 
			continue
 

	
 
		print conf.__class__.__name__ + ": Starting " + f + " test ..."
 
		test = imp.load_source('test', './' + f)
 
		try:
 
			ret = conf.start(test.Client, test.Responder)
 
		except:
 
			ret = False
 
		if not ret:
 
			exitcode = -1
 

	
 
sys.exit(exitcode)
 

	
0 comments (0 inline, 0 general)