Mercurial > crates > nonstick
annotate testharness/src/lib.rs @ 183:4f46681b3f54 default tip
Catch a few stray cargo fmt things.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Wed, 30 Jul 2025 18:43:07 -0400 |
parents | 346dc13724ce |
children |
rev | line source |
---|---|
104
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
1 //! The nonstick library |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
2 |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
3 use crate::nonstick::items::ItemsMut; |
172
6727cbe56f4a
Test environment variable setting; minor cleanup.
Paul Fisher <paul@pfish.zone>
parents:
171
diff
changeset
|
4 use crate::nonstick::EnvironMapMut; |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
5 use std::cell::Cell; |
104
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 extern crate nonstick; |
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
8 use nonstick::conv::{ErrorMsg, InfoMsg, MaskedQAndA, QAndA}; |
166
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
9 use nonstick::{ |
173
46e8ce5cd5d1
Miscellaneous doc and code cleanups.
Paul Fisher <paul@pfish.zone>
parents:
172
diff
changeset
|
10 error, info, pam_export, AuthnFlags, AuthtokAction, AuthtokFlags, ErrorCode, ModuleClient, |
166
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
11 PamModule, |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
12 }; |
169
77470e45e397
Set up stuff to work the way Sun expects it to.
Paul Fisher <paul@pfish.zone>
parents:
168
diff
changeset
|
13 use std::ffi::CStr; |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
14 use std::os::unix::ffi::OsStrExt; |
104
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
15 |
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
16 struct TestHarness; |
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
17 |
146
1bc52025156b
Split PAM items into their own separate struct.
Paul Fisher <paul@pfish.zone>
parents:
127
diff
changeset
|
18 impl<M: ModuleClient> PamModule<M> for TestHarness { |
166
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
19 fn authenticate(handle: &mut M, args: Vec<&CStr>, _: AuthnFlags) -> nonstick::Result<()> { |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
20 let strings: Vec<_> = args.iter().map(|&a| Vec::from(a.to_bytes())).collect(); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
21 if strings != vec![Vec::from(b"param"), Vec::from(b"param2")] { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
22 return Err(ErrorCode::SystemError); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
23 } |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
24 let username = handle.username(None)?; |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
25 if username != "initial" { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
26 return Err(ErrorCode::UserUnknown); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
27 } |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
28 handle |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
29 .items_mut() |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
30 .set_user(Some("updated-in-process".as_ref()))?; |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
31 handle.set_module_data("florgus", Cell::new(99))?; |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
32 let authtok = handle.authtok(Some("custom".as_ref()))?; |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
33 if authtok.as_bytes() != b"valid" { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
34 return Err(ErrorCode::AuthenticationError); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
35 } |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
36 let info = InfoMsg::new("Watch out!".as_ref()); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
37 let err = ErrorMsg::new("It's broken!".as_ref()); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
38 let public = QAndA::new("How many?".as_ref()); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
39 let private = MaskedQAndA::new("Where?".as_ref()); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
40 let msgs = &[ |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
41 info.exchange(), |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
42 err.exchange(), |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
43 public.exchange(), |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
44 private.exchange(), |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
45 ]; |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
46 handle.communicate(msgs); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
47 let public = public.answer()?; |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
48 info!(handle, "public question: {:?}", public); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
49 let private = private.answer()?; |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
50 info!(handle, "private question: {:?}", private); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
51 if public.as_bytes() == b"123" && private.as_bytes() == b"abc" { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
52 Ok(()) |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
53 } else { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
54 Err(ErrorCode::Abort) |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
55 } |
104
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 } |
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 |
166
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
58 fn account_management(handle: &mut M, _: Vec<&CStr>, _: AuthnFlags) -> nonstick::Result<()> { |
172
6727cbe56f4a
Test environment variable setting; minor cleanup.
Paul Fisher <paul@pfish.zone>
parents:
171
diff
changeset
|
59 match handle.username(None)?.as_bytes() { |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
60 b"initial" => return Err(ErrorCode::AccountExpired), |
172
6727cbe56f4a
Test environment variable setting; minor cleanup.
Paul Fisher <paul@pfish.zone>
parents:
171
diff
changeset
|
61 b"updated-in-process" => (), |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
62 _ => return Err(ErrorCode::UserUnknown), |
172
6727cbe56f4a
Test environment variable setting; minor cleanup.
Paul Fisher <paul@pfish.zone>
parents:
171
diff
changeset
|
63 }; |
6727cbe56f4a
Test environment variable setting; minor cleanup.
Paul Fisher <paul@pfish.zone>
parents:
171
diff
changeset
|
64 let value: &Cell<i32> = handle |
6727cbe56f4a
Test environment variable setting; minor cleanup.
Paul Fisher <paul@pfish.zone>
parents:
171
diff
changeset
|
65 .get_module_data("florgus") |
6727cbe56f4a
Test environment variable setting; minor cleanup.
Paul Fisher <paul@pfish.zone>
parents:
171
diff
changeset
|
66 .ok_or(ErrorCode::SessionError)?; |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
67 let florgus_str: Option<&i32> = handle.get_module_data("florgus"); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
68 if let Some(s) = florgus_str { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
69 error!( |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
70 handle, |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
71 "module_data type mismatch: florgus = <{s}> but should not be set" |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
72 ) |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
73 } |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
74 if value.get() != 99 { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
75 error!(handle, "wrong value! {}", value.get()); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
76 return Err(ErrorCode::AuthTokError); |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
77 } |
172
6727cbe56f4a
Test environment variable setting; minor cleanup.
Paul Fisher <paul@pfish.zone>
parents:
171
diff
changeset
|
78 handle.environ_mut().insert("nin", "nine inch nails"); |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
79 let password = handle.authtok(None)?; |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
80 if password.as_bytes() == b"valid" { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
81 Err(ErrorCode::NewAuthTokRequired) |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
82 } else { |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
83 Ok(()) |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
84 } |
104
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
85 } |
159
634cd5f2ac8b
Separate logging into its own trait apart from the rest of PAM.
Paul Fisher <paul@pfish.zone>
parents:
146
diff
changeset
|
86 |
166
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
87 fn change_authtok( |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
88 handle: &mut M, |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
89 _: Vec<&CStr>, |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
90 action: AuthtokAction, |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
91 _flags: AuthtokFlags, |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
92 ) -> nonstick::Result<()> { |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
93 match action { |
171
e27c5c667a5a
Create full new types for return code and flags, separate end to end.
Paul Fisher <paul@pfish.zone>
parents:
169
diff
changeset
|
94 AuthtokAction::Validate => { |
182
346dc13724ce
Check for Sun-specific password change behavior.
Paul Fisher <paul@pfish.zone>
parents:
173
diff
changeset
|
95 let expected: &[u8] = if cfg!(pam_impl = "Sun") { |
346dc13724ce
Check for Sun-specific password change behavior.
Paul Fisher <paul@pfish.zone>
parents:
173
diff
changeset
|
96 b"valid" |
346dc13724ce
Check for Sun-specific password change behavior.
Paul Fisher <paul@pfish.zone>
parents:
173
diff
changeset
|
97 } else { |
346dc13724ce
Check for Sun-specific password change behavior.
Paul Fisher <paul@pfish.zone>
parents:
173
diff
changeset
|
98 b"old token!" |
346dc13724ce
Check for Sun-specific password change behavior.
Paul Fisher <paul@pfish.zone>
parents:
173
diff
changeset
|
99 }; |
346dc13724ce
Check for Sun-specific password change behavior.
Paul Fisher <paul@pfish.zone>
parents:
173
diff
changeset
|
100 if handle.old_authtok(None)?.as_bytes() != expected { |
167
0cabe7b94a4f
Check for old_authtok in change_authtok to emulate real behavior.
Paul Fisher <paul@pfish.zone>
parents:
166
diff
changeset
|
101 return Err(ErrorCode::AuthenticationError); |
0cabe7b94a4f
Check for old_authtok in change_authtok to emulate real behavior.
Paul Fisher <paul@pfish.zone>
parents:
166
diff
changeset
|
102 } |
168
6642e89d29a2
more closely follow real password change flow
Paul Fisher <paul@pfish.zone>
parents:
167
diff
changeset
|
103 Ok(()) |
6642e89d29a2
more closely follow real password change flow
Paul Fisher <paul@pfish.zone>
parents:
167
diff
changeset
|
104 } |
6642e89d29a2
more closely follow real password change flow
Paul Fisher <paul@pfish.zone>
parents:
167
diff
changeset
|
105 AuthtokAction::Update => { |
166
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
106 let password = handle.authtok(None)?; |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
107 if password.as_bytes() != b"acceptable" { |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
108 return Err(ErrorCode::PermissionDenied); |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
109 } |
2f5913131295
Separate flag/action flags into flags and action.
Paul Fisher <paul@pfish.zone>
parents:
163
diff
changeset
|
110 Ok(()) |
163
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
111 } |
a75a66cb4181
Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents:
159
diff
changeset
|
112 } |
159
634cd5f2ac8b
Separate logging into its own trait apart from the rest of PAM.
Paul Fisher <paul@pfish.zone>
parents:
146
diff
changeset
|
113 } |
104
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
114 } |
a2676475e86b
Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
115 |
173
46e8ce5cd5d1
Miscellaneous doc and code cleanups.
Paul Fisher <paul@pfish.zone>
parents:
172
diff
changeset
|
116 pam_export!(TestHarness); |