update_engine: Skip post DownloadAction exclusions

Post |DownloadAction| don't have direct reference to |Payload|s held
within the |PayloadState|. Hence it's required to halt exclusions for
|Action|s post |DownloadAction|.

This is done by setting the |payload_index_| within |PayloadState| >= to
the |candidate_urls_|/|response_.packages| size.

DCHECKs added where |payload_index_| is used as usage may cause out of
bounds indexing.

This change removes the dangling reference to the last |Payload|, as
previously |NextPayload()| kept |payload_index_| pointing to the last
|Payload| within |PayloadState|.

BUG=chromium:928805
TEST=FEATURES=test emerge-$B update_engine

Change-Id: I3f6a9a3cc26bb84f94506e45e1d6e906624e5dd7
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2261292
Tested-by: Jae Hoon Kim <kimjae@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
Commit-Queue: Amin Hassani <ahassani@chromium.org>
Auto-Submit: Jae Hoon Kim <kimjae@chromium.org>
diff --git a/payload_state_unittest.cc b/payload_state_unittest.cc
index bf9aed4..0bf52d9 100644
--- a/payload_state_unittest.cc
+++ b/payload_state_unittest.cc
@@ -1778,4 +1778,49 @@
   payload_state.IncrementFailureCount();
 }
 
+TEST(PayloadStateTest, HaltExclusionPostPayloadExhaustion) {
+  PayloadState payload_state;
+  FakeSystemState fake_system_state;
+  StrictMock<MockExcluder> mock_excluder;
+  EXPECT_CALL(*fake_system_state.mock_update_attempter(), GetExcluder())
+      .WillOnce(Return(&mock_excluder));
+  EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
+
+  OmahaResponse response;
+  // Non-critical package.
+  response.packages.push_back(
+      {.payload_urls = {"http://test1a", "http://test2a"},
+       .size = 123456789,
+       .metadata_size = 58123,
+       .metadata_signature = "msign",
+       .hash = "hash",
+       .can_exclude = true});
+  payload_state.SetResponse(response);
+
+  // Exclusion should be called when excluded.
+  EXPECT_CALL(mock_excluder, Exclude(utils::GetExclusionName("http://test1a")))
+      .WillOnce(Return(true));
+  payload_state.ExcludeCurrentPayload();
+
+  // No more paylods to go through.
+  EXPECT_FALSE(payload_state.NextPayload());
+
+  // Exclusion should not be called as all |Payload|s are exhausted.
+  payload_state.ExcludeCurrentPayload();
+}
+
+TEST(PayloadStateTest, NonInfinitePayloadIndexIncrement) {
+  PayloadState payload_state;
+  FakeSystemState fake_system_state;
+  EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
+
+  payload_state.SetResponse({});
+
+  EXPECT_FALSE(payload_state.NextPayload());
+  int payload_index = payload_state.payload_index_;
+
+  EXPECT_FALSE(payload_state.NextPayload());
+  EXPECT_EQ(payload_index, payload_state.payload_index_);
+}
+
 }  // namespace chromeos_update_engine