PageRenderTime 18ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/GSMCharsetTest.java

http://mobicents.googlecode.com/
Java | 130 lines | 72 code | 20 blank | 38 comment | 2 complexity | 7cec975d4c65fcfd6b75645c23072aac MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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;
  23. import java.nio.ByteBuffer;
  24. import java.nio.CharBuffer;
  25. import java.util.Arrays;
  26. import static org.testng.Assert.*;
  27. import org.testng.annotations.*;
  28. /**
  29. *
  30. * @author amit bhayani
  31. * @author sergey vetyutnev
  32. *
  33. */
  34. public class GSMCharsetTest {
  35. @Test(groups = { "functional.encode","gsm"})
  36. public void testEncode() throws Exception {
  37. this.doTestEncode("123456\r", new byte[] { 49, -39, -116, 86, -77, 53, 26 }, true);
  38. this.doTestEncode("1\r", new byte[] { -79, 6 }, true);
  39. this.doTestEncode("1", new byte[] { 49 }, true);
  40. // case: adding '\r' instead of '@' - USSD style
  41. this.doTestEncode("1234567", new byte[] { 49, -39, -116, 86, -77, -35, 26 }, true);
  42. // case: adding double '\r' when '\r' will be removed at the decoder side - USSD style
  43. this.doTestEncode("1234567\r", new byte[] { 49, -39, -116, 86, -77, -35, 26, 13 }, true);
  44. // case: adding '@' (not '\r') - SMS style
  45. this.doTestEncode("1234567", new byte[] { 49, -39, -116, 86, -77, -35, 0 }, false);
  46. // case: do not adding double '\r' when '\r' will be removed at the decoder side - SMS style
  47. this.doTestEncode("1234567\r", new byte[] { 49, -39, -116, 86, -77, -35, 26 }, false);
  48. // This raw data is from nad1053.pcap, 2nd packet.
  49. this.doTestEncode("*125*+31628839999#", new byte[] { (byte) 0xaa, (byte) 0x98, (byte) 0xac, (byte) 0xa6, 0x5a, (byte) 0xcd, 0x62, 0x36, 0x19, 0x0e,
  50. 0x37, (byte) 0xcb, (byte) 0xe5, 0x72, (byte) 0xb9, 0x11 }, true);
  51. // This raw data is from nad1053.pcap, last packet.
  52. this.doTestEncode("You don t have enough credit to call this number. ", new byte[] { (byte) 0xd9, (byte) 0x77, 0x1d, 0x44, (byte) 0x7e, (byte) 0xbb,
  53. 0x41, 0x74, 0x10, 0x3a, 0x6c, 0x2f, (byte) 0x83, (byte) 0xca, (byte) 0xee, 0x77, (byte) 0xfd, (byte) 0x8c, 0x06, (byte) 0x8d, (byte) 0xe5,
  54. 0x65, 0x72, (byte) 0x9a, 0x0e, (byte) 0xa2, (byte) 0xbf, 0x41, (byte) 0xe3, 0x30, (byte) 0x9b, 0x0d, (byte) 0xa2, (byte) 0xa3, (byte) 0xd3,
  55. 0x73, (byte) 0x90, (byte) 0xbb, (byte) 0xde, 0x16, (byte) 0x97, (byte) 0xe5, 0x2e, 0x10 }, true);
  56. // The USSD String represented by above raw data
  57. this.doTestEncode("ndmgapp2ndmgapp2", new byte[] { 0x6e, 0x72, (byte) 0xfb, 0x1c, (byte) 0x86, (byte) 0xc3, 0x65, 0x6e, 0x72, (byte) 0xfb, 0x1c,
  58. (byte) 0x86, (byte) 0xc3, 0x65 }, true);
  59. // this.doTestEncode("*88#", new byte[] { 0x2a, 0x1c, 0x6e, (byte)0xd4 }, true);
  60. this.doTestEncode("*88#", new byte[] { 0x2a, 0x1c, 0x6e, (byte)0x4 }, true);
  61. }
  62. @Test(groups = { "functional.decode","gsm"})
  63. public void testDecode() throws Exception {
  64. this.doTestDecode("123456\r", new byte[] { 49, -39, -116, 86, -77, 53, 26 }, true, 0, 0);
  65. this.doTestDecode("1\r", new byte[] { -79, 6 }, true, 0, 0);
  66. this.doTestDecode("1", new byte[] { 49 }, true, 0, 0);
  67. this.doTestDecode("1234567", new byte[] { 49, -39, -116, 86, -77, -35, 26 }, true, 0, 0);
  68. this.doTestDecode("1234567\r\r", new byte[] { 49, -39, -116, 86, -77, -35, 26, 13 }, true, 0, 0);
  69. this.doTestDecode("1234567", new byte[] { 49, -39, -116, 86, -77, -35, 0 }, false, 7, 0);
  70. this.doTestDecode("1234567\r", new byte[] { 49, -39, -116, 86, -77, -35, 26 }, false, 8, 0);
  71. // This raw data is from nad1053.pcap, 2nd packet.
  72. this.doTestDecode("*125*+31628839999#", new byte[] { (byte) 0xaa, (byte) 0x98, (byte) 0xac, (byte) 0xa6, 0x5a, (byte) 0xcd, 0x62, 0x36, 0x19, 0x0e,
  73. 0x37, (byte) 0xcb, (byte) 0xe5, 0x72, (byte) 0xb9, 0x11 }, true, 0, 0);
  74. // This raw data is from nad1053.pcap, last packet.
  75. this.doTestDecode("You don t have enough credit to call this number. ", new byte[] { (byte) 0xd9, (byte) 0x77, 0x1d, 0x44, (byte) 0x7e, (byte) 0xbb,
  76. 0x41, 0x74, 0x10, 0x3a, 0x6c, 0x2f, (byte) 0x83, (byte) 0xca, (byte) 0xee, 0x77, (byte) 0xfd, (byte) 0x8c, 0x06, (byte) 0x8d, (byte) 0xe5,
  77. 0x65, 0x72, (byte) 0x9a, 0x0e, (byte) 0xa2, (byte) 0xbf, 0x41, (byte) 0xe3, 0x30, (byte) 0x9b, 0x0d, (byte) 0xa2, (byte) 0xa3, (byte) 0xd3,
  78. 0x73, (byte) 0x90, (byte) 0xbb, (byte) 0xde, 0x16, (byte) 0x97, (byte) 0xe5, 0x2e, 0x10 }, true, 0, 0);
  79. // The USSD String represented by above raw data
  80. this.doTestDecode("ndmgapp2ndmgapp2", new byte[] { 0x6e, 0x72, (byte) 0xfb, 0x1c, (byte) 0x86, (byte) 0xc3, 0x65, 0x6e, 0x72, (byte) 0xfb, 0x1c,
  81. (byte) 0x86, (byte) 0xc3, 0x65 }, true, 0, 0);
  82. this.doTestDecode("*88#", new byte[] { 0x2a, 0x1c, 0x6e, (byte)0xd4 }, true, 0, 0);
  83. }
  84. private void doTestEncode(String decodedString, byte[] encodedData, boolean ussdStyleEncoding) throws Exception {
  85. GSMCharset cs = new GSMCharset("GSM", new String[] {});
  86. GSMCharsetEncoder encoder = (GSMCharsetEncoder) cs.newEncoder();
  87. if (ussdStyleEncoding)
  88. encoder.setGSMCharsetEncodingData(new GSMCharsetEncodingData());
  89. else
  90. encoder.setGSMCharsetEncodingData(new GSMCharsetEncodingData(null));
  91. ByteBuffer bb = encoder.encode(CharBuffer.wrap(decodedString));
  92. byte[] data = new byte[bb.limit()];
  93. bb.get(data);
  94. assertTrue(Arrays.equals(encodedData, data));
  95. }
  96. private void doTestDecode(String decodedString, byte[] encodedData, boolean ussdStyleEncoding, int totalSeptetCount, int leadingSeptetSkipCount)
  97. throws Exception {
  98. ByteBuffer bb = ByteBuffer.wrap(encodedData);
  99. GSMCharset cs = new GSMCharset("GSM", new String[] {});
  100. GSMCharsetDecoder decoder = (GSMCharsetDecoder) cs.newDecoder();
  101. if (ussdStyleEncoding)
  102. decoder.setGSMCharsetDecodingData(new GSMCharsetDecodingData());
  103. else
  104. decoder.setGSMCharsetDecodingData(new GSMCharsetDecodingData(totalSeptetCount, leadingSeptetSkipCount));
  105. CharBuffer bf = null;
  106. bf = decoder.decode(bb);
  107. String s1 = bf.toString();
  108. assertTrue(s1.equals(decodedString));
  109. }
  110. }