diff --git a/jsmpp/src/main/java/org/jsmpp/util/StringValidator.java b/jsmpp/src/main/java/org/jsmpp/util/StringValidator.java index 705cba1..e6a44ba 100644 --- a/jsmpp/src/main/java/org/jsmpp/util/StringValidator.java +++ b/jsmpp/src/main/java/org/jsmpp/util/StringValidator.java @@ -34,21 +34,21 @@ public static void validateString(String value, StringParameter param) if (param.isRangeMinAndMax()) { if (!isCOctetStringValid(value, param.getMax())) { throw new PDUStringException("C-Octet String value '" - + value + "' cannot more than " + param.getMax() - + ". Actual length of string is " + value.length(), + + value + "' length must be less than " + param.getMax() + + ". Actual length is " + value.length(), param); } } else if (!isCOctetStringNullOrNValValid(value, param.getMax())) { throw new PDUStringException( - "C-Octet String value should be 1 or " + param.getMax() - + ". The actual length of string is " + "C-Octet String value '" + value + "' length should be 1 or " + (param.getMax() - 1) + + ". Actual length is " + value.length(), param); } } else if (param.getType() == StringType.OCTET_STRING && !isOctetStringValid(value, param.getMax())) { throw new PDUStringException("Octet String value '" + value - + "' cannot more than " + param.getMax() - + ". Actual length of string is " + value.length(), param); + + "' length must be less than or equal to " + param.getMax() + + ". Actual length is " + value.length(), param); } } @@ -58,21 +58,21 @@ public static void validateString(byte[] value, StringParameter param) if (param.isRangeMinAndMax()) { if (!isCOctetStringValid(value, param.getMax())) { throw new PDUStringException("C-Octet String value '" - + new String(value) + "' cannot more than " - + param.getMax() + ". Actual length of string is " + + new String(value) + "' length must be less than " + + param.getMax() + ". Actual length is " + value.length, param); } } else if (!isCOctetStringNullOrNValValid(value, param.getMax())) { throw new PDUStringException( - "C-Octet String value should be 1 or " + param.getMax() - + ". The actual length of string is " + "C-Octet String value '" + new String(value) + "' length should be 1 or " + (param.getMax() - 1) + + ". Actual length is " + value.length, param); } } else if (param.getType() == StringType.OCTET_STRING && !isOctetStringValid(value, param.getMax())) { throw new PDUStringException("Octet String value '" - + new String(value) + "' cannot more than " - + param.getMax() + ". Actual length of string is " + + new String(value) + "' length must be less than or equal to " + + param.getMax() + ". Actual length is " + value.length, param); } } @@ -131,9 +131,10 @@ static boolean isCOctetStringNullOrNValValid(byte[] value, return true; if (value.length == 0) return true; - if (value.length != length) - return false; - return true; + if (value.length == length - 1) { + return true; + } + return false; } /** diff --git a/jsmpp/src/test/java/org/jsmpp/bean/StringValidationTest.java b/jsmpp/src/test/java/org/jsmpp/bean/StringValidationTest.java deleted file mode 100644 index ecfc9d3..0000000 --- a/jsmpp/src/test/java/org/jsmpp/bean/StringValidationTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.jsmpp.bean; - - -import org.jsmpp.PDUStringException; -import org.jsmpp.util.StringParameter; -import org.jsmpp.util.StringValidator; -import static org.testng.Assert.*; -import org.testng.annotations.Test; - -/** - * Test case of string validation. - * @author uudashr - * @version 1.0 - * - */ -public class StringValidationTest { - - @Test(groups="checkintest") - public void testValidation() { - try { - StringValidator.validateString("", StringParameter.SYSTEM_ID); - } catch (PDUStringException e) { - fail("Should be okay inserting empty string"); - } - - try { - StringValidator.validateString((String)null, StringParameter.SYSTEM_ID); - } catch (PDUStringException e) { - fail("Should be okay inserting null string"); - } - - try { - StringValidator.validateString("smsgw", StringParameter.SYSTEM_ID); - } catch (PDUStringException e) { - fail("Should be okay inserting string that has length less than 16"); - } - - try { - StringValidator.validateString("smsgwsmsgwsmsgw", StringParameter.SYSTEM_ID); - } catch (PDUStringException e) { - fail("Should be okay inserting 15 char of string"); - } - - try { - StringValidator.validateString("smsgwsmsgwsmsgwe", StringParameter.SYSTEM_ID); - fail("Should fail inserting 16 char of string"); - } catch (PDUStringException e) { - } - - try { - StringValidator.validateString("smsgwsmsgwsmsgwee", StringParameter.SYSTEM_ID); - fail("Should be fail inserting 17 char of string"); - } catch (PDUStringException e) { - } - } -} diff --git a/jsmpp/src/test/java/org/jsmpp/util/StringValidatorTest.java b/jsmpp/src/test/java/org/jsmpp/util/StringValidatorTest.java new file mode 100644 index 0000000..f55bccd --- /dev/null +++ b/jsmpp/src/test/java/org/jsmpp/util/StringValidatorTest.java @@ -0,0 +1,172 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.jsmpp.util; + + +import org.jsmpp.PDUStringException; +import org.jsmpp.util.StringParameter; +import org.jsmpp.util.StringValidator; +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * Test case of string validation. + * @author uudashr + * @version 1.0 + * + */ +public class StringValidatorTest { + + @Test(groups="checkintest") + public void testValidation() { + try { + StringValidator.validateString("", StringParameter.SYSTEM_ID); + } catch (PDUStringException e) { + fail("Should be okay inserting empty string"); + } + + try { + StringValidator.validateString((String)null, StringParameter.SYSTEM_ID); + } catch (PDUStringException e) { + fail("Should be okay inserting null string"); + } + + try { + StringValidator.validateString("smsgw", StringParameter.SYSTEM_ID); + } catch (PDUStringException e) { + fail("Should be okay inserting string that has length less than 16"); + } + + try { + StringValidator.validateString("smsgwsmsgwsmsgw", StringParameter.SYSTEM_ID); + } catch (PDUStringException e) { + fail("Should be okay inserting 15 char of string"); + } + + try { + StringValidator.validateString("smsgwsmsgwsmsgwe", StringParameter.SYSTEM_ID); + fail("Should fail inserting 16 char of string"); + } catch (PDUStringException e) { + } + + try { + StringValidator.validateString("smsgwsmsgwsmsgwee", StringParameter.SYSTEM_ID); + fail("Should be fail inserting 17 char of string"); + } catch (PDUStringException e) { + } + } + + @Test(groups="checkintest") + public void validateStringOctedStringWithString() throws Exception { + StringValidator.validateString("", StringParameter.SHORT_MESSAGE); + StringValidator.validateString("short messages", StringParameter.SHORT_MESSAGE); + String shortMessage = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567" + + "89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901" + + "234567890123456789012345678901234567890123456789012345678901234"; + StringValidator.validateString(shortMessage, StringParameter.SHORT_MESSAGE); + + try { + shortMessage = shortMessage + "5"; + StringValidator.validateString(shortMessage, StringParameter.SHORT_MESSAGE); + fail("PDUStringException expected"); + } catch (PDUStringException e) { + // expected + assertEquals(e.getMessage(), "Octet String value '" + shortMessage + "' length must be less than or equal " + + "to 254. Actual length is 255"); + } + } + + @Test(groups="checkintest") + public void validateStringOctedStringWithByteArray() throws Exception { + StringValidator.validateString("".getBytes("UTF-8"), StringParameter.SHORT_MESSAGE); + StringValidator.validateString("short messages".getBytes("UTF-8"), StringParameter.SHORT_MESSAGE); + String shortMessage = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567" + + "89012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901" + + "234567890123456789012345678901234567890123456789012345678901234"; + StringValidator.validateString(shortMessage.getBytes("UTF-8"), StringParameter.SHORT_MESSAGE); + + try { + shortMessage = shortMessage + "5"; + StringValidator.validateString(shortMessage.getBytes("UTF-8"), StringParameter.SHORT_MESSAGE); + fail("PDUStringException expected"); + } catch (PDUStringException e) { + // expected + assertEquals(e.getMessage(), "Octet String value '" + shortMessage + "' length must be less than or equal " + + "to 254. Actual length is 255"); + } + } + + @Test(groups="checkintest") + public void validateStringCOctedStringWithString() throws Exception { + StringValidator.validateString("", StringParameter.SYSTEM_ID); + StringValidator.validateString("System ID", StringParameter.SYSTEM_ID); + StringValidator.validateString("123456789012345", StringParameter.SYSTEM_ID); + + try { + StringValidator.validateString("1234567890123456", StringParameter.SYSTEM_ID); + fail("PDUStringException expected"); + } catch (PDUStringException e) { + // expected + assertEquals(e.getMessage(), "C-Octet String value '1234567890123456' length must be less than 16. " + + "Actual length is 16"); + } + } + + @Test(groups="checkintest") + public void validateStringCOctedStringWithByteArray() throws Exception { + StringValidator.validateString("".getBytes("UTF-8"), StringParameter.SYSTEM_ID); + StringValidator.validateString("System ID".getBytes("UTF-8"), StringParameter.SYSTEM_ID); + StringValidator.validateString("123456789012345".getBytes("UTF-8"), StringParameter.SYSTEM_ID); + + try { + StringValidator.validateString("1234567890123456".getBytes("UTF-8"), StringParameter.SYSTEM_ID); + fail("PDUStringException expected"); + } catch (PDUStringException e) { + // expected + assertEquals(e.getMessage(), "C-Octet String value '1234567890123456' length must be less than 16. " + + "Actual length is 16"); + } + } + + @Test(groups="checkintest") + public void validateStringCOctedStringWithStringAndWithoutARange() throws Exception { + StringValidator.validateString("", StringParameter.SCHEDULE_DELIVERY_TIME); + StringValidator.validateString("020610233429000R", StringParameter.SCHEDULE_DELIVERY_TIME); + + try { + StringValidator.validateString("020610233429000RX", StringParameter.SCHEDULE_DELIVERY_TIME); + fail("PDUStringException expected"); + } catch (PDUStringException e) { + // expected + assertEquals(e.getMessage(), "C-Octet String value '020610233429000RX' length should be 1 or 16. Actual " + + "length is 17"); + } + } + + @Test(groups="checkintest") + public void validateStringCOctedStringWithByteArrayAndWithoutARange() throws Exception { + StringValidator.validateString("".getBytes("UTF-8"), StringParameter.SCHEDULE_DELIVERY_TIME); + StringValidator.validateString("020610233429000R".getBytes("UTF-8"), StringParameter.SCHEDULE_DELIVERY_TIME); + + try { + StringValidator.validateString("020610233429000RX".getBytes("UTF-8"), StringParameter.SCHEDULE_DELIVERY_TIME); + fail("PDUStringException expected"); + } catch (PDUStringException e) { + // expected + assertEquals(e.getMessage(), "C-Octet String value '020610233429000RX' length should be 1 or 16. Actual " + + "length is 17"); + } + } +}