1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/enterprise/reporting/extension_request/extension_request_observer.h"
#include "base/containers/contains.h"
#include "base/json/json_reader.h"
#include "base/json/values_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "extensions/browser/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace enterprise_reporting {
namespace {
constexpr char kApprovedNotificationId[] = "extension_approved_notificaiton";
constexpr char kRejectedNotificationId[] = "extension_rejected_notificaiton";
constexpr char kInstalledNotificationId[] = "extension_installed_notificaiton";
constexpr char kExtensionId1[] = "abcdefghijklmnopabcdefghijklmnop";
constexpr char kExtensionId2[] = "abcdefghijklmnopabcdefghijklmnpo";
constexpr char kExtensionId3[] = "abcdefghijklmnopabcdefghijklmoop";
constexpr char kExtensionId4[] = "abcdefghijklmnopabcdefghijklmopo";
constexpr char kExtensionId5[] = "abcdefghijklmnopabcdefghijklmpop";
constexpr char kExtensionId6[] = "abcdefghijklmnopabcdefghijklmppo";
constexpr char kExtensionSettings[] = R"({
"abcdefghijklmnopabcdefghijklmnop" : {
"installation_mode": "allowed"
},
"abcdefghijklmnopabcdefghijklmnpo" : {
"installation_mode": "blocked"
},
"abcdefghijklmnopabcdefghijklmoop" : {
"installation_mode": "force_installed",
"update_url": "https://clients2.google.com/service/update2/crx"
},
"abcdefghijklmnopabcdefghijklmopo" : {
"installation_mode": "normal_installed",
"update_url": "https://clients2.google.com/service/update2/crx"
}
})";
constexpr char kExtensionSettingsUpdate[] = R"({
"abcdefghijklmnopabcdefghijklmnop" : {
"installation_mode": "blocked"
},
"abcdefghijklmnopabcdefghijklmnpo" : {
"installation_mode": "allowed"
},
"abcdefghijklmnopabcdefghijklmoop" : {
"installation_mode": "normal_installed",
"update_url": "https://clients2.google.com/service/update2/crx"
},
"abcdefghijklmnopabcdefghijklmopo" : {
"installation_mode": "force_installed",
"update_url": "https://clients2.google.com/service/update2/crx"
}
})";
constexpr char kPendingListUpdateMetricsName[] =
"Enterprise.CloudExtensionRequestUpdated";
} // namespace
class ExtensionRequestObserverTest : public BrowserWithTestWindowTest {
public:
void SetUp() override {
BrowserWithTestWindowTest::SetUp();
display_service_tester_ =
std::make_unique<NotificationDisplayServiceTester>(profile());
ToggleExtensionRequest(true);
}
void ToggleExtensionRequest(bool enable) {
profile()->GetTestingPrefService()->SetManagedPref(
prefs::kCloudExtensionRequestEnabled,
std::make_unique<base::Value>(enable));
}
// Creates fake pending request in pref.
void SetPendingList(const std::vector<std::string>& ids) {
base::Value::Dict id_values;
for (const auto& id : ids) {
id_values.Set(id, base::Value::Dict().Set(
extension_misc::kExtensionRequestTimestamp,
::base::TimeToValue(base::Time::Now())));
}
profile()->GetTestingPrefService()->SetUserPref(
prefs::kCloudExtensionRequestIds, std::move(id_values));
}
std::vector<std::optional<message_center::Notification>>
GetAllNotifications() {
return {display_service_tester_->GetNotification(kApprovedNotificationId),
display_service_tester_->GetNotification(kRejectedNotificationId),
display_service_tester_->GetNotification(kInstalledNotificationId)};
}
// Waits and verifies if the notifications are displayed or not.
void VerifyNotification(bool has_notification) {
task_environment()->RunUntilIdle();
for (auto& notification : GetAllNotifications())
EXPECT_EQ(has_notification, notification.has_value());
}
//
void SetExtensionSettings(const std::string& settings_string) {
std::optional<base::Value> settings =
base::JSONReader::Read(settings_string);
ASSERT_TRUE(settings.has_value());
profile()->GetTestingPrefService()->SetManagedPref(
extensions::pref_names::kExtensionManagement, std::move(*settings));
}
void CloseNotificationAndVerify(
const std::string& notification_id,
const std::vector<std::string>& expected_removed_requests) {
// Record the number of requests before closing any notification.
size_t number_of_existing_requests =
profile()->GetPrefs()->GetDict(prefs::kCloudExtensionRequestIds).size();
// Close the notification
base::RunLoop close_run_loop;
display_service_tester_->SetNotificationClosedClosure(
close_run_loop.QuitClosure());
display_service_tester_->SimulateClick(
NotificationHandler::Type::TRANSIENT, notification_id,
std::optional<int>(), std::optional<std::u16string>());
close_run_loop.Run();
// Verify that only |expected_removed_requests| are removed from the pref.
const base::Value::Dict& actual_pending_requests =
profile()->GetPrefs()->GetDict(prefs::kCloudExtensionRequestIds);
EXPECT_EQ(number_of_existing_requests - expected_removed_requests.size(),
actual_pending_requests.size());
for (auto it : actual_pending_requests) {
EXPECT_FALSE(base::Contains(expected_removed_requests, it.first));
}
closed_notification_count_ += 1;
histogram_tester()->ExpectBucketCount(kPendingListUpdateMetricsName,
/*removed*/ 1,
closed_notification_count_);
}
base::HistogramTester* histogram_tester() { return &histogram_tester_; }
private:
base::HistogramTester histogram_tester_;
int closed_notification_count_ = 0;
std::unique_ptr<NotificationDisplayServiceTester> display_service_tester_;
};
TEST_F(ExtensionRequestObserverTest, NoPendingRequestTest) {
SetPendingList({});
ExtensionRequestObserver observer(profile());
VerifyNotification(false);
SetExtensionSettings(kExtensionSettings);
VerifyNotification(false);
histogram_tester()->ExpectTotalCount(kPendingListUpdateMetricsName, 0);
}
TEST_F(ExtensionRequestObserverTest, UserConfirmNotification) {
SetPendingList({kExtensionId1, kExtensionId2, kExtensionId3, kExtensionId4,
kExtensionId5, kExtensionId6});
ExtensionRequestObserver observer(profile());
VerifyNotification(false);
SetExtensionSettings(kExtensionSettings);
VerifyNotification(true);
CloseNotificationAndVerify(kApprovedNotificationId, {kExtensionId1});
CloseNotificationAndVerify(kRejectedNotificationId, {kExtensionId2});
CloseNotificationAndVerify(kInstalledNotificationId,
{kExtensionId3, kExtensionId4});
}
TEST_F(ExtensionRequestObserverTest, NotificationClosedWithoutUserConfirmed) {
std::vector<std::string> pending_list = {kExtensionId1, kExtensionId2,
kExtensionId3, kExtensionId4,
kExtensionId5, kExtensionId6};
SetPendingList(pending_list);
std::unique_ptr<ExtensionRequestObserver> observer =
std::make_unique<ExtensionRequestObserver>(profile());
VerifyNotification(false);
SetExtensionSettings(kExtensionSettings);
VerifyNotification(true);
observer.reset();
VerifyNotification(false);
// No request removed when notification is not closed by user.
EXPECT_EQ(
pending_list.size(),
profile()->GetPrefs()->GetDict(prefs::kCloudExtensionRequestIds).size());
histogram_tester()->ExpectTotalCount(kPendingListUpdateMetricsName, 0);
}
TEST_F(ExtensionRequestObserverTest, NotificationClose) {
SetPendingList({kExtensionId1, kExtensionId2, kExtensionId3, kExtensionId4,
kExtensionId5, kExtensionId6});
ExtensionRequestObserver observer(profile());
VerifyNotification(false);
SetExtensionSettings(kExtensionSettings);
VerifyNotification(true);
SetExtensionSettings("{}");
VerifyNotification(false);
histogram_tester()->ExpectTotalCount(kPendingListUpdateMetricsName, 0);
}
TEST_F(ExtensionRequestObserverTest, NotificationUpdate) {
SetPendingList({kExtensionId1, kExtensionId2, kExtensionId3, kExtensionId4,
kExtensionId5, kExtensionId6});
ExtensionRequestObserver observer(profile());
VerifyNotification(false);
SetExtensionSettings(kExtensionSettings);
VerifyNotification(true);
SetExtensionSettings(kExtensionSettingsUpdate);
VerifyNotification(true);
histogram_tester()->ExpectTotalCount(kPendingListUpdateMetricsName, 0);
}
TEST_F(ExtensionRequestObserverTest, ExtensionRequestPolicyToggle) {
std::vector<std::string> pending_list = {kExtensionId1, kExtensionId2,
kExtensionId3, kExtensionId4,
kExtensionId5, kExtensionId6};
SetPendingList(pending_list);
SetExtensionSettings(kExtensionSettings);
ToggleExtensionRequest(false);
// No notification without the policy.
ExtensionRequestObserver observer(profile());
VerifyNotification(false);
// Show notification when the policy is turned on.
ToggleExtensionRequest(true);
VerifyNotification(true);
// Close all notifictions when the policy is off again.
ToggleExtensionRequest(false);
VerifyNotification(false);
// And no pending requests are removed.
EXPECT_EQ(
pending_list.size(),
profile()->GetPrefs()->GetDict(prefs::kCloudExtensionRequestIds).size());
histogram_tester()->ExpectTotalCount(kPendingListUpdateMetricsName, 0);
}
TEST_F(ExtensionRequestObserverTest, PendingRequestAddedAfterPolicyUpdated) {
ExtensionRequestObserver observer(profile());
VerifyNotification(false);
SetExtensionSettings(kExtensionSettings);
VerifyNotification(false);
histogram_tester()->ExpectTotalCount(kPendingListUpdateMetricsName, 0);
SetPendingList({kExtensionId1, kExtensionId2, kExtensionId3, kExtensionId4,
kExtensionId5, kExtensionId6});
VerifyNotification(true);
histogram_tester()->ExpectUniqueSample(kPendingListUpdateMetricsName,
/*added*/ 0, 1);
}
TEST_F(ExtensionRequestObserverTest, UpdateWithReportEnabledAndDisabled) {
ExtensionRequestObserver observer(profile());
base::MockCallback<ExtensionRequestObserver::ReportTrigger> callback;
observer.EnableReport(callback.Get());
EXPECT_CALL(callback, Run(profile())).Times(1);
SetPendingList({kExtensionId1});
observer.DisableReport();
EXPECT_CALL(callback, Run(::testing::_)).Times(0);
SetPendingList({kExtensionId1, kExtensionId2});
}
} // namespace enterprise_reporting
|