oxedyne/fe2o3/fe2o3_steel/tests/docroot_link.rs
8.7 KiB, 12 runs
created by r141143373:77846, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! A docroot that is a link is followed on every request, so one rename of the link moves a vhost |
| 2 | //! to another folder with no restart. Steel resolves `public_dir_rel` once at start, lexically and |
| 3 | //! never canonically, and opens each file through it per request. Two pages of one length and one |
| 4 | //! second, one in each folder, never share an entity tag, because the tag digests device and inode. |
| 5 | //! |
| 6 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 7 | //! Anthropic Claude |
| 8 | |
| 9 | #![cfg(unix)] |
| 10 | |
| 11 | mod vhost_rig; |
| 12 | |
| 13 | use vhost_rig::{ |
| 14 | Rig, |
| 15 | Scratch, |
| 16 | Site, |
| 17 | }; |
| 18 | |
| 19 | use oxedyne_fe2o3_steel::srv::cfg::VhostConfig; |
| 20 | |
| 21 | use oxedyne_fe2o3_core::{ |
| 22 | path::NormalPath, |
| 23 | prelude::*, |
| 24 | }; |
| 25 | |
| 26 | use std::{ |
| 27 | os::unix::fs::symlink, |
| 28 | time::{ |
| 29 | Duration, |
| 30 | UNIX_EPOCH, |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | const HOST: &str = "static.test"; |
| 35 | const SECS: u64 = 1_700_000_000; // the one modification second every page is given |
| 36 | |
| 37 | /// Folders `a` and `b`, each a page and a script of one length and second, and `live` naming `a`, |
| 38 | /// served by a rig started on `live`. |
| 39 | fn on_link(label: &str) -> Outcome<(Scratch, Rig, tokio::runtime::Runtime)> { |
| 40 | let web = res!(Scratch::new(label)); |
| 41 | for (dir, who) in [("a", "A"), ("b", "B")] { |
| 42 | res!(deploy(&web, &fmt!("{}/index.html", dir), &fmt!("<p>page {}</p>", who))); |
| 43 | res!(deploy(&web, &fmt!("{}/x.js", dir), &fmt!("let who = '{}';", who))); |
| 44 | } |
| 45 | res!(symlink("a", web.0.join("live"))); |
| 46 | let rig = res!(Rig::new(vec![Site::new(HOST, &web.0.join("live"))])); |
| 47 | Ok((web, rig, res!(vhost_rig::runtime()))) |
| 48 | } |
| 49 | |
| 50 | /// Puts `text` at `rel` as a deploy does: written beside it, dated `SECS`, renamed over it. |
| 51 | fn deploy(web: &Scratch, rel: &str, text: &str) -> Outcome<()> { |
| 52 | let tmp = fmt!("{}.tmp", rel); |
| 53 | res!(web.put(&tmp, text)); |
| 54 | let file = res!(std::fs::File::options().write(true).open(web.0.join(&tmp))); |
| 55 | res!(file.set_modified(UNIX_EPOCH + Duration::from_secs(SECS))); |
| 56 | res!(std::fs::rename(web.0.join(&tmp), web.0.join(rel))); |
| 57 | Ok(()) |
| 58 | } |
| 59 | |
| 60 | /// Points `live` at `target` by one rename of a fresh link over it, as the move does. |
| 61 | fn flip(web: &Scratch, target: &str) -> Outcome<()> { |
| 62 | let tmp = web.0.join(".live.tmp"); |
| 63 | res!(symlink(target, &tmp)); |
| 64 | res!(std::fs::rename(&tmp, web.0.join("live"))); |
| 65 | Ok(()) |
| 66 | } |
| 67 | |
| 68 | fn etag(r: &vhost_rig::Reply) -> Outcome<String> { |
| 69 | r.field("etag").ok_or_else(|| err!("No entity tag: {:?}.", r.head; Test, Missing)) |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn a_renamed_docroot_link_is_served_at_the_next_request() -> Outcome<()> { |
| 74 | let (web, rig, rt) = res!(on_link("link_flip")); |
| 75 | res!(rt.block_on(async { |
| 76 | let r = res!(rig.fetch(HOST, "GET", "/", &[], "").await); |
| 77 | assert!(r.status() == 200 && r.body.contains("page A"), "{:?}", r); |
| 78 | // The same server, not restarted, asked with no validator. |
| 79 | res!(flip(&web, "b")); |
| 80 | for (path, want) in [("/", "page B"), ("/x.js", "'B'")] { |
| 81 | let r = res!(rig.fetch(HOST, "GET", path, &[], "").await); |
| 82 | assert!(r.status() == 200 && r.body.contains(want), "GET {}: {:?}", path, r); |
| 83 | } |
| 84 | res!(flip(&web, "a")); |
| 85 | let r = res!(rig.fetch(HOST, "GET", "/", &[], "").await); |
| 86 | assert!(r.body.contains("page A"), "after the way back: {:?}", r); |
| 87 | Ok::<(), Error<ErrTag>>(()) |
| 88 | })); |
| 89 | // A Steel started on the link as it now stands serves the folder it names. |
| 90 | res!(flip(&web, "b")); |
| 91 | let rig = res!(Rig::new(vec![Site::new(HOST, &web.0.join("live"))])); |
| 92 | rt.block_on(async { |
| 93 | let r = res!(rig.fetch(HOST, "GET", "/", &[], "").await); |
| 94 | assert!(r.body.contains("page B"), "after a restart on the flipped link: {:?}", r); |
| 95 | Ok(()) |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn a_tag_from_one_folder_never_matches_the_other() -> Outcome<()> { |
| 101 | let (web, rig, rt) = res!(on_link("link_tag")); |
| 102 | rt.block_on(async { |
| 103 | let tag = res!(etag(&res!(rig.fetch(HOST, "GET", "/", &[], "").await))); |
| 104 | // A tab open on A's page revalidates after the flip, and must be given B's. |
| 105 | res!(flip(&web, "b")); |
| 106 | let held = fmt!("If-None-Match: {}", tag); |
| 107 | let r = res!(rig.fetch(HOST, "GET", "/", &[&held], "").await); |
| 108 | assert_eq!(r.status(), 200, "A's tag {} was taken for B's page: {:?}", tag, r); |
| 109 | assert!(r.body.contains("page B"), "{:?}", r); |
| 110 | Ok(()) |
| 111 | }) |
| 112 | } |
| 113 | |
| 114 | /// Pages renamed over one path, the first and third of one length and second, never share a tag, |
| 115 | /// though ext4 hands the first one's inode number straight to the third. |
| 116 | #[test] |
| 117 | fn a_tag_never_matches_a_later_page_at_the_same_path() -> Outcome<()> { |
| 118 | let (web, rig, rt) = res!(on_link("link_redeploy")); |
| 119 | rt.block_on(async { |
| 120 | for round in 0..5 { |
| 121 | res!(deploy(&web, "a/index.html", "<p>page v1</p>")); |
| 122 | let tag = res!(etag(&res!(rig.fetch(HOST, "GET", "/", &[], "").await))); |
| 123 | res!(deploy(&web, "a/index.html", "<p>page v2, longer</p>")); |
| 124 | res!(deploy(&web, "a/index.html", "<p>page v3</p>")); |
| 125 | let held = fmt!("If-None-Match: {}", tag); |
| 126 | let r = res!(rig.fetch(HOST, "GET", "/", &[&held], "").await); |
| 127 | assert!(r.status() == 200 && r.body.contains("page v3"), |
| 128 | "round {}: v1's tag {} was taken for v3: {:?}", round, tag, r); |
| 129 | } |
| 130 | Ok(()) |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn a_tag_issued_before_this_change_misses_once() -> Outcome<()> { |
| 136 | let (web, rig, rt) = res!(on_link("link_old_tag")); |
| 137 | let len = res!(std::fs::metadata(web.0.join("a/index.html"))).len(); |
| 138 | rt.block_on(async { |
| 139 | // A page left open across the deploy holds a tag of the earlier form. |
| 140 | let old = fmt!("If-None-Match: \"{:x}-{:x}\"", SECS, len); |
| 141 | let r = res!(rig.fetch(HOST, "GET", "/", &[&old], "").await); |
| 142 | assert_eq!(r.status(), 200, "the earlier form still validates: {:?}", r); |
| 143 | let held = fmt!("If-None-Match: {}", res!(etag(&r))); |
| 144 | let r = res!(rig.fetch(HOST, "GET", "/", &[&held], "").await); |
| 145 | assert_eq!(r.status(), 304, "the tag just given does not validate: {:?}", r); |
| 146 | Ok(()) |
| 147 | }) |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn the_same_file_still_revalidates_plain_and_coded() -> Outcome<()> { |
| 152 | let (web, rig, rt) = res!(on_link("link_coded")); |
| 153 | res!(web.put("a/big.js", &"let who = 'A'; // a line the encoder can shrink\n".repeat(100))); |
| 154 | rt.block_on(async { |
| 155 | for coding in [None, Some("gzip")] { |
| 156 | let accept = coding.map(|c| fmt!("Accept-Encoding: {}", c)); |
| 157 | let mut fields: Vec<&str> = accept.iter().map(|s| s.as_str()).collect(); |
| 158 | let r = res!(rig.fetch(HOST, "GET", "/big.js", &fields, "").await); |
| 159 | assert_eq!(r.field("content-encoding").as_deref(), coding, "{:?}", r.head); |
| 160 | let held = fmt!("If-None-Match: {}", res!(etag(&r))); |
| 161 | fields.push(&held); |
| 162 | let r = res!(rig.fetch(HOST, "GET", "/big.js", &fields, "").await); |
| 163 | assert_eq!(r.status(), 304, "{} did not revalidate: {:?}", held, r.head); |
| 164 | } |
| 165 | Ok(()) |
| 166 | }) |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn a_link_naming_a_missing_folder_is_refused_at_start() -> Outcome<()> { |
| 171 | let web = res!(Scratch::new("link_gone")); |
| 172 | res!(symlink("gone", web.0.join("live"))); |
| 173 | let vh = VhostConfig { public_dir_rel: Some(fmt!("live")), ..Default::default() }; |
| 174 | match vh.get_public_dir(&web.0.clone().normalise().absolute()) { |
| 175 | Ok(dir) => Err(err!("A dangling link was taken as the docroot {:?}.", dir; Test)), |
| 176 | Err(e) => { |
| 177 | let link = fmt!("{}", web.0.join("live").display()); |
| 178 | assert!(fmt!("{}", e).contains(&link), "the refusal does not name {}: {}", link, e); |
| 179 | Ok(()) |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn a_link_inside_the_docroot_is_served() -> Outcome<()> { |
| 186 | let (web, rig, rt) = res!(on_link("link_inside")); |
| 187 | res!(web.put("shared/x.txt", "shared text")); |
| 188 | res!(symlink("../shared", web.0.join("a/docs"))); |
| 189 | rt.block_on(async { |
| 190 | let r = res!(rig.fetch(HOST, "GET", "/docs/x.txt", &[], "").await); |
| 191 | assert!(r.status() == 200 && r.body.contains("shared text"), "{:?}", r); |
| 192 | Ok(()) |
| 193 | }) |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn a_link_removed_between_requests_is_a_404() -> Outcome<()> { |
| 198 | let (web, rig, rt) = res!(on_link("link_removed")); |
| 199 | rt.block_on(async { |
| 200 | assert_eq!(res!(rig.fetch(HOST, "GET", "/", &[], "").await).status(), 200); |
| 201 | // The link gone, then the link naming a file rather than a folder. |
| 202 | res!(std::fs::remove_file(web.0.join("live"))); |
| 203 | for target in [None, Some("a/index.html")] { |
| 204 | if let Some(target) = target { |
| 205 | res!(symlink(target, web.0.join("live"))); |
| 206 | } |
| 207 | for path in ["/", "/x.js"] { |
| 208 | let r = res!(rig.fetch(HOST, "GET", path, &[], "").await); |
| 209 | assert!(r.status() == 404 && r.body.contains("File not found."), "{:?}", r); |
| 210 | assert!(r.field("cache-control").unwrap_or_default().contains("no-cache"), "{:?}", r); |
| 211 | } |
| 212 | } |
| 213 | Ok(()) |
| 214 | }) |
| 215 | } |