Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 1 | // 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 McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 6 | |
| 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 McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 19 | #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 McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 25 | |
| 26 | namespace base { |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 27 | namespace { |
Roger McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 28 | |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 29 | constexpr size_t kArbitrarySize = 64 << 10; |
| 30 | |
| 31 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE) |
| 32 | constexpr GlobalDescriptors::Key kArbitraryDescriptorKey = 42; |
| 33 | #endif |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | TEST(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 McFarlane | c9251650 | 2023-05-15 20:05:12 | [diff] [blame] | 54 | } |
| 55 | |
Roger McFarlane | 2f24688 | 2025-01-22 16:12:14 | [diff] [blame] | 56 | namespace { |
| 57 | // Constants from content::ProcessType; |
| 58 | constexpr int PROCESS_TYPE_RENDERER = 3; |
| 59 | constexpr int PROCESS_TYPE_GPU = 9; |
| 60 | constexpr int PROCESS_TYPE_UTILITY = 6; |
| 61 | } // namespace |
| 62 | |
| 63 | TEST(HistogramSharedMemoryTest, PassOnCommandLineIsDisabled) { |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 64 | test::ScopedFeatureList feature_list; |
| 65 | feature_list.InitAndDisableFeature(kPassHistogramSharedMemoryOnLaunch); |
| 66 | |
Roger McFarlane | 2f24688 | 2025-01-22 16:12:14 | [diff] [blame] | 67 | 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 McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 74 | |
Roger McFarlane | 2f24688 | 2025-01-22 16:12:14 | [diff] [blame] | 75 | TEST(HistogramSharedMemoryTest, PassOnCommandLineIsEnabled) { |
| 76 | test::ScopedFeatureList feature_list; |
| 77 | feature_list.InitAndEnableFeature(kPassHistogramSharedMemoryOnLaunch); |
Roger McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 78 | |
Roger McFarlane | 2f24688 | 2025-01-22 16:12:14 | [diff] [blame] | 79 | 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 McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 88 | |
Roger McFarlane | 2f24688 | 2025-01-22 16:12:14 | [diff] [blame] | 89 | #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 McFarlane | cba0e90 | 2024-01-24 17:51:37 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | MULTIPROCESS_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 | // |
|
|