Commit 6a4029fe authored by Rohit Rao's avatar Rohit Rao Committed by Commit Bot

[ios] Adds EG2 tests for enterprise policy.

Adds tests to ensure that about:policy loads properly and that policies
are parsed properly out of NSUserDefaults.

Adds EG2 helpers to retrieve policy settings from the current platform
policy provider.

BUG=1024115,1027249

Change-Id: I9835d106c873efa020293bfff4d59f0feb47abe3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2088532
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarMike Dougherty <michaeldo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749184}
parent 3f5e4bb8
......@@ -28,6 +28,9 @@ bool DisableSigninRecallPromo() {
bool DisableUpdateService() {
return false;
}
policy::ConfigurationPolicyProvider* GetOverriddenPlatformPolicyProvider() {
return nullptr;
}
void SetUpTestsIfPresent() {}
void RunTestsIfPresent() {}
......
......@@ -5,6 +5,10 @@
#ifndef IOS_CHROME_APP_TESTS_HOOK_H_
#define IOS_CHROME_APP_TESTS_HOOK_H_
namespace policy {
class ConfigurationPolicyProvider;
}
namespace tests_hook {
// Returns true if app group access should be disabled as tests don't have the
......@@ -32,6 +36,10 @@ bool DisableSigninRecallPromo();
// infobar won't be shown during testing.
bool DisableUpdateService();
// Returns a policy provider that should be installed as the platform policy
// provider when testing. May return nullptr.
policy::ConfigurationPolicyProvider* GetOverriddenPlatformPolicyProvider();
// Global integration tests setup. This is not used by EarlGrey-based
// integration tests.
void SetUpTestsIfPresent();
......
......@@ -214,6 +214,7 @@ source_set("browser_impl") {
"//components/variations/field_trial_config",
"//components/variations/service",
"//components/version_info",
"//ios/chrome/app:tests_hook",
"//ios/chrome/browser/browser_state",
"//ios/chrome/browser/browser_state:browser_state_impl",
"//ios/chrome/browser/component_updater",
......
......@@ -40,6 +40,7 @@
#include "components/update_client/configurator.h"
#include "components/update_client/update_query_params.h"
#include "components/variations/service/variations_service.h"
#include "ios/chrome/app/tests_hook.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state_manager_impl.h"
......@@ -423,6 +424,15 @@ BrowserPolicyConnectorIOS* ApplicationContextImpl::GetBrowserPolicyConnector() {
DCHECK(ui::ResourceBundle::HasSharedInstance());
browser_policy_connector_ = std::make_unique<BrowserPolicyConnectorIOS>(
base::Bind(&BuildPolicyHandlerList));
// Install a mock platform policy provider, if running under EG2 and one
// is supplied.
policy::ConfigurationPolicyProvider* test_policy_provider =
tests_hook::GetOverriddenPlatformPolicyProvider();
if (test_policy_provider) {
browser_policy_connector_->SetPolicyProviderForTesting(
test_policy_provider);
}
}
}
return browser_policy_connector_.get();
......
......@@ -46,3 +46,61 @@ source_set("feature_flags") {
"//ios/chrome/browser:utils",
]
}
source_set("test_support") {
testonly = true
sources = [
"test_platform_policy_provider.cc",
"test_platform_policy_provider.h",
]
deps = [ "//base" ]
public_deps = [ "//components/policy/core/common:test_support" ]
configs += [ "//build/config/compiler:enable_arc" ]
}
source_set("eg2_tests") {
defines = [ "CHROME_EARL_GREY_2" ]
configs += [
"//build/config/compiler:enable_arc",
"//build/config/ios:xctest_config",
]
testonly = true
sources = [
"policy_egtest.mm",
"policy_egtest_app_interface.h",
]
deps = [
"//base",
"//components/strings",
"//ios/chrome/browser:utils",
"//ios/chrome/test/earl_grey:eg_test_support+eg2",
"//ios/testing/earl_grey:eg_test_support+eg2",
"//ios/third_party/earl_grey2:test_lib",
"//ui/base",
]
libs = [ "UIKit.framework" ]
}
source_set("eg_app_support+eg2") {
defines = [ "CHROME_EARL_GREY_2" ]
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"policy_egtest_app_interface.h",
"policy_egtest_app_interface.mm",
]
deps = [
":policy",
"//base",
"//components/policy/core/browser",
"//components/policy/core/common",
"//ios/chrome/browser",
]
libs = [ "Foundation.framework" ]
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/testing/earl_grey/earl_grey_test.h"
#include <memory>
#include "base/json/json_string_value_serializer.h"
#include "base/strings/sys_string_conversions.h"
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/chrome_switches.h"
#import "ios/chrome/browser/policy/policy_egtest_app_interface.h"
#include "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#include "ios/chrome/test/earl_grey/chrome_test_case.h"
#include "ios/testing/earl_grey/app_launch_configuration.h"
#include "ios/testing/earl_grey/app_launch_manager.h"
#include "ui/base/l10n/l10n_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
GREY_STUB_CLASS_IN_APP_MAIN_QUEUE(PolicyEGTestAppInterface)
namespace {
// Returns the value of a given policy, looked up in the current platform policy
// provider.
std::unique_ptr<base::Value> GetPlatformPolicy(const std::string& key) {
std::string json_representation =
base::SysNSStringToUTF8([PolicyEGTestAppInterface
valueForPlatformPolicy:base::SysUTF8ToNSString(key)]);
JSONStringValueDeserializer deserializer(json_representation);
return deserializer.Deserialize(/*error_code=*/nullptr,
/*error_message=*/nullptr);
}
} // namespace
// Test case to verify that enterprise policies are set and respected.
@interface PolicyTestCase : ChromeTestCase
@end
@implementation PolicyTestCase
- (AppLaunchConfiguration)appConfigurationForTestCase {
// Use commandline args to insert fake policy data into NSUserDefaults. To the
// app, this policy data will appear under the
// "com.apple.configuration.managed" key.
AppLaunchConfiguration config;
config.additional_args.push_back(std::string("--") +
switches::kEnableEnterprisePolicy);
config.relaunch_policy = NoForceRelaunchAndResetState;
return config;
}
// Tests that about:policy is available.
- (void)testAboutPolicy {
[ChromeEarlGrey loadURL:GURL("chrome://policy")];
[ChromeEarlGrey waitForWebStateContainingText:l10n_util::GetStringUTF8(
IDS_POLICY_SHOW_UNSET)];
}
@end
// Test case that uses the production platform policy provider.
@interface PolicyPlatformProviderTestCase : ChromeTestCase
@end
@implementation PolicyPlatformProviderTestCase
- (AppLaunchConfiguration)appConfigurationForTestCase {
AppLaunchConfiguration config;
config.additional_args.push_back(std::string("--") +
switches::kEnableEnterprisePolicy);
// Commandline flags that start with a single "-" are automatically added to
// the NSArgumentDomain in NSUserDefaults. Set fake policy data that can be
// read by the production platform policy provider.
config.additional_args.push_back("-com.apple.configuration.managed");
config.additional_args.push_back("{ChromePolicy={"
"DefaultSearchProviderName=Test;"
"NotARegisteredPolicy=Unknown;"
"};}");
config.relaunch_policy = NoForceRelaunchAndResetState;
return config;
}
// Tests that policies are properly loaded from NSUserDefaults when using the
// production platform policy provider.
// Tests the value of a policy that was explicitly set.
- (void)testProductionPlatformProviderPolicyExplicitlySet {
std::unique_ptr<base::Value> searchValue =
GetPlatformPolicy("DefaultSearchProviderName");
GREYAssertTrue(searchValue && searchValue->is_string(),
@"searchSuggestValue was not of type string");
GREYAssertEqual(searchValue->GetString(), "Test",
@"searchSuggestValue had an unexpected value");
}
// Test the value of a policy that exists in the schema but was not explicitly
// set.
- (void)testProductionPlatformProviderPolicyNotSet {
std::unique_ptr<base::Value> blocklistValue =
GetPlatformPolicy("URLBlacklist");
GREYAssertTrue(blocklistValue && blocklistValue->is_none(),
@"blocklistValue was unexpectedly present");
}
// Test the value of a policy that was set in the configuration but is unknown
// to the policy system.
- (void)testProductionPlatformProviderPolicyUnknown {
std::unique_ptr<base::Value> unknownValue =
GetPlatformPolicy("NotARegisteredPolicy");
GREYAssertTrue(unknownValue && unknownValue->is_string(),
@"unknownValue was not of type string");
GREYAssertEqual(unknownValue->GetString(), "Unknown",
@"unknownValue had an unexpected value");
}
@end
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_POLICY_POLICY_EGTEST_APP_INTERFACE_H_
#define IOS_CHROME_BROWSER_POLICY_POLICY_EGTEST_APP_INTERFACE_H_
#import <Foundation/Foundation.h>
@interface PolicyEGTestAppInterface : NSObject
// Returns a JSON-encoded representation of the value for the given |policyKey|.
// Looks for the policy in the platform policy provider under the CHROME policy
// namespace.
+ (NSString*)valueForPlatformPolicy:(NSString*)policyKey;
@end
#endif // IOS_CHROME_BROWSER_POLICY_POLICY_EGTEST_APP_INTERFACE_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/policy/policy_egtest_app_interface.h"
#include "base/json/json_string_value_serializer.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/configuration_policy_provider.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_namespace.h"
#include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/policy/browser_policy_connector_ios.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// Returns a JSON-encoded string representing the given |pref|. If |pref| is
// nullptr, returns a string representing a base::Value of type NONE.
NSString* SerializedValue(const base::Value* value) {
base::Value none_value(base::Value::Type::NONE);
if (!value) {
value = &none_value;
}
DCHECK(value);
std::string serialized_value;
JSONStringValueSerializer serializer(&serialized_value);
serializer.Serialize(*value);
return base::SysUTF8ToNSString(serialized_value);
}
}
@implementation PolicyEGTestAppInterface
+ (NSString*)valueForPlatformPolicy:(NSString*)policyKey {
const std::string key = base::SysNSStringToUTF8(policyKey);
BrowserPolicyConnectorIOS* connector =
GetApplicationContext()->GetBrowserPolicyConnector();
if (!connector) {
return SerializedValue(nullptr);
}
const policy::ConfigurationPolicyProvider* platformProvider =
connector->GetPlatformProvider();
if (!platformProvider) {
return SerializedValue(nullptr);
}
const policy::PolicyBundle& policyBundle = platformProvider->policies();
const policy::PolicyMap& policyMap = policyBundle.Get(
policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, ""));
return SerializedValue(policyMap.GetValue(key));
}
@end
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/policy/test_platform_policy_provider.h"
#include "base/no_destructor.h"
policy::MockConfigurationPolicyProvider* GetTestPlatformPolicyProvider() {
static base::NoDestructor<policy::MockConfigurationPolicyProvider> provider;
provider->SetAutoRefresh();
return provider.get();
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_POLICY_TEST_PLATFORM_POLICY_PROVIDER_H_
#define IOS_CHROME_BROWSER_POLICY_TEST_PLATFORM_POLICY_PROVIDER_H_
#include "components/policy/core/common/mock_configuration_policy_provider.h"
// Returns a singleton mock that can be installed as the platform policy
// provider when testing. Subsequent calls to this method will return the same
// object, which can then be used to update the current set of policies.
policy::MockConfigurationPolicyProvider* GetTestPlatformPolicyProvider();
#endif // IOS_CHROME_BROWSER_POLICY_TEST_PLATFORM_POLICY_PROVIDER_H_
......@@ -342,7 +342,12 @@ source_set("hooks") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [ "eg_tests_hook.mm" ]
deps = [ "//ios/chrome/app:tests_hook" ]
deps = [
"//base",
"//components/policy/core/common:test_support",
"//ios/chrome/app:tests_hook",
"//ios/chrome/browser/policy:test_support",
]
}
source_set("eg_app_support+eg2") {
......@@ -393,6 +398,7 @@ source_set("eg_app_support+eg2") {
"//ios/chrome/browser/ntp:features",
"//ios/chrome/browser/passwords",
"//ios/chrome/browser/passwords:eg_app_support+eg2",
"//ios/chrome/browser/policy:eg_app_support+eg2",
"//ios/chrome/browser/translate:eg_app_support+eg2",
"//ios/chrome/browser/ui:feature_flags",
"//ios/chrome/browser/ui/authentication:eg_app_support+eg2",
......
......@@ -4,6 +4,9 @@
#include "ios/chrome/app/tests_hook.h"
#include "base/command_line.h"
#include "ios/chrome/browser/policy/test_platform_policy_provider.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
......@@ -34,6 +37,16 @@ bool DisableUpdateService() {
return true;
}
policy::ConfigurationPolicyProvider* GetOverriddenPlatformPolicyProvider() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
"com.apple.configuration.managed")) {
DVLOG(1) << "Policy data present in NSUserDefaults, not installing test "
"platform provider";
return nullptr;
}
return GetTestPlatformPolicyProvider();
}
void SetUpTestsIfPresent() {
// No-op for Earl Grey.
}
......
......@@ -58,6 +58,7 @@ chrome_ios_eg2_test("ios_chrome_integration_eg2tests_module") {
"//ios/chrome/browser/net:eg2_tests",
"//ios/chrome/browser/ntp_tiles:eg2_tests",
"//ios/chrome/browser/passwords:eg2_tests",
"//ios/chrome/browser/policy:eg2_tests",
"//ios/chrome/browser/prerender:eg2_tests",
"//ios/chrome/browser/translate:eg2_tests",
"//ios/chrome/browser/ui/autofill:eg2_tests",
......
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