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
|
// 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.
#ifndef CHROME_BROWSER_ENTERPRISE_CONNECTORS_TEST_DEEP_SCANNING_TEST_UTILS_H_
#define CHROME_BROWSER_ENTERPRISE_CONNECTORS_TEST_DEEP_SCANNING_TEST_UTILS_H_
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/enterprise/buildflags/buildflags.h"
#include "components/enterprise/common/proto/connectors.pb.h"
#include "components/enterprise/connectors/core/reporting_test_utils.h"
#include "components/enterprise/data_controls/core/browser/verdict.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "extensions/buildflags/buildflags.h"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/common/extensions/api/enterprise_reporting_private.h"
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
namespace policy {
class MockCloudPolicyClient;
}
namespace enterprise_connectors::test {
// Helper class that represents a report that's expected from a test. The
// non-optional fields are expected for every kind of Deep Scanning reports.
// The optional ones are not present on every Deep Scanning event. The mimetype
// field is handled by a pointer to a set since different builds/platforms can
// return different mimetype strings for the same file.
class EventReportValidator : public EventReportValidatorBase {
public:
explicit EventReportValidator(policy::MockCloudPolicyClient* client);
~EventReportValidator();
void ExpectDangerousDeepScanningResult(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_source,
const std::string& expected_destination,
const std::string& expected_filename,
const std::string& expected_sha256,
const std::string& expected_threat_type,
const std::string& expected_trigger,
const std::set<std::string>* expected_mimetypes,
int64_t expected_content_size,
const std::string& expected_result,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
const std::optional<std::string>& expected_scan_id);
void ExpectSensitiveDataEvent(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_source,
const std::string& expected_destination,
const std::string& expected_filename,
const std::string& expected_sha256,
const std::string& expected_trigger,
const ContentAnalysisResponse::Result& expected_dlp_verdict,
const std::set<std::string>* expected_mimetypes,
std::optional<int64_t> expected_content_size,
const std::string& expected_result,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
const std::string& expected_scan_id,
const std::optional<std::string>& expected_content_transfer_method,
const std::optional<std::u16string>& expected_user_justification);
void ExpectDataControlsSensitiveDataEvent(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_source,
const std::string& expected_destination,
const std::set<std::string>* expected_mimetypes,
const std::string& expected_trigger,
const data_controls::Verdict::TriggeredRules& triggered_rules,
const std::string& expected_result,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
int64_t expected_content_size);
#if BUILDFLAG(ENABLE_EXTENSIONS)
void ExpectDataMaskingEvent(
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
extensions::api::enterprise_reporting_private::DataMaskingEvent
expected_event);
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
void ExpectSensitiveDataEvents(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_source,
const std::string& expected_destination,
const std::vector<std::string>& expected_filenames,
const std::vector<std::string>& expected_sha256s,
const std::string& expected_trigger,
const std::vector<ContentAnalysisResponse::Result>& expected_dlp_verdicts,
const std::set<std::string>* expected_mimetypes,
int64_t expected_content_size,
const std::vector<std::string>& expected_results,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
const std::vector<std::string>& expected_scan_ids,
const std::optional<std::string>& expected_content_transfer_method,
const std::optional<std::u16string>& expected_user_justification);
void ExpectDangerousDeepScanningResultAndSensitiveDataEvent(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_source,
const std::string& expected_destination,
const std::string& expected_filename,
const std::string& expected_sha256,
const std::string& expected_threat_type,
const std::string& expected_trigger,
const ContentAnalysisResponse::Result& expected_dlp_verdict,
const std::set<std::string>* expected_mimetypes,
int64_t expected_content_size,
const std::string& expected_result,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
const std::string& expected_scan_id,
const std::optional<std::string>& expected_content_transfer_method);
void ExpectSensitiveDataEventAndDangerousDeepScanningResult(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_source,
const std::string& expected_destination,
const std::string& expected_filename,
const std::string& expected_sha256,
const std::string& expected_threat_type,
const std::string& expected_trigger,
const ContentAnalysisResponse::Result& expected_dlp_verdict,
const std::set<std::string>* expected_mimetypes,
int64_t expected_content_size,
const std::string& expected_result,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
const std::string& expected_scan_id);
void ExpectUnscannedFileEvent(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_source,
const std::string& expected_destination,
const std::string& expected_filename,
const std::string& expected_sha256,
const std::string& expected_trigger,
const std::string& expected_reason,
const std::set<std::string>* expected_mimetypes,
std::optional<int64_t> expected_content_size,
const std::string& expected_result,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
const std::optional<std::string>& expected_content_transfer_method);
void ExpectUnscannedFileEvents(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_source,
const std::string& expected_destination,
const std::vector<std::string>& expected_filenames,
const std::vector<std::string>& expected_sha256s,
const std::string& expected_trigger,
const std::string& expected_reason,
const std::set<std::string>* expected_mimetypes,
int64_t expected_content_size,
const std::string& expected_result,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier,
const std::optional<std::string>& expected_content_transfer_method);
void ExpectDangerousDownloadEvent(
const std::string& expected_url,
const std::string& expected_tab_url,
const std::string& expected_filename,
const std::string& expected_sha256,
const std::string& expected_threat_type,
const std::string& expected_trigger,
const std::set<std::string>* expected_mimetypes,
int64_t expected_content_size,
const std::string& expected_result,
const std::string& expected_profile_username,
const std::string& expected_profile_identifier);
// Closure to run once all expected events are validated.
void SetDoneClosure(base::RepeatingClosure closure);
private:
void ValidateReport(const base::Value::Dict* report);
void ValidateFederatedOrigin(const base::Value::Dict* value);
void ValidateIdentities(const base::Value::Dict* value);
void ValidateMimeType(const base::Value::Dict* value);
void ValidateDlpVerdict(const base::Value::Dict* value,
const ContentAnalysisResponse::Result& result);
void ValidateDlpRule(
const base::Value::Dict* value,
const ContentAnalysisResponse::Result::TriggeredRule& expected_rule);
void ValidateFilenameMappedAttributes(const base::Value::Dict* value);
void ValidateDataControlsAttributes(const base::Value::Dict* event);
#if BUILDFLAG(ENABLE_EXTENSIONS)
void ValidateDataMaskingAttributes(const base::Value::Dict* event);
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
std::string event_key_;
std::optional<std::string> url_;
std::optional<std::string> tab_url_;
std::optional<std::string> source_;
std::optional<std::string> destination_;
std::optional<std::string> trigger_ = std::nullopt;
std::optional<std::string> threat_type_ = std::nullopt;
std::optional<std::string> unscanned_reason_ = std::nullopt;
std::optional<std::string> content_transfer_method_ = std::nullopt;
std::optional<std::u16string> user_justification_ = std::nullopt;
std::optional<int64_t> content_size_ = std::nullopt;
raw_ptr<const std::set<std::string>> mimetypes_ = nullptr;
std::string username_;
std::string profile_identifier_;
std::optional<bool> is_federated_ = std::nullopt;
std::optional<std::string> federated_origin_ = std::nullopt;
std::optional<std::u16string> login_user_name_ = std::nullopt;
std::optional<std::vector<std::pair<std::string, std::u16string>>>
password_breach_identities_ = std::nullopt;
std::optional<std::string> data_controls_result_ = std::nullopt;
data_controls::Verdict::TriggeredRules data_controls_triggered_rules_;
#if BUILDFLAG(ENABLE_EXTENSIONS)
// `DataMaskingEvent`'s copy constructor is deleted, so to keep
// `EventReportValidator` copyable a lazy builder is used to store its
// expected value.
base::RepeatingCallback<
extensions::api::enterprise_reporting_private::DataMaskingEvent()>
expected_data_masking_rules_builder_;
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
// When multiple files generate events, we don't necessarily know in which
// order they will be reported. As such, we use maps to ensure all of them
// are called as expected.
base::flat_map<std::string, ContentAnalysisResponse::Result> dlp_verdicts_;
base::flat_map<std::string, std::string> results_;
base::flat_map<std::string, std::string> filenames_and_hashes_;
base::flat_map<std::string, std::string> scan_ids_;
};
// Helper class to set up tests to use `EventReportValidator`.
class EventReportValidatorHelper {
public:
explicit EventReportValidatorHelper(Profile* profile,
bool browser_test = false);
~EventReportValidatorHelper();
EventReportValidator CreateValidator();
private:
raw_ptr<Profile> profile_;
std::unique_ptr<policy::MockCloudPolicyClient> client_;
signin::IdentityTestEnvironment identity_test_environment_;
};
#if BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
// Helper functions that set Connector policies for testing.
void SetAnalysisConnector(PrefService* prefs,
AnalysisConnector connector,
const std::string& pref_value,
bool machine_scope = true);
void ClearAnalysisConnector(PrefService* prefs, AnalysisConnector connector);
#endif // BUILDFLAG(ENTERPRISE_CONTENT_ANALYSIS)
#if !BUILDFLAG(IS_CHROMEOS)
// Helper function to set the profile DM token. It installs a
// MockCloudPolicyClient with |dm_token| into |profile|'s UserCloudPolicyManager
// to simulate |profile|'s DM token.
void SetProfileDMToken(Profile* profile, const std::string& dm_token);
#endif
} // namespace enterprise_connectors::test
#endif // CHROME_BROWSER_ENTERPRISE_CONNECTORS_TEST_DEEP_SCANNING_TEST_UTILS_H_
|