rustforge
RustForge: the read-only, cgit-style git web host serving this site
10641 bytes ยท raw
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | //! End-to-end tests: build real git repositories in a temp dir, then check
//! the HTML pages and do a real `git clone` against a running server.
use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt;
use rustforge::config::Config;
use rustforge::web::{router, AppState};
use std::path::Path;
use std::process::Command;
use tower::ServiceExt; // for `oneshot`
/// Run a git command in `dir`, panicking with its output if it fails.
fn git(dir: &Path, args: &[&str]) -> String {
let out = Command::new("git")
.args([
"-c",
"user.name=Test User",
"-c",
"user.email=test@example.com",
"-c",
"init.defaultBranch=main",
])
.args(args)
.current_dir(dir)
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_AUTHOR_DATE", "2026-01-01T12:00:00Z")
.env("GIT_COMMITTER_DATE", "2026-01-01T12:00:00Z")
.output()
.expect("git is installed");
assert!(out.status.success(), "git {args:?} failed: {}", String::from_utf8_lossy(&out.stderr));
String::from_utf8_lossy(&out.stdout).into_owned()
}
/// A temp dir holding: `work/` (normal repo, 56 commits) and `bare.git`.
struct Fixture {
dir: tempfile::TempDir,
head: String,
change_commit: String,
}
fn fixture() -> Fixture {
let dir = tempfile::tempdir().unwrap();
let work = dir.path().join("work");
std::fs::create_dir(&work).unwrap();
git(&work, &["init", "-q"]);
std::fs::write(
work.join("README.md"),
"# Hello Readme\n\nSome **bold** text.\n\n<script>alert('readme')</script>\n",
)
.unwrap();
std::fs::create_dir(work.join("src")).unwrap();
std::fs::write(work.join("src/main.rs"), "fn main() {\n println!(\"old line\");\n}\n").unwrap();
std::fs::write(work.join("src/odd name #1.txt"), "spaces & hashes <ok>\n").unwrap();
git(&work, &["add", "."]);
git(&work, &["commit", "-q", "-m", "initial commit"]);
// 54 filler commits so the log has more than one page (50 per page).
for i in 0..54 {
git(&work, &["commit", "-q", "--allow-empty", "-m", &format!("filler {i}")]);
}
std::fs::write(work.join("src/main.rs"), "fn main() {\n println!(\"new line\");\n}\n").unwrap();
git(&work, &["commit", "-q", "-am", "change <greeting>"]);
let change_commit = git(&work, &["rev-parse", "HEAD"]).trim().to_string();
git(&work, &["tag", "v1.0"]);
git(&work, &["branch", "feature/x"]);
let head = change_commit.clone();
git(dir.path(), &["clone", "-q", "--bare", "work", "bare.git"]);
Fixture { dir, head, change_commit }
}
fn state_for(f: &Fixture) -> rustforge::web::Shared {
let toml = format!(
r#"
[server]
site_title = "test site"
clone_base_url = "http://example.test"
[[repo]]
name = "work"
path = "{}"
description = "non-bare <b>repo</b>"
[[repo]]
name = "bare"
path = "{}"
description = "a bare repo"
"#,
f.dir.path().join("work").display(),
f.dir.path().join("bare.git").display(),
);
let cfg = Config::from_toml(&toml).unwrap();
let repos = cfg.validate().unwrap();
AppState::new(cfg.server, repos)
}
/// Make one request against the router (no network) and return status + body.
async fn get(f: &Fixture, uri: &str) -> (StatusCode, String) {
let app = router(state_for(f));
let res = app.oneshot(Request::get(uri).body(Body::empty()).unwrap()).await.unwrap();
let status = res.status();
let bytes = res.into_body().collect().await.unwrap().to_bytes();
(status, String::from_utf8_lossy(&bytes).into_owned())
}
#[tokio::test]
async fn index_lists_repos_and_escapes_descriptions() {
let f = fixture();
let (status, body) = get(&f, "/").await;
assert_eq!(status, StatusCode::OK);
assert!(body.contains("href=\"/work/\""));
assert!(body.contains("href=\"/bare/\""));
assert!(body.contains("non-bare <b>repo</b>"));
assert!(!body.contains("<b>repo</b>"));
assert!(body.contains("ago"), "idle column should show an age");
assert!(body.contains("<title>test site</title>"));
assert!(body.contains("generated by RustForge v"));
let (_, body) = get(&f, "/work/log").await;
assert!(body.contains("<title>log - work - test site</title>"), "{body}");
}
#[tokio::test]
async fn summary_shows_refs_commits_and_safe_readme() {
let f = fixture();
for repo in ["work", "bare"] {
let (status, body) = get(&f, &format!("/{repo}/")).await;
assert_eq!(status, StatusCode::OK, "{repo}");
assert!(body.contains("feature/x"), "branch listed");
assert!(body.contains("v1.0"), "tag listed");
assert!(body.contains("change <greeting>"), "commit subject escaped");
assert!(body.contains("<h1>Hello Readme</h1>"), "README rendered");
assert!(body.contains("<strong>bold</strong>"));
assert!(!body.contains("<script>"), "raw HTML in README must not pass through");
assert!(body.contains(&format!("git clone http://example.test/{repo}")));
}
// /work redirects to /work/
let (status, _) = get(&f, "/work").await;
assert_eq!(status, StatusCode::PERMANENT_REDIRECT);
}
#[tokio::test]
async fn log_paginates() {
let f = fixture();
let (status, page1) = get(&f, "/work/log").await;
assert_eq!(status, StatusCode::OK);
assert!(page1.contains("older ยป"));
assert!(!page1.contains("ยซ newer"));
assert!(!page1.contains("initial commit"));
let (_, page2) = get(&f, "/work/log?ofs=50").await;
assert!(page2.contains("initial commit"));
assert!(page2.contains("ยซ newer"));
assert!(!page2.contains("older ยป"));
// A branch name with a slash works as ?h=
let (status, _) = get(&f, "/work/log?h=feature%2Fx").await;
assert_eq!(status, StatusCode::OK);
}
#[tokio::test]
async fn commit_page_shows_colored_diff() {
let f = fixture();
let (status, body) = get(&f, &format!("/work/commit/{}", f.change_commit)).await;
assert_eq!(status, StatusCode::OK);
assert!(body.contains("src/main.rs"));
assert!(body.contains("class=\"del\""), "{body}");
assert!(body.contains("class=\"add\""));
assert!(body.contains("println!("new line");") || body.contains("println!(\"new line\");"));
// short ids work too
let (status, _) = get(&f, &format!("/bare/commit/{}", &f.change_commit[..8])).await;
assert_eq!(status, StatusCode::OK);
}
#[tokio::test]
async fn tree_blob_and_raw() {
let f = fixture();
let (status, body) = get(&f, "/work/tree").await;
assert_eq!(status, StatusCode::OK);
assert!(body.contains("href=\"/work/tree/src\""));
assert!(body.contains("README.md"));
let (status, body) = get(&f, "/work/tree/src?h=v1.0").await;
assert_eq!(status, StatusCode::OK);
assert!(body.contains("/work/blob/src/odd%20name%20%231.txt?h=v1.0"));
let (status, body) = get(&f, "/work/blob/src/main.rs").await;
assert_eq!(status, StatusCode::OK);
assert!(body.contains("id=\"n3\""), "line numbers");
assert!(body.contains("hl-"), "syntax highlighting classes");
let (status, body) = get(&f, "/work/blob/src/odd%20name%20%231.txt").await;
assert_eq!(status, StatusCode::OK);
assert!(body.contains("spaces & hashes <ok>"));
let (status, body) = get(&f, "/bare/raw/src/main.rs").await;
assert_eq!(status, StatusCode::OK);
assert_eq!(body, "fn main() {\n println!(\"new line\");\n}\n");
// Old version via ?h=<first commit>
let first = git(&f.dir.path().join("work"), &["rev-list", "--max-parents=0", "HEAD"]);
let (_, body) = get(&f, &format!("/work/raw/src/main.rs?h={}", first.trim())).await;
assert!(body.contains("old line"));
assert!(!f.head.is_empty());
}
#[tokio::test]
async fn refuses_traversal_unknown_repos_and_odd_refs() {
let f = fixture();
for uri in [
"/work/tree/..",
"/work/tree/../../etc",
"/work/tree/src/..%2F..%2F..%2Fetc%2Fpasswd",
"/work/raw/%2E%2E/%2E%2E/etc/passwd",
"/work/blob/.git/config",
"/nope/",
"/..%2F/",
"/.git/",
"/work/log?h=HEAD@%7B1%7D",
"/work/log?h=main%5E",
"/work/log?h=--all",
"/work/commit/zzzz",
] {
let (status, _) = get(&f, uri).await;
assert_eq!(status, StatusCode::NOT_FOUND, "{uri}");
}
let (status, _) = get(&f, "/work/info/refs?service=git-receive-pack").await;
assert_eq!(status, StatusCode::FORBIDDEN);
}
#[tokio::test(flavor = "multi_thread")]
async fn git_clone_over_http_works() {
let f = fixture();
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let app = router(state_for(&f));
tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let out_dir = tempfile::tempdir().unwrap();
// Protocol v2 (modern default) and v0 (older clients), bare and non-bare.
for (repo, proto) in [("work", "2"), ("bare", "2"), ("work.git", "0"), ("bare", "0")] {
let dest = out_dir.path().join(format!("{repo}-v{proto}"));
let url = format!("http://{addr}/{repo}");
let dest_s = dest.to_str().unwrap().to_string();
let proto_cfg = format!("protocol.version={proto}");
let status = tokio::task::spawn_blocking(move || {
Command::new("git")
.args(["-c", &proto_cfg, "clone", "-q", &url, &dest_s])
.env("GIT_TERMINAL_PROMPT", "0")
.status()
.unwrap()
})
.await
.unwrap();
assert!(status.success(), "clone of {repo} (protocol v{proto}) failed");
let head = git(&dest, &["rev-parse", "HEAD"]);
assert_eq!(head.trim(), f.head, "{repo}");
let tags = git(&dest, &["tag"]);
assert!(tags.contains("v1.0"));
let fsck = git(&dest, &["fsck", "--no-progress"]);
assert!(!fsck.contains("error"));
}
// Pushing must fail.
let work = out_dir.path().join("work-v2");
let url = format!("http://{addr}/work");
let status = tokio::task::spawn_blocking(move || {
Command::new("git")
.args(["push", "-q", &url, "HEAD:refs/heads/evil"])
.current_dir(&work)
.env("GIT_TERMINAL_PROMPT", "0")
.stderr(std::process::Stdio::null())
.status()
.unwrap()
})
.await
.unwrap();
assert!(!status.success(), "push must be refused");
}
|