File: user_stream_data_source_win_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,608 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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 "components/stability_report/user_stream_data_source_win.h"

#include <memory>
#include <tuple>
#include <utility>

#include "base/process/memory.h"
#include "base/process/process.h"
#include "components/stability_report/stability_report.pb.h"
#include "components/stability_report/test/stability_report_reader.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/crashpad/crashpad/snapshot/test/test_exception_snapshot.h"
#include "third_party/crashpad/crashpad/snapshot/test/test_process_snapshot.h"

namespace stability_report {

namespace {

constexpr uint64_t kExpectedAllocationAttempt = 12345;

using ::testing::Bool;
using ::testing::Combine;
using ::testing::TestWithParam;
using ::testing::Values;

enum class ExceptionCode {
  // No ExceptionSnapshot.
  kNone,
  // ExceptionSnapshot has a non-OOM code.
  kNotOOM,
  // ExceptionSnapshot with kOomExceptionCode and allocation info in Codes().
  kOOMWithAllocation,
  // ExceptionSnapshot with kOomExceptionCode but no allocation info in Codes().
  kOOMWithoutAllocation,
};

class StabilityReportUserStreamDataSourceTest
    : public TestWithParam<std::tuple<bool, ExceptionCode>> {
 protected:
  StabilityReportUserStreamDataSourceTest() {
    std::tie(is_valid_process_, exception_code_) = GetParam();
  }

  bool is_valid_process_;
  ExceptionCode exception_code_;
};

INSTANTIATE_TEST_SUITE_P(All,
                         StabilityReportUserStreamDataSourceTest,
                         Combine(Bool(),
                                 Values(ExceptionCode::kNone,
                                        ExceptionCode::kNotOOM,
                                        ExceptionCode::kOOMWithAllocation,
                                        ExceptionCode::kOOMWithoutAllocation)));

TEST_P(StabilityReportUserStreamDataSourceTest, ReadProcess) {
  crashpad::test::TestProcessSnapshot process_snapshot;
  process_snapshot.SetProcessID(is_valid_process_
                                    ? base::Process::Current().Pid()
                                    : base::kNullProcessId);
  if (exception_code_ != ExceptionCode::kNone) {
    auto exception_snapshot =
        std::make_unique<crashpad::test::TestExceptionSnapshot>();
    switch (exception_code_) {
      case ExceptionCode::kNotOOM:
        // Set an arbitrary error code.
        exception_snapshot->SetException(ERROR_INVALID_HANDLE);
        exception_snapshot->SetCodes({kExpectedAllocationAttempt});
        break;
      case ExceptionCode::kOOMWithAllocation:
        exception_snapshot->SetException(base::win::kOomExceptionCode);
        exception_snapshot->SetCodes({kExpectedAllocationAttempt});
        break;
      case ExceptionCode::kOOMWithoutAllocation:
        exception_snapshot->SetException(base::win::kOomExceptionCode);
        exception_snapshot->SetCodes({});
        break;
      case ExceptionCode::kNone:
        // Should not be reached.
        FAIL();
    }
    process_snapshot.SetException(std::move(exception_snapshot));
  }

  // Collect a StabilityReport from `process_snapshot`.
  UserStreamDataSourceWin source;
  std::unique_ptr<crashpad::MinidumpUserExtensionStreamDataSource> data_source =
      source.ProduceStreamData(&process_snapshot);
  ASSERT_TRUE(data_source);

  // Read the StabilityReport back out of the stream data.
  test::StabilityReportReader reader;
  ASSERT_TRUE(data_source->ReadStreamData(&reader));

  // Validate ProcessState.
  ASSERT_EQ(reader.report().process_states_size(), 1);
  const ProcessState& process_state = reader.report().process_states(0);
  EXPECT_EQ(process_state.process_id(), process_snapshot.ProcessID());
  ASSERT_TRUE(process_state.has_memory_state());
  ASSERT_TRUE(process_state.memory_state().has_windows_memory());
  const ProcessState::MemoryState::WindowsMemory& process_memory =
      process_state.memory_state().windows_memory();
  const ProcessState::FileSystemState::WindowsFileSystemState& process_fs =
      process_state.file_system_state().windows_file_system_state();

  // Memory stats are only filled in if the process could be queried.
  if (is_valid_process_) {
    EXPECT_GT(process_memory.process_private_usage(), 0U);
    EXPECT_GT(process_memory.process_peak_workingset_size(), 0U);
    EXPECT_GT(process_memory.process_peak_pagefile_usage(), 0U);
    EXPECT_GT(process_fs.process_handle_count(), 0U);
  } else {
    EXPECT_FALSE(process_memory.has_process_private_usage());
    EXPECT_FALSE(process_memory.has_process_peak_workingset_size());
    EXPECT_FALSE(process_memory.has_process_peak_pagefile_usage());
    EXPECT_FALSE(process_fs.has_process_handle_count());
  }

  // Allocation attempt is only set on OOM when all info is available.
  if (exception_code_ == ExceptionCode::kOOMWithAllocation) {
    EXPECT_EQ(process_memory.process_allocation_attempt(),
              kExpectedAllocationAttempt);
  } else {
    EXPECT_FALSE(process_memory.has_process_allocation_attempt());
  }

  // Validate SystemMemoryState.
  ASSERT_TRUE(reader.report().has_system_memory_state());
  ASSERT_TRUE(reader.report().system_memory_state().has_windows_memory());
  const SystemMemoryState::WindowsMemory& system_memory =
      reader.report().system_memory_state().windows_memory();
  EXPECT_GT(system_memory.system_commit_limit(), 0U);
  EXPECT_GT(system_memory.system_commit_remaining(), 0U);
  EXPECT_GT(system_memory.system_handle_count(), 0U);
}

}  // namespace

}  // namespace stability_report