Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle only positive sizes of OctetString. Negative sizes are not taken

into consideration now.
  • Loading branch information...
commit 1ecafb82a6d5436f91e67792a0fb030a1f779c49 1 parent 19af87a
@tysonite authored
View
25 runtime/src/OctetStringType.cc
@@ -2,6 +2,9 @@
#include "Utils.hh"
#include "OctetStringType.hh"
+// The size of value part to be reported in the exception message
+#define VALUE_PART_SIZE 10U
+
namespace asn1
{
@@ -10,22 +13,26 @@ void OctetStringType::checkType(const ValueType& value) const
{
for (SizesType::const_iterator p = _sizes.begin(); p != _sizes.end(); ++p)
{
- if (p->first == p->second && value.size() != p->first)
+ ValueType::size_type valueSize = value.size();
+ if (p->first == p->second && valueSize != p->first)
{
- throw ASN1Exception(toString() + " value '" + value + "' size is not equal to size '" +
- utils::ntos(p->first) + "'");
+ throw ASN1Exception(toString() + " value '" + value.substr(0,
+ std::min(VALUE_PART_SIZE, valueSize)) + "' size is not equal to size '"
+ + utils::ntos(p->first) + "'");
}
- if (value.size() < p->first)
+ if (p->first >= 0 && valueSize < p->first)
{
- throw ASN1Exception(toString() + " value '" + value + "' size is less than minimum size '" +
- utils::ntos(p->first) + "'");
+ throw ASN1Exception(toString() + " value '" + value.substr(0,
+ std::min(VALUE_PART_SIZE, valueSize)) + "' size is less than minimum size '"
+ + utils::ntos(p->first) + "'");
}
- if (value.size() > p->second)
+ if (p->second >= 0 && valueSize > p->second)
{
- throw ASN1Exception(toString() + " value '" + value + "' size is more than maximum size '" +
- utils::ntos(p->second) + "'");
+ throw ASN1Exception(toString() + " value '" + value.substr(0,
+ std::min(VALUE_PART_SIZE, valueSize)) + "' size is more than maximum size '"
+ + utils::ntos(p->second) + "'");
}
}
}
View
2  runtime/tests/CMakeLists.txt
@@ -2,7 +2,7 @@ project (asn1-runtime)
cmake_minimum_required (VERSION 2.6)
-set (SOURCES_NOT_COMPILABLE BerBuffer_tests.cc ChoiceType_tests.cc Utils_tests.cc AnyType_tests.cc)
+set (SOURCES_NOT_COMPILABLE BerBuffer_tests.cc ChoiceType_tests.cc OctetStringType_tests.cc Utils_tests.cc AnyType_tests.cc)
set (SOURCES TestBER.cc ${SOURCES_NOT_COMPILABLE})
set_source_files_properties (${SOURCES_NOT_COMPILABLE}
View
69 runtime/tests/OctetStringType_tests.cc
@@ -0,0 +1,69 @@
+namespace octetstringtype_tests
+{
+
+BOOST_AUTO_TEST_CASE(OctetStringType_CheckType_Correct)
+{
+ asn1::OctetStringType type;
+ asn1::OctetStringType::ValueType value("abcd");
+
+ type.addSize(0, 5);
+ BOOST_CHECK_NO_THROW(type.checkType(value));
+}
+
+BOOST_AUTO_TEST_CASE(OctetStringType_CheckType_MultipleSizes_Correct)
+{
+ asn1::OctetStringType type;
+ asn1::OctetStringType::ValueType value("abcd");
+
+ type.addSize(3, 10);
+ type.addSize(1, 5);
+ BOOST_CHECK_NO_THROW(type.checkType(value));
+}
+
+BOOST_AUTO_TEST_CASE(OctetStringType_CheckType_MultipleSizes_Incorrect)
+{
+ asn1::OctetStringType type;
+ asn1::OctetStringType::ValueType value("abcd");
+
+ type.addSize(0, 5);
+ type.addSize(2, 3);
+ BOOST_CHECK_THROW(type.checkType(value), asn1::ASN1Exception);
+}
+
+BOOST_AUTO_TEST_CASE(OctetStringType_CheckType_SizeEqual)
+{
+ asn1::OctetStringType type;
+ asn1::OctetStringType::ValueType value("abc");
+
+ type.addSize(3, 3);
+ BOOST_CHECK_NO_THROW(type.checkType(value));
+}
+
+BOOST_AUTO_TEST_CASE(OctetStringType_CheckType_SizeNotEqual)
+{
+ asn1::OctetStringType type;
+ asn1::OctetStringType::ValueType value("abcedfg");
+
+ type.addSize(3, 3);
+ BOOST_CHECK_THROW(type.checkType(value), asn1::ASN1Exception);
+}
+
+BOOST_AUTO_TEST_CASE(OctetStringType_CheckType_ValueIsNotInRange_Min)
+{
+ asn1::OctetStringType type;
+ asn1::OctetStringType::ValueType value("ab");
+
+ type.addSize(3, 5);
+ BOOST_CHECK_THROW(type.checkType(value), asn1::ASN1Exception);
+}
+
+BOOST_AUTO_TEST_CASE(OctetStringType_CheckType_ValueIsNotInRange_Max)
+{
+ asn1::OctetStringType type;
+ asn1::OctetStringType::ValueType value("abcdefg");
+
+ type.addSize(3, 5);
+ BOOST_CHECK_THROW(type.checkType(value), asn1::ASN1Exception);
+}
+
+}
View
1  runtime/tests/TestBER.cc
@@ -26,6 +26,7 @@ namespace ut = boost::unit_test;
#include "BerBuffer_tests.cc"
#include "ChoiceType_tests.cc"
#include "AnyType_tests.cc"
+#include "OctetStringType_tests.cc"
#include "Utils_tests.cc"
void TestBEREmptyIntegerWithValue(const asn1::Integer& vToWrite)
Please sign in to comment.
Something went wrong with that request. Please try again.