crabpot 0.10.7

Native Rust sandbox runtime for AI agents, rewritten from Anthropic's sandbox-runtime
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! What a Windows host reads before a command runs: the sandbox user, the WFP
//! fence and the profile grant, from the three srt-win queries upstream's
//! `getWindowsSandboxUserStatus`, `mapWfpStatus` and
//! `checkWindowsSandboxStatus` make.

use core::ops::RangeInclusive;
use std::collections::BTreeMap;

use crabpot_windows::fs_access::{
    ConflictOptions, Disk, Host, ProfileGrant, profile_exclusion_conflicts,
};
use serde::Deserialize;

use crate::{ProfileConflict, StartError};

/// The variables that point a sandboxed tool at the real user's config files
/// in place, in upstream's order. `profile status` may report any of them, and
/// a status keeps no other name it reports.
pub const PROFILE_READ_ENV_VARS: [&str; 6] = [
    "NPM_CONFIG_USERCONFIG",
    "GIT_CONFIG_GLOBAL",
    "PIP_CONFIG_FILE",
    "MAVEN_ARGS",
    "RUSTUP_HOME",
    "CARGO_HOME",
];

/// What the install says about the sandbox, read without elevation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct WindowsStatus {
    /// Whether a command can be launched as the sandbox user.
    pub user: UserState,
    /// The sandbox user's SID, when it has an account.
    pub sandbox_sid: Option<String>,
    /// The SID of the group the sandbox user is in, when it has one. A grant
    /// made for the sandbox is made for this group or for the account above.
    pub sandbox_group_sid: Option<String>,
    /// The SID of the account this process runs as, when the machine names it.
    pub real_user_sid: Option<String>,
    /// The SID of the user who owns the session this process runs in, when the
    /// machine names one.
    pub session_user_sid: Option<String>,
    /// The install's setup version beside this build's.
    pub setup_version: SetupVersion,
    /// Whether the WFP fence leaves loopback open.
    pub loopback: LoopbackMode,
    /// The loopback ports the fence leaves open for the proxy, when it has
    /// filters to read them from.
    pub proxy_ports: Option<RangeInclusive<u16>>,
    /// Whether a profile read grant is in place.
    pub profile: ProfileState,
    /// What the profile read grant keeps unreadable, empty when there is none.
    pub profile_exclusions: ProfileExclusions,
    /// The variables, from [`PROFILE_READ_ENV_VARS`], that name the real user's
    /// files the profile read grant reaches, each with its value. Empty unless
    /// the grant is current. Nothing sets these on a sandboxed command: a host
    /// that wants them sets them itself.
    pub profile_read_env: Vec<(String, String)>,
    /// Whether the Secondary Logon service can launch the sandbox user. False
    /// when it is disabled or missing, and true when it could not be read,
    /// which is not proof of a problem.
    pub seclogon_runs: bool,
    /// Whether this process runs as the user who owns its session.
    pub in_desktop_session: bool,
    /// Whether the sandbox user can read this binary, which it runs as the
    /// runner, by the binary's own access list: the check
    /// [`Sandbox::start`](crate::Sandbox::start) makes before it grants
    /// anything, which a host can read ahead of a start. `None` when the machine
    /// could not make the check, which refuses nothing.
    pub runner_readable: Option<bool>,
}

/// Whether a command can be launched as the sandbox user.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum UserState {
    /// The user exists, its credential is stored, and the install's setup
    /// version is this build's.
    Provisioned,
    /// Any of those is missing. A setup version mismatch reads as this, since
    /// srt-win refuses a launch for it the way it does for no install.
    NotProvisioned,
}

/// The setup version of the install and of this build.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct SetupVersion {
    /// What the install recorded, or `None` when there is no install.
    pub installed: Option<u32>,
    /// What this build needs.
    pub expected: u32,
}

/// Whether the sandbox user may connect to loopback.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LoopbackMode {
    /// Every loopback port is open.
    Open,
    /// Only the proxy's ports are open.
    Closed,
}

