Skip to content

Commit a7ad2cd

Browse files
committed
test: add Platform mode unit tests
- Proto conversion: NetworkEnforcementMode::Platform -> NetworkMode::Platform - Default: zero proto value -> NetworkMode::Proxy (backward compatible) - Platform mode preserves proxy config for loopback CONNECT proxy - Policy round-trip: serialize/deserialize preserves network_enforcement - Policy validation: Platform mode passes restrictive validation Ref: NVIDIA#899
1 parent 17af0a3 commit a7ad2cd

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

crates/openshell-policy/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,4 +1821,24 @@ network_policies:
18211821
"port >65535 should fail to parse"
18221822
);
18231823
}
1824+
1825+
#[test]
1826+
fn platform_mode_round_trip() {
1827+
let mut policy = restrictive_default_policy();
1828+
policy.network_enforcement = 1; // PLATFORM
1829+
let yaml = serialize_sandbox_policy(&policy).unwrap();
1830+
let parsed = parse_sandbox_policy(&yaml).unwrap();
1831+
assert_eq!(parsed.network_enforcement, 1);
1832+
}
1833+
1834+
#[test]
1835+
fn platform_mode_passes_validation() {
1836+
let mut policy = restrictive_default_policy();
1837+
policy.network_enforcement = 1; // PLATFORM
1838+
let result = validate_sandbox_policy(&policy);
1839+
assert!(
1840+
result.is_ok(),
1841+
"Platform mode should pass validation: {result:?}"
1842+
);
1843+
}
18241844
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Tests for NetworkMode::Platform (Issue #899).
5+
6+
use openshell_sandbox::policy::{NetworkMode, SandboxPolicy};
7+
8+
#[test]
9+
fn platform_mode_from_proto() {
10+
use openshell_core::proto::{NetworkEnforcementMode, SandboxPolicy as ProtoSandboxPolicy};
11+
12+
let mut proto = ProtoSandboxPolicy::default();
13+
proto.network_enforcement = NetworkEnforcementMode::Platform as i32;
14+
15+
let policy: SandboxPolicy = proto.try_into().expect("conversion should succeed");
16+
assert!(
17+
matches!(policy.network.mode, NetworkMode::Platform),
18+
"expected Platform, got {:?}",
19+
policy.network.mode
20+
);
21+
}
22+
23+
#[test]
24+
fn namespace_mode_from_proto_default() {
25+
use openshell_core::proto::SandboxPolicy as ProtoSandboxPolicy;
26+
27+
let proto = ProtoSandboxPolicy::default();
28+
let policy: SandboxPolicy = proto.try_into().expect("conversion should succeed");
29+
assert!(
30+
matches!(policy.network.mode, NetworkMode::Proxy),
31+
"default (zero) should map to Proxy, got {:?}",
32+
policy.network.mode
33+
);
34+
}
35+
36+
#[test]
37+
fn platform_mode_allows_proxy_config() {
38+
use openshell_core::proto::{NetworkEnforcementMode, SandboxPolicy as ProtoSandboxPolicy};
39+
40+
let mut proto = ProtoSandboxPolicy::default();
41+
proto.network_enforcement = NetworkEnforcementMode::Platform as i32;
42+
43+
let policy: SandboxPolicy = proto.try_into().expect("conversion should succeed");
44+
assert!(
45+
policy.network.proxy.is_some(),
46+
"Platform mode should still have proxy config for loopback CONNECT proxy"
47+
);
48+
}

0 commit comments

Comments
 (0)