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
// Copyright (C) 2020 Matthew Waters <matthew@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::convert::TryFrom;

use crate::message::{StunParseError, StunWriteError};

use super::{Attribute, AttributeType, RawAttribute};

/// The Software [`Attribute`]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Software {
    software: String,
}
impl Attribute for Software {
    const TYPE: AttributeType = AttributeType(0x8022);

    fn length(&self) -> u16 {
        self.software.len() as u16
    }
}
impl<'a> From<&'a Software> for RawAttribute<'a> {
    fn from(value: &'a Software) -> RawAttribute<'a> {
        RawAttribute::new(Software::TYPE, value.software.as_bytes())
    }
}
impl<'a> TryFrom<&RawAttribute<'a>> for Software {
    type Error = StunParseError;

    fn try_from(raw: &RawAttribute) -> Result<Self, Self::Error> {
        raw.check_type_and_len(Self::TYPE, ..=763)?;
        Ok(Self {
            software: std::str::from_utf8(&raw.value)
                .map_err(|_| StunParseError::InvalidAttributeData)?
                .to_owned(),
        })
    }
}

impl Software {
    /// Create a new unknown attributes [`Attribute`]
    ///
    /// # Errors
    ///
    /// If the length of the provided string is too long for the [`Attribute`]
    ///
    /// # Examples
    ///
    /// ```
    /// # use stun_types::attribute::*;
    /// let software = Software::new("stun-types 0.1").unwrap();
    /// assert_eq!(software.software(), "stun-types 0.1");
    /// ```
    pub fn new(software: &str) -> Result<Self, StunWriteError> {
        // TODO: should only allow 128 characters
        if software.len() > 763 {
            return Err(StunWriteError::TooLarge {
                expected: 763,
                actual: software.len(),
            });
        }
        Ok(Self {
            software: software.to_owned(),
        })
    }

    /// The value of the software field
    ///
    /// # Examples
    ///
    /// ```
    /// # use stun_types::attribute::*;
    /// let software = Software::new("stun-types 0.1").unwrap();
    /// assert_eq!(software.software(), "stun-types 0.1");
    /// ```
    pub fn software(&self) -> &str {
        &self.software
    }
}

impl std::fmt::Display for Software {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: '{}'", Software::TYPE, self.software)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use byteorder::{BigEndian, ByteOrder};
    use tracing::trace;

    #[test]
    fn software() {
        let _log = crate::tests::test_init_log();
        let software = Software::new("software").unwrap();
        trace!("{software}");
        assert_eq!(software.software(), "software");
        assert_eq!(software.length() as usize, "software".len());
        let raw = RawAttribute::from(&software);
        trace!("{raw}");
        assert_eq!(raw.get_type(), Software::TYPE);
        let software2 = Software::try_from(&raw).unwrap();
        assert_eq!(software2.software(), "software");
        // provide incorrectly typed data
        let mut data: Vec<_> = raw.into();
        BigEndian::write_u16(&mut data[0..2], 0);
        assert!(matches!(
            Software::try_from(&RawAttribute::from_bytes(data.as_ref()).unwrap()),
            Err(StunParseError::WrongAttributeImplementation)
        ));
    }

    #[test]
    fn software_not_utf8() {
        let _log = crate::tests::test_init_log();
        let attr = Software::new("software").unwrap();
        let raw = RawAttribute::from(&attr);
        let mut data = raw.to_bytes();
        data[6] = 0x88;
        assert!(matches!(
            Software::try_from(&RawAttribute::from_bytes(data.as_ref()).unwrap()),
            Err(StunParseError::InvalidAttributeData)
        ));
    }

    #[test]
    fn software_new_too_large() {
        let _log = crate::tests::test_init_log();
        let mut large = String::new();
        for _i in 0..95 {
            large.push_str("abcdefgh");
        }
        large.push_str("abcd");
        assert!(matches!(
            Software::new(&large),
            Err(StunWriteError::TooLarge {
                expected: 763,
                actual: 764
            })
        ));
    }
}