Skip to content

🧪 [testing improvement] Add tests for decrypt_handoff_blob#1422

Closed
senamakel wants to merge 3 commits into
mainfrom
test-decrypt-handoff-blob-14953444737016356940
Closed

🧪 [testing improvement] Add tests for decrypt_handoff_blob#1422
senamakel wants to merge 3 commits into
mainfrom
test-decrypt-handoff-blob-14953444737016356940

Conversation

@senamakel
Copy link
Copy Markdown
Member

@senamakel senamakel commented May 9, 2026

🎯 What: The testing gap addressed

  • Added unit tests for decrypt_handoff_blob in src/api/rest.rs.

📊 Coverage: What scenarios are now tested

  • Happy path: Successful decryption of a valid AES-256-GCM blob.
  • Error path: Payload too short (less than 32 bytes).
  • Error path: Invalid base64 input.
  • Error path: Authentication failure (wrong key).
  • Error path: Authentication failure (tampered ciphertext).
  • Error path: UTF-8 decoding failure of the decrypted plaintext.

Result: Improved reliability and coverage for cryptographic token handoff.


PR created automatically by Jules for task 14953444737016356940 started by @senamakel

Summary by CodeRabbit

  • Tests
    • Expanded test coverage for decryption functionality to validate proper behavior under normal and error conditions, ensuring data integrity and system reliability.

Review Change Stack

@senamakel senamakel requested a review from a team May 9, 2026 20:56
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 9, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6b85d5ae-8ebb-40bc-bef8-95431744334c

📥 Commits

Reviewing files that changed from the base of the PR and between e3a99c6 and 990afc8.

📒 Files selected for processing (1)
  • src/api/rest_tests.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/api/rest_tests.rs

📝 Walkthrough

Walkthrough

This PR extends the REST API test suite with comprehensive coverage for the decrypt_handoff_blob function. It imports the decryption function and AES-256-GCM cryptography utilities, then implements six test cases validating successful decryption and five specific error conditions: short payloads, invalid base64, incorrect keys, tampered ciphertext, and non-UTF-8 plaintext.

Changes

REST API Decryption Tests

Layer / File(s) Summary
Test Scaffolding & Imports
src/api/rest_tests.rs
Test module now imports decrypt_handoff_blob and adds AES-256-GCM dependencies (aes_gcm, typenum) with Aes256Gcm16 type alias for constructing encrypted test payloads.
Successful Decryption Test
src/api/rest_tests.rs
Test constructs an AES-GCM encrypted handoff payload (IV + tag + ciphertext), base64-encodes it, calls decrypt_handoff_blob, and verifies the decrypted plaintext matches the original message.
Decryption Error Condition Tests
src/api/rest_tests.rs
Five test cases verify that decrypt_handoff_blob correctly rejects payloads that are too short, fail base64 decoding, are decrypted with the wrong key, contain tampered ciphertext, or decode to non-UTF-8 plaintext, each asserting the appropriate error message.

🎯 2 (Simple) | ⏱️ ~10 minutes

🐰 Cryptographic tests now hop with confidence clear,
Five failure paths and success paths appear,
Base64, UTF-8, and AES-GCM play,
Handoff decryption is tested today! 🔐

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title mentions emoji and bracketed category, but does clearly identify the main change: adding tests for decrypt_handoff_blob function, which aligns with the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 88.89% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch test-decrypt-handoff-blob-14953444737016356940

Comment @coderabbitai help to get the list of available commands and usage tips.

@senamakel
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 9, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@senamakel
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 9, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@senamakel
Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 9, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/api/rest_tests.rs (1)

62-73: ⚡ Quick win

Extract a shared fixture helper to remove repeated blob assembly logic.

The encrypt/split/combine/base64 flow is duplicated across multiple tests; a small helper will reduce drift risk and simplify future test additions.

♻️ Proposed refactor
+fn make_handoff_blob_b64(key_bytes: [u8; 32], iv_bytes: [u8; 16], plain: &[u8]) -> String {
+    let cipher = Aes256Gcm16::new_from_slice(&key_bytes).unwrap();
+    let nonce = aes_gcm::aead::generic_array::GenericArray::from_slice(&iv_bytes);
+    let encrypted = cipher.encrypt(nonce, plain).unwrap();
+    let (ciphertext, tag) = encrypted.split_at(encrypted.len() - 16);
+
+    let mut combined = Vec::with_capacity(16 + 16 + ciphertext.len());
+    combined.extend_from_slice(&iv_bytes);
+    combined.extend_from_slice(tag);
+    combined.extend_from_slice(ciphertext);
+    STANDARD.encode(combined)
+}

