277 lines
8.9 KiB
Rust
277 lines
8.9 KiB
Rust
// SPDX-License-Identifier: CPAL-1.0
|
|
// Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution
|
|
// License 1.0: see LICENSE (its Exhibit A is this file's notice).
|
|
|
|
//! The routes repository holds exactly `ianitor.toml` and `hosts/<hostname>.toml`, and the
|
|
//! loader requires the exact path (ROADMAP §10: the check used to look at the base name only).
|
|
//! Validation errors carry a stable code, a field path and params for the UI (docs/ui-api.md §3.1).
|
|
|
|
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
|
|
use ianitor_core::routes::{EntryKind, RoutePath, check_path, check_tree};
|
|
use ianitor_core::{SourceFile, ValidationError, load};
|
|
|
|
#[test]
|
|
fn only_the_site_file_and_host_files_are_allowed() {
|
|
assert_eq!(
|
|
check_path("ianitor.toml", EntryKind::File).unwrap(),
|
|
RoutePath::Site
|
|
);
|
|
assert_eq!(
|
|
check_path("hosts/wiki.home.test.toml", EntryKind::File).unwrap(),
|
|
RoutePath::Host {
|
|
hostname: "wiki.home.test".into()
|
|
}
|
|
);
|
|
for bad in [
|
|
"README.md",
|
|
".gitattributes",
|
|
"hosts.toml",
|
|
"other/wiki.home.test.toml",
|
|
"hosts/sub/wiki.home.test.toml",
|
|
"hosts/wiki.home.test.toml/x",
|
|
"hosts/Wiki.home.test.toml",
|
|
"hosts/wiki.home.test.TOML",
|
|
"hosts/wiki.home.test",
|
|
"hosts/.toml",
|
|
"hosts/*.home.test.toml",
|
|
"hosts/wiki..test.toml",
|
|
"hosts/10.0.0.1.toml",
|
|
"hosts/wiki home.toml",
|
|
"hosts\\wiki.home.test.toml",
|
|
"hosts/wiki\\x.toml",
|
|
"IANITOR.toml",
|
|
"./ianitor.toml",
|
|
] {
|
|
let e = check_path(bad, EntryKind::File).unwrap_err();
|
|
assert!(
|
|
matches!(e, ValidationError::UnexpectedFile { ref file, .. } if file == bad),
|
|
"{bad}: {e:?}"
|
|
);
|
|
assert_eq!(e.code(), "repo.unexpected_file");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn links_executables_and_submodules_are_refused() {
|
|
for kind in [
|
|
EntryKind::Symlink,
|
|
EntryKind::Submodule,
|
|
EntryKind::Executable,
|
|
] {
|
|
assert!(check_path("hosts/a.test.toml", kind).is_err(), "{kind:?}");
|
|
assert!(check_path("ianitor.toml", kind).is_err(), "{kind:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_tree_needs_the_site_file_and_reports_every_stray_file() {
|
|
let ok = [
|
|
("ianitor.toml", EntryKind::File),
|
|
("hosts/a.test.toml", EntryKind::File),
|
|
];
|
|
assert!(check_tree(ok).is_ok());
|
|
let errs = check_tree([
|
|
("hosts/a.test.toml", EntryKind::File),
|
|
("notes.txt", EntryKind::File),
|
|
("hosts/b/c.toml", EntryKind::File),
|
|
])
|
|
.unwrap_err();
|
|
assert_eq!(errs.len(), 3, "{errs:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn the_loader_requires_the_exact_path() {
|
|
let site = SourceFile {
|
|
name: "ianitor.toml",
|
|
contents: "",
|
|
};
|
|
let body = "hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n";
|
|
for name in [
|
|
"app.test.toml",
|
|
"other/app.test.toml",
|
|
"hosts/x/app.test.toml",
|
|
"hosts\\app.test.toml",
|
|
"./hosts/app.test.toml",
|
|
] {
|
|
let errs = load(
|
|
&site,
|
|
&[SourceFile {
|
|
name,
|
|
contents: body,
|
|
}],
|
|
)
|
|
.unwrap_err();
|
|
assert!(
|
|
errs.errors()
|
|
.iter()
|
|
.any(|e| matches!(e, ValidationError::FileNameMismatch { .. })),
|
|
"{name}: {errs:?}"
|
|
);
|
|
}
|
|
assert!(
|
|
load(
|
|
&site,
|
|
&[SourceFile {
|
|
name: "hosts/app.test.toml",
|
|
contents: body
|
|
}]
|
|
)
|
|
.is_ok()
|
|
);
|
|
}
|
|
|
|
fn errors_for(body: &str) -> Vec<ValidationError> {
|
|
let site = SourceFile {
|
|
name: "ianitor.toml",
|
|
contents: "secrets = [\"api_key\"]\n",
|
|
};
|
|
load(
|
|
&site,
|
|
&[SourceFile {
|
|
name: "hosts/app.test.toml",
|
|
contents: body,
|
|
}],
|
|
)
|
|
.unwrap_err()
|
|
.into_errors()
|
|
}
|
|
|
|
#[test]
|
|
fn errors_have_codes_fields_and_params_for_the_ui() {
|
|
let cases: &[(&str, &str, Option<&str>)] = &[
|
|
(
|
|
"upstreams = [\"http://127.0.0.1:1\"]\n",
|
|
"host.hostname.missing",
|
|
Some("hostname"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\n",
|
|
"host.upstreams.missing",
|
|
Some("upstreams"),
|
|
),
|
|
(
|
|
"hostname = \"App.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
|
"host.hostname.uppercase",
|
|
Some("hostname"),
|
|
),
|
|
(
|
|
"hostname = \"app test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
|
"host.hostname.spaces",
|
|
Some("hostname"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\", \"ftp://x\"]\n",
|
|
"host.upstream.scheme",
|
|
Some("upstreams[1]"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1/x\"]\n",
|
|
"host.upstream.path",
|
|
Some("upstreams[0]"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\", \"http://127.0.0.1:1\"]\n",
|
|
"host.upstream.duplicate",
|
|
Some("upstreams[1]"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\", \"https://127.0.0.1:2\"]\n",
|
|
"host.upstream.mixed_schemes",
|
|
Some("upstreams"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\ncolour = 1\n",
|
|
"host.field.unknown",
|
|
Some("colour"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nvalue = \"1\"\n[[request_headers]]\naction = \"set\"\nname = \"Host\"\nvalue = \"x\"\n",
|
|
"host.header.protected",
|
|
Some("request_headers[1].name"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[[response_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"api_key\"\n",
|
|
"host.header.secret_response",
|
|
Some("response_headers[0]"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"nope\"\n",
|
|
"host.header.secret_unknown",
|
|
Some("request_headers[0].secret"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[health_check]\npath = \"/x\"\ninterval_secs = 0\n",
|
|
"host.health.interval",
|
|
Some("health_check.interval_secs"),
|
|
),
|
|
(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\nauth = \"forward-auth\"\n",
|
|
"host.auth.not_configured",
|
|
Some("auth"),
|
|
),
|
|
];
|
|
for (body, code, field) in cases {
|
|
let errs = errors_for(body);
|
|
let e = errs
|
|
.iter()
|
|
.find(|e| e.code() == *code)
|
|
.unwrap_or_else(|| panic!("no {code} for {body:?}: {errs:?}"));
|
|
assert_eq!(e.field().as_deref(), *field, "{code}");
|
|
assert_eq!(e.params()["file"], "hosts/app.test.toml");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn params_carry_suggestions() {
|
|
let errs = errors_for("hostname = \"App.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n");
|
|
let e = errs
|
|
.iter()
|
|
.find(|e| e.code() == "host.hostname.uppercase")
|
|
.unwrap();
|
|
assert_eq!(e.params()["suggestion"], "app.test");
|
|
let errs = errors_for("hostname = \"app.test\"\nupstreams = [\"http://10.0.0.2:3000/app\"]\n");
|
|
let e = errs
|
|
.iter()
|
|
.find(|e| e.code() == "host.upstream.path")
|
|
.unwrap();
|
|
assert_eq!(e.params()["suggestion"], "http://10.0.0.2:3000");
|
|
assert_eq!(e.params()["url"], "http://10.0.0.2:3000/app");
|
|
}
|
|
|
|
#[test]
|
|
fn a_password_in_an_upstream_url_is_never_repeated() {
|
|
let errs = errors_for(
|
|
"hostname = \"app.test\"\nupstreams = [\"http://admin:hunter2@10.0.0.2:3000\", \"ftp://u:hunter2@x\", \"http://admin:hunter2@10.0.0.2:3000\"]\n",
|
|
);
|
|
assert!(!errs.is_empty());
|
|
for e in &errs {
|
|
let all = format!("{e} {:?} {:?}", e.params(), e.field());
|
|
assert!(!all.contains("hunter2"), "{all}");
|
|
}
|
|
let e = errs
|
|
.iter()
|
|
.find(|e| e.code() == "host.upstream.credentials")
|
|
.unwrap();
|
|
assert_eq!(e.params()["url"], "http://\u{2026}@10.0.0.2:3000");
|
|
}
|
|
|
|
#[test]
|
|
fn credentials_with_slash_query_or_hash_are_never_repeated() {
|
|
for url in [
|
|
"http://admin:hunter/2@10.0.0.1:80",
|
|
"http://admin:hunter?2@10.0.0.1:80",
|
|
"http://admin:hunter#2@10.0.0.1:80",
|
|
"ftp://admin:hunter2@10.0.0.1",
|
|
] {
|
|
let errs = errors_for(&format!(
|
|
"hostname = \"app.test\"\nupstreams = [\"{url}\"]\n"
|
|
));
|
|
assert!(!errs.is_empty(), "{url} accepted");
|
|
for e in &errs {
|
|
let all = format!("{e} {:?}", e.params());
|
|
assert!(!all.contains("hunter"), "{url}: {all}");
|
|
}
|
|
}
|
|
}
|