Snap for 4632767 from bd29001fa4d0a34c7239dffb490a648d65637ec2 to pi-release

Change-Id: I3b1e616b25e224d57b808ebdfde1fa9f3c360f0f
diff --git a/payload_consumer/delta_performer.cc b/payload_consumer/delta_performer.cc
index 8aba9bb..c2c43db 100644
--- a/payload_consumer/delta_performer.cc
+++ b/payload_consumer/delta_performer.cc
@@ -1220,26 +1220,26 @@
 class PuffinExtentStream : public puffin::StreamInterface {
  public:
   // Constructor for creating a stream for reading from an |ExtentReader|.
-  PuffinExtentStream(std::unique_ptr<ExtentReader> reader, size_t size)
+  PuffinExtentStream(std::unique_ptr<ExtentReader> reader, uint64_t size)
       : PuffinExtentStream(std::move(reader), nullptr, size) {}
 
   // Constructor for creating a stream for writing to an |ExtentWriter|.
-  PuffinExtentStream(std::unique_ptr<ExtentWriter> writer, size_t size)
+  PuffinExtentStream(std::unique_ptr<ExtentWriter> writer, uint64_t size)
       : PuffinExtentStream(nullptr, std::move(writer), size) {}
 
   ~PuffinExtentStream() override = default;
 
-  bool GetSize(size_t* size) const override {
+  bool GetSize(uint64_t* size) const override {
     *size = size_;
     return true;
   }
 
-  bool GetOffset(size_t* offset) const override {
+  bool GetOffset(uint64_t* offset) const override {
     *offset = offset_;
     return true;
   }
 
-  bool Seek(size_t offset) override {
+  bool Seek(uint64_t offset) override {
     if (is_read_) {
       TEST_AND_RETURN_FALSE(reader_->Seek(offset));
       offset_ = offset;
@@ -1275,7 +1275,7 @@
  private:
   PuffinExtentStream(std::unique_ptr<ExtentReader> reader,
                      std::unique_ptr<ExtentWriter> writer,
-                     size_t size)
+                     uint64_t size)
       : reader_(std::move(reader)),
         writer_(std::move(writer)),
         size_(size),
diff --git a/payload_generator/delta_diff_utils.cc b/payload_generator/delta_diff_utils.cc
index bcbc3a5..877e13f 100644
--- a/payload_generator/delta_diff_utils.cc
+++ b/payload_generator/delta_diff_utils.cc
@@ -40,6 +40,7 @@
 #include <base/threading/simple_thread.h>
 #include <brillo/data_encoding.h>
 #include <bsdiff/bsdiff.h>
+#include <bsdiff/patch_writer_factory.h>
 
 #include "update_engine/common/hash_calculator.h"
 #include "update_engine/common/subprocess.h"
@@ -73,6 +74,8 @@
 // memory intensive, so we limit these operations to 150 MiB.
 const uint64_t kMaxPuffdiffDestinationSize = 150 * 1024 * 1024;  // bytes
 
+const int kBrotliCompressionQuality = 11;
+
 // Process a range of blocks from |range_start| to |range_end| in the extent at
 // position |*idx_p| of |extents|. If |do_remove| is true, this range will be
 // removed, which may cause the extent to be trimmed, split or removed entirely.
@@ -740,21 +743,33 @@
         TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&patch));
         ScopedPathUnlinker unlinker(patch.value());
 
+        std::unique_ptr<bsdiff::PatchWriterInterface> bsdiff_patch_writer;
+        InstallOperation_Type operation_type = InstallOperation::BSDIFF;
+        if (version.OperationAllowed(InstallOperation::BROTLI_BSDIFF)) {
+          bsdiff_patch_writer =
+              bsdiff::CreateBSDF2PatchWriter(patch.value(),
+                                             bsdiff::CompressorType::kBrotli,
+                                             kBrotliCompressionQuality);
+          operation_type = InstallOperation::BROTLI_BSDIFF;
+        } else {
+          bsdiff_patch_writer = bsdiff::CreateBsdiffPatchWriter(patch.value());
+          if (version.OperationAllowed(InstallOperation::SOURCE_BSDIFF)) {
+            operation_type = InstallOperation::SOURCE_BSDIFF;
+          }
+        }
+
         brillo::Blob bsdiff_delta;
         TEST_AND_RETURN_FALSE(0 == bsdiff::bsdiff(old_data.data(),
                                                   old_data.size(),
                                                   new_data.data(),
                                                   new_data.size(),
-                                                  patch.value().c_str(),
+                                                  bsdiff_patch_writer.get(),
                                                   nullptr));
 
         TEST_AND_RETURN_FALSE(utils::ReadFile(patch.value(), &bsdiff_delta));
         CHECK_GT(bsdiff_delta.size(), static_cast<brillo::Blob::size_type>(0));
         if (bsdiff_delta.size() < data_blob.size()) {
-          operation.set_type(
-              version.OperationAllowed(InstallOperation::SOURCE_BSDIFF)
-                  ? InstallOperation::SOURCE_BSDIFF
-                  : InstallOperation::BSDIFF);
+          operation.set_type(operation_type);
           data_blob = std::move(bsdiff_delta);
         }
       }
diff --git a/testrunner.cc b/testrunner.cc
index 934ea91..81d4548 100644
--- a/testrunner.cc
+++ b/testrunner.cc
@@ -16,18 +16,14 @@
 
 // based on pam_google_testrunner.cc
 
-#include <string>
-
 #include <xz.h>
 
 #include <base/at_exit.h>
 #include <base/command_line.h>
-#include <base/environment.h>
 #include <brillo/test_helpers.h>
 #include <gtest/gtest.h>
 
 #include "update_engine/common/terminator.h"
-#include "update_engine/common/test_utils.h"
 #include "update_engine/payload_generator/xz.h"
 
 int main(int argc, char **argv) {
@@ -44,19 +40,12 @@
   // the default exit status of 1.  Corresponding reverts are necessary in
   // terminator_unittest.cc.
   chromeos_update_engine::Terminator::Init(2);
-  // In Android bsdiff is located in update_engine_unittests, add it to PATH.
-#ifdef __ANDROID__
-  std::unique_ptr<base::Environment> env(base::Environment::Create());
-  std::string path_env;
-  CHECK(env->GetVar("PATH", &path_env));
-  path_env +=
-      ":" + chromeos_update_engine::test_utils::GetBuildArtifactsPath().value();
-  CHECK(env->SetVar("PATH", path_env));
-#endif
   LOG(INFO) << "parsing command line arguments";
   base::CommandLine::Init(argc, argv);
   LOG(INFO) << "initializing gtest";
   SetUpTests(&argc, argv, true);
+  // Logging to string is not thread safe.
+  brillo::LogToString(false);
   LOG(INFO) << "running unit tests";
   int test_result = RUN_ALL_TESTS();
   LOG(INFO) << "unittest return value: " << test_result;