/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/message/tlv/TLVTableImplTest.java

http://mobicents.googlecode.com/ · Java · 357 lines · 273 code · 41 blank · 43 comment · 2 complexity · c8f12c3dafaf920797ddd25a99e482c3 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.message.tlv;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertFalse;
  25. import static org.testng.Assert.assertNull;
  26. import static org.testng.Assert.assertTrue;
  27. import static org.testng.Assert.fail;
  28. import java.io.ByteArrayOutputStream;
  29. import java.io.IOException;
  30. import java.util.BitSet;
  31. import org.testng.annotations.Test;
  32. import org.mobicents.protocols.smpp.message.param.BitmaskParamDescriptor;
  33. import org.mobicents.protocols.smpp.message.tlv.InvalidSizeForValueException;
  34. import org.mobicents.protocols.smpp.message.tlv.TLVTableImpl;
  35. import org.mobicents.protocols.smpp.message.tlv.Tag;
  36. import org.mobicents.protocols.smpp.util.PacketDecoderImpl;
  37. import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
  38. @Test
  39. public class TLVTableImplTest {
  40. public void testTLVTableAddParams() {
  41. TLVTableImpl table = new TLVTableImpl();
  42. try {
  43. assertFalse(table.containsKey(Tag.DEST_ADDR_SUBUNIT));
  44. table.put(Tag.DEST_ADDR_SUBUNIT, new Integer(0x56));
  45. assertTrue(table.containsKey(Tag.DEST_ADDR_SUBUNIT));
  46. } catch (Exception x) {
  47. fail("Failed to set IntegerValue size 1");
  48. }
  49. try {
  50. assertFalse(table.containsKey(Tag.DEST_TELEMATICS_ID));
  51. table.put(Tag.DEST_TELEMATICS_ID, new Integer(0xe2e1));
  52. assertTrue(table.containsKey(Tag.DEST_TELEMATICS_ID));
  53. } catch (Exception x) {
  54. fail("Failed to set IntegerValue size 2");
  55. }
  56. try {
  57. assertFalse(table.containsKey(Tag.QOS_TIME_TO_LIVE));
  58. table.put(Tag.QOS_TIME_TO_LIVE, new Long(0xe4e3e2e1L));
  59. assertTrue(table.containsKey(Tag.QOS_TIME_TO_LIVE));
  60. } catch (Exception x) {
  61. fail("Failed to set IntegerValue size 4");
  62. }
  63. try {
  64. assertFalse(table.containsKey(Tag.ADDITIONAL_STATUS_INFO_TEXT));
  65. table.put(Tag.ADDITIONAL_STATUS_INFO_TEXT, "Test info");
  66. assertTrue(table.containsKey(Tag.ADDITIONAL_STATUS_INFO_TEXT));
  67. } catch (Exception x) {
  68. fail("Failed to set StringValue.");
  69. }
  70. try {
  71. assertFalse(table.containsKey(Tag.CALLBACK_NUM_ATAG));
  72. byte[] b = {0x67, 0x67, 0x67};
  73. table.put(Tag.CALLBACK_NUM_ATAG, b);
  74. assertTrue(table.containsKey(Tag.CALLBACK_NUM_ATAG));
  75. } catch (Exception x) {
  76. fail("Failed to set OctetValue.");
  77. }
  78. try {
  79. assertFalse(table.containsKey(Tag.MS_MSG_WAIT_FACILITIES));
  80. BitSet bitSet = new BitSet();
  81. table.put(Tag.MS_MSG_WAIT_FACILITIES, bitSet);
  82. assertTrue(table.containsKey(Tag.MS_MSG_WAIT_FACILITIES));
  83. Tag newTag = Tag.defineTag(0xdead, new BitmaskParamDescriptor(), 1);
  84. assertFalse(table.containsKey(newTag));
  85. table.put(newTag, bitSet);
  86. assertTrue(table.containsKey(newTag));
  87. } catch (Exception x) {
  88. fail("Failed to set Bitmask value");
  89. }
  90. }
  91. public void testTLVTableFailAddParams() {
  92. TLVTableImpl tab = new TLVTableImpl();
  93. try {
  94. // Try and set a string that's too long.
  95. String longString = new String(
  96. "111111111111111111111111111111111111111111111111111111111111111111111111111"
  97. + "222222222222222222222222222222222222222222222222222222222222222222222222222"
  98. + "333333333333333333333333333333333333333333333333333333333333333333333333333"
  99. + "444444444444444444444444444444444444444444444444444444444444444444444444444"
  100. + "555555555555555555555555555555555555555555555555555555555555555555555555555"
  101. + "666666666666666666666666666666666666666666666666666666666666666666666666666");
  102. tab.put(Tag.ADDITIONAL_STATUS_INFO_TEXT, longString);
  103. fail("Set a StringValue that was too long.");
  104. } catch (InvalidSizeForValueException x) {
  105. }
  106. try {
  107. // Try and set an OctetValue that's too short
  108. byte[] b = new byte[1];
  109. tab.put(Tag.SOURCE_SUBADDRESS, b);
  110. fail("Set an OctetValue that was too short.");
  111. } catch (InvalidSizeForValueException x) {
  112. }
  113. try {
  114. // Try and set an OctetValue that's too long
  115. byte[] b = new byte[70];
  116. tab.put(Tag.CALLBACK_NUM_ATAG, b);
  117. fail("Set an OctetValue that was too long.");
  118. } catch (InvalidSizeForValueException x) {
  119. }
  120. }
  121. public void testGetStringReturnsNullOnUnsetTag() throws Exception {
  122. TLVTableImpl table = new TLVTableImpl();
  123. assertNull(table.getString(Tag.RECEIPTED_MESSAGE_ID));
  124. }
  125. public void testGetBitmaskReturnsNullOnUnsetTag() throws Exception {
  126. TLVTableImpl table = new TLVTableImpl();
  127. assertNull(table.getBitmask(Tag.MS_MSG_WAIT_FACILITIES));
  128. }
  129. public void testGetBytesReturnsNullOnUnsetTag() throws Exception {
  130. TLVTableImpl table = new TLVTableImpl();
  131. assertNull(table.getBytes(Tag.DEST_SUBADDRESS));
  132. }
  133. public void testGetIntReturnsNegativeOneOnUnsetTag() throws Exception {
  134. TLVTableImpl table = new TLVTableImpl();
  135. assertEquals(table.getInt(Tag.DEST_TELEMATICS_ID), -1);
  136. }
  137. public void testGetLongReturnsNegativeOneOnUnsetTag() throws Exception {
  138. TLVTableImpl table = new TLVTableImpl();
  139. assertEquals(table.getLong(Tag.DEST_TELEMATICS_ID), -1L);
  140. }
  141. public void testGetIntAndGetLongSucceed() throws Exception {
  142. TLVTableImpl table = new TLVTableImpl();
  143. table.put(Tag.DEST_ADDR_SUBUNIT, new Integer(56));
  144. assertEquals(table.getInt(Tag.DEST_ADDR_SUBUNIT), 56);
  145. assertEquals(table.getLong(Tag.DEST_ADDR_SUBUNIT), 56L);
  146. }
  147. public void testGetIntThrowsExceptionOnIncorrectType() throws Exception {
  148. try {
  149. TLVTableImpl table = new TLVTableImpl();
  150. table.put(Tag.RECEIPTED_MESSAGE_ID, "messageID");
  151. table.getInt(Tag.RECEIPTED_MESSAGE_ID);
  152. fail("Expected a ClassCastException on call to getInt");
  153. } catch (ClassCastException x) {
  154. }
  155. }
  156. public void testGetLongThrowsExceptionOnIncorrectType() throws Exception {
  157. try {
  158. TLVTableImpl table = new TLVTableImpl();
  159. table.put(Tag.RECEIPTED_MESSAGE_ID, "messageID");
  160. table.getLong(Tag.RECEIPTED_MESSAGE_ID);
  161. fail("Expected a ClassCastException on call to getInt");
  162. } catch (ClassCastException x) {
  163. }
  164. }
  165. public void testGetStringSucceeds() throws Exception {
  166. TLVTableImpl table = new TLVTableImpl();
  167. table.put(Tag.RECEIPTED_MESSAGE_ID, "messageID");
  168. table.put(Tag.DEST_ADDR_SUBUNIT, new Integer(124));
  169. assertEquals(table.getString(Tag.RECEIPTED_MESSAGE_ID), "messageID");
  170. assertEquals(table.getString(Tag.DEST_ADDR_SUBUNIT), "124");
  171. }
  172. public void testGetBitmaskSucceeds() throws Exception {
  173. final BitSet bitSet = new BitSet();
  174. TLVTableImpl table = new TLVTableImpl();
  175. table.put(Tag.MS_MSG_WAIT_FACILITIES, bitSet);
  176. assertEquals(table.getBitmask(Tag.MS_MSG_WAIT_FACILITIES), bitSet);
  177. }
  178. public void testGetBitmaskThrowsExceptionOnIncorrectType() throws Exception {
  179. try {
  180. TLVTableImpl table = new TLVTableImpl();
  181. table.put(Tag.RECEIPTED_MESSAGE_ID, "messageID");
  182. table.getBitmask(Tag.RECEIPTED_MESSAGE_ID);
  183. fail("Expected a ClassCastException on call to getBitmask");
  184. } catch (ClassCastException x) {
  185. }
  186. }
  187. public void testGetBytesSucceeds() throws Exception {
  188. byte[] array = new byte[] {1, 2, 3, 4};
  189. TLVTableImpl table = new TLVTableImpl();
  190. table.put(Tag.DEST_SUBADDRESS, array);
  191. assertEquals(table.getBytes(Tag.DEST_SUBADDRESS), array);
  192. }
  193. public void testGetBytesThrowsExceptionOnIncorrectType() throws Exception {
  194. try {
  195. TLVTableImpl table = new TLVTableImpl();
  196. table.put(Tag.MS_MSG_WAIT_FACILITIES, new BitSet());
  197. table.getBytes(Tag.MS_MSG_WAIT_FACILITIES);
  198. fail("Expected a ClassCastException on call to getBytes");
  199. } catch (ClassCastException x) {
  200. }
  201. }
  202. public void testTLVTableSerialize() {
  203. // If testTLVTableAddParams fails, this will fail too...make sure it's
  204. // working first!
  205. // First, create a table with at least one parameter in it for
  206. // each type of encoder defined.
  207. TLVTableImpl origTable = new TLVTableImpl();
  208. byte[] b = {0x56, 0x67, 0x69};
  209. BitSet bitSet = new BitSet();
  210. bitSet.set(3);
  211. // 0x56 == 86 decimal
  212. origTable.put(Tag.DEST_ADDR_SUBUNIT, new Integer(0x56));
  213. // 0xe2e1 == 58081 decimal
  214. origTable.put(Tag.DEST_TELEMATICS_ID, new Integer(0xe2e1));
  215. origTable.put(Tag.QOS_TIME_TO_LIVE, new Long((long) Integer.MAX_VALUE));
  216. origTable.put(Tag.ADDITIONAL_STATUS_INFO_TEXT, "Test info");
  217. origTable.put(Tag.CALLBACK_NUM_ATAG, b);
  218. origTable.put(Tag.MS_MSG_WAIT_FACILITIES, bitSet);
  219. ByteArrayOutputStream out = new ByteArrayOutputStream();
  220. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  221. try {
  222. origTable.writeTo(encoder);
  223. } catch (IOException x) {
  224. fail("I/O Exception while writing to output stream.");
  225. }
  226. byte[] serialized = out.toByteArray();
  227. // The table must report the same length as it actually serializes to..
  228. if (origTable.getLength() != serialized.length) {
  229. fail("Table getLength is different to actual encoded length");
  230. }
  231. PacketDecoderImpl decoder = new PacketDecoderImpl(serialized);
  232. TLVTableImpl newTable = new TLVTableImpl();
  233. newTable.readFrom(decoder, serialized.length);
  234. doTableAssertions(origTable, newTable);
  235. assertEquals(decoder.getParsePosition(), serialized.length);
  236. decoder.setParsePosition(0);
  237. newTable = new TLVTableImpl();
  238. newTable.readFrom(decoder, serialized.length);
  239. doTableAssertions(origTable, newTable);
  240. assertEquals(decoder.getParsePosition(), serialized.length);
  241. }
  242. /**
  243. * This test creates a byte array representing a TLVTableImpl which contains a
  244. * tag that the API does not know about. The API should be able to decode
  245. * any optional parameter that is well-formed - the fact that it doesn't
  246. * know about it beforehand should not cause an error in the API.
  247. */
  248. // TODO dependencies
  249. public void testTLVTableDeSerializeUnknown() throws Exception {
  250. // Set up a byte array which contains 2 known optional parameters
  251. // followed by 2 unknowns.
  252. final Integer testIntValue = new Integer(0xbcad);
  253. final String testStringValue = "smppapi tlv tests";
  254. TLVTableImpl table = new TLVTableImpl();
  255. table.put(Tag.DEST_TELEMATICS_ID, testIntValue);
  256. table.put(Tag.ADDITIONAL_STATUS_INFO_TEXT, testStringValue);
  257. ByteArrayOutputStream out = new ByteArrayOutputStream();
  258. PacketEncoderImpl encoder = new PacketEncoderImpl(out);
  259. table.writeTo(encoder);
  260. // Tag '0xcafe', length 2.
  261. encoder.writeBytes(new byte[] {
  262. (byte) 0xca,
  263. (byte) 0xfe,
  264. (byte) 0x00,
  265. (byte) 0x02,
  266. (byte) 0xfe,
  267. (byte) 0xed,
  268. });
  269. // Tag '0xbeef', length 5
  270. encoder.writeBytes(new byte[] {
  271. (byte) 0xbe,
  272. (byte) 0xef,
  273. (byte) 0x00,
  274. (byte) 0x05,
  275. (byte) 0xba,
  276. (byte) 0xbe,
  277. (byte) 0xde,
  278. (byte) 0xad,
  279. (byte) 0x99,
  280. });
  281. byte[] b = out.toByteArray();
  282. try {
  283. // Run the test - attempt to deserialize the table.
  284. PacketDecoderImpl decoder = new PacketDecoderImpl(out.toByteArray());
  285. TLVTableImpl tab = new TLVTableImpl();
  286. tab.readFrom(decoder, b.length);
  287. assertEquals(decoder.getParsePosition(), b.length);
  288. assertEquals(tab.get(Tag.DEST_TELEMATICS_ID), testIntValue);
  289. assertEquals(tab.get(Tag.ADDITIONAL_STATUS_INFO_TEXT), testStringValue);
  290. b = (byte[]) tab.get(0xcafe);
  291. byte[] expectedValue = {(byte) 0xfe, (byte) 0xed};
  292. assertEquals(b, expectedValue);
  293. b = (byte[]) tab.get(0xbeef);
  294. expectedValue = new byte[] {(byte) 0xba, (byte) 0xbe, (byte) 0xde,
  295. (byte) 0xad, (byte) 0x99, };
  296. assertEquals(b, expectedValue);
  297. } catch (Exception x) {
  298. x.printStackTrace(System.err);
  299. fail("Deserialize failed. " + x.getMessage());
  300. }
  301. }
  302. private void doTableAssertions(TLVTableImpl origTable, TLVTableImpl newTable) {
  303. assertEquals(((Number) origTable.get(Tag.DEST_ADDR_SUBUNIT)).longValue(),
  304. ((Number) newTable.get(Tag.DEST_ADDR_SUBUNIT)).longValue());
  305. assertEquals(((Number) origTable.get(Tag.DEST_TELEMATICS_ID)).longValue(),
  306. ((Number) newTable.get(Tag.DEST_TELEMATICS_ID)).longValue());
  307. assertEquals(((Number) origTable.get(Tag.QOS_TIME_TO_LIVE)).longValue(),
  308. ((Number) newTable.get(Tag.QOS_TIME_TO_LIVE)).longValue());
  309. assertEquals(origTable.get(Tag.ADDITIONAL_STATUS_INFO_TEXT),
  310. newTable.get(Tag.ADDITIONAL_STATUS_INFO_TEXT));
  311. assertEquals((byte[]) newTable.get(Tag.CALLBACK_NUM_ATAG),
  312. (byte[]) origTable.get(Tag.CALLBACK_NUM_ATAG));
  313. assertEquals((BitSet) origTable.get(Tag.MS_MSG_WAIT_FACILITIES),
  314. (BitSet) newTable.get(Tag.MS_MSG_WAIT_FACILITIES));
  315. }
  316. }