Three `--offline` gates pass every test switched off: a hosted Cargo unwind, a Gradle rollback with no backup and a gem unwind then send registry requests

Đang mở Phù hợp với người mới
#2,429 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
cli, testing

Hướng nghiên cứu

Issue nêu ba bài kiểm thử hồi quy còn thiếu: tests/upstream_restore_golden.rs, patch/jvm_jar.rs và tests/e2e_redirect_gem_stale_install.rs. Bắt đầu bằng cách đọc các ví dụ kiểm thử được nêu và các điều kiện kiểm tra tương ứng trong patch/redirect/upstream/client.rs, patch/jvm_jar.rs và commands/gem_refresh.rs. Thêm các bài kiểm thử ghi lại những yêu cầu gửi đến registry trong các thao tác ngoại tuyến được mô tả; hoàn thành khi mỗi bài kiểm thử thất bại nếu có yêu cầu ngoại tuyến được gửi và đạt với hành vi hiện tại.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

agent:triaged arch-audit priority:p3 refactor

[agent] Found by the architecture refactor routine. The 56 mutants ran on main at 1bc104a; the three below ran again at 9f2bc04, against the suites that reach them. Test code only: main behaves as the contract says in every case run.

The problem

The contract's --offline is a strict airgap: "never contact the network". Nothing enforces it in one place. The CLI mirrors the flag into SOCKET_OFFLINE for telemetry and the org lookup, and every other request is refused where it is made: 58 places in the CLI and core test the flag or hand it down. A new request path that forgets its test, or a refactor that drops one, sends requests from an airgapped run, and only a test can say so.

For three of those places no test can. With the gate switched off, every test passes and an offline run sends a request:

Gate With it off, an --offline run Tests
the hosted unwind's crates.io lookup (core patch/redirect/upstream/client.rs, line 502, cargo_cksum) rollback, remove and a vendored takeover of a hosted Cargo pin ask the crates.io index for the checksum none reaches the line with offline on
a Gradle jar with no backup (core patch/jvm_jar.rs, line 1073) rollback --offline downloads the jar from Maven Central one unit test reaches it and passes
the gem unwind's checksum lookup (CLI commands/gem_refresh.rs, line 1624, the flag handed to gem_upstream) rollback, remove and vendor --revert ask the gem registry's compact index, on a lock without CHECKSUMS e2e_redirect_gem_stale_install reaches it five times and passes

Why they pass: no test runs a hosted Cargo unwind offline at all (the golden suite does for gem, Composer, PyPI and uv, and other suites for npm, Go and NuGet). The Gradle and gem tests that run offline point no registry at a server that counts, so "asked and was refused" reads the same as "did not ask". #2421 has the gem refresh's sibling gate (line 1758, on a lock with CHECKSUMS).

Evidence

One build carried 56 mutants (each reads its gate as online), behind a switch read from the environment that also logged every time its gate was reached with offline on. One traced run of the 238 test binaries that run without Docker or a live API said which suites reach which gate; each mutant then ran against those suites.

Result Mutants
caught 37: every command's own refusal, the telemetry and org-lookup switches, the update check, the hosted unwind for npm, PyPI, Go, gem, Composer and NuGet, the Maven vendoring lookups
survive or unreached, and a request is sent 3: the table above
survive or unreached, no test written yet 9: the stale-cache check after a vendored gem revert, the native rebuild of a gem refresh and of a rollback, vex fetching a patched package.json blob, vendor over hosted pins (three places), a Maven sidecar lookup, a gem build dependency
equivalent, message only or unreachable 7

Three new tests, each green on main and red on its mutant alone:

# main at 9f2bc04; SPMUT unset
test cargo_offline_restore_asks_no_index ... ok
test patch::jvm_jar::tests::gradle_offline_rollback_without_backup_asks_no_registry ... ok
test an_offline_vendored_unwind_sends_no_request ... ok

SPMUT=52  test cargo_offline_restore_asks_no_index ... FAILED
          tests/fixtures/redirect/cargo/cargo/basic: an offline restore sent ["/se/rd/serde"]
SPMUT=41  test patch::jvm_jar::tests::gradle_offline_rollback_without_backup_asks_no_registry ... FAILED
          an offline rollback sent ["/com/example/lib/1.0/lib-1.0.jar"]
SPMUT=27  test an_offline_vendored_unwind_sends_no_request ... FAILED
          ["rollback", "--yes"]: an offline unwind sent ["/upstream/info/stale-probe-gem"]

# the suites that reach each gate, at 9f2bc04, with the mutant on
SPMUT=52  upstream_restore_golden: 65 passed
SPMUT=41  core patch::jvm_jar unit tests: 16 passed; gradle_agent_cli: 39 passed
SPMUT=27  e2e_redirect_gem_stale_install: 76 passed
The three tests

For core tests/upstream_restore_golden.rs:

/// An offline restore of a hosted Cargo pin asks the crates.io index
/// nothing: each pin is restored from the lock's own twin entry or
/// refused as offline.
#[tokio::test]
#[serial]
async fn cargo_offline_restore_asks_no_index() {
    let mut refused = 0;
    for case in load("cargo/cargo") {
        if !case.input.contains_key("Cargo.lock") {
            continue;
        }
        let server = MockServer::start().await;
        let _env = EnvGuard::set("SOCKET_CRATES_INDEX", server.uri());
        let (after, statuses) = run_case_with(&case, None, &offline()).await;
        let sent: Vec<String> = server
            .received_requests()
            .await
            .unwrap()
            .iter()
            .map(|r| r.url.path().to_string())
            .collect();
        assert!(sent.is_empty(), "{}: an offline restore sent {sent:?}", case.dir.display());
        for (purl, status) in &statuses {
            if let PinStatus::Refused(why) = status {
                assert!(why.contains("offline"), "{}: {purl}: {why}", case.dir.display());
                refused += 1;
            }
        }
        if statuses.iter().all(|(_, s)| matches!(s, PinStatus::Refused(_))) {
            assert_eq!(after, case.expected, "{}: a refused pin must change nothing", case.dir.display());
        }
    }
    assert!(refused > 0, "no case was refused offline");
}

For the tests of core patch/jvm_jar.rs:

/// A Gradle copy that holds the patch and has no backup is left as it is
/// by an offline rollback, which asks the registry nothing.
#[tokio::test]
#[serial_test::serial]
async fn gradle_offline_rollback_without_backup_asks_no_registry() {
    let original = pristine_jar();
    let files = record();
    let server = wiremock::MockServer::start().await;
    let _registry = EnvGuard::set("SOCKET_MAVEN_REGISTRY", server.uri());
    let d = tempfile::tempdir().unwrap();
    let socket = d.path().join(".socket");
    let hash_dir = d
        .path()
        .join(".gradle/caches/modules-2/files-2.1/com.example/lib/1.0")
        .join(sha1_hex(&original));
    std::fs::create_dir_all(&hash_dir).unwrap();
    std::fs::write(hash_dir.join("lib-1.0.jar"), patched_jar()).unwrap();
    let back = rollback_jar_swap(&restore(&files, &socket, true), &[hash_dir.clone()]).await;
    let sent: Vec<String> = server
        .received_requests()
        .await
        .unwrap()
        .iter()
        .map(|r| r.url.path().to_string())
        .collect();
    assert!(sent.is_empty(), "an offline rollback sent {sent:?}");
    assert!(!back[0].success);
    let error = back[0].error.as_deref().unwrap();
    assert!(error.contains("--offline prevents downloading it again"), "{error}");
    assert!(std::fs::read(hash_dir.join("lib-1.0.jar")).unwrap() == patched_jar());
}

For CLI tests/e2e_redirect_gem_stale_install.rs:

/// An offline vendored unwind sends no request: not to the patch
/// service, and not to the gem registry for the checksum of a gem the
/// lock pins none for.
#[tokio::test(flavor = "multi_thread")]
async fn an_offline_vendored_unwind_sends_no_request() {
    for unwind in VENDORED_UNWINDS {
        let server = MockServer::start().await;
        let tmp = tempfile::tempdir().unwrap();
        let proj = tmp.path().join("proj");
        let (_gem_dir, archive) = agent_then_vendored(&server, &proj).await;
        std::fs::remove_dir_all(archive.parent().unwrap()).unwrap();
        let before = server.received_requests().await.unwrap().len();
        let mut args = unwind.to_vec();
        args.push("--json");
        args.push("--offline");
        let (code, stdout, stderr) = unwind_run(&proj, &server.uri(), &args);
        assert_eq!(code, 0, "{unwind:?}: {stdout}\nstderr:\n{stderr}");
        let after = server.received_requests().await.unwrap();
        let sent: Vec<String> = after[before..].iter().map(|r| r.url.path().to_string()).collect();
        assert!(sent.is_empty(), "{unwind:?}: an offline unwind sent {sent:?}");
    }
}

The mutant is one line at each gate, for example if self.offline && !crate::spmut(52) {, with spmut(n) reading SPMUT from the environment. The production diff was never committed.

Not run: Windows and macOS, the Docker suites, the tests that need a live API, and the two in-memory hosted suites, whose harness clears the environment the switch is read from. The nine gates of the third row were not probed: each needs a scenario where the mutant sends a request.

Planned fix

Add the three tests. No production change.

A wider fix would be structural: refuse an offline request in the two functions every registry request of the unwind goes through, and in the one client the vendored and JVM paths share, so that a new lookup cannot forget its test. That is a refactor with no defect behind it today, so it is not planned here.


Generated by Claude Code

Ngôn ngữ chính
Rust
Star
9
Fork
2
Merge trung bình
1 ngày 2 giờ
Pull request đã merge (30 ngày)
213

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của SocketDev/socket-patch

Tất cả issue của SocketDev/socket-patch

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.