/// The paths a profile read grant keeps out of a session's grants, as
/// `profile status` records them.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
pub struct ProfileExclusions {
    /// Every protected path the grant keeps unreadable, wildcards as written. A
    /// session may not grant one, a path inside one, or a path above one.
    #[serde(default)]
    pub exclusions: Vec<String>,
    /// Directories the grant leaves ungranted without denying them. A session
    /// may not grant one, or a path above one.
    #[serde(default)]
    pub ungranted_only: Vec<String>,
}

impl ProfileExclusions {
    /// The `paths` a session may not grant, each with the exclusion it meets, as
    /// upstream's `windowsProfileExclusionConflicts` finds them. A path equal
    /// to, inside or above a protected exclusion meets it, and above a wildcard
    /// one only when something on disk beneath matches. An ungranted-only
    /// directory is met by a path equal to it or above it. `write` is whether
    /// the paths would be granted for writing, and is recorded on each result.
    ///
    /// Paths are read against this process's home directory, working directory
    /// and disk. With no profile grant recorded there are no exclusions, and
    /// nothing conflicts.
    #[must_use]
    pub fn conflicts(&self, paths: &[String], write: bool) -> Vec<ProfileConflict> {
        self.conflicts_on(&Host::from_process(), paths, write)
    }

    /// [`Self::conflicts`] against `host`.
    pub(crate) fn conflicts_on<D: Disk>(
        &self,
        host: &Host<D>,
        paths: &[String],
        write: bool,
    ) -> Vec<ProfileConflict> {
        let grant = ProfileGrant {
            exclusions: &self.exclusions,
            ungranted_only: &self.ungranted_only,
        };
        let options = ConflictOptions {
            write,
            ..ConflictOptions::default()
        };
        profile_exclusion_conflicts(host, paths, &grant, &options)
            .into_iter()
            .map(|conflict| ProfileConflict {
                path: conflict.path,
                exclusion: conflict.exclusion,
                write,
            })
            .collect()
    }
}

/// Whether the profile read grant is in place, as `profile status` reports it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProfileState {
    /// No grant.
    None,
    /// A grant a session may rely on.
    Current,
    /// A grant an older list made.
    Outdated,
    /// A grant that did not finish.
    Incomplete,
    /// A grant or forget is running.
    InProgress,
    /// The record could not be read, or says what this build does not know.
    Unknown,
}

impl WindowsStatus {
    /// The account and the group a sandbox command runs as, which is who a grant
    /// made for the sandbox names. A machine with no install names neither, and
    /// a check left with nothing but the well-known groups.
    #[must_use]
    pub fn sandbox_sids(&self) -> Vec<&str> {
        let mut sids = Vec::new();
        sids.extend(self.sandbox_sid.as_deref());
        sids.extend(self.sandbox_group_sid.as_deref());
        sids
    }

    /// The [`Self::profile_read_env`] pairs a host has not already set. A name
    /// in `supplied` is matched without regard to case, as Windows reads
    /// variable names, and is left out.
    #[must_use]
    pub fn profile_read_env_for<'a>(
        &self,
        supplied: impl IntoIterator<Item = &'a str>,
    ) -> Vec<(String, String)> {
        let supplied: Vec<String> = supplied.into_iter().map(str::to_uppercase).collect();
        self.profile_read_env
            .iter()
            .filter(|(name, _)| !supplied.contains(&name.to_uppercase()))
            .cloned()
            .collect()
    }
}

/// The error a srt-win query that exits with `code` stands for, when it is one
/// of the exits a host acts on: 15 for no usable install, 18 for seclogon.
pub fn refusal_for_exit(code: i32) -> Option<StartError> {
    match code {
        15 => Some(StartError::NotProvisioned),
        18 => Some(StartError::SeclogonDisabled),
        _ => None,
    }
}

/// `user status`, as far as a status reads it.
#[derive(Deserialize)]
struct UserJson {
    user: UserAccount,
    cred_present: bool,
    marker_version: Option<u32>,
    setup_version: u32,
    real_user_sid: Option<String>,
    session_user_sid: Option<String>,
    seclogon: String,
}

#[derive(Deserialize)]
struct UserAccount {
    exists: bool,
    sid: Option<String>,
    group_sid: Option<String>,
}

/// `wfp status`, as far as a status reads it.
#[derive(Deserialize)]
struct WfpJson {
    port_range: Option<[u16; 2]>,
    allow_loopback: bool,
}

