@@ -979,6 +979,72 @@ TEST(ParseCBORTest, UnexpectedEofInMapError) {
979979 EXPECT_EQ (" " , out);
980980}
981981
982+ TEST (ParseCBORTest, TopLevelCantBeEmptyEnvelope) {
983+ // Normally, an array would be allowed inside an envelope, but
984+ // the top-level envelope is required to contain a map.
985+ std::vector<uint8_t > bytes = {0xd8 , 0x5a , 0 , 0 , 0 , 0 }; // envelope
986+ std::string out;
987+ Status status;
988+ std::unique_ptr<StreamingParserHandler> json_writer =
989+ NewJSONEncoder (&GetTestPlatform (), &out, &status);
990+ ParseCBOR (span<uint8_t >(bytes.data (), bytes.size ()), json_writer.get ());
991+ EXPECT_EQ (Error::CBOR_MAP_START_EXPECTED , status.error );
992+ EXPECT_EQ (bytes.size (), status.pos );
993+ EXPECT_EQ (" " , out);
994+ }
995+
996+ TEST (ParseCBORTest, MapStartExpectedAtTopLevel) {
997+ // Normally, an array would be allowed inside an envelope, but
998+ // the top-level envelope is required to contain a map.
999+ constexpr uint8_t kPayloadLen = 1 ;
1000+ std::vector<uint8_t > bytes = {0xd8 ,
1001+ 0x5a ,
1002+ 0 ,
1003+ 0 ,
1004+ 0 ,
1005+ kPayloadLen , // envelope
1006+ EncodeIndefiniteLengthArrayStart ()};
1007+ EXPECT_EQ (kPayloadLen , bytes.size () - 6 );
1008+ std::string out;
1009+ Status status;
1010+ std::unique_ptr<StreamingParserHandler> json_writer =
1011+ NewJSONEncoder (&GetTestPlatform (), &out, &status);
1012+ ParseCBOR (span<uint8_t >(bytes.data (), bytes.size ()), json_writer.get ());
1013+ EXPECT_EQ (Error::CBOR_MAP_START_EXPECTED , status.error );
1014+ EXPECT_EQ (6u , status.pos );
1015+ EXPECT_EQ (" " , out);
1016+ }
1017+
1018+ TEST (ParseCBORTest, OnlyMapsAndArraysSupportedInsideEnvelopes) {
1019+ // The top level is a map with key "foo", and the value
1020+ // is an envelope that contains just a number (1). We don't
1021+ // allow numbers to be contained in an envelope though, only
1022+ // maps and arrays.
1023+ constexpr uint8_t kPayloadLen = 1 ;
1024+ std::vector<uint8_t > bytes = {0xd8 ,
1025+ 0x5a ,
1026+ 0 ,
1027+ 0 ,
1028+ 0 ,
1029+ kPayloadLen , // envelope
1030+ EncodeIndefiniteLengthMapStart ()};
1031+ EncodeString8 (SpanFrom (" foo" ), &bytes);
1032+ for (uint8_t byte : {0xd8 , 0x5a , 0 , 0 , 0 , /* payload_len*/ 1 })
1033+ bytes.emplace_back (byte);
1034+ size_t error_pos = bytes.size ();
1035+ bytes.push_back (1 ); // Envelope contents / payload = number 1.
1036+ bytes.emplace_back (EncodeStop ());
1037+
1038+ std::string out;
1039+ Status status;
1040+ std::unique_ptr<StreamingParserHandler> json_writer =
1041+ NewJSONEncoder (&GetTestPlatform (), &out, &status);
1042+ ParseCBOR (span<uint8_t >(bytes.data (), bytes.size ()), json_writer.get ());
1043+ EXPECT_EQ (Error::CBOR_MAP_OR_ARRAY_EXPECTED_IN_ENVELOPE , status.error );
1044+ EXPECT_EQ (error_pos, status.pos );
1045+ EXPECT_EQ (" " , out);
1046+ }
1047+
9821048TEST (ParseCBORTest, InvalidMapKeyError) {
9831049 constexpr uint8_t kPayloadLen = 2 ;
9841050 std::vector<uint8_t > bytes = {0xd8 , 0x5a , 0 ,
@@ -1195,18 +1261,18 @@ TEST(ParseCBORTest, InvalidSignedError) {
11951261}
11961262
11971263TEST (ParseCBORTest, TrailingJunk) {
1198- constexpr uint8_t kPayloadLen = 35 ;
1264+ constexpr uint8_t kPayloadLen = 12 ;
11991265 std::vector<uint8_t > bytes = {0xd8 , 0x5a , 0 , 0 , 0 , kPayloadLen , // envelope
12001266 0xbf }; // map start
12011267 EncodeString8 (SpanFrom (" key" ), &bytes);
12021268 EncodeString8 (SpanFrom (" value" ), &bytes);
12031269 bytes.push_back (0xff ); // Up to here, it's a perfectly fine msg.
1270+ ASSERT_EQ (kPayloadLen , bytes.size () - 6 );
12041271 size_t error_pos = bytes.size ();
1272+ // Now write some trailing junk after the message.
12051273 EncodeString8 (SpanFrom (" trailing junk" ), &bytes);
1206-
12071274 internals::WriteTokenStart (MajorType::UNSIGNED ,
12081275 std::numeric_limits<uint64_t >::max (), &bytes);
1209- EXPECT_EQ (kPayloadLen , bytes.size () - 6 );
12101276 std::string out;
12111277 Status status;
12121278 std::unique_ptr<StreamingParserHandler> json_writer =
@@ -1217,6 +1283,29 @@ TEST(ParseCBORTest, TrailingJunk) {
12171283 EXPECT_EQ (" " , out);
12181284}
12191285
1286+ TEST (ParseCBORTest, EnvelopeContentsLengthMismatch) {
1287+ constexpr uint8_t kPartialPayloadLen = 5 ;
1288+ std::vector<uint8_t > bytes = {0xd8 , 0x5a , 0 ,
1289+ 0 , 0 , kPartialPayloadLen , // envelope
1290+ 0xbf }; // map start
1291+ EncodeString8 (SpanFrom (" key" ), &bytes);
1292+ // kPartialPayloadLen would need to indicate the length of the entire map,
1293+ // all the way past the 0xff map stop character. Instead, it only covers
1294+ // a portion of the map.
1295+ EXPECT_EQ (bytes.size () - 6 , kPartialPayloadLen );
1296+ EncodeString8 (SpanFrom (" value" ), &bytes);
1297+ bytes.push_back (0xff ); // map stop
1298+
1299+ std::string out;
1300+ Status status;
1301+ std::unique_ptr<StreamingParserHandler> json_writer =
1302+ NewJSONEncoder (&GetTestPlatform (), &out, &status);
1303+ ParseCBOR (span<uint8_t >(bytes.data (), bytes.size ()), json_writer.get ());
1304+ EXPECT_EQ (Error::CBOR_ENVELOPE_CONTENTS_LENGTH_MISMATCH , status.error );
1305+ EXPECT_EQ (bytes.size (), status.pos );
1306+ EXPECT_EQ (" " , out);
1307+ }
1308+
12201309// =============================================================================
12211310// cbor::AppendString8EntryToMap - for limited in-place editing of messages
12221311// =============================================================================
@@ -1376,7 +1465,7 @@ TEST(JsonEncoder, IncompleteUtf8Sequence) {
13761465
13771466 { // 🌎 takes four bytes to encode in UTF-8. We test with the first three;
13781467 // This means we're trying to emit a string that consists solely of an
1379- // incomplete UTF-8 sequence. So the string in the JSON output is emtpy .
1468+ // incomplete UTF-8 sequence. So the string in the JSON output is empty .
13801469 std::string world_utf8 = " 🌎" ;
13811470 ASSERT_EQ (4u , world_utf8.size ());
13821471 std::vector<uint8_t > chars (world_utf8.begin (), world_utf8.begin () + 3 );
0 commit comments