Add unittest for filesystem verification action

Test: treehuggre
Change-Id: I03f69b7add96eaa481b1152a1425f4cb669d1113
diff --git a/common/mock_dynamic_partition_control.h b/common/mock_dynamic_partition_control.h
new file mode 100644
index 0000000..3cbf9be
--- /dev/null
+++ b/common/mock_dynamic_partition_control.h
@@ -0,0 +1,84 @@
+//
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include <stdint.h>
+
+#include <memory>
+#include <set>
+#include <string>
+#include <vector>
+
+#include <gmock/gmock.h>
+
+#include "update_engine/common/dynamic_partition_control_interface.h"
+#include "update_engine/payload_consumer/file_descriptor.h"
+
+namespace chromeos_update_engine {
+
+class MockDynamicPartitionControl : public DynamicPartitionControlInterface {
+ public:
+  MOCK_METHOD(void, Cleanup, (), (override));
+  MOCK_METHOD(bool, GetDeviceDir, (std::string*), (override));
+  MOCK_METHOD(FeatureFlag, GetDynamicPartitionsFeatureFlag, (), (override));
+  MOCK_METHOD(FeatureFlag, GetVirtualAbCompressionFeatureFlag, (), (override));
+  MOCK_METHOD(FeatureFlag, GetVirtualAbFeatureFlag, (), (override));
+  MOCK_METHOD(bool, FinishUpdate, (bool), (override));
+  MOCK_METHOD(FileDescriptorPtr,
+              OpenCowReader,
+              (const std::string& unsuffixed_partition_name,
+               const std::optional<std::string>& source_path,
+               bool is_append),
+              (override));
+  MOCK_METHOD(bool, MapAllPartitions, (), (override));
+  MOCK_METHOD(bool, UnmapAllPartitions, (), (override));
+
+  MOCK_METHOD(bool,
+              OptimizeOperation,
+              (const std::string&, const InstallOperation&, InstallOperation*),
+              (override));
+
+  std::unique_ptr<android::snapshot::ISnapshotWriter> OpenCowWriter(
+      const std::string& unsuffixed_partition_name,
+      const std::optional<std::string>&,
+      bool is_append = false) override {
+    return nullptr;
+  }
+
+  MOCK_METHOD(
+      bool,
+      PreparePartitionsForUpdate,
+      (uint32_t, uint32_t, const DeltaArchiveManifest&, bool, uint64_t*),
+      (override));
+
+  MOCK_METHOD(bool, ResetUpdate, (PrefsInterface*), (override));
+  MOCK_METHOD(std::unique_ptr<AbstractAction>,
+              GetCleanupPreviousUpdateAction,
+              (BootControlInterface*,
+               PrefsInterface*,
+               CleanupPreviousUpdateActionDelegateInterface*),
+              (override));
+  MOCK_METHOD(bool,
+              ListDynamicPartitionsForSlot,
+              (uint32_t, std::vector<std::string>*),
+              (override));
+  MOCK_METHOD(bool,
+              VerifyExtentsForUntouchedPartitions,
+              (uint32_t, uint32_t, const std::vector<std::string>&),
+              (override));
+  MOCK_METHOD(bool, IsDynamicPartition, (const std::string&), (override));
+};
+
+}  // namespace chromeos_update_engine
diff --git a/payload_consumer/filesystem_verifier_action_unittest.cc b/payload_consumer/filesystem_verifier_action_unittest.cc
index 925fdab..e9560f0 100644
--- a/payload_consumer/filesystem_verifier_action_unittest.cc
+++ b/payload_consumer/filesystem_verifier_action_unittest.cc
@@ -27,13 +27,22 @@
 #include <brillo/secure_blob.h>
 #include <gtest/gtest.h>
 
+#include "gmock/gmock-actions.h"
 #include "update_engine/common/dynamic_partition_control_stub.h"
 #include "update_engine/common/hash_calculator.h"
+#include "update_engine/common/mock_dynamic_partition_control.h"
 #include "update_engine/common/test_utils.h"
 #include "update_engine/common/utils.h"
+#include "update_engine/payload_consumer/install_plan.h"
 
 using brillo::MessageLoop;
 using std::string;
