Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AllUsers and AllGroups #11

Merged
merged 9 commits into from
Nov 1, 2023
40 changes: 40 additions & 0 deletions examples/mocking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
extern crate uzers;

use uzers::mock::MockUsers;
use uzers::{AllGroups, Group, Groups, UsersCache, UsersSnapshot};

fn iter_aware<G: Groups + AllGroups>(g: &G) {
println!("All groups:");
for group in g.get_all_groups() {
println!("- {group:?}");
}

no_iter(g);
}

fn no_iter<G: Groups>(g: &G) {
let me = g.get_current_groupname();
let root = g.get_group_by_gid(0);
println!("My group is {me:?}, gid 0 is {root:?}");
}

fn main() {
env_logger::init();

// UsersCache can only be used with `no_iter`
println!("\n--- UsersCache ---");
no_iter(&UsersCache::new());

// UsersSnapshot can be used with both `no_iter` and `iter_aware`
println!("\n--- UsersSnapshot: all groups ---");
no_iter(unsafe { &UsersSnapshot::new() });
println!("\n--- UsersSnapshot: primary groups of some system users ---");
iter_aware(unsafe { &UsersSnapshot::only_users(|u| u.uid() < 10) });

// MockUsers can be used with both `no_iter` and `iter_aware`
println!("\n--- MockUsers ---");
let mut mock = MockUsers::with_current_uid(1000);
mock.add_group(Group::new(1000, "fred"));
mock.add_group(Group::new(0, "r00t"));
iter_aware(&mock);
}
Loading