
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | // The routes, run with mock handlers: no server or store actor. Fake cases |
| 2 | // need no asset disk reads; `run_on_disk` is the separate real-file control. |
| 3 | // Run the full suite with `louise test --native` because Argon2 is native. |
| 4 | // The pure fs operation+route fixture has no Argon2 call and selects with |
| 5 | // `--filter fake_files_cover_operations_and_static_routes_without_leaking_writes`. |
| 6 | |
| 7 | import app. |
| 8 | { type User |
| 9 | , type log |
| 10 | , type users |
| 11 | , NameTaken |
| 12 | , User |
| 13 | , add_user |
| 14 | , find_user |
| 15 | , log |
| 16 | , new_session |
| 17 | , session_user |
| 18 | } |
| 19 | import argon2 |
| 20 | import std/bytes |
| 21 | import std/dict |
| 22 | import std/file.{type fs, exists, list_dir, read_bytes, size, write_bytes} |
| 23 | import std/http.{type Request, type Response, Request} |
| 24 | import std/http/cookie |
| 25 | import std/list |
| 26 | import std/option |
| 27 | import std/random.{type random} |
| 28 | import std/ref |
| 29 | import std/result |
| 30 | import std/string |
| 31 | import std/test.{assert, assert_equal} |
| 32 | |
| 33 | /// Collect the log instead of printing it. |
| 34 | fn with_mock_log :: action: fn() -> a / <log | e> -> (a, List<String>) / <e> |
| 35 | { |
| 36 | region |
| 37 | { |
| 38 | let lines = ref.new [] |
| 39 | let res = handle |
| 40 | { |
| 41 | action () |
| 42 | } |
| 43 | with |
| 44 | { log message -> ref.set lines [message, ..ref.get lines] } |
| 45 | (res, list.reverse (ref.get lines)) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Users and sessions in memory. Session tokens are predictable here. |
| 50 | fn with_mock_users :: |
| 51 | seed: List<User> |
| 52 | action: fn() -> a / <users | e> |
| 53 | -> a / <e> |
| 54 | { |
| 55 | region |
| 56 | { |
| 57 | let users = ref.new (dict.from_list (list.map seed fn u { (u.name, u) })) |
| 58 | let sessions = ref.new (dict.empty ()) |
| 59 | handle |
| 60 | { |
| 61 | action () |
| 62 | } |
| 63 | with |
| 64 | { find_user name -> dict.get (ref.get users) name |
| 65 | , add_user user -> if dict.has (ref.get users) user.name |
| 66 | { |
| 67 | Error(NameTaken) |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | ref.set users (dict.insert (ref.get users) user.name user) |
| 72 | Ok(()) |
| 73 | } |
| 74 | , new_session name -> |
| 75 | { |
| 76 | let token = "token-{name}" |
| 77 | ref.set sessions (dict.insert (ref.get sessions) token name) |
| 78 | token |
| 79 | } |
| 80 | , session_user token -> dict.get (ref.get sessions) token |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// At most four files, 128-byte UTF-8 paths and 1024-byte contents: a test |
| 86 | /// fake must stay tiny even when a test itself is wrong. The map backs all |
| 87 | /// five `fs` operations. Seeds declare their parents; later writes require an |
| 88 | /// implicit root or represented non-empty parent and never create a |
| 89 | /// directory. `list_dir` represents non-empty directories only; its entries |
| 90 | /// are direct names, as with a real directory. |
| 91 | fn with_fake_files :: |
| 92 | seed: List<(String, Bytes)> |
| 93 | action: fn() -> a / <fs | e> |
| 94 | -> a / <e> |
| 95 | { |
| 96 | if list.length seed > 4 |
| 97 | { |
| 98 | panic "with_fake_files: at most 4 fake files" |
| 99 | } |
| 100 | list.each seed check_fake_file |
| 101 | region |
| 102 | { |
| 103 | let files = ref.new (dict.from_list seed) |
| 104 | handle |
| 105 | { |
| 106 | action () |
| 107 | } |
| 108 | with |
| 109 | { read_bytes path -> match dict.get (ref.get files) path |
| 110 | { Some(content) -> Ok(content) |
| 111 | , None -> Error("fake file not found: {path}") |
| 112 | } |
| 113 | , write_bytes path |
| 114 | content -> |
| 115 | { |
| 116 | check_fake_file (path, content) |
| 117 | let current = ref.get files |
| 118 | if string.ends_with path "/" || !fake_parent_exists current path |
| 119 | { |
| 120 | Error("fake parent directory not found for: {path}") |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | if !dict.has current path && dict.size current >= 4 |
| 125 | { |
| 126 | panic "with_fake_files: at most 4 fake files" |
| 127 | } |
| 128 | ref.set files (dict.insert current path content) |
| 129 | Ok(()) |
| 130 | } |
| 131 | } |
| 132 | , exists path -> dict.has (ref.get files) path |
| 133 | , size path -> match dict.get (ref.get files) path |
| 134 | { Some(content) -> Ok(bytes.length content) |
| 135 | , None -> Error("fake file not found: {path}") |
| 136 | } |
| 137 | , list_dir path -> match fake_names path (dict.to_list (ref.get files)) |
| 138 | { [] -> Error("fake directory not found: {path}"), names -> Ok(names) } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | fn check_fake_file :: entry: (String, Bytes) -> () |
| 144 | { |
| 145 | let (path, content) = entry |
| 146 | if string.byte_length path > 128 |
| 147 | { |
| 148 | panic "with_fake_files: a path is over 128 bytes" |
| 149 | } |
| 150 | if bytes.length content > 1024 |
| 151 | { |
| 152 | panic "with_fake_files: content is over 1024 bytes" |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /// The parent named by the last `/`. A leading `/` names the fake's other |
| 157 | /// implicit root; no separator gives the implicit current directory. |
| 158 | fn fake_parent :: path: String -> Option<String> |
| 159 | { |
| 160 | fake_parent_chars (string.chars path) [] None |
| 161 | } |
| 162 | |
| 163 | fn fake_parent_chars :: |
| 164 | chars: List<String> |
| 165 | prefix_reversed: List<String> |
| 166 | found: Option<String> |
| 167 | -> Option<String> |
| 168 | { |
| 169 | match chars |
| 170 | { [] -> found |
| 171 | , [c, ..rest] -> |
| 172 | { |
| 173 | let parent = if c == "/" |
| 174 | { |
| 175 | let prefix = string.concat (list.reverse prefix_reversed) |
| 176 | if prefix == "" |
| 177 | { |
| 178 | Some("/") |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | Some(prefix) |
| 183 | } |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | found |
| 188 | } |
| 189 | fake_parent_chars rest [c, ..prefix_reversed] parent |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /// Seed paths declare their parents. A later write can use an implicit |
| 195 | /// current directory (no separator or `.`) or `/`; otherwise its parent must |
| 196 | /// be a represented non-empty directory. Writing never creates a directory. |
| 197 | fn fake_parent_exists :: files path -> Bool |
| 198 | { |
| 199 | match fake_parent path |
| 200 | { None -> True |
| 201 | , Some(".") -> True |
| 202 | , Some("/") -> True |
| 203 | , Some(directory) -> fake_names directory (dict.to_list files) != [] |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /// Direct file names under `directory`, from the fake's at-most-four full |
| 208 | /// paths. There is deliberately no recursive traversal. |
| 209 | fn fake_names :: directory: String files: List<(String, Bytes)> -> List<String> |
| 210 | { |
| 211 | match files |
| 212 | { [] -> [] |
| 213 | , [(path, _), ..rest] -> match fake_name directory path |
| 214 | { Some(name) -> [name, ..fake_names directory rest] |
| 215 | , None -> fake_names directory rest |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | fn fake_name :: directory: String path: String -> Option<String> |
| 221 | { |
| 222 | let prefix = if directory == "" || string.ends_with directory "/" |
| 223 | { |
| 224 | directory |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | directory <> "/" |
| 229 | } |
| 230 | if !string.starts_with path prefix |
| 231 | { |
| 232 | None |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | let name = string.slice path (string.length prefix) (string.length path) |
| 237 | if name == "" || string.contains name "/" |
| 238 | { |
| 239 | None |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | Some(name) |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | fn ada :: -> User / <ffi> |
| 249 | { |
| 250 | match argon2.hash |
| 251 | "lovelace" |
| 252 | "seed-salt" |
| 253 | argon2.Params(t_cost: 1, m_cost_kib: 64, parallelism: 1) |
| 254 | { Ok(hash) -> User("ada", hash), Error(e) -> panic e } |
| 255 | } |
| 256 | |
| 257 | fn get :: path: String cookie_header: String -> Request |
| 258 | { |
| 259 | Request( "GET" |
| 260 | , path |
| 261 | , if cookie_header == "" { [] } else { [("Cookie", cookie_header)] } |
| 262 | , bytes.empty () |
| 263 | ) |
| 264 | } |
| 265 | |
| 266 | fn post :: path: String body: String -> Request |
| 267 | { |
| 268 | Request("POST", path, [], bytes.from_string body) |
| 269 | } |
| 270 | |
| 271 | fn header :: resp: Response name: String -> String |
| 272 | { |
| 273 | match list.filter resp.headers fn h { h.0 == name } |
| 274 | { [(_, v), ..] -> v, _ -> "" } |
| 275 | } |
| 276 | |
| 277 | /// The response body as text. |
| 278 | fn text :: resp: Response -> String |
| 279 | { |
| 280 | bytes.to_string_lossy |
| 281 | (option.unwrap_or (http.whole_body resp) (bytes.empty ())) |
| 282 | } |
| 283 | |
| 284 | fn at :: xs: List<Response> i: Int -> Response |
| 285 | { |
| 286 | match xs |
| 287 | { [x, ..rest] -> if i == 0 { x } else { at rest (i - 1) } |
| 288 | , [] -> panic "no response {i}" |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /// Deliberately different from the files under `static/`: a passing request |
| 293 | /// proves it used this map, not the host disk. |
| 294 | fn fake_assets :: -> List<(String, Bytes)> |
| 295 | { |
| 296 | [ ( "./static/style.css" |
| 297 | , bytes.from_string |
| 298 | "/* fake-stylesheet */ body \{ backdrop-filter: none; \}" |
| 299 | ) |
| 300 | , ("./static/robots.txt", bytes.from_string "# fake-robots\nUser-agent: *\n") |
| 301 | ] |
| 302 | } |
| 303 | |
| 304 | /// Run requests one after another against the same mock users and files, |
| 305 | /// with seeded randomness: the same session tokens and salts on every run. |
| 306 | fn run :: requests: List<Request> -> (List<Response>, List<String>) / <ffi> |
| 307 | { |
| 308 | // `eve`'s stored hash is corrupted. |
| 309 | let seed = [ada (), User("eve", "not a hash")] |
| 310 | use <- random.with_seed 1 |
| 311 | use <- with_mock_log |
| 312 | use <- with_mock_users seed |
| 313 | use <- with_fake_files (fake_assets ()) |
| 314 | list.map requests app.route |
| 315 | } |
| 316 | |
| 317 | /// A separate real-filesystem control, never routed through |
| 318 | /// `with_fake_files`. The fixture is a small, fixed file in static/. |
| 319 | fn run_on_disk :: |
| 320 | requests: List<Request> |
| 321 | -> (List<Response>, List<String>) / <ffi, fs> |
| 322 | { |
| 323 | let seed = [ada (), User("eve", "not a hash")] |
| 324 | use <- random.with_seed 1 |
| 325 | use <- with_mock_log |
| 326 | use <- with_mock_users seed |
| 327 | list.map requests app.route |
| 328 | } |
| 329 | |
| 330 | @test |
| 331 | fn anonymous_visitors_get_the_login_page :: -> () / <ffi> |
| 332 | { |
| 333 | let (rs, logs) = run [get "/" ""] |
| 334 | let home = at rs 0 |
| 335 | assert_equal home.status 200 |
| 336 | assert (string.contains (text home) "<title>Log in</title>") "login page" |
| 337 | assert_equal logs ["GET /"] |
| 338 | } |
| 339 | |
| 340 | @test |
| 341 | fn the_stylesheet_is_served_from_the_in_memory_fs :: -> () / <ffi> |
| 342 | { |
| 343 | let (rs, logs) = run [get "/static/style.css" ""] |
| 344 | let style = at rs 0 |
| 345 | assert_equal style.status 200 |
| 346 | assert_equal (header style "Content-Type") "text/css; charset=utf-8" |
| 347 | assert |
| 348 | (string.contains (text style) "fake-stylesheet") |
| 349 | "the fake, not host disk" |
| 350 | assert_equal logs ["GET /static/style.css"] |
| 351 | } |
| 352 | |
| 353 | @test |
| 354 | fn static_files_are_served_from_the_in_memory_fs :: -> () / <ffi> |
| 355 | { |
| 356 | let (rs, logs) = run [get "/static/robots.txt" ""] |
| 357 | let robots = at rs 0 |
| 358 | assert_equal robots.status 200 |
| 359 | assert_equal (header robots "Content-Type") "text/plain; charset=utf-8" |
| 360 | assert (string.contains (text robots) "fake-robots") "the fake, not host disk" |
| 361 | assert_equal logs ["GET /static/robots.txt"] |
| 362 | } |
| 363 | |
| 364 | /// Real-root control: its exact contents come from a dedicated fixture, |
| 365 | /// not the unchanged style.css/robots.txt files that the fake replaces. |
| 366 | @test |
| 367 | fn a_known_static_file_is_served_from_disk :: -> () / <ffi, fs> |
| 368 | { |
| 369 | let (rs, logs) = run_on_disk [get "/static/on_disk_control.txt" ""] |
| 370 | let asset = at rs 0 |
| 371 | assert_equal asset.status 200 |
| 372 | assert_equal (header asset "Content-Type") "text/plain; charset=utf-8" |
| 373 | assert_equal (text asset) "louise http_demo on-disk control\n" |
| 374 | assert_equal logs ["GET /static/on_disk_control.txt"] |
| 375 | } |
| 376 | |
| 377 | @test |
| 378 | fn a_missing_static_file_and_a_traversal_are_404s :: -> () / <ffi> |
| 379 | { |
| 380 | let (rs, _) = run [get "/static/nope.txt" "", get "/static/../louise.toml" ""] |
| 381 | assert_equal ((at rs 0).status) 404 |
| 382 | assert_equal ((at rs 1).status) 404 |
| 383 | } |
| 384 | |
| 385 | /// Pure fake coverage: this test needs no argon2 call or real filesystem, |
| 386 | /// so the interpreter can run it as well as the native demo suite. |
| 387 | @test |
| 388 | fn fake_files_cover_operations_and_static_routes_without_leaking_writes :: |
| 389 | { |
| 390 | with_fake_files |
| 391 | [("./static/a.txt", bytes.from_string "a")] |
| 392 | fn |
| 393 | { |
| 394 | assert_equal (file.exists "./static/a.txt") True |
| 395 | assert_equal (file.exists "./static/missing.txt") False |
| 396 | assert_equal (file.read "./static/a.txt") Ok("a") |
| 397 | assert_equal |
| 398 | (result.map (file.read_bytes "./static/a.txt") bytes.to_string_lossy) |
| 399 | Ok("a") |
| 400 | assert_equal (file.size "./static/a.txt") Ok(1) |
| 401 | assert_equal (file.list_dir "./static") Ok(["a.txt"]) |
| 402 | assert_equal |
| 403 | (file.write "missing-parent/new.txt" "no") |
| 404 | (Error("fake parent directory not found for: missing-parent/new.txt")) |
| 405 | assert_equal (file.exists "missing-parent/new.txt") False |
| 406 | // The seed declares ./static, so this valid parent needs no directory |
| 407 | // creation (which the real write API cannot do either). |
| 408 | assert_equal (file.write "./static/new.txt" "new") Ok(()) |
| 409 | assert_equal (file.read "./static/new.txt") Ok("new") |
| 410 | assert_equal (file.list_dir "./static") Ok(["a.txt", "new.txt"]) |
| 411 | } |
| 412 | // A fresh fake does not observe the earlier handler's mutation. |
| 413 | with_fake_files |
| 414 | [] |
| 415 | fn |
| 416 | { |
| 417 | assert_equal (file.exists "./static/new.txt") False |
| 418 | assert_equal (file.exists "/definitely/not/read") False |
| 419 | } |
| 420 | |
| 421 | // The full demo is native because authentication calls Argon2 through C. |
| 422 | // This static-route transcript deliberately has no seed-user hashing, |
| 423 | // RNG, native FFI call, real file access, or host environment lookup. |
| 424 | let (rs, logs) = |
| 425 | with_mock_log |
| 426 | fn |
| 427 | { |
| 428 | with_mock_users |
| 429 | [] |
| 430 | fn |
| 431 | { |
| 432 | with_fake_files |
| 433 | (fake_assets ()) |
| 434 | fn |
| 435 | { |
| 436 | list.map |
| 437 | [ get "/static/style.css" "" |
| 438 | , get "/static/robots.txt" "" |
| 439 | , get "/static/nope.txt" "" |
| 440 | , get "/static/../louise.toml" "" |
| 441 | ] |
| 442 | app.route |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | let style = at rs 0 |
| 447 | let robots = at rs 1 |
| 448 | assert_equal style.status 200 |
| 449 | assert_equal (header style "Content-Type") "text/css; charset=utf-8" |
| 450 | assert (string.contains (text style) "fake-stylesheet") "the fake stylesheet" |
| 451 | assert_equal robots.status 200 |
| 452 | assert_equal (header robots "Content-Type") "text/plain; charset=utf-8" |
| 453 | assert (string.contains (text robots) "fake-robots") "the fake robots file" |
| 454 | assert_equal ((at rs 2).status) 404 |
| 455 | assert_equal ((at rs 3).status) 404 |
| 456 | assert_equal |
| 457 | logs |
| 458 | [ "GET /static/style.css" |
| 459 | , "GET /static/robots.txt" |
| 460 | , "GET /static/nope.txt" |
| 461 | , "GET /static/../louise.toml" |
| 462 | ] |
| 463 | } |
| 464 | |
| 465 | @test |
| 466 | @should_panic "at most 4 fake files" |
| 467 | fn fake_files_reject_even_a_small_over_limit_fixture :: |
| 468 | { |
| 469 | let seed = list.map (list.range 1 5) fn n { ("{n}.txt", bytes.empty ()) } |
| 470 | with_fake_files seed fn { () } |
| 471 | } |
| 472 | |
| 473 | @test |
| 474 | fn a_wrong_password_is_rejected :: -> () / <ffi> |
| 475 | { |
| 476 | let (rs, logs) = run [post "/login" "name=ada&password=nope"] |
| 477 | let resp = at rs 0 |
| 478 | assert_equal resp.status 401 |
| 479 | assert (string.contains (text resp) "Wrong name or password.") "error message" |
| 480 | assert_equal logs ["POST /login", "failed login ada"] |
| 481 | } |
| 482 | |
| 483 | @test |
| 484 | fn an_unknown_user_gets_the_same_answer :: -> () / <ffi> |
| 485 | { |
| 486 | let (rs, logs) = run [post "/login" "name=bob&password=lovelace"] |
| 487 | let resp = at rs 0 |
| 488 | assert_equal resp.status 401 |
| 489 | assert (string.contains (text resp) "Wrong name or password.") "error message" |
| 490 | assert_equal logs ["POST /login", "failed login bob"] |
| 491 | } |
| 492 | |
| 493 | @test |
| 494 | fn a_corrupted_hash_is_a_server_error :: -> () / <ffi> |
| 495 | { |
| 496 | let (rs, logs) = run [post "/login" "name=eve&password=anything"] |
| 497 | assert_equal ((at rs 0).status) 500 |
| 498 | assert_equal |
| 499 | logs |
| 500 | ["POST /login", "cannot check the password of eve: Decoding failed"] |
| 501 | } |
| 502 | |
| 503 | @test |
| 504 | fn logging_in_starts_a_session :: -> () / <ffi> |
| 505 | { |
| 506 | let (rs, _) = |
| 507 | run |
| 508 | [post "/login" "name=ada&password=lovelace", get "/" "session=token-ada"] |
| 509 | let login = at rs 0 |
| 510 | let home = at rs 1 |
| 511 | assert_equal login.status 303 |
| 512 | assert_equal |
| 513 | (header login "Set-Cookie") |
| 514 | "session=token-ada; Path=/; Secure; HttpOnly; SameSite=Lax" |
| 515 | assert (string.contains (text home) "Welcome, ada!") "home page" |
| 516 | } |
| 517 | |
| 518 | @test |
| 519 | fn registration_checks_its_input :: -> () / <ffi> |
| 520 | { |
| 521 | let (rs, _) = |
| 522 | run |
| 523 | [ post "/register" "name=bob&password=short" |
| 524 | , post "/register" "name=ada&password=long+enough" |
| 525 | , post "/register" "name=bob&password=long+enough" |
| 526 | , get "/" "theme=dark; session=token-bob" |
| 527 | ] |
| 528 | let short = at rs 0 |
| 529 | let taken = at rs 1 |
| 530 | let ok = at rs 2 |
| 531 | let home = at rs 3 |
| 532 | assert_equal short.status 400 |
| 533 | assert_equal taken.status 409 |
| 534 | assert_equal ok.status 303 |
| 535 | assert |
| 536 | (string.contains (text home) "Welcome, bob!") |
| 537 | "the new user is logged in" |
| 538 | } |
| 539 | |
| 540 | @test |
| 541 | fn unknown_pages_are_not_found :: -> () / <ffi> |
| 542 | { |
| 543 | let (rs, _) = run [get "/nope" ""] |
| 544 | let resp = at rs 0 |
| 545 | assert_equal resp.status 404 |
| 546 | } |
| 547 | |
| 548 | @test |
| 549 | fn a_new_cookie_has_safe_defaults :: |
| 550 | { |
| 551 | assert_equal |
| 552 | (cookie.to_header (cookie.new "session" "abc")) |
| 553 | "session=abc; Path=/; Secure; HttpOnly; SameSite=Lax" |
| 554 | } |
| 555 | |
| 556 | @test |
| 557 | fn a_cookie_is_adjusted_with_a_record_update :: |
| 558 | { |
| 559 | let c = |
| 560 | cookie.SetCookie( ..cookie.new "theme" "dark" |
| 561 | , secure: False |
| 562 | , http_only: False |
| 563 | , domain: Some("example.com") |
| 564 | , max_age: Some(3600) |
| 565 | , same_site: Some(cookie.Strict) |
| 566 | ) |
| 567 | assert_equal |
| 568 | (cookie.to_header c) |
| 569 | "theme=dark; Path=/; Domain=example.com; Max-Age=3600; SameSite=Strict" |
| 570 | } |
| 571 | |
| 572 | @test |
| 573 | fn expiring_a_cookie_empties_it_with_max_age_0 :: |
| 574 | { |
| 575 | assert_equal |
| 576 | (cookie.to_header (cookie.expire "session")) |
| 577 | "session=; Path=/; Max-Age=0; Secure; HttpOnly; SameSite=Lax" |
| 578 | } |
| 579 | |
| 580 | @test |
| 581 | fn same_site_none_is_written_nonesite :: |
| 582 | { |
| 583 | // `NoneSite`, not `None` (which is `Option`'s), for `SameSite=None`. |
| 584 | let c = |
| 585 | cookie.SetCookie(..cookie.new "x" "y", same_site: Some(cookie.NoneSite)) |
| 586 | assert |
| 587 | (string.contains (cookie.to_header c) "; SameSite=None") |
| 588 | "SameSite=None" |
| 589 | let unset = cookie.SetCookie(..cookie.new "x" "y", same_site: None) |
| 590 | assert |
| 591 | (!string.contains (cookie.to_header unset) "SameSite") |
| 592 | "no SameSite attribute" |
| 593 | } |
| 594 | |
| 595 | @test |
| 596 | fn set_cookie_appends_a_header :: |
| 597 | { |
| 598 | let resp = cookie.set_cookie (http.text 200 "ok") (cookie.new "a" "1") |
| 599 | assert_equal (header resp "Content-Type") "text/plain; charset=utf-8" |
| 600 | assert_equal |
| 601 | (header resp "Set-Cookie") |
| 602 | "a=1; Path=/; Secure; HttpOnly; SameSite=Lax" |
| 603 | } |
| 604 | |
| 605 | @test |
| 606 | @should_panic "cannot contain" |
| 607 | fn a_cookie_name_with_a_space_is_rejected :: |
| 608 | { |
| 609 | let _ = cookie.new "a b" "x" |
| 610 | () |
| 611 | } |
| 612 | |
| 613 | @test |
| 614 | @should_panic "cannot contain" |
| 615 | fn a_cookie_value_with_a_semicolon_is_rejected :: |
| 616 | { |
| 617 | let _ = cookie.new "a" "x;y" |
| 618 | () |
| 619 | } |
| 620 | |
| 621 | @test |
| 622 | @should_panic "cannot be empty or contain" |
| 623 | fn a_cookie_name_with_an_equals_is_rejected :: |
| 624 | { |
| 625 | let _ = cookie.new "a=b" "x" |
| 626 | () |
| 627 | } |
| 628 | |
| 629 | @test |
| 630 | @should_panic "boom" |
| 631 | fn the_boom_route_crashes_its_handler :: -> () / <ffi> |
| 632 | { |
| 633 | let _ = run [get "/boom" ""] |
| 634 | () |
| 635 | } |
| 636 | |
| 637 | @test |
| 638 | fn the_decoy_hash_costs_as_much_as_a_real_one :: -> () / <ffi, random> |
| 639 | { |
| 640 | // Same algorithm and parameters as a stored hash, so checking a |
| 641 | // password against it takes as long. |
| 642 | let real = argon2.hash_password "lovelace" (app.hashing ()) |
| 643 | assert_equal (real |> result.map cost) Ok(cost (app.decoy_hash ())) |
| 644 | // A well-formed hash, that no password here matches. |
| 645 | assert_equal (argon2.verify "lovelace" (app.decoy_hash ())) Ok(False) |
| 646 | } |
| 647 | |
| 648 | /// A PHC string's algorithm, version and parameters, before the salt. |
| 649 | fn cost :: phc: String -> List<String> |
| 650 | { |
| 651 | match string.split phc "$" |
| 652 | { [_, algorithm, version, params, ..] -> [algorithm, version, params] |
| 653 | , parts -> parts |
| 654 | } |
| 655 | } |