Map subuids from a spawned helper instead of an unsafe fork
Browse files at this commit6e9cbff1fcf958fd1b85a9b1f4309ba1a6317473Parent: 843ec87- refactor
- map subuids from a spawned helper instead of an unsafe fork
2 changed files +183 -95
crates/forge-runner/src/cli.rsmodified+2 -1
| @@ -137,7 +137,7 @@ where | |||
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | /// Whether this process was started as a hidden sandbox helper: argv[1] is `__sandbox`, | 139 | /// Whether this process was started as a hidden sandbox helper: argv[1] is `__sandbox`, |
| 140 | -/// `__sandbox-rm` or `__sandbox-unpack`. Callers that have their own runtime or subscriber check | 140 | +/// `__sandbox-rm`, `__sandbox-map` or `__sandbox-unpack`. Callers that have their own runtime or subscriber check |
| 141 | /// this first, and then call [`hidden_main`]. | 141 | /// this first, and then call [`hidden_main`]. |
| 142 | pub fn is_sandbox_invocation() -> bool { | 142 | pub fn is_sandbox_invocation() -> bool { |
| 143 | std::env::args_os() | 143 | std::env::args_os() |
| @@ -161,6 +161,7 @@ fn hidden_dispatch(args: &[OsString]) -> i32 { | |||
| 161 | match args.first().map(|arg| arg.as_encoded_bytes()) { | 161 | match args.first().map(|arg| arg.as_encoded_bytes()) { |
| 162 | Some(b"__sandbox") => crate::sandbox::helper_main(), | 162 | Some(b"__sandbox") => crate::sandbox::helper_main(), |
| 163 | Some(b"__sandbox-rm") => crate::sandbox::idmap::rm_helper_main(&args[1..]), | 163 | Some(b"__sandbox-rm") => crate::sandbox::idmap::rm_helper_main(&args[1..]), |
| 164 | + Some(b"__sandbox-map") => crate::sandbox::idmap::map_helper_main(&args[1..]), | ||
| 164 | Some(b"__sandbox-unpack") => crate::oci::unpack_helper_main(), | 165 | Some(b"__sandbox-unpack") => crate::oci::unpack_helper_main(), |
| 165 | _ => { | 166 | _ => { |
| 166 | eprintln!("forge-sandbox: unknown helper command"); | 167 | eprintln!("forge-sandbox: unknown helper command"); |
crates/forge-runner/src/sandbox/idmap.rsmodified+181 -94
| @@ -8,22 +8,21 @@ | |||
| 8 | //! setuid helpers `newuidmap` and `newgidmap` from shadow-utils, so that is what the helper | 8 | //! setuid helpers `newuidmap` and `newgidmap` from shadow-utils, so that is what the helper |
| 9 | //! runs. | 9 | //! runs. |
| 10 | //! | 10 | //! |
| 11 | -//! The namespace entry functions ([`enter_mapped_userns`]) must run in a single-threaded process. | 11 | +//! The namespace entry functions ([`enter_mapped_userns`]) must run in a single-threaded process, |
| 12 | -//! The sandbox helpers are single-threaded, and [`enter_mapped_userns`] forks in subuid mode. | 12 | +//! because `unshare(CLONE_NEWUSER)` fails in a multi-threaded one. The sandbox helpers are |
| 13 | +//! single-threaded. Subuid mode never forks: it starts a `__sandbox-map` helper of this same | ||
| 14 | +//! binary with `std::process::Command`, and that helper writes the maps. | ||
| 13 | 15 | ||
| 14 | use std::ffi::OsString; | 16 | use std::ffi::OsString; |
| 15 | -use std::fs::{self, File}; | 17 | +use std::fs; |
| 16 | use std::io::{self, Read, Write}; | 18 | use std::io::{self, Read, Write}; |
| 17 | use std::os::unix::fs::PermissionsExt; | 19 | use std::os::unix::fs::PermissionsExt; |
| 18 | use std::path::{Path, PathBuf}; | 20 | use std::path::{Path, PathBuf}; |
| 19 | use std::process::{Command, Stdio}; | 21 | use std::process::{Command, Stdio}; |
| 20 | 22 | ||
| 21 | -use nix::errno::Errno; | ||
| 22 | -use nix::fcntl::OFlag; | ||
| 23 | use nix::mount::{MsFlags, mount}; | 23 | use nix::mount::{MsFlags, mount}; |
| 24 | use nix::sched::{CloneFlags, unshare}; | 24 | use nix::sched::{CloneFlags, unshare}; |
| 25 | -use nix::sys::wait::{WaitStatus, waitpid}; | 25 | +use nix::unistd::{Uid, User, getgid, getuid}; |
| 26 | -use nix::unistd::{ForkResult, Pid, Uid, User, fork, getgid, getpid, getuid, pipe2}; | ||
| 27 | use serde::{Deserialize, Serialize}; | 26 | use serde::{Deserialize, Serialize}; |
| 28 | 27 | ||
| 29 | /// The most ids a subordinate range may contribute. This is the usual size of a range. | 28 | /// The most ids a subordinate range may contribute. This is the usual size of a range. |
| @@ -32,6 +31,8 @@ const MAX_SUBID_COUNT: u32 = 65536; | |||
| 32 | const MIN_SUBID_COUNT: u32 = 1000; | 31 | const MIN_SUBID_COUNT: u32 = 1000; |
| 33 | /// Where the setuid mapping tools are looked for, in order. | 32 | /// Where the setuid mapping tools are looked for, in order. |
| 34 | const TOOL_DIRS: [&str; 4] = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"]; | 33 | const TOOL_DIRS: [&str; 4] = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"]; |
| 34 | +/// The hidden subcommand, dispatched from the runner binary, that writes the maps in subuid mode. | ||
| 35 | +pub const MAP_HELPER_CMD: &str = "__sandbox-map"; | ||
| 35 | 36 | ||
| 36 | /// How the sandbox maps ids. Set by `[sandbox] userns` in the runner config. | 37 | /// How the sandbox maps ids. Set by `[sandbox] userns` in the runner config. |
| 37 | #[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq)] | 38 | #[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq)] |
| @@ -231,8 +232,8 @@ fn find_tool(name: &str) -> Option<PathBuf> { | |||
| 231 | /// Enters a new user and mount namespace whose ids are mapped as `map` says. The process is root | 232 | /// Enters a new user and mount namespace whose ids are mapped as `map` says. The process is root |
| 232 | /// inside it, and its mounts are private, so they don't propagate to the host. | 233 | /// inside it, and its mounts are private, so they don't propagate to the host. |
| 233 | /// | 234 | /// |
| 234 | -/// Must be called from a single-threaded process, because subuid mode forks. The caller is the | 235 | +/// Must be called from a single-threaded process, because the unshare needs one. The caller is |
| 235 | -/// sandbox helper, which has no other threads. | 236 | +/// the sandbox helper, which has no other threads. |
| 236 | pub fn enter_mapped_userns(map: &IdMap) -> Result<(), IdMapError> { | 237 | pub fn enter_mapped_userns(map: &IdMap) -> Result<(), IdMapError> { |
| 237 | match (map.sub_uid, map.sub_gid) { | 238 | match (map.sub_uid, map.sub_gid) { |
| 238 | (Some((uid_start, count_uid)), Some((gid_start, count_gid))) => { | 239 | (Some((uid_start, count_uid)), Some((gid_start, count_gid))) => { |
| @@ -263,9 +264,12 @@ fn enter_single(map: &IdMap) -> Result<(), IdMapError> { | |||
| 263 | .map_err(|e| IdMapError::Map(format!("cannot write uid_map: {e}"))) | 264 | .map_err(|e| IdMapError::Map(format!("cannot write uid_map: {e}"))) |
| 264 | } | 265 | } |
| 265 | 266 | ||
| 266 | -/// What the forked child needs to write the maps. | 267 | +/// The arguments of the hidden `__sandbox-map` helper. It writes the maps for a process that has |
| 268 | +/// just entered a new user namespace. | ||
| 269 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| 267 | struct MapJob { | 270 | struct MapJob { |
| 268 | - parent: i32, | 271 | + /// The pid of the process whose new namespace gets the maps. |
| 272 | + parent: u32, | ||
| 269 | uid: u32, | 273 | uid: u32, |
| 270 | gid: u32, | 274 | gid: u32, |
| 271 | uid_start: u32, | 275 | uid_start: u32, |
| @@ -273,95 +277,135 @@ struct MapJob { | |||
| 273 | count: u32, | 277 | count: u32, |
| 274 | } | 278 | } |
| 275 | 279 | ||
| 276 | -/// Subuid mode. The parent unshares, and a forked child writes the maps with `newuidmap` and | 280 | +impl MapJob { |
| 277 | -/// `newgidmap`. The child can't do this itself, because the maps belong to the parent's new | 281 | + const USAGE: &'static str = |
| 278 | -/// namespace. | 282 | + "usage: __sandbox-map <parent> <uid> <gid> <uid_start> <gid_start> <count>"; |
| 283 | + | ||
| 284 | + fn to_args(self) -> Vec<OsString> { | ||
| 285 | + [ | ||
| 286 | + self.parent, | ||
| 287 | + self.uid, | ||
| 288 | + self.gid, | ||
| 289 | + self.uid_start, | ||
| 290 | + self.gid_start, | ||
| 291 | + self.count, | ||
| 292 | + ] | ||
| 293 | + .iter() | ||
| 294 | + .map(|n| OsString::from(n.to_string())) | ||
| 295 | + .collect() | ||
| 296 | + } | ||
| 297 | + | ||
| 298 | + fn from_args(args: &[OsString]) -> Result<Self, String> { | ||
| 299 | + let [parent, uid, gid, uid_start, gid_start, count] = args else { | ||
| 300 | + return Err(Self::USAGE.into()); | ||
| 301 | + }; | ||
| 302 | + let job = Self { | ||
| 303 | + parent: parse_arg(parent)?, | ||
| 304 | + uid: parse_arg(uid)?, | ||
| 305 | + gid: parse_arg(gid)?, | ||
| 306 | + uid_start: parse_arg(uid_start)?, | ||
| 307 | + gid_start: parse_arg(gid_start)?, | ||
| 308 | + count: parse_arg(count)?, | ||
| 309 | + }; | ||
| 310 | + if job.parent == 0 || job.count == 0 { | ||
| 311 | + return Err(format!( | ||
| 312 | + "parent and count must be positive\n{}", | ||
| 313 | + Self::USAGE | ||
| 314 | + )); | ||
| 315 | + } | ||
| 316 | + Ok(job) | ||
| 317 | + } | ||
| 318 | +} | ||
| 319 | + | ||
| 320 | +fn parse_arg(arg: &OsString) -> Result<u32, String> { | ||
| 321 | + arg.to_str() | ||
| 322 | + .and_then(|s| s.parse().ok()) | ||
| 323 | + .ok_or_else(|| format!("not a number: {}", arg.to_string_lossy())) | ||
| 324 | +} | ||
| 325 | + | ||
| 326 | +/// Subuid mode. A helper process is started before the namespace exists. The parent then unshares | ||
| 327 | +/// and tells the helper to go ahead. The helper runs `newuidmap` and `newgidmap` against the | ||
| 328 | +/// parent's pid, because the maps belong to the parent's new namespace, and this process can't | ||
| 329 | +/// write them itself. | ||
| 279 | /// | 330 | /// |
| 280 | -/// The child waits on a pipe until the parent has unshared. The parent then writes one byte, and | 331 | +/// The helper blocks on its stdin until the parent writes one byte and closes it. The helper |
| 281 | -/// the child runs the two tools. The child reports its failure through a second pipe, which the | 332 | +/// reports failures on stderr, which the parent reads once the helper has exited. |
| 282 | -/// parent reads to EOF before it reaps the child. | ||
| 283 | fn enter_subuid(map: &IdMap, uid_start: u32, gid_start: u32, count: u32) -> Result<(), IdMapError> { | 333 | fn enter_subuid(map: &IdMap, uid_start: u32, gid_start: u32, count: u32) -> Result<(), IdMapError> { |
| 284 | - let pipe_err = |e: Errno| IdMapError::Map(format!("cannot create pipe: {e}")); | ||
| 285 | - let (go_r, go_w) = pipe2(OFlag::O_CLOEXEC).map_err(pipe_err)?; | ||
| 286 | - let (err_r, err_w) = pipe2(OFlag::O_CLOEXEC).map_err(pipe_err)?; | ||
| 287 | let job = MapJob { | 334 | let job = MapJob { |
| 288 | - parent: getpid().as_raw(), | 335 | + parent: std::process::id(), |
| 289 | uid: map.uid, | 336 | uid: map.uid, |
| 290 | gid: map.gid, | 337 | gid: map.gid, |
| 291 | uid_start, | 338 | uid_start, |
| 292 | gid_start, | 339 | gid_start, |
| 293 | count, | 340 | count, |
| 294 | }; | 341 | }; |
| 342 | + let exe = std::env::current_exe() | ||
| 343 | + .map_err(|e| IdMapError::Map(format!("cannot find the runner binary: {e}")))?; | ||
| 344 | + let mut child = Command::new(exe) | ||
| 345 | + .arg(MAP_HELPER_CMD) | ||
| 346 | + .args(job.to_args()) | ||
| 347 | + .stdin(Stdio::piped()) | ||
| 348 | + .stdout(Stdio::null()) | ||
| 349 | + .stderr(Stdio::piped()) | ||
| 350 | + .spawn() | ||
| 351 | + .map_err(|e| IdMapError::Map(format!("cannot start the mapping helper: {e}")))?; | ||
| 352 | + let Some(mut go) = child.stdin.take() else { | ||
| 353 | + stop_child(&mut child); | ||
| 354 | + return Err(IdMapError::Map("no stdin for the mapping helper".into())); | ||
| 355 | + }; | ||
| 295 | 356 | ||
| 296 | - // SAFETY: the helper is single-threaded (see the module docs), so the child is a copy of a | 357 | + let signalled = unshare(CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_NEWNS) |
| 297 | - // process with one thread, and no lock held by another thread can block it. The child only | 358 | + .map_err(|e| IdMapError::Namespace(e.to_string())) |
| 298 | - // reads a pipe, runs std::process::Command, and exits. | 359 | + .and_then(|()| { |
| 299 | - match unsafe { fork() }.map_err(|e| IdMapError::Map(format!("cannot fork: {e}")))? { | 360 | + go.write_all(b"1") |
| 300 | - ForkResult::Child => { | 361 | + .map_err(|e| IdMapError::Map(format!("cannot signal the mapping helper: {e}"))) |
| 301 | - drop(go_w); | 362 | + }); |
| 302 | - drop(err_r); | 363 | + // Closing stdin ends the helper's wait, so it exits instead of blocking if the signal |
| 303 | - let mut go = File::from(go_r); | 364 | + // never landed. |
| 304 | - let mut err = File::from(err_w); | 365 | + drop(go); |
| 305 | - let code = match write_maps_in_child(&mut go, &job) { | 366 | + if let Err(e) = signalled { |
| 306 | - Ok(()) => 0, | 367 | + stop_child(&mut child); |
| 307 | - Err(message) => { | 368 | + return Err(e); |
| 308 | - let _ = err.write_all(message.as_bytes()); | ||
| 309 | - 1 | ||
| 310 | - } | ||
| 311 | - }; | ||
| 312 | - std::process::exit(code) | ||
| 313 | - } | ||
| 314 | - ForkResult::Parent { child } => { | ||
| 315 | - drop(go_r); | ||
| 316 | - drop(err_w); | ||
| 317 | - let mut go_w = File::from(go_w); | ||
| 318 | - let mut err_r = File::from(err_r); | ||
| 319 | - let signalled = unshare(CloneFlags::CLONE_NEWUSER | CloneFlags::CLONE_NEWNS) | ||
| 320 | - .map_err(|e| IdMapError::Namespace(e.to_string())) | ||
| 321 | - .and_then(|()| { | ||
| 322 | - go_w.write_all(b"1").map_err(|e| { | ||
| 323 | - IdMapError::Map(format!("cannot signal the mapping helper: {e}")) | ||
| 324 | - }) | ||
| 325 | - }); | ||
| 326 | - // Closing the pipe tells the child to exit if it is still waiting. | ||
| 327 | - drop(go_w); | ||
| 328 | - let mut message = Vec::new(); | ||
| 329 | - let _ = err_r.read_to_end(&mut message); | ||
| 330 | - let status = wait_for_child(child); | ||
| 331 | - signalled?; | ||
| 332 | - match status { | ||
| 333 | - Ok(WaitStatus::Exited(_, 0)) => {} | ||
| 334 | - Ok(_) => { | ||
| 335 | - return Err(IdMapError::Map(format!( | ||
| 336 | - "cannot map user ids: {}", | ||
| 337 | - String::from_utf8_lossy(&message).trim() | ||
| 338 | - ))); | ||
| 339 | - } | ||
| 340 | - Err(e) => { | ||
| 341 | - return Err(IdMapError::Map(format!( | ||
| 342 | - "waiting for the mapping helper: {e}" | ||
| 343 | - ))); | ||
| 344 | - } | ||
| 345 | - } | ||
| 346 | - // newgidmap leaves setgroups allowed. Drop the supplementary groups the process had | ||
| 347 | - // before the namespace existed. | ||
| 348 | - nix::unistd::setgroups(&[]) | ||
| 349 | - .map_err(|e| IdMapError::Map(format!("cannot clear groups: {e}"))) | ||
| 350 | - } | ||
| 351 | } | 369 | } |
| 370 | + | ||
| 371 | + let output = child | ||
| 372 | + .wait_with_output() | ||
| 373 | + .map_err(|e| IdMapError::Map(format!("waiting for the mapping helper: {e}")))?; | ||
| 374 | + if !output.status.success() { | ||
| 375 | + let stderr = String::from_utf8_lossy(&output.stderr); | ||
| 376 | + let detail = match stderr.trim() { | ||
| 377 | + "" => output.status.to_string(), | ||
| 378 | + text => text.to_owned(), | ||
| 379 | + }; | ||
| 380 | + return Err(IdMapError::Map(format!("cannot map user ids: {detail}"))); | ||
| 381 | + } | ||
| 382 | + // newgidmap leaves setgroups allowed. Drop the supplementary groups the process had before | ||
| 383 | + // the namespace existed. | ||
| 384 | + nix::unistd::setgroups(&[]).map_err(|e| IdMapError::Map(format!("cannot clear groups: {e}"))) | ||
| 352 | } | 385 | } |
| 353 | 386 | ||
| 354 | -/// Runs in the forked child. It waits for the parent's byte, then maps the uids and gids. | 387 | +/// Kills a helper that will not be used, and reaps it so nothing is left behind. |
| 355 | -fn write_maps_in_child(go: &mut File, job: &MapJob) -> Result<(), String> { | 388 | +fn stop_child(child: &mut std::process::Child) { |
| 356 | - let mut byte = [0u8; 1]; | 389 | + let _ = child.kill(); |
| 357 | - loop { | 390 | + let _ = child.wait(); |
| 358 | - match go.read(&mut byte) { | 391 | +} |
| 359 | - Ok(1) => break, | 392 | + |
| 360 | - Ok(_) => return Err("user namespace setup was cancelled".into()), | 393 | +/// `<exe> __sandbox-map <parent> <uid> <gid> <uid_start> <gid_start> <count>`, with the gate byte |
| 361 | - Err(e) if e.kind() == io::ErrorKind::Interrupted => continue, | 394 | +/// on stdin. Waits for the byte, then writes the uid and gid maps of `parent`. Exit 0 on success, |
| 362 | - Err(e) => return Err(format!("cannot read from the parent: {e}")), | 395 | +/// or 125 with a `forge-sandbox:` line on stderr. |
| 396 | +pub fn map_helper_main(args: &[OsString]) -> i32 { | ||
| 397 | + match map_helper(args) { | ||
| 398 | + Ok(()) => 0, | ||
| 399 | + Err(message) => { | ||
| 400 | + eprintln!("forge-sandbox: {message}"); | ||
| 401 | + 125 | ||
| 363 | } | 402 | } |
| 364 | } | 403 | } |
| 404 | +} | ||
| 405 | + | ||
| 406 | +fn map_helper(args: &[OsString]) -> Result<(), String> { | ||
| 407 | + let job = MapJob::from_args(args)?; | ||
| 408 | + wait_for_go(&mut io::stdin().lock())?; | ||
| 365 | let parent = job.parent.to_string(); | 409 | let parent = job.parent.to_string(); |
| 366 | let count = job.count.to_string(); | 410 | let count = job.count.to_string(); |
| 367 | let uid_start = job.uid_start.to_string(); | 411 | let uid_start = job.uid_start.to_string(); |
| @@ -378,6 +422,19 @@ fn write_maps_in_child(go: &mut File, job: &MapJob) -> Result<(), String> { | |||
| 378 | ) | 422 | ) |
| 379 | } | 423 | } |
| 380 | 424 | ||
| 425 | +/// Blocks until the parent writes its byte. EOF before the byte means the parent gave up. | ||
| 426 | +fn wait_for_go(go: &mut impl Read) -> Result<(), String> { | ||
| 427 | + let mut byte = [0u8; 1]; | ||
| 428 | + loop { | ||
| 429 | + match go.read(&mut byte) { | ||
| 430 | + Ok(1) => return Ok(()), | ||
| 431 | + Ok(_) => return Err("user namespace setup was cancelled".into()), | ||
| 432 | + Err(e) if e.kind() == io::ErrorKind::Interrupted => continue, | ||
| 433 | + Err(e) => return Err(format!("cannot read from the parent: {e}")), | ||
| 434 | + } | ||
| 435 | + } | ||
| 436 | +} | ||
| 437 | + | ||
| 381 | fn run_map_tool(tool: &str, args: &[&str]) -> Result<(), String> { | 438 | fn run_map_tool(tool: &str, args: &[&str]) -> Result<(), String> { |
| 382 | let path = find_tool(tool).ok_or_else(|| format!("{tool} is not installed"))?; | 439 | let path = find_tool(tool).ok_or_else(|| format!("{tool} is not installed"))?; |
| 383 | let output = Command::new(&path) | 440 | let output = Command::new(&path) |
| @@ -396,15 +453,6 @@ fn run_map_tool(tool: &str, args: &[&str]) -> Result<(), String> { | |||
| 396 | } | 453 | } |
| 397 | } | 454 | } |
| 398 | 455 | ||
| 399 | -fn wait_for_child(child: Pid) -> Result<WaitStatus, Errno> { | ||
| 400 | - loop { | ||
| 401 | - match waitpid(child, None) { | ||
| 402 | - Err(Errno::EINTR) => continue, | ||
| 403 | - other => return other, | ||
| 404 | - } | ||
| 405 | - } | ||
| 406 | -} | ||
| 407 | - | ||
| 408 | /// `<exe> __sandbox-rm <path>`, with the [`IdMap`] JSON on stdin. Enters the namespace, makes the | 456 | /// `<exe> __sandbox-rm <path>`, with the [`IdMap`] JSON on stdin. Enters the namespace, makes the |
| 409 | /// directories under `path` writable, and removes it. Exit 0 on success, or 125 with a | 457 | /// directories under `path` writable, and removes it. Exit 0 on success, or 125 with a |
| 410 | /// `forge-sandbox:` line on stderr. | 458 | /// `forge-sandbox:` line on stderr. |
| @@ -590,6 +638,45 @@ mod tests { | |||
| 590 | assert_eq!(uneven.size(), 11); | 638 | assert_eq!(uneven.size(), 11); |
| 591 | } | 639 | } |
| 592 | 640 | ||
| 641 | + fn os(args: &[&str]) -> Vec<OsString> { | ||
| 642 | + args.iter().map(OsString::from).collect() | ||
| 643 | + } | ||
| 644 | + | ||
| 645 | + #[test] | ||
| 646 | + fn map_job_args_round_trip() { | ||
| 647 | + let job = MapJob { | ||
| 648 | + parent: 4242, | ||
| 649 | + uid: 1000, | ||
| 650 | + gid: 1001, | ||
| 651 | + uid_start: 524288, | ||
| 652 | + gid_start: 589824, | ||
| 653 | + count: 65536, | ||
| 654 | + }; | ||
| 655 | + let args = job.to_args(); | ||
| 656 | + assert_eq!( | ||
| 657 | + args, | ||
| 658 | + os(&["4242", "1000", "1001", "524288", "589824", "65536"]) | ||
| 659 | + ); | ||
| 660 | + assert_eq!(MapJob::from_args(&args), Ok(job)); | ||
| 661 | + } | ||
| 662 | + | ||
| 663 | + #[test] | ||
| 664 | + fn map_job_args_reject_bad_input() { | ||
| 665 | + let good = ["4242", "1000", "1001", "524288", "589824", "65536"]; | ||
| 666 | + assert!(MapJob::from_args(&os(&good[..5])).is_err(), "too few"); | ||
| 667 | + let mut extra = good.to_vec(); | ||
| 668 | + extra.push("1"); | ||
| 669 | + assert!(MapJob::from_args(&os(&extra)).is_err(), "too many"); | ||
| 670 | + for (index, bad) in [(0, "0"), (1, "-1"), (2, "x"), (3, ""), (5, "0")] { | ||
| 671 | + let mut args = good.to_vec(); | ||
| 672 | + args[index] = bad; | ||
| 673 | + assert!( | ||
| 674 | + MapJob::from_args(&os(&args)).is_err(), | ||
| 675 | + "argument {index} = {bad:?} should be rejected" | ||
| 676 | + ); | ||
| 677 | + } | ||
| 678 | + } | ||
| 679 | + | ||
| 593 | #[test] | 680 | #[test] |
| 594 | fn default_is_single() { | 681 | fn default_is_single() { |
| 595 | let map = IdMap::default(); | 682 | let map = IdMap::default(); |