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
use {
fidl_fuchsia_component as fcomponent, fidl_fuchsia_component_decl as fdecl,
fidl_fuchsia_io as fio,
};
pub async fn create_child_component(
child_name: &str,
child_url: &str,
collection_name: &str,
realm: &fcomponent::RealmProxy,
) -> Result<(), fcomponent::Error> {
let mut collection_ref = fdecl::CollectionRef { name: collection_name.to_string() };
let child_decl = fdecl::Child {
name: Some(child_name.to_string()),
url: Some(child_url.to_string()),
startup: Some(fdecl::StartupMode::Lazy), environment: None,
..fdecl::Child::EMPTY
};
let child_args = fcomponent::CreateChildArgs {
numbered_handles: None,
..fcomponent::CreateChildArgs::EMPTY
};
realm
.create_child(&mut collection_ref, child_decl, child_args)
.await
.map_err(|_| fcomponent::Error::Internal)??;
Ok(())
}
pub async fn open_child_component_exposed_dir(
child_name: &str,
collection_name: &str,
realm: &fcomponent::RealmProxy,
) -> Result<fio::DirectoryProxy, fcomponent::Error> {
let mut child_ref = fdecl::ChildRef {
name: child_name.to_string(),
collection: Some(collection_name.to_string()),
};
let (client_end, server_end) = fidl::endpoints::create_proxy::<fio::DirectoryMarker>()
.map_err(|_| fcomponent::Error::Internal)?;
realm
.open_exposed_dir(&mut child_ref, server_end)
.await
.map_err(|_| fcomponent::Error::Internal)??;
Ok(client_end)
}
pub async fn destroy_child_component(
child_name: &str,
collection_name: &str,
realm: &fcomponent::RealmProxy,
) -> Result<(), fcomponent::Error> {
let mut child_ref = fdecl::ChildRef {
name: child_name.to_string(),
collection: Some(collection_name.to_string()),
};
realm.destroy_child(&mut child_ref).await.map_err(|_| fcomponent::Error::Internal)??;
Ok(())
}
#[cfg(test)]
mod tests {
use {
super::{
create_child_component, destroy_child_component, open_child_component_exposed_dir,
},
fidl::endpoints::{spawn_stream_handler, Proxy},
fidl_fuchsia_component as fcomponent, fidl_fuchsia_io as fio, fuchsia_async as fasync,
futures::prelude::*,
lazy_static::lazy_static,
test_util::Counter,
};
fn spawn_directory_server<F: 'static>(
mut directory_server: fio::DirectoryRequestStream,
request_handler: F,
) where
F: Fn(fio::DirectoryRequest) + Send,
{
fasync::Task::spawn(async move {
while let Some(directory_request) = directory_server.try_next().await.unwrap() {
request_handler(directory_request);
}
})
.detach();
}
#[fuchsia::test]
async fn create_child_parameters() {
let child_name = "test_child";
let child_url = "test_url";
let child_collection = "test_collection";
let realm_proxy = spawn_stream_handler(move |realm_request| async move {
match realm_request {
fcomponent::RealmRequest::CreateChild { collection, decl, args: _, responder } => {
assert_eq!(decl.name.unwrap(), child_name);
assert_eq!(decl.url.unwrap(), child_url);
assert_eq!(&collection.name, child_collection);
let _ = responder.send(&mut Ok(()));
}
_ => panic!("Realm handler received an unexpected request"),
}
})
.unwrap();
assert!(create_child_component(child_name, child_url, child_collection, &realm_proxy)
.await
.is_ok());
}
#[fuchsia::test]
async fn create_child_success() {
let realm_proxy = spawn_stream_handler(move |realm_request| async move {
match realm_request {
fcomponent::RealmRequest::CreateChild {
collection: _,
decl: _,
args: _,
responder,
} => {
let _ = responder.send(&mut Ok(()));
}
_ => panic!("Realm handler received an unexpected request"),
}
})
.unwrap();
assert!(create_child_component("", "", "", &realm_proxy).await.is_ok());
}
#[fuchsia::test]
async fn create_child_error() {
let realm_proxy = spawn_stream_handler(move |realm_request| async move {
match realm_request {
fcomponent::RealmRequest::CreateChild {
collection: _,
decl: _,
args: _,
responder,
} => {
let _ = responder.send(&mut Err(fcomponent::Error::Internal));
}
_ => panic!("Realm handler received an unexpected request"),
}
})
.unwrap();
assert!(create_child_component("", "", "", &realm_proxy).await.is_err());
}
#[fuchsia::test]
async fn open_child_component_exposed_dir_parameters() {
let child_name = "test_child";
let child_collection = "test_collection";
let realm_proxy = spawn_stream_handler(move |realm_request| async move {
match realm_request {
fcomponent::RealmRequest::OpenExposedDir { child, exposed_dir: _, responder } => {
assert_eq!(child.name, child_name);
assert_eq!(child.collection, Some(child_collection.to_string()));
let _ = responder.send(&mut Ok(()));
}
_ => panic!("Realm handler received an unexpected request"),
}
})
.unwrap();
assert!(open_child_component_exposed_dir(child_name, child_collection, &realm_proxy)
.await
.is_ok());
}
#[fuchsia::test]
async fn open_child_component_exposed_dir_success() {
let realm_proxy = spawn_stream_handler(move |realm_request| async move {
match realm_request {
fcomponent::RealmRequest::OpenExposedDir {
child: _,
exposed_dir: _,
responder,
} => {
let _ = responder.send(&mut Ok(()));
}
_ => panic!("Realm handler received an unexpected request"),
}
})
.unwrap();
assert!(open_child_component_exposed_dir("", "", &realm_proxy).await.is_ok());
}
#[fuchsia::test]
async fn open_child_exposed_dir_success() {
lazy_static! {
static ref CALL_COUNT: Counter = Counter::new(0);
}
let directory_request_handler = |directory_request| match directory_request {
fio::DirectoryRequest::Open { path: fake_capability_path, .. } => {
CALL_COUNT.inc();
assert_eq!(fake_capability_path, "fake_capability_path");
}
_ => panic!("Directory handler received an unexpected request"),
};
let realm_proxy = spawn_stream_handler(move |realm_request| async move {
match realm_request {
fcomponent::RealmRequest::OpenExposedDir {
child: _,
exposed_dir: exposed_dir_server,
responder,
} => {
CALL_COUNT.inc();
spawn_directory_server(
exposed_dir_server.into_stream().unwrap(),
directory_request_handler,
);
let _ = responder.send(&mut Ok(()));
}
_ => panic!("Realm handler received an unexpected request"),
}
})
.unwrap();
let exposed_dir = open_child_component_exposed_dir("", "", &realm_proxy).await.unwrap();
let (client_end, server_end) =
fidl::endpoints::create_proxy::<fio::DirectoryMarker>().unwrap();
assert!(fdio::service_connect_at(
&exposed_dir.into_channel().unwrap().into_zx_channel(),
"fake_capability_path",
server_end.into_channel()
)
.is_ok());
assert!(client_end.rewind().await.is_err());
assert_eq!(CALL_COUNT.get(), 2);
}
#[fuchsia::test]
async fn open_child_component_exposed_dir_error() {
let realm_proxy = spawn_stream_handler(move |realm_request| async move {
match realm_request {
fcomponent::RealmRequest::OpenExposedDir {
child: _,
exposed_dir: _,
responder,
} => {
let _ = responder.send(&mut Err(fcomponent::Error::Internal));
}
_ => panic!("Realm handler received an unexpected request"),
}
})
.unwrap();
assert!(open_child_component_exposed_dir("", "", &realm_proxy).await.is_err());
}
#[fuchsia::test]
async fn destroy_child_parameters() {
let child_name = "test_child";
let child_collection = "test_collection";
let realm_proxy = spawn_stream_handler(move |realm_request| async move {
match realm_request {
fcomponent::RealmRequest::DestroyChild { child, responder } => {
assert_eq!(child.name, child_name);
assert_eq!(child.collection, Some(child_collection.to_string()));
let _ = responder.send(&mut Ok(()));
}
_ => panic!("Realm handler received an unexpected request"),
}
})
.unwrap();
assert!(destroy_child_component(child_name, child_collection, &realm_proxy).await.is_ok());
}
}