/protocols/ss7/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/service/sms/SM_RP_DATest.java

http://mobicents.googlecode.com/ · Java · 239 lines · 144 code · 57 blank · 38 comment · 3 complexity · 004a7ff2a54d18a74130be6c6ab0958b 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.ss7.map.service.sms;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertTrue;
  25. import java.io.ByteArrayInputStream;
  26. import java.io.ByteArrayOutputStream;
  27. import java.io.InputStream;
  28. import java.io.ObjectInputStream;
  29. import java.io.ObjectOutputStream;
  30. import java.util.Arrays;
  31. import org.mobicents.protocols.asn.AsnInputStream;
  32. import org.mobicents.protocols.asn.AsnOutputStream;
  33. import org.mobicents.protocols.asn.Tag;
  34. import org.mobicents.protocols.ss7.map.api.primitives.AddressNature;
  35. import org.mobicents.protocols.ss7.map.api.primitives.AddressString;
  36. import org.mobicents.protocols.ss7.map.api.primitives.IMSI;
  37. import org.mobicents.protocols.ss7.map.api.primitives.NumberingPlan;
  38. import org.mobicents.protocols.ss7.map.primitives.AddressStringImpl;
  39. import org.mobicents.protocols.ss7.map.primitives.IMSIImpl;
  40. import org.mobicents.protocols.ss7.map.primitives.LMSIImpl;
  41. import org.testng.annotations.Test;
  42. /**
  43. *
  44. * @author sergey vetyutnev
  45. *
  46. */
  47. public class SM_RP_DATest {
  48. private byte[] getEncodedData_ServiceCentreAddressDA() {
  49. return new byte[] { -124, 7, -111, 33, 49, -107, 6, 105, 0 };
  50. }
  51. private byte[] getEncodedData_LMSI() {
  52. return new byte[] { (byte)129, 4, 0, 7, (byte)144, (byte)178 };
  53. }
  54. private byte[] getEncodedData_IMSI() {
  55. return new byte[] { -128, 8, 64, 1, 4, 34, 18, 22, 69, -9 };
  56. }
  57. private byte[] getEncodedData_No() {
  58. return new byte[] { (byte)133, 0 };
  59. }
  60. @Test(groups = { "functional.decode","service.sms"})
  61. public void testDecode() throws Exception {
  62. byte[] rawData = getEncodedData_ServiceCentreAddressDA();
  63. AsnInputStream asn = new AsnInputStream(rawData);
  64. int tag = asn.readTag();
  65. SM_RP_DAImpl da = new SM_RP_DAImpl();
  66. da.decodeAll(asn);
  67. assertEquals( tag,4);
  68. assertEquals( asn.getTagClass(),Tag.CLASS_CONTEXT_SPECIFIC);
  69. AddressString nnm = da.getServiceCentreAddressDA();
  70. assertEquals( nnm.getAddressNature(),AddressNature.international_number);
  71. assertEquals( nnm.getNumberingPlan(),NumberingPlan.ISDN);
  72. assertEquals( nnm.getAddress(),"121359609600");
  73. rawData = getEncodedData_LMSI();
  74. asn = new AsnInputStream(rawData);
  75. tag = asn.readTag();
  76. da = new SM_RP_DAImpl();
  77. da.decodeAll(asn);
  78. assertEquals( tag,1);
  79. assertEquals( asn.getTagClass(),Tag.CLASS_CONTEXT_SPECIFIC);
  80. assertTrue(Arrays.equals(new byte[] { 0, 7, -112, -78 }, da.getLMSI().getData()));
  81. rawData = getEncodedData_IMSI();
  82. asn = new AsnInputStream(rawData);
  83. tag = asn.readTag();
  84. da = new SM_RP_DAImpl();
  85. da.decodeAll(asn);
  86. assertEquals( tag,0);
  87. assertEquals( asn.getTagClass(),Tag.CLASS_CONTEXT_SPECIFIC);
  88. IMSI imsi = da.getIMSI();
  89. assertEquals( imsi.getData(),"041040222161547");
  90. // assertEquals( (long)imsi.getMCC(),41);
  91. // assertEquals( (long)imsi.getMNC(),4);
  92. // assertEquals( imsi.getMSIN(),"0222161547");
  93. rawData = getEncodedData_No();
  94. asn = new AsnInputStream(rawData);
  95. tag = asn.readTag();
  96. da = new SM_RP_DAImpl();
  97. da.decodeAll(asn);
  98. assertEquals( tag,5);
  99. assertEquals( asn.getTagClass(),Tag.CLASS_CONTEXT_SPECIFIC);
  100. assertTrue(da.getServiceCentreAddressDA() == null);
  101. assertTrue(da.getIMSI() == null);
  102. assertTrue(da.getLMSI() == null);
  103. }
  104. @Test(groups = { "functional.encode","service.sms"})
  105. public void testEncode() throws Exception {
  106. AddressStringImpl astr = new AddressStringImpl(AddressNature.international_number, NumberingPlan.ISDN, "121359609600");
  107. SM_RP_DAImpl da = new SM_RP_DAImpl(astr);
  108. AsnOutputStream asnOS = new AsnOutputStream();
  109. da.encodeAll(asnOS);
  110. byte[] encodedData = asnOS.toByteArray();
  111. byte[] rawData = getEncodedData_ServiceCentreAddressDA();
  112. assertTrue( Arrays.equals(rawData,encodedData));
  113. LMSIImpl lmsi = new LMSIImpl(new byte[] { 0, 7, -112, -78 });
  114. da = new SM_RP_DAImpl(lmsi);
  115. asnOS = new AsnOutputStream();
  116. da.encodeAll(asnOS);
  117. encodedData = asnOS.toByteArray();
  118. rawData = getEncodedData_LMSI();
  119. assertTrue( Arrays.equals(rawData,encodedData));
  120. IMSIImpl imsi = new IMSIImpl("041040222161547");
  121. da = new SM_RP_DAImpl(imsi);
  122. asnOS = new AsnOutputStream();
  123. da.encodeAll(asnOS);
  124. encodedData = asnOS.toByteArray();
  125. rawData = getEncodedData_IMSI();
  126. assertTrue( Arrays.equals(rawData,encodedData));
  127. da = new SM_RP_DAImpl();
  128. asnOS = new AsnOutputStream();
  129. da.encodeAll(asnOS);
  130. encodedData = asnOS.toByteArray();
  131. rawData = getEncodedData_No();
  132. assertTrue( Arrays.equals(rawData,encodedData));
  133. }
  134. @Test(groups = { "functional.serialize", "service.sms" })
  135. public void testSerialization() throws Exception {
  136. AddressStringImpl astr = new AddressStringImpl(AddressNature.international_number, NumberingPlan.ISDN, "121359609600");
  137. SM_RP_DAImpl original = new SM_RP_DAImpl(astr);
  138. // serialize
  139. ByteArrayOutputStream out = new ByteArrayOutputStream();
  140. ObjectOutputStream oos = new ObjectOutputStream(out);
  141. oos.writeObject(original);
  142. oos.close();
  143. // deserialize
  144. byte[] pickled = out.toByteArray();
  145. InputStream in = new ByteArrayInputStream(pickled);
  146. ObjectInputStream ois = new ObjectInputStream(in);
  147. Object o = ois.readObject();
  148. SM_RP_DAImpl copy = (SM_RP_DAImpl) o;
  149. //test result
  150. assertEquals(copy.getServiceCentreAddressDA(), original.getServiceCentreAddressDA());
  151. LMSIImpl lmsi = new LMSIImpl(new byte[] { 0, 7, -112, -78 });
  152. original = new SM_RP_DAImpl(lmsi);
  153. // serialize
  154. out = new ByteArrayOutputStream();
  155. oos = new ObjectOutputStream(out);
  156. oos.writeObject(original);
  157. oos.close();
  158. // deserialize
  159. pickled = out.toByteArray();
  160. in = new ByteArrayInputStream(pickled);
  161. ois = new ObjectInputStream(in);
  162. o = ois.readObject();
  163. copy = (SM_RP_DAImpl) o;
  164. //test result
  165. assertEquals(copy.getLMSI(), original.getLMSI());
  166. IMSIImpl imsi = new IMSIImpl("041040222161547");
  167. original = new SM_RP_DAImpl(imsi);
  168. // serialize
  169. out = new ByteArrayOutputStream();
  170. oos = new ObjectOutputStream(out);
  171. oos.writeObject(original);
  172. oos.close();
  173. // deserialize
  174. pickled = out.toByteArray();
  175. in = new ByteArrayInputStream(pickled);
  176. ois = new ObjectInputStream(in);
  177. o = ois.readObject();
  178. copy = (SM_RP_DAImpl) o;
  179. //test result
  180. assertEquals(copy.getIMSI(), original.getIMSI());
  181. }
  182. }