PageRenderTime 52ms CodeModel.GetById 2ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/message/tlv/TLVTableImplTest.java

http://mobicents.googlecode.com/
Java | 357 lines | 273 code | 41 blank | 43 comment | 2 complexity | c8f12c3dafaf920797ddd25a99e482c3 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.smpp.message.tlv;
 24
 25import static org.testng.Assert.assertEquals;
 26import static org.testng.Assert.assertFalse;
 27import static org.testng.Assert.assertNull;
 28import static org.testng.Assert.assertTrue;
 29import static org.testng.Assert.fail;
 30
 31import java.io.ByteArrayOutputStream;
 32import java.io.IOException;
 33import java.util.BitSet;
 34
 35import org.testng.annotations.Test;
 36
 37import org.mobicents.protocols.smpp.message.param.BitmaskParamDescriptor;
 38import org.mobicents.protocols.smpp.message.tlv.InvalidSizeForValueException;
 39import org.mobicents.protocols.smpp.message.tlv.TLVTableImpl;
 40import org.mobicents.protocols.smpp.message.tlv.Tag;
 41import org.mobicents.protocols.smpp.util.PacketDecoderImpl;
 42import org.mobicents.protocols.smpp.util.PacketEncoderImpl;
 43
 44@Test
 45public class TLVTableImplTest {
 46    public void testTLVTableAddParams() {
 47        TLVTableImpl table = new TLVTableImpl();
 48
 49        try {
 50            assertFalse(table.containsKey(Tag.DEST_ADDR_SUBUNIT));
 51            table.put(Tag.DEST_ADDR_SUBUNIT, new Integer(0x56));
 52            assertTrue(table.containsKey(Tag.DEST_ADDR_SUBUNIT));
 53        } catch (Exception x) {
 54            fail("Failed to set IntegerValue size 1");
 55        }
 56
 57        try {
 58            assertFalse(table.containsKey(Tag.DEST_TELEMATICS_ID));
 59            table.put(Tag.DEST_TELEMATICS_ID, new Integer(0xe2e1));
 60            assertTrue(table.containsKey(Tag.DEST_TELEMATICS_ID));
 61        } catch (Exception x) {
 62            fail("Failed to set IntegerValue size 2");
 63        }
 64
 65        try {
 66            assertFalse(table.containsKey(Tag.QOS_TIME_TO_LIVE));
 67            table.put(Tag.QOS_TIME_TO_LIVE, new Long(0xe4e3e2e1L));
 68            assertTrue(table.containsKey(Tag.QOS_TIME_TO_LIVE));
 69        } catch (Exception x) {
 70            fail("Failed to set IntegerValue size 4");
 71        }
 72
 73        try {
 74            assertFalse(table.containsKey(Tag.ADDITIONAL_STATUS_INFO_TEXT));
 75            table.put(Tag.ADDITIONAL_STATUS_INFO_TEXT, "Test info");
 76            assertTrue(table.containsKey(Tag.ADDITIONAL_STATUS_INFO_TEXT));
 77        } catch (Exception x) {
 78            fail("Failed to set StringValue.");
 79        }
 80
 81        try {
 82            assertFalse(table.containsKey(Tag.CALLBACK_NUM_ATAG));
 83            byte[] b = {0x67, 0x67, 0x67};
 84            table.put(Tag.CALLBACK_NUM_ATAG, b);
 85            assertTrue(table.containsKey(Tag.CALLBACK_NUM_ATAG));
 86        } catch (Exception x) {
 87            fail("Failed to set OctetValue.");
 88        }
 89        try {
 90            assertFalse(table.containsKey(Tag.MS_MSG_WAIT_FACILITIES));
 91            BitSet bitSet = new BitSet();
 92            table.put(Tag.MS_MSG_WAIT_FACILITIES, bitSet);
 93            assertTrue(table.containsKey(Tag.MS_MSG_WAIT_FACILITIES));
 94            Tag newTag = Tag.defineTag(0xdead, new BitmaskParamDescriptor(), 1);
 95            assertFalse(table.containsKey(newTag));
 96            table.put(newTag, bitSet);
 97            assertTrue(table.containsKey(newTag));
 98        } catch (Exception x) {
 99            fail("Failed to set Bitmask value");
100        }
101    }
102    
103    public void testTLVTableFailAddParams() {
104        TLVTableImpl tab = new TLVTableImpl();
105
106        try {
107            // Try and set a string that's too long.
108            String longString = new String(
109                    "111111111111111111111111111111111111111111111111111111111111111111111111111"
110                    + "222222222222222222222222222222222222222222222222222222222222222222222222222"
111                    + "333333333333333333333333333333333333333333333333333333333333333333333333333"
112                    + "444444444444444444444444444444444444444444444444444444444444444444444444444"
113                    + "555555555555555555555555555555555555555555555555555555555555555555555555555"
114                    + "666666666666666666666666666666666666666666666666666666666666666666666666666");
115            tab.put(Tag.ADDITIONAL_STATUS_INFO_TEXT, longString);
116            fail("Set a StringValue that was too long.");
117        } catch (InvalidSizeForValueException x) {
118        }
119
120        try {
121            // Try and set an OctetValue that's too short
122            byte[] b = new byte[1];
123            tab.put(Tag.SOURCE_SUBADDRESS, b);
124            fail("Set an OctetValue that was too short.");
125        } catch (InvalidSizeForValueException x) {
126        }
127
128        try {
129            // Try and set an OctetValue that's too long
130            byte[] b = new byte[70];
131            tab.put(Tag.CALLBACK_NUM_ATAG, b);
132            fail("Set an OctetValue that was too long.");
133        } catch (InvalidSizeForValueException x) {
134        }
135    }
136
137    public void testGetStringReturnsNullOnUnsetTag() throws Exception {
138        TLVTableImpl table = new TLVTableImpl();
139        assertNull(table.getString(Tag.RECEIPTED_MESSAGE_ID));
140    }
141
142    public void testGetBitmaskReturnsNullOnUnsetTag() throws Exception {
143        TLVTableImpl table = new TLVTableImpl();
144        assertNull(table.getBitmask(Tag.MS_MSG_WAIT_FACILITIES));
145    }
146
147    public void testGetBytesReturnsNullOnUnsetTag() throws Exception {
148        TLVTableImpl table = new TLVTableImpl();
149        assertNull(table.getBytes(Tag.DEST_SUBADDRESS));
150    }
151
152    public void testGetIntReturnsNegativeOneOnUnsetTag() throws Exception {
153        TLVTableImpl table = new TLVTableImpl();
154        assertEquals(table.getInt(Tag.DEST_TELEMATICS_ID), -1);
155    }
156    
157    public void testGetLongReturnsNegativeOneOnUnsetTag() throws Exception {
158        TLVTableImpl table = new TLVTableImpl();
159        assertEquals(table.getLong(Tag.DEST_TELEMATICS_ID), -1L);
160    }
161    
162    public void testGetIntAndGetLongSucceed() throws Exception {
163        TLVTableImpl table = new TLVTableImpl();
164        table.put(Tag.DEST_ADDR_SUBUNIT, new Integer(56));
165        assertEquals(table.getInt(Tag.DEST_ADDR_SUBUNIT), 56);
166        assertEquals(table.getLong(Tag.DEST_ADDR_SUBUNIT), 56L);
167    }
168
169    public void testGetIntThrowsExceptionOnIncorrectType() throws Exception {
170        try {
171            TLVTableImpl table = new TLVTableImpl();
172            table.put(Tag.RECEIPTED_MESSAGE_ID, "messageID");
173            table.getInt(Tag.RECEIPTED_MESSAGE_ID);
174            fail("Expected a ClassCastException on call to getInt");
175        } catch (ClassCastException x) {
176        }
177    }
178
179    public void testGetLongThrowsExceptionOnIncorrectType() throws Exception {
180        try {
181            TLVTableImpl table = new TLVTableImpl();
182            table.put(Tag.RECEIPTED_MESSAGE_ID, "messageID");
183            table.getLong(Tag.RECEIPTED_MESSAGE_ID);
184            fail("Expected a ClassCastException on call to getInt");
185        } catch (ClassCastException x) {
186        }
187    }
188
189    public void testGetStringSucceeds() throws Exception {
190        TLVTableImpl table = new TLVTableImpl();
191        table.put(Tag.RECEIPTED_MESSAGE_ID, "messageID");
192        table.put(Tag.DEST_ADDR_SUBUNIT, new Integer(124));
193        assertEquals(table.getString(Tag.RECEIPTED_MESSAGE_ID), "messageID");
194        assertEquals(table.getString(Tag.DEST_ADDR_SUBUNIT), "124");
195    }
196
197    public void testGetBitmaskSucceeds() throws Exception {
198        final BitSet bitSet = new BitSet();
199        TLVTableImpl table = new TLVTableImpl();
200        table.put(Tag.MS_MSG_WAIT_FACILITIES, bitSet);
201        assertEquals(table.getBitmask(Tag.MS_MSG_WAIT_FACILITIES), bitSet);
202    }
203
204    public void testGetBitmaskThrowsExceptionOnIncorrectType() throws Exception {
205        try {
206            TLVTableImpl table = new TLVTableImpl();
207            table.put(Tag.RECEIPTED_MESSAGE_ID, "messageID");
208            table.getBitmask(Tag.RECEIPTED_MESSAGE_ID);
209            fail("Expected a ClassCastException on call to getBitmask");
210        } catch (ClassCastException x) {
211        }
212    }
213
214    public void testGetBytesSucceeds() throws Exception {
215        byte[] array = new byte[] {1, 2, 3, 4};
216        TLVTableImpl table = new TLVTableImpl();
217        table.put(Tag.DEST_SUBADDRESS, array);
218        assertEquals(table.getBytes(Tag.DEST_SUBADDRESS), array);
219    }
220
221    public void testGetBytesThrowsExceptionOnIncorrectType() throws Exception {
222        try {
223            TLVTableImpl table = new TLVTableImpl();
224            table.put(Tag.MS_MSG_WAIT_FACILITIES, new BitSet());
225            table.getBytes(Tag.MS_MSG_WAIT_FACILITIES);
226            fail("Expected a ClassCastException on call to getBytes");
227        } catch (ClassCastException x) {
228        }
229    }
230    
231    public void testTLVTableSerialize() {
232        // If testTLVTableAddParams fails, this will fail too...make sure it's
233        // working first!
234        // First, create a table with at least one parameter in it for
235        // each type of encoder defined.
236        TLVTableImpl origTable = new TLVTableImpl();
237        byte[] b = {0x56, 0x67, 0x69};
238        BitSet bitSet = new BitSet();
239        bitSet.set(3);
240        // 0x56 == 86 decimal
241        origTable.put(Tag.DEST_ADDR_SUBUNIT, new Integer(0x56));
242        // 0xe2e1 == 58081 decimal
243        origTable.put(Tag.DEST_TELEMATICS_ID, new Integer(0xe2e1));
244        origTable.put(Tag.QOS_TIME_TO_LIVE, new Long((long) Integer.MAX_VALUE));
245        origTable.put(Tag.ADDITIONAL_STATUS_INFO_TEXT, "Test info");
246        origTable.put(Tag.CALLBACK_NUM_ATAG, b);
247        origTable.put(Tag.MS_MSG_WAIT_FACILITIES, bitSet);
248
249        ByteArrayOutputStream out = new ByteArrayOutputStream();
250        PacketEncoderImpl encoder = new PacketEncoderImpl(out);
251        try {
252            origTable.writeTo(encoder);
253        } catch (IOException x) {
254            fail("I/O Exception while writing to output stream.");
255        }
256        byte[] serialized = out.toByteArray();
257
258        // The table must report the same length as it actually serializes to..
259        if (origTable.getLength() != serialized.length) {
260            fail("Table getLength is different to actual encoded length");
261        }
262
263        PacketDecoderImpl decoder = new PacketDecoderImpl(serialized);
264        TLVTableImpl newTable = new TLVTableImpl();
265        newTable.readFrom(decoder, serialized.length);
266        doTableAssertions(origTable, newTable);
267        assertEquals(decoder.getParsePosition(), serialized.length);
268
269        decoder.setParsePosition(0);
270        newTable = new TLVTableImpl();
271        newTable.readFrom(decoder, serialized.length);
272        doTableAssertions(origTable, newTable);
273        assertEquals(decoder.getParsePosition(), serialized.length);
274    }
275
276    /**
277     * This test creates a byte array representing a TLVTableImpl which contains a
278     * tag that the API does not know about. The API should be able to decode
279     * any optional parameter that is well-formed - the fact that it doesn't
280     * know about it beforehand should not cause an error in the API.
281     */
282    // TODO dependencies
283    public void testTLVTableDeSerializeUnknown() throws Exception {
284        // Set up a byte array which contains 2 known optional parameters
285        // followed by 2 unknowns.
286        final Integer testIntValue = new Integer(0xbcad);
287        final String testStringValue = "smppapi tlv tests";
288        TLVTableImpl table = new TLVTableImpl();
289        table.put(Tag.DEST_TELEMATICS_ID, testIntValue);
290        table.put(Tag.ADDITIONAL_STATUS_INFO_TEXT, testStringValue);
291        ByteArrayOutputStream out = new ByteArrayOutputStream();
292        PacketEncoderImpl encoder = new PacketEncoderImpl(out);
293        table.writeTo(encoder);
294        // Tag '0xcafe', length 2.
295        encoder.writeBytes(new byte[] {
296                (byte) 0xca,
297                (byte) 0xfe,
298                (byte) 0x00,
299                (byte) 0x02,
300                (byte) 0xfe,
301                (byte) 0xed,
302        });
303        // Tag '0xbeef', length 5
304        encoder.writeBytes(new byte[] {
305                (byte) 0xbe,
306                (byte) 0xef,
307                (byte) 0x00,
308                (byte) 0x05,
309                (byte) 0xba,
310                (byte) 0xbe,
311                (byte) 0xde,
312                (byte) 0xad,
313                (byte) 0x99,
314        });
315
316        byte[] b = out.toByteArray();
317        try {
318            // Run the test - attempt to deserialize the table.
319            PacketDecoderImpl decoder = new PacketDecoderImpl(out.toByteArray());
320            TLVTableImpl tab = new TLVTableImpl();
321            tab.readFrom(decoder, b.length);
322            assertEquals(decoder.getParsePosition(), b.length);
323            assertEquals(tab.get(Tag.DEST_TELEMATICS_ID), testIntValue);
324            assertEquals(tab.get(Tag.ADDITIONAL_STATUS_INFO_TEXT), testStringValue);
325
326            b = (byte[]) tab.get(0xcafe);
327            byte[] expectedValue = {(byte) 0xfe, (byte) 0xed};
328
329            assertEquals(b, expectedValue);
330
331            b = (byte[]) tab.get(0xbeef);
332            expectedValue = new byte[] {(byte) 0xba, (byte) 0xbe, (byte) 0xde,
333                    (byte) 0xad, (byte) 0x99, };
334
335            assertEquals(b, expectedValue);
336
337        } catch (Exception x) {
338            x.printStackTrace(System.err);
339            fail("Deserialize failed. " + x.getMessage());
340        }
341    }
342    
343    private void doTableAssertions(TLVTableImpl origTable, TLVTableImpl newTable) {
344        assertEquals(((Number) origTable.get(Tag.DEST_ADDR_SUBUNIT)).longValue(),
345                ((Number) newTable.get(Tag.DEST_ADDR_SUBUNIT)).longValue());
346        assertEquals(((Number) origTable.get(Tag.DEST_TELEMATICS_ID)).longValue(),
347                ((Number) newTable.get(Tag.DEST_TELEMATICS_ID)).longValue());
348        assertEquals(((Number) origTable.get(Tag.QOS_TIME_TO_LIVE)).longValue(),
349                ((Number) newTable.get(Tag.QOS_TIME_TO_LIVE)).longValue());
350        assertEquals(origTable.get(Tag.ADDITIONAL_STATUS_INFO_TEXT),
351                newTable.get(Tag.ADDITIONAL_STATUS_INFO_TEXT));
352        assertEquals((byte[]) newTable.get(Tag.CALLBACK_NUM_ATAG),
353                (byte[]) origTable.get(Tag.CALLBACK_NUM_ATAG));
354        assertEquals((BitSet) origTable.get(Tag.MS_MSG_WAIT_FACILITIES),
355                (BitSet) newTable.get(Tag.MS_MSG_WAIT_FACILITIES));
356    }
357}