mod support;
use support::{
FakeGithub, files_under, fixture_token, is_the_secret_store, run, runner_manager,
runner_manager_against,
};
fn wsl_access_canary() -> String {
format!("{}{}", "ghu_", "b1ReceiveAccessCanary0000000000")
}
fn wsl_refresh_canary() -> String {
format!("{}{}", "ghr_", "b1ReceiveRefreshCanary000000000")
}
fn document(access: &str, refresh: &str) -> String {
format!(
r#"{{"access_token":"{access}","refresh_token":"{refresh}",
"access_expires_at":"2026-09-06T20:00:00Z",
"refresh_expires_at":"2027-03-05T12:00:00Z"}}"#
)
}
const INVALID_ARGUMENT: i32 = 9;
const NOT_AUTHENTICATED: i32 = 3;
#[test]
fn receive_is_absent_from_help_and_still_runs() {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let help = run({
let mut command = runner_manager(data_dir.path());
command.args(["auth", "--help"]);
command
});
assert_eq!(help.code, 0, "`auth --help` must succeed: {}", help.stderr);
assert!(
!help.stdout.contains("receive"),
"`02-target-architecture.md` hides this command from ordinary help, and \
`cli_command_surface.rs` asserts the published list is exhaustive:\n{}",
help.stdout
);
let own_help = run({
let mut command = runner_manager(data_dir.path());
command.args(["auth", "receive", "--help"]);
command
});
assert_eq!(
own_help.code, 0,
"the hidden command must still be reachable: {}",
own_help.stderr
);
assert!(
own_help.stdout.contains("--start-at"),
"got: {}",
own_help.stdout
);
}
#[test]
fn receive_refuses_to_guess_a_start_mode() {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let outcome = run({
let mut command = runner_manager(data_dir.path());
command
.args(["auth", "receive"])
.write_stdin(document(&wsl_access_canary(), &wsl_refresh_canary()));
command
});
assert_ne!(outcome.code, 0, "a missing start mode must not be guessed");
assert!(
outcome.stderr.contains("--start-at"),
"the refusal must name what is missing: {}",
outcome.stderr
);
}
#[test]
fn a_received_credential_is_one_auth_status_reports_as_authenticated() {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let github = FakeGithub::start();
github.with_installation(11, "acme", "Organization", "selected", &["acme/repo"]);
let received = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command
.args(["auth", "receive", "--start-at", "boot"])
.write_stdin(document(&fixture_token(), &wsl_refresh_canary()));
command
});
assert_eq!(
received.code,
0,
"the handoff must succeed:\n{}",
received.both()
);
assert!(
received.stdout.contains("machine-scoped store"),
"the report names the store it wrote, and nothing about the value: {}",
received.stdout
);
assert!(
received.stdout.contains("renews itself"),
"a pair with a refresh half must be reported as renewable: {}",
received.stdout
);
let status = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command.args(["auth", "status"]);
command
});
assert_eq!(
status.code,
0,
"the host is signed in with the credential it was handed:\n{}",
status.both()
);
}
#[test]
fn receiving_for_a_start_mode_records_it_so_the_host_reads_the_store_it_wrote() {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let github = FakeGithub::start();
github.with_installation(11, "acme", "Organization", "selected", &["acme/repo"]);
let received = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command
.args(["auth", "receive", "--start-at", "login"])
.write_stdin(document(&fixture_token(), &wsl_refresh_canary()));
command
});
assert_eq!(
received.code,
0,
"the handoff must succeed:\n{}",
received.both()
);
assert!(
received.stdout.contains("user-scoped store"),
"`--start-at login` writes the user-scoped store: {}",
received.stdout
);
let status = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command.args(["auth", "status"]);
command
});
assert_eq!(
status.code,
0,
"the store this host reads must be the one the handoff wrote:\n{}",
status.both()
);
}
#[test]
fn every_refusal_leaves_the_host_with_nothing_stored() {
let oversized = "x".repeat(64 * 1024 + 1);
let truncated = format!(r#"{{"access_token":"{}"#, wsl_access_canary());
let cases: Vec<(&str, &str)> = vec![
("an empty document", ""),
("whitespace only", " \n"),
("prose", "not a credential at all"),
(
"an HTML error page",
"<html><body>502 Bad Gateway</body></html>",
),
("an object with no access token", r#"{"refresh_token":"x"}"#),
("an empty access token", r#"{"access_token":""}"#),
("a JSON array", r#"["ghu_looksLikeAToken"]"#),
("a document one byte over the ceiling", oversized.as_str()),
("a truncated document", truncated.as_str()),
(
"a document whose access expiry is not an instant",
r#"{"access_token":"ghu_x","access_expires_at":"tomorrow"}"#,
),
(
"a document whose refresh token is not a string",
r#"{"access_token":"ghu_x","refresh_token":1234}"#,
),
];
for (what, input) in cases {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let github = FakeGithub::start();
github.with_installation(11, "acme", "Organization", "selected", &["acme/repo"]);
let refused = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command
.args(["auth", "receive", "--start-at", "boot"])
.write_stdin(input.to_string());
command
});
assert_eq!(
refused.code,
INVALID_ARGUMENT,
"{what} must be refused as an invalid argument:\n{}",
refused.both()
);
assert!(
refused.stdout.is_empty(),
"{what} must produce no report at all, and produced: {}",
refused.stdout
);
assert!(
refused.stderr.contains("Nothing was stored"),
"{what} must say plainly that nothing was stored: {}",
refused.stderr
);
let status = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command.args(["auth", "status"]);
command
});
assert_eq!(
status.code,
NOT_AUTHENTICATED,
"after {what} the host must hold no credential at all:\n{}",
status.both()
);
}
}
#[test]
fn no_part_of_a_received_credential_reaches_the_output_or_any_file_but_the_store() {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let received = run({
let mut command = runner_manager(data_dir.path());
command
.env("RUST_LOG", "trace")
.args(["auth", "receive", "--start-at", "boot"])
.write_stdin(document(&wsl_access_canary(), &wsl_refresh_canary()));
command
});
assert_eq!(
received.code,
0,
"the handoff must succeed, or this scan measures nothing:\n{}",
received.both()
);
let mut corpus = vec![
("the command's stdout".to_string(), received.stdout.clone()),
("the command's stderr".to_string(), received.stderr.clone()),
];
let mut files_seen = 0_usize;
for path in files_under(data_dir.path()) {
if is_the_secret_store(&path) {
continue;
}
let Ok(bytes) = std::fs::read(&path) else {
continue;
};
files_seen += 1;
corpus.push((
format!("the file {}", path.display()),
String::from_utf8_lossy(&bytes).into_owned(),
));
}
assert!(
files_seen > 0,
"the run left no files to scan, so a clean result would mean nothing"
);
let found = scan(&corpus);
assert!(
found.is_empty(),
"`03-security-and-lifecycle.md` guarantee 3: the credential document is absent from \
logs, errors, status JSON and temporary files. Found:\n {}",
found.join("\n ")
);
let planted: Vec<(String, String)> = needles()
.into_iter()
.map(|(name, value)| {
(
format!("a planted fragment for {name}"),
format!("prefix {value} suffix"),
)
})
.collect();
assert_eq!(
scan(&planted).len(),
needles().len(),
"the scanner must find every planted needle, or the clean result above says nothing"
);
}
fn needles() -> Vec<(&'static str, String)> {
vec![
("the access token", wsl_access_canary()),
("the refresh token", wsl_refresh_canary()),
]
}
fn scan(corpus: &[(String, String)]) -> Vec<String> {
let mut found = Vec::new();
for (name, needle) in needles() {
for (origin, text) in corpus {
if text.contains(&needle) {
found.push(format!("{name} appears in {origin}"));
}
}
}
found
}