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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::attributes::{stunt_attribute, DecodeAttributeValue, EncodeAttributeValue};
use crate::context::{AttributeDecoderContext, AttributeEncoderContext};
use crate::{Decode, Encode, StunError, StunErrorType};
use std::convert::TryFrom;
const SOFTWARE: u16 = 0x8022;
const MAX_ENCODED_SIZE: usize = 509;
const MAX_DECODED_SIZE: usize = 763;
#[derive(Debug, PartialEq, Clone, Hash, Eq, PartialOrd, Ord)]
pub struct Software(String);
impl Software {
pub fn new<S>(value: S) -> Result<Self, StunError>
where
S: Into<String>,
{
let value: String = value.into();
let value_len = value.len();
(value.len() <= MAX_ENCODED_SIZE)
.then_some(Self(value))
.ok_or_else(|| {
StunError::new(
StunErrorType::ValueTooLong,
format!(
"Value length {} > max. encoded size {}",
value_len, MAX_ENCODED_SIZE
),
)
})
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl TryFrom<&str> for Software {
type Error = StunError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Software::new(value)
}
}
impl TryFrom<&String> for Software {
type Error = StunError;
fn try_from(value: &String) -> Result<Self, Self::Error> {
Software::new(value)
}
}
impl TryFrom<String> for Software {
type Error = StunError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Software::new(value)
}
}
impl PartialEq<&str> for Software {
fn eq(&self, other: &&str) -> bool {
self.as_str().eq(*other)
}
}
impl PartialEq<Software> for &str {
fn eq(&self, other: &Software) -> bool {
other.as_str().eq(*self)
}
}
impl PartialEq<str> for Software {
fn eq(&self, other: &str) -> bool {
self.as_str().eq(other)
}
}
impl PartialEq<String> for Software {
fn eq(&self, other: &String) -> bool {
other.eq(self.as_str())
}
}
impl PartialEq<Software> for String {
fn eq(&self, other: &Software) -> bool {
self.eq(other.as_str())
}
}
impl AsRef<str> for Software {
fn as_ref(&self) -> &str {
&self.0
}
}
impl AsRef<String> for Software {
fn as_ref(&self) -> &String {
&self.0
}
}
impl DecodeAttributeValue for Software {
fn decode(ctx: AttributeDecoderContext) -> Result<(Self, usize), StunError> {
let raw_value = ctx.raw_value();
if raw_value.len() > MAX_DECODED_SIZE {
return Err(StunError::new(
StunErrorType::ValueTooLong,
format!(
"Value length {} > max. decoded size {}",
raw_value.len(),
MAX_DECODED_SIZE
),
));
}
let (val, size) = <&'_ str as Decode<'_>>::decode(ctx.raw_value())?;
Ok((Self(val.to_string()), size))
}
}
impl EncodeAttributeValue for Software {
fn encode(&self, mut ctx: AttributeEncoderContext) -> Result<usize, StunError> {
if self.as_str().len() > MAX_ENCODED_SIZE {
return Err(StunError::new(
StunErrorType::ValueTooLong,
format!(
"Value length {} > max. encoded size {}",
self.as_str().len(),
MAX_ENCODED_SIZE
),
));
}
self.0.as_str().encode(ctx.raw_value_mut())
}
}
impl crate::attributes::AsVerifiable for Software {}
stunt_attribute!(Software, SOFTWARE);
#[cfg(test)]
mod tests {
use super::*;
use crate::StunAttribute;
#[test]
fn constructor() {
let name = String::from("Test Software v1.0");
let attr_1 = Software::try_from(&name).expect("Can not create Software attribute");
let attr_2 = Software::new(&name).expect("Can not create Software attribute");
let attr_3 = Software::try_from(name.as_str()).expect("Can not create Software attribute");
let attr_4 = Software::try_from(name.clone()).expect("Can not create Software attribute");
assert_eq!(attr_1, name);
assert_eq!(name, attr_1);
assert_eq!(name, attr_3);
assert_eq!(name, attr_4);
assert_eq!(attr_1, "Test Software v1.0");
assert_eq!("Test Software v1.0", attr_1);
assert_eq!(attr_1, attr_2);
let value: &String = attr_1.as_ref();
assert!(name.eq(value));
let value: &str = attr_1.as_ref();
assert!(name.eq(value));
let value = "x".repeat(MAX_ENCODED_SIZE);
let _result = Software::new(value.as_str()).expect("Can not create a Sofware attribute");
let value = "x".repeat(MAX_ENCODED_SIZE + 1);
let result = Software::new(value);
assert_eq!(
result.expect_err("Error expected"),
StunErrorType::ValueTooLong
);
}
#[test]
fn decode_software_value() {
let dummy_msg = [];
let value = "example";
let ctx = AttributeDecoderContext::new(None, &dummy_msg, value.as_bytes());
let (software, size) = Software::decode(ctx).expect("Can not decode Software");
assert_eq!(size, 7);
assert_eq!(software.as_str(), "example");
let value = "x".repeat(MAX_DECODED_SIZE);
let ctx = AttributeDecoderContext::new(None, &dummy_msg, value.as_bytes());
let (_nonce, size) = Software::decode(ctx).expect("Can not decode Software");
assert_eq!(size, MAX_DECODED_SIZE);
let value = "x".repeat(MAX_DECODED_SIZE + 1);
let ctx = AttributeDecoderContext::new(None, &dummy_msg, value.as_bytes());
assert_eq!(
Software::decode(ctx).expect_err("Error expected"),
StunErrorType::ValueTooLong
);
}
#[test]
fn encode_software_value() {
let dummy_msg: [u8; 0] = [0x0; 0];
let software =
Software::try_from("test software").expect("Can not create a Sofware attribute");
let mut buffer: [u8; 13] = [0x0; 13];
let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
let result = software.encode(ctx);
assert_eq!(result, Ok(13));
let mut buffer: [u8; MAX_ENCODED_SIZE] = [0x0; MAX_ENCODED_SIZE];
let software = Software::try_from("x".repeat(MAX_ENCODED_SIZE))
.expect("Can not create a Sofware attribute");
let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
let result = software.encode(ctx);
assert_eq!(result, Ok(MAX_ENCODED_SIZE));
let mut buffer: [u8; 12] = [0x0; 12];
let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
let result = software.encode(ctx);
assert_eq!(
result.expect_err("Error expected"),
StunErrorType::SmallBuffer
);
let mut buffer: [u8; MAX_ENCODED_SIZE + 1] = [0x0; MAX_ENCODED_SIZE + 1];
let software = Software("x".repeat(MAX_ENCODED_SIZE + 1));
let ctx = AttributeEncoderContext::new(None, &dummy_msg, &mut buffer);
let result = software.encode(ctx);
assert_eq!(
result.expect_err("Error expected"),
StunErrorType::ValueTooLong
);
}
#[test]
fn software_stunt_attribute() {
let attr = StunAttribute::Software(
Software::new("test").expect("Can not create Software attribute"),
);
assert!(attr.is_software());
assert!(attr.as_software().is_ok());
assert!(attr.as_unknown().is_err());
let dbg_fmt = format!("{:?}", attr);
assert_eq!("Software(Software(\"test\"))", dbg_fmt);
}
}