annotate testharness/src/bin/testharness.rs @ 163:a75a66cb4181

Add end-to-end tests; fix issues found by tests. - Create tests and installer/remover shell script - Fix Pointer/pointee problems - Add Debug formatting - Misc cleanup
author Paul Fisher <paul@pfish.zone>
date Mon, 14 Jul 2025 17:40:11 -0400
parents a2676475e86b
children 2f5913131295
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
104
a2676475e86b Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 //! The actual program which runs the tests.
a2676475e86b Create the very start of a test suite.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2
163
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
3 use nonstick::conv::Exchange;
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
4 use nonstick::items::Items;
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
5 use nonstick::libpam::TransactionBuilder;
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
6 use nonstick::{Conversation, ErrorCode, Flags, LibPamTransaction, PamShared, Transaction};
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
7 use std::cell::Cell;
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
8 use std::ffi::OsString;
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
9 use std::os::unix::ffi::OsStrExt;
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
10
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
11 fn main() {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
12 test_wrong_user();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
13 test_wrong_password();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
14 test_correct();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
15 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
16
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
17 #[derive(Debug, Default)]
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
18 struct TestHarness {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
19 username_requested: Cell<bool>,
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
20 wrong_username: bool,
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
21 wrong_password: bool,
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
22 changing_password: Cell<bool>,
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
23 change_prompt_count: Cell<u8>,
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
24 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
25
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
26 impl Conversation for &TestHarness {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
27 fn communicate(&self, messages: &[Exchange]) {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
28 if let [only_msg] = messages {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
29 match only_msg {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
30 Exchange::Prompt(p) => {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
31 if self.username_requested.get() {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
32 panic!("username already requested!")
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
33 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
34 if self.wrong_username {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
35 p.set_answer(Ok(OsString::from("not-right")))
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
36 } else {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
37 p.set_answer(Ok(OsString::from("initial")))
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
38 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
39 self.username_requested.set(true)
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
40 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
41 Exchange::MaskedPrompt(p) => {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
42 let answer = if self.changing_password.get() {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
43 let prompts = self.change_prompt_count.get();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
44 self.change_prompt_count.set(prompts + 1);
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
45 match prompts {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
46 0 => "mistake",
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
47 1 => "mismatch",
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
48 2 => "acceptable",
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
49 3 => "acceptable",
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
50 _ => panic!("unexpected number of prompts!"),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
51 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
52 } else if self.wrong_password {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
53 "bogus"
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
54 } else {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
55 "valid"
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
56 };
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
57 p.set_answer(Ok(OsString::from(answer)));
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
58 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
59 Exchange::Error(e) if self.changing_password.get() => e.set_answer(Ok(())),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
60 other => panic!("Unknown message {other:?}!"),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
61 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
62 } else {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
63 for msg in messages {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
64 match msg {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
65 Exchange::Info(i) => i.set_answer(Ok(())),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
66 Exchange::Error(e) => e.set_answer(Ok(())),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
67 Exchange::Prompt(p) => match p.question().as_bytes() {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
68 b"How many?" => p.set_answer(Ok(OsString::from("123"))),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
69 _ => p.set_answer(Err(ErrorCode::ConversationError)),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
70 },
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
71 Exchange::MaskedPrompt(p) => match p.question().as_bytes() {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
72 b"Where?" => p.set_answer(Ok(OsString::from("abc"))),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
73 _ => p.set_answer(Err(ErrorCode::ConversationError)),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
74 },
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
75 other => other.set_error(ErrorCode::Abort),
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
76 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
77 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
78 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
79 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
80 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
81
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
82 impl TestHarness {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
83 fn start(&self) -> LibPamTransaction<&Self> {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
84 TransactionBuilder::new_with_service("nonstick-testharness")
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
85 .build(self)
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
86 .expect("expected build success")
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
87 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
88 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
89
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
90 fn test_wrong_user() {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
91 let harness = TestHarness {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
92 wrong_username: true,
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
93 ..Default::default()
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
94 };
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
95 let mut tx = harness.start();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
96 let auth = tx.authenticate(Flags::empty());
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
97 assert_eq!(auth, Err(ErrorCode::UserUnknown));
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
98 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
99
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
100 fn test_wrong_password() {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
101 let harness = TestHarness {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
102 wrong_password: true,
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
103 ..Default::default()
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
104 };
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
105 let mut tx = harness.start();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
106 let auth = tx.authenticate(Flags::empty());
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
107 assert_eq!(auth, Err(ErrorCode::AuthenticationError));
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
108 }
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
109
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
110 fn test_correct() {
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
111 let harness = TestHarness::default();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
112 let mut tx = harness.start();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
113 tx.authenticate(Flags::empty()).unwrap();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
114 assert_eq!(tx.items().user().unwrap().unwrap(), "updated-in-process");
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
115 let result = tx.account_management(Flags::empty());
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
116 assert_eq!(result, Err(ErrorCode::NewAuthTokRequired));
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
117 harness.changing_password.set(true);
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
118 let change = tx.change_authtok(Flags::CHANGE_EXPIRED_AUTHTOK);
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
119 assert_eq!(change, Err(ErrorCode::TryAgain));
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
120 tx.change_authtok(Flags::CHANGE_EXPIRED_AUTHTOK).unwrap();
a75a66cb4181 Add end-to-end tests; fix issues found by tests.
Paul Fisher <paul@pfish.zone>
parents: 104
diff changeset
121 }