@@ -43,6 +43,8 @@ private slots:
4343 void initParserEmpty ();
4444
4545 // parsing API
46+ void integers_data ();
47+ void integers ();
4648 void fixed_data ();
4749 void fixed ();
4850 void strings_data ();
@@ -322,6 +324,104 @@ void addFixedData()
322324
323325}
324326
327+ static void addIntegers ()
328+ {
329+ QTest::addColumn<QByteArray>(" data" );
330+ QTest::addColumn<quint64>(" expectedRaw" );
331+ QTest::addColumn<qint64>(" expectedValue" );
332+ QTest::addColumn<bool >(" isNegative" );
333+ QTest::addColumn<bool >(" inInt64Range" );
334+
335+ // unsigned integers
336+ QTest::newRow (" 0" ) << raw (" \x00 " ) << Q_UINT64_C (0 ) << Q_INT64_C (0 ) << false << true ;
337+ QTest::newRow (" 1" ) << raw (" \x01 " ) << Q_UINT64_C (1 ) << Q_INT64_C (1 ) << false << true ;
338+ QTest::newRow (" 10" ) << raw (" \x0a " ) << Q_UINT64_C (10 ) << Q_INT64_C (10 ) << false << true ;
339+ QTest::newRow (" 23" ) << raw (" \x17 " ) << Q_UINT64_C (23 ) << Q_INT64_C (23 ) << false << true ;
340+ QTest::newRow (" 24" ) << raw (" \x18\x18 " ) << Q_UINT64_C (24 ) << Q_INT64_C (24 ) << false << true ;
341+ QTest::newRow (" UINT8_MAX" ) << raw (" \x18\xff " ) << Q_UINT64_C (255 ) << Q_INT64_C (255 ) << false << true ;
342+ QTest::newRow (" UINT8_MAX+1" ) << raw (" \x19\x01\x00 " ) << Q_UINT64_C (256 ) << Q_INT64_C (256 ) << false << true ;
343+ QTest::newRow (" UINT16_MAX" ) << raw (" \x19\xff\xff " ) << Q_UINT64_C (65535 ) << Q_INT64_C (65535 ) << false << true ;
344+ QTest::newRow (" UINT16_MAX+1" ) << raw (" \x1a\0\1\x00\x00 " ) << Q_UINT64_C (65536 ) << Q_INT64_C (65536 ) << false << true ;
345+ QTest::newRow (" UINT32_MAX" ) << raw (" \x1a\xff\xff\xff\xff " ) << Q_UINT64_C (4294967295 ) << Q_INT64_C (4294967295 ) << false << true ;
346+ QTest::newRow (" UINT32_MAX+1" ) << raw (" \x1b\0\0\0\1\0\0\0\0 " ) << Q_UINT64_C (4294967296 ) << Q_INT64_C (4294967296 ) << false << true ;
347+ QTest::newRow (" INT64_MAX" ) << raw (" \x1b " " \x7f\xff\xff\xff " " \xff\xff\xff\xff " )
348+ << quint64 (std::numeric_limits<qint64>::max ())
349+ << std::numeric_limits<qint64>::max () << false << true ;
350+ QTest::newRow (" UINT64_MAX" ) << raw (" \x1b " " \xff\xff\xff\xff " " \xff\xff\xff\xff " )
351+ << std::numeric_limits<quint64>::max () << qint64 (-123456 ) << false << false ;
352+
353+ // negative integers
354+ QTest::newRow (" -1" ) << raw (" \x20 " ) << Q_UINT64_C (0 ) << Q_INT64_C (-1 ) << true << true ;
355+ QTest::newRow (" -2" ) << raw (" \x21 " ) << Q_UINT64_C (1 ) << Q_INT64_C (-2 ) << true << true ;
356+ QTest::newRow (" -24" ) << raw (" \x37 " ) << Q_UINT64_C (23 ) << Q_INT64_C (-24 ) << true << true ;
357+ QTest::newRow (" -25" ) << raw (" \x38\x18 " ) << Q_UINT64_C (24 ) << Q_INT64_C (-25 ) << true << true ;
358+ QTest::newRow (" -UINT8_MAX" ) << raw (" \x38\xff " ) << Q_UINT64_C (255 ) << Q_INT64_C (-256 ) << true << true ;
359+ QTest::newRow (" -UINT8_MAX-1" ) << raw (" \x39\x01\x00 " ) << Q_UINT64_C (256 ) << Q_INT64_C (-257 ) << true << true ;
360+ QTest::newRow (" -UINT16_MAX" ) << raw (" \x39\xff\xff " ) << Q_UINT64_C (65535 ) << Q_INT64_C (-65536 ) << true << true ;
361+ QTest::newRow (" -UINT16_MAX-1" ) << raw (" \x3a\0\1\x00\x00 " ) << Q_UINT64_C (65536 ) << Q_INT64_C (-65537 ) << true << true ;
362+ QTest::newRow (" -UINT32_MAX" ) << raw (" \x3a\xff\xff\xff\xff " ) << Q_UINT64_C (4294967295 ) << Q_INT64_C (-4294967296 ) << true << true ;
363+ QTest::newRow (" -UINT32_MAX-1" ) << raw (" \x3b\0\0\0\1\0\0\0\0 " ) << Q_UINT64_C (4294967296 ) << Q_INT64_C (-4294967297 ) << true << true ;
364+ QTest::newRow (" INT64_MIN+1" ) << raw (" \x3b\x7f\xff\xff\xff " " \xff\xff\xff\xfe " )
365+ << quint64 (std::numeric_limits<qint64>::max () - 1 )
366+ << (std::numeric_limits<qint64>::min () + 1 )
367+ << true << true ;
368+ QTest::newRow (" INT64_MIN" ) << raw (" \x3b\x7f\xff\xff\xff " " \xff\xff\xff\xff " )
369+ << quint64 (std::numeric_limits<qint64>::max ())
370+ << std::numeric_limits<qint64>::min ()
371+ << true << true ;
372+ QTest::newRow (" INT64_MIN-1" ) << raw (" \x3b\x80\0\0\0 " " \0\0\0\0 " ) << Q_UINT64_C (9223372036854775808 ) << qint64 (-123456 ) << true << false ;
373+ QTest::newRow (" -UINT64_MAX" ) << raw (" \x3b " " \xff\xff\xff\xff " " \xff\xff\xff\xfe " )
374+ << (std::numeric_limits<quint64>::max () - 1 ) << qint64 (-123456 ) << true << false ;
375+ QTest::newRow (" -UINT64_MAX+1" ) << raw (" \x3b " " \xff\xff\xff\xff " " \xff\xff\xff\xff " )
376+ << std::numeric_limits<quint64>::max () << qint64 (-123456 ) << true << false ;
377+ }
378+
379+ void tst_Parser::integers_data ()
380+ {
381+ addIntegers ();
382+ }
383+
384+ void tst_Parser::integers ()
385+ {
386+ QFETCH (QByteArray, data);
387+ QFETCH (bool , isNegative);
388+ QFETCH (quint64, expectedRaw);
389+ QFETCH (qint64, expectedValue);
390+ QFETCH (bool , inInt64Range);
391+
392+ CborParser parser;
393+ CborValue first;
394+ CborError err = cbor_parser_init (reinterpret_cast <const quint8 *>(data.constData ()), data.length (), 0 , &parser, &first);
395+ QVERIFY2 (!err, QByteArray (" Got error \" " ) + cbor_error_string (err) + " \" " );
396+ QVERIFY (cbor_value_is_integer (&first));
397+
398+ uint64_t raw;
399+ cbor_value_get_raw_integer (&first, &raw);
400+ QCOMPARE (quint64 (raw), expectedRaw);
401+
402+ if (isNegative) {
403+ QVERIFY (cbor_value_is_negative_integer (&first));
404+ QVERIFY (!cbor_value_is_unsigned_integer (&first));
405+ } else {
406+ QVERIFY (!cbor_value_is_negative_integer (&first));
407+ QVERIFY (cbor_value_is_unsigned_integer (&first));
408+ }
409+
410+ int64_t value;
411+ if (inInt64Range) {
412+ cbor_value_get_int64 (&first, &value);
413+ QCOMPARE (qint64 (value), expectedValue);
414+ }
415+
416+ err = cbor_value_get_int64_checked (&first, &value);
417+ QCOMPARE (err, inInt64Range ? CborNoError : CborErrorDataTooLarge);
418+
419+ int ivalue;
420+ bool inIntRange = inInt64Range && (expectedValue == int (expectedValue));
421+ err = cbor_value_get_int_checked (&first, &ivalue);
422+ QCOMPARE (err, inIntRange ? CborNoError : CborErrorDataTooLarge);
423+ }
424+
325425void tst_Parser::fixed_data ()
326426{
327427 addColumns ();
0 commit comments