wlan_common/ie/
rates_writer.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::append::Append;
6use crate::error::FrameWriteError;
7use crate::ie::{
8    write_extended_supported_rates, write_supported_rates, EXTENDED_SUPPORTED_RATES_MAX_LEN,
9    SUPPORTED_RATES_MAX_LEN,
10};
11use zerocopy::SplitByteSlice;
12
13pub struct RatesWriter<S>(S);
14
15impl<S: SplitByteSlice> RatesWriter<S> {
16    pub fn try_new(rates: S) -> Result<RatesWriter<S>, FrameWriteError> {
17        if rates.len() == 0 {
18            Err(FrameWriteError::InvalidData(format!("no rates to write")))
19        } else if rates.len() > SUPPORTED_RATES_MAX_LEN + EXTENDED_SUPPORTED_RATES_MAX_LEN {
20            Err(FrameWriteError::InvalidData(format!("rates will not fit in elements")))
21        } else {
22            Ok(RatesWriter(rates))
23        }
24    }
25
26    pub fn write_supported_rates<B: Append>(&self, buf: &mut B) {
27        let num_rates = std::cmp::min(self.0.len(), SUPPORTED_RATES_MAX_LEN);
28        // safe to unwrap because we truncated the slice
29        write_supported_rates(&mut *buf, &self.0[..num_rates]).unwrap();
30    }
31
32    pub fn write_extended_supported_rates<B: Append>(&self, buf: &mut B) {
33        if self.0.len() > SUPPORTED_RATES_MAX_LEN {
34            // safe to unwrap because it is guaranteed to fit.
35            write_extended_supported_rates(&mut *buf, &self.0[SUPPORTED_RATES_MAX_LEN..]).unwrap();
36        }
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn empty_rates_error() {
46        let rates = [];
47        assert!(RatesWriter::try_new(&rates[..]).is_err());
48    }
49
50    #[test]
51    fn too_many_rates_error() {
52        let rates = [0; 1 + SUPPORTED_RATES_MAX_LEN + EXTENDED_SUPPORTED_RATES_MAX_LEN];
53        assert!(RatesWriter::try_new(&rates[..]).is_err());
54    }
55
56    #[test]
57    fn max_num_of_rates_ok() {
58        let rates = [42; SUPPORTED_RATES_MAX_LEN + EXTENDED_SUPPORTED_RATES_MAX_LEN];
59        assert!(RatesWriter::try_new(&rates[..]).is_ok());
60    }
61
62    #[test]
63    fn rates_fit_in_supp_rates() {
64        let rates: Vec<u8> = (0..SUPPORTED_RATES_MAX_LEN as u8).collect();
65        let rates_writer = RatesWriter::try_new(&rates[..]).expect("Should be valid RatesWriter");
66        let mut buf = vec![];
67        rates_writer.write_supported_rates(&mut buf);
68        rates_writer.write_extended_supported_rates(&mut buf);
69        assert_eq!(
70            &buf[..],
71            &[
72                1, 8, // ID and length
73                0, 1, 2, 3, 4, 5, 6, 7, // actual rates
74            ]
75        );
76    }
77
78    #[test]
79    fn rates_span_two_elements() {
80        let rates: Vec<u8> = (0..(1 + SUPPORTED_RATES_MAX_LEN as u8)).collect();
81        let rates_writer = RatesWriter::try_new(&rates[..]).expect("Should be valid RatesWriter");
82        let mut buf = vec![];
83        rates_writer.write_supported_rates(&mut buf);
84        rates_writer.write_extended_supported_rates(&mut buf);
85        assert_eq!(
86            &buf[..],
87            &[
88                1, 8, // SupportedRates ID and length
89                0, 1, 2, 3, 4, 5, 6, 7, // supp_rates
90                50, 1, // ExtendedSupportedRates ID and length
91                8, // ext_supp_rates
92            ]
93        )
94    }
95}