/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/net/ReplayLinkTest.java

http://mobicents.googlecode.com/ · Java · 205 lines · 164 code · 20 blank · 21 comment · 0 complexity · 348f9141788f1d123f956b93133a7730 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.smpp.net;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertFalse;
  25. import static org.testng.Assert.assertNotNull;
  26. import static org.testng.Assert.assertTrue;
  27. import static org.testng.Assert.fail;
  28. import java.io.ByteArrayInputStream;
  29. import java.io.ByteArrayOutputStream;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import org.testng.annotations.BeforeMethod;
  34. import org.testng.annotations.Test;
  35. import org.mobicents.protocols.smpp.Address;
  36. import org.mobicents.protocols.smpp.message.BindTransceiver;
  37. import org.mobicents.protocols.smpp.message.BindTransceiverResp;
  38. import org.mobicents.protocols.smpp.message.BindTransmitter;
  39. import org.mobicents.protocols.smpp.message.DeliverSM;
  40. import org.mobicents.protocols.smpp.message.DeliverSMResp;
  41. import org.mobicents.protocols.smpp.message.SMPPPacket;
  42. import org.mobicents.protocols.smpp.message.SubmitSM;
  43. import org.mobicents.protocols.smpp.message.SubmitSMResp;
  44. import org.mobicents.protocols.smpp.message.Unbind;
  45. import org.mobicents.protocols.smpp.message.UnbindResp;
  46. import org.mobicents.protocols.smpp.message.tlv.Tag;
  47. import org.mobicents.protocols.smpp.net.ReadTimeoutException;
  48. import org.mobicents.protocols.smpp.net.ReplayLink;
  49. import org.mobicents.protocols.smpp.util.PacketEncoder;
  50. import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
  51. import org.mobicents.protocols.smpp.version.SMPPVersion;
  52. public class ReplayLinkTest {
  53. private ByteArrayInputStream inbound;
  54. private ByteArrayInputStream outbound;
  55. private List<SMPPPacket> session = new ArrayList<SMPPPacket>();
  56. @Test(expectedExceptions = IllegalStateException.class)
  57. public void testReadThrowsExceptionInUnconnectedState() throws Exception {
  58. ReplayLink link = new ReplayLink(inbound, outbound);
  59. assertFalse(link.isConnected());
  60. link.read();
  61. }
  62. @Test(expectedExceptions = IllegalStateException.class)
  63. public void testWriteThrowsExceptionInUnconnectedState() throws Exception {
  64. ReplayLink link = new ReplayLink(inbound, outbound);
  65. assertFalse(link.isConnected());
  66. link.write(new BindTransmitter(), true);
  67. }
  68. @Test(expectedExceptions = ReadTimeoutException.class)
  69. public void testReadThrowsExceptionAfterTimeout() throws Exception {
  70. ReplayLink link = new ReplayLink(inbound, outbound);
  71. link.setTimeout(1000);
  72. assertFalse(link.isConnected());
  73. link.connect();
  74. assertTrue(link.isConnected());
  75. link.read();
  76. }
  77. @Test
  78. public void testSession() throws Exception {
  79. ReplayLink link = new ReplayLink(inbound, outbound);
  80. link.connect();
  81. SMPPPacket packet;
  82. packet = doRequest(link, BindTransceiver.class, 0);
  83. link.write(packet, true);
  84. doResponse(link, BindTransceiverResp.class, 1);
  85. packet = doRequest(link, SubmitSM.class, 2);
  86. link.write(packet, true);
  87. doResponse(link, SubmitSMResp.class, 3);
  88. doResponse(link, DeliverSM.class, 4);
  89. packet = doRequest(link, DeliverSMResp.class, 5);
  90. link.write(packet, true);
  91. packet = doRequest(link, Unbind.class, 6);
  92. link.write(packet, true);
  93. doResponse(link, UnbindResp.class, 7);
  94. }
  95. @Test
  96. public void testPacketIsNotDiscardedWhenReadTimeoutOccurs() throws Exception {
  97. ReplayLink link = new ReplayLink(inbound, outbound);
  98. link.setTimeout(500);
  99. link.connect();
  100. try {
  101. link.read();
  102. fail("Should have thrown ReadTimeoutException.");
  103. } catch (ReadTimeoutException x) {
  104. }
  105. link.write(link.getNextOutbound(), true);
  106. SMPPPacket packet = link.read();
  107. assertPacket(packet, BindTransceiverResp.class, 1);
  108. }
  109. @BeforeMethod
  110. void setupStreams() throws Exception {
  111. ByteArrayOutputStream outPackets = new ByteArrayOutputStream();
  112. ByteArrayOutputStream inPackets = new ByteArrayOutputStream();
  113. PacketEncoder outEncoder = new PacketEncoderImpl(outPackets);
  114. PacketEncoder inEncoder = new PacketEncoderImpl(inPackets);
  115. addBindPackets(outEncoder, inEncoder);
  116. addSubmitPackets(outEncoder, inEncoder);
  117. addDeliverPackets(outEncoder, inEncoder);
  118. addUnbindPackets(outEncoder, inEncoder);
  119. inbound = new ByteArrayInputStream(inPackets.toByteArray());
  120. outbound = new ByteArrayInputStream(outPackets.toByteArray());
  121. }
  122. private SMPPPacket doRequest(ReplayLink link, Class<?> type, int sessionIndex) throws Exception {
  123. SMPPPacket packet = link.getNextOutbound();
  124. assertPacket(packet, type, sessionIndex);
  125. return packet;
  126. }
  127. private void doResponse(ReplayLink link, Class<?> type, int sessionIndex) throws Exception {
  128. assertPacket(link.read(), type, sessionIndex);
  129. }
  130. private void assertPacket(SMPPPacket packet, Class<?> type, int sessionIndex) {
  131. assertNotNull(packet);
  132. assertEquals(packet.getClass(), type);
  133. assertEquals(packet, session.get(sessionIndex));
  134. }
  135. private void addBindPackets(PacketEncoder outEncoder, PacketEncoder inEncoder) throws IOException {
  136. BindTransceiver bind = new BindTransceiver();
  137. bind.setSequenceNum(1L);
  138. bind.setSystemId("systemId");
  139. bind.setPassword("password");
  140. bind.setSystemType("systemType");
  141. bind.setAddressTon(1);
  142. bind.setAddressNpi(1);
  143. bind.setAddressRange("12345[67]");
  144. BindTransceiverResp bindResp = new BindTransceiverResp(bind);
  145. bindResp.setSystemId("smsc");
  146. bindResp.setTLV(Tag.SC_INTERFACE_VERSION,
  147. new Integer(SMPPVersion.VERSION_5_0.getVersionID()));
  148. bind.writeTo(outEncoder);
  149. bindResp.writeTo(inEncoder);
  150. session.add(bind);
  151. session.add(bindResp);
  152. }
  153. private void addSubmitPackets(PacketEncoder outEncoder, PacketEncoder inEncoder) throws IOException {
  154. SubmitSM submit = new SubmitSM();
  155. submit.setSequenceNum(2L);
  156. submit.setDestination(new Address(1, 1, "54321"));
  157. submit.setMessage("Message Text".getBytes("US-ASCII"));
  158. SubmitSMResp submitResp = new SubmitSMResp(submit);
  159. submitResp.setMessageId("message-1");
  160. submit.writeTo(outEncoder);
  161. submitResp.writeTo(inEncoder);
  162. session.add(submit);
  163. session.add(submitResp);
  164. }
  165. private void addDeliverPackets(PacketEncoder outEncoder, PacketEncoder inEncoder) throws IOException {
  166. DeliverSM deliver = new DeliverSM();
  167. deliver.setSequenceNum(1L);
  168. deliver.setDestination(new Address(1, 1, "123456"));
  169. deliver.setMessage("Another Message".getBytes("US-ASCII"));
  170. DeliverSMResp deliverResp = new DeliverSMResp(deliver);
  171. deliverResp.writeTo(outEncoder);
  172. deliver.writeTo(inEncoder);
  173. session.add(deliver);
  174. session.add(deliverResp);
  175. }
  176. private void addUnbindPackets(PacketEncoder outEncoder, PacketEncoder inEncoder) throws IOException {
  177. Unbind unbind = new Unbind();
  178. unbind.setSequenceNum(3L);
  179. UnbindResp unbindResp = new UnbindResp(unbind);
  180. unbind.writeTo(outEncoder);
  181. unbindResp.writeTo(inEncoder);
  182. session.add(unbind);
  183. session.add(unbindResp);
  184. }
  185. }