Skip to content

Commit

Permalink
Update tests for new exceptions and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Aug 26, 2018
1 parent 2d2caea commit 8b5c3a3
Showing 1 changed file with 31 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna.platform.win32;
package com.sun.jna.platform.win32.COM;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -36,10 +36,12 @@
import org.junit.Before;
import org.junit.Test;

import com.sun.jna.platform.win32.Ole32;
import com.sun.jna.platform.win32.Variant;
import com.sun.jna.platform.win32.COM.COMException;
import com.sun.jna.platform.win32.COM.COMUtils;
import com.sun.jna.platform.win32.COM.Wbemcli;
import com.sun.jna.platform.win32.COM.WbemcliUtil;
import com.sun.jna.platform.win32.COM.Wbemcli.WbemcliException;
import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;

Expand Down Expand Up @@ -99,26 +101,26 @@ public void testWmiExceptions() {
try {
WbemcliUtil.queryWMI(processQuery);
fail("Win32_ClassDoesNotExist does not exist.");
} catch (WbemcliException expected) {
assertEquals(Wbemcli.WBEM_E_INVALID_CLASS, expected.getErrorCode());
} catch (COMException expected) {
assertEquals(Wbemcli.WBEM_E_INVALID_CLASS, expected.getHresult());
}

// Valid class but properties don't match the class
processQuery.setWmiClassName("Win32_OperatingSystem");
try {
WbemcliUtil.queryWMI(processQuery);
fail("Properties in the process enum aren't in Win32_OperatingSystem");
} catch (WbemcliException expected) {
assertEquals(Wbemcli.WBEM_E_INVALID_QUERY, expected.getErrorCode());
} catch (COMException expected) {
assertEquals(Wbemcli.WBEM_E_INVALID_QUERY, expected.getHresult());
}

// Invalid namespace
processQuery.setNameSpace("Invalid");
try {
WbemcliUtil.queryWMI(processQuery);
fail("This is an invalid namespace.");
} catch (WbemcliException expected) {
assertEquals(Wbemcli.WBEM_E_INVALID_NAMESPACE, expected.getErrorCode());
} catch (COMException expected) {
assertEquals(Wbemcli.WBEM_E_INVALID_NAMESPACE, expected.getHresult());
}
}

Expand All @@ -137,56 +139,30 @@ public void testWmiProcesses() {
assertTrue(processes.getResultCount() > 0);
int lastProcessIndex = processes.getResultCount() - 1;

// PID is UINT32 = VT_I4, should only work with getInteger
assertTrue(processes.getInteger(ProcessProperty.PROCESSID, lastProcessIndex) >= 0);
// Other types should throw exception with error code VT_I4 = 3
try {
processes.getString(ProcessProperty.PROCESSID, lastProcessIndex);
fail("VT_I4 type cannot be returned as a String");
} catch (WbemcliException expected) {
assertEquals(Variant.VT_I4, expected.getErrorCode());
}
// PID is UINT32 = VT_I4
assertEquals(Variant.VT_I4, processes.getVtType(ProcessProperty.PROCESSID));
assertTrue((Integer) processes.getValue(ProcessProperty.PROCESSID, lastProcessIndex) >= 0);

// WSS is UINT64 = STRING, should only work with getString and parse to
// a long
String wssStr = processes.getString(ProcessProperty.WORKINGSETSIZE, lastProcessIndex);
// WSS is UINT64 = STRING
assertEquals(Variant.VT_BSTR, processes.getVtType(ProcessProperty.WORKINGSETSIZE));
String wssStr = (String) processes.getValue(ProcessProperty.WORKINGSETSIZE, lastProcessIndex);
assertTrue(Long.parseLong(wssStr) > 0);
// Other types should throw exception with error code VT_BSTR = 8
try {
processes.getInteger(ProcessProperty.WORKINGSETSIZE, lastProcessIndex);
fail("VT_BSTR type cannot be returned as an Integer");
} catch (WbemcliException expected) {
assertEquals(Variant.VT_BSTR, expected.getErrorCode());
}

// EXECUTIONSTATE is always null, should only work with getValue
// EXECUTIONSTATE is always null
assertEquals(Variant.VT_NULL, processes.getVtType(ProcessProperty.EXECUTIONSTATE));
Object state = processes.getValue(ProcessProperty.EXECUTIONSTATE, lastProcessIndex);
assertNull(state);
// Other types should return defaults
assertEquals("", processes.getString(ProcessProperty.EXECUTIONSTATE, lastProcessIndex));
assertEquals((Integer) 0, processes.getInteger(ProcessProperty.EXECUTIONSTATE, lastProcessIndex));
assertEquals((Short) (short) 0, processes.getShort(ProcessProperty.EXECUTIONSTATE, lastProcessIndex));
assertEquals((Byte) (byte) 0, processes.getByte(ProcessProperty.EXECUTIONSTATE, lastProcessIndex));
assertEquals((Float) 0f, processes.getFloat(ProcessProperty.EXECUTIONSTATE, lastProcessIndex));
assertEquals((Double) 0d, processes.getDouble(ProcessProperty.EXECUTIONSTATE, lastProcessIndex));
assertEquals(Boolean.FALSE, processes.getBoolean(ProcessProperty.EXECUTIONSTATE, lastProcessIndex));

// CreationDate is DATETIME = STRING, should only work with getString

// CreationDate is DATETIME = STRING
// and be in CIM_DATETIME format yyyymmddhhmmss.mmmmmm+zzz
String cdate = processes.getString(ProcessProperty.CREATIONDATE, lastProcessIndex);
assertEquals(Variant.VT_BSTR, processes.getVtType(ProcessProperty.CREATIONDATE));
String cdate = (String) processes.getValue(ProcessProperty.CREATIONDATE, lastProcessIndex);

assertEquals(25, cdate.length());
assertEquals('.', cdate.charAt(14));
assertTrue(Integer.parseInt(cdate.substring(0, 4)) > 1970);
assertTrue(Integer.parseInt(cdate.substring(4, 6)) <= 12);
assertTrue(Integer.parseInt(cdate.substring(6, 8)) <= 31);
// Other types should throw exception with error code VT_BSTR = 8
try {
processes.getFloat(ProcessProperty.CREATIONDATE, lastProcessIndex);
fail("VT_BSTR type cannot be returned as a Float");
} catch (WbemcliException expected) {
assertEquals(Variant.VT_BSTR, expected.getErrorCode());
}
}

