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