diff options
4 files changed, 272 insertions, 3 deletions
diff --git a/src/net/java/sip/communicator/impl/protocol/irc/command/Msg.java b/src/net/java/sip/communicator/impl/protocol/irc/command/Msg.java index 06bdef2..03dda0b 100644 --- a/src/net/java/sip/communicator/impl/protocol/irc/command/Msg.java +++ b/src/net/java/sip/communicator/impl/protocol/irc/command/Msg.java @@ -16,6 +16,11 @@ import net.java.sip.communicator.impl.protocol.irc.*; public class Msg implements Command { /** + * Index of start of nick and message parameters. + */ + private static final int END_OF_MSG_COMMAND_PREFIX = 5; + + /** * Instance of the IRC connection. */ private IrcConnection connection; @@ -47,15 +52,32 @@ public class Msg implements Command @Override public void execute(final String source, final String line) { + if (line.length() < END_OF_MSG_COMMAND_PREFIX) + { + return; + } final String part = line.substring(5); int endOfNick = part.indexOf(' '); if (endOfNick == -1) { throw new IllegalArgumentException("Invalid private message " - + "format. Message was not sent."); + + "format: Expecting both nick and message. Message was " + + "not sent."); } final String target = part.substring(0, endOfNick); - final String command = part.substring(endOfNick + 1); - this.connection.getClient().message(target, command); + if (target.length() == 0) + { + throw new IllegalArgumentException("Invalid private message " + + "format: Zero-length nick is not allowed. Message was not " + + "sent."); + } + final String message = part.substring(endOfNick + 1); + if (message.length() == 0) + { + throw new IllegalArgumentException("Invalid private message " + + "format: Zero-length message is not allowed. Message was not " + + "sent."); + } + this.connection.getClient().message(target, message); } } diff --git a/src/net/java/sip/communicator/impl/protocol/irc/command/Nick.java b/src/net/java/sip/communicator/impl/protocol/irc/command/Nick.java index 06b5e4c..7d3d257 100644 --- a/src/net/java/sip/communicator/impl/protocol/irc/command/Nick.java +++ b/src/net/java/sip/communicator/impl/protocol/irc/command/Nick.java @@ -46,6 +46,11 @@ public class Nick implements Command @Override public void execute(final String source, final String line) { + if (line.length() <= 5) + { + // no name parameter available, so nothing to do here + return; + } final String part = line.substring(6); final String newNick; int indexOfSep = part.indexOf(' '); diff --git a/test/net/java/sip/communicator/impl/protocol/irc/command/MsgTest.java b/test/net/java/sip/communicator/impl/protocol/irc/command/MsgTest.java new file mode 100644 index 0000000..5e45794 --- /dev/null +++ b/test/net/java/sip/communicator/impl/protocol/irc/command/MsgTest.java @@ -0,0 +1,131 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package net.java.sip.communicator.impl.protocol.irc.command; + +import junit.framework.*; +import net.java.sip.communicator.impl.protocol.irc.*; + +import org.easymock.*; + +import com.ircclouds.irc.api.*; + +/** + * @author Danny van Heumen + */ +public class MsgTest + extends TestCase +{ + public void testConstruction() + { + new Msg(); + } + + public void testGoodInit() + { + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(connection); + + Msg msg = new Msg(); + msg.init(null, connection); + } + + public void testBadInit() + { + ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + EasyMock.replay(provider); + + Msg msg = new Msg(); + try + { + msg.init(provider, null); + Assert.fail("Should not reach this, expected IAE."); + } + catch (IllegalArgumentException e) + { + } + } + + public void testMessageNoNickNoMsg() + { + ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(provider, connection); + + Msg msg = new Msg(); + msg.init(provider, connection); + msg.execute("#test", "/msg"); + } + + public void testMessageZeroLengthMsg() + { + ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(provider, connection); + + Msg msg = new Msg(); + msg.init(provider, connection); + try + { + msg.execute("#test", "/msg "); + Assert.fail(); + } + catch (IllegalArgumentException e) + { + } + } + + public void testMessageZeroLengthNickMsg() + { + ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(provider, connection); + + Msg msg = new Msg(); + msg.init(provider, connection); + try + { + msg.execute("#test", "/msg "); + Assert.fail(); + } + catch (IllegalArgumentException e) + { + } + } + + public void testMessageNickZeroLengthMsg() + { + ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(provider, connection); + + Msg msg = new Msg(); + msg.init(provider, connection); + try + { + msg.execute("#test", "/msg target "); + Assert.fail(); + } + catch (IllegalArgumentException e) + { + } + } + + public void testPrivateMessage() + { + ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + IRCApi client = EasyMock.createMock(IRCApi.class); + EasyMock.expect(connection.getClient()).andReturn(client); + client.message(EasyMock.eq("target"), EasyMock.eq("This is my target message.")); + EasyMock.expectLastCall(); + EasyMock.replay(provider, connection); + + Msg msg = new Msg(); + msg.init(provider, connection); + msg.execute("#test", "/msg target This is my target message."); + } +} diff --git a/test/net/java/sip/communicator/impl/protocol/irc/command/NickTest.java b/test/net/java/sip/communicator/impl/protocol/irc/command/NickTest.java new file mode 100644 index 0000000..1400c33 --- /dev/null +++ b/test/net/java/sip/communicator/impl/protocol/irc/command/NickTest.java @@ -0,0 +1,111 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package net.java.sip.communicator.impl.protocol.irc.command; + +import junit.framework.*; +import net.java.sip.communicator.impl.protocol.irc.*; + +import org.easymock.*; + +/** + * @author Danny van Heumen + */ +public class NickTest + extends TestCase +{ + + public NickTest(String testName) + { + super(testName); + } + + public void testConstruction() + { + new Nick(); + } + + public void testNullProviderInit() + { + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(connection); + + Nick nick = new Nick(); + nick.init(null, connection); + } + + public void testNullConnectionInit() + { + ProtocolProviderServiceIrcImpl provider = + EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + EasyMock.replay(provider); + + Nick nick = new Nick(); + try + { + nick.init(provider, null); + Assert.fail("Should not reach this as we expected an IAE for null" + + " connection."); + } + catch (IllegalArgumentException e) + { + // IAE was expected. + } + } + + public void testEmptyNickCommand() + { + ProtocolProviderServiceIrcImpl provider = + EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(provider, connection); + + Nick nick = new Nick(); + nick.init(provider, connection); + nick.execute("#test", "/nick"); + } + + public void testEmptyNickCommandWithSpace() + { + ProtocolProviderServiceIrcImpl provider = + EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(provider, connection); + + Nick nick = new Nick(); + nick.init(provider, connection); + nick.execute("#test", "/nick "); + } + + public void testEmptyNickCommandWithDoubleSpace() + { + ProtocolProviderServiceIrcImpl provider = + EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + EasyMock.replay(provider, connection); + + Nick nick = new Nick(); + nick.init(provider, connection); + nick.execute("#test", "/nick "); + } + + public void testNickCommandWithNickAndSpace() + { + ProtocolProviderServiceIrcImpl provider = + EasyMock.createMock(ProtocolProviderServiceIrcImpl.class); + IrcConnection connection = EasyMock.createMock(IrcConnection.class); + IdentityManager idmgr = EasyMock.createMock(IdentityManager.class); + + EasyMock.expect(connection.getIdentityManager()).andReturn(idmgr); + idmgr.setNick(EasyMock.eq("myNewN1ck")); + EasyMock.expectLastCall(); + EasyMock.replay(provider, connection, idmgr); + + Nick nick = new Nick(); + nick.init(provider, connection); + nick.execute("#test", "/nick myNewN1ck "); + } +} |