@Test
Expand All @@ -198,35 +174,16 @@ public void testWmiOperatingSystem() {
// There has to be at least one os (this one!)
assertTrue(os.getResultCount() > 0);

// ForegroundApplicationBoost is UINT8 = VT_UI1, should only work with
// getByte
assertTrue(os.getByte(OperatingSystemProperty.FOREGROUNDAPPLICATIONBOOST, 0) >= 0);
// Other types should throw exception with error code VT_UI1 = 17
try {
os.getInteger(OperatingSystemProperty.FOREGROUNDAPPLICATIONBOOST, 0);
fail("VT_UI1 type cannot be returned as an Integer");
} catch (WbemcliException expected) {
assertEquals(Variant.VT_UI1, expected.getErrorCode());
}
// ForegroundApplicationBoost is UINT8 = VT_UI1
assertEquals(Variant.VT_UI1, os.getVtType(OperatingSystemProperty.FOREGROUNDAPPLICATIONBOOST));
assertTrue((Byte) os.getValue(OperatingSystemProperty.FOREGROUNDAPPLICATIONBOOST, 0) >= 0);

// OSTYPE is UINT16 = VT_I4, should only work with getInteger
assertTrue(os.getInteger(OperatingSystemProperty.OSTYPE, 0) >= 0);
// Other types should throw exception with error code VT_I4 = 3
try {
os.getByte(OperatingSystemProperty.OSTYPE, 0);
fail("VT_I4 type cannot be returned as a Byte");
} catch (WbemcliException expected) {
assertEquals(Variant.VT_I4, expected.getErrorCode());
}
// OSTYPE is UINT16 = VT_I4
assertEquals(Variant.VT_I4, os.getVtType(OperatingSystemProperty.OSTYPE));
assertTrue((Integer) os.getValue(OperatingSystemProperty.OSTYPE, 0) >= 0);

// PRIMARY is BOOLEAN = VT_BOOL, should only work with getBoolean
assertNotNull(os.getValue(OperatingSystemProperty.PRIMARY, 0));
// Other types should throw exception with error code VT_BOOL = 11
try {
os.getDouble(OperatingSystemProperty.PRIMARY, 0);
fail("VT_BOOL type cannot be returned as a Double");
} catch (WbemcliException expected) {
assertEquals(Variant.VT_BOOL, expected.getErrorCode());
}
// PRIMARY is BOOLEAN = VT_BOOL
assertEquals(Variant.VT_BOOL, os.getVtType(OperatingSystemProperty.PRIMARY));
assertNotNull((Boolean) os.getValue(OperatingSystemProperty.PRIMARY, 0));
}
}

0 comments on commit 8b5c3a3

Please sign in to comment.