• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 pub mod protocol;
6 pub mod vsock;
7 
8 pub(crate) use protocol::*;
9 use serde::Deserialize;
10 use serde::Serialize;
11 use serde_keyvalue::FromKeyValues;
12 pub use vsock::Vsock;
13 pub use vsock::VsockError;
14 
15 #[derive(Debug, Deserialize, Serialize, PartialEq, Eq, FromKeyValues)]
16 #[serde(deny_unknown_fields)]
17 // Configuration for a Vsock device.
18 pub struct VsockConfig {
19     /// CID to be used for this vsock device.
20     pub cid: u64,
21 }
22 
23 impl VsockConfig {
24     /// Create a new vsock configuration.
new(cid: u64) -> Self25     pub fn new(cid: u64) -> Self {
26         Self { cid }
27     }
28 }
29 
30 #[cfg(test)]
31 mod tests {
32     use serde_keyvalue::from_key_values;
33     use serde_keyvalue::ErrorKind;
34     use serde_keyvalue::ParseError;
35 
36     use super::*;
37 
from_vsock_arg(options: &str) -> Result<VsockConfig, ParseError>38     fn from_vsock_arg(options: &str) -> Result<VsockConfig, ParseError> {
39         from_key_values(options)
40     }
41 
42     #[test]
params_from_key_values()43     fn params_from_key_values() {
44         // Default device
45         assert_eq!(from_vsock_arg("cid=56").unwrap(), VsockConfig { cid: 56 });
46 
47         // No argument
48         assert_eq!(
49             from_vsock_arg("").unwrap_err(),
50             ParseError {
51                 kind: ErrorKind::SerdeError("missing field `cid`".into()),
52                 pos: 0
53             }
54         );
55 
56         // Cid passed twice
57         assert_eq!(
58             from_vsock_arg("cid=42,cid=56").unwrap_err(),
59             ParseError {
60                 kind: ErrorKind::SerdeError("duplicate field `cid`".into()),
61                 pos: 0,
62             }
63         );
64 
65         // Invalid argument
66         assert_eq!(
67             from_vsock_arg("invalid=foo").unwrap_err(),
68             ParseError {
69                 kind: ErrorKind::SerdeError("unknown field `invalid`, expected `cid`".into()),
70                 pos: 0,
71             }
72         );
73     }
74 }
75