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 297 298 299 300 301 302 303 304 305 306 307
|
// Copyright 2019 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/safe_browsing/cloud_content_scanning/deep_scanning_utils.h"
#include <limits>
#include <string>
#include <tuple>
#include <utility>
#include "base/files/file_path.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/enterprise/connectors/common.h"
#include "components/crash/core/common/crash_buildflags.h"
#include "components/crash/core/common/crash_key.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace safe_browsing {
namespace {
constexpr BinaryUploadService::Result kAllBinaryUploadServiceResults[]{
BinaryUploadService::Result::UNKNOWN,
BinaryUploadService::Result::SUCCESS,
BinaryUploadService::Result::UPLOAD_FAILURE,
BinaryUploadService::Result::TIMEOUT,
BinaryUploadService::Result::FILE_TOO_LARGE,
BinaryUploadService::Result::FAILED_TO_GET_TOKEN,
BinaryUploadService::Result::UNAUTHORIZED,
BinaryUploadService::Result::FILE_ENCRYPTED,
};
#if !BUILDFLAG(USE_CRASH_KEY_STUBS)
constexpr std::pair<ScanningCrashKey, const char*> kAllCrashKeys[] = {
{ScanningCrashKey::PENDING_FILE_UPLOADS, "pending-file-upload-scans"},
{ScanningCrashKey::PENDING_FILE_DOWNLOADS, "pending-file-download-scans"},
{ScanningCrashKey::PENDING_TEXT_UPLOADS, "pending-text-upload-scans"},
{ScanningCrashKey::PENDING_PRINTS, "pending-print-scans"},
{ScanningCrashKey::TOTAL_FILE_UPLOADS, "total-file-upload-scans"},
{ScanningCrashKey::TOTAL_FILE_DOWNLOADS, "total-file-download-scans"},
{ScanningCrashKey::TOTAL_TEXT_UPLOADS, "total-text-upload-scans"},
{ScanningCrashKey::TOTAL_PRINTS, "total-print-scans"}};
#endif // !BUILDFLAG(USE_CRASH_KEY_STUBS)
constexpr int64_t kTotalBytes = 1000;
constexpr base::TimeDelta kDuration = base::Seconds(10);
constexpr base::TimeDelta kInvalidDuration = base::Seconds(0);
} // namespace
class DeepScanningUtilsUMATest
: public testing::TestWithParam<
std::tuple<bool, DeepScanAccessPoint, BinaryUploadService::Result>> {
public:
DeepScanningUtilsUMATest() = default;
bool is_cloud() const { return std::get<0>(GetParam()); }
DeepScanAccessPoint access_point() const { return std::get<1>(GetParam()); }
std::string access_point_string() const {
return DeepScanAccessPointToString(access_point());
}
BinaryUploadService::Result result() const { return std::get<2>(GetParam()); }
bool success() const {
return result() == BinaryUploadService::Result::SUCCESS;
}
std::string result_value(bool success) const {
return BinaryUploadServiceResultToString(result(), success);
}
const base::HistogramTester& histograms() const { return histograms_; }
const char* metric_prefix() {
return is_cloud() ? "SafeBrowsing.DeepScan."
: "SafeBrowsing.LocalDeepScan.";
}
private:
base::HistogramTester histograms_;
};
INSTANTIATE_TEST_SUITE_P(
Tests,
DeepScanningUtilsUMATest,
testing::Combine(testing::Bool(),
testing::Values(DeepScanAccessPoint::DOWNLOAD,
DeepScanAccessPoint::UPLOAD,
DeepScanAccessPoint::DRAG_AND_DROP,
DeepScanAccessPoint::PASTE,
DeepScanAccessPoint::PRINT,
DeepScanAccessPoint::FILE_TRANSFER),
testing::ValuesIn(kAllBinaryUploadServiceResults)));
TEST_P(DeepScanningUtilsUMATest, SuccessfulScanVerdicts) {
// Record metrics for the 5 successful scan possibilities:
// - A default response
// - A DLP response with SUCCESS
// - A malware respopnse with MALWARE, UWS, CLEAN
RecordDeepScanMetrics(is_cloud(), access_point(), kDuration, kTotalBytes,
result(),
enterprise_connectors::ContentAnalysisResponse());
RecordDeepScanMetrics(
is_cloud(), access_point(), kDuration, kTotalBytes, result(),
SimpleContentAnalysisResponseForTesting(
/*dlp_success*/ true,
/*malware_success*/ std::nullopt, /*has_custom_rule_message*/ false));
for (const std::string& verdict : {"malware", "uws", "safe"}) {
enterprise_connectors::ContentAnalysisResponse response;
auto* malware_result = response.add_results();
malware_result->set_tag("malware");
malware_result->set_status(
enterprise_connectors::ContentAnalysisResponse::Result::SUCCESS);
if (verdict != "safe") {
auto* rule = malware_result->add_triggered_rules();
rule->set_rule_name("malware");
rule->set_action(enterprise_connectors::TriggeredRule::BLOCK);
}
RecordDeepScanMetrics(is_cloud(), access_point(), kDuration, kTotalBytes,
result(), response);
}
if (result() == BinaryUploadService::Result::UNAUTHORIZED) {
EXPECT_EQ(0u, histograms().GetTotalCountsForPrefix(metric_prefix()).size());
} else {
// We expect at least 2 histograms (<access-point>.Duration and
// <access-point>.<result>.Duration), but only expect a third histogram in
// the success case (bytes/seconds). Each of these should have 5 records to
// match each of the RecordDeepScanMetrics calls.
uint64_t expected_histograms = success() ? 3u : 2u;
auto recorded = histograms().GetTotalCountsForPrefix(metric_prefix());
EXPECT_EQ(expected_histograms, recorded.size());
if (success()) {
EXPECT_EQ(recorded[metric_prefix() + access_point_string() +
".BytesPerSeconds"],
5);
}
EXPECT_EQ(recorded[metric_prefix() + access_point_string() + ".Duration"],
5);
EXPECT_EQ(recorded[metric_prefix() + access_point_string() + "." +
result_value(true) + ".Duration"],
5);
}
}
TEST_P(DeepScanningUtilsUMATest, UnsuccessfulDlpScanVerdicts) {
// Record metrics for the 2 unsuccessful DLP scan possibilities.
for (const auto status :
{enterprise_connectors::ContentAnalysisResponse::Result::FAILURE,
enterprise_connectors::ContentAnalysisResponse::Result::
STATUS_UNKNOWN}) {
enterprise_connectors::ContentAnalysisResponse response;
auto* dlp_result = response.add_results();
dlp_result->set_tag("dlp");
dlp_result->set_status(status);
RecordDeepScanMetrics(is_cloud(), access_point(), kDuration, kTotalBytes,
result(), response);
}
if (result() == BinaryUploadService::Result::UNAUTHORIZED) {
EXPECT_EQ(0u, histograms().GetTotalCountsForPrefix(metric_prefix()).size());
} else {
auto recorded = histograms().GetTotalCountsForPrefix(metric_prefix());
EXPECT_EQ(2u, recorded.size());
EXPECT_EQ(recorded[metric_prefix() + access_point_string() + ".Duration"],
2);
EXPECT_EQ(recorded[metric_prefix() + access_point_string() + "." +
result_value(false) + ".Duration"],
2);
}
}
TEST_P(DeepScanningUtilsUMATest, UnsuccessfulMalwareScanVerdict) {
// Record metrics for the 2 unsuccessful malware scan possibilities.
for (const auto status :
{enterprise_connectors::ContentAnalysisResponse::Result::FAILURE,
enterprise_connectors::ContentAnalysisResponse::Result::
STATUS_UNKNOWN}) {
enterprise_connectors::ContentAnalysisResponse response;
auto* malware_result = response.add_results();
malware_result->set_tag("malware");
malware_result->set_status(status);
RecordDeepScanMetrics(is_cloud(), access_point(), kDuration, kTotalBytes,
result(), response);
}
if (result() == BinaryUploadService::Result::UNAUTHORIZED) {
EXPECT_EQ(0u, histograms().GetTotalCountsForPrefix(metric_prefix()).size());
} else {
auto recorded = histograms().GetTotalCountsForPrefix(metric_prefix());
EXPECT_EQ(2u, recorded.size());
EXPECT_EQ(recorded[metric_prefix() + access_point_string() + ".Duration"],
2);
EXPECT_EQ(recorded[metric_prefix() + access_point_string() + "." +
result_value(false) + ".Duration"],
2);
}
}
TEST_P(DeepScanningUtilsUMATest, BypassScanVerdict) {
RecordDeepScanMetrics(is_cloud(), access_point(), kDuration, kTotalBytes,
"BypassedByUser", false);
EXPECT_EQ(2u, histograms().GetTotalCountsForPrefix(metric_prefix()).size());
histograms().ExpectTimeBucketCount(
metric_prefix() + access_point_string() + ".Duration", kDuration, 1);
histograms().ExpectTimeBucketCount(
metric_prefix() + access_point_string() + ".BypassedByUser.Duration",
kDuration, 1);
}
TEST_P(DeepScanningUtilsUMATest, CancelledByUser) {
RecordDeepScanMetrics(is_cloud(), access_point(), kDuration, kTotalBytes,
"CancelledByUser", false);
EXPECT_EQ(2u, histograms().GetTotalCountsForPrefix(metric_prefix()).size());
histograms().ExpectTimeBucketCount(
metric_prefix() + access_point_string() + ".Duration", kDuration, 1);
histograms().ExpectTimeBucketCount(
metric_prefix() + access_point_string() + ".CancelledByUser.Duration",
kDuration, 1);
}
TEST_P(DeepScanningUtilsUMATest, InvalidDuration) {
RecordDeepScanMetrics(is_cloud(), access_point(), kInvalidDuration,
kTotalBytes, result(),
enterprise_connectors::ContentAnalysisResponse());
EXPECT_EQ(0u, histograms().GetTotalCountsForPrefix(metric_prefix()).size());
}
#if !BUILDFLAG(USE_CRASH_KEY_STUBS)
class DeepScanningUtilsCrashKeysTest
: public testing::TestWithParam<std::pair<ScanningCrashKey, const char*>> {
public:
void SetUp() override {
crash_reporter::ResetCrashKeysForTesting();
crash_reporter::InitializeCrashKeysForTesting();
}
void TearDown() override { crash_reporter::ResetCrashKeysForTesting(); }
ScanningCrashKey key_enum() { return std::get<0>(GetParam()); }
const char* key_string() { return std::get<1>(GetParam()); }
};
INSTANTIATE_TEST_SUITE_P(,
DeepScanningUtilsCrashKeysTest,
testing::ValuesIn(kAllCrashKeys));
TEST_P(DeepScanningUtilsCrashKeysTest, SmallModifications) {
// The key implicitly starts at 0.
IncrementCrashKey(key_enum(), 1);
EXPECT_EQ("1", crash_reporter::GetCrashKeyValue(key_string()));
IncrementCrashKey(key_enum(), 1);
EXPECT_EQ("2", crash_reporter::GetCrashKeyValue(key_string()));
DecrementCrashKey(key_enum(), 1);
EXPECT_EQ("1", crash_reporter::GetCrashKeyValue(key_string()));
DecrementCrashKey(key_enum(), 1);
EXPECT_TRUE(crash_reporter::GetCrashKeyValue(key_string()).empty());
}
TEST_P(DeepScanningUtilsCrashKeysTest, LargeModifications) {
// The key implicitly starts at 0.
IncrementCrashKey(key_enum(), 100);
EXPECT_EQ("100", crash_reporter::GetCrashKeyValue(key_string()));
IncrementCrashKey(key_enum(), 100);
EXPECT_EQ("200", crash_reporter::GetCrashKeyValue(key_string()));
DecrementCrashKey(key_enum(), 100);
EXPECT_EQ("100", crash_reporter::GetCrashKeyValue(key_string()));
DecrementCrashKey(key_enum(), 100);
EXPECT_TRUE(crash_reporter::GetCrashKeyValue(key_string()).empty());
}
TEST_P(DeepScanningUtilsCrashKeysTest, InvalidModifications) {
// The crash key value cannot be negative.
DecrementCrashKey(key_enum(), 1);
EXPECT_TRUE(crash_reporter::GetCrashKeyValue(key_string()).empty());
DecrementCrashKey(key_enum(), 100);
EXPECT_TRUE(crash_reporter::GetCrashKeyValue(key_string()).empty());
// The crash key value is restricted to 6 digits. If a modification would
// exceed it, it is clamped so crashes will indicate that the key was set at a
// very high value.
IncrementCrashKey(key_enum(), 123456789);
EXPECT_EQ("999999", crash_reporter::GetCrashKeyValue(key_string()));
IncrementCrashKey(key_enum(), 123456789);
EXPECT_EQ("999999", crash_reporter::GetCrashKeyValue(key_string()));
}
#endif // !BUILDFLAG(USE_CRASH_KEY_STUBS)
} // namespace safe_browsing
|