View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.windows.wmi;
6   
7   import static org.hamcrest.MatcherAssert.assertThat;
8   import static org.hamcrest.Matchers.both;
9   import static org.hamcrest.Matchers.greaterThan;
10  import static org.hamcrest.Matchers.greaterThanOrEqualTo;
11  import static org.hamcrest.Matchers.is;
12  import static org.hamcrest.Matchers.lessThanOrEqualTo;
13  import static org.hamcrest.Matchers.notNullValue;
14  
15  import java.util.Set;
16  import java.util.stream.Collectors;
17  import java.util.stream.IntStream;
18  
19  import org.junit.jupiter.api.Test;
20  import org.junit.jupiter.api.condition.EnabledOnOs;
21  import org.junit.jupiter.api.condition.OS;
22  
23  import com.sun.jna.platform.win32.VersionHelpers;
24  import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
25  
26  import oshi.driver.windows.wmi.Win32LogicalDisk.LogicalDiskProperty;
27  import oshi.driver.windows.wmi.Win32Process.CommandLineProperty;
28  import oshi.util.platform.windows.WmiQueryHandler;
29  import oshi.util.platform.windows.WmiUtil;
30  
31  @EnabledOnOs(OS.WINDOWS)
32  class WMIDriversTest {
33  
34      @Test
35      void testQueryWMIMSAcpi() {
36          assertThat("Failed MSAcpiThermalZoneTemperature.queryCurrentTemperature",
37                  MSAcpiThermalZoneTemperature.queryCurrentTemperature().getResultCount(), is(greaterThanOrEqualTo(0)));
38      }
39  
40      @Test
41      void testQueryWMIMSFT() {
42          WmiQueryHandler handler = WmiQueryHandler.createInstance();
43          assertThat(handler, is(notNullValue()));
44          boolean comInit = handler.initCOM();
45  
46          assertThat("Failed MSFTStorage.queryPhysicalDisks", MSFTStorage.queryPhysicalDisks(handler).getResultCount(),
47                  is(greaterThanOrEqualTo(0)));
48          assertThat("Failed MSFTStorage.queryStoragePoolPhysicalDisks",
49                  MSFTStorage.queryStoragePoolPhysicalDisks(handler).getResultCount(), is(greaterThanOrEqualTo(0)));
50          assertThat("Failed MSFTStorage.queryStoragePools", MSFTStorage.queryStoragePools(handler).getResultCount(),
51                  is(greaterThanOrEqualTo(0)));
52          assertThat("Failed MSFTStorage.queryVirtualDisks", MSFTStorage.queryVirtualDisks(handler).getResultCount(),
53                  is(greaterThanOrEqualTo(0)));
54  
55          if (comInit) {
56              handler.unInitCOM();
57          }
58      }
59  
60      // OhmHardware and OhmSensor excluded
61      // We'll never have OHM running during testing
62  
63      @Test
64      void testQueryWMIWin32() {
65          WmiQueryHandler handler = WmiQueryHandler.createInstance();
66          assertThat(handler, is(notNullValue()));
67          boolean comInit = handler.initCOM();
68  
69          assertThat("Failed Win32DiskDrive.queryDiskDrive", Win32DiskDrive.queryDiskDrive(handler).getResultCount(),
70                  is(greaterThanOrEqualTo(0)));
71  
72          assertThat("Failed Win32DiskDriveToDiskPartition.queryDriveToPartition",
73                  Win32DiskDriveToDiskPartition.queryDriveToPartition(handler).getResultCount(),
74                  is(greaterThanOrEqualTo(0)));
75  
76          assertThat("Failed Win32DiskPartition.queryPartition",
77                  Win32DiskPartition.queryPartition(handler).getResultCount(), is(greaterThanOrEqualTo(0)));
78  
79          assertThat("Failed Win32LogicalDiskToPartition.queryDiskToPartition",
80                  Win32LogicalDiskToPartition.queryDiskToPartition(handler).getResultCount(),
81                  is(greaterThanOrEqualTo(0)));
82  
83          if (comInit) {
84              handler.unInitCOM();
85          }
86  
87          assertThat("Failed Win32BaseBoard.queryBaseboardInfo", Win32BaseBoard.queryBaseboardInfo().getResultCount(),
88                  is(greaterThan(0)));
89  
90          assertThat("Failed Win32Bios.queryBiosInfo", Win32Bios.queryBiosInfo().getResultCount(), is(greaterThan(0)));
91          assertThat("Failed Win32Bios.querySerialNumber", Win32Bios.querySerialNumber().getResultCount(),
92                  is(greaterThan(0)));
93  
94          assertThat("Failed Win32ComputerSystemProduct.queryIdentifyingNumberUUID",
95                  Win32ComputerSystemProduct.queryIdentifyingNumberUUID().getResultCount(), is(greaterThan(0)));
96  
97          assertThat("Failed Win32Fan.querySpeed", Win32Fan.querySpeed().getResultCount(), is(greaterThanOrEqualTo(0)));
98  
99          WmiResult<LogicalDiskProperty> ld = Win32LogicalDisk.queryLogicalDisk(null, false);
100         assertThat("Failed Win32LogicalDisk.queryLogicalDisk", ld.getResultCount(), is(greaterThan(0)));
101         assertThat(
102                 "Failed Win32LogicalDisk.queryLogicalDisk", Win32LogicalDisk
103                         .queryLogicalDisk(WmiUtil.getString(ld, LogicalDiskProperty.NAME, 0), false).getResultCount(),
104                 is(greaterThan(0)));
105         assertThat("Failed Win32LogicalDisk.queryLogicalDisk",
106                 Win32LogicalDisk.queryLogicalDisk(null, true).getResultCount(), is(greaterThan(0)));
107 
108         assertThat("Failed Win32OperatingSystem.queryOsVersion", Win32OperatingSystem.queryOsVersion().getResultCount(),
109                 is(greaterThan(0)));
110 
111         if (VersionHelpers.IsWindows10OrGreater()) {
112             assertThat("Failed Win32PhysicalMemory.queryphysicalMemory",
113                     Win32PhysicalMemory.queryphysicalMemory().getResultCount(), is(greaterThan(0)));
114         } else {
115             assertThat("Failed Win32PhysicalMemory.queryphysicalMemoryWin8",
116                     Win32PhysicalMemory.queryphysicalMemoryWin8().getResultCount(), is(greaterThan(0)));
117         }
118 
119         WmiResult<CommandLineProperty> cl = Win32Process.queryCommandLines(null);
120         assertThat("Failed Win32Process.queryCommandLines", cl.getResultCount(), is(greaterThan(0)));
121         Set<Integer> clset = IntStream.range(0, cl.getResultCount())
122                 .map(i -> WmiUtil.getUint32(cl, CommandLineProperty.PROCESSID, i)).boxed().collect(Collectors.toSet());
123         assertThat("Failed Win32Process.queryProcesses", Win32Process.queryProcesses(clset).getResultCount(),
124                 is(both(greaterThan(0)).and(lessThanOrEqualTo(clset.size()))));
125 
126         Win32ProcessCached cache = Win32ProcessCached.getInstance();
127         assertThat("Failed Win32ProcessCached.getCommandLine",
128                 cache.getCommandLine(WmiUtil.getUint32(cl, CommandLineProperty.PROCESSID, 0), 0L), is(notNullValue()));
129 
130         assertThat("Failed Win32Processor.queryBitness", Win32Processor.queryBitness().getResultCount(),
131                 is(greaterThan(0)));
132         assertThat("Failed Win32Processor.queryProcessorId", Win32Processor.queryProcessorId().getResultCount(),
133                 is(greaterThan(0)));
134         assertThat("Failed Win32Processor.queryVoltage", Win32Processor.queryVoltage().getResultCount(),
135                 is(greaterThan(0)));
136 
137         assertThat("Failed Win32VideoController.queryVideoController",
138                 Win32VideoController.queryVideoController().getResultCount(), is(greaterThanOrEqualTo(0)));
139     }
140 }