rmad: Check HWWP status again in Finalize step

Check HWWP status after disabling cr50 factory mode to make sure it's
really enabled.

BUG=b:187197162
TEST=FEATURES=test emerge-octopus rmad

Change-Id: I3b20af22d18f4e530c64b318d1bd5a83b75ff627
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/3611262
Tested-by: Cheng-Han Yang <chenghan@chromium.org>
Commit-Queue: Cheng-Han Yang <chenghan@chromium.org>
Reviewed-by: Gene Chang <genechang@google.com>
diff --git a/rmad/state_handler/finalize_state_handler.cc b/rmad/state_handler/finalize_state_handler.cc
index 3ff54cd..d0f77c1 100644
--- a/rmad/state_handler/finalize_state_handler.cc
+++ b/rmad/state_handler/finalize_state_handler.cc
@@ -150,13 +150,28 @@
       return;
     }
   }
+
   status_.set_progress(0.5);
+
+  // Disable factory mode if it's still enabled.
   if (!cr50_utils_->DisableFactoryMode()) {
     LOG(ERROR) << "Failed to disable factory mode";
     status_.set_status(FinalizeStatus::RMAD_FINALIZE_STATUS_FAILED_BLOCKING);
     status_.set_error(FinalizeStatus::RMAD_FINALIZE_ERROR_CANNOT_ENABLE_HWWP);
     return;
   }
+
+  status_.set_progress(0.8);
+
+  // Make sure HWWP is disabled.
+  if (int hwwp_status;
+      !crossystem_utils_->GetHwwpStatus(&hwwp_status) || hwwp_status != 1) {
+    LOG(ERROR) << "HWWP is still disabled";
+    status_.set_status(FinalizeStatus::RMAD_FINALIZE_STATUS_FAILED_BLOCKING);
+    status_.set_error(FinalizeStatus::RMAD_FINALIZE_ERROR_CANNOT_ENABLE_HWWP);
+    return;
+  }
+
   // TODO(chenghan): Check cr50 data (e.g. board ID) and GBB flags.
   status_.set_status(FinalizeStatus::RMAD_FINALIZE_STATUS_COMPLETE);
   status_.set_progress(1);
diff --git a/rmad/state_handler/finalize_state_handler_test.cc b/rmad/state_handler/finalize_state_handler_test.cc
index e3f26bf..9cdeae7 100644
--- a/rmad/state_handler/finalize_state_handler_test.cc
+++ b/rmad/state_handler/finalize_state_handler_test.cc
@@ -5,6 +5,7 @@
 #include <memory>
 #include <string>
 #include <utility>
+#include <vector>
 
 #include <base/files/file_util.h>
 #include <base/memory/scoped_refptr.h>
@@ -24,6 +25,7 @@
 using testing::_;
 using testing::DoAll;
 using testing::Eq;
+using testing::InSequence;
 using testing::Invoke;
 using testing::NiceMock;
 using testing::Return;
