Search in sources :

Example 11 with Account

use of com.sun.jna.platform.win32.Advapi32Util.Account in project jna by java-native-access.

the class Advapi32Test method testLookupAccountSid.

public void testLookupAccountSid() {
    // get SID bytes
    String sidString = EVERYONE;
    PSIDByReference sid = new PSIDByReference();
    assertTrue("Failed to create sid", Advapi32.INSTANCE.ConvertStringSidToSid(sidString, sid));
    PSID value = sid.getValue();
    try {
        int sidLength = Advapi32.INSTANCE.GetLengthSid(value);
        assertTrue("Non-positive sid length", sidLength > 0);
        // lookup account
        IntByReference cchName = new IntByReference();
        IntByReference cchReferencedDomainName = new IntByReference();
        PointerByReference peUse = new PointerByReference();
        assertFalse(Advapi32.INSTANCE.LookupAccountSid(null, value, null, cchName, null, cchReferencedDomainName, peUse));
        assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
        assertTrue(cchName.getValue() > 0);
        assertTrue(cchReferencedDomainName.getValue() > 0);
        char[] referencedDomainName = new char[cchReferencedDomainName.getValue()];
        char[] name = new char[cchName.getValue()];
        assertTrue(Advapi32.INSTANCE.LookupAccountSid(null, value, name, cchName, referencedDomainName, cchReferencedDomainName, peUse));
        // SidTypeWellKnownGroup
        assertEquals(5, peUse.getPointer().getInt(0));
        String nameString = Native.toString(name);
        String referencedDomainNameString = Native.toString(referencedDomainName);
        assertTrue(nameString.length() > 0);
        if (AbstractWin32TestSupport.isEnglishLocale) {
            assertEquals("Everyone", nameString);
        }
        assertTrue(referencedDomainNameString.length() == 0);
    } finally {
        Kernel32Util.freeLocalMemory(value.getPointer());
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PSIDByReference(com.sun.jna.platform.win32.WinNT.PSIDByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) PSID(com.sun.jna.platform.win32.WinNT.PSID)

Aggregations

Account (com.sun.jna.platform.win32.Advapi32Util.Account)6 PSID (com.sun.jna.platform.win32.WinNT.PSID)4 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)3 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)3 IntByReference (com.sun.jna.ptr.IntByReference)3 USER_INFO_1 (com.sun.jna.platform.win32.LMAccess.USER_INFO_1)2 PointerByReference (com.sun.jna.ptr.PointerByReference)2 UserModel (com.gitblit.models.UserModel)1 Memory (com.sun.jna.Memory)1 Win32Exception (com.sun.jna.platform.win32.Win32Exception)1 PSIDByReference (com.sun.jna.platform.win32.WinNT.PSIDByReference)1 SID_AND_ATTRIBUTES (com.sun.jna.platform.win32.WinNT.SID_AND_ATTRIBUTES)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 IWindowsAccount (waffle.windows.auth.IWindowsAccount)1 IWindowsIdentity (waffle.windows.auth.IWindowsIdentity)1