-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize support for access rights in
windows-registry
(#3482)
- Loading branch information
Showing
7 changed files
with
149 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use windows_registry::*; | ||
use windows_result::*; | ||
use windows_sys::Win32::{Foundation::*, System::Registry::*}; | ||
|
||
#[test] | ||
fn access() { | ||
let test_key = "software\\windows-rs\\tests\\access"; | ||
_ = CURRENT_USER.remove_tree(test_key); | ||
|
||
let key = CURRENT_USER | ||
.options() | ||
.create() | ||
.access(KEY_WRITE) | ||
.open(test_key) | ||
.unwrap(); | ||
|
||
key.set_u64("u64", 123u64).unwrap(); | ||
|
||
assert_eq!( | ||
key.get_u64("u64").unwrap_err().code(), | ||
HRESULT::from_win32(ERROR_ACCESS_DENIED) | ||
); | ||
|
||
let key = CURRENT_USER | ||
.options() | ||
.access(KEY_READ) | ||
.open(test_key) | ||
.unwrap(); | ||
|
||
assert_eq!(key.get_u64("u64").unwrap(), 123u64); | ||
|
||
assert_eq!( | ||
key.set_u64("u64", 123u64).unwrap_err().code(), | ||
HRESULT::from_win32(ERROR_ACCESS_DENIED) | ||
); | ||
} | ||
|
||
#[test] | ||
fn flags() { | ||
// `OpenOptions` defaults to no access | ||
let mut options = CURRENT_USER.options(); | ||
assert_eq!(get_access(&options), 0); | ||
|
||
// `read` and `write` equate to `KEY_READ` and `KEY_WRITE` | ||
options.read().write(); | ||
assert_eq!(get_access(&options), KEY_READ | KEY_WRITE); | ||
|
||
// Combine additional access rights | ||
options.access(KEY_WOW64_32KEY); | ||
assert_eq!(get_access(&options), KEY_WOW64_32KEY | KEY_READ | KEY_WRITE); | ||
|
||
// Start with specific access rights | ||
let mut options = CURRENT_USER.options(); | ||
options.access(KEY_WOW64_32KEY | KEY_QUERY_VALUE); | ||
assert_eq!(get_access(&options), KEY_WOW64_32KEY | KEY_QUERY_VALUE); | ||
|
||
// `read` is additive | ||
options.read(); | ||
assert_eq!( | ||
get_access(&options), | ||
KEY_WOW64_32KEY | KEY_QUERY_VALUE | KEY_READ | ||
); | ||
|
||
// `write` is additive | ||
options.write(); | ||
assert_eq!( | ||
get_access(&options), | ||
KEY_WOW64_32KEY | KEY_QUERY_VALUE | KEY_READ | KEY_WRITE | ||
); | ||
} | ||
|
||
fn get_access(options: &OpenOptions) -> u32 { | ||
regex::Regex::new(r#"access:\s*(\d+)"#) | ||
.unwrap() | ||
.captures(&format!("{options:?}")) | ||
.unwrap() | ||
.get(1) | ||
.unwrap() | ||
.as_str() | ||
.parse() | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use windows_registry::*; | ||
use windows_result::*; | ||
use windows_sys::Win32::Foundation::*; | ||
|
||
#[test] | ||
fn read_write() { | ||
let test_key = "software\\windows-rs\\tests\\read_write"; | ||
_ = CURRENT_USER.remove_tree(test_key); | ||
|
||
let key = CURRENT_USER | ||
.options() | ||
.create() | ||
.write() | ||
.open(test_key) | ||
.unwrap(); | ||
|
||
key.set_u64("u64", 123u64).unwrap(); | ||
|
||
assert_eq!( | ||
key.get_u64("u64").unwrap_err().code(), | ||
HRESULT::from_win32(ERROR_ACCESS_DENIED) | ||
); | ||
|
||
let key = CURRENT_USER.options().read().open(test_key).unwrap(); | ||
|
||
assert_eq!(key.get_u64("u64").unwrap(), 123u64); | ||
|
||
assert_eq!( | ||
key.set_u64("u64", 123u64).unwrap_err().code(), | ||
HRESULT::from_win32(ERROR_ACCESS_DENIED) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters