| @@ -430,4 +430,61 @@ mod tests { |
| 430 | 430 | } |
| 431 | 431 | ); |
| 432 | 432 | } |
| 433 | + |
| 434 | + /// The externs build `std/http`'s `Head` and `Chunk` values. |
| 435 | + unsafe extern "C" fn builds_std_http_values(ctx: *mut Ctx) -> Value { |
| 436 | + use crate::abi::{Block, UNIT, int_get}; |
| 437 | + use crate::heap::drop_value; |
| 438 | + use crate::values::{bytes_from, list_items, text}; |
| 439 | + let field = |v: Value, i: usize| { |
| 440 | + // SAFETY: the test reads fields of the cells the externs made. |
| 441 | + unsafe { *Block::fields_ptr(v as *mut Block).add(i) } |
| 442 | + }; |
| 443 | + // SAFETY: as above. |
| 444 | + let tag = |v: Value| unsafe { (*(v as *mut Block)).header.tag }; |
| 445 | + // SAFETY: every value is owned by this test. |
| 446 | + unsafe { |
| 447 | + let head = bytes_from(ctx, b"GET /x HTTP/1.1\r\nA: b\xff\r\n\r\n"); |
| 448 | + let complete = tl_http_parse_request(ctx, head, int(0)); |
| 449 | + assert_eq!(tag(complete), HEAD_COMPLETE); |
| 450 | + assert_eq!( |
| 451 | + (text(field(complete, 0)), text(field(complete, 1))), |
| 452 | + ("GET", "/x") |
| 453 | + ); |
| 454 | + assert_eq!(int_get(field(complete, 2)), 1); |
| 455 | + let header = list_items(field(complete, 3)).next().unwrap(); |
| 456 | + assert_eq!((tag(header), text(field(header, 1))), (0, "b\u{fffd}")); |
| 457 | + assert_eq!(field(complete, 4), int(26)); |
| 458 | + assert_eq!(field(complete, 5), int(-1)); |
| 459 | + // A negative offset is 0; one past the end is the end. |
| 460 | + let again = tl_http_parse_request(ctx, head, int(-5)); |
| 461 | + assert_eq!(field(again, 4), int(26)); |
| 462 | + let bad = tl_http_parse_request(ctx, head, int(3)); |
| 463 | + assert_eq!(tag(bad), HEAD_INVALID); |
| 464 | + assert_eq!(tl_http_parse_request(ctx, head, int(99)), int(HEAD_PARTIAL)); |
| 465 | + |
| 466 | + let chunks = bytes_from(ctx, b"5\r\nhello\r\n0\r\n\r\n"); |
| 467 | + let data = tl_http_parse_chunk(ctx, chunks, int(0)); |
| 468 | + assert_eq!(tag(data), CHUNK_DATA); |
| 469 | + assert_eq!((field(data, 0), field(data, 1)), (int(3), int(5))); |
| 470 | + let end = tl_http_parse_chunk(ctx, chunks, int(10)); |
| 471 | + assert_eq!((tag(end), field(end, 0)), (CHUNK_END, int(15))); |
| 472 | + let invalid = tl_http_parse_chunk(ctx, chunks, int(4)); |
| 473 | + assert_eq!(tag(invalid), CHUNK_INVALID); |
| 474 | + let partial = tl_http_parse_chunk(ctx, chunks, int(99)); |
| 475 | + assert_eq!(partial, int(CHUNK_PARTIAL)); |
| 476 | + for v in [head, complete, again, bad, chunks, data, end, invalid] { |
| 477 | + drop_value(ctx, v); |
| 478 | + } |
| 479 | + } |
| 480 | + assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 481 | + UNIT |
| 482 | + } |
| 483 | + |
| 484 | + #[test] |
| 485 | + fn externs_build_std_http_values() { |
| 486 | + // SAFETY: the test follows the generated-code convention. |
| 487 | + let code = unsafe { crate::sched::run_test_actor(builds_std_http_values) }; |
| 488 | + assert_eq!(code, 0); |
| 489 | + } |
| 433 | 490 | } |