chromium/chrome/browser/ssl/https_first_mode_settings_tracker_unittest.cc

// Copyright 2023 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/ssl/https_first_mode_settings_tracker.h"
#include "base/json/values_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/safe_browsing/advanced_protection_status_manager.h"
#include "chrome/browser/safe_browsing/advanced_protection_status_manager_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/ssl/stateful_ssl_host_state_delegate_factory.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_test_utils.h"
#include "components/security_interstitials/content/stateful_ssl_host_state_delegate.h"
#include "components/security_interstitials/core/https_only_mode_metrics.h"
#include "components/site_engagement/content/site_engagement_score.h"
#include "components/site_engagement/content/site_engagement_service.h"
#include "components/site_engagement/core/pref_names.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

kSiteEngagementHeuristicAccumulatedHostCountHistogram;
kSiteEngagementHeuristicEnforcementDurationHistogram;
kSiteEngagementHeuristicHostCountHistogram;
kSiteEngagementHeuristicStateHistogram;
SiteEngagementHeuristicState;

// Tests for HTTPS First Mode settings, such as enabling HFM through Site
// Engagement scores.
class HttpsFirstModeSettingsTrackerTest : public testing::Test {};

void MaybeEnableHttpsFirstModeForEngagedSitesAndWait(
    HttpsFirstModeService* hfm_service) {}

// Check that changing the HFM pref clears Site Engagement heuristic's HTTPS
// enforcelist and effectively disables the heuristic.
TEST_F(HttpsFirstModeSettingsTrackerTest,
       SiteEngagementHeuristic_ShouldNotEnableIfPrefIsSet) {}

// Check that high site engagement scores of HTTPS URLs with non-default ports
// do not auto-enable HTTPS-First Mode.
TEST_F(
    HttpsFirstModeSettingsTrackerTest,
    SiteEngagementHeuristic_ShouldIgnoreEngagementScoreOfUrlWithNonDefaultPort) {}

TEST_F(HttpsFirstModeSettingsTrackerTest,
       SiteEngagementHeuristic_ShouldEnforceHttps) {}

// If a site was previously been HTTPS-enforced no longer is in the site
// engagement list, it should no longer be HTTPS-enforced anymore.
TEST_F(HttpsFirstModeSettingsTrackerTest,
       SiteEngagementHeuristic_NoEngagementScoreShouldUnenforceHttps) {}

// Tests the Typically Secure User heuristic to ensure that it respects the
// finch flag. See TypicallySecureUserPref for more details.
// Regression test for crbug.com/1475747.
TEST_F(HttpsFirstModeSettingsTrackerTest,
       TypicallySecureUser_DisabledByDefault) {}

// Creates the HFM service and waits for it to write the initial values into
// the typically secure fallbacks pref.
HttpsFirstModeService*
CreateHttpsFirstModeServiceAndWaitForTypicallySecureUserPrefInitialized(
    Profile* profile,
    base::SimpleTestClock* clock) {}

// A new profile shouldn't write any prefs related to Typically Secure User
// heuristic. TypicallySecureUserTest's SetUp() sets an old profile creation
// time and then create HttpsFirstModeService, so this can't be a
// TypicallySecureUserTest.
TEST_F(HttpsFirstModeSettingsTrackerTest, TypicallySecureUser_NewProfile) {}

// Typically Secure User fallback entries pref written by an older version of
// Chrome should be handled properly by new versions.
TEST_F(HttpsFirstModeSettingsTrackerTest, TypicallySecureUser_OldVersion) {}

class TypicallySecureUserTest : public HttpsFirstModeSettingsTrackerTest {};

// An old profile should initialize the prefs related to Typically Secure User
// heuristic.
TEST_F(TypicallySecureUserTest, ProfileOldEnough) {}

// Checks that Typically Secure Heuristic must observe navigations for at least
// a week before enabling HFM pref.
TEST_F(TypicallySecureUserTest, EnablePrefWhenObservedForLongEnough) {}

// Checks that Typically Secure Heuristic must observe navigations for at least
// a week before enabling HFM pref.
TEST_F(TypicallySecureUserTest,
       DontEnablePrefWhenObservedForLongEnoughWithManyWarnings) {}

// Tests for the Typically Secure User heuristic. This test repeatedly calls
// RecordHttpsUpgradeFallbackEvent which is normally called
// from HTTPS-Upgrade fallbacks in production code. It then checks if the
// HTTPS-First Mode pref is enabled.
TEST_F(TypicallySecureUserTest, HFMEnabled) {}

// Checks that manually changing the HFM pref in the UI clears the HTTP
// allowlist. A variant of this test
// (TypicallySecureUserTest.PrefUpdatedByHeuristic_ShouldNotClearAllowlist)
// checks that a heuristic auto-enabling HFM does NOT clear the allowlist.
TEST_F(HttpsFirstModeSettingsTrackerTest, PrefUpdated_ShouldClearAllowlist) {}

TEST_F(TypicallySecureUserTest,
       PrefUpdatedByHeuristic_ShouldNotClearAllowlist) {}

// Tests that the correct setting at startup is logged, when the Balanced Mode
// feature flag is enabled but not on by default.
TEST_F(HttpsFirstModeSettingsTrackerTest, StartupBalancedModeAvailable) {}

// Tests that the correct setting at startup is logged, when Balanced Mode
// is auto-enabled.
TEST_F(HttpsFirstModeSettingsTrackerTest, StartupBalancedModeAutoEnabled) {}