From patchwork Tue Aug 16 01:54:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17130 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 9893CC3272 for ; Tue, 16 Aug 2022 01:54:36 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4FED161FCC; Tue, 16 Aug 2022 03:54:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1660614876; bh=2yct2hbDhZ7Y5+s+AVYn0T62Z6ztIKO9p9BNEyV/fAA=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=3cjkh3uH2zoUJZBdjYcvVS1MYXLZi8gr8uWay7rgwQbxSTO0Hzqw5vf/F/iDfjrXM iz7OqnVVqVLizaIrWIQfzZZKyWdeVBrXM5rnasv1V2ROF1+xrpJ220EcgGrEgAFXbH My3x34C1hF+r6n3dXlLOaJkT7tpxpuQ7JsA+w2+arVNu1mZppvB+PfJKSV3pgilHSI PlxbIDiutnRQ5UFU9IYfsfC91JP0CSEuSavp+7FU1SwG2wNZkOZ+Bidb/tAgks68Gj 6EEntPS9GliMjQ+Z9PW245gmP232QIy0ZMuVpaKoesr7Q7VqA2dqx17an6N3dDK/Gl sL8ye3xyojltA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 912B161FC7 for ; Tue, 16 Aug 2022 03:54:32 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="dqYJtJDr"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2E7D0496; Tue, 16 Aug 2022 03:54:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1660614872; bh=2yct2hbDhZ7Y5+s+AVYn0T62Z6ztIKO9p9BNEyV/fAA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dqYJtJDrFeTI+YXaKGBp48hEDwwSQ1VgfWDreroBdzPALLNoeNIbUaQgbudujY5ni JXVJuRpjLR+YYDTK6ep0NVn4Wj7MSXjcdlno4mjNs5er3sfrrXiyukbWdnTwOOIBdI cpaCJqvd9M3adDSKOJQEGTDoQlD6EtSS0Hqm/2AE= To: libcamera-devel@lists.libcamera.org Date: Tue, 16 Aug 2022 04:54:08 +0300 Message-Id: <20220816015414.7462-4-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220816015414.7462-1-laurent.pinchart@ideasonboard.com> References: <20220816015414.7462-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 3/9] test: yaml-parser: Test out-of-range checks on integer parsing X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 Reviewed-by: Jacopo Mondi Reviewed-by: Paul Elder --- test/yaml-parser.cpp | 77 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp index 803e70beb782..28f8cc8822b3 100644 --- a/test/yaml-parser.cpp +++ b/test/yaml-parser.cpp @@ -24,8 +24,10 @@ using namespace std; static const string testYaml = "string: libcamera\n" "double: 3.14159\n" - "uint32_t: 100\n" - "int32_t: -100\n" + "int16_t: -1000\n" + "uint16_t: 1000\n" + "int32_t: -100000\n" + "uint32_t: 100000\n" "size: [1920, 1080]\n" "list:\n" " - James\n" @@ -74,6 +76,8 @@ protected: enum class Type { String, + Int16, + UInt16, Int32, UInt32, Double, @@ -86,8 +90,10 @@ protected: { bool isList = type == Type::List || type == Type::Size; bool isScalar = !isList && type != Type::Dictionary; - bool isInteger = type == Type::Int32 || type == Type::UInt32; - bool isSigned = type == Type::Int32; + bool isInteger16 = type == Type::Int16 || type == Type::UInt16; + 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())) { std::cerr @@ -118,6 +124,20 @@ protected: return TestFail; } + if (!isInteger16 && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "int16_t" << std::endl; + return TestFail; + } + + if ((!isInteger16 || isSigned) && obj.get()) { + std::cerr + << "Object " << name << " didn't fail to parse as " + << "uint16_t" << std::endl; + return TestFail; + } + if (!isInteger && obj.get()) { std::cerr << "Object " << name << " didn't fail to parse as " @@ -154,7 +174,8 @@ protected: { uint64_t unsignedValue = static_cast(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. */ @@ -174,6 +195,26 @@ protected: return TestFail; } + if (isInteger16) { + if (obj.get().value_or(0) != value || + obj.get(0) != value) { + std::cerr + << "Object " << name << " failed to parse as " + << "int16_t" << std::endl; + return TestFail; + } + } + + if (isInteger16 && !isSigned) { + if (obj.get().value_or(0) != unsignedValue || + obj.get(0) != unsignedValue) { + std::cerr + << "Object " << name << " failed to parse as " + << "uint16_t" << std::endl; + return TestFail; + } + } + if (obj.get().value_or(0) != value || obj.get(0) != value) { std::cerr @@ -231,8 +272,8 @@ protected: } std::vector rootElemNames = { - "string", "double", "int32_t", "uint32_t", "size", - "list", "dictionary", "level1", + "string", "double", "int16_t", "uint16_t", "int32_t", + "uint32_t", "size", "list", "dictionary", "level1", }; for (const char *name : rootElemNames) { @@ -255,13 +296,31 @@ protected: 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 */ auto &int32Obj = (*root)["int32_t"]; if (testObjectType(int32Obj, "int32_t", Type::Int32) != TestPass) return TestFail; - if (testIntegerObject(int32Obj, "int32_t", Type::Int32, -100) != TestPass) + if (testIntegerObject(int32Obj, "int32_t", Type::Int32, -100000) != TestPass) return TestFail; /* Test uint32_t object */ @@ -270,7 +329,7 @@ protected: if (testObjectType(uint32Obj, "uint32_t", Type::UInt32) != TestPass) return TestFail; - if (testIntegerObject(uint32Obj, "uint32_t", Type::UInt32, 100) != TestPass) + if (testIntegerObject(uint32Obj, "uint32_t", Type::UInt32, 100000) != TestPass) return TestFail; /* Test double value */