PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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