1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
35  package org.marre.sms.transport.gsm;
36  
37  import junit.framework.TestCase;
38  
39  import org.marre.sms.SmsAddress;
40  import org.marre.sms.SmsException;
41  import org.marre.sms.SmsMessage;
42  import org.marre.sms.SmsPdu;
43  import org.marre.sms.SmsTextMessage;
44  import org.marre.sms.SmsUdhElement;
45  import org.marre.sms.SmsUdhUtil;
46  import org.marre.util.StringUtil;
47  
48  /***
49   * 
50   * @author Markus Eriksson
51   * @version $Id: GsmEncoderTest.java,v 1.4 2005/11/26 18:43:39 c95men Exp $
52   */
53  
54  public class GsmEncoderTest extends TestCase
55  {    
56      class UDH5Msg extends SmsTextMessage 
57      {
58          public UDH5Msg(String txt) 
59          {
60              super(txt);
61          }
62          
63          public SmsUdhElement[] getUdhElements() 
64          {
65              return new SmsUdhElement[]{SmsUdhUtil.get8BitApplicationPortUdh(10,11)};
66          }
67      }
68      
69      public void testUdh5() throws SmsException
70      {
71          SmsMessage udhMsg;
72          SmsPdu[] smsPdus;
73          byte[] data;
74  
75          // 5 bytes UDH, 2 septets
76          udhMsg = new UDH5Msg("01");
77          smsPdus = udhMsg.getPdus();
78          data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
79          assertEquals("4100039121F30000080404020A0BC062",
80                  StringUtil.bytesToHexString(data));
81          
82          // 5 bytes UDH, 3 septets
83          udhMsg = new UDH5Msg("012");
84          smsPdus = udhMsg.getPdus();
85          data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
86          assertEquals("4100039121F30000090404020A0BC06232",
87                  StringUtil.bytesToHexString(data));
88          
89          // 5 bytes UDH, 4 septets
90          udhMsg = new UDH5Msg("0123");
91          smsPdus = udhMsg.getPdus();
92          data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
93          assertEquals("4100039121F300000A0404020A0BC062B219",
94                  StringUtil.bytesToHexString(data));
95          
96          // 5 bytes UDH, 5 septets
97          udhMsg = new UDH5Msg("01234");
98          smsPdus = udhMsg.getPdus();
99          data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
100         assertEquals("4100039121F300000B0404020A0BC062B2190D",
101                 StringUtil.bytesToHexString(data));
102         
103         // 5 bytes UDH, 6 septets
104         udhMsg = new UDH5Msg("012345");
105         smsPdus = udhMsg.getPdus();
106         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
107         assertEquals("4100039121F300000C0404020A0BC062B219AD06",
108                 StringUtil.bytesToHexString(data));
109         
110         // 5 bytes UDH, 7 septets
111         udhMsg = new UDH5Msg("0123456");
112         smsPdus = udhMsg.getPdus();
113         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
114         assertEquals("4100039121F300000D0404020A0BC062B219AD6603",
115                 StringUtil.bytesToHexString(data));
116         
117         // 5 bytes UDH, 8 septets
118         udhMsg = new UDH5Msg("01234567");
119         smsPdus = udhMsg.getPdus();
120         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
121         assertEquals("4100039121F300000E0404020A0BC062B219AD66BB01",
122                 StringUtil.bytesToHexString(data));
123         
124         // 5 bytes UDH, 9 septets
125         udhMsg = new UDH5Msg("012345678");
126         smsPdus = udhMsg.getPdus();
127         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
128         assertEquals("4100039121F300000F0404020A0BC062B219AD66BBE100",
129                 StringUtil.bytesToHexString(data));
130     }
131     
132     class UDH6Msg extends SmsTextMessage 
133     {
134         public UDH6Msg(String txt) 
135         {
136             super(txt);
137         }
138         
139         public SmsUdhElement[] getUdhElements() 
140         {
141             return new SmsUdhElement[]{SmsUdhUtil.get8BitConcatUdh(3, 2, 1)};
142         }
143     }
144     
145     public void testUdh6() throws SmsException
146     {
147         SmsMessage udhMsg;
148         SmsPdu[] smsPdus;
149         byte[] data;
150 
151         // 6 bytes UDH, 2 septets
152         udhMsg = new UDH6Msg("01");
153         smsPdus = udhMsg.getPdus();
154         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
155         assertEquals("4100039121F30000090500030302016031",
156                 StringUtil.bytesToHexString(data));
157         
158         // 6 bytes UDH, 3 septets
159         udhMsg = new UDH6Msg("012");
160         smsPdus = udhMsg.getPdus();
161         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
162         assertEquals("4100039121F300000A050003030201603119",
163                 StringUtil.bytesToHexString(data));
164         
165         // 6 bytes UDH, 4 septets
166         udhMsg = new UDH6Msg("0123");
167         smsPdus = udhMsg.getPdus();
168         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
169         assertEquals("4100039121F300000B0500030302016031D90C",
170                 StringUtil.bytesToHexString(data));
171         
172         // 6 bytes UDH, 5 septets
173         udhMsg = new UDH6Msg("01234");
174         smsPdus = udhMsg.getPdus();
175         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
176         assertEquals("4100039121F300000C0500030302016031D98C06",
177                 StringUtil.bytesToHexString(data));
178         
179         // 6 bytes UDH, 6 septets
180         udhMsg = new UDH6Msg("012345");
181         smsPdus = udhMsg.getPdus();
182         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
183         assertEquals("4100039121F300000D0500030302016031D98C5603",
184                 StringUtil.bytesToHexString(data));
185         
186         // 6 bytes UDH, 7 septets
187         udhMsg = new UDH6Msg("0123456");
188         smsPdus = udhMsg.getPdus();
189         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
190         assertEquals("4100039121F300000E0500030302016031D98C56B301",
191                 StringUtil.bytesToHexString(data));
192         
193         // 6 bytes UDH, 8 septets
194         udhMsg = new UDH6Msg("01234567");
195         smsPdus = udhMsg.getPdus();
196         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
197         assertEquals("4100039121F300000F0500030302016031D98C56B3DD00",
198                 StringUtil.bytesToHexString(data));
199         
200         // 6 bytes UDH, 9 septets
201         udhMsg = new UDH6Msg("012345678");
202         smsPdus = udhMsg.getPdus();
203         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
204         assertEquals("4100039121F30000100500030302016031D98C56B3DD70",
205                 StringUtil.bytesToHexString(data));
206     }
207     
208     class UDH7Msg extends SmsTextMessage 
209     {
210         public UDH7Msg(String txt) 
211         {
212             super(txt);
213         }
214         
215         public SmsUdhElement[] getUdhElements() 
216         {
217             return new SmsUdhElement[]{SmsUdhUtil.get16BitApplicationPortUdh(10,11)};
218         }
219     }
220     
221     public void testUdh7() throws SmsException
222     {
223         SmsMessage udhMsg;
224         SmsPdu[] smsPdus;
225         byte[] data;
226 
227         // 7 bytes UDH, 5 septets
228         udhMsg = new UDH7Msg("01234");
229         smsPdus = udhMsg.getPdus();
230         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
231         assertEquals("4100039121F300000D060504000A000BB0986C4603",
232                 StringUtil.bytesToHexString(data));
233         
234         // 7 bytes UDH, 6 septets
235         udhMsg = new UDH7Msg("012345");
236         smsPdus = udhMsg.getPdus();
237         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
238         assertEquals("4100039121F300000E060504000A000BB0986C46AB01",
239                 StringUtil.bytesToHexString(data));
240         
241         // 7 bytes UDH, 7 septets
242         udhMsg = new UDH7Msg("0123456");
243         smsPdus = udhMsg.getPdus();
244         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
245         assertEquals("4100039121F300000F060504000A000BB0986C46ABD900",
246                 StringUtil.bytesToHexString(data));
247         
248         // 5 bytes UDH, 8 septets
249         udhMsg = new UDH7Msg("01234567");
250         smsPdus = udhMsg.getPdus();
251         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
252         assertEquals("4100039121F3000010060504000A000BB0986C46ABD96E",
253                 StringUtil.bytesToHexString(data));
254         
255         // 5 bytes UDH, 9 septets
256         udhMsg = new UDH7Msg("012345678");
257         smsPdus = udhMsg.getPdus();
258         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
259         assertEquals("4100039121F3000011060504000A000BB0986C46ABD96E38",
260                 StringUtil.bytesToHexString(data));
261 
262         // 5 bytes UDH, 10 septets
263         udhMsg = new UDH7Msg("0123456789");
264         smsPdus = udhMsg.getPdus();
265         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
266         assertEquals("4100039121F3000012060504000A000BB0986C46ABD96EB81C",
267                 StringUtil.bytesToHexString(data));        
268     }
269     
270     public void testSeptetEncoder() throws SmsException
271     {
272         SmsTextMessage textMsg;
273         SmsPdu[] smsPdus;
274         byte[] data;
275      
276         // 160 chars should fit within one SMS
277         textMsg = new SmsTextMessage("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789");
278         smsPdus = textMsg.getPdus();
279         assertEquals(1, smsPdus.length);
280         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
281         assertEquals("0100039121F30000A0B0986C46ABD96EB81C2C269BD16AB61B2E078BC966B49AED86CBC162B219AD66BBE172B0986C46ABD96EB81C2C269BD16AB61B2E078BC966B49AED86CBC162B219AD66BBE172B0986C46ABD96EB81C2C269BD16AB61B2E078BC966B49AED86CBC162B219AD66BBE172B0986C46ABD96EB81C2C269BD16AB61B2E078BC966B49AED86CBC162B219AD66BBE172",
282                      StringUtil.bytesToHexString(data));
283         
284         // 0 chars
285         textMsg = new SmsTextMessage("");
286         smsPdus = textMsg.getPdus();
287         assertEquals(1, smsPdus.length);
288         data = GsmEncoder.encodePdu(smsPdus[0], new SmsAddress("123"), new SmsAddress("456"));
289         assertEquals("0100039121F3000000",
290                      StringUtil.bytesToHexString(data));        
291     }    
292 }