blob: 182e7335f096ee94747abdfce8b020c24f71c55a [file] [log] [blame]
Roger McFarlanec92516502023-05-15 20:05:121// Copyright 2023 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/metrics/histogram_shared_memory.h"
Roger McFarlanecba0e902024-01-24 17:51:376
7#include "base/base_switches.h"
8#include "base/command_line.h"
9#include "base/metrics/persistent_histogram_allocator.h"
10#include "base/process/launch.h"
11#include "base/strings/string_number_conversions.h"
12#include "base/strings/string_split.h"
13#include "base/test/multiprocess_test.h"
14#include "base/test/scoped_feature_list.h"
15#include "base/test/test_timeouts.h"
16#include "base/unguessable_token.h"
17#include "build/build_config.h"
Roger McFarlanec92516502023-05-15 20:05:1218#include "testing/gtest/include/gtest/gtest.h"
Roger McFarlanecba0e902024-01-24 17:51:3719#include "testing/multiprocess_func_list.h"
20
21#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
22#include "base/files/platform_file.h"
23#include "base/posix/global_descriptors.h"
24#endif
Roger McFarlanec92516502023-05-15 20:05:1225
26namespace base {
Roger McFarlanecba0e902024-01-24 17:51:3727namespace {
Roger McFarlanec92516502023-05-15 20:05:1228
Roger McFarlanecba0e902024-01-24 17:51:3729constexpr size_t kArbitrarySize = 64 << 10;
30
31#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE)
32constexpr GlobalDescriptors::Key kArbitraryDescriptorKey = 42;
33#endif
34
35} // namespace
36
37TEST(HistogramSharedMemoryTest, Create) {
38 UnsafeSharedMemoryRegion region;
39
40 constexpr int kProcessId = 1234;
41 constexpr int kProcessType = 5678;
42 constexpr char kProcessName[] = "TestProcess";
43
44 auto shared_memory = HistogramSharedMemory::Create(
45 kProcessId, {kProcessType, kProcessName, kArbitrarySize});
46
47 ASSERT_TRUE(shared_memory.has_value());
48
49 ASSERT_TRUE(shared_memory->region.IsValid());
50 EXPECT_EQ(kArbitrarySize, shared_memory->region.GetSize());
51
52 ASSERT_TRUE(shared_memory->allocator);
53 EXPECT_EQ(kArbitrarySize, shared_memory->allocator->size());
Roger McFarlanec92516502023-05-15 20:05:1254}
55
Roger McFarlane2f246882025-01-22 16:12:1456namespace {
57// Constants from content::ProcessType;
58constexpr int PROCESS_TYPE_RENDERER = 3;
59constexpr int PROCESS_TYPE_GPU = 9;
60constexpr int PROCESS_TYPE_UTILITY = 6;
61} // namespace
62
63TEST(HistogramSharedMemoryTest, PassOnCommandLineIsDisabled) {
Roger McFarlanecba0e902024-01-24 17:51:3764 test::ScopedFeatureList feature_list;
65 feature_list.InitAndDisableFeature(kPassHistogramSharedMemoryOnLaunch);
66
Roger McFarlane2f246882025-01-22 16:12:1467 EXPECT_FALSE(
68 HistogramSharedMemory::PassOnCommandLineIsEnabled(PROCESS_TYPE_RENDERER));
69 EXPECT_FALSE(
70 HistogramSharedMemory::PassOnCommandLineIsEnabled(PROCESS_TYPE_GPU));
71 EXPECT_FALSE(
72 HistogramSharedMemory::PassOnCommandLineIsEnabled(PROCESS_TYPE_UTILITY));
73}
Roger McFarlanecba0e902024-01-24 17:51:3774
Roger McFarlane2f246882025-01-22 16:12:1475TEST(HistogramSharedMemoryTest, PassOnCommandLineIsEnabled) {
76 test::ScopedFeatureList feature_list;
77 feature_list.InitAndEnableFeature(kPassHistogramSharedMemoryOnLaunch);
Roger McFarlanecba0e902024-01-24 17:51:3778
Roger McFarlane2f246882025-01-22 16:12:1479 EXPECT_TRUE(
80 HistogramSharedMemory::PassOnCommandLineIsEnabled(PROCESS_TYPE_RENDERER));
81#if BUILDFLAG(IS_CHROMEOS)
82 EXPECT_FALSE(
83 HistogramSharedMemory::PassOnCommandLineIsEnabled(PROCESS_TYPE_GPU));
84#else // !BUILDFLAG(IS_CHROMEOS)
85 EXPECT_TRUE(
86 HistogramSharedMemory::PassOnCommandLineIsEnabled(PROCESS_TYPE_GPU));
87#endif // !BUILDFLAG(IS_CHROMEOS)
Roger McFarlanecba0e902024-01-24 17:51:3788
Roger McFarlane2f246882025-01-22 16:12:1489#if BUILDFLAG(IS_ANDROID)
90 EXPECT_FALSE(
91 HistogramSharedMemory::PassOnCommandLineIsEnabled(PROCESS_TYPE_UTILITY));
92#else // !BUILDFLAG(IS_ANDROID)
93 EXPECT_TRUE(
94 HistogramSharedMemory::PassOnCommandLineIsEnabled(PROCESS_TYPE_UTILITY));
95#endif // !BUILDFLAG(IS_ANDROID)
Roger McFarlanecba0e902024-01-24 17:51:3796}
97
98MULTIPROCESS_TEST_MAIN(InitFromLaunchParameters) {
99// On POSIX we generally use the descriptor map to look up inherited handles.
100// On most POSIX platforms we have to manually sure the mapping is updated,
101// for the purposes of this test.
102//