update_engine: NextPayload() resets URL index

This change fixes a bug/assumption that |PayloadState| used to make in
regards to URL index related to payloads. When a URL index is
incremented, there is no gurauntee that subsequent payloads will have
the same number of candidate URLs, hence it is critical to reset the URL
index back to 0 for subsequent payloads. This fix also allows candidate
URLs to not be skipped over for multi-package/payload request/responses.
The max number of times a URL is allowed to fail is reduced from 10 to 3
to allow preferred URLs to always be used as the intial URL for payloads.

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

Change-Id: I67732b2b7da08f580d1b554fd85eb06b3bf1f761
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2188552
Tested-by: Jae Hoon Kim <kimjae@chromium.org>
Commit-Queue: Andrew Lassalle <andrewlassalle@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
diff --git a/payload_state_unittest.cc b/payload_state_unittest.cc
index 869c24e..4a0afcf 100644
--- a/payload_state_unittest.cc
+++ b/payload_state_unittest.cc
@@ -1655,4 +1655,31 @@
   EXPECT_EQ(null_time, payload_state.GetP2PFirstAttemptTimestamp());
 }
 
+TEST(PayloadStateTest, NextPayloadResetsUrlIndex) {
+  PayloadState payload_state;
+  FakeSystemState fake_system_state;
+  EXPECT_TRUE(payload_state.Initialize(&fake_system_state));
+
+  OmahaResponse response;
+  response.packages.push_back(
+      {.payload_urls = {"http://test1a", "http://test2a"},
+       .size = 123456789,
+       .metadata_size = 58123,
+       .metadata_signature = "msign",
+       .hash = "hash"});
+  response.packages.push_back({.payload_urls = {"http://test1b"},
+                               .size = 123456789,
+                               .metadata_size = 58123,
+                               .metadata_signature = "msign",
+                               .hash = "hash"});
+  payload_state.SetResponse(response);
+
+  EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1a");
+  payload_state.IncrementUrlIndex();
+  EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test2a");
+
+  EXPECT_TRUE(payload_state.NextPayload());
+  EXPECT_EQ(payload_state.GetCurrentUrl(), "http://test1b");
+}
+
 }  // namespace chromeos_update_engine