/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/net/ReplayLinkTest.java
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 23package org.mobicents.protocols.smpp.net; 24 25import static org.testng.Assert.assertEquals; 26import static org.testng.Assert.assertFalse; 27import static org.testng.Assert.assertNotNull; 28import static org.testng.Assert.assertTrue; 29import static org.testng.Assert.fail; 30 31import java.io.ByteArrayInputStream; 32import java.io.ByteArrayOutputStream; 33import java.io.IOException; 34import java.util.ArrayList; 35import java.util.List; 36 37import org.testng.annotations.BeforeMethod; 38import org.testng.annotations.Test; 39 40import org.mobicents.protocols.smpp.Address; 41import org.mobicents.protocols.smpp.message.BindTransceiver; 42import org.mobicents.protocols.smpp.message.BindTransceiverResp; 43import org.mobicents.protocols.smpp.message.BindTransmitter; 44import org.mobicents.protocols.smpp.message.DeliverSM; 45import org.mobicents.protocols.smpp.message.DeliverSMResp; 46import org.mobicents.protocols.smpp.message.SMPPPacket; 47import org.mobicents.protocols.smpp.message.SubmitSM; 48import org.mobicents.protocols.smpp.message.SubmitSMResp; 49import org.mobicents.protocols.smpp.message.Unbind; 50import org.mobicents.protocols.smpp.message.UnbindResp; 51import org.mobicents.protocols.smpp.message.tlv.Tag; 52import org.mobicents.protocols.smpp.net.ReadTimeoutException; 53import org.mobicents.protocols.smpp.net.ReplayLink; 54import org.mobicents.protocols.smpp.util.PacketEncoder; 55import org.mobicents.protocols.smpp.util.PacketEncoderImpl; 56import org.mobicents.protocols.smpp.version.SMPPVersion; 57 58public class ReplayLinkTest { 59 60 private ByteArrayInputStream inbound; 61 private ByteArrayInputStream outbound; 62 private List<SMPPPacket> session = new ArrayList<SMPPPacket>(); 63 64 @Test(expectedExceptions = IllegalStateException.class) 65 public void testReadThrowsExceptionInUnconnectedState() throws Exception { 66 ReplayLink link = new ReplayLink(inbound, outbound); 67 assertFalse(link.isConnected()); 68 link.read(); 69 } 70 71 @Test(expectedExceptions = IllegalStateException.class) 72 public void testWriteThrowsExceptionInUnconnectedState() throws Exception { 73 ReplayLink link = new ReplayLink(inbound, outbound); 74 assertFalse(link.isConnected()); 75 link.write(new BindTransmitter(), true); 76 } 77 78 @Test(expectedExceptions = ReadTimeoutException.class) 79 public void testReadThrowsExceptionAfterTimeout() throws Exception { 80 ReplayLink link = new ReplayLink(inbound, outbound); 81 link.setTimeout(1000); 82 assertFalse(link.isConnected()); 83 link.connect(); 84 assertTrue(link.isConnected()); 85 link.read(); 86 } 87 88 @Test 89 public void testSession() throws Exception { 90 ReplayLink link = new ReplayLink(inbound, outbound); 91 link.connect(); 92 SMPPPacket packet; 93 packet = doRequest(link, BindTransceiver.class, 0); 94 link.write(packet, true); 95 doResponse(link, BindTransceiverResp.class, 1); 96 packet = doRequest(link, SubmitSM.class, 2); 97 link.write(packet, true); 98 doResponse(link, SubmitSMResp.class, 3); 99 doResponse(link, DeliverSM.class, 4); 100 packet = doRequest(link, DeliverSMResp.class, 5); 101 link.write(packet, true); 102 packet = doRequest(link, Unbind.class, 6); 103 link.write(packet, true); 104 doResponse(link, UnbindResp.class, 7); 105 } 106 107 @Test 108 public void testPacketIsNotDiscardedWhenReadTimeoutOccurs() throws Exception { 109 ReplayLink link = new ReplayLink(inbound, outbound); 110 link.setTimeout(500); 111 link.connect(); 112 try { 113 link.read(); 114 fail("Should have thrown ReadTimeoutException."); 115 } catch (ReadTimeoutException x) { 116 } 117 link.write(link.getNextOutbound(), true); 118 SMPPPacket packet = link.read(); 119 assertPacket(packet, BindTransceiverResp.class, 1); 120 } 121 122 @BeforeMethod 123 void setupStreams() throws Exception { 124 ByteArrayOutputStream outPackets = new ByteArrayOutputStream(); 125 ByteArrayOutputStream inPackets = new ByteArrayOutputStream(); 126 PacketEncoder outEncoder = new PacketEncoderImpl(outPackets); 127 PacketEncoder inEncoder = new PacketEncoderImpl(inPackets); 128 addBindPackets(outEncoder, inEncoder); 129 addSubmitPackets(outEncoder, inEncoder); 130 addDeliverPackets(outEncoder, inEncoder); 131 addUnbindPackets(outEncoder, inEncoder); 132 inbound = new ByteArrayInputStream(inPackets.toByteArray()); 133 outbound = new ByteArrayInputStream(outPackets.toByteArray()); 134 } 135 136 private SMPPPacket doRequest(ReplayLink link, Class<?> type, int sessionIndex) throws Exception { 137 SMPPPacket packet = link.getNextOutbound(); 138 assertPacket(packet, type, sessionIndex); 139 return packet; 140 } 141 142 private void doResponse(ReplayLink link, Class<?> type, int sessionIndex) throws Exception { 143 assertPacket(link.read(), type, sessionIndex); 144 } 145 146 private void assertPacket(SMPPPacket packet, Class<?> type, int sessionIndex) { 147 assertNotNull(packet); 148 assertEquals(packet.getClass(), type); 149 assertEquals(packet, session.get(sessionIndex)); 150 } 151 152 private void addBindPackets(PacketEncoder outEncoder, PacketEncoder inEncoder) throws IOException { 153 BindTransceiver bind = new BindTransceiver(); 154 bind.setSequenceNum(1L); 155 bind.setSystemId("systemId"); 156 bind.setPassword("password"); 157 bind.setSystemType("systemType"); 158 bind.setAddressTon(1); 159 bind.setAddressNpi(1); 160 bind.setAddressRange("12345[67]"); 161 BindTransceiverResp bindResp = new BindTransceiverResp(bind); 162 bindResp.setSystemId("smsc"); 163 bindResp.setTLV(Tag.SC_INTERFACE_VERSION, 164 new Integer(SMPPVersion.VERSION_5_0.getVersionID())); 165 bind.writeTo(outEncoder); 166 bindResp.writeTo(inEncoder); 167 session.add(bind); 168 session.add(bindResp); 169 } 170 171 private void addSubmitPackets(PacketEncoder outEncoder, PacketEncoder inEncoder) throws IOException { 172 SubmitSM submit = new SubmitSM(); 173 submit.setSequenceNum(2L); 174 submit.setDestination(new Address(1, 1, "54321")); 175 submit.setMessage("Message Text".getBytes("US-ASCII")); 176 SubmitSMResp submitResp = new SubmitSMResp(submit); 177 submitResp.setMessageId("message-1"); 178 submit.writeTo(outEncoder); 179 submitResp.writeTo(inEncoder); 180 session.add(submit); 181 session.add(submitResp); 182 } 183 184 private void addDeliverPackets(PacketEncoder outEncoder, PacketEncoder inEncoder) throws IOException { 185 DeliverSM deliver = new DeliverSM(); 186 deliver.setSequenceNum(1L); 187 deliver.setDestination(new Address(1, 1, "123456")); 188 deliver.setMessage("Another Message".getBytes("US-ASCII")); 189 DeliverSMResp deliverResp = new DeliverSMResp(deliver); 190 deliverResp.writeTo(outEncoder); 191 deliver.writeTo(inEncoder); 192 session.add(deliver); 193 session.add(deliverResp); 194 } 195 196 private void addUnbindPackets(PacketEncoder outEncoder, PacketEncoder inEncoder) throws IOException { 197 Unbind unbind = new Unbind(); 198 unbind.setSequenceNum(3L); 199 UnbindResp unbindResp = new UnbindResp(unbind); 200 unbind.writeTo(outEncoder); 201 unbindResp.writeTo(inEncoder); 202 session.add(unbind); 203 session.add(unbindResp); 204 } 205}