Bläddra i källkod

deserializeMsgPack() inserts null instead of returning NotSupported

Benoit Blanchon 4 år sedan
förälder
incheckning
71f44aaee1

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@ HEAD
 
 * Removed support for `char` values, see below (issue #1498)
 * `deserializeJson()` leaves `\uXXXX` unchanged instead of returning `NotSupported`
+* `deserializeMsgPack()` inserts `null` instead of returning `NotSupported`
 
 > ### BREAKING CHANGES
 >

+ 2 - 1
extras/tests/MsgPackDeserializer/deserializeVariant.cpp

@@ -20,7 +20,8 @@ static void check(const char* input, U expected) {
 static void checkNotSupported(const char* input) {
   DynamicJsonDocument doc(4096);
   DeserializationError error = deserializeMsgPack(doc, input);
-  REQUIRE(error == DeserializationError::NotSupported);
+  REQUIRE(error == DeserializationError::Ok);
+  REQUIRE(doc.isNull());
 }
 #endif
 

+ 47 - 0
extras/tests/MsgPackDeserializer/incompleteInput.cpp

@@ -108,4 +108,51 @@ TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
   SECTION("str 32") {
     checkAllSizes("\xdb\x00\x00\x00\x05hello", 10);
   }
+
+  SECTION("bin 8") {
+    checkAllSizes("\xc4\x01X", 3);
+  }
+
+  SECTION("bin 16") {
+    checkAllSizes("\xc5\x00\x01X", 4);
+  }
+
+  SECTION("bin 32") {
+    checkAllSizes("\xc6\x00\x00\x00\x01X", 6);
+  }
+
+  SECTION("ext 8") {
+    checkAllSizes("\xc7\x01\x01\x01", 4);
+  }
+
+  SECTION("ext 16") {
+    checkAllSizes("\xc8\x00\x01\x01\x01", 5);
+  }
+
+  SECTION("ext 32") {
+    checkAllSizes("\xc9\x00\x00\x00\x01\x01\x01", 7);
+  }
+
+  SECTION("fixext 1") {
+    checkAllSizes("\xd4\x01\x01", 3);
+  }
+
+  SECTION("fixext 2") {
+    checkAllSizes("\xd5\x01\x01\x02", 4);
+  }
+
+  SECTION("fixext 4") {
+    checkAllSizes("\xd6\x01\x01\x02\x03\x04", 6);
+  }
+
+  SECTION("fixext 8") {
+    checkAllSizes("\xd7\x01\x01\x02\x03\x04\x05\x06\x07\x08", 10);
+  }
+
+  SECTION("fixext 16") {
+    checkAllSizes(
+        "\xd8\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E"
+        "\x0F\x10",
+        18);
+  }
 }

+ 33 - 24
extras/tests/MsgPackDeserializer/notSupported.cpp

@@ -5,69 +5,78 @@
 #include <ArduinoJson.h>
 #include <catch.hpp>
 
-static void checkNotSupported(const char* input) {
+static void checkMsgPackDocument(const char* input, size_t inputSize,
+                                 const char* expectedJson) {
   DynamicJsonDocument doc(4096);
 
-  DeserializationError error = deserializeMsgPack(doc, input);
+  DeserializationError error = deserializeMsgPack(doc, input, inputSize);
 
-  REQUIRE(error == DeserializationError::NotSupported);
+  REQUIRE(error == DeserializationError::Ok);
+  std::string actualJson;
+  serializeJson(doc, actualJson);
+  REQUIRE(actualJson == expectedJson);
+}
+
+static void checkMsgPackError(const char* input, size_t inputSize,
+                              DeserializationError expectedError) {
+  DynamicJsonDocument doc(4096);
+
+  DeserializationError error = deserializeMsgPack(doc, input, inputSize);
+
+  REQUIRE(error == expectedError);
 }
 
 TEST_CASE("deserializeMsgPack() return NotSupported") {
   SECTION("bin 8") {
-    checkNotSupported("\xc4");
+    checkMsgPackDocument("\x92\xc4\x01X\x2A", 5, "[null,42]");
   }
 
   SECTION("bin 16") {
-    checkNotSupported("\xc5");
+    checkMsgPackDocument("\x92\xc5\x00\x01X\x2A", 6, "[null,42]");
   }
 
   SECTION("bin 32") {
-    checkNotSupported("\xc6");
+    checkMsgPackDocument("\x92\xc6\x00\x00\x00\x01X\x2A", 8, "[null,42]");
   }
 
   SECTION("ext 8") {
-    checkNotSupported("\xc7");
+    checkMsgPackDocument("\x92\xc7\x01\x01\x01\x2A", 6, "[null,42]");
   }
 
   SECTION("ext 16") {
-    checkNotSupported("\xc8");
+    checkMsgPackDocument("\x92\xc8\x00\x01\x01\x01\x2A", 7, "[null,42]");
   }
 
   SECTION("ext 32") {
-    checkNotSupported("\xc9");
+    checkMsgPackDocument("\x92\xc9\x00\x00\x00\x01\x01\x01\x2A", 9,
+                         "[null,42]");
   }
 
   SECTION("fixext 1") {
-    checkNotSupported("\xd4");
+    checkMsgPackDocument("\x92\xd4\x01\x01\x2A", 5, "[null,42]");
   }
 
   SECTION("fixext 2") {
-    checkNotSupported("\xd5");
+    checkMsgPackDocument("\x92\xd5\x01\x01\x02\x2A", 6, "[null,42]");
   }
 
   SECTION("fixext 4") {
-    checkNotSupported("\xd6");
+    checkMsgPackDocument("\x92\xd6\x01\x01\x02\x03\x04\x2A", 8, "[null,42]");
   }
 
   SECTION("fixext 8") {
-    checkNotSupported("\xd7");
+    checkMsgPackDocument("\x92\xd7\x01\x01\x02\x03\x04\x05\x06\x07\x08\x2A", 12,
+                         "[null,42]");
   }
 
   SECTION("fixext 16") {
-    checkNotSupported("\xd8");
-  }
-
-  SECTION("unsupported in array") {
-    checkNotSupported("\x91\xc4");
-  }
-
-  SECTION("unsupported in map") {
-    checkNotSupported("\x81\xc4\x00\xA1H");
-    checkNotSupported("\x81\xA1H\xc4\x00");
+    checkMsgPackDocument(
+        "\x92\xd8\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E"
+        "\x0F\x10\x2A",
+        20, "[null,42]");
   }
 
   SECTION("integer as key") {
-    checkNotSupported("\x81\x01\xA1H");
+    checkMsgPackError("\x81\x01\xA1H", 3, DeserializationError::InvalidInput);
   }
 }

+ 31 - 69
src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp

@@ -40,11 +40,6 @@ class MsgPackDeserializer {
     return false;
   }
 
-  bool notSupported() {
-    _error = DeserializationError::NotSupported;
-    return false;
-  }
-
   template <typename TFilter>
   bool parseVariant(VariantData &variant, TFilter filter,
                     NestingLimit nestingLimit) {
@@ -74,41 +69,23 @@ class MsgPackDeserializer {
           variant.setBoolean(true);
         return true;
 
-      case 0xc4:  // bin 8
-        if (allowValue)
-          return notSupported();
-        else
-          return skipString<uint8_t>();
+      case 0xc4:  // bin 8 (not supported)
+        return skipString<uint8_t>();
 
-      case 0xc5:  // bin 16
-        if (allowValue)
-          return notSupported();
-        else
-          return skipString<uint16_t>();
+      case 0xc5:  // bin 16 (not supported)
+        return skipString<uint16_t>();
 
-      case 0xc6:  // bin 32
-        if (allowValue)
-          return notSupported();
-        else
-          return skipString<uint32_t>();
+      case 0xc6:  // bin 32 (not supported)
+        return skipString<uint32_t>();
 
-      case 0xc7:  // ext 8
-        if (allowValue)
-          return notSupported();
-        else
-          return skipExt<uint8_t>();
+      case 0xc7:  // ext 8 (not supported)
+        return skipExt<uint8_t>();
 
-      case 0xc8:  // ext 16
-        if (allowValue)
-          return notSupported();
-        else
-          return skipExt<uint16_t>();
+      case 0xc8:  // ext 16 (not supported)
+        return skipExt<uint16_t>();
 
-      case 0xc9:  // ext 32
-        if (allowValue)
-          return notSupported();
-        else
-          return skipExt<uint32_t>();
+      case 0xc9:  // ext 32 (not supported)
+        return skipExt<uint32_t>();
 
       case 0xca:
         if (allowValue)
@@ -141,14 +118,14 @@ class MsgPackDeserializer {
           return skipBytes(4);
 
       case 0xcf:
-        if (allowValue)
 #if ARDUINOJSON_USE_LONG_LONG
+        if (allowValue)
           return readInteger<uint64_t>(variant);
-#else
-          return notSupported();
-#endif
         else
           return skipBytes(8);
+#else
+        return skipBytes(8);  // not supported
+#endif
 
       case 0xd0:
         if (allowValue)
@@ -169,44 +146,29 @@ class MsgPackDeserializer {
           return skipBytes(4);
 
       case 0xd3:
-        if (allowValue)
 #if ARDUINOJSON_USE_LONG_LONG
+        if (allowValue)
           return readInteger<int64_t>(variant);
+        else
+          return skipBytes(8);  // not supported
 #else
-          return notSupported();
+        return skipBytes(8);
 #endif
-        else
-          return skipBytes(8);
 
-      case 0xd4:  // fixext 1
-        if (allowValue)
-          return notSupported();
-        else
-          return skipBytes(2);
+      case 0xd4:  // fixext 1 (not supported)
+        return skipBytes(2);
 
-      case 0xd5:  // fixext 2
-        if (allowValue)
-          return notSupported();
-        else
-          return skipBytes(3);
+      case 0xd5:  // fixext 2 (not supported)
+        return skipBytes(3);
 
-      case 0xd6:  // fixext 4
-        if (allowValue)
-          return notSupported();
-        else
-          return skipBytes(5);
+      case 0xd6:  // fixext 4 (not supported)
+        return skipBytes(5);
 
-      case 0xd7:  // fixext 8
-        if (allowValue)
-          return notSupported();
-        else
-          return skipBytes(9);
+      case 0xd7:  // fixext 8 (not supported)
+        return skipBytes(9);
 
-      case 0xd8:  // fixext 16
-        if (allowValue)
-          return notSupported();
-        else
-          return skipBytes(17);
+      case 0xd8:  // fixext 16 (not supported)
+        return skipBytes(17);
 
       case 0xd9:
         if (allowValue)
@@ -508,7 +470,7 @@ class MsgPackDeserializer {
         return readString<uint32_t>();
 
       default:
-        return notSupported();
+        return invalidInput();
     }
   }