| 1 | use std::{ffi::OsString, mem, path::PathBuf}; |
| 2 | |
| 3 | use crate::common::SudoString; |
| 4 | |
| 5 | use super::DEFAULT_USER; |
| 6 | |
| 7 | #[cfg_attr(test, derive(Debug, PartialEq))] |
| 8 | pub enum SuAction { |
| 9 | Help(SuHelpOptions), |
| 10 | Version(SuVersionOptions), |
| 11 | Run(SuRunOptions), |
| 12 | } |
| 13 | |
| 14 | impl SuAction { |
| 15 | pub fn from_env() -> Result<Self, String> { |
| 16 | SuOptions::parse_arguments(std::env::args())?.validate() |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | #[cfg_attr(test, derive(Debug, PartialEq))] |
| 21 | pub struct SuHelpOptions {} |
| 22 | |
| 23 | impl TryFrom<SuOptions> for SuHelpOptions { |
| 24 | type Error = String; |
| 25 | |
| 26 | fn try_from(mut opts: SuOptions) -> Result<Self, Self::Error> { |
| 27 | let help = mem::take(&mut opts.help); |
| 28 | debug_assert!(help); |
| 29 | reject_all("--help", opts)?; |
| 30 | Ok(Self {}) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[cfg_attr(test, derive(Debug, PartialEq))] |
| 35 | pub struct SuVersionOptions {} |
| 36 | |
| 37 | impl TryFrom<SuOptions> for SuVersionOptions { |
| 38 | type Error = String; |
| 39 | |
| 40 | fn try_from(mut opts: SuOptions) -> Result<Self, Self::Error> { |
| 41 | let version = mem::take(&mut opts.version); |
| 42 | debug_assert!(version); |
| 43 | reject_all("--version", opts)?; |
| 44 | Ok(Self {}) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[derive(Debug)] |
| 49 | #[cfg_attr(test, derive(PartialEq))] |
| 50 | pub struct SuRunOptions { |
| 51 | // -c |
| 52 | pub command: Option<OsString>, |
| 53 | // -g |
| 54 | pub group: Vec<SudoString>, |
| 55 | // -l |
| 56 | pub login: bool, |
| 57 | // -p |
| 58 | pub preserve_environment: bool, |
| 59 | // -s |
| 60 | pub shell: Option<PathBuf>, |
| 61 | // -G |
| 62 | pub supp_group: Vec<SudoString>, |
| 63 | // -w |
| 64 | pub whitelist_environment: Vec<String>, |
| 65 | |
| 66 | pub user: SudoString, |
| 67 | pub arguments: Vec<OsString>, |
| 68 | } |
| 69 | |
| 70 | #[cfg(test)] |
| 71 | impl Default for SuRunOptions { |
| 72 | fn default() -> Self { |
| 73 | Self { |
| 74 | command: None, |
| 75 | group: vec![], |
| 76 | login: false, |
| 77 | preserve_environment: false, |
| 78 | shell: None, |
| 79 | supp_group: vec![], |
| 80 | whitelist_environment: vec![], |
| 81 | user: DEFAULT_USER.into(), |
| 82 | arguments: vec![], |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | impl TryFrom<SuOptions> for SuRunOptions { |
| 88 | type Error = String; |
| 89 | |
| 90 | fn try_from(mut opts: SuOptions) -> Result<Self, Self::Error> { |
| 91 | let command = mem::take(&mut opts.command); |
| 92 | let group = mem::take(&mut opts.group); |
| 93 | let login = mem::take(&mut opts.login); |
| 94 | let preserve_environment = mem::take(&mut opts.preserve_environment); |
| 95 | // always `true`; cannot be disabled via the CLI |
| 96 | let _pty = mem::take(&mut opts.pty); |
| 97 | let shell = mem::take(&mut opts.shell); |
| 98 | let supp_group = mem::take(&mut opts.supp_group); |
| 99 | let whitelist_environment = mem::take(&mut opts.whitelist_environment); |
| 100 | let mut positional_args = mem::take(&mut opts.positional_args); |
| 101 | |
| 102 | reject_all("run mode", opts)?; |
| 103 | |
| 104 | let user = if positional_args.is_empty() { |
| 105 | DEFAULT_USER.to_string() |
| 106 | } else { |
| 107 | positional_args.remove(0) |
| 108 | }; |
| 109 | let arguments = positional_args.into_iter().map(OsString::from).collect(); |
| 110 | |
| 111 | Ok(Self { |
| 112 | command, |
| 113 | group, |
| 114 | login, |
| 115 | preserve_environment, |
| 116 | shell, |
| 117 | supp_group, |
| 118 | whitelist_environment, |
| 119 | user: SudoString::try_from(user).map_err(|err| err.to_string())?, |
| 120 | arguments, |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | fn reject_all(context: &str, opts: SuOptions) -> Result<(), String> { |
| 126 | macro_rules! ensure_options_absent { |
| 127 | ($($opt:ident,)*) => { |
| 128 | let SuOptions { |
| 129 | $($opt),* |
| 130 | } = opts; |
| 131 | |
| 132 | $(if !$opt.is_absent() { |
| 133 | let name = concat!("--", stringify!($opt)).replace('_', "-"); |
| 134 | return Err(format!("{context} conflicts with {name}")); |
| 135 | })* |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | ensure_options_absent! { |
| 140 | command, |
| 141 | group, |
| 142 | help, |
| 143 | login, |
| 144 | preserve_environment, |
| 145 | pty, |
| 146 | shell, |
| 147 | supp_group, |
| 148 | version, |
| 149 | whitelist_environment, |
| 150 | positional_args, |
| 151 | }; |
| 152 | |
| 153 | if !positional_args.is_absent() { |
| 154 | return Err(format!("{context} conflicts with positional argument")); |
| 155 | } |
| 156 | |
| 157 | Ok(()) |
| 158 | } |
| 159 | |
| 160 | trait IsAbsent { |
| 161 | fn is_absent(&self) -> bool; |
| 162 | } |
| 163 | |
| 164 | impl IsAbsent for bool { |
| 165 | fn is_absent(&self) -> bool { |
| 166 | !*self |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | impl<T> IsAbsent for Option<T> { |
| 171 | fn is_absent(&self) -> bool { |
| 172 | self.is_none() |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | impl<T> IsAbsent for Vec<T> { |
| 177 | fn is_absent(&self) -> bool { |
| 178 | self.is_empty() |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[derive(Debug, Default, PartialEq)] |
| 183 | pub(super) struct SuOptions { |
| 184 | // -c |
| 185 | command: Option<OsString>, |
| 186 | // -g |
| 187 | group: Vec<SudoString>, |
| 188 | // -h |
| 189 | help: bool, |
| 190 | // -l |
| 191 | login: bool, |
| 192 | // -p |
| 193 | preserve_environment: bool, |
| 194 | // -P |
| 195 | pty: bool, |
| 196 | // -s |
| 197 | shell: Option<PathBuf>, |
| 198 | // -G |
| 199 | supp_group: Vec<SudoString>, |
| 200 | // -V |
| 201 | version: bool, |
| 202 | // -w |
| 203 | whitelist_environment: Vec<String>, |
| 204 | |
| 205 | positional_args: Vec<String>, |
| 206 | } |
| 207 | |
| 208 | type OptionSetter = fn(&mut SuOptions, Option<String>) -> Result<(), String>; |
| 209 | |
| 210 | struct SuOption { |
| 211 | short: char, |
| 212 | long: &'static str, |
| 213 | takes_argument: bool, |
| 214 | set: OptionSetter, |
| 215 | } |
| 216 | |
| 217 | impl SuOptions { |
| 218 | const SU_OPTIONS: &'static [SuOption] = &[ |
| 219 | SuOption { |
| 220 | short: 'c', |
| 221 | long: "command", |
| 222 | takes_argument: true, |
| 223 | set: |sudo_options, argument| { |
| 224 | if argument.is_some() { |
| 225 | sudo_options.command = argument.map(OsString::from); |
| 226 | Ok(()) |
| 227 | } else { |
| 228 | Err("no command provided".into()) |
| 229 | } |
| 230 | }, |
| 231 | }, |
| 232 | SuOption { |
| 233 | short: 'g', |
| 234 | long: "group", |
| 235 | takes_argument: true, |
| 236 | set: |sudo_options, argument| { |
| 237 | if let Some(value) = argument { |
| 238 | sudo_options.group.push(SudoString::from_cli_string(value)); |
| 239 | Ok(()) |
| 240 | } else { |
| 241 | Err("no group provided".into()) |
| 242 | } |
| 243 | }, |
| 244 | }, |
| 245 | SuOption { |
| 246 | short: 'G', |
| 247 | long: "supp-group", |
| 248 | takes_argument: true, |
| 249 | set: |sudo_options, argument| { |
| 250 | if let Some(value) = argument { |
| 251 | sudo_options |
| 252 | .supp_group |
| 253 | .push(SudoString::from_cli_string(value)); |
| 254 | Ok(()) |
| 255 | } else { |
| 256 | Err("no supplementary group provided".into()) |
| 257 | } |
| 258 | }, |
| 259 | }, |
| 260 | SuOption { |
| 261 | short: 'l', |
| 262 | long: "login", |
| 263 | takes_argument: false, |
| 264 | set: |sudo_options, _| { |
| 265 | if sudo_options.login { |
| 266 | Err(more_than_once("--login")) |
| 267 | } else { |
| 268 | sudo_options.login = true; |
| 269 | Ok(()) |
| 270 | } |
| 271 | }, |
| 272 | }, |
| 273 | SuOption { |
| 274 | short: 'p', |
| 275 | long: "preserve-environment", |
| 276 | takes_argument: false, |
| 277 | set: |sudo_options, _| { |
| 278 | if sudo_options.preserve_environment { |
| 279 | Err(more_than_once("--preserve-environment")) |
| 280 | } else { |
| 281 | sudo_options.preserve_environment = true; |
| 282 | Ok(()) |
| 283 | } |
| 284 | }, |
| 285 | }, |
| 286 | SuOption { |
| 287 | short: 'm', |
| 288 | long: "preserve-environment", |
| 289 | takes_argument: false, |
| 290 | set: |sudo_options, _| { |
| 291 | if sudo_options.preserve_environment { |
| 292 | Err(more_than_once("--preserve-environment")) |
| 293 | } else { |
| 294 | sudo_options.preserve_environment = true; |
| 295 | Ok(()) |
| 296 | } |
| 297 | }, |
| 298 | }, |
| 299 | SuOption { |
| 300 | short: 'P', |
| 301 | long: "pty", |
| 302 | takes_argument: false, |
| 303 | set: |sudo_options, _| { |
| 304 | if sudo_options.pty { |
| 305 | Err(more_than_once("--pty")) |
| 306 | } else { |
| 307 | sudo_options.pty = true; |
| 308 | Ok(()) |
| 309 | } |
| 310 | }, |
| 311 | }, |
| 312 | SuOption { |
| 313 | short: 's', |
| 314 | long: "shell", |
| 315 | takes_argument: true, |
| 316 | set: |sudo_options, argument| { |
| 317 | if let Some(path) = argument { |
| 318 | sudo_options.shell = Some(PathBuf::from(path)); |
| 319 | Ok(()) |
| 320 | } else { |
| 321 | Err("no shell provided".into()) |
| 322 | } |
| 323 | }, |
| 324 | }, |
| 325 | SuOption { |
| 326 | short: 'w', |
| 327 | long: "whitelist-environment", |
| 328 | takes_argument: true, |
| 329 | set: |sudo_options, argument| { |
| 330 | if let Some(list) = argument { |
| 331 | let values: Vec<String> = list.split(',').map(str::to_string).collect(); |
| 332 | sudo_options.whitelist_environment.extend(values); |
| 333 | Ok(()) |
| 334 | } else { |
| 335 | Err("no environment whitelist provided".into()) |
| 336 | } |
| 337 | }, |
| 338 | }, |
| 339 | SuOption { |
| 340 | short: 'V', |
| 341 | long: "version", |
| 342 | takes_argument: false, |
| 343 | set: |sudo_options, _| { |
| 344 | if sudo_options.version { |
| 345 | Err(more_than_once("--version")) |
| 346 | } else { |
| 347 | sudo_options.version = true; |
| 348 | Ok(()) |
| 349 | } |
| 350 | }, |
| 351 | }, |
| 352 | SuOption { |
| 353 | short: 'h', |
| 354 | long: "help", |
| 355 | takes_argument: false, |
| 356 | set: |sudo_options, _| { |
| 357 | if sudo_options.help { |
| 358 | Err(more_than_once("--help")) |
| 359 | } else { |
| 360 | sudo_options.help = true; |
| 361 | Ok(()) |
| 362 | } |
| 363 | }, |
| 364 | }, |
| 365 | ]; |
| 366 | |
| 367 | /// parse su arguments into SuOptions struct |
| 368 | pub(super) fn parse_arguments( |
| 369 | arguments: impl IntoIterator<Item = String>, |
| 370 | ) -> Result<SuOptions, String> { |
| 371 | let mut options: SuOptions = SuOptions::default(); |
| 372 | let mut arg_iter = arguments.into_iter().skip(1); |
| 373 | |
| 374 | while let Some(arg) = arg_iter.next() { |
| 375 | // - or -l or --login indicates a login shell should be started |
| 376 | if arg == "-" { |
| 377 | if options.login { |
| 378 | return Err(more_than_once("--login")); |
| 379 | } else { |
| 380 | options.login = true; |
| 381 | } |
| 382 | } else if arg == "--" { |
| 383 | // only positional arguments after this point |
| 384 | options.positional_args.extend(arg_iter); |
| 385 | |
| 386 | break; |
| 387 | |
| 388 | // if the argument starts with -- it must be a full length option name |
| 389 | } else if let Some(unprefixed) = arg.strip_prefix("--") { |
| 390 | // parse assignments like '--group=ferris' |
| 391 | if let Some((key, value)) = unprefixed.split_once('=') { |
| 392 | // lookup the option by name |
| 393 | if let Some(option) = Self::SU_OPTIONS.iter().find(|o| o.long == key) { |
| 394 | // the value is already present, when the option does not take any arguments this results in an error |
| 395 | if option.takes_argument { |
| 396 | (option.set)(&mut options, Some(value.to_string()))?; |
| 397 | } else { |
| 398 | Err(format!("'--{}' does not take any arguments", option.long))?; |
| 399 | } |
| 400 | } else { |
| 401 | Err(format!("unrecognized option '{arg}'"))?; |
| 402 | } |
| 403 | // lookup the option |
| 404 | } else if let Some(option) = Self::SU_OPTIONS.iter().find(|o| o.long == unprefixed) |
| 405 | { |
| 406 | // try to parse an argument when the option needs an argument |
| 407 | if option.takes_argument { |
| 408 | let next_arg = arg_iter.next(); |
| 409 | (option.set)(&mut options, next_arg)?; |
| 410 | } else { |
| 411 | (option.set)(&mut options, None)?; |
| 412 | } |
| 413 | } else { |
| 414 | Err(format!("unrecognized option '{arg}'"))?; |
| 415 | } |
| 416 | } else if let Some(unprefixed) = arg.strip_prefix('-') { |
| 417 | // flags can be grouped, so we loop over the the characters |
| 418 | let mut chars = unprefixed.chars(); |
| 419 | while let Some(curr) = chars.next() { |
| 420 | // lookup the option |
| 421 | if let Some(option) = Self::SU_OPTIONS.iter().find(|o| o.short == curr) { |
| 422 | // try to parse an argument when one is necessary, either the rest of the current flag group or the next argument |
| 423 | let rest = chars.as_str(); |
| 424 | |
| 425 | if option.takes_argument { |
| 426 | let next_arg = if rest.is_empty() { |
| 427 | arg_iter.next() |
| 428 | } else { |
| 429 | Some(rest.to_string()) |
| 430 | }; |
| 431 | (option.set)(&mut options, next_arg)?; |
| 432 | // stop looping over flags if the current flag takes an argument |
| 433 | break; |
| 434 | } else { |
| 435 | // parse flag without argument |
| 436 | (option.set)(&mut options, None)?; |
| 437 | } |
| 438 | } else { |
| 439 | Err(format!("unrecognized option '{curr}'"))?; |
| 440 | } |
| 441 | } |
| 442 | } else { |
| 443 | options.positional_args.push(arg); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | Ok(options) |
| 448 | } |
| 449 | |
| 450 | pub(super) fn validate(self) -> Result<SuAction, String> { |
| 451 | let action = if self.help { |
| 452 | SuAction::Help(self.try_into()?) |
| 453 | } else if self.version { |
| 454 | SuAction::Version(self.try_into()?) |
| 455 | } else { |
| 456 | SuAction::Run(self.try_into()?) |
| 457 | }; |
| 458 | Ok(action) |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | fn more_than_once(flag: &str) -> String { |
| 463 | format!("argument '{flag}' was provided more than once, but cannot be used multiple times") |
| 464 | } |
| 465 | |
| 466 | #[cfg(test)] |
| 467 | mod tests { |
| 468 | use std::vec; |
| 469 | |
| 470 | use super::{SuAction, SuHelpOptions, SuOptions, SuRunOptions, SuVersionOptions}; |
| 471 | |
| 472 | fn parse(args: &[&str]) -> SuAction { |
| 473 | let mut args = args.iter().map(|s| s.to_string()).collect::<Vec<String>>(); |
| 474 | args.insert(0, "/bin/su".to_string()); |
| 475 | SuOptions::parse_arguments(args) |
| 476 | .unwrap() |
| 477 | .validate() |
| 478 | .unwrap() |
| 479 | } |
| 480 | |
| 481 | #[test] |
| 482 | fn it_parses_group() { |
| 483 | let expected = SuAction::Run(SuRunOptions { |
| 484 | group: vec!["ferris".into()], |
| 485 | ..<_>::default() |
| 486 | }); |
| 487 | assert_eq!(expected, parse(&["-g", "ferris"])); |
| 488 | assert_eq!(expected, parse(&["-gferris"])); |
| 489 | assert_eq!(expected, parse(&["--group", "ferris"])); |
| 490 | assert_eq!(expected, parse(&["--group=ferris"])); |
| 491 | } |
| 492 | |
| 493 | #[test] |
| 494 | fn it_parses_shell_default() { |
| 495 | let result = parse(&["--shell", "/bin/bash"]); |
| 496 | assert_eq!( |
| 497 | result, |
| 498 | SuAction::Run(SuRunOptions { |
| 499 | shell: Some("/bin/bash".into()), |
| 500 | ..<_>::default() |
| 501 | }) |
| 502 | ); |
| 503 | } |
| 504 | |
| 505 | #[test] |
| 506 | fn it_parses_whitelist() { |
| 507 | let result = parse(&["-w", "FOO,BAR"]); |
| 508 | assert_eq!( |
| 509 | result, |
| 510 | SuAction::Run(SuRunOptions { |
| 511 | whitelist_environment: vec!["FOO".to_string(), "BAR".to_string()], |
| 512 | ..<_>::default() |
| 513 | }) |
| 514 | ); |
| 515 | } |
| 516 | |
| 517 | #[test] |
| 518 | fn it_parses_combined_options() { |
| 519 | let expected = SuAction::Run(SuRunOptions { |
| 520 | login: true, |
| 521 | ..<_>::default() |
| 522 | }); |
| 523 | |
| 524 | assert_eq!(expected, parse(&["-Pl"])); |
| 525 | assert_eq!(expected, parse(&["-lP"])); |
| 526 | } |
| 527 | |
| 528 | #[test] |
| 529 | fn it_parses_combined_options_and_arguments() { |
| 530 | let expected = SuAction::Run(SuRunOptions { |
| 531 | login: true, |
| 532 | shell: Some("/bin/bash".into()), |
| 533 | ..<_>::default() |
| 534 | }); |
| 535 | |
| 536 | assert_eq!(expected, parse(&["-Pls/bin/bash"])); |
| 537 | assert_eq!(expected, parse(&["-Pls", "/bin/bash"])); |
| 538 | assert_eq!(expected, parse(&["-Pl", "-s/bin/bash"])); |
| 539 | assert_eq!(expected, parse(&["-lP", "-s", "/bin/bash"])); |
| 540 | assert_eq!(expected, parse(&["-lP", "--shell=/bin/bash"])); |
| 541 | assert_eq!(expected, parse(&["-lP", "--shell", "/bin/bash"])); |
| 542 | } |
| 543 | |
| 544 | #[test] |
| 545 | fn it_parses_an_user() { |
| 546 | let expected = SuAction::Run(SuRunOptions { |
| 547 | user: "ferris".into(), |
| 548 | ..<_>::default() |
| 549 | }); |
| 550 | assert_eq!(expected, parse(&["-P", "ferris"])); |
| 551 | assert_eq!(expected, parse(&["ferris", "-P"])); |
| 552 | } |
| 553 | |
| 554 | #[test] |
| 555 | fn it_parses_arguments() { |
| 556 | let expected = SuAction::Run(SuRunOptions { |
| 557 | user: "ferris".into(), |
| 558 | arguments: vec!["script.sh".into()], |
| 559 | ..<_>::default() |
| 560 | }); |
| 561 | |
| 562 | assert_eq!(expected, parse(&["-P", "ferris", "script.sh"])); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn it_parses_command() { |
| 567 | let expected = SuAction::Run(SuRunOptions { |
| 568 | command: Some("'echo hi'".into()), |
| 569 | ..<_>::default() |
| 570 | }); |
| 571 | assert_eq!(expected, parse(&["-c", "'echo hi'"])); |
| 572 | assert_eq!(expected, parse(&["-c'echo hi'"])); |
| 573 | assert_eq!(expected, parse(&["--command", "'echo hi'"])); |
| 574 | assert_eq!(expected, parse(&["--command='echo hi'"])); |
| 575 | |
| 576 | let expected = SuAction::Run(SuRunOptions { |
| 577 | command: Some("env".into()), |
| 578 | ..<_>::default() |
| 579 | }); |
| 580 | assert_eq!(expected, parse(&["-c", "env"])); |
| 581 | assert_eq!(expected, parse(&["-cenv"])); |
| 582 | assert_eq!(expected, parse(&["--command", "env"])); |
| 583 | assert_eq!(expected, parse(&["--command=env"])); |
| 584 | } |
| 585 | |
| 586 | #[test] |
| 587 | fn it_parses_supplementary_group() { |
| 588 | let expected = SuAction::Run(SuRunOptions { |
| 589 | supp_group: vec!["ferris".into()], |
| 590 | ..<_>::default() |
| 591 | }); |
| 592 | assert_eq!(expected, parse(&["-G", "ferris"])); |
| 593 | assert_eq!(expected, parse(&["-Gferris"])); |
| 594 | assert_eq!(expected, parse(&["--supp-group", "ferris"])); |
| 595 | assert_eq!(expected, parse(&["--supp-group=ferris"])); |
| 596 | } |
| 597 | |
| 598 | #[test] |
| 599 | fn it_parses_multiple_supplementary_groups() { |
| 600 | let expected = SuAction::Run(SuRunOptions { |
| 601 | supp_group: vec!["ferris".into(), "krabbetje".into(), "krabbe".into()], |
| 602 | ..<_>::default() |
| 603 | }); |
| 604 | assert_eq!( |
| 605 | expected, |
| 606 | parse(&["-G", "ferris", "-G", "krabbetje", "--supp-group", "krabbe"]) |
| 607 | ); |
| 608 | } |
| 609 | |
| 610 | #[test] |
| 611 | fn it_parses_login() { |
| 612 | let expected = SuAction::Run(SuRunOptions { |
| 613 | login: true, |
| 614 | ..<_>::default() |
| 615 | }); |
| 616 | assert_eq!(expected, parse(&["-"])); |
| 617 | assert_eq!(expected, parse(&["-l"])); |
| 618 | assert_eq!(expected, parse(&["--login"])); |
| 619 | } |
| 620 | |
| 621 | #[test] |
| 622 | fn it_parses_pty() { |
| 623 | let expected = SuAction::Run(<_>::default()); |
| 624 | assert_eq!(expected, parse(&["-P"])); |
| 625 | assert_eq!(expected, parse(&["--pty"])); |
| 626 | } |
| 627 | |
| 628 | #[test] |
| 629 | fn it_parses_shell() { |
| 630 | let expected = SuAction::Run(SuRunOptions { |
| 631 | shell: Some("some-shell".into()), |
| 632 | ..<_>::default() |
| 633 | }); |
| 634 | assert_eq!(expected, parse(&["-s", "some-shell"])); |
| 635 | assert_eq!(expected, parse(&["-ssome-shell"])); |
| 636 | assert_eq!(expected, parse(&["--shell", "some-shell"])); |
| 637 | assert_eq!(expected, parse(&["--shell=some-shell"])); |
| 638 | } |
| 639 | |
| 640 | #[test] |
| 641 | fn it_parses_whitelist_environment() { |
| 642 | let expected = SuAction::Run(SuRunOptions { |
| 643 | whitelist_environment: vec!["FOO".to_string(), "BAR".to_string()], |
| 644 | ..<_>::default() |
| 645 | }); |
| 646 | assert_eq!(expected, parse(&["-w", "FOO,BAR"])); |
| 647 | assert_eq!(expected, parse(&["-wFOO,BAR"])); |
| 648 | assert_eq!(expected, parse(&["--whitelist-environment", "FOO,BAR"])); |
| 649 | assert_eq!(expected, parse(&["--whitelist-environment=FOO,BAR"])); |
| 650 | } |
| 651 | |
| 652 | #[test] |
| 653 | fn it_parses_help() { |
| 654 | let expected = SuAction::Help(SuHelpOptions {}); |
| 655 | assert_eq!(expected, parse(&["-h"])); |
| 656 | assert_eq!(expected, parse(&["--help"])); |
| 657 | } |
| 658 | |
| 659 | #[test] |
| 660 | fn it_parses_version() { |
| 661 | let expected = SuAction::Version(SuVersionOptions {}); |
| 662 | assert_eq!(expected, parse(&["-V"])); |
| 663 | assert_eq!(expected, parse(&["--version"])); |
| 664 | } |
| 665 | |
| 666 | #[test] |
| 667 | fn short_flag_whitespace() { |
| 668 | let expected = SuAction::Run(SuRunOptions { |
| 669 | group: vec![" ".into()], |
| 670 | ..<_>::default() |
| 671 | }); |
| 672 | assert_eq!(expected, parse(&["-g "])); |
| 673 | } |
| 674 | |
| 675 | #[test] |
| 676 | fn short_flag_whitespace_positional_argument() { |
| 677 | let expected = SuAction::Run(SuRunOptions { |
| 678 | group: vec![" ".into()], |
| 679 | user: "ghost".into(), |
| 680 | ..<_>::default() |
| 681 | }); |
| 682 | assert_eq!(expected, parse(&["-g ", "ghost"])); |
| 683 | } |
| 684 | |
| 685 | #[test] |
| 686 | fn long_flag_equal_whitespace() { |
| 687 | let expected = SuAction::Run(SuRunOptions { |
| 688 | group: vec![" ".into()], |
| 689 | ..<_>::default() |
| 690 | }); |
| 691 | assert_eq!(expected, parse(&["--group= "])); |
| 692 | } |
| 693 | |
| 694 | #[test] |
| 695 | fn flag_after_positional_argument() { |
| 696 | let expected = SuAction::Run(SuRunOptions { |
| 697 | arguments: vec![], |
| 698 | login: true, |
| 699 | user: "ferris".into(), |
| 700 | ..<_>::default() |
| 701 | }); |
| 702 | assert_eq!(expected, parse(&["ferris", "-l"])); |
| 703 | } |
| 704 | |
| 705 | #[test] |
| 706 | fn flags_after_dash() { |
| 707 | let expected = SuAction::Run(SuRunOptions { |
| 708 | command: Some("echo".into()), |
| 709 | login: true, |
| 710 | ..<_>::default() |
| 711 | }); |
| 712 | assert_eq!(expected, parse(&["-", "-c", "echo"])); |
| 713 | } |
| 714 | |
| 715 | #[test] |
| 716 | fn only_positional_args_after_dashdash() { |
| 717 | let expected = SuAction::Run(SuRunOptions { |
| 718 | user: "ferris".into(), |
| 719 | arguments: vec!["-c".into(), "echo".into()], |
| 720 | ..<_>::default() |
| 721 | }); |
| 722 | assert_eq!(expected, parse(&["--", "ferris", "-c", "echo"])); |
| 723 | } |
| 724 | |
| 725 | #[test] |
| 726 | fn repeated_boolean_flag() { |
| 727 | let f = |s: &str| s.to_string(); |
| 728 | |
| 729 | assert!(SuOptions::parse_arguments(["su", "-l", "-l"].map(f)).is_err()); |
| 730 | assert!(SuOptions::parse_arguments(["su", "-", "-l"].map(f)).is_err()); |
| 731 | assert!(SuOptions::parse_arguments(["su", "--login", "-l"].map(f)).is_err()); |
| 732 | |
| 733 | assert!(SuOptions::parse_arguments(["su", "-p", "-p"].map(f)).is_err()); |
| 734 | assert!(SuOptions::parse_arguments(["su", "-p", "--preserve-environment"].map(f)).is_err()); |
| 735 | } |
| 736 | } |