File size: 5,406 Bytes
afa0cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use codex_arg0::Arg0DispatchPaths;
use codex_core::config::ConfigBuilder;
use pretty_assertions::assert_eq;

use super::CheckStatus;
use super::sandbox_check;

#[tokio::test]
async fn reports_missing_linux_sandbox_helper() {
    let home = tempfile::tempdir().expect("create Codex home");
    let config = ConfigBuilder::default()
        .codex_home(home.path().to_path_buf())
        .build()
        .await
        .expect("load sandbox config");
    let arg0_paths = Arg0DispatchPaths {
        codex_linux_sandbox_exe: Some(home.path().join("missing-linux-helper")),
        ..Default::default()
    };

    let check = sandbox_check(&config, &arg0_paths);

    assert_eq!(check.status, CheckStatus::Warning);
    assert_eq!(check.summary, "Linux sandbox helper path does not exist");
}

#[cfg(target_os = "windows")]
#[tokio::test]
async fn reports_refresh_failure_after_setup_completed() {
    let home = tempfile::tempdir().expect("create Codex home");
    let version = codex_windows_sandbox::SETUP_VERSION;
    for (path, contents) in [
        (
            ".sandbox/setup_marker.json",
            serde_json::json!({
                "version": version,
                "offline_username": codex_windows_sandbox::OFFLINE_USERNAME,
                "online_username": codex_windows_sandbox::ONLINE_USERNAME,
            }),
        ),
        (
            ".sandbox-secrets/sandbox_users.json",
            serde_json::json!({
                "version": version,
                "offline": {
                    "username": codex_windows_sandbox::OFFLINE_USERNAME,
                    "password": "",
                },
                "online": {
                    "username": codex_windows_sandbox::ONLINE_USERNAME,
                    "password": "",
                },
            }),
        ),
    ] {
        let path = home.path().join(path);
        std::fs::create_dir_all(path.parent().expect("fixture parent"))
            .expect("create fixture directory");
        std::fs::write(path, contents.to_string()).expect("write sandbox fixture");
    }
    assert!(codex_windows_sandbox::sandbox_setup_is_complete(
        home.path()
    ));
    codex_windows_sandbox::write_setup_error_report(
        home.path(),
        &codex_windows_sandbox::SetupErrorReport {
            code: codex_windows_sandbox::SetupErrorCode::HelperFirewallPolicyIneffective,
            message: "firewall policy rejected the refresh".to_string(),
        },
    )
    .expect("write refresh failure");
    std::fs::write(
        home.path().join("config.toml"),
        "[windows]\nsandbox = \"elevated\"\n",
    )
    .expect("write elevated sandbox config");
    let config = ConfigBuilder::default()
        .codex_home(home.path().to_path_buf())
        .build()
        .await
        .expect("load sandbox config");

    let check = sandbox_check(&config, &Arg0DispatchPaths::default());

    assert_eq!(check.status, CheckStatus::Fail);
    assert_eq!(
        check.issues[0].measured.as_deref(),
        Some("helper_firewall_policy_ineffective")
    );
}

#[cfg(target_os = "windows")]
#[tokio::test]
async fn rejects_oversized_setup_failure_reports() {
    let home = tempfile::tempdir().expect("create Codex home");
    codex_windows_sandbox::write_setup_error_report(
        home.path(),
        &codex_windows_sandbox::SetupErrorReport {
            code: codex_windows_sandbox::SetupErrorCode::HelperFirewallPolicyIneffective,
            message: "x".repeat(64 * 1024),
        },
    )
    .expect("write oversized setup failure");
    std::fs::write(
        home.path().join("config.toml"),
        "[windows]\nsandbox = \"elevated\"\n",
    )
    .expect("write elevated sandbox config");
    let config = ConfigBuilder::default()
        .codex_home(home.path().to_path_buf())
        .build()
        .await
        .expect("load sandbox config");

    let check = sandbox_check(&config, &Arg0DispatchPaths::default());

    assert_eq!(check.status, CheckStatus::Warning);
    assert!(
        check
            .details
            .contains(&"setup failure report: unreadable".to_string())
    );
    assert_eq!(
        check.issues[0].cause,
        "Windows sandbox setup failure report could not be read"
    );
    assert_eq!(
        check.issues[0].measured.as_deref(),
        Some("larger than 64 KiB")
    );
}

#[cfg(target_os = "windows")]
#[tokio::test]
async fn reports_malformed_setup_failure_reports() {
    let home = tempfile::tempdir().expect("create Codex home");
    let path = codex_windows_sandbox::setup_error_path(home.path());
    std::fs::create_dir_all(path.parent().expect("setup failure report parent"))
        .expect("create sandbox directory");
    std::fs::write(path, "{").expect("write malformed setup failure report");
    std::fs::write(
        home.path().join("config.toml"),
        "[windows]\nsandbox = \"elevated\"\n",
    )
    .expect("write elevated sandbox config");
    let config = ConfigBuilder::default()
        .codex_home(home.path().to_path_buf())
        .build()
        .await
        .expect("load sandbox config");

    let check = sandbox_check(&config, &Arg0DispatchPaths::default());

    assert_eq!(check.status, CheckStatus::Warning);
    assert_eq!(
        check.issues[0].cause,
        "Windows sandbox setup failure report could not be read"
    );
    assert!(check.issues[0].measured.is_some());
}