@@ -44,7 +46,7 @@
   };
 
   scoped_refptr<FinalizeStateHandler> CreateStateHandler(
-      int hwwp_status,
+      const std::vector<int>& wp_status_list,
       bool enable_swwp_success,
       bool disable_factory_mode_success) {
     // Mock |Cr50Utils|.
@@ -53,10 +55,15 @@
         .WillByDefault(Return(disable_factory_mode_success));
     // Mock |CrosSystemUtils|.
     auto mock_crossystem_utils =
-        std::make_unique<NiceMock<MockCrosSystemUtils>>();
-    ON_CALL(*mock_crossystem_utils,
-            GetInt(Eq(CrosSystemUtils::kHwwpStatusProperty), _))
-        .WillByDefault(DoAll(SetArgPointee<1>(hwwp_status), Return(true)));
+        std::make_unique<StrictMock<MockCrosSystemUtils>>();
+    {
+      InSequence seq;
+      for (int i = 0; i < wp_status_list.size(); ++i) {
+        EXPECT_CALL(*mock_crossystem_utils,
+                    GetInt(Eq(CrosSystemUtils::kHwwpStatusProperty), _))
+            .WillOnce(DoAll(SetArgPointee<1>(wp_status_list[i]), Return(true)));
+      }
+    }
     // Mock |FlashromUtils|.
     auto mock_flashrom_utils = std::make_unique<NiceMock<MockFlashromUtils>>();
     ON_CALL(*mock_flashrom_utils, EnableSoftwareWriteProtection())
@@ -82,7 +89,7 @@
 };
 
 TEST_F(FinalizeStateHandlerTest, InitializeState_HwwpDisabled_Success) {
-  auto handler = CreateStateHandler(0, true, true);
+  auto handler = CreateStateHandler({0, 1}, true, true);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
 
   EXPECT_CALL(signal_sender_, SendFinalizeProgressSignal(_))
@@ -97,7 +104,7 @@
 }
 
 TEST_F(FinalizeStateHandlerTest, InitializeState_HwwpEnabled_Success) {
-  auto handler = CreateStateHandler(1, false, true);
+  auto handler = CreateStateHandler({1, 1}, false, true);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
 
   EXPECT_CALL(signal_sender_, SendFinalizeProgressSignal(_))
@@ -112,7 +119,7 @@
 }
 
 TEST_F(FinalizeStateHandlerTest, InitializeState_EnableSwwpFailed) {
-  auto handler = CreateStateHandler(0, false, true);
+  auto handler = CreateStateHandler({0}, false, true);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
 
   EXPECT_CALL(signal_sender_, SendFinalizeProgressSignal(_))
@@ -128,7 +135,7 @@
 }
 
 TEST_F(FinalizeStateHandlerTest, InitializeState_DisableFactoryModeFailed) {
-  auto handler = CreateStateHandler(0, true, false);
+  auto handler = CreateStateHandler({0}, true, false);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
 
   EXPECT_CALL(signal_sender_, SendFinalizeProgressSignal(_))
@@ -143,8 +150,24 @@
   task_environment_.FastForwardBy(FinalizeStateHandler::kReportStatusInterval);
 }
 
+TEST_F(FinalizeStateHandlerTest, InitializeState_HwwpDisabled) {
+  auto handler = CreateStateHandler({0, 0}, true, true);
+  EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
+
+  EXPECT_CALL(signal_sender_, SendFinalizeProgressSignal(_))
+      .WillOnce(Invoke([](const FinalizeStatus& status) {
+        EXPECT_EQ(status.status(),
+                  FinalizeStatus::RMAD_FINALIZE_STATUS_FAILED_BLOCKING);
+        EXPECT_EQ(status.progress(), 0.8);
+        EXPECT_EQ(status.error(),
+                  FinalizeStatus::RMAD_FINALIZE_ERROR_CANNOT_ENABLE_HWWP);
+      }));
+  handler->RunState();
+  task_environment_.FastForwardBy(FinalizeStateHandler::kReportStatusInterval);
+}
+
 TEST_F(FinalizeStateHandlerTest, GetNextStateCase_Success) {
-  auto handler = CreateStateHandler(0, true, true);
+  auto handler = CreateStateHandler({0, 1}, true, true);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
   handler->RunState();
   task_environment_.RunUntilIdle();
@@ -159,7 +182,7 @@
 }
 
 TEST_F(FinalizeStateHandlerTest, GetNextStateCase_InProgress) {
-  auto handler = CreateStateHandler(0, true, true);
+  auto handler = CreateStateHandler({0, 1}, true, true);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
   handler->RunState();
 
@@ -175,7 +198,7 @@
 }
 
 TEST_F(FinalizeStateHandlerTest, GetNextStateCase_MissingState) {
-  auto handler = CreateStateHandler(0, true, true);
+  auto handler = CreateStateHandler({0, 1}, true, true);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
   handler->RunState();
   task_environment_.RunUntilIdle();
@@ -188,7 +211,7 @@
 }
 
 TEST_F(FinalizeStateHandlerTest, GetNextStateCase_MissingArgs) {
-  auto handler = CreateStateHandler(0, true, true);
+  auto handler = CreateStateHandler({0, 1}, true, true);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
   handler->RunState();
   task_environment_.RunUntilIdle();
@@ -203,7 +226,7 @@
 }
 
 TEST_F(FinalizeStateHandlerTest, GetNextStateCase_BlockingFailure_Retry) {
-  auto handler = CreateStateHandler(0, true, false);
+  auto handler = CreateStateHandler({0, 0}, true, false);
   EXPECT_EQ(handler->InitializeState(), RMAD_ERROR_OK);
   handler->RunState();
   task_environment_.RunUntilIdle();