Skip to content

Commit 0b085af

Browse files
authored
Cleanup unit tests (#1402)
2 parents c861be2 + 62d1d30 commit 0b085af

File tree

10 files changed

+134
-183
lines changed

10 files changed

+134
-183
lines changed

src/pam/securemem.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl PamBuffer {
2626
}
2727

2828
// initialize the buffer with already existing data (otherwise populating it is a bit hairy)
29-
// this is inferior than placing the data into the securebuffer directly
29+
// this is inferior to placing the data into the securebuffer directly
3030
#[cfg(test)]
3131
pub fn new(mut src: impl AsMut<[u8]>) -> Self {
3232
let mut buffer = PamBuffer::default();

src/su/cli.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@ impl SuAction {
1515
pub fn from_env() -> Result<Self, String> {
1616
SuOptions::parse_arguments(std::env::args())?.validate()
1717
}
18-
19-
#[cfg(test)]
20-
pub fn parse_arguments(args: impl IntoIterator<Item = String>) -> Result<Self, String> {
21-
SuOptions::parse_arguments(args)?.validate()
22-
}
23-
24-
#[cfg(test)]
25-
#[allow(clippy::result_large_err)]
26-
pub fn try_into_run(self) -> Result<SuRunOptions, Self> {
27-
if let Self::Run(v) = self {
28-
Ok(v)
29-
} else {
30-
Err(self)
31-
}
32-
}
3318
}
3419

3520
#[cfg_attr(test, derive(Debug, PartialEq))]
@@ -195,7 +180,7 @@ impl<T> IsAbsent for Vec<T> {
195180
}
196181

197182
#[derive(Debug, Default, PartialEq)]
198-
struct SuOptions {
183+
pub(super) struct SuOptions {
199184
// -c
200185
command: Option<String>,
201186
// -g
@@ -380,7 +365,9 @@ impl SuOptions {
380365
];
381366

382367
/// parse su arguments into SuOptions struct
383-
fn parse_arguments(arguments: impl IntoIterator<Item = String>) -> Result<SuOptions, String> {
368+
pub(super) fn parse_arguments(
369+
arguments: impl IntoIterator<Item = String>,
370+
) -> Result<SuOptions, String> {
384371
let mut options: SuOptions = SuOptions::default();
385372
let mut arg_iter = arguments.into_iter().skip(1);
386373

@@ -460,7 +447,7 @@ impl SuOptions {
460447
Ok(options)
461448
}
462449

463-
fn validate(self) -> Result<SuAction, String> {
450+
pub(super) fn validate(self) -> Result<SuAction, String> {
464451
let action = if self.help {
465452
SuAction::Help(self.try_into()?)
466453
} else if self.version {

src/su/context.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,23 @@ mod tests {
226226

227227
use crate::{
228228
common::Error,
229-
su::cli::{SuAction, SuRunOptions},
229+
su::cli::{SuAction, SuOptions, SuRunOptions},
230230
};
231231

232232
use super::SuContext;
233233

234234
fn get_options(args: &[&str]) -> SuRunOptions {
235235
let mut args = args.iter().map(|s| s.to_string()).collect::<Vec<String>>();
236236
args.insert(0, "/bin/su".to_string());
237-
SuAction::parse_arguments(args)
237+
let SuAction::Run(options) = SuOptions::parse_arguments(args)
238238
.unwrap()
239-
.try_into_run()
239+
.validate()
240240
.unwrap()
241+
else {
242+
panic!();
243+
};
244+
245+
options
241246
}
242247

243248
#[test]

src/sudo/cli/mod.rs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -44,64 +44,6 @@ impl SudoAction {
4444
let opts = SudoOptions::try_parse_from(iter)?;
4545
opts.validate()
4646
}
47-
48-
#[cfg(test)]
49-
#[must_use]
50-
pub fn is_edit(&self) -> bool {
51-
matches!(self, Self::Edit(..))
52-
}
53-
54-
#[cfg(test)]
55-
#[must_use]
56-
pub fn is_help(&self) -> bool {
57-
matches!(self, Self::Help(..))
58-
}
59-
60-
#[cfg(test)]
61-
#[must_use]
62-
pub fn is_remove_timestamp(&self) -> bool {
63-
matches!(self, Self::RemoveTimestamp(..))
64-
}
65-
66-
#[cfg(test)]
67-
#[must_use]
68-
pub fn is_reset_timestamp(&self) -> bool {
69-
matches!(self, Self::ResetTimestamp(..))
70-
}
71-
72-
#[cfg(test)]
73-
#[must_use]
74-
pub fn is_list(&self) -> bool {
75-
matches!(self, Self::List(..))
76-
}
77-
78-
#[cfg(test)]
79-
#[must_use]
80-
pub fn is_version(&self) -> bool {
81-
matches!(self, Self::Version(..))
82-
}
83-
84-
#[cfg(test)]
85-
#[must_use]
86-
pub fn is_validate(&self) -> bool {
87-
matches!(self, Self::Validate(..))
88-
}
89-
90-
#[cfg(test)]
91-
#[allow(clippy::result_large_err)]
92-
pub fn try_into_run(self) -> Result<SudoRunOptions, Self> {
93-
if let Self::Run(v) = self {
94-
Ok(v)
95-
} else {
96-
Err(self)
97-
}
98-
}
99-
100-
#[cfg(test)]
101-
#[must_use]
102-
pub fn is_run(&self) -> bool {
103-
matches!(self, Self::Run(..))
104-
}
10547
}
10648

10749
// sudo -h | -K | -k | -V

src/sudo/cli/tests.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,57 @@
11
use crate::common::SudoPath;
22

3-
use super::{SudoAction, SudoOptions};
3+
use super::{SudoAction, SudoOptions, SudoRunOptions};
4+
5+
impl SudoAction {
6+
#[must_use]
7+
pub fn is_edit(&self) -> bool {
8+
matches!(self, Self::Edit(..))
9+
}
10+
11+
#[must_use]
12+
pub fn is_help(&self) -> bool {
13+
matches!(self, Self::Help(..))
14+
}
15+
16+
#[must_use]
17+
pub fn is_remove_timestamp(&self) -> bool {
18+
matches!(self, Self::RemoveTimestamp(..))
19+
}
20+
21+
#[must_use]
22+
pub fn is_reset_timestamp(&self) -> bool {
23+
matches!(self, Self::ResetTimestamp(..))
24+
}
25+
26+
#[must_use]
27+
pub fn is_list(&self) -> bool {
28+
matches!(self, Self::List(..))
29+
}
30+
31+
#[must_use]
32+
pub fn is_version(&self) -> bool {
33+
matches!(self, Self::Version(..))
34+
}
35+
36+
#[must_use]
37+
pub fn is_validate(&self) -> bool {
38+
matches!(self, Self::Validate(..))
39+
}
40+
41+
#[allow(clippy::result_large_err)]
42+
pub fn try_into_run(self) -> Result<SudoRunOptions, Self> {
43+
if let Self::Run(v) = self {
44+
Ok(v)
45+
} else {
46+
Err(self)
47+
}
48+
}
49+
50+
#[must_use]
51+
pub fn is_run(&self) -> bool {
52+
matches!(self, Self::Run(..))
53+
}
54+
}
455

556
/// Passing '-E' with a variable fails
657
#[test]

src/sudoers/ast.rs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ pub enum Qualified<T> {
1818
Forbid(T) = HARDENED_ENUM_VALUE_1,
1919
}
2020

21-
impl<T> Qualified<T> {
22-
#[cfg(test)]
23-
pub fn as_allow(&self) -> Option<&T> {
24-
if let Self::Allow(v) = self {
25-
Some(v)
26-
} else {
27-
None
28-
}
29-
}
30-
}
31-
3221
/// Type aliases; many items can be replaced by ALL, aliases, and negated.
3322
pub type Spec<T> = Qualified<Meta<T>>;
3423
pub type SpecList<T> = Vec<Spec<T>>;
@@ -173,51 +162,6 @@ pub enum Sudo {
173162
LineComment = HARDENED_ENUM_VALUE_4,
174163
}
175164

176-
impl Sudo {
177-
#[cfg(test)]
178-
pub fn is_spec(&self) -> bool {
179-
matches!(self, Self::Spec(..))
180-
}
181-
182-
#[cfg(test)]
183-
pub fn is_decl(&self) -> bool {
184-
matches!(self, Self::Decl(..))
185-
}
186-
187-
#[cfg(test)]
188-
pub fn is_line_comment(&self) -> bool {
189-
matches!(self, Self::LineComment)
190-
}
191-
192-
#[cfg(test)]
193-
pub fn is_include(&self) -> bool {
194-
matches!(self, Self::Include(..))
195-
}
196-
197-
#[cfg(test)]
198-
pub fn is_include_dir(&self) -> bool {
199-
matches!(self, Self::IncludeDir(..))
200-
}
201-
202-
#[cfg(test)]
203-
pub fn as_include(&self) -> &str {
204-
if let Self::Include(v, _) = self {
205-
v
206-
} else {
207-
panic!()
208-
}
209-
}
210-
211-
#[cfg(test)]
212-
pub fn as_spec(&self) -> Option<&PermissionSpec> {
213-
if let Self::Spec(v) = self {
214-
Some(v)
215-
} else {
216-
None
217-
}
218-
}
219-
}
220-
221165
/// grammar:
222166
/// ```text
223167
/// identifier = name

src/sudoers/test/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,60 @@ use super::char_stream::CharStream;
55
use super::*;
66
use basic_parser::{parse_eval, parse_lines, parse_string};
77

8+
impl<T> Qualified<T> {
9+
pub fn as_allow(&self) -> Option<&T> {
10+
if let Self::Allow(v) = self {
11+
Some(v)
12+
} else {
13+
None
14+
}
15+
}
16+
}
17+
18+
impl<T> Meta<T> {
19+
pub fn is_alias(&self) -> bool {
20+
matches!(self, Self::Alias(..))
21+
}
22+
}
23+
24+
impl Sudo {
25+
pub fn is_spec(&self) -> bool {
26+
matches!(self, Self::Spec(..))
27+
}
28+
29+
pub fn is_decl(&self) -> bool {
30+
matches!(self, Self::Decl(..))
31+
}
32+
33+
pub fn is_line_comment(&self) -> bool {
34+
matches!(self, Self::LineComment)
35+
}
36+
37+
pub fn is_include(&self) -> bool {
38+
matches!(self, Self::Include(..))
39+
}
40+
41+
pub fn is_include_dir(&self) -> bool {
42+
matches!(self, Self::IncludeDir(..))
43+
}
44+
45+
pub fn as_include(&self) -> &str {
46+
if let Self::Include(v, _) = self {
47+
v
48+
} else {
49+
panic!()
50+
}
51+
}
52+
53+
pub fn as_spec(&self) -> Option<&PermissionSpec> {
54+
if let Self::Spec(v) = self {
55+
Some(v)
56+
} else {
57+
None
58+
}
59+
}
60+
}
61+
862
#[derive(PartialEq)]
963
struct Named(&'static str);
1064

src/sudoers/tokens.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,6 @@ pub enum Meta<T> {
100100
Alias(String) = HARDENED_ENUM_VALUE_2,
101101
}
102102

103-
impl<T> Meta<T> {
104-
#[cfg(test)]
105-
pub fn is_alias(&self) -> bool {
106-
matches!(self, Self::Alias(..))
107-
}
108-
}
109-
110103
impl<T: Token> Token for Meta<T> {
111104
fn construct(raw: String) -> Result<Self, String> {
112105
// `T` may accept whitespace resulting in `raw` having trailing whitespace which would make

src/system/mod.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -978,14 +978,6 @@ mod tests {
978978
assert!(super::Process::tty_device_id(WithProcess::Current).is_ok());
979979
}
980980

981-
#[test]
982-
fn get_process_start_time() {
983-
let time = super::Process::starting_time(WithProcess::Current).unwrap();
984-
let now = super::ProcessCreateTime::now().unwrap();
985-
assert!(time.secs() > now.secs() - 24 * 60 * 60);
986-
assert!(time < now);
987-
}
988-
989981
#[test]
990982
fn pgid_test() {
991983
use super::{getpgid, setpgid};

0 commit comments

Comments
 (0)