ctb_workspace_ipc/services/
network.rs1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29include!("network.generated.rs");
30
31#[cfg(test)]
32#[allow(
33 clippy::panic,
34 clippy::expect_used,
35 clippy::unwrap_used,
36 clippy::unwrap_in_result,
37 clippy::panic_in_result_fn,
38 clippy::indexing_slicing,
39 clippy::arithmetic_side_effects,
40 reason = "Standard repository test boilerplate"
41)]
42mod tests {
43 use super::*;
44
45 use crate::auth::capability::{
46 CapabilitySet, MethodRule, MethodSelector, ServiceName,
47 };
48
49 use crate::protocol::{MethodId, Request};
50 use crate::router::{ConnectionContext, Router};
51 use crate::services::network::{
52 METHOD_FETCH, METHOD_READ_FILE, SERVICE_NAME as NETWORK_SERVICE_NAME,
53 };
54 use crate::types::ConnectionId;
55
56 use std::collections::HashMap;
57 use std::sync::Arc;
58
59 fn ensure_mock_backend() -> anyhow::Result<()> {
60 let backend: Arc<dyn ctb_network::NetworkBackend> =
61 Arc::new(ctb_network::MockNetworkBackend);
62 ctb_network::init_backend(&backend)?;
63 Ok(())
64 }
65
66 #[expect(
68 clippy::panic_in_result_fn,
69 reason = "Standard repository test boilerplate"
70 )]
71 #[crate::ctb_test("tokio")]
72 async fn routes_network_fetch() -> anyhow::Result<()> {
73 ensure_mock_backend()?;
74 let router = crate::router::IpcRouter::new();
75
76 let mut allowed: HashMap<ServiceName, Vec<MethodRule>> = HashMap::new();
77 allowed.insert(
78 ServiceName(NETWORK_SERVICE_NAME.to_string()),
79 vec![MethodRule {
80 method: MethodSelector::Exact(METHOD_FETCH.into()),
81 quotas: None,
82 }],
83 );
84
85 let ctx = ConnectionContext {
86 id: ConnectionId::default(),
87 capabilities: CapabilitySet {
88 allowed,
89 global_limits: None,
90 },
91 metadata: None,
92 };
93
94 let args = postcard_helpers::encode(
95 &"https://example.com".to_string(),
96 "url",
97 )?;
98 let req = Request {
99 id: Default::default(),
100 method: MethodId {
101 service: NETWORK_SERVICE_NAME.into(),
102 method: METHOD_FETCH.into(),
103 },
104 args,
105 };
106
107 let resp = router.dispatch(&ctx, req).await?;
108 anyhow::ensure!(resp.ok, "expected ok response, got: {:?}", resp.error);
109 let bytes = resp
110 .result
111 .ok_or_else(|| anyhow::anyhow!("missing result bytes"))?;
112 let decoded: Vec<u8> =
113 postcard_helpers::decode(&bytes, "fetch result")?;
114 anyhow::ensure!(
115 decoded
116 == vec![
117 69, 120, 97, 109, 112, 108, 101, 32, 72, 84, 84, 80, 83,
118 32, 70, 101, 116, 99, 104
119 ],
120 "unexpected bytes: {decoded:?}"
121 );
122 Ok(())
123 }
124
125 #[expect(
127 clippy::panic_in_result_fn,
128 reason = "Standard repository test boilerplate"
129 )]
130 #[crate::ctb_test("tokio")]
131 async fn routes_network_read_file() -> anyhow::Result<()> {
132 ensure_mock_backend()?;
133 let router = crate::router::IpcRouter::new();
134
135 let mut allowed: HashMap<ServiceName, Vec<MethodRule>> = HashMap::new();
136 allowed.insert(
137 ServiceName(NETWORK_SERVICE_NAME.to_string()),
138 vec![MethodRule {
139 method: MethodSelector::Exact(METHOD_READ_FILE.into()),
140 quotas: None,
141 }],
142 );
143
144 let ctx = ConnectionContext {
145 id: ConnectionId::default(),
146 capabilities: CapabilitySet {
147 allowed,
148 global_limits: None,
149 },
150 metadata: None,
151 };
152
153 let args: Vec<u8> = postcard_helpers::encode(
154 &"/tmp/example/file.txt".to_string(),
155 "file path",
156 )?;
157 let req = Request {
158 id: Default::default(),
159 method: MethodId {
160 service: NETWORK_SERVICE_NAME.into(),
161 method: METHOD_READ_FILE.into(),
162 },
163 args,
164 };
165
166 let resp = router.dispatch(&ctx, req).await?;
167 anyhow::ensure!(resp.ok, "expected ok response, got: {:?}", resp.error);
168 let bytes = resp
169 .result
170 .ok_or_else(|| anyhow::anyhow!("missing result bytes"))?;
171 let decoded: Vec<u8> =
172 postcard_helpers::decode(&bytes, "read_file result")?;
173 anyhow::ensure!(
174 decoded
175 == vec![
176 69, 120, 97, 109, 112, 108, 101, 32, 70, 105, 108, 101, 32,
177 82, 101, 97, 100
178 ],
179 "unexpected bytes: {decoded:?}"
180 );
181 Ok(())
182 }
183}