+using testing::_;
+using testing::AtLeast;
+using testing::DoAll;
+using testing::NiceMock;
+using testing::Return;
+using testing::SetArgPointee;
 
 namespace chromeos_update_engine {
 
@@ -49,6 +58,8 @@
   bool DoTest(bool terminate_early, bool hash_fail);
 
   void BuildActions(const InstallPlan& install_plan);
+  void BuildActions(const InstallPlan& install_plan,
+                    DynamicPartitionControlInterface* dynamic_control);
 
   brillo::FakeMessageLoop loop_{nullptr};
   ActionProcessor processor_;
@@ -188,10 +199,11 @@
 }
 
 void FilesystemVerifierActionTest::BuildActions(
-    const InstallPlan& install_plan) {
+    const InstallPlan& install_plan,
+    DynamicPartitionControlInterface* dynamic_control) {
   auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
   auto verifier_action =
-      std::make_unique<FilesystemVerifierAction>(&dynamic_control_stub_);
+      std::make_unique<FilesystemVerifierAction>(dynamic_control);
   auto collector_action =
       std::make_unique<ObjectCollectorAction<InstallPlan>>();
 
@@ -205,6 +217,11 @@
   processor_.EnqueueAction(std::move(collector_action));
 }
 
+void FilesystemVerifierActionTest::BuildActions(
+    const InstallPlan& install_plan) {
+  BuildActions(install_plan, &dynamic_control_stub_);
+}
+
 class FilesystemVerifierActionTest2Delegate : public ActionProcessorDelegate {
  public:
   void ActionCompleted(ActionProcessor* processor,
@@ -385,4 +402,51 @@
   EXPECT_EQ(ErrorCode::kSuccess, delegate.code());
 }
 
+TEST_F(FilesystemVerifierActionTest, RunWithVABC) {
+  InstallPlan install_plan;
+  InstallPlan::Partition& part = install_plan.partitions.emplace_back();
+  part.name = "fake_part";
+  part.target_path = "/dev/fake_target_path";
+  part.target_size = 4096 * 4096;
+  part.block_size = 4096;
+  part.source_path = "/dev/fake_source_path";
+
+  NiceMock<MockDynamicPartitionControl> dynamic_control;
+
+  ON_CALL(dynamic_control, GetDynamicPartitionsFeatureFlag())
+      .WillByDefault(Return(FeatureFlag(FeatureFlag::Value::LAUNCH)));
+  ON_CALL(dynamic_control, GetVirtualAbCompressionFeatureFlag())
+      .WillByDefault(Return(FeatureFlag(FeatureFlag::Value::LAUNCH)));
+  ON_CALL(dynamic_control, OpenCowReader(_, _, _))
+      .WillByDefault(Return(nullptr));
+  ON_CALL(dynamic_control, IsDynamicPartition(part.name))
+      .WillByDefault(Return(true));
+
+  EXPECT_CALL(dynamic_control, GetVirtualAbCompressionFeatureFlag())
+      .Times(AtLeast(1));
+  EXPECT_CALL(dynamic_control, OpenCowReader(part.name, {part.source_path}, _))
+      .Times(1);
+  EXPECT_CALL(dynamic_control, ListDynamicPartitionsForSlot(_, _))
+      .WillRepeatedly(
+          DoAll(SetArgPointee<1, std::vector<std::string>>({part.name}),
+                Return(true)));
+
+  BuildActions(install_plan, &dynamic_control);
+
+  FilesystemVerifierActionTestDelegate delegate;
+  processor_.set_delegate(&delegate);
+
+  loop_.PostTask(
+      FROM_HERE,
+      base::Bind(
+          [](ActionProcessor* processor) { processor->StartProcessing(); },
+          base::Unretained(&processor_)));
+  loop_.Run();
+
+  EXPECT_FALSE(processor_.IsRunning());
+  EXPECT_TRUE(delegate.ran());
+  // Filesystem verifier will fail, because we returned nullptr as CowReader
+  EXPECT_EQ(ErrorCode::kFilesystemVerifierError, delegate.code());
+}
+
 }  // namespace chromeos_update_engine