test: yaml-parser: Test out-of-range checks on integer parsing

Add 16-bit integer parsing tests, including a test to verify the
out-of-range checks when parsing 32-bit integers as 16-bit values. That
test currently fails.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
This commit is contained in:
Laurent Pinchart 2022-08-16 00:47:42 +03:00
parent 629e65b15b
commit 7615f58f9b

View file

@ -24,8 +24,10 @@ using namespace std;
static const string testYaml = static const string testYaml =
"string: libcamera\n" "string: libcamera\n"
"double: 3.14159\n" "double: 3.14159\n"
"uint32_t: 100\n" "int16_t: -1000\n"
"int32_t: -100\n" "uint16_t: 1000\n"
"int32_t: -100000\n"
"uint32_t: 100000\n"
"size: [1920, 1080]\n" "size: [1920, 1080]\n"
"list:\n" "list:\n"
" - James\n" " - James\n"
@ -74,6 +76,8 @@ protected:
enum class Type { enum class Type {
String, String,
Int16,
UInt16,
Int32, Int32,
UInt32, UInt32,
Double, Double,
@ -86,8 +90,10 @@ protected:
{ {
bool isList = type == Type::List || type == Type::Size; bool isList = type == Type::List || type == Type::Size;
bool isScalar = !isList && type != Type::Dictionary; bool isScalar = !isList && type != Type::Dictionary;
bool isInteger = type == Type::Int32 || type == Type::UInt32; bool isInteger16 = type == Type::Int16 || type == Type::UInt16;
bool isSigned = type == Type::Int32; bool isInteger32 = type == Type::Int32 || type == Type::UInt32;
bool isInteger = isInteger16 || isInteger32;
bool isSigned = type == Type::Int16 || type == Type::Int32;
if ((isScalar && !obj.isValue()) || (!isScalar && obj.isValue())) { if ((isScalar && !obj.isValue()) || (!isScalar && obj.isValue())) {
std::cerr std::cerr
@ -118,6 +124,20 @@ protected:
return TestFail; return TestFail;
} }
if (!isInteger16 && obj.get<int16_t>()) {
std::cerr
<< "Object " << name << " didn't fail to parse as "
<< "int16_t" << std::endl;
return TestFail;
}
if ((!isInteger16 || isSigned) && obj.get<uint16_t>()) {
std::cerr
<< "Object " << name << " didn't fail to parse as "
<< "uint16_t" << std::endl;
return TestFail;
}
if (!isInteger && obj.get<int32_t>()) { if (!isInteger && obj.get<int32_t>()) {
std::cerr std::cerr
<< "Object " << name << " didn't fail to parse as " << "Object " << name << " didn't fail to parse as "
@ -154,7 +174,8 @@ protected:
{ {
uint64_t unsignedValue = static_cast<uint64_t>(value); uint64_t unsignedValue = static_cast<uint64_t>(value);
std::string strValue = std::to_string(value); std::string strValue = std::to_string(value);
bool isSigned = type == Type::Int32; bool isInteger16 = type == Type::Int16 || type == Type::UInt16;
bool isSigned = type == Type::Int16 || type == Type::Int32;
/* All integers can be parsed as strings or double. */ /* All integers can be parsed as strings or double. */
@ -174,6 +195,26 @@ protected:
return TestFail; return TestFail;
} }
if (isInteger16) {
if (obj.get<int16_t>().value_or(0) != value ||
obj.get<int16_t>(0) != value) {
std::cerr
<< "Object " << name << " failed to parse as "
<< "int16_t" << std::endl;
return TestFail;
}
}
if (isInteger16 && !isSigned) {
if (obj.get<uint16_t>().value_or(0) != unsignedValue ||
obj.get<uint16_t>(0) != unsignedValue) {
std::cerr
<< "Object " << name << " failed to parse as "
<< "uint16_t" << std::endl;
return TestFail;
}
}
if (obj.get<int32_t>().value_or(0) != value || if (obj.get<int32_t>().value_or(0) != value ||
obj.get<int32_t>(0) != value) { obj.get<int32_t>(0) != value) {
std::cerr std::cerr
@ -231,8 +272,8 @@ protected:
} }
std::vector<const char *> rootElemNames = { std::vector<const char *> rootElemNames = {
"string", "double", "int32_t", "uint32_t", "size", "string", "double", "int16_t", "uint16_t", "int32_t",
"list", "dictionary", "level1", "uint32_t", "size", "list", "dictionary", "level1",
}; };
for (const char *name : rootElemNames) { for (const char *name : rootElemNames) {
@ -255,13 +296,31 @@ protected:
return TestFail; return TestFail;
} }
/* Test int16_t object */
auto &int16Obj = (*root)["int16_t"];
if (testObjectType(int16Obj, "int16_t", Type::Int16) != TestPass)
return TestFail;
if (testIntegerObject(int16Obj, "int16_t", Type::Int16, -1000) != TestPass)
return TestFail;
/* Test uint16_t object */
auto &uint16Obj = (*root)["uint16_t"];
if (testObjectType(uint16Obj, "uint16_t", Type::UInt16) != TestPass)
return TestFail;
if (testIntegerObject(uint16Obj, "uint16_t", Type::UInt16, 1000) != TestPass)
return TestFail;
/* Test int32_t object */ /* Test int32_t object */
auto &int32Obj = (*root)["int32_t"]; auto &int32Obj = (*root)["int32_t"];
if (testObjectType(int32Obj, "int32_t", Type::Int32) != TestPass) if (testObjectType(int32Obj, "int32_t", Type::Int32) != TestPass)
return TestFail; return TestFail;
if (testIntegerObject(int32Obj, "int32_t", Type::Int32, -100) != TestPass) if (testIntegerObject(int32Obj, "int32_t", Type::Int32, -100000) != TestPass)
return TestFail; return TestFail;
/* Test uint32_t object */ /* Test uint32_t object */
@ -270,7 +329,7 @@ protected:
if (testObjectType(uint32Obj, "uint32_t", Type::UInt32) != TestPass) if (testObjectType(uint32Obj, "uint32_t", Type::UInt32) != TestPass)
return TestFail; return TestFail;
if (testIntegerObject(uint32Obj, "uint32_t", Type::UInt32, 100) != TestPass) if (testIntegerObject(uint32Obj, "uint32_t", Type::UInt32, 100000) != TestPass)
return TestFail; return TestFail;
/* Test double value */ /* Test double value */