Pin the raw-byte round trip behind drawing playback
Browse files at this commitbdb487150146f282a452ee7d29f176c7ad0f6cafParent: a333c09- test
- pin the raw-byte round trip behind drawing playback
1 changed file +374 -0
- tests/integration.rs +374 -0
tests/integration.rsmodified+374 -0
| @@ -12441,3 +12441,377 @@ async fn solution_images_edits_and_counts() { | |||
| 12441 | .await; | 12441 | .await; |
| 12442 | assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); | 12442 | assert_eq!(res.status, StatusCode::NO_CONTENT, "{}", res.body); |
| 12443 | } | 12443 | } |
| 12444 | + | ||
| 12445 | +// --- drawing byte-fidelity (stroke-JSON tEXt passthrough) ------------------- | ||
| 12446 | +// The frontend's canvas saves a drawing as a PNG that also carries its | ||
| 12447 | +// editable stroke JSON inside a custom `tEXt` chunk; the backend only ever | ||
| 12448 | +// stores and re-serves the uploaded bytes (see `read_upload` / `serve_inline_blob` | ||
| 12449 | +// in web/mod.rs). These tests build a real (if minimal) PNG with a `tEXt` | ||
| 12450 | +// chunk and assert the round trip is byte-for-byte — if anyone ever adds | ||
| 12451 | +// image re-encoding, compression, or thumbnailing, every stored drawing | ||
| 12452 | +// silently stops being replayable, and this is what would catch it. | ||
| 12453 | + | ||
| 12454 | +/// CRC-32 (ISO-HDLC — the checksum PNG chunks and zlib both use), bit by bit: | ||
| 12455 | +/// no table, since this only ever runs over a few dozen fixture bytes. No new | ||
| 12456 | +/// crate for one checksum used to hand-build a PNG below. | ||
| 12457 | +fn crc32(bytes: &[u8]) -> u32 { | ||
| 12458 | + let mut crc = 0xFFFF_FFFFu32; | ||
| 12459 | + for &byte in bytes { | ||
| 12460 | + crc ^= byte as u32; | ||
| 12461 | + for _ in 0..8 { | ||
| 12462 | + crc = if crc & 1 == 1 { | ||
| 12463 | + (crc >> 1) ^ 0xEDB8_8320 | ||
| 12464 | + } else { | ||
| 12465 | + crc >> 1 | ||
| 12466 | + }; | ||
| 12467 | + } | ||
| 12468 | + } | ||
| 12469 | + !crc | ||
| 12470 | +} | ||
| 12471 | + | ||
| 12472 | +/// Pin the hand-rolled checksum against the standard CRC-32 check value. | ||
| 12473 | +/// Nothing downstream ever validates a chunk's CRC (the server never parses | ||
| 12474 | +/// PNG structure), so a broken implementation here would silently build | ||
| 12475 | +/// fixtures that only *look* like PNGs — this is the one thing that would | ||
| 12476 | +/// catch that. | ||
| 12477 | +#[tokio::test] | ||
| 12478 | +async fn crc32_matches_the_standard_check_value() { | ||
| 12479 | + assert_eq!(crc32(b"123456789"), 0xCBF4_3926); | ||
| 12480 | +} | ||
| 12481 | + | ||
| 12482 | +/// Frame one PNG chunk: 4-byte big-endian length, 4-byte type, the data, then | ||
| 12483 | +/// the CRC-32 over type+data. | ||
| 12484 | +fn png_chunk(kind: &[u8; 4], data: &[u8]) -> Vec<u8> { | ||
| 12485 | + let mut chunk = Vec::with_capacity(12 + data.len()); | ||
| 12486 | + chunk.extend_from_slice(&(data.len() as u32).to_be_bytes()); | ||
| 12487 | + chunk.extend_from_slice(kind); | ||
| 12488 | + chunk.extend_from_slice(data); | ||
| 12489 | + let crc_body: Vec<u8> = kind.iter().chain(data.iter()).copied().collect(); | ||
| 12490 | + chunk.extend_from_slice(&crc32(&crc_body).to_be_bytes()); | ||
| 12491 | + chunk | ||
| 12492 | +} | ||
| 12493 | + | ||
| 12494 | +/// A genuinely valid, minimal (1x1 grayscale) PNG carrying a custom `tEXt` | ||
| 12495 | +/// chunk whose text is `marker` — the same trick the frontend uses to smuggle | ||
| 12496 | +/// its editable stroke JSON inside a drawing's PNG bytes. Built inline (no | ||
| 12497 | +/// binary fixture file) so the test commits exactly the bytes it later | ||
| 12498 | +/// asserts on. | ||
| 12499 | +fn png_with_text_chunk(marker: &str) -> Vec<u8> { | ||
| 12500 | + let mut png = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]; | ||
| 12501 | + // IHDR: 1x1 pixel, 8-bit depth, grayscale, no interlace. | ||
| 12502 | + png.extend(png_chunk(b"IHDR", &[0, 0, 0, 1, 0, 0, 0, 1, 8, 0, 0, 0, 0])); | ||
| 12503 | + // tEXt: keyword "Comment", NUL, then the marker — exactly where a real | ||
| 12504 | + // drawing's stroke JSON rides. | ||
| 12505 | + let mut text = b"Comment\0".to_vec(); | ||
| 12506 | + text.extend_from_slice(marker.as_bytes()); | ||
| 12507 | + png.extend(png_chunk(b"tEXt", &text)); | ||
| 12508 | + // IDAT: one stored (uncompressed) deflate block over the single | ||
| 12509 | + // [filter=None, pixel=0] scanline, so this decodes as a real black pixel. | ||
| 12510 | + png.extend(png_chunk( | ||
| 12511 | + b"IDAT", | ||
| 12512 | + &[ | ||
| 12513 | + 0x78, 0x01, // zlib header | ||
| 12514 | + 0x01, 0x02, 0x00, 0xFD, 0xFF, // stored block: BFINAL+BTYPE, LEN, NLEN | ||
| 12515 | + 0x00, 0x00, // scanline: filter=None, pixel=0 | ||
| 12516 | + 0x00, 0x02, 0x00, 0x01, // Adler-32 of the scanline bytes | ||
| 12517 | + ], | ||
| 12518 | + )); | ||
| 12519 | + png.extend(png_chunk(b"IEND", &[])); | ||
| 12520 | + png | ||
| 12521 | +} | ||
| 12522 | + | ||
| 12523 | +/// The primary invariant: a student's answer drawing survives its round trip | ||
| 12524 | +/// byte-for-byte, `tEXt` chunk included — on both read paths that matter, the | ||
| 12525 | +/// student's own (for editing) and the teacher's grading read (for replay). | ||
| 12526 | +/// If the backend ever re-encodes or strips metadata from this PNG, the | ||
| 12527 | +/// stroke JSON in the `tEXt` chunk is gone and the drawing stops replaying. | ||
| 12528 | +#[tokio::test] | ||
| 12529 | +async fn answer_image_round_trip_keeps_the_stroke_text_chunk_intact() { | ||
| 12530 | + let (app, db) = app_and_db().await; | ||
| 12531 | + let teacher = login_as(&app, &db, "rt_t", "teacher").await; | ||
| 12532 | + let student = login(&app, "rt_s").await; | ||
| 12533 | + let student_id = me_id(&app, &student).await; | ||
| 12534 | + | ||
| 12535 | + let course = create_course(&app, &teacher, "art").await; | ||
| 12536 | + let subject = create_subject(&app, &teacher, &course, "sketching").await; | ||
| 12537 | + enroll(&app, &teacher, &course, &student_id).await; | ||
| 12538 | + let exam = scheduled_exam( | ||
| 12539 | + &app, | ||
| 12540 | + &teacher, | ||
| 12541 | + &course, | ||
| 12542 | + json!({ "title": "sketch quiz", "kind": "quiz", "mode": "open" }), | ||
| 12543 | + ) | ||
| 12544 | + .await; | ||
| 12545 | + let question = create_question( | ||
| 12546 | + &app, | ||
| 12547 | + &teacher, | ||
| 12548 | + &exam, | ||
| 12549 | + &subject, | ||
| 12550 | + json!({ "text": "Draw a triangle.", "kind": "text", "points": 10 }), | ||
| 12551 | + ) | ||
| 12552 | + .await; | ||
| 12553 | + let res = send( | ||
| 12554 | + &app, | ||
| 12555 | + "POST", | ||
| 12556 | + &format!("/exams/{exam}/attempt"), | ||
| 12557 | + Some(&student), | ||
| 12558 | + None, | ||
| 12559 | + ) | ||
| 12560 | + .await; | ||
| 12561 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 12562 | + | ||
| 12563 | + let png = png_with_text_chunk(r#"{"strokes":[[1,1],[2,2],[3,3]]}"#); | ||
| 12564 | + let own = format!("/exams/{exam}/attempt/answers/{question}/image"); | ||
| 12565 | + let (status, body) = post_image(&app, &student, &own, "image/png", &png).await; | ||
| 12566 | + assert_eq!(status, StatusCode::CREATED, "{}", body); | ||
| 12567 | + | ||
| 12568 | + // The student replays their own drawing from this route. | ||
| 12569 | + let (status, headers, bytes) = | ||
| 12570 | + common::send_raw(&app, "GET", &own, Some(&student), None, Vec::new()).await; | ||
| 12571 | + assert_eq!(status, StatusCode::OK); | ||
| 12572 | + assert_eq!(headers["content-type"], "image/png"); | ||
| 12573 | + assert_eq!(bytes, png, "the student's own read must be byte-identical"); | ||
| 12574 | + | ||
| 12575 | + // The teacher replays it too, via the grading route — same requirement. | ||
| 12576 | + let teacher_uri = format!("/exams/{exam}/attempts/{student_id}/answers/{question}/image"); | ||
| 12577 | + let (status, _, bytes) = | ||
| 12578 | + common::send_raw(&app, "GET", &teacher_uri, Some(&teacher), None, Vec::new()).await; | ||
| 12579 | + assert_eq!(status, StatusCode::OK); | ||
| 12580 | + assert_eq!(bytes, png, "the grader read must be byte-identical too"); | ||
| 12581 | +} | ||
| 12582 | + | ||
| 12583 | +/// The pool question photo is a drawing too (a student can attach a photo or | ||
| 12584 | +/// a canvas sketch of the problem) — same byte-for-byte requirement as an | ||
| 12585 | +/// answer image, or a teacher replaying a hand-drawn question loses the | ||
| 12586 | +/// stroke JSON in its `tEXt` chunk. | ||
| 12587 | +#[tokio::test] | ||
| 12588 | +async fn question_photo_round_trip_keeps_the_stroke_text_chunk_intact() { | ||
| 12589 | + let app = mem_app().await; | ||
| 12590 | + let asker = login(&app, "ali").await; | ||
| 12591 | + let res = send( | ||
| 12592 | + &app, | ||
| 12593 | + "POST", | ||
| 12594 | + "/questions", | ||
| 12595 | + Some(&asker), | ||
| 12596 | + Some(json!({ "title": "Şekil", "body": "Çizim ekli" })), | ||
| 12597 | + ) | ||
| 12598 | + .await; | ||
| 12599 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 12600 | + let qid = id_of(&res.body); | ||
| 12601 | + | ||
| 12602 | + let png = png_with_text_chunk(r#"{"strokes":[[4,4],[5,5]]}"#); | ||
| 12603 | + let uri = format!("/questions/{qid}/image"); | ||
| 12604 | + let (status, body) = post_image(&app, &asker, &uri, "image/png", &png).await; | ||
| 12605 | + assert_eq!(status, StatusCode::CREATED, "{}", body); | ||
| 12606 | + | ||
| 12607 | + let (status, headers, bytes) = | ||
| 12608 | + common::send_raw(&app, "GET", &uri, Some(&asker), None, Vec::new()).await; | ||
| 12609 | + assert_eq!(status, StatusCode::OK); | ||
| 12610 | + assert_eq!(headers["content-type"], "image/png"); | ||
| 12611 | + assert_eq!( | ||
| 12612 | + bytes, png, | ||
| 12613 | + "the pool question photo must round-trip byte-for-byte" | ||
| 12614 | + ); | ||
| 12615 | +} | ||
| 12616 | + | ||
| 12617 | +/// Same story on a solution's worked-steps photo: the backend must never | ||
| 12618 | +/// touch these bytes, or a replayed solution drawing loses its stroke JSON. | ||
| 12619 | +#[tokio::test] | ||
| 12620 | +async fn solution_photo_round_trip_keeps_the_stroke_text_chunk_intact() { | ||
| 12621 | + let (app, db) = app_and_db().await; | ||
| 12622 | + let asker = login(&app, "ali").await; | ||
| 12623 | + let helper = login(&app, "veli").await; | ||
| 12624 | + let teacher = login_as(&app, &db, "hoca", "teacher").await; | ||
| 12625 | + | ||
| 12626 | + let res = send( | ||
| 12627 | + &app, | ||
| 12628 | + "POST", | ||
| 12629 | + "/questions", | ||
| 12630 | + Some(&asker), | ||
| 12631 | + Some(json!({ "title": "Limit", "body": "Çözüm arıyorum" })), | ||
| 12632 | + ) | ||
| 12633 | + .await; | ||
| 12634 | + let qid = id_of(&res.body); | ||
| 12635 | + let res = send( | ||
| 12636 | + &app, | ||
| 12637 | + "POST", | ||
| 12638 | + &format!("/questions/{qid}/approve"), | ||
| 12639 | + Some(&teacher), | ||
| 12640 | + None, | ||
| 12641 | + ) | ||
| 12642 | + .await; | ||
| 12643 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 12644 | + let res = send( | ||
| 12645 | + &app, | ||
| 12646 | + "POST", | ||
| 12647 | + &format!("/questions/{qid}/solutions"), | ||
| 12648 | + Some(&helper), | ||
| 12649 | + Some(json!({ "body": "İşte çözüm." })), | ||
| 12650 | + ) | ||
| 12651 | + .await; | ||
| 12652 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 12653 | + let sid = id_of(&res.body); | ||
| 12654 | + | ||
| 12655 | + let png = png_with_text_chunk(r#"{"strokes":[[6,6],[7,7]]}"#); | ||
| 12656 | + let uri = format!("/questions/{qid}/solutions/{sid}/image"); | ||
| 12657 | + let (status, body) = post_image(&app, &helper, &uri, "image/png", &png).await; | ||
| 12658 | + assert_eq!(status, StatusCode::CREATED, "{}", body); | ||
| 12659 | + | ||
| 12660 | + let (status, headers, bytes) = | ||
| 12661 | + common::send_raw(&app, "GET", &uri, Some(&asker), None, Vec::new()).await; | ||
| 12662 | + assert_eq!(status, StatusCode::OK); | ||
| 12663 | + assert_eq!(headers["content-type"], "image/png"); | ||
| 12664 | + assert_eq!( | ||
| 12665 | + bytes, png, | ||
| 12666 | + "the solution photo must round-trip byte-for-byte" | ||
| 12667 | + ); | ||
| 12668 | +} | ||
| 12669 | + | ||
| 12670 | +/// The contrast to `question_pool_image_upload_and_freeze`'s 409-after-approval: | ||
| 12671 | +/// a solution carries no moderation state, so its author replaces the photo | ||
| 12672 | +/// freely at any time, even on a long-approved question. | ||
| 12673 | +#[tokio::test] | ||
| 12674 | +async fn solution_photo_replaces_freely_unlike_the_frozen_question_photo() { | ||
| 12675 | + let (app, db) = app_and_db().await; | ||
| 12676 | + let asker = login(&app, "ali").await; | ||
| 12677 | + let helper = login(&app, "veli").await; | ||
| 12678 | + let teacher = login_as(&app, &db, "hoca", "teacher").await; | ||
| 12679 | + | ||
| 12680 | + let res = send( | ||
| 12681 | + &app, | ||
| 12682 | + "POST", | ||
| 12683 | + "/questions", | ||
| 12684 | + Some(&asker), | ||
| 12685 | + Some(json!({ "title": "Türev", "body": "Nasıl alınır?" })), | ||
| 12686 | + ) | ||
| 12687 | + .await; | ||
| 12688 | + let qid = id_of(&res.body); | ||
| 12689 | + let res = send( | ||
| 12690 | + &app, | ||
| 12691 | + "POST", | ||
| 12692 | + &format!("/questions/{qid}/approve"), | ||
| 12693 | + Some(&teacher), | ||
| 12694 | + None, | ||
| 12695 | + ) | ||
| 12696 | + .await; | ||
| 12697 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 12698 | + let res = send( | ||
| 12699 | + &app, | ||
| 12700 | + "POST", | ||
| 12701 | + &format!("/questions/{qid}/solutions"), | ||
| 12702 | + Some(&helper), | ||
| 12703 | + Some(json!({ "body": "Cevap burada." })), | ||
| 12704 | + ) | ||
| 12705 | + .await; | ||
| 12706 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 12707 | + let sid = id_of(&res.body); | ||
| 12708 | + | ||
| 12709 | + let uri = format!("/questions/{qid}/solutions/{sid}/image"); | ||
| 12710 | + let (status, _) = post_image(&app, &helper, &uri, "image/png", b"first-draft").await; | ||
| 12711 | + assert_eq!(status, StatusCode::CREATED); | ||
| 12712 | + // No freeze: a second replace on the same long-approved question still lands. | ||
| 12713 | + let (status, _) = post_image(&app, &helper, &uri, "image/png", b"second-draft").await; | ||
| 12714 | + assert_eq!( | ||
| 12715 | + status, | ||
| 12716 | + StatusCode::CREATED, | ||
| 12717 | + "a solution photo must never freeze" | ||
| 12718 | + ); | ||
| 12719 | + let (status, _, bytes) = | ||
| 12720 | + common::send_raw(&app, "GET", &uri, Some(&helper), None, Vec::new()).await; | ||
| 12721 | + assert_eq!(status, StatusCode::OK); | ||
| 12722 | + assert_eq!(bytes, b"second-draft"); | ||
| 12723 | +} | ||
| 12724 | + | ||
| 12725 | +/// The other half of a pending question's photo visibility (the asker-vs-stranger | ||
| 12726 | +/// half is already pinned by `question_pool_image_upload_and_freeze`): teacher+ | ||
| 12727 | +/// is the approval queue's other reader, so a pending photo must reach them too. | ||
| 12728 | +#[tokio::test] | ||
| 12729 | +async fn pool_question_pending_photo_reaches_teacher_too() { | ||
| 12730 | + let (app, db) = app_and_db().await; | ||
| 12731 | + let asker = login(&app, "ali").await; | ||
| 12732 | + let teacher = login_as(&app, &db, "hoca", "teacher").await; | ||
| 12733 | + | ||
| 12734 | + let res = send( | ||
| 12735 | + &app, | ||
| 12736 | + "POST", | ||
| 12737 | + "/questions", | ||
| 12738 | + Some(&asker), | ||
| 12739 | + Some(json!({ "title": "Soru", "body": "Yardım lazım" })), | ||
| 12740 | + ) | ||
| 12741 | + .await; | ||
| 12742 | + let qid = id_of(&res.body); | ||
| 12743 | + let uri = format!("/questions/{qid}/image"); | ||
| 12744 | + let (status, _) = post_image(&app, &asker, &uri, "image/png", b"png-bytes").await; | ||
| 12745 | + assert_eq!(status, StatusCode::CREATED); | ||
| 12746 | + | ||
| 12747 | + let (status, _, bytes) = | ||
| 12748 | + common::send_raw(&app, "GET", &uri, Some(&teacher), None, Vec::new()).await; | ||
| 12749 | + assert_eq!( | ||
| 12750 | + status, | ||
| 12751 | + StatusCode::OK, | ||
| 12752 | + "teacher+ must see a pending photo too" | ||
| 12753 | + ); | ||
| 12754 | + assert_eq!(bytes, b"png-bytes"); | ||
| 12755 | +} | ||
| 12756 | + | ||
| 12757 | +/// `GET` on a pool photo needs student role or higher; parents are read-only | ||
| 12758 | +/// report observers (see `question_pool_locks_parents_out`), so both photo | ||
| 12759 | +/// routes must refuse them exactly like the rest of the pool. | ||
| 12760 | +#[tokio::test] | ||
| 12761 | +async fn pool_photo_reads_require_student_role() { | ||
| 12762 | + let (app, db) = app_and_db().await; | ||
| 12763 | + let asker = login(&app, "ali").await; | ||
| 12764 | + let helper = login(&app, "veli").await; | ||
| 12765 | + let teacher = login_as(&app, &db, "hoca", "teacher").await; | ||
| 12766 | + let parent = login_as(&app, &db, "anne", "parent").await; | ||
| 12767 | + | ||
| 12768 | + let res = send( | ||
| 12769 | + &app, | ||
| 12770 | + "POST", | ||
| 12771 | + "/questions", | ||
| 12772 | + Some(&asker), | ||
| 12773 | + Some(json!({ "title": "Soru", "body": "Yardım lazım" })), | ||
| 12774 | + ) | ||
| 12775 | + .await; | ||
| 12776 | + let qid = id_of(&res.body); | ||
| 12777 | + let q_uri = format!("/questions/{qid}/image"); | ||
| 12778 | + let (status, _) = post_image(&app, &asker, &q_uri, "image/png", b"q-bytes").await; | ||
| 12779 | + assert_eq!(status, StatusCode::CREATED); | ||
| 12780 | + let res = send( | ||
| 12781 | + &app, | ||
| 12782 | + "POST", | ||
| 12783 | + &format!("/questions/{qid}/approve"), | ||
| 12784 | + Some(&teacher), | ||
| 12785 | + None, | ||
| 12786 | + ) | ||
| 12787 | + .await; | ||
| 12788 | + assert_eq!(res.status, StatusCode::OK, "{}", res.body); | ||
| 12789 | + let res = send( | ||
| 12790 | + &app, | ||
| 12791 | + "POST", | ||
| 12792 | + &format!("/questions/{qid}/solutions"), | ||
| 12793 | + Some(&helper), | ||
| 12794 | + Some(json!({ "body": "Cevap burada." })), | ||
| 12795 | + ) | ||
| 12796 | + .await; | ||
| 12797 | + assert_eq!(res.status, StatusCode::CREATED, "{}", res.body); | ||
| 12798 | + let sid = id_of(&res.body); | ||
| 12799 | + let s_uri = format!("/questions/{qid}/solutions/{sid}/image"); | ||
| 12800 | + let (status, _) = post_image(&app, &helper, &s_uri, "image/png", b"s-bytes").await; | ||
| 12801 | + assert_eq!(status, StatusCode::CREATED); | ||
| 12802 | + | ||
| 12803 | + let (status, _, _) = | ||
| 12804 | + common::send_raw(&app, "GET", &q_uri, Some(&parent), None, Vec::new()).await; | ||
| 12805 | + assert_eq!( | ||
| 12806 | + status, | ||
| 12807 | + StatusCode::FORBIDDEN, | ||
| 12808 | + "parents can't read a question photo" | ||
| 12809 | + ); | ||
| 12810 | + let (status, _, _) = | ||
| 12811 | + common::send_raw(&app, "GET", &s_uri, Some(&parent), None, Vec::new()).await; | ||
| 12812 | + assert_eq!( | ||
| 12813 | + status, | ||
| 12814 | + StatusCode::FORBIDDEN, | ||
| 12815 | + "parents can't read a solution photo" | ||
| 12816 | + ); | ||
| 12817 | +} | ||