Skip to content

Commit

Permalink
Final changes based on feedback
Browse files Browse the repository at this point in the history
* Changed Privilege constructor
* Changed enable interface to return this
* Removed try with resources
* Fixed javadoc
  • Loading branch information
amarcionek committed Dec 6, 2016
1 parent d8a8438 commit bfd9087
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2661,11 +2661,10 @@ public static class Privilege implements Closeable {

/**
* Construct and enable a set of privileges
* @param privileges the name of the privileges in the form of SE_* from Advapi32.java
* @enable if true, enable the privilege immediately.
* @param privileges the names of the privileges in the form of SE_* from Advapi32.java
* @throws IllegalArgumentException
*/
public Privilege(String[] privileges, boolean enable) throws IllegalArgumentException, Win32Exception {
public Privilege(String... privileges) throws IllegalArgumentException, Win32Exception {
pLuids = new WinNT.LUID[privileges.length];
int i = 0;
for (String p : privileges) {
Expand All @@ -2675,12 +2674,10 @@ public Privilege(String[] privileges, boolean enable) throws IllegalArgumentExce
}
i++;
}
if (enable)
this.enable();
}

/**
* Calls {@link#disable} to remove the privileges
* Calls disable() to remove the privileges
* @see java.io.Closeable#close()
*/
@Override
Expand All @@ -2695,10 +2692,10 @@ public void close() {
* @return pointer to self (Privilege) as a convenience for try with resources statements
* @throws Win32Exception
*/
public void enable() throws Win32Exception {
public Privilege enable() throws Win32Exception {
// Ignore if already enabled.
if (privilegesEnabled)
return;
return this;

// Get thread token
final HANDLEByReference phThreadToken = new HANDLEByReference();
Expand Down Expand Up @@ -2740,6 +2737,7 @@ public void enable() throws Win32Exception {
phThreadToken.setValue(null);
}
}
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sun.jna.platform.win32.Advapi32Util.Account;
import com.sun.jna.platform.win32.Advapi32Util.EventLogIterator;
import com.sun.jna.platform.win32.Advapi32Util.EventLogRecord;
import com.sun.jna.platform.win32.Advapi32Util.Privilege;
import com.sun.jna.platform.win32.LMAccess.USER_INFO_1;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
Expand Down Expand Up @@ -604,20 +605,28 @@ public void testBackupEncryptedFile() throws Exception {
*/
public void testPrivilege() {
// Test multiple known privileges
try(Advapi32Util.Privilege p = new Advapi32Util.Privilege(new String[] { WinNT.SE_ASSIGNPRIMARYTOKEN_NAME, WinNT.SE_BACKUP_NAME }, true);) {
Privilege privilege = new Privilege(WinNT.SE_ASSIGNPRIMARYTOKEN_NAME, WinNT.SE_BACKUP_NAME);
try {
privilege.enable();
// Will throw if it fails p.enable() fails
}
finally {
privilege.close();
}

// Test unknown privilege
try(Advapi32Util.Privilege p = new Advapi32Util.Privilege(new String[] { "NOT_A_PRIVILEGE"}, true);) {
// Will throw if it fails p.enable() fails
try {
privilege = new Privilege("NOT_A_PRIVILEGE");
}
catch (IllegalArgumentException ex) {
// Exception is expected
}
catch (Exception ex) {
fail("Encountered unknown exception - " + ex.getMessage());
}
finally {
privilege.close();
}
}

private File createTempFile() throws Exception{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,9 @@ public void testDeviceIoControlFsctlReparse() throws IOException {
delLink.deleteOnExit();

// Required for FSCTL_SET_REPARSE_POINT
try(Advapi32Util.Privilege restore = new Advapi32Util.Privilege(new String[] { WinNT.SE_RESTORE_NAME }, true)) {

Advapi32Util.Privilege restore = new Advapi32Util.Privilege(WinNT.SE_RESTORE_NAME);
try {
restore.enable();
HANDLE hFile = Kernel32.INSTANCE.CreateFile(link.toAbsolutePath().toString(),
WinNT.GENERIC_READ | WinNT.FILE_WRITE_ATTRIBUTES | WinNT.FILE_WRITE_EA,
WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE | WinNT.FILE_SHARE_DELETE,
Expand Down Expand Up @@ -693,6 +694,9 @@ public void testDeviceIoControlFsctlReparse() throws IOException {
Kernel32Util.closeHandle(hFile);
}
}
finally {
restore.close();
}
}

public void testCopyFile() throws IOException {
Expand Down

0 comments on commit bfd9087

Please sign in to comment.