/// `profile status`, as far as a status reads it.
#[derive(Deserialize)]
struct ProfileJson {
    state: String,
    #[serde(default)]
    env: BTreeMap<String, String>,
    #[serde(flatten)]
    exclusions: ProfileExclusions,
}

/// Reads the status through `run`, which runs one srt-win query with the
/// arguments after it and returns what it printed. `sublayer` is the WFP
/// sublayer the filters were installed under, when it is not the default.
pub(crate) fn read(
    sublayer: Option<&str>,
    run: impl Fn(&'static str, &[&str]) -> Result<String, StartError>,
) -> Result<WindowsStatus, StartError> {
    let user: UserJson = parse("user status", &[], &run)?;
    let wfp_args: Vec<&str> = sublayer
        .into_iter()
        .flat_map(|guid| ["--sublayer-guid", guid])
        .collect();
    let wfp: WfpJson = parse("wfp status", &wfp_args, &run)?;
    let profile: ProfileJson = parse("profile status", &[], &run)?;
    let profile_state = match profile.state.as_str() {
        "none" => ProfileState::None,
        "current" => ProfileState::Current,
        "outdated" => ProfileState::Outdated,
        "incomplete" => ProfileState::Incomplete,
        "in_progress" => ProfileState::InProgress,
        _ => ProfileState::Unknown,
    };
    let profile_read_env = if profile_state == ProfileState::Current {
        PROFILE_READ_ENV_VARS
            .iter()
            .filter_map(|name| {
                let value = profile.env.get(*name).filter(|value| !value.is_empty())?;
                Some(((*name).to_owned(), value.clone()))
            })
            .collect()
    } else {
        Vec::new()
    };
    let provisioned =
        user.user.exists && user.cred_present && user.marker_version == Some(user.setup_version);
    // A session without an owner is session 0, which is refused by name before
    // the status is read.
    let in_desktop_session =
        user.session_user_sid.is_some() && user.session_user_sid == user.real_user_sid;
    Ok(WindowsStatus {
        user: if provisioned {
            UserState::Provisioned
        } else {
            UserState::NotProvisioned
        },
        sandbox_sid: user.user.sid,
        sandbox_group_sid: user.user.group_sid,
        real_user_sid: user.real_user_sid,
        session_user_sid: user.session_user_sid,
        setup_version: SetupVersion {
            installed: user.marker_version,
            expected: user.setup_version,
        },
        loopback: if wfp.allow_loopback {
            LoopbackMode::Open
        } else {
            LoopbackMode::Closed
        },
        proxy_ports: wfp.port_range.map(|[low, high]| low..=high),
        profile: profile_state,
        profile_exclusions: profile.exclusions,
        profile_read_env,
        seclogon_runs: !matches!(user.seclogon.as_str(), "disabled" | "missing"),
        in_desktop_session,
        // The runner check reads this binary's own access list, which no srt-win
        // query answers: the caller that names this binary fills it.
        runner_readable: None,
    })
}

/// Runs `command` through `run` and reads what it printed as `T`.
fn parse<T: for<'de> Deserialize<'de>>(
    command: &'static str,
    args: &[&str],
    run: &impl Fn(&'static str, &[&str]) -> Result<String, StartError>,
) -> Result<T, StartError> {
    serde_json::from_str(&run(command, args)?).map_err(|_| StartError::StatusFailed {
        command,
        code: None,
        stderr: String::new(),
    })
}

#[cfg(test)]
mod tests {
    use core::cell::RefCell;

    use crabpot_windows::fs_access::{Disk, DiskError, Entry, Kind};

    use super::*;

    /// A volume where only the listed directories exist, spelled as given.
    struct Volume(&'static [&'static str]);

    impl Disk for Volume {
        fn real_path(&self, path: &str) -> Result<String, DiskError> {
            self.0
                .iter()
                .find(|dir| dir.eq_ignore_ascii_case(path))
                .map(|dir| (*dir).to_owned())
                .ok_or(DiskError::Absent)
        }

        fn exists(&self, path: &str) -> bool {
            self.real_path(path).is_ok()
        }

        fn lstat(&self, path: &str) -> Result<Kind, DiskError> {
            self.real_path(path).map(|_| Kind::Dir)
        }

        fn read_dir(&self, _: &str) -> Result<Vec<Entry>, DiskError> {
            Ok(Vec::new())
        }
    }

    fn host() -> Host<Volume> {
        Host::new(
            Some(r"C:\Users\me"),
            Some(r"C:\work"),
            Volume(&[r"C:\Users\me\.cargo", r"C:\Users\me\.cargo\bin"]),
        )
    }

    fn cargo_credentials() -> ProfileExclusions {
        ProfileExclusions {
            exclusions: vec![r"C:\Users\me\.cargo\credentials".to_owned()],
            ungranted_only: Vec::new(),
        }
    }

    #[test]
    fn a_path_above_a_protected_exclusion_conflicts() {
        let found =
            cargo_credentials().conflicts_on(&host(), &[r"C:\Users\me\.cargo".to_owned()], false);

        assert_eq!(
            found,
            [ProfileConflict {
                path: r"C:\Users\me\.cargo".to_owned(),
                exclusion: r"C:\Users\me\.cargo\credentials".to_owned(),
                write: false,
            }]
        );
    }

    #[test]
    fn a_path_beside_an_exclusion_does_not_conflict() {
        let found = cargo_credentials().conflicts_on(
            &host(),
            &[r"C:\Users\me\.cargo\bin".to_owned()],
            false,
        );

        assert_eq!(found, []);
    }

    #[test]
    fn a_write_conflict_says_so() {
        let found =
            cargo_credentials().conflicts_on(&host(), &[r"C:\Users\me\.cargo".to_owned()], true);

        assert!(found.iter().all(|conflict| conflict.write));
        assert_eq!(found.len(), 1);
    }

    #[test]
    fn no_exclusions_means_no_conflicts() {
        let found = ProfileExclusions::default().conflicts_on(
            &host(),
            &[r"C:\Users\me\.cargo".to_owned()],
            false,
        );

        assert_eq!(found, []);
    }

    const PROVISIONED: &str = r#"{
        "user": {"name": "crabpot-sandbox", "exists": true, "sid": "S-1-5-21-1",
                 "group_exists": true, "group_sid": "S-1-5-21-9",
                 "in_builtin_users": true,
                 "in_sandbox_group": true, "hidden_from_logon": true},
        "cred_present": true, "marker_version": 3, "setup_version": 3,
        "real_user_sid": "S-1-5-21-2", "session_user_sid": "S-1-5-21-2",
        "profile_grant": {"state": "present"}, "seclogon": "ok"
    }"#;
    const WFP: &str =
        r#"{"state":"installed","filters":4,"port_range":[3128,3130],"allow_loopback":false}"#;
    const PROFILE: &str = r#"{"state":"current","record_version":1}"#;
    /// An account with no group beside it, which is what a machine whose sandbox
    /// group is missing reads as.
    const NO_GROUP: &str = r#"{
        "user": {"name": "crabpot-sandbox", "exists": true, "sid": "S-1-5-21-1",
                 "group_exists": false, "group_sid": null,
                 "in_builtin_users": true,
                 "in_sandbox_group": false, "hidden_from_logon": true},
        "cred_present": true, "marker_version": 3, "setup_version": 3,
        "real_user_sid": "S-1-5-21-2", "session_user_sid": "S-1-5-21-2",
        "profile_grant": {"state": "present"}, "seclogon": "ok"
    }"#;

    /// A srt-win whose three queries print what it is given.
    fn fake(user: &str, wfp: &str) -> impl Fn(&'static str, &[&str]) -> Result<String, StartError> {
        let (user, wfp) = (user.to_owned(), wfp.to_owned());
        move |command, _| match command {
            "user status" => Ok(user.clone()),
            "wfp status" => Ok(wfp.clone()),
            "profile status" => Ok(PROFILE.to_owned()),
            other => panic!("unexpected query {other}"),
        }
    }

    #[test]
    fn user_status_json_maps_to_the_state() {
        let status = read(None, fake(PROVISIONED, WFP)).unwrap();

        assert_eq!(status.user, UserState::Provisioned);
        assert_eq!(status.sandbox_sid.as_deref(), Some("S-1-5-21-1"));
        assert_eq!(status.sandbox_group_sid.as_deref(), Some("S-1-5-21-9"));
        assert_eq!(status.sandbox_sids(), ["S-1-5-21-1", "S-1-5-21-9"]);
        assert_eq!(status.real_user_sid.as_deref(), Some("S-1-5-21-2"));
        assert_eq!(status.session_user_sid.as_deref(), Some("S-1-5-21-2"));
        assert_eq!(
            status.setup_version,
            SetupVersion {
                installed: Some(3),
                expected: 3
            }
        );
        assert_eq!(status.loopback, LoopbackMode::Closed);
        assert_eq!(status.profile, ProfileState::Current);
        assert!(status.seclogon_runs);
        assert!(status.in_desktop_session);
    }

    /// `read` runs the three srt-win queries and reads no access list, so the
    /// runner check is left to the caller that names this binary.
    #[test]
    fn a_read_status_leaves_the_runner_check_to_the_caller() {
        let status = read(None, fake(PROVISIONED, WFP)).unwrap();

        assert_eq!(status.runner_readable, None);
    }

    /// A grant made for the sandbox is made for its account or for the group it
    /// is in, and a machine missing one of the two is asked about the other.
    #[test]
    fn the_sandbox_sids_are_who_a_grant_for_the_sandbox_names() {
        let both = read(None, fake(PROVISIONED, WFP)).unwrap();
        assert_eq!(both.sandbox_sids(), ["S-1-5-21-1", "S-1-5-21-9"]);

        let no_group = read(None, fake(NO_GROUP, WFP)).unwrap();
        assert_eq!(no_group.sandbox_sids(), ["S-1-5-21-1"]);
    }

    #[test]
    fn setup_version_mismatch_is_reported_not_provisioned() {
        let older = PROVISIONED.replace(r#""marker_version": 3"#, r#""marker_version": 2"#);

        let status = read(None, fake(&older, WFP)).unwrap();

        assert_eq!(status.user, UserState::NotProvisioned);
        assert_eq!(
            status.setup_version,
            SetupVersion {
                installed: Some(2),
                expected: 3
            }
        );
    }

    #[test]
    fn no_install_is_not_provisioned() {
        let none = r#"{"user": {"exists": false}, "cred_present": false,
            "marker_version": null, "setup_version": 3, "real_user_sid": "S-1-5-21-2",
            "session_user_sid": "S-1-5-21-2", "profile_grant": {"state": "none"},
            "seclogon": "disabled"}"#;
        let absent = r#"{"state":"absent","filters":0,"allow_loopback":true}"#;

        let status = read(None, fake(none, absent)).unwrap();

        assert_eq!(status.user, UserState::NotProvisioned);
        assert_eq!(status.setup_version.installed, None);
        assert_eq!(status.proxy_ports, None);
        assert_eq!(status.loopback, LoopbackMode::Open);
        assert!(!status.seclogon_runs);
    }

    #[test]
    fn the_profile_exclusions_come_from_profile_status() {
        let profile = r#"{"state":"current","exclusions":["C:\\Users\\me\\.ssh"],
            "ungranted_only":["C:\\Users\\me\\AppData\\Local\\Temp"]}"#;

        let status = read(None, |command, _| match command {
            "user status" => Ok(PROVISIONED.to_owned()),
            "wfp status" => Ok(WFP.to_owned()),
            _ => Ok(profile.to_owned()),
        })
        .unwrap();

        assert_eq!(
            status.profile_exclusions,
            ProfileExclusions {
                exclusions: vec![r"C:\Users\me\.ssh".to_owned()],
                ungranted_only: vec![r"C:\Users\me\AppData\Local\Temp".to_owned()],
            }
        );
    }

    /// A srt-win whose `profile status` prints `profile`.
    fn status_with_profile(profile: &str) -> WindowsStatus {
        let profile = profile.to_owned();
        read(None, move |command, _| match command {
            "user status" => Ok(PROVISIONED.to_owned()),
            "wfp status" => Ok(WFP.to_owned()),
            _ => Ok(profile.clone()),
        })
        .unwrap()
    }

    #[test]
    fn a_current_grant_reads_its_profile_env() {
        let status = status_with_profile(
            r#"{"state":"current","env":{"GIT_CONFIG_GLOBAL":"C:\\Users\\me\\.gitconfig"}}"#,
        );

        assert_eq!(
            status.profile_read_env,
            [(
                "GIT_CONFIG_GLOBAL".to_owned(),
                r"C:\Users\me\.gitconfig".to_owned()
            )]
        );
    }

    #[test]
    fn a_grant_that_is_not_current_has_no_profile_env() {
        let status = status_with_profile(
            r#"{"state":"outdated","env":{"GIT_CONFIG_GLOBAL":"C:\\Users\\me\\.gitconfig"}}"#,
        );

        assert_eq!(status.profile_read_env, []);
    }

    #[test]
    fn an_unknown_or_empty_profile_env_name_is_dropped() {
        let status =
            status_with_profile(r#"{"state":"current","env":{"OTHER":"x","PIP_CONFIG_FILE":""}}"#);

        assert_eq!(status.profile_read_env, []);
    }

    #[test]
    fn a_name_the_host_already_sets_is_left_out() {
        let status = status_with_profile(
            r#"{"state":"current","env":{"CARGO_HOME":"C:\\Users\\me\\.cargo",
                "GIT_CONFIG_GLOBAL":"C:\\Users\\me\\.gitconfig"}}"#,
        );

        assert_eq!(
            status.profile_read_env_for(["git_config_global"]),
            [("CARGO_HOME".to_owned(), r"C:\Users\me\.cargo".to_owned())]
        );
        assert_eq!(status.profile_read_env_for([]).len(), 2);
    }

    #[test]
    fn exit_codes_15_and_18_map_to_their_errors() {
        assert_eq!(refusal_for_exit(15), Some(StartError::NotProvisioned));
        assert_eq!(refusal_for_exit(18), Some(StartError::SeclogonDisabled));
        assert_eq!(refusal_for_exit(0), None);
        assert_eq!(refusal_for_exit(1), None);
    }

    #[test]
    fn the_port_range_comes_from_wfp_status() {
        let status = read(None, fake(PROVISIONED, WFP)).unwrap();

        assert_eq!(status.proxy_ports, Some(3128..=3130));
    }

    #[test]
    fn a_user_in_another_users_session_is_not_in_the_desktop_session() {
        let runas = PROVISIONED.replace(
            r#""session_user_sid": "S-1-5-21-2""#,
            r#""session_user_sid": "S-1-5-21-9""#,
        );

        let status = read(None, fake(&runas, WFP)).unwrap();

        assert!(!status.in_desktop_session);
    }

    #[test]
    fn the_sublayer_reaches_wfp_status_alone() {
        let asked = RefCell::new(Vec::new());
        let guid = "0f4b1c2d-0000-4000-8000-000000000001";

        read(Some(guid), |command, args| {
            asked.borrow_mut().push((command, args.join(" ")));
            fake(PROVISIONED, WFP)(command, args)
        })
        .unwrap();

        assert_eq!(
            asked.into_inner(),
            [
                ("user status", String::new()),
                ("wfp status", format!("--sublayer-guid {guid}")),
                ("profile status", String::new()),
            ]
        );
    }

    #[test]
    fn a_failed_status_carries_what_srt_win_printed() {
        let error = read(None, |command, _| {
            Err(StartError::StatusFailed {
                command,
                code: Some(1),
                stderr: "deny refused: x".to_owned(),
            })
        })
        .unwrap_err();

        assert_eq!(
            error,
            StartError::StatusFailed {
                command: "user status",
                code: Some(1),
                stderr: "deny refused: x".to_owned()
            }
        );
        assert!(
            error
                .to_string()
                .ends_with("`srt-win user status` exited 1: deny refused: x")
        );
    }

    #[test]
    fn output_that_is_not_json_is_a_failed_query() {
        let error = read(None, fake("not json", WFP)).unwrap_err();

        assert_eq!(
            error,
            StartError::StatusFailed {
                command: "user status",
                code: None,
                stderr: String::new()
            }
        );
    }
}