xref: /aosp_15_r20/external/cronet/base/android/pmf_utils_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2024 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/android/pmf_utils.h"
6 
7 #include "base/files/file_util.h"
8 #include "build/build_config.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace base::android {
12 
TEST(PmfUtilsTest,CalculatePrivateMemoryFootprint)13 TEST(PmfUtilsTest, CalculatePrivateMemoryFootprint) {
14   const char kStatusFile[] =
15       "First:    1\n"
16       "Second:  2 kB\n"
17       "VmSwap: 10 kB\n"
18       "Third:  10 kB\n"
19       "VmHWM:  72 kB\n"
20       "Last:     8";
21   const char kStatmFile[] = "100 40 25 0 0";
22   const uint64_t expected_swap_kb = 10;
23   const uint64_t expected_pmf =
24       (40 - 25) * getpagesize() / 1024 + expected_swap_kb;
25 
26   base::FilePath statm_path;
27   EXPECT_TRUE(base::CreateTemporaryFile(&statm_path));
28   EXPECT_TRUE(base::WriteFile(statm_path, kStatmFile));
29   base::FilePath status_path;
30   EXPECT_TRUE(base::CreateTemporaryFile(&status_path));
31   EXPECT_TRUE(base::WriteFile(status_path, kStatusFile));
32 
33   base::File statm_file(statm_path,
34                         base::File::FLAG_OPEN | base::File::FLAG_READ);
35   base::File status_file(status_path,
36                          base::File::FLAG_OPEN | base::File::FLAG_READ);
37 
38   std::optional<uint64_t> pmf =
39       PmfUtils::CalculatePrivateMemoryFootprintForTesting(statm_file,
40                                                           status_file);
41 
42   EXPECT_TRUE(pmf.has_value());
43   EXPECT_EQ(expected_pmf, pmf.value() / 1024);
44 }
45 
46 }  // namespace base::android
47