0
Files
src/content/browser/host_zoom_map_impl_unittest.cc
Arthur Sonzogni d5ce01f7ef C++11 std::array rewrite for memory safety [11/19]
Split from:
https://chromium-review.googlesource.com/c/chromium/src/+/6004959

Generated patch
---------------
- Tool: ./tool/clang/spanify/rewrite-multiple-platform.sh
- Platform: Linux.
- Filter: This includes 2400/4222 patches. I included the std::array
      ones and excluded build errors.

Google announcement:
--------------------
https://groups.google.com/a/google.com/g/chrome-memory-safety/c/RMiO4gaVLQA/m/Yz-3NCObAgAJ

Benchmarks:
----------
See design doc and
https://chromium-review.googlesource.com/c/chromium/src/+/6004959/21

Description
-----------
The consensus during the memory safety summit was to begin rewriting
relevant C-style arrays to C++11 std::array. It can be done immediately,
offers better developer ergonomics, and fix large chunks of the
-Wunsafe-buffer-usage errors in Chrome.

To clarify, this effort is complementary to the longer plan work with
enabling -fsanitize=array-bounds, and we plan to leverage both,
especially for protecting 3p code.

[Attached] is a document detailing the rationale, benefits, and
considerations for potential compile-time and performance impacts.

[Attached]:https://docs.google.com/document/d/1z5aBDg26lHmNDjXRCysElWKx7E4PAJXqykI_k7ondJI/edit?tab=t.0#heading=h.cqgo7wvp0kzt

NO_IFTTT=No need to update base/debug/stack_trace.h

Bug: 378069401
Change-Id: I6d36543c267f12c672cce00d75cf833fff238ebf
R: dcheng@chromium.org
AX-Relnotes: n/a.
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6039265
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Auto-Submit: Arthur Sonzogni <arthursonzogni@chromium.org>
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Owners-Override: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1395908}
2024-12-13 05:35:28 -08:00

158 lines
5.6 KiB
C++

// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/host_zoom_map_impl.h"
#include <stddef.h>
#include <array>
#include "base/memory/ref_counted.h"
#include "base/test/simple_test_clock.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
class HostZoomMapTest : public testing::Test {
public:
HostZoomMapTest() = default;
private:
BrowserTaskEnvironment task_environment_;
};
TEST_F(HostZoomMapTest, GetSetZoomLevel) {
HostZoomMapImpl host_zoom_map;
double zoomed = 2.5;
host_zoom_map.SetZoomLevelForHost("zoomed.com", zoomed);
EXPECT_DOUBLE_EQ(0,
host_zoom_map.GetZoomLevelForHostAndScheme("http", "normal.com"));
EXPECT_DOUBLE_EQ(zoomed,
host_zoom_map.GetZoomLevelForHostAndScheme("http", "zoomed.com"));
}
TEST_F(HostZoomMapTest, GetSetZoomLevelWithScheme) {
HostZoomMapImpl host_zoom_map;
double zoomed = 2.5;
double default_zoom = 1.5;
host_zoom_map.SetZoomLevelForHostAndScheme("chrome", "login", 0);
host_zoom_map.SetDefaultZoomLevel(default_zoom);
EXPECT_DOUBLE_EQ(0,
host_zoom_map.GetZoomLevelForHostAndScheme("chrome", "login"));
EXPECT_DOUBLE_EQ(default_zoom,
host_zoom_map.GetZoomLevelForHostAndScheme("http", "login"));
host_zoom_map.SetZoomLevelForHost("login", zoomed);
EXPECT_DOUBLE_EQ(0,
host_zoom_map.GetZoomLevelForHostAndScheme("chrome", "login"));
EXPECT_DOUBLE_EQ(zoomed,
host_zoom_map.GetZoomLevelForHostAndScheme("http", "login"));
}
TEST_F(HostZoomMapTest, GetAllZoomLevels) {
HostZoomMapImpl host_zoom_map;
double zoomed = 2.5;
host_zoom_map.SetZoomLevelForHost("zoomed.com", zoomed);
host_zoom_map.SetZoomLevelForHostAndScheme("https", "zoomed.com", zoomed);
host_zoom_map.SetZoomLevelForHostAndScheme("chrome", "login", zoomed);
HostZoomMap::ZoomLevelVector levels = host_zoom_map.GetAllZoomLevels();
auto expected = std::to_array<HostZoomMap::ZoomLevelChange>({
{HostZoomMap::ZOOM_CHANGED_FOR_HOST, "zoomed.com", std::string(), zoomed},
{HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST, "login", "chrome",
zoomed},
{HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST, "zoomed.com", "https",
zoomed},
});
ASSERT_EQ(std::size(expected), levels.size());
for (size_t i = 0; i < std::size(expected); ++i) {
SCOPED_TRACE(testing::Message() << "levels[" << i << "]");
EXPECT_EQ(expected[i].mode, levels[i].mode);
EXPECT_EQ(expected[i].scheme, levels[i].scheme);
EXPECT_EQ(expected[i].host, levels[i].host);
EXPECT_EQ(expected[i].zoom_level, levels[i].zoom_level);
EXPECT_EQ(expected[i].last_modified, base::Time());
}
}
TEST_F(HostZoomMapTest, LastModifiedTimestamp) {
HostZoomMapImpl host_zoom_map;
base::Time now = base::Time::Now();
base::SimpleTestClock test_clock;
host_zoom_map.SetClockForTesting(&test_clock);
test_clock.SetNow(now);
host_zoom_map.SetZoomLevelForHost("zoomed.com", 1.5);
host_zoom_map.SetZoomLevelForHost("zoomed2.com", 2.0);
base::Time later = now + base::Seconds(1);
test_clock.SetNow(later);
host_zoom_map.SetZoomLevelForHost("zoomed2.com", 2.5);
host_zoom_map.SetZoomLevelForHost("zoomzoom.com", 3);
host_zoom_map.SetZoomLevelForHostAndScheme("chrome", "login", 3);
HostZoomMap::ZoomLevelVector levels = host_zoom_map.GetAllZoomLevels();
std::string scheme;
auto expected = std::to_array<HostZoomMap::ZoomLevelChange>({
{HostZoomMap::ZOOM_CHANGED_FOR_HOST, "zoomed.com", scheme, 1.5, now},
{HostZoomMap::ZOOM_CHANGED_FOR_HOST, "zoomed2.com", scheme, 2.5, later},
{HostZoomMap::ZOOM_CHANGED_FOR_HOST, "zoomzoom.com", scheme, 3.0, later},
{HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST, "login", "chrome", 3.0,
base::Time()},
});
ASSERT_EQ(std::size(expected), levels.size());
for (size_t i = 0; i < std::size(expected); ++i) {
SCOPED_TRACE(testing::Message() << "levels[" << i << "]");
EXPECT_EQ(expected[i].mode, levels[i].mode);
EXPECT_EQ(expected[i].scheme, levels[i].scheme);
EXPECT_EQ(expected[i].host, levels[i].host);
EXPECT_EQ(expected[i].zoom_level, levels[i].zoom_level);
EXPECT_EQ(expected[i].last_modified, levels[i].last_modified);
}
}
TEST_F(HostZoomMapTest, ClearZoomLevels) {
HostZoomMapImpl host_zoom_map;
base::SimpleTestClock test_clock;
host_zoom_map.SetClockForTesting(&test_clock);
base::Time now = base::Time::Now();
test_clock.SetNow(now - base::Hours(3));
host_zoom_map.SetZoomLevelForHost("zoomzoom.com", 3.5);
test_clock.SetNow(now - base::Hours(1));
host_zoom_map.SetZoomLevelForHost("zoom.com", 1.5);
test_clock.SetNow(now - base::Days(31));
host_zoom_map.SetZoomLevelForHost("zoom2.com", 2.5);
EXPECT_EQ(3u, host_zoom_map.GetAllZoomLevels().size());
host_zoom_map.ClearZoomLevels(now - base::Hours(2), base::Time::Max());
ASSERT_EQ(2u, host_zoom_map.GetAllZoomLevels().size());
EXPECT_EQ("zoom2.com", host_zoom_map.GetAllZoomLevels()[0].host);
EXPECT_EQ("zoomzoom.com", host_zoom_map.GetAllZoomLevels()[1].host);
host_zoom_map.ClearZoomLevels(base::Time(), now - base::Days(30));
ASSERT_EQ(1u, host_zoom_map.GetAllZoomLevels().size());
EXPECT_EQ("zoomzoom.com", host_zoom_map.GetAllZoomLevels()[0].host);
host_zoom_map.ClearZoomLevels(base::Time(), base::Time::Max());
EXPECT_EQ(0u, host_zoom_map.GetAllZoomLevels().size());
// Host and scheme settings should not be affected.
host_zoom_map.SetZoomLevelForHostAndScheme("chrome", "login", 3);
host_zoom_map.ClearZoomLevels(base::Time(), base::Time::Max());
EXPECT_EQ(1u, host_zoom_map.GetAllZoomLevels().size());
}
} // namespace content