xref: /aosp_15_r20/external/cronet/base/win/wmi_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2006-2008 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/win/wmi.h"
6 
7 #include <windows.h>
8 
9 #include "base/win/scoped_com_initializer.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 using Microsoft::WRL::ComPtr;
13 
14 namespace base {
15 namespace win {
16 
17 class WMITest : public ::testing::Test {
18  private:
19   ScopedCOMInitializer com_initializer;
20 };
21 
TEST_F(WMITest,TestLocalConnectionSecurityBlanket)22 TEST_F(WMITest, TestLocalConnectionSecurityBlanket) {
23   ComPtr<IWbemServices> wmi_services = nullptr;
24   EXPECT_TRUE(CreateLocalWmiConnection(true, &wmi_services));
25   ASSERT_NE(wmi_services.Get(), nullptr);
26   ULONG refs = wmi_services.Reset();
27   EXPECT_EQ(0u, refs);
28 }
29 
TEST_F(WMITest,TestLocalConnectionNoSecurityBlanket)30 TEST_F(WMITest, TestLocalConnectionNoSecurityBlanket) {
31   ComPtr<IWbemServices> wmi_services = nullptr;
32   EXPECT_TRUE(CreateLocalWmiConnection(false, &wmi_services));
33   ASSERT_NE(wmi_services.Get(), nullptr);
34   ULONG refs = wmi_services.Reset();
35   EXPECT_EQ(0u, refs);
36 }
37 
TEST_F(WMITest,TestCreateClassMethod)38 TEST_F(WMITest, TestCreateClassMethod) {
39   ComPtr<IWbemServices> wmi_services = nullptr;
40   EXPECT_TRUE(CreateLocalWmiConnection(true, &wmi_services));
41   ASSERT_NE(wmi_services.Get(), nullptr);
42   ComPtr<IWbemClassObject> class_method = nullptr;
43   EXPECT_TRUE(CreateWmiClassMethodObject(
44       wmi_services.Get(), L"Win32_ShortcutFile", L"Rename", &class_method));
45   ASSERT_NE(class_method.Get(), nullptr);
46   ULONG refs = class_method.Reset();
47   EXPECT_EQ(0u, refs);
48   refs = wmi_services.Reset();
49   EXPECT_EQ(0u, refs);
50 }
51 
52 // Creates an instance of cmd which executes 'echo' and exits immediately.
TEST_F(WMITest,TestLaunchProcess)53 TEST_F(WMITest, TestLaunchProcess) {
54   int pid = 0;
55   bool result = WmiLaunchProcess(L"cmd.exe /c echo excelent!", &pid);
56   EXPECT_TRUE(result);
57   EXPECT_GT(pid, 0);
58 }
59 
TEST_F(WMITest,TestComputerSystemInfo)60 TEST_F(WMITest, TestComputerSystemInfo) {
61   WmiComputerSystemInfo info = WmiComputerSystemInfo::Get();
62   EXPECT_FALSE(info.serial_number().empty());
63 }
64 
65 }  // namespace win
66 }  // namespace base
67