Permalink
Browse files

Added unit tests

  • Loading branch information...
1 parent b744daf commit dedaa6d21db8bc9fe308f18b33c9f6311f800c9f @jjlauer jjlauer committed Jul 27, 2011
View
@@ -3,6 +3,17 @@ http://twitter.com/
http://www.cloudhopper.com/
SMPP Library
--------------------------------------------------------------------------------
+4.1.0 - 2011-07-26
+ * Upgraded ch-commons-util dependency from 5.0 to 5.1.0
+ * Upgraded ch-commons-gsm dependency from 2.0 to 2.1.0
+ * Upgrade parent POM dependency from 1.0 to 1.5
+ * Switched to new version format.
+ * Added Version class to compile build info into jar
+ * Added SmppInvalidArgumentException class
+ * Fixed issue with short messages > than 255 bytes in length not triggering
+ an exception to be thrown when attempting to setShortMessage(). The length
+ of the byte array is now checked.
+
4.0 - 2011-05-09
* Upgraded ch-commons-util dependency from 4.1 to 5.0
* WARN: There are some minor source and binary backwards compatability issues
View
18 pom.xml
@@ -7,13 +7,13 @@
<groupId>cloudhopper</groupId>
<artifactId>ch-smpp</artifactId>
<packaging>jar</packaging>
- <version>4.0</version>
+ <version>4.1.0</version>
<name>ch-smpp</name>
<parent>
<groupId>cloudhopper</groupId>
<artifactId>ch-maven-parent-public</artifactId>
- <version>1.1</version>
+ <version>1.5</version>
</parent>
<repositories>
@@ -25,15 +25,6 @@
</repository>
</repositories>
- <build>
- <plugins>
- <plugin>
- <groupId>com.mycila.maven-license-plugin</groupId>
- <artifactId>maven-license-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
<!--
I prefer jars managed out of the Cloudhopper Maven Repo with more consistent
versioning. I apologize for not using more common Maven repos
@@ -96,10 +87,11 @@
</dependencies>
<properties>
+ <main.java.package>com.cloudhopper.smpp</main.java.package>
<netty.version>[3.2,)</netty.version>
<ch-commons-charset.version>[2.0,)</ch-commons-charset.version>
- <ch-commons-gsm.version>[2.0,)</ch-commons-gsm.version>
- <ch-commons-util.version>5.0-SNAPSHOT</ch-commons-util.version>
+ <ch-commons-gsm.version>[2.1,)</ch-commons-gsm.version>
+ <ch-commons-util.version>[5.1,)</ch-commons-util.version>
</properties>
</project>
@@ -1,19 +1,19 @@
/** DO NOT EDIT THIS FILE. IT IS AUTO GENERATED BY MAVEN */
package com.cloudhopper.smpp;
public final class Version {
- private static final String TIMESTAMP="20110726-2121";
+ private static final String TIMESTAMP="20110726-2124";
private static final String VERSION="4.1.0";
private static final String NAME="ch-smpp";
private static final String VENDOR="cloudhopper";
- private static final String LONG_VERSION="4.1.0 (Build @ 20110726-2121)";
+ private static final String LONG_VERSION="4.1.0 (Build @ 20110726-2124)";
/** Returns the library vendor such as "cloudhopper" */
static public String getVendor() { return VENDOR; }
- /** Returns the library build timestamp such as "20110726-2121" */
+ /** Returns the library build timestamp such as "20110726-2124" */
static public String getTimestamp() { return TIMESTAMP; }
/** Returns the library name such as "ch-smpp" */
static public String getName() { return NAME; }
/** Returns the library version such as "4.1.0" */
static public String getVersion() { return VERSION; }
- /** Returns a longer library version that includes the timestamp such as "4.1.0 (Build @ 20110726-2121)" */
+ /** Returns a longer library version that includes the timestamp such as "4.1.0 (Build @ 20110726-2124)" */
static public String getLongVersion() { return LONG_VERSION; }
}
@@ -19,6 +19,7 @@
import com.cloudhopper.smpp.type.UnrecoverablePduException;
import com.cloudhopper.smpp.type.RecoverablePduException;
import com.cloudhopper.commons.util.StringUtil;
+import com.cloudhopper.smpp.type.SmppInvalidArgumentException;
import com.cloudhopper.smpp.util.ChannelBufferUtil;
import com.cloudhopper.smpp.util.PduUtil;
import org.jboss.netty.buffer.ChannelBuffer;
@@ -55,7 +56,10 @@ public int getShortMessageLength() {
return this.shortMessage;
}
- public void setShortMessage(byte[] value) {
+ public void setShortMessage(byte[] value) throws SmppInvalidArgumentException {
+ if (value != null && value.length > 255) {
+ throw new SmppInvalidArgumentException("A short message in a PDU can only be a max of 255 bytes [actual=" + value.length + "]; use optional parameter message_payload as an alternative");
+ }
this.shortMessage = value;
}
@@ -1172,5 +1172,76 @@ public void decodeLargeSequenceNumber() throws Exception {
Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
Assert.assertEquals(0, pdu0.getShortMessageLength());
}
+
+ @Test
+ public void decodeWAUMalformedPacket() throws Exception {
+ ChannelBuffer buffer = BufferHelper.createBuffer("000000400000000500000000A2859F22313030310001013434393531333631393230000001343034303430343034303430343034300000000000000000080000");
+
+ DeliverSm pdu0 = (DeliverSm)transcoder.decode(buffer);
+ /**
+ Assert.assertEquals(64, pdu0.getCommandLength());
+ Assert.assertEquals(SmppConstants.CMD_ID_DELIVER_SM, pdu0.getCommandId());
+ Assert.assertEquals(0, pdu0.getCommandStatus());
+ Assert.assertEquals(-1568301278, pdu0.getSequenceNumber());
+ Assert.assertEquals(true, pdu0.isRequest());
+ Assert.assertEquals("1001", pdu0.getServiceType());
+ Assert.assertEquals(0x01, pdu0.getSourceAddress().getTon());
+ Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
+ Assert.assertEquals("44951361920", pdu0.getSourceAddress().getAddress());
+ Assert.assertEquals(0x00, pdu0.getDestAddress().getTon());
+ Assert.assertEquals(0x01, pdu0.getDestAddress().getNpi());
+ Assert.assertEquals("4040404040404040", pdu0.getDestAddress().getAddress());
+ Assert.assertEquals(0x00, pdu0.getEsmClass());
+ Assert.assertEquals(0x00, pdu0.getProtocolId());
+ Assert.assertEquals(0x00, pdu0.getPriority());
+ Assert.assertEquals("", pdu0.getScheduleDeliveryTime());
+ Assert.assertEquals("", pdu0.getValidityPeriod());
+ Assert.assertEquals(0x00, pdu0.getRegisteredDelivery());
+ Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
+ Assert.assertEquals(0x08, pdu0.getDataCoding());
+ Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
+ Assert.assertEquals(0, pdu0.getShortMessageLength());
+ */
+ }
+
+
+ @Test
+ public void decodeSubmitSmWith255ByteShortMessage() throws Exception {
+ String text255 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in orci magna. Etiam auctor ultrices lacus vel suscipit. Maecenas eget faucibus purus. Etiam aliquet mollis fermentum. Proin vel augue arcu. Praesent venenatis tristique ante turpis duis.";
+ byte[] text255Bytes = text255.getBytes("ISO-8859-1");
+
+ ChannelBuffer buffer = BufferHelper.createBuffer("00000130000000040000000000004FE80001013430343034000101343439353133363139323000000000000001000000FF" + HexUtil.toHexString(text255Bytes));
+
+ SubmitSm pdu0 = (SubmitSm)transcoder.decode(buffer);
+
+ Assert.assertEquals(304, pdu0.getCommandLength());
+ Assert.assertEquals(SmppConstants.CMD_ID_SUBMIT_SM, pdu0.getCommandId());
+ Assert.assertEquals(0, pdu0.getCommandStatus());
+ Assert.assertEquals(20456, pdu0.getSequenceNumber());
+ Assert.assertEquals(true, pdu0.isRequest());
+ Assert.assertEquals("", pdu0.getServiceType());
+ Assert.assertEquals(0x01, pdu0.getSourceAddress().getTon());
+ Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
+ Assert.assertEquals("40404", pdu0.getSourceAddress().getAddress());
+ Assert.assertEquals(0x01, pdu0.getDestAddress().getTon());
+ Assert.assertEquals(0x01, pdu0.getDestAddress().getNpi());
+ Assert.assertEquals("44951361920", pdu0.getDestAddress().getAddress());
+ Assert.assertEquals(0x00, pdu0.getEsmClass());
+ Assert.assertEquals(0x00, pdu0.getProtocolId());
+ Assert.assertEquals(0x00, pdu0.getPriority());
+ Assert.assertEquals("", pdu0.getScheduleDeliveryTime());
+ Assert.assertEquals("", pdu0.getValidityPeriod());
+ Assert.assertEquals(0x01, pdu0.getRegisteredDelivery());
+ Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
+ Assert.assertEquals(0x00, pdu0.getDataCoding());
+ Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
+ Assert.assertEquals(255, pdu0.getShortMessage().length);
+ Assert.assertArrayEquals(text255Bytes, pdu0.getShortMessage());
+
+ Assert.assertEquals(null, pdu0.getOptionalParameters());
+
+ // interesting -- this example has optional parameters it happened to skip...
+ Assert.assertEquals(0, buffer.readableBytes());
+ }
}
@@ -15,6 +15,7 @@
package com.cloudhopper.smpp.transcoder;
// third party imports
+import com.cloudhopper.commons.util.HexString;
import com.cloudhopper.smpp.type.Address;
import com.cloudhopper.commons.util.HexUtil;
import com.cloudhopper.smpp.SmppConstants;
@@ -37,6 +38,8 @@
import com.cloudhopper.smpp.pdu.Unbind;
import com.cloudhopper.smpp.pdu.UnbindResp;
import com.cloudhopper.smpp.tlv.Tlv;
+import com.cloudhopper.smpp.type.SmppInvalidArgumentException;
+import java.io.UnsupportedEncodingException;
import org.junit.*;
import org.jboss.netty.buffer.ChannelBuffer;
import org.slf4j.Logger;
@@ -367,4 +370,61 @@ public void encodeGenericNack() throws Exception {
Assert.assertArrayEquals(HexUtil.toByteArray("00000010800000000000000100082a77"), BufferHelper.createByteArray(buffer));
}
+ @Test
+ public void encodeSubmitSmWithShortMessageAsMax255Bytes() throws Exception {
+ SubmitSm pdu0 = new SubmitSm();
+
+ String text255 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in orci magna. Etiam auctor ultrices lacus vel suscipit. Maecenas eget faucibus purus. Etiam aliquet mollis fermentum. Proin vel augue arcu. Praesent venenatis tristique ante turpis duis.";
+
+ pdu0.setSequenceNumber(20456);
+ pdu0.setSourceAddress(new Address((byte)0x01, (byte)0x01, "40404"));
+ pdu0.setDestAddress(new Address((byte)0x01, (byte)0x01, "44951361920"));
+ pdu0.setEsmClass((byte)0x00);
+ pdu0.setProtocolId((byte)0x00);
+ pdu0.setPriority((byte)0x00);
+ pdu0.setScheduleDeliveryTime(null);
+ pdu0.setValidityPeriod(null);
+ pdu0.setRegisteredDelivery((byte)0x01);
+ pdu0.setReplaceIfPresent((byte)0x00);
+ pdu0.setDataCoding((byte)0x00);
+ pdu0.setDefaultMsgId((byte)0x00);
+ pdu0.setShortMessage(text255.getBytes("ISO-8859-1"));
+
+ ChannelBuffer buffer = transcoder.encode(pdu0);
+
+ String expectedHex = "00000130000000040000000000004FE80001013430343034000101343439353133363139323000000000000001000000FF" + HexUtil.toHexString(text255.getBytes("ISO-8859-1")).toUpperCase();
+ String actualHex = HexUtil.toHexString(BufferHelper.createByteArray(buffer)).toUpperCase();
+
+ Assert.assertEquals(expectedHex, actualHex);
+ }
+
+ @Test
+ public void encodeSubmitSmWithShortMessageUsing256BytesThrowsInvalidArgumentException() throws Exception {
+ SubmitSm pdu0 = new SubmitSm();
+
+ String text256 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in orci magna. Etiam auctor ultrices lacus vel suscipit. Maecenas eget faucibus purus. Etiam aliquet mollis fermentum. Proin vel augue arcu. Praesent venenatis tristique ante turpis duis.A";
+
+ pdu0.setSequenceNumber(20456);
+ pdu0.setSourceAddress(new Address((byte)0x01, (byte)0x01, "40404"));
+ pdu0.setDestAddress(new Address((byte)0x01, (byte)0x01, "44951361920"));
+ pdu0.setEsmClass((byte)0x00);
+ pdu0.setProtocolId((byte)0x00);
+ pdu0.setPriority((byte)0x00);
+ pdu0.setScheduleDeliveryTime(null);
+ pdu0.setValidityPeriod(null);
+ pdu0.setRegisteredDelivery((byte)0x01);
+ pdu0.setReplaceIfPresent((byte)0x00);
+ pdu0.setDataCoding((byte)0x00);
+ pdu0.setDefaultMsgId((byte)0x00);
+ // 256 bytes is *NOT* permitted for a short message since only a single
+ // byte can represent the length of the message -- in version <= 4.0
+ // this would be permitted -- causing an overflow for the byte, but still
+ // writing out all 256 bytes in the buffer
+ try {
+ pdu0.setShortMessage(text256.getBytes("ISO-8859-1"));
+ Assert.fail();
+ } catch (SmppInvalidArgumentException e) {
+ // expected behavior
+ }
+ }
}

0 comments on commit dedaa6d

Please sign in to comment.