fixup decode logic for fragmented IOBufs
[folly.git] / folly / experimental / bser / test / BserTest.cpp
index 9c05355855635ceb41be4bacea232c1472a0da2b..97bf5c75dfadba6de334f8a6e4f9d5e9f11fccc2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2016-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -108,6 +108,7 @@ TEST(Bser, PduLength) {
         // complete length available
         auto buf = folly::IOBuf::wrapBuffer(template_blob, 3);
         auto len = folly::bser::decodePduLength(&*buf);
+        (void)len;
         LOG(ERROR) << "managed to return a length, but only had 3 bytes";
       }(),
       std::out_of_range);
@@ -117,5 +118,32 @@ TEST(Bser, PduLength) {
   EXPECT_EQ(len, 44) << "PduLength should be 44, got " << len;
 }
 
+TEST(Bser, CursorLength) {
+  folly::bser::serialization_opts opts;
+  std::string inputStr("hello there please break");
+
+  // This test is exercising the decode logic for pathological
+  // fragmentation cases.  We try a few permutations with the
+  // BSER header being fragmented to tickle boundary conditions
+
+  auto longSerialized = folly::bser::toBser(inputStr, opts);
+  for (uint32_t i = 1; i < longSerialized.size(); ++i) {
+    folly::IOBufQueue q;
+
+    q.append(folly::IOBuf::wrapBuffer(longSerialized.data(), i));
+    uint32_t j = i;
+    while (j < longSerialized.size()) {
+      q.append(folly::IOBuf::wrapBuffer(&longSerialized[j], 1));
+      ++j;
+    }
+
+    auto pdu_len = folly::bser::decodePduLength(q.front());
+    auto buf = q.split(pdu_len);
+
+    auto hello = folly::bser::parseBser(buf.get());
+    EXPECT_EQ(inputStr, hello.asString());
+  }
+}
+
 /* vim:ts=2:sw=2:et:
  */