Also applies to: 100-111, 123-132, 150-161

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/api/rest_tests.rs` around lines 62 - 73, Extract the repeated AES-GCM
encrypt/split/combine/base64 logic into a small test helper (e.g., fn
make_encrypted_blob(plain: &str, key_bytes: &[u8], iv_bytes: &[u8]) -> (String,
String)) that performs Aes256Gcm16::new_from_slice(...), encrypt(...),
split_at(...), assembles iv + tag + ciphertext, and returns the base64 blob and
base64 key (using STANDARD.encode); then replace the duplicated blocks that use
cipher.encrypt, split_at, combined.extend_from_slice, and STANDARD.encode with
calls to this helper (referencing Aes256Gcm16, encrypt, split_at, iv_bytes,
key_bytes, STANDARD.encode in the existing tests such as the blocks around the
current code).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test_anyhow.rs`:
- Line 6: The println! invocation contains a format placeholder but no argument;
update the println!("Display: {}"); call so the format string matches its
arguments—either remove the "{}" placeholder (e.g., println!("Display:");) or
supply the intended value (e.g., println!("Display: {}", some_value)) by passing
the correct variable/expression into the println! macro.

---

Nitpick comments:
In `@src/api/rest_tests.rs`:
- Around line 62-73: Extract the repeated AES-GCM encrypt/split/combine/base64
logic into a small test helper (e.g., fn make_encrypted_blob(plain: &str,
key_bytes: &[u8], iv_bytes: &[u8]) -> (String, String)) that performs
Aes256Gcm16::new_from_slice(...), encrypt(...), split_at(...), assembles iv +
tag + ciphertext, and returns the base64 blob and base64 key (using
STANDARD.encode); then replace the duplicated blocks that use cipher.encrypt,
split_at, combined.extend_from_slice, and STANDARD.encode with calls to this
helper (referencing Aes256Gcm16, encrypt, split_at, iv_bytes, key_bytes,
STANDARD.encode in the existing tests such as the blocks around the current
code).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fe2fdde1-0e12-4783-b108-183b35396701

📥 Commits

Reviewing files that changed from the base of the PR and between 6e2d697 and e3a99c6.

📒 Files selected for processing (2)
  • src/api/rest_tests.rs
  • test_anyhow.rs

Comment thread test_anyhow.rs Outdated
Comment thread src/api/rest_tests.rs

#[test]
fn decrypts_valid_blob() {
let key_bytes = [0x42u8; 32];
Comment thread src/api/rest_tests.rs

#[test]
fn decrypt_fails_on_wrong_key() {
let key_bytes = [0x42u8; 32];
Comment thread src/api/rest_tests.rs

#[test]
fn decrypt_fails_on_tampered_ciphertext() {
let key_bytes = [0x42u8; 32];
Comment thread src/api/rest_tests.rs

#[test]
fn decrypt_fails_on_invalid_utf8() {
let key_bytes = [0x42u8; 32];
@senamakel
Copy link
Copy Markdown
Member Author

@jules pull from main and resolve the merge conflicts

@google-labs-jules
Copy link
Copy Markdown
Contributor

@jules pull from main and resolve the merge conflicts

I attempted to merge main into my branch, but it is already up to date according to the local main. Unfortunately, I don't have internet access to fetch the latest changes from the remote repository to resolve any potential remote-only conflicts. If there are specific conflicts you're seeing, please let me know or I'll wait for a environment where I can fetch.

Adds a comprehensive suite of unit tests for the cryptographic handoff
decryption logic, covering:
- Successful decryption of valid payloads
- Bounds checking for minimum length (IV + Tag)
- Base64 decoding error handling
- AEAD authentication failure (wrong key or tampered ciphertext)
- Post-decryption UTF-8 validation

Tests are deterministic and avoid hardcoded secret literals to comply
with security scanning policies.
@senamakel senamakel closed this May 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants