hardware_address/
infini_band.rs

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
const INFINI_BAND_ADDRESS_SIZE: usize = 20;

addr_ty!(
  /// Represents a physical 20-octet InfiniBand format address.
  #[derive(Eq, PartialEq, Ord, PartialOrd, Hash)]
  InfiniBandAddr[INFINI_BAND_ADDRESS_SIZE]
);

#[cfg(test)]
mod tests {
  use super::*;
  use crate::TestCase;

  use std::{string::ToString, vec, vec::Vec};

  fn test_cases() -> Vec<TestCase<INFINI_BAND_ADDRESS_SIZE>> {
    vec![
      // RFC 4391, Section 9.1.1
      TestCase {
        input: "00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01",
        output: Some(vec![
          0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x5e,
          0x10, 0x00, 0x00, 0x00, 0x01,
        ]),
        err: None,
      },
      TestCase {
        input: "00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01",
        output: Some(vec![
          0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x5e,
          0x10, 0x00, 0x00, 0x00, 0x01,
        ]),
        err: None,
      },
      TestCase {
        input: "0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001",
        output: Some(vec![
          0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x5e,
          0x10, 0x00, 0x00, 0x00, 0x01,
        ]),
        err: None,
      },
    ]
  }

  #[test]
  fn parse() {
    let cases = test_cases();
    for (i, test) in cases.iter().enumerate() {
      let result = InfiniBandAddr::try_from(test.input);

      match (result, &test.output) {
        (Ok(out), Some(expected)) => {
          assert_eq!(
            out.as_bytes(),
            expected.as_slice(),
            "Test case {}: InfiniBandAddr::parse({}) output mismatch",
            i,
            test.input
          );

          // Test round-trip if this was a valid case
          if test.err.is_none() {
            let formatted = out.to_string();
            let round_trip = InfiniBandAddr::try_from(formatted.as_str());
            assert!(
              round_trip.is_ok(),
              "Test case {}: Round-trip parse failed for {}",
              i,
              formatted
            );
            assert_eq!(
              round_trip.unwrap(),
              out,
              "Test case {}: Round-trip value mismatch",
              i
            );
          }
        }
        (Err(err), None) => {
          assert_eq!(
            Some(&err),
            test.err.as_ref(),
            "Test case {}: Expected error containing '{:?}', got '{:?}'",
            i,
            test.err,
            err
          );
        }
        (Ok(out), None) => {
          panic!(
            "Test case {}: Expected error '{:?}', got success: {:?}",
            i, test.err, out
          );
        }
        (Err(err), Some(expected)) => {
          panic!(
            "Test case {}: Expected {:?}, got error: {:?}",
            i, expected, err
          );
        }
      }
    }
  }

  #[test]
  fn formatted() {
    let addr =
      InfiniBandAddr::try_from("00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01")
        .unwrap();
    assert_eq!(
      addr.to_string(),
      "00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01"
    );

    let dot = addr.to_dot_seperated_array();
    let dot_str = core::str::from_utf8(&dot).unwrap();
    assert_eq!(dot_str, "0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001");

    let dashed = addr.to_hyphen_seperated_array();
    let dashed_str = core::str::from_utf8(&dashed).unwrap();
    assert_eq!(
      dashed_str,
      "00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01"
    );
  }

  #[cfg(feature = "serde")]
  #[test]
  fn serde_human_readable() {
    let addr =
      InfiniBandAddr::try_from("00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01")
        .unwrap();
    let json = serde_json::to_string(&addr).unwrap();
    assert_eq!(
      json,
      "\"00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01\""
    );

    let addr2: InfiniBandAddr = serde_json::from_str(&json).unwrap();
    assert_eq!(addr, addr2);
  }

  #[cfg(feature = "serde")]
  #[test]
  fn serde_human_unreadable() {
    let addr =
      InfiniBandAddr::try_from("00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01")
        .unwrap();
    let encoded = bincode::serialize(&addr).unwrap();
    assert_eq!(
      encoded,
      [
        0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x5e,
        0x10, 0x00, 0x00, 0x00, 0x01,
      ]
    );
    assert_eq!(addr.octets(), encoded.as_slice());

    let addr2: InfiniBandAddr = bincode::deserialize(&encoded).unwrap();
    assert_eq!(addr, addr2);
    let addr3 = InfiniBandAddr::new([
      0x00, 0x00, 0x00, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x5e,
      0x10, 0x00, 0x00, 0x00, 0x01,
    ]);
    assert_eq!(addr, addr3);
    println!("{:?}", addr);
  }
}