/protocols/asn/asn-impl/src/test/java/org/mobicents/protocols/asn/ExternalTest.java

http://mobicents.googlecode.com/ · Java · 146 lines · 100 code · 38 blank · 8 comment · 8 complexity · c70dc54d7363fcd3672ead27b2a42963 MD5 · raw file

  1. package org.mobicents.protocols.asn;
  2. import java.util.Arrays;
  3. import junit.framework.TestCase;
  4. import org.junit.After;
  5. import org.junit.AfterClass;
  6. import org.junit.Before;
  7. import org.junit.BeforeClass;
  8. import org.junit.Test;
  9. /**
  10. *
  11. * @author amit bhayani
  12. *
  13. */
  14. public class ExternalTest extends TestCase {
  15. @BeforeClass
  16. public static void setUpClass() throws Exception {
  17. }
  18. @AfterClass
  19. public static void tearDownClass() throws Exception {
  20. }
  21. @Before
  22. public void setUp() throws Exception {
  23. }
  24. @After
  25. public void tearDown() {
  26. }
  27. @Test
  28. public void testDecode() throws Exception {
  29. // This raw data is from wireshark trace of TCAP - MAP
  30. byte[] data = new byte[] { 0x28, 0x23, 0x06, 0x07, 0x04, 0x00,
  31. 0x00, 0x01, 0x01, 0x01, 0x01, (byte) 0xa0, 0x18, (byte) 0xa0,
  32. (byte) 0x80, (byte) 0x80, 0x09, (byte) 0x96, 0x02, 0x24,
  33. (byte) 0x80, 0x03, 0x00, (byte) 0x80, 0x00, (byte) 0xf2,
  34. (byte) 0x81, 0x07, (byte) 0x91, 0x13, 0x26, (byte) 0x98,
  35. (byte) 0x86, 0x03, (byte) 0xf0, 0x00, 0x00 };
  36. AsnInputStream asin = new AsnInputStream(data);
  37. int tag = asin.readTag();
  38. assertTrue(External._TAG_IMPLICIT_SEQUENCE == tag);
  39. External external = new External();
  40. external.decode(asin);
  41. assertTrue(external.isOid());
  42. assertTrue(Arrays.equals(new long[] { 0, 4, 0, 0, 1, 1, 1, 1 },
  43. external.getOidValue()));
  44. assertFalse(external.isInteger());
  45. assertTrue(external.isAsn());
  46. assertTrue(Arrays.equals(new byte[] { (byte) 0xa0, (byte) 0x80,
  47. (byte) 0x80, 0x09, (byte) 0x96, 0x02, 0x24, (byte) 0x80, 0x03,
  48. 0x00, (byte) 0x80, 0x00, (byte) 0xf2, (byte) 0x81, 0x07,
  49. (byte) 0x91, 0x13, 0x26, (byte) 0x98, (byte) 0x86, 0x03,
  50. (byte) 0xf0, 0x00, 0x00 }, external.getEncodeType()));
  51. }
  52. @Test
  53. public void testEncode() throws Exception {
  54. // This raw data is from wireshark trace of TCAP - MAP
  55. byte[] data = new byte[] { 0x28, 0x23, 0x06, 0x07, 0x04, 0x00,
  56. 0x00, 0x01, 0x01, 0x01, 0x01, (byte) 0xa0, 0x18, (byte) 0xa0,
  57. (byte) 0x80, (byte) 0x80, 0x09, (byte) 0x96, 0x02, 0x24,
  58. (byte) 0x80, 0x03, 0x00, (byte) 0x80, 0x00, (byte) 0xf2,
  59. (byte) 0x81, 0x07, (byte) 0x91, 0x13, 0x26, (byte) 0x98,
  60. (byte) 0x86, 0x03, (byte) 0xf0, 0x00, 0x00 };
  61. External external = new External();
  62. external.setOid(true);
  63. external.setOidValue(new long[] { 0, 4, 0, 0, 1, 1, 1, 1 });
  64. external.setAsn(true);
  65. external.setEncodeType(new byte[] { (byte) 0xa0, (byte) 0x80,
  66. (byte) 0x80, 0x09, (byte) 0x96, 0x02, 0x24, (byte) 0x80, 0x03,
  67. 0x00, (byte) 0x80, 0x00, (byte) 0xf2, (byte) 0x81, 0x07,
  68. (byte) 0x91, 0x13, 0x26, (byte) 0x98, (byte) 0x86, 0x03,
  69. (byte) 0xf0, 0x00, 0x00 });
  70. AsnOutputStream asnOs = new AsnOutputStream();
  71. external.encode(asnOs);
  72. byte[] encodedData = asnOs.toByteArray();
  73. System.out.println(dump(encodedData, encodedData.length, false));
  74. assertTrue(Arrays.equals(data, encodedData));
  75. }
  76. public final static String dump(byte[] buff, int size, boolean asBits) {
  77. String s = "";
  78. for (int i = 0; i < size; i++) {
  79. String ss = null;
  80. if(!asBits)
  81. {
  82. ss = Integer.toHexString(buff[i] & 0xff);
  83. }
  84. else
  85. {
  86. ss = Integer.toBinaryString(buff[i] & 0xff);
  87. }
  88. ss = fillInZeroPrefix(ss,asBits);
  89. s += " " + ss;
  90. }
  91. return s;
  92. }
  93. public final static String fillInZeroPrefix(String ss, boolean asBits) {
  94. if (asBits) {
  95. if (ss.length() < 8) {
  96. for (int j = ss.length(); j < 8; j++) {
  97. ss = "0" + ss;
  98. }
  99. }
  100. } else {
  101. // hex
  102. if (ss.length() < 2) {
  103. ss = "0" + ss;
  104. }
  105. }
  106. return ss;
  107. }
  108. }