Commit 88cb499a authored by Omar Morsi's avatar Omar Morsi Committed by Chromium LUCI CQ

Use ExtensionForceInstallMixin in enterprise.platformKeys API tests

After CL:2566756, enterprise.platformKeys API tests are ready to use
mixins. Using ExtensionForceInstallMixin will make it easier and cleaner
when extending enterprise.platformKeys API tests.

Bug: 1148294
Change-Id: I4ffa1b0cc8422695ad75fd5057020269f561447c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2569510
Commit-Queue: Omar Morsi <omorsi@google.com>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarAlexander Hendrich <hendrich@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833746}
parent 04101b1c
...@@ -10,16 +10,16 @@ ...@@ -10,16 +10,16 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/platform_keys/platform_keys_test_base.h" #include "chrome/browser/extensions/api/platform_keys/platform_keys_test_base.h"
#include "chrome/browser/extensions/policy_test_utils.h"
#include "chrome/browser/net/nss_context.h" #include "chrome/browser/net/nss_context.h"
#include "chrome/browser/policy/extension_force_install_mixin.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h" #include "content/public/common/content_switches.h"
...@@ -27,11 +27,14 @@ ...@@ -27,11 +27,14 @@
#include "crypto/nss_util_internal.h" #include "crypto/nss_util_internal.h"
#include "crypto/scoped_nss_types.h" #include "crypto/scoped_nss_types.h"
#include "crypto/scoped_test_system_nss_key_slot.h" #include "crypto/scoped_test_system_nss_key_slot.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_event_histogram_value.h"
#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_registry.h"
#include "extensions/common/api/test.h"
#include "extensions/common/extension_id.h"
#include "extensions/test/extension_test_message_listener.h" #include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h" #include "extensions/test/result_catcher.h"
#include "net/cert/nss_cert_database.h" #include "net/cert/nss_cert_database.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -39,16 +42,16 @@ namespace extensions { ...@@ -39,16 +42,16 @@ namespace extensions {
namespace { namespace {
// The test extension will query for the state of the system token. // This message sent from a browsertest to the background script to test the API
constexpr char kWaitingForSystemTokenStateMessage[] = // behavior for an extension running in a user session with system token
"Waiting for system token state message"; // enabled.
constexpr char kUserSessionWithSystemTokenEnabledMode[] =
// The message sent from a browsertest to the background script in case the "User session with system token enabled mode.";
// system token is enabled. // This message sent from a browsertest to the background script to test the API
constexpr char kSystemTokenEnabledMessage[] = "System token enabled."; // behavior for an extension running in a user session with system token
// The message sent from a browsertest to the background script in case the // disabled.
// system token is disabled. constexpr char kUserSessionWithSystemTokenDisabledMode[] =
constexpr char kSystemTokenDisabledMessage[] = "System token disabled."; "User session with system token disabled mode.";
// The test extension has a certificate referencing this private key which will // The test extension has a certificate referencing this private key which will
// be stored in the user's token in the test setup. // be stored in the user's token in the test setup.
...@@ -121,6 +124,18 @@ const unsigned char privateKeyPkcs8System[] = { ...@@ -121,6 +124,18 @@ const unsigned char privateKeyPkcs8System[] = {
0xd8, 0x71, 0x69, 0x5e, 0x8d, 0xb4, 0x48, 0x1c, 0xa4, 0x01, 0xce, 0xc1, 0xd8, 0x71, 0x69, 0x5e, 0x8d, 0xb4, 0x48, 0x1c, 0xa4, 0x01, 0xce, 0xc1,
0xb5, 0x6f, 0xe9, 0x1b, 0x32, 0x91, 0x34, 0x38}; 0xb5, 0x6f, 0xe9, 0x1b, 0x32, 0x91, 0x34, 0x38};
base::FilePath GetExtensionDirName() {
return base::PathService::CheckedGet(chrome::DIR_TEST_DATA)
.Append(
FILE_PATH_LITERAL("extensions/api_test/enterprise_platform_keys/"));
}
base::FilePath GetExtensionPemFileName() {
return base::PathService::CheckedGet(chrome::DIR_TEST_DATA)
.Append(FILE_PATH_LITERAL(
"extensions/api_test/enterprise_platform_keys.pem"));
}
void ImportPrivateKeyPKCS8ToSlot(const unsigned char* pkcs8_der, void ImportPrivateKeyPKCS8ToSlot(const unsigned char* pkcs8_der,
size_t pkcs8_der_size, size_t pkcs8_der_size,
PK11SlotInfo* slot) { PK11SlotInfo* slot) {
...@@ -142,18 +157,6 @@ void ImportPrivateKeyPKCS8ToSlot(const unsigned char* pkcs8_der, ...@@ -142,18 +157,6 @@ void ImportPrivateKeyPKCS8ToSlot(const unsigned char* pkcs8_der,
crypto::ScopedSECKEYPrivateKey seckey(seckey_raw); crypto::ScopedSECKEYPrivateKey seckey(seckey_raw);
} }
// The managed_storage extension has a key defined in its manifest, so that
// its extension ID is well-known and the policy system can push policies for
// the extension.
const char kTestExtensionID[] = "aecpbnckhoppanpmefllkdkohionpmig";
const char kTestExtensionUpdateManifest[] =
R"(<?xml version='1.0' encoding='UTF-8'?>
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='$1'>
<updatecheck codebase='$2' version='0.1' />
</app>
</gupdate>)";
struct Params { struct Params {
Params(PlatformKeysTestBase::SystemTokenStatus system_token_status, Params(PlatformKeysTestBase::SystemTokenStatus system_token_status,
PlatformKeysTestBase::EnrollmentStatus enrollment_status, PlatformKeysTestBase::EnrollmentStatus enrollment_status,
...@@ -185,24 +188,10 @@ class EnterprisePlatformKeysTest ...@@ -185,24 +188,10 @@ class EnterprisePlatformKeysTest
} }
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
embedded_test_server()->ServeFilesFromDirectory(temp_dir_.GetPath());
crx_path_ = temp_dir_.GetPath().Append(kCrxFileName);
update_manifest_path_ = temp_dir_.GetPath().Append(kUpdateManifestFileName);
extension_path_ = test_data_dir_.Append(kExtensionDirName);
pem_path_ = test_data_dir_.Append(kPemFileName);
base::FilePath created_crx_path =
PackExtensionWithOptions(extension_path_, crx_path_, pem_path_,
/*pem_out_path=*/base::FilePath());
ASSERT_EQ(created_crx_path, crx_path_);
GenerateUpdateManifestFile();
PlatformKeysTestBase::SetUpOnMainThread(); PlatformKeysTestBase::SetUpOnMainThread();
extension_force_install_mixin_.InitWithMockPolicyProvider(
profile(), mock_policy_provider());
} }
void DidGetCertDatabase(const base::Closure& done_callback, void DidGetCertDatabase(const base::Closure& done_callback,
...@@ -216,18 +205,20 @@ class EnterprisePlatformKeysTest ...@@ -216,18 +205,20 @@ class EnterprisePlatformKeysTest
} }
protected: protected:
const std::string kUpdateManifestFileName = std::string GetTestMode() {
"enterprise_platform_keys_update_manifest.xml"; // Only if the system token exists, and the current user is of the same
// domain as the device is enrolled to, the system token is available to the
void SetUpTestListeners() { // extension.
catcher_ = std::make_unique<extensions::ResultCatcher>(); if (system_token_status() == SystemTokenStatus::EXISTS &&
listener_ = std::make_unique<ExtensionTestMessageListener>( enrollment_status() == EnrollmentStatus::ENROLLED &&
kWaitingForSystemTokenStateMessage, user_status() == UserStatus::MANAGED_AFFILIATED_DOMAIN) {
/*will_reply=*/true); return kUserSessionWithSystemTokenEnabledMode;
}
return kUserSessionWithSystemTokenDisabledMode;
} }
std::unique_ptr<extensions::ResultCatcher> catcher_; ExtensionForceInstallMixin extension_force_install_mixin_{&mixin_host_};
std::unique_ptr<ExtensionTestMessageListener> listener_;
private: private:
void PrepareTestSystemSlotOnIO( void PrepareTestSystemSlotOnIO(
...@@ -239,29 +230,6 @@ class EnterprisePlatformKeysTest ...@@ -239,29 +230,6 @@ class EnterprisePlatformKeysTest
system_slot->slot()); system_slot->slot());
} }
void GenerateUpdateManifestFile() {
const std::string kContent = base::ReplaceStringPlaceholders(
kTestExtensionUpdateManifest,
{kTestExtensionID,
embedded_test_server()->GetURL("/" + kCrxFileName).spec().c_str()},
/*offsets=*/nullptr);
int written_bytes = base::WriteFile(update_manifest_path_, kContent.data(),
kContent.size());
ASSERT_EQ(written_bytes, static_cast<int>(kContent.length()));
}
const std::string kCrxFileName = "enterprise_platform_keys.crx";
const std::string kExtensionDirName = "enterprise_platform_keys";
const std::string kPemFileName = "enterprise_platform_keys.pem";
base::FilePath crx_path_;
base::FilePath extension_path_;
base::FilePath pem_path_;
base::FilePath update_manifest_path_;
base::ScopedTempDir temp_dir_;
DISALLOW_COPY_AND_ASSIGN(EnterprisePlatformKeysTest); DISALLOW_COPY_AND_ASSIGN(EnterprisePlatformKeysTest);
}; };
...@@ -280,26 +248,25 @@ IN_PROC_BROWSER_TEST_P(EnterprisePlatformKeysTest, Basic) { ...@@ -280,26 +248,25 @@ IN_PROC_BROWSER_TEST_P(EnterprisePlatformKeysTest, Basic) {
base::Unretained(this), loop.QuitClosure())); base::Unretained(this), loop.QuitClosure()));
loop.Run(); loop.Run();
} }
policy_test_utils::SetExtensionInstallForcelistPolicy(
kTestExtensionID,
embedded_test_server()->GetURL("/" + kUpdateManifestFileName), profile(),
mock_policy_provider());
SetUpTestListeners();
ASSERT_TRUE(listener_->WaitUntilSatisfied());
// Only if the system token exists, and the current user is of the same domain
// as the device is enrolled to, the system token is available to the
// extension.
if (system_token_status() == SystemTokenStatus::EXISTS &&
enrollment_status() == EnrollmentStatus::ENROLLED &&
user_status() == UserStatus::MANAGED_AFFILIATED_DOMAIN) {
listener_->Reply(kSystemTokenEnabledMessage);
} else {
listener_->Reply(kSystemTokenDisabledMessage);
}
ASSERT_TRUE(catcher_->GetNextResult()); extensions::ExtensionId extension_id;
ASSERT_TRUE(extension_force_install_mixin_.ForceInstallFromSourceDir(
GetExtensionDirName(), GetExtensionPemFileName(),
ExtensionForceInstallMixin::WaitMode::kBackgroundPageFirstLoad,
&extension_id));
api::test::OnMessage::Info info;
info.data = GetTestMode();
auto event = std::make_unique<extensions::Event>(
extensions::events::FOR_TEST,
extensions::api::test::OnMessage::kEventName,
api::test::OnMessage::Create(info), profile());
extensions::EventRouter::Get(profile())->DispatchEventToExtension(
extension_id, std::move(event));
extensions::ResultCatcher catcher;
ASSERT_TRUE(catcher.GetNextResult());
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
......
...@@ -4,12 +4,17 @@ ...@@ -4,12 +4,17 @@
'use strict'; 'use strict';
// The message sent from a browsertest to the background script in case the // TODO(crbug.com/1148294): Add LOGIN_SCREEN_MODE.
// system token is enabled. // This message sent from a browsertest to the background script to test the API
const SYSTEM_TOKEN_ENABLED_MESSAGE = 'System token enabled.'; // behavior for an extension running in a user session with system token
// The message sent from a browsertest to the background script in case the // enabled.
// system token is disabled. const USER_SESSION_WITH_SYSTEM_TOKEN_ENABLED_MODE =
const SYSTEM_TOKEN_DISABLED_MESSAGE = 'System token disabled.'; 'User session with system token enabled mode.';
// This message sent from a browsertest to the background script to test the API
// behavior for an extension running in a user session with system token
// disabled.
const USER_SESSION_WITH_SYSTEM_TOKEN_DISABLED_MODE =
'User session with system token disabled mode.';
var assertEq = chrome.test.assertEq; var assertEq = chrome.test.assertEq;
var assertTrue = chrome.test.assertTrue; var assertTrue = chrome.test.assertTrue;
...@@ -881,20 +886,17 @@ function runTests(userToken, systemToken) { ...@@ -881,20 +886,17 @@ function runTests(userToken, systemToken) {
chrome.test.runTests(testsIndependentOfKeys.concat(testsNotParameterized)); chrome.test.runTests(testsIndependentOfKeys.concat(testsNotParameterized));
} }
// |waitForSystemTokenStateMessage()| waits for the browser test to send a // This function is executed when the C++ side of the test sends the test mode.
// message with the state of the system token to run tests accordingly. The // The browser test logic can be found at:
// browser test logic can be found at:
// c/b/e/api/enterprise_platform_keys/enterprise_platform_keys_apitest_nss.cc // c/b/e/api/enterprise_platform_keys/enterprise_platform_keys_apitest_nss.cc
function waitForSystemTokenStateMessage(systemTokenStateMessage) { function testModeListener(message) {
if (systemTokenStateMessage == SYSTEM_TOKEN_ENABLED_MESSAGE) { if (message.data === USER_SESSION_WITH_SYSTEM_TOKEN_ENABLED_MODE) {
beforeTests(/*systemTokenEnabled=*/ true, runTests); beforeTests(/*systemTokenEnabled=*/ true, runTests);
} else if (systemTokenStateMessage == SYSTEM_TOKEN_DISABLED_MESSAGE) { } else if (message.data === USER_SESSION_WITH_SYSTEM_TOKEN_DISABLED_MODE) {
beforeTests(/*systemTokenEnabled=*/ false, runTests); beforeTests(/*systemTokenEnabled=*/ false, runTests);
} else { } else {
// No background script tests should run. fail();
succeed();
} }
} }
chrome.test.sendMessage( chrome.test.onMessage.addListener(testModeListener);
'Waiting for system token state message', waitForSystemTokenStateMessage);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment