Пример #1
0
// Test deserializing from an unmanaged IOBuf, which doesn't control the
// lifetime of the underlying data
TEST(GitBlob, testDeserializeUnmanaged) {
  string blobHash("3a8f8eb91101860fd8484154885838bf322964d0");
  Hash hash(blobHash);

  string contents("{\n  \"breakConfig\": true\n}\n");
  auto gitBlobObjectStr = folly::to<string>(string("blob 26\x00", 8), contents);
  folly::ByteRange gitBlobObject = folly::StringPiece{gitBlobObjectStr};
  EXPECT_EQ(blobHash, Hash::sha1(gitBlobObject).toString())
      << "SHA-1 of contents should match key";

  IOBuf buf(IOBuf::WRAP_BUFFER, gitBlobObject);
  auto blob = deserializeGitBlob(hash, &buf);
  EXPECT_EQ(hash, blob->getHash());
  EXPECT_FALSE(blob->getContents().isShared())
      << "deserializeGitBlob() must make a copy of the buffer given "
      << "an unmanaged IOBuf as input";
  EXPECT_EQ(contents, StringPiece{blob->getContents().clone()->coalesce()});
  // Make sure the blob contents are still valid after freeing our string data.
  {
    std::string empty;
    gitBlobObjectStr.swap(empty);
    buf = IOBuf();
  }
  EXPECT_EQ(contents, StringPiece{blob->getContents().clone()->coalesce()});
}