diff --git a/src/lib/core/CHIPTLVReader.cpp b/src/lib/core/CHIPTLVReader.cpp index 81b8636ccb4960..0b6c348dc8a96f 100644 --- a/src/lib/core/CHIPTLVReader.cpp +++ b/src/lib/core/CHIPTLVReader.cpp @@ -130,41 +130,73 @@ CHIP_ERROR TLVReader::Get(bool & v) CHIP_ERROR TLVReader::Get(int8_t & v) { - uint64_t v64 = 0; + int64_t v64 = 0; CHIP_ERROR err = Get(v64); - v = CastToSigned(static_cast(v64)); + if (!CanCastTo(v64)) + { + return CHIP_ERROR_INVALID_INTEGER_VALUE; + } + v = static_cast(v64); return err; } CHIP_ERROR TLVReader::Get(int16_t & v) { - uint64_t v64 = 0; + int64_t v64 = 0; CHIP_ERROR err = Get(v64); - v = CastToSigned(static_cast(v64)); + if (!CanCastTo(v64)) + { + return CHIP_ERROR_INVALID_INTEGER_VALUE; + } + v = static_cast(v64); return err; } CHIP_ERROR TLVReader::Get(int32_t & v) { - uint64_t v64 = 0; + int64_t v64 = 0; CHIP_ERROR err = Get(v64); - v = CastToSigned(static_cast(v64)); + if (!CanCastTo(v64)) + { + return CHIP_ERROR_INVALID_INTEGER_VALUE; + } + v = static_cast(v64); return err; } CHIP_ERROR TLVReader::Get(int64_t & v) { - uint64_t v64 = 0; - CHIP_ERROR err = Get(v64); - v = CastToSigned(v64); - return err; + // Internal callers of this method depend on it not modifying "v" on failure. + switch (ElementType()) + { + case TLVElementType::Int8: + v = CastToSigned(static_cast(mElemLenOrVal)); + break; + case TLVElementType::Int16: + v = CastToSigned(static_cast(mElemLenOrVal)); + break; + case TLVElementType::Int32: + v = CastToSigned(static_cast(mElemLenOrVal)); + break; + case TLVElementType::Int64: + v = CastToSigned(mElemLenOrVal); + break; + default: + return CHIP_ERROR_WRONG_TLV_TYPE; + } + + return CHIP_NO_ERROR; } CHIP_ERROR TLVReader::Get(uint8_t & v) { uint64_t v64 = 0; CHIP_ERROR err = Get(v64); - v = static_cast(v64); + if (!CanCastTo(v64)) + { + return CHIP_ERROR_INVALID_INTEGER_VALUE; + } + v = static_cast(v64); return err; } @@ -172,7 +204,11 @@ CHIP_ERROR TLVReader::Get(uint16_t & v) { uint64_t v64 = 0; CHIP_ERROR err = Get(v64); - v = static_cast(v64); + if (!CanCastTo(v64)) + { + return CHIP_ERROR_INVALID_INTEGER_VALUE; + } + v = static_cast(v64); return err; } @@ -180,24 +216,19 @@ CHIP_ERROR TLVReader::Get(uint32_t & v) { uint64_t v64 = 0; CHIP_ERROR err = Get(v64); - v = static_cast(v64); + if (!CanCastTo(v64)) + { + return CHIP_ERROR_INVALID_INTEGER_VALUE; + } + v = static_cast(v64); return err; } CHIP_ERROR TLVReader::Get(uint64_t & v) { + // Internal callers of this method depend on it not modifying "v" on failure. switch (ElementType()) { - case TLVElementType::Int8: - v = static_cast(static_cast(CastToSigned(static_cast(mElemLenOrVal)))); - break; - case TLVElementType::Int16: - v = static_cast(static_cast(CastToSigned(static_cast(mElemLenOrVal)))); - break; - case TLVElementType::Int32: - v = static_cast(static_cast(CastToSigned(static_cast(mElemLenOrVal)))); - break; - case TLVElementType::Int64: case TLVElementType::UInt8: case TLVElementType::UInt16: case TLVElementType::UInt32: diff --git a/src/lib/core/tests/TestCHIPTLV.cpp b/src/lib/core/tests/TestCHIPTLV.cpp index 3d61ddc0bff7a9..2ac5609e929d4a 100644 --- a/src/lib/core/tests/TestCHIPTLV.cpp +++ b/src/lib/core/tests/TestCHIPTLV.cpp @@ -50,7 +50,8 @@ using namespace chip::TLV; enum { TestProfile_1 = 0xAABBCCDD, - TestProfile_2 = 0x11223344 + TestProfile_2 = 0x11223344, + TestProfile_3 = 0x11223355 }; // clang-format off @@ -359,6 +360,206 @@ static const uint8_t Encoding1_DataMacro [] = }; // clang-format on +static CHIP_ERROR WriteIntMinMax(nlTestSuite * inSuite, TLVWriter & writer) +{ + CHIP_ERROR err; + + err = writer.Put(AnonymousTag, static_cast(INT8_MIN)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(INT8_MAX)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(INT16_MIN)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(INT16_MAX)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(INT32_MIN)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(INT32_MAX)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(INT64_MIN)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(INT64_MAX)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(UINT8_MAX)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(UINT16_MAX)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(UINT32_MAX)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + err = writer.Put(AnonymousTag, static_cast(UINT64_MAX)); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + return err; +} + +static void CheckIntMinMax(nlTestSuite * inSuite, TLVReader & reader) +{ + // Writer did Put(AnonymousTag, static_cast(INT8_MIN)) + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT8_MIN)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT8_MIN)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT8_MIN)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT8_MIN)); + + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(INT8_MAX)) + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT8_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT8_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT8_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT8_MAX)); + + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(INT16_MIN)) + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT16_MIN)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT16_MIN)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT16_MIN)); + + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(INT16_MAX)) + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT16_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT16_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT16_MAX)); + + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(INT32_MIN)) + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT32_MIN)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT32_MIN)); + + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(INT32_MAX)) + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT32_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT32_MAX)); + + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(INT64_MIN)) + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT64_MIN)); + + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(INT64_MAX)) + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(INT64_MAX)); + + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_SignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(UINT8_MAX)) + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT8_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT8_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT8_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT8_MAX)); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(UINT16_MAX)) + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT16_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT16_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT16_MAX)); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(UINT32_MAX)) + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT32_MAX)); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT32_MAX)); + + TestNext(inSuite, reader); + + // Writer did Put(AnonymousTag, static_cast(UINT64_MAX)) + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_WRONG_TLV_TYPE); + + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(0), CHIP_ERROR_INVALID_INTEGER_VALUE); + TEST_GET_NOERROR(inSuite, reader, kTLVType_UnsignedInteger, AnonymousTag, static_cast(UINT64_MAX)); +} + void WriteEncoding1(nlTestSuite * inSuite, TLVWriter & writer) { CHIP_ERROR err; @@ -509,10 +710,10 @@ void ReadEncoding1(nlTestSuite * inSuite, TLVReader & reader) TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); - TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); - TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); - TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); - TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); + TEST_GET(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42), CHIP_ERROR_WRONG_TLV_TYPE); TestNext(inSuite, reader3); @@ -520,6 +721,7 @@ void ReadEncoding1(nlTestSuite * inSuite, TLVReader & reader) TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(-17)); TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(-17)); TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(-17)); + TEST_GET(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(-17), CHIP_ERROR_WRONG_TLV_TYPE); TestNext(inSuite, reader3); @@ -528,7 +730,8 @@ void ReadEncoding1(nlTestSuite * inSuite, TLVReader & reader) TestNext(inSuite, reader3); - TEST_GET_NOERROR(inSuite, reader3, kTLVType_UnsignedInteger, AnonymousTag, static_cast(40000000000ULL)); + TEST_GET(inSuite, reader3, kTLVType_UnsignedInteger, AnonymousTag, static_cast(40000000000ULL), + CHIP_ERROR_WRONG_TLV_TYPE); TEST_GET_NOERROR(inSuite, reader3, kTLVType_UnsignedInteger, AnonymousTag, static_cast(40000000000ULL)); TestNext(inSuite, reader3); @@ -1381,6 +1584,39 @@ void CheckSimpleWriteRead(nlTestSuite * inSuite, void * inContext) ReadEncoding1(inSuite, reader); } +static void TestIntMinMax(nlTestSuite * inSuite, void * inContext) +{ + CHIP_ERROR err; + + uint8_t buf[2048]; + TLVWriter writer, writer1; + TLVReader reader, reader1; + + writer.Init(buf, sizeof(buf)); + writer.ImplicitProfileId = TestProfile_3; + + err = writer.OpenContainer(ProfileTag(TestProfile_3, 1), kTLVType_Array, writer1); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + WriteIntMinMax(inSuite, writer1); + + err = writer.CloseContainer(writer1); + NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); + + reader.Init(buf, sizeof(buf)); + reader.ImplicitProfileId = TestProfile_3; + + TestNext(inSuite, reader); + + TestAndOpenContainer(inSuite, reader, kTLVType_Array, ProfileTag(TestProfile_3, 1), reader1); + + TestNext(inSuite, reader1); + + CheckIntMinMax(inSuite, reader1); + + TestEndAndCloseContainer(inSuite, reader, reader1); +} + /** * Log the specified message in the form of @a aFormat. * @@ -2981,8 +3217,8 @@ void TestCHIPTLVReaderDup(nlTestSuite * inSuite) TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); - TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); - TEST_GET_NOERROR(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42)); + TEST_GET(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42), CHIP_ERROR_WRONG_TLV_TYPE); + TEST_GET(inSuite, reader3, kTLVType_SignedInteger, AnonymousTag, static_cast(42), CHIP_ERROR_WRONG_TLV_TYPE); TestNext(inSuite, reader3); @@ -2998,7 +3234,8 @@ void TestCHIPTLVReaderDup(nlTestSuite * inSuite) TestNext(inSuite, reader3); - TEST_GET_NOERROR(inSuite, reader3, kTLVType_UnsignedInteger, AnonymousTag, static_cast(40000000000ULL)); + TEST_GET(inSuite, reader3, kTLVType_UnsignedInteger, AnonymousTag, static_cast(40000000000ULL), + CHIP_ERROR_WRONG_TLV_TYPE); TEST_GET_NOERROR(inSuite, reader3, kTLVType_UnsignedInteger, AnonymousTag, static_cast(40000000000ULL)); TestNext(inSuite, reader3); @@ -4090,6 +4327,7 @@ static const nlTest sTests[] = NL_TEST_DEF("CHIP TLV Reader Fuzz Test", TLVReaderFuzzTest), NL_TEST_DEF("CHIP TLV GetStringView Test", CheckGetStringView), NL_TEST_DEF("CHIP TLV GetByteView Test", CheckGetByteView), + NL_TEST_DEF("Int Min/Max Test", TestIntMinMax), NL_TEST_SENTINEL() };