Skip to content

Commit fa9aaef

Browse files
alexandruagandreeaflorescu
authored andcommitted
net_util: added ser/de support for MacAddr
Implemented the Serialize and Deserialize traits for struct MacAddr. Added checks to ensure we only accept MAC address components which contain two digits. Also, added some unit tests for the new functionality. Signed-off-by: Alexandru Agache <[email protected]>
1 parent 9f85b08 commit fa9aaef

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

net_util/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ authors = ["The Chromium OS Authors"]
55

66
[dependencies]
77
libc = ">=0.2.39"
8+
serde = "=1.0.27"
89

910
net_sys = { path = "../net_sys" }
1011
sys_util = { path = "../sys_util" }
1112

1213
[dev-dependencies]
1314
lazy_static = "=1.0.0"
1415
pnet = "=0.21.0"
16+
serde_json = "=1.0.9"

net_util/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
extern crate lazy_static;
1010
extern crate libc;
1111
extern crate net_sys;
12+
extern crate serde;
1213
extern crate sys_util;
1314

1415
mod mac;

net_util/src/mac.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use std::result::Result;
22

3+
use serde::de::{Deserialize, Deserializer, Error};
4+
use serde::ser::{Serialize, Serializer};
5+
36
pub const MAC_ADDR_LEN: usize = 6;
47

58
#[derive(Clone, Debug, PartialEq)]
@@ -21,6 +24,9 @@ impl MacAddr {
2124
}
2225

2326
for i in 0..MAC_ADDR_LEN {
27+
if v[i].len() != 2 {
28+
return Err(s.as_ref());
29+
}
2430
bytes[i] = u8::from_str_radix(v[i], 16).map_err(|_| s.as_ref())?;
2531
}
2632

@@ -40,27 +46,62 @@ impl MacAddr {
4046
}
4147
}
4248

49+
impl Serialize for MacAddr {
50+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51+
where
52+
S: Serializer,
53+
{
54+
self.to_string().serialize(serializer)
55+
}
56+
}
57+
58+
impl<'de> Deserialize<'de> for MacAddr {
59+
fn deserialize<D>(deserializer: D) -> Result<MacAddr, D::Error>
60+
where
61+
D: Deserializer<'de>,
62+
{
63+
let s = String::deserialize(deserializer)?;
64+
MacAddr::parse_str(&s).map_err(|_| D::Error::custom("The provided MAC address is invalid."))
65+
}
66+
}
67+
4368
#[cfg(test)]
4469
mod tests {
70+
extern crate serde_json;
71+
4572
use super::*;
4673

4774
#[test]
4875
fn test_mac_addr() {
4976
// too long
5077
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:aa:aa").is_err());
78+
5179
// invalid hex
5280
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:ax").is_err());
5381

82+
// single digit mac address component should be invalid
83+
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:b").is_err());
84+
85+
// components with more than two digits should also be invalid
86+
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:bbb").is_err());
87+
5488
let mac = MacAddr::parse_str("12:34:56:78:9a:BC").unwrap();
5589

5690
println!("parsed MAC address: {}", mac.to_string());
5791

5892
let bytes = mac.get_bytes();
59-
assert_eq!(bytes[0], 0x12);
60-
assert_eq!(bytes[1], 0x34);
61-
assert_eq!(bytes[2], 0x56);
62-
assert_eq!(bytes[3], 0x78);
63-
assert_eq!(bytes[4], 0x9a);
64-
assert_eq!(bytes[5], 0xbc);
93+
assert_eq!(bytes, [0x12u8, 0x34, 0x56, 0x78, 0x9a, 0xbc]);
94+
}
95+
96+
#[test]
97+
fn test_mac_addr_serialization_and_deserialization() {
98+
let mac: MacAddr =
99+
serde_json::from_str("\"12:34:56:78:9a:bc\"").expect("MacAddr deserialization failed.");
100+
101+
let bytes = mac.get_bytes();
102+
assert_eq!(bytes, [0x12u8, 0x34, 0x56, 0x78, 0x9a, 0xbc]);
103+
104+
let s = serde_json::to_string(&mac).expect("MacAddr serialization failed.");
105+
assert_eq!(s, "\"12:34:56:78:9a:bc\"");
65106
}
66107
}

0 commit comments

Comments
 (0)