updater: Add ABORT command.

This will be used for testing purpose only, replacing the previously
used "fail", to intentionally abort an update.

As we're separating the logic between commands parsing and execution,
"abort" needs to be considered as a valid command during the parsing.

Test: recovery_unit_test and recovery_component_test on marlin.
Change-Id: I47c41c423e62c41cc8515fd92f3c5959be08da02
diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp
index 937c5e1..fc71385 100644
--- a/updater/blockimg.cpp
+++ b/updater/blockimg.cpp
@@ -1489,6 +1489,11 @@
   return 0;
 }
 
+static int PerformCommandAbort(CommandParameters&) {
+  LOG(INFO) << "Aborting as instructed";
+  return -1;
+}
+
 using CommandFunction = std::function<int(CommandParameters&)>;
 
 using CommandMap = std::unordered_map<Command::Type, CommandFunction>;
@@ -1888,6 +1893,7 @@
   // Commands which are not allowed are set to nullptr to skip them completely.
   const CommandMap command_map{
     // clang-format off
+    { Command::Type::ABORT,   PerformCommandAbort },
     { Command::Type::BSDIFF,  PerformCommandDiff },
     { Command::Type::ERASE,   nullptr },
     { Command::Type::FREE,    PerformCommandFree },
@@ -1908,6 +1914,7 @@
                           const std::vector<std::unique_ptr<Expr>>& argv) {
   const CommandMap command_map{
     // clang-format off
+    { Command::Type::ABORT,   PerformCommandAbort },
     { Command::Type::BSDIFF,  PerformCommandDiff },
     { Command::Type::ERASE,   PerformCommandErase },
     { Command::Type::FREE,    PerformCommandFree },
diff --git a/updater/commands.cpp b/updater/commands.cpp
index fb19ebc..e881496 100644
--- a/updater/commands.cpp
+++ b/updater/commands.cpp
@@ -29,8 +29,16 @@
 
 using namespace std::string_literals;
 
+bool Command::abort_allowed_ = false;
+
 Command::Type Command::ParseType(const std::string& type_str) {
-  if (type_str == "bsdiff") {
+  if (type_str == "abort") {
+    if (!abort_allowed_) {
+      LOG(ERROR) << "ABORT disallowed";
+      return Type::LAST;
+    }
+    return Type::ABORT;
+  } else if (type_str == "bsdiff") {
     return Type::BSDIFF;
   } else if (type_str == "erase") {
     return Type::ERASE;
@@ -237,6 +245,13 @@
             src_hash, &source_info, err)) {
       return {};
     }
+  } else if (op == Type::ABORT) {
+    // No-op, other than sanity checking the input args.
+    if (pos != tokens.size()) {
+      *err = android::base::StringPrintf("invalid number of args: %zu (expected 0)",
+                                         tokens.size() - pos);
+      return {};
+    }
   } else {
     *err = "invalid op";
     return {};
diff --git a/updater/include/private/commands.h b/updater/include/private/commands.h
index 784892f..087d7cf 100644
--- a/updater/include/private/commands.h
+++ b/updater/include/private/commands.h
@@ -213,9 +213,13 @@
 //      - Free the given stash data.
 //      - Meaningful args: StashInfo
 //
+//    abort
+//      - Abort the current update. Allowed for testing code only.
+//
 class Command {
  public:
   enum class Type {
+    ABORT,
     BSDIFF,
     ERASE,
     FREE,
@@ -280,6 +284,11 @@
   }
 
  private:
+  friend class ResumableUpdaterTest;
+  friend class UpdaterTest;
+
+  FRIEND_TEST(CommandsTest, Parse_ABORT_Allowed);
+  FRIEND_TEST(CommandsTest, Parse_InvalidNumberOfArgs);
   FRIEND_TEST(CommandsTest, ParseTargetInfoAndSourceInfo_InvalidInput);
   FRIEND_TEST(CommandsTest, ParseTargetInfoAndSourceInfo_StashesOnly);
   FRIEND_TEST(CommandsTest, ParseTargetInfoAndSourceInfo_SourceBlocksAndStashes);
@@ -293,6 +302,9 @@
                                            const std::string& src_hash, SourceInfo* source,
                                            std::string* err);
 
+  // Allows parsing ABORT command, which should be used for testing purpose only.
+  static bool abort_allowed_;
+
   // The type of the command.
   Type type_{ Type::LAST };
   // The index of the Command object, which is specified by the caller.