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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/windows_services/elevated_tracing_service/system_tracing_session.h"
#include <stdint.h>
#include "base/containers/heap_array.h"
#include "base/memory/scoped_refptr.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/test/task_environment.h"
#include "base/win/scoped_com_initializer.h"
#include "base/win/win_util.h"
#include "chrome/common/win/eventlog_messages.h"
#include "chrome/install_static/install_util.h"
#include "chrome/windows_services/elevated_tracing_service/elevated_tracing_service_delegate.h"
#include "chrome/windows_services/elevated_tracing_service/session_registry.h"
#include "chrome/windows_services/service_program/service.h"
#include "chrome/windows_services/service_program/test_support/scoped_mock_context.h"
#include "mojo/public/cpp/platform/named_platform_channel.h"
#include "mojo/public/cpp/system/invitation.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class DummyOuter
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
IUnknown> {
public:
using Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
IUnknown>::CastToUnknown;
};
} // namespace
class SystemTracingSessionTest : public ::testing::Test {
protected:
SystemTracingSessionTest() = default;
void SetUp() override {
ASSERT_TRUE(scoped_com_initializer_.Succeeded());
ASSERT_HRESULT_SUCCEEDED(
Service::RegisterClassObjects(service_delegate_, {}, cookies_));
}
void TearDown() override { Service::UnregisterClassObjects(cookies_); }
elevated_tracing_service::Delegate& service_delegate() {
return service_delegate_;
}
private:
base::test::TaskEnvironment task_environment_;
base::win::ScopedCOMInitializer scoped_com_initializer_;
scoped_refptr<elevated_tracing_service::SessionRegistry> session_registry_{
base::MakeRefCounted<elevated_tracing_service::SessionRegistry>()};
elevated_tracing_service::Delegate service_delegate_;
base::HeapArray<DWORD> cookies_;
};
TEST_F(SystemTracingSessionTest, GetLogEventCategory) {
ASSERT_EQ(service_delegate().GetLogEventCategory(), TRACING_SERVICE_CATEGORY);
}
TEST_F(SystemTracingSessionTest, GetLogEventMessageId) {
ASSERT_EQ(service_delegate().GetLogEventMessageId(),
MSG_TRACING_SERVICE_LOG_MESSAGE);
}
TEST_F(SystemTracingSessionTest, NoAggregation) {
ScopedMockContext mock_context;
ASSERT_TRUE(mock_context.Succeeded());
auto dummy_outer = Microsoft::WRL::Make<DummyOuter>();
ASSERT_TRUE(bool(dummy_outer));
Microsoft::WRL::ComPtr<IUnknown> unknown;
ASSERT_EQ(::CoCreateInstance(install_static::GetTracingServiceClsid(),
dummy_outer->CastToUnknown(),
CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&unknown)),
CLASS_E_NOAGGREGATION);
}
// Tests that AcceptInvitation returns E_INVALIDARG as appropriate.
TEST_F(SystemTracingSessionTest, AcceptInvitation) {
ScopedMockContext mock_context;
ASSERT_TRUE(mock_context.Succeeded());
Microsoft::WRL::ComPtr<IUnknown> unknown;
ASSERT_HRESULT_SUCCEEDED(
::CoCreateInstance(install_static::GetTracingServiceClsid(), nullptr,
CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&unknown)));
Microsoft::WRL::ComPtr<ISystemTraceSession> trace_session;
ASSERT_HRESULT_SUCCEEDED(unknown.As(&trace_session));
unknown.Reset();
DWORD pid = base::kNullProcessId;
ASSERT_EQ(trace_session->AcceptInvitation(nullptr, &pid), E_INVALIDARG);
ASSERT_EQ(trace_session->AcceptInvitation(L"", &pid), E_INVALIDARG);
ASSERT_EQ(trace_session->AcceptInvitation(L"invalid", nullptr), E_INVALIDARG);
}
|