From dcb1091fd299e1e75c04603fe6661208066faa85 Mon Sep 17 00:00:00 2001 From: Stuart Longland Date: Sat, 3 Jul 2021 10:12:31 +1000 Subject: [PATCH] WSHUB-458: reader unit tests: Simplify the example reader We simplify this reader in two ways: 1. we remove the `consumed` member of `struct Input`, and instead use the `CborValue`'s `source.token` member, which we treat as an unsigned integer offset into our `QByteArray`. 2. we replace the reader-specific `struct Input` with the `QByteArray` it was wrapping, since that's the only thing now contained in our `struct Input`. If a `CborValue` gets cloned, the pointer referred to by `source.token` similarly gets cloned, thus when we advance the pointer on the clone, it leaves the original alone, so computing the length of unknown-length entities in the CBOR document can be done safely. --- tests/parser/tst_parser.cpp | 50 ++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/tests/parser/tst_parser.cpp b/tests/parser/tst_parser.cpp index bd52e933..73c391b1 100644 --- a/tests/parser/tst_parser.cpp +++ b/tests/parser/tst_parser.cpp @@ -757,32 +757,32 @@ void tst_Parser::mapsAndArrays() "{_ 1: [_ " + expected + "], \"Hello\": {_ " + expected + ": (_ )}}"); } -struct Input { - QByteArray data; - int consumed; -}; - static const CborParserOperations byteArrayOps = { /* can_read_bytes = */ [](const CborValue *value, size_t len) { - auto input = static_cast(value->parser->data.ctx); - return input->data.size() - input->consumed >= int(len); + auto data = static_cast(value->parser->data.ctx); + auto consumed = uintptr_t(value->source.token); + return uintptr_t(data->size()) - consumed >= uintptr_t(len); }, /* read_bytes = */ [](const CborValue *value, void *dst, size_t offset, size_t len) { - auto input = static_cast(value->parser->data.ctx); - return memcpy(dst, input->data.constData() + input->consumed + offset, len); + auto data = static_cast(value->parser->data.ctx); + auto consumed = uintptr_t(value->source.token); + return memcpy(dst, data->constData() + consumed + offset, len); }, /* advance_bytes = */ [](CborValue *value, size_t len) { - auto input = static_cast(value->parser->data.ctx); - input->consumed += int(len); + auto consumed = uintptr_t(value->source.token); + consumed += int(len); + value->source.token = (void*)consumed; }, /* transfer_string = */ [](CborValue *value, const void **userptr, size_t offset, size_t len) { // ### - auto input = static_cast(value->parser->data.ctx); - if (input->data.size() - input->consumed < int(len + offset)) + auto data = static_cast(value->parser->data.ctx); + auto consumed = uintptr_t(value->source.token); + if (uintptr_t(data->size()) - consumed < uintptr_t(len + offset)) return CborErrorUnexpectedEOF; - input->consumed += int(offset); - *userptr = input->data.constData() + input->consumed; - input->consumed += int(len); + consumed += int(offset); + *userptr = data->constData() + consumed; + consumed += int(len); + value->source.token = (void*)consumed; return CborNoError; } }; @@ -792,11 +792,9 @@ void tst_Parser::readerApi() QFETCH(QByteArray, data); QFETCH(QString, expected); - Input input = { data, 0 }; - CborParser parser; CborValue first; - CborError err = cbor_parser_init_reader(&byteArrayOps, &parser, &first, &input); + CborError err = cbor_parser_init_reader(&byteArrayOps, &parser, &first, &data); QCOMPARE(err, CborNoError); QString decoded; @@ -805,7 +803,7 @@ void tst_Parser::readerApi() QCOMPARE(decoded, expected); // check we consumed everything - QCOMPARE(input.consumed, data.size()); + QCOMPARE(uintptr_t(first.source.token), uintptr_t(data.size())); } void tst_Parser::reparse_data() @@ -820,23 +818,23 @@ void tst_Parser::reparse() QFETCH(QByteArray, data); QFETCH(QString, expected); - Input input = { QByteArray(), 0 }; + QByteArray buffer; CborParser parser; CborValue first; - CborError err = cbor_parser_init_reader(&byteArrayOps, &parser, &first, &input); + CborError err = cbor_parser_init_reader(&byteArrayOps, &parser, &first, &buffer); QCOMPARE(err, CborErrorUnexpectedEOF); for (int i = 0; i < data.size(); ++i) { - input.data = data.left(i); + buffer = data.left(i); err = cbor_value_reparse(&first); if (err != CborErrorUnexpectedEOF) qDebug() << "At" << i; QCOMPARE(err, CborErrorUnexpectedEOF); - QCOMPARE(input.consumed, 0); + QCOMPARE(uintptr_t(first.source.token), 0U); } // now it should work - input.data = data; + buffer = data; err = cbor_value_reparse(&first); QCOMPARE(err, CborNoError); @@ -846,7 +844,7 @@ void tst_Parser::reparse() QCOMPARE(decoded, expected); // check we consumed everything - QCOMPARE(input.consumed, data.size()); + QCOMPARE(uintptr_t(first.source.token), uintptr_t(data.size())); } void tst_Parser::chunkedString_data()