Disable ContentVerifier when FakeCWS is used

Current implementation of FakeCWS doesn't support signed hashes for
extensions' resources. Therefore content verification of extensions
provided by FakeCWS may fail and extensions will be false-positively
marked as corrupted. This commit disables content verification when
FakeCWS is used, as we don't need it in corresponding tests.

Also this commit removes condition when overriding WebStore URLs for
ExtensionsClient, now they are always updated. That condition only added
possible flakiness. In tests ExtensionsClient was initialized after
command line changes, so it used overridden values anyway.

Bug: 1051513
Change-Id: Ie74553f6f778efa0e82f17f64149d688e320f151
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2051931
Commit-Queue: Anatoliy Potapchuk <apotapchuk@chromium.org>
Reviewed-by: Anatoliy Potapchuk <apotapchuk@chromium.org>
Reviewed-by: Roman Sorokin [CET] <rsorokin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#741446}
diff --git a/chrome/browser/chromeos/app_mode/fake_cws.cc b/chrome/browser/chromeos/app_mode/fake_cws.cc
index 3fd5d017..3a1ee4a 100644
--- a/chrome/browser/chromeos/app_mode/fake_cws.cc
+++ b/chrome/browser/chromeos/app_mode/fake_cws.cc
@@ -15,6 +15,7 @@
 #include "base/threading/thread_restrictions.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/chrome_switches.h"
+#include "chrome/common/initialize_extensions_client.h"
 #include "chrome/test/base/in_process_browser_test.h"
 #include "crypto/sha2.h"
 #include "extensions/common/extensions_client.h"
@@ -71,12 +72,32 @@
   return !ids->empty();
 }
 
+// FakeCWS uses ScopedIgnoreContentVerifierForTest to disable extension
+// content verification. This helper could be instantiated only once. Usually
+// that not an issue, since FakeCWS is also instantiated only once, in a base
+// test class. Some tests use a secondary FakeCWS instance. This flag will be
+// set by first created FakeCWS (which we'll call "primary"), and only primary
+// FakeCWS will hold the ScopedIgnoreContentVerifierForTest instance.
+bool g_is_fakecws_active = false;
+
 }  // namespace
 
 FakeCWS::FakeCWS() : update_check_count_(0) {
+  if (!g_is_fakecws_active) {
+    g_is_fakecws_active = true;
+    scoped_ignore_content_verifier_ =
+        std::make_unique<extensions::ScopedIgnoreContentVerifierForTest>();
+  }
 }
 
 FakeCWS::~FakeCWS() {
+  // If the secondary FakeCWS was desructed after primary one, secondary will
+  // work without scoped_ignore_content_verifier_. We want to catch such a
+  // situation, so we check that primary FakeCWS is not destroyed yet.
+  DCHECK(g_is_fakecws_active);
+
+  if (scoped_ignore_content_verifier_)
+    g_is_fakecws_active = false;
 }
 
 void FakeCWS::Init(net::EmbeddedTestServer* embedded_test_server) {
@@ -85,7 +106,7 @@
   update_check_end_point_ = "/update_check.xml";
 
   SetupWebStoreURL(embedded_test_server->base_url());
-  OverrideGalleryCommandlineSwitches(GalleryUpdateMode::kOnlyCommandLine);
+  OverrideGalleryCommandlineSwitches();
   embedded_test_server->RegisterRequestHandler(
       base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
 }
@@ -97,8 +118,7 @@
   update_check_end_point_ = update_check_end_point;
 
   SetupWebStoreURL(embedded_test_server->base_url());
-  OverrideGalleryCommandlineSwitches(
-      GalleryUpdateMode::kModifyExtensionsClient);
+  OverrideGalleryCommandlineSwitches();
 
   embedded_test_server->RegisterRequestHandler(
       base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
@@ -156,8 +176,7 @@
   web_store_url_ = test_server_url.ReplaceComponents(replace_webstore_host);
 }
 
-void FakeCWS::OverrideGalleryCommandlineSwitches(
-    GalleryUpdateMode gallery_update_mode) {
+void FakeCWS::OverrideGalleryCommandlineSwitches() {
   DCHECK(web_store_url_.is_valid());
 
   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
@@ -175,8 +194,8 @@
   command_line->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL,
                                   update_url.spec());
 
-  if (gallery_update_mode == GalleryUpdateMode::kModifyExtensionsClient)
-    extensions::ExtensionsClient::Get()->InitializeWebStoreUrls(command_line);
+  EnsureExtensionsClientInitialized();
+  extensions::ExtensionsClient::Get()->InitializeWebStoreUrls(command_line);
 }
 
 bool FakeCWS::GetUpdateCheckContent(const std::vector<std::string>& ids,
diff --git a/chrome/browser/chromeos/app_mode/fake_cws.h b/chrome/browser/chromeos/app_mode/fake_cws.h
index 8092eca..3c7a50d 100644
--- a/chrome/browser/chromeos/app_mode/fake_cws.h
+++ b/chrome/browser/chromeos/app_mode/fake_cws.h
@@ -6,10 +6,12 @@
 #define CHROME_BROWSER_CHROMEOS_APP_MODE_FAKE_CWS_H_
 
 #include <map>
+#include <memory>
 #include <string>
 #include <vector>
 
 #include "base/macros.h"
+#include "extensions/browser/scoped_ignore_content_verifier_for_test.h"
 #include "net/test/embedded_test_server/embedded_test_server.h"
 #include "net/test/embedded_test_server/http_request.h"
 #include "net/test/embedded_test_server/http_response.h"
@@ -53,8 +55,7 @@
   };
 
   void SetupWebStoreURL(const GURL& test_server_url);
-  void OverrideGalleryCommandlineSwitches(
-      GalleryUpdateMode gallery_update_mode);
+  void OverrideGalleryCommandlineSwitches();
 
   bool GetUpdateCheckContent(const std::vector<std::string>& ids,
                              std::string* update_check_content);
@@ -73,6 +74,18 @@
   std::map<std::string, std::string> id_to_update_check_content_map_;
   int update_check_count_;
 
+  // FakeCWS overrides Chrome Web Store URLs, so extensions it provides in tests
+  // are considered as extensions from Chrome Web Store. ContentVerifier assumes
+  // that Chrome Web Store provides signed hashes for its extensions' resources
+  // (a.k.a. verified_contents.json). FakeCWS currently doesn't provide such
+  // hashes, so content verification fails with MISSING_ALL_HASHES, which is
+  // considered as a corruption. In order to not false-positively detect
+  // corruption in test extensions, disable content verification.
+  // TODO(https://crbug.com/1051560) Make FakeCWS support this feature of the
+  // real Chrome Web Store and remove this scoped ignore.
+  std::unique_ptr<extensions::ScopedIgnoreContentVerifierForTest>
+      scoped_ignore_content_verifier_;
+
   DISALLOW_COPY_AND_ASSIGN(FakeCWS);
 };