stun_agent/
lib.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! STUN Agent library for Rust.
//!
//! This crate provides a STUN I/O-free protocol implementation.
//! An I/O-free protocol implementation, often referred to as a
//! [`sans-IO`](`https://sans-io.readthedocs.io/index.html`) implementation, is a
//! network protocol implementation that contains no code for network I/O or
//! asynchronous flow control. This means the protocol implementation is agnostic
//! to the underlying networking stack and can be used in any environment that provides
//! the necessary network I/O and asynchronous flow control.
//!
//! These STUN agents are designed for use in a client-server architecture where the
//! client sends a request and the server responds.
//!
//! This sans-IO protocol implementation is defined entirely in terms of synchronous
//! functions returning synchronous results, without blocking or waiting for any form
//! of I/O. This makes it suitable for a wide range of environments, enhancing testing,
//! flexibility, correctness, re-usability and simplicity.
//!
//! This library currently provides support for writing STUN clients. Support for
//! writing servers is not yet implemented. The main element of this library is:
//! - [`StunClient`](`crate::StunClient`): The STUN client that sends STUN requests and indications to a STUN server.
#![deny(missing_docs)]

use std::{ops::Deref, slice::Iter, sync::Arc};

use stun_rs::MessageHeader;
use stun_rs::StunAttribute;
use stun_rs::MESSAGE_HEADER_SIZE;

mod client;
mod events;
mod fingerprint;
mod integrity;
mod lt_cred_mech;
mod message;
mod rtt;
mod st_cred_mech;
mod timeout;

pub use crate::client::RttConfig;
pub use crate::client::StunClient;
pub use crate::client::StunClienteBuilder;
pub use crate::client::TransportReliability;
pub use crate::events::StunTransactionError;
pub use crate::events::StuntClientEvent;
pub use crate::message::StunAttributes;

/// Describes the error that can occur during the STUN agent operation.
#[derive(Debug, PartialEq, Eq)]
pub enum StunAgentError {
    /// Indicates that the STUN agent has discarded the buffer
    Discarded,
    /// Indicates that the STUN agent has received an invalid STUN packet
    FingerPrintValidationFailed,
    /// Indicates that the STUN agent has ignored the operation
    Ignored,
    /// Indicates that the STUN agent has reached the maximum number of outstanding requests
    MaxOutstandingRequestsReached,
    /// Indicates that the STUN agent has received an invalid STUN packet
    StunCheckFailed,
    /// Indicates that the STUN agent has detected an internal error, and the [`String`] contains the error message
    InternalError(String),
}

/// Describes the kind of integrity protection that can be used.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Integrity {
    /// [`MessageInttegrity`](stun_rs::attributes::stun::MessageIntegrity) protection
    MessageIntegrity,
    /// [`MessageIntegritySha256`](stun_rs::attributes::stun::MessageIntegritySha256) protection
    MessageIntegritySha256,
}

/// Describes the kind of credential mechanism that can be used by the STUN agent.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CredentialMechanism {
    /// [Short-term credential mechanism](https://datatracker.ietf.org/doc/html/rfc8489#section-9.1)
    /// with the specified [`Integrity`] in case the agent knows from an external mechanism
    /// which message integrity algorithm is supported by both agents.
    ShortTerm(Option<Integrity>),
    /// [Long-term credential mechanism](https://datatracker.ietf.org/doc/html/rfc8489#section-9.2)
    LongTerm,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct StunPacketInternal {
    buffer: Vec<u8>,
    size: usize,
}

/// A chunk of bytes that represents a STUN packet that can be cloned.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StunPacket(Arc<StunPacketInternal>);

impl StunPacket {
    /// Creates a STUN packet from a vector which is filled up to `size` bytes.
    pub(crate) fn new(buffer: Vec<u8>, size: usize) -> Self {
        let internal = StunPacketInternal { buffer, size };
        StunPacket(Arc::new(internal))
    }
}

impl Deref for StunPacket {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.0.buffer[..self.0.size]
    }
}

impl AsRef<[u8]> for StunPacket {
    fn as_ref(&self) -> &[u8] {
        self
    }
}

/// A STUN packet decoder that can be used to decode a STUN packet.
/// The [`StunPacketDecoder`] is helpful when reading bytes from a stream oriented connection,
/// such as a `TCP` stream, or even when reading bytes from a datagram oriented connection, such as
/// a `UDP` socket when the STUN packet is fragmented.
///```rust
/// # use stun_agent::StunPacketDecoder;
///
/// //let buffer = vec![0; 1024];
/// //let mut decoder = StunPacketDecoder::new(buffer).expect("Failed to create decoder");
///```
#[derive(Debug)]
pub struct StunPacketDecoder {
    buffer: Vec<u8>,
    current_size: usize,
    expected_size: Option<usize>,
}

/// Describes the possible outcomes of the STUN packet decoding.
/// - If the STUN packet has been fully decoded, the method returns the decoded STUN packet
/// and the number of bytes consumed.
/// - If the STUN packet has not been fully decoded, the method returns the decoder and the
/// number of bytes still needed to complete the STUN packet, if known.
#[derive(Debug)]
pub enum StunPacketDecodedValue {
    /// Returns the decoded STUN packet and the number of bytes consumed from the input.
    Decoded((StunPacket, usize)),
    /// Returns the decoder and the number of bytes missing to complete the STUN packet if known.
    MoreBytesNeeded((StunPacketDecoder, Option<usize>)),
}

/// Describe the error type that can occur during the STUN packet decoding.
#[derive(Debug)]
pub enum StunPacketErrorType {
    /// The buffer is too small to hold the STUN packet.
    SmallBuffer,
    /// The buffer does not contain a valid STUN header.
    InvalidStunPacket,
}

/// Describes the error that can occur during the STUN packet decoding.
#[derive(Debug)]
pub struct StunPacketDecodedError {
    /// The type of error that occurred during the STUN packet decoding.
    pub error_type: StunPacketErrorType,
    /// The internal buffer filled with bytes.
    pub buffer: Vec<u8>,
    /// The size of the buffer that has been filled.
    pub size: usize,
    /// The number of bytes consumed from the input data.
    pub consumed: usize,
}

impl StunPacketDecoder {
    /// Creates a new STUN packet decoder using the provided buffer. The buffer must be
    /// at least 20 bytes long to accommodate the STUN message header. If the buffer is
    /// too small, an error is returned.
    pub fn new(buffer: Vec<u8>) -> Result<Self, StunPacketDecodedError> {
        if buffer.len() < MESSAGE_HEADER_SIZE {
            return Err(StunPacketDecodedError {
                error_type: StunPacketErrorType::SmallBuffer,
                buffer,
                size: 0,
                consumed: 0,
            });
        }
        Ok(StunPacketDecoder {
            buffer,
            current_size: 0,
            expected_size: None,
        })
    }

    /// Decodes the given data and returns the decoded STUN packet. This method takes the data
    /// read so far as an argument and returns one of the following outcomes:
    /// - If the STUN packet has been fully decoded, the method returns the decoded STUN packet
    /// and the number of bytes consumed.
    /// - If the STUN packet has not been fully decoded, the method returns the decoder and the
    /// number of bytes still needed to complete the STUN packet, if known.
    /// - If the buffer is too small or the header does not correspond to a STUN message, the
    /// method returns an error.
    /// Note: This method does not perform a full validation of the STUN message; it only checks
    /// the header. Integrity checks and other validations will be performed by the STUN agent.
    pub fn decode(mut self, data: &[u8]) -> Result<StunPacketDecodedValue, StunPacketDecodedError> {
        match self.expected_size {
            Some(size) => {
                // At this point we know that the buffer is big enough to hold the message,
                // so we do not need to check bounds.
                let first = self.current_size;
                let remaining = size - first;
                if data.len() >= remaining {
                    // Copy only up to the message length
                    self.buffer[first..size].copy_from_slice(&data[..remaining]);
                    let packet = StunPacket::new(self.buffer, size);
                    Ok(StunPacketDecodedValue::Decoded((packet, remaining)))
                } else {
                    // Copy all the data
                    self.buffer[first..first + data.len()].copy_from_slice(&data[..data.len()]);
                    self.current_size += data.len();
                    Ok(StunPacketDecodedValue::MoreBytesNeeded((
                        self,
                        Some(remaining - data.len()),
                    )))
                }
            }
            None => {
                let header_length = self.current_size + data.len();
                if header_length >= MESSAGE_HEADER_SIZE {
                    let first = self.current_size;
                    let remaining = MESSAGE_HEADER_SIZE - first;

                    // Write the STUN message header
                    self.buffer[first..first + remaining].copy_from_slice(&data[..remaining]);

                    // We can decode the header now
                    let slice: &[u8; MESSAGE_HEADER_SIZE] =
                        self.buffer[..MESSAGE_HEADER_SIZE].try_into().unwrap();
                    let Ok(header) = MessageHeader::try_from(slice) else {
                        return Err(StunPacketDecodedError {
                            error_type: StunPacketErrorType::InvalidStunPacket,
                            buffer: self.buffer,
                            size: MESSAGE_HEADER_SIZE,
                            consumed: remaining,
                        });
                    };
                    let msg_length = header.msg_length as usize;

                    // Check if the buffer provided is big enough to hold the message
                    if self.buffer.len() < msg_length + MESSAGE_HEADER_SIZE {
                        return Err(StunPacketDecodedError {
                            error_type: StunPacketErrorType::SmallBuffer,
                            buffer: self.buffer,
                            size: MESSAGE_HEADER_SIZE,
                            consumed: remaining,
                        });
                    }

                    self.expected_size = Some(msg_length + MESSAGE_HEADER_SIZE);

                    if data.len() >= msg_length + remaining {
                        // Copy only up to the message length
                        self.buffer[MESSAGE_HEADER_SIZE..MESSAGE_HEADER_SIZE + msg_length]
                            .copy_from_slice(&data[remaining..remaining + msg_length]);
                        let packet = StunPacket::new(self.buffer, msg_length + MESSAGE_HEADER_SIZE);
                        Ok(StunPacketDecodedValue::Decoded((
                            packet,
                            remaining + msg_length,
                        )))
                    } else {
                        // Copy all the remaining data
                        self.buffer
                            [MESSAGE_HEADER_SIZE..MESSAGE_HEADER_SIZE + data.len() - remaining]
                            .copy_from_slice(&data[remaining..data.len()]);
                        self.current_size += data.len();
                        let remaining = msg_length + MESSAGE_HEADER_SIZE - self.current_size;
                        Ok(StunPacketDecodedValue::MoreBytesNeeded((
                            self,
                            Some(remaining),
                        )))
                    }
                } else {
                    // The number of bytes is less than the header size, so we can safety copy all
                    // the data because the minimum size of the byte is 20 bytes.
                    let first = self.current_size;
                    let remaining = data.len();
                    self.buffer[first..first + remaining].copy_from_slice(&data[..remaining]);
                    self.current_size += data.len();

                    // We still don't know the message length
                    Ok(StunPacketDecodedValue::MoreBytesNeeded((self, None)))
                }
            }
        }
    }
}

#[derive(Debug)]
struct ProtectedAttributeIteratorObject<'a> {
    iter: Iter<'a, StunAttribute>,
    integrity: bool,
    integrity_sha256: bool,
    fingerprint: bool,
}

trait ProtectedAttributeIterator<'a> {
    fn protected_iter(&self) -> ProtectedAttributeIteratorObject<'a>;
}

impl<'a> ProtectedAttributeIterator<'a> for &'a [StunAttribute] {
    fn protected_iter(&self) -> ProtectedAttributeIteratorObject<'a> {
        ProtectedAttributeIteratorObject {
            iter: self.iter(),
            integrity: false,
            integrity_sha256: false,
            fingerprint: false,
        }
    }
}

impl<'a> Iterator for ProtectedAttributeIteratorObject<'a> {
    type Item = &'a StunAttribute;

    fn next(&mut self) -> Option<Self::Item> {
        for attr in &mut self.iter {
            if attr.is_message_integrity() {
                if self.integrity || self.integrity_sha256 || self.fingerprint {
                    continue;
                }
                self.integrity = true;
            } else if attr.is_message_integrity_sha256() {
                if self.integrity_sha256 || self.fingerprint {
                    continue;
                }
                self.integrity_sha256 = true;
            } else if attr.is_fingerprint() {
                if self.fingerprint {
                    continue;
                }
                self.fingerprint = true;
            } else if self.integrity || self.integrity_sha256 || self.fingerprint {
                continue;
            }
            return Some(attr);
        }
        None
    }
}

#[cfg(test)]
mod tests_stun_packet {
    use super::*;

    #[test]
    fn test_stun_packet() {
        let buffer = vec![0; 10];
        assert_eq!(buffer.len(), 10);

        // Create a stun packet that is only filled up to 5 bytes.
        let packet = StunPacket::new(buffer, 5);
        assert_eq!(packet.as_ref().len(), 5);

        let buffer = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let packet = StunPacket::new(buffer, 5);
        assert_eq!(packet.len(), 5);
        assert_eq!(packet.as_ref(), &[0, 1, 2, 3, 4]);
    }
}

#[cfg(test)]
mod tests_protected_iterator {
    use super::*;
    use stun_rs::{
        attributes::stun::{
            Fingerprint, MessageIntegrity, MessageIntegritySha256, Nonce, Realm, UserName,
        },
        methods::BINDING,
        Algorithm, AlgorithmId, HMACKey, MessageClass, StunMessageBuilder,
    };

    const USERNAME: &str = "test-username";
    const NONCE: &str = "test-nonce";
    const REALM: &str = "test-realm";
    const PASSWORD: &str = "test-password";

    #[test]
    fn test_protected_iterator() {
        let username = UserName::new(USERNAME).expect("Failed to create username");
        let nonce = Nonce::new(NONCE).expect("Failed to create nonce");
        let realm = Realm::new(REALM).expect("Failed to create realm");
        let algorithm = Algorithm::from(AlgorithmId::MD5);
        let key = HMACKey::new_long_term(&username, &realm, PASSWORD, algorithm)
            .expect("Failed to create HMACKey");
        let integrity = MessageIntegrity::new(key.clone());
        let integrity_sha256 = MessageIntegritySha256::new(key);

        let msg = StunMessageBuilder::new(BINDING, MessageClass::Request)
            .with_attribute(username)
            .with_attribute(nonce)
            .with_attribute(realm)
            .with_attribute(integrity)
            .with_attribute(integrity_sha256)
            .with_attribute(Fingerprint::default())
            .build();

        let mut iter = msg.attributes().protected_iter();
        let attr = iter.next().expect("Expected attribute UserName");
        assert!(attr.is_user_name());

        let attr = iter.next().expect("Expected attribute Nonce");
        assert!(attr.is_nonce());

        let attr = iter.next().expect("Expected attribute Realm");
        assert!(attr.is_realm());

        let attr = iter.next().expect("Expected attribute MessageIntegrity");
        assert!(attr.is_message_integrity());

        let attr = iter
            .next()
            .expect("Expected attribute MessageIntegritySha256");
        assert!(attr.is_message_integrity_sha256());

        let attr = iter.next().expect("Expected attribute FingerPrint");
        assert!(attr.is_fingerprint());

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_protected_iterator_only_message_integrity() {
        let username = UserName::new(USERNAME).expect("Failed to create username");
        let nonce = Nonce::new(NONCE).expect("Failed to create nonce");
        let realm = Realm::new(REALM).expect("Failed to create realm");
        let algorithm = Algorithm::from(AlgorithmId::MD5);
        let key = HMACKey::new_long_term(&username, &realm, PASSWORD, algorithm)
            .expect("Failed to create HMACKey");
        let integrity = MessageIntegrity::new(key.clone());
        let integrity_sha256 = MessageIntegritySha256::new(key);

        let msg = StunMessageBuilder::new(BINDING, MessageClass::Request)
            .with_attribute(integrity)
            .with_attribute(username)
            .with_attribute(nonce)
            .with_attribute(realm)
            .with_attribute(integrity_sha256)
            .build();

        let mut iter = msg.attributes().protected_iter();
        let attr = iter.next().expect("Expected attribute MessageIntegrity");
        assert!(attr.is_message_integrity());

        let attr = iter
            .next()
            .expect("Expected attribute MessageIntegritySha256");
        assert!(attr.is_message_integrity_sha256());

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_protected_iterator_skip_non_protected() {
        let username = UserName::new(USERNAME).expect("Failed to create username");
        let nonce = Nonce::new(NONCE).expect("Failed to create nonce");
        let realm = Realm::new(REALM).expect("Failed to create realm");
        let algorithm = Algorithm::from(AlgorithmId::MD5);
        let key = HMACKey::new_long_term(&username, &realm, PASSWORD, algorithm)
            .expect("Failed to create HMACKey");
        let integrity = MessageIntegrity::new(key.clone());
        let integrity_sha256 = MessageIntegritySha256::new(key);

        let msg = StunMessageBuilder::new(BINDING, MessageClass::Request)
            .with_attribute(username)
            .with_attribute(integrity)
            .with_attribute(nonce)
            .with_attribute(integrity_sha256)
            .with_attribute(realm)
            .with_attribute(Fingerprint::default())
            .build();

        let mut iter = msg.attributes().protected_iter();
        let attr = iter.next().expect("Expected attribute UserName");
        assert!(attr.is_user_name());

        let attr = iter.next().expect("Expected attribute MessageIntegrity");
        assert!(attr.is_message_integrity());

        let attr = iter
            .next()
            .expect("Expected attribute MessageIntegritySha256");
        assert!(attr.is_message_integrity_sha256());

        let attr = iter.next().expect("Expected attribute FingerPrint");
        assert!(attr.is_fingerprint());

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_protected_iterator_skip_message_integrity() {
        let username = UserName::new(USERNAME).expect("Failed to create username");
        let nonce = Nonce::new(NONCE).expect("Failed to create nonce");
        let realm = Realm::new(REALM).expect("Failed to create realm");
        let algorithm = Algorithm::from(AlgorithmId::MD5);
        let key = HMACKey::new_long_term(&username, &realm, PASSWORD, algorithm)
            .expect("Failed to create HMACKey");
        let integrity = MessageIntegrity::new(key.clone());
        let integrity_sha256 = MessageIntegritySha256::new(key);

        let msg = StunMessageBuilder::new(BINDING, MessageClass::Request)
            .with_attribute(username)
            .with_attribute(integrity_sha256)
            .with_attribute(nonce)
            .with_attribute(integrity)
            .with_attribute(realm)
            .with_attribute(Fingerprint::default())
            .build();

        let mut iter = msg.attributes().protected_iter();
        let attr = iter.next().expect("Expected attribute UserName");
        assert!(attr.is_user_name());

        let attr = iter
            .next()
            .expect("Expected attribute MessageIntegritySha256");
        assert!(attr.is_message_integrity_sha256());

        // MessageIntegrity can not go after MessageIntegritySha256, so it must be skipped
        let attr = iter.next().expect("Expected attribute FingerPrint");
        assert!(attr.is_fingerprint());

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_protected_iterator_skip_message_integrity_sha256() {
        let username = UserName::new(USERNAME).expect("Failed to create username");
        let nonce = Nonce::new(NONCE).expect("Failed to create nonce");
        let realm = Realm::new(REALM).expect("Failed to create realm");
        let algorithm = Algorithm::from(AlgorithmId::MD5);
        let key = HMACKey::new_long_term(&username, &realm, PASSWORD, algorithm)
            .expect("Failed to create HMACKey");
        let integrity = MessageIntegrity::new(key.clone());
        let integrity_sha256 = MessageIntegritySha256::new(key);

        let msg = StunMessageBuilder::new(BINDING, MessageClass::Request)
            .with_attribute(username)
            .with_attribute(Fingerprint::default())
            .with_attribute(nonce)
            .with_attribute(integrity_sha256)
            .with_attribute(integrity)
            .with_attribute(realm)
            .with_attribute(Fingerprint::default())
            .build();

        let mut iter = msg.attributes().protected_iter();
        let attr = iter.next().expect("Expected attribute UserName");
        assert!(attr.is_user_name());

        let attr = iter.next().expect("Expected attribute FingerPrint");
        assert!(attr.is_fingerprint());

        // All attributes after FingerPrint must be skipped
        assert!(iter.next().is_none());
    }

    #[test]
    fn test_protected_iterator_skip_duplicated_integrity_attrs() {
        let username = UserName::new(USERNAME).expect("Failed to create username");
        let realm = Realm::new(REALM).expect("Failed to create realm");
        let algorithm = Algorithm::from(AlgorithmId::MD5);
        let key = HMACKey::new_long_term(&username, realm, PASSWORD, algorithm)
            .expect("Failed to create HMACKey");
        let integrity = MessageIntegrity::new(key.clone());
        let integrity_sha256 = MessageIntegritySha256::new(key);

        let msg = StunMessageBuilder::new(BINDING, MessageClass::Request)
            .with_attribute(username)
            .with_attribute(integrity.clone())
            .with_attribute(integrity)
            .with_attribute(integrity_sha256.clone())
            .with_attribute(integrity_sha256)
            .with_attribute(Fingerprint::default())
            .with_attribute(Fingerprint::default())
            .build();

        let mut iter = msg.attributes().protected_iter();
        let attr = iter.next().expect("Expected attribute UserName");
        assert!(attr.is_user_name());

        let attr = iter.next().expect("Expected attribute MessageIntegrity");
        assert!(attr.is_message_integrity());

        let attr = iter
            .next()
            .expect("Expected attribute MessageIntegritySha256");
        assert!(attr.is_message_integrity_sha256());

        let attr = iter.next().expect("Expected attribute FingerPrint");
        assert!(attr.is_fingerprint());

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_protected_iterator_skip_corner_cases() {
        let username = UserName::new(USERNAME).expect("Failed to create username");

        let key = HMACKey::new_short_term("test-password").expect("Failed to create HMACKey");
        let integrity = MessageIntegrity::new(key.clone());
        let integrity_sha256 = MessageIntegritySha256::new(key);

        let msg = StunMessageBuilder::new(BINDING, MessageClass::Request)
            .with_attribute(integrity.clone())
            .with_attribute(integrity.clone())
            .with_attribute(integrity_sha256.clone())
            .with_attribute(integrity.clone())
            .with_attribute(integrity_sha256.clone())
            .with_attribute(Fingerprint::default())
            .with_attribute(integrity)
            .with_attribute(integrity_sha256)
            .with_attribute(Fingerprint::default())
            .with_attribute(username)
            .build();

        let mut iter = msg.attributes().protected_iter();
        let attr = iter.next().expect("Expected attribute MessageIntegrity");
        assert!(attr.is_message_integrity());
        let attr = iter
            .next()
            .expect("Expected attribute MessageIntegritySha256");
        assert!(attr.is_message_integrity_sha256());
        let attr = iter.next().expect("Expected attribute FingerPrint");
        assert!(attr.is_fingerprint());

        assert!(iter.next().is_none());
    }
}

#[cfg(test)]
mod test_stun_packet_decoder {
    use super::*;
    use stun_vectors::SAMPLE_IPV4_RESPONSE;

    #[test]
    fn test_stun_packet_decoder_small_parts() {
        let buffer = vec![0; 1024];
        let decoder = StunPacketDecoder::new(buffer).expect("Failed to create decoder");

        let mut index = 0;
        let data = &SAMPLE_IPV4_RESPONSE[index..10];
        let decoded = decoder.decode(data).expect("Failed to decode");
        let StunPacketDecodedValue::MoreBytesNeeded((decoder, remaining)) = decoded else {
            panic!("Expected more bytes needed");
        };
        // Message header is not processed, so we have no information about remaining bytes
        assert_eq!(remaining, None);
        assert_eq!(decoder.current_size, 10);
        assert!(decoder.expected_size.is_none());

        index = 10;
        let data = &SAMPLE_IPV4_RESPONSE[index..15];
        let decoded = decoder.decode(data).expect("Failed to decode");
        let StunPacketDecodedValue::MoreBytesNeeded((decoder, remaining)) = decoded else {
            panic!("Expected more bytes needed");
        };
        // Message header is not processed, so we have no information about remaining bytes
        assert_eq!(remaining, None);
        assert_eq!(decoder.current_size, 15);
        assert!(decoder.expected_size.is_none());
        assert_eq!(decoder.buffer[..15], SAMPLE_IPV4_RESPONSE[..15]);

        index = 15;
        let data = &SAMPLE_IPV4_RESPONSE[index..index + 5];
        let decoded = decoder.decode(data).expect("Failed to decode");
        let StunPacketDecodedValue::MoreBytesNeeded((decoder, remaining)) = decoded else {
            panic!("Expected more bytes needed");
        };
        // Header is processed and the msg length is 60 (0x3C)
        assert_eq!(remaining, Some(60));
        assert_eq!(decoder.current_size, 20);
        assert_eq!(decoder.expected_size, Some(60 + MESSAGE_HEADER_SIZE));
        assert_eq!(decoder.buffer[..20], SAMPLE_IPV4_RESPONSE[..20]);

        index = 20;
        let data = &SAMPLE_IPV4_RESPONSE[index..index + 30];
        let decoded = decoder.decode(data).expect("Failed to decode");
        let StunPacketDecodedValue::MoreBytesNeeded((decoder, remaining)) = decoded else {
            panic!("Expected more bytes needed");
        };
        assert_eq!(remaining, Some(30));
        assert_eq!(decoder.current_size, 50);
        assert_eq!(decoder.buffer[..50], SAMPLE_IPV4_RESPONSE[..50]);

        index = 50;
        let data = &SAMPLE_IPV4_RESPONSE[index..index + 29];
        let decoded = decoder.decode(data).expect("Failed to decode");
        let StunPacketDecodedValue::MoreBytesNeeded((decoder, remaining)) = decoded else {
            panic!("Expected more bytes needed");
        };
        assert_eq!(remaining, Some(1));
        assert_eq!(decoder.current_size, 79);
        assert_eq!(decoder.buffer[..79], SAMPLE_IPV4_RESPONSE[..79]);

        // Complete the byte remaining to complete the STUN packet
        index = 79;
        let data = &SAMPLE_IPV4_RESPONSE[index..index + 1];
        let decoded = decoder.decode(data).expect("Failed to decode");
        let StunPacketDecodedValue::Decoded((packet, consumed)) = decoded else {
            panic!("Stun packed not decoded");
        };
        assert_eq!(consumed, 1);
        assert_eq!(&SAMPLE_IPV4_RESPONSE, packet.as_ref());
    }

    #[test]
    fn test_stun_packet_decoder_one_step() {
        let buffer = vec![0; 1024];
        let decoder = StunPacketDecoder::new(buffer).expect("Failed to create decoder");

        // Read the buffer in one go
        let decoded = decoder
            .decode(&SAMPLE_IPV4_RESPONSE)
            .expect("Failed to decode");
        let StunPacketDecodedValue::Decoded((packet, consumed)) = decoded else {
            panic!("Stun packed not decoded");
        };
        assert_eq!(consumed, SAMPLE_IPV4_RESPONSE.len());
        assert_eq!(&SAMPLE_IPV4_RESPONSE, packet.as_ref());
    }

    #[test]
    fn test_stun_packet_decoder_two_step() {
        let buffer = vec![0; 1024];
        let decoder = StunPacketDecoder::new(buffer).expect("Failed to create decoder");

        let data = &SAMPLE_IPV4_RESPONSE[..15];
        let decoded = decoder.decode(data).expect("Failed to decode");
        let StunPacketDecodedValue::MoreBytesNeeded((decoder, remaining)) = decoded else {
            panic!("Expected more bytes needed");
        };
        // Message header is not processed, so we have no information about remaining bytes
        assert_eq!(remaining, None);
        assert_eq!(decoder.current_size, 15);
        assert!(decoder.expected_size.is_none());

        // Read the rest of the packet
        let data = &SAMPLE_IPV4_RESPONSE[15..];
        let decoded = decoder.decode(data).expect("Failed to decode");
        let StunPacketDecodedValue::Decoded((packet, consumed)) = decoded else {
            panic!("Stun packed not decoded");
        };
        assert_eq!(consumed, data.len());
        assert_eq!(&SAMPLE_IPV4_RESPONSE, packet.as_ref());
    }

    #[test]
    fn test_stun_packet_decoder_byte_by_byte() {
        let buffer = vec![0; 1024];
        let mut decoder = StunPacketDecoder::new(buffer).expect("Failed to create decoder");

        let total = SAMPLE_IPV4_RESPONSE.len();
        for index in 0..total {
            let data = &SAMPLE_IPV4_RESPONSE[index..index + 1];
            let decoded = decoder.decode(data).expect("Failed to decode");
            if index < total - 1 {
                let StunPacketDecodedValue::MoreBytesNeeded((deco, remaining)) = decoded else {
                    panic!("Expected more bytes needed");
                };
                if index >= MESSAGE_HEADER_SIZE - 1 {
                    assert_eq!(remaining, Some(total - 1 - index));
                } else {
                    assert_eq!(remaining, None);
                }
                decoder = deco;
            } else {
                let StunPacketDecodedValue::Decoded((packet, consumed)) = decoded else {
                    panic!("Stun packed not decoded");
                };
                assert_eq!(consumed, 1);
                assert_eq!(&SAMPLE_IPV4_RESPONSE, packet.as_ref());
                break;
            }
        }
    }

    #[test]
    fn test_stun_packet_decoder_small_buffer() {
        let buffer = vec![0; 10];
        let error = StunPacketDecoder::new(buffer).expect_err("Expected small buffer error");
        let StunPacketErrorType::SmallBuffer = error.error_type else {
            panic!("Expected small buffer error");
        };

        let buffer = vec![0; 50];
        let decoder = StunPacketDecoder::new(buffer).expect("Failed to create decoder");

        let result = decoder
            .decode(&SAMPLE_IPV4_RESPONSE[..10])
            .expect("Failed to decode");
        // We could not read the whole header, so it won't fail
        let StunPacketDecodedValue::MoreBytesNeeded((decoder, None)) = result else {
            panic!("Expected more bytes needed");
        };

        let error = decoder
            .decode(&SAMPLE_IPV4_RESPONSE[10..])
            .expect_err("Expected error");
        // The header is read and the buffer is too small to hold the whole message
        let StunPacketErrorType::SmallBuffer = error.error_type else {
            panic!("Expected small buffer error");
        };

        // Test the same scenario but trying to decode the buffer in one go
        let buffer = vec![0; 50];
        let decoder = StunPacketDecoder::new(buffer).expect("Failed to create decoder");
        let error = decoder
            .decode(&SAMPLE_IPV4_RESPONSE)
            .expect_err("Expected error");
        let StunPacketErrorType::SmallBuffer = error.error_type else {
            panic!("Expected small buffer error");
        };
    }

    #[test]
    fn test_stun_packet_decoder_invalid_stun_packet() {
        let buffer = vec![0; 1024];
        let decoder = StunPacketDecoder::new(buffer).expect("Failed to create decoder");

        let data = vec![0; 1024];
        let error = decoder.decode(&data).expect_err("Expected error");
        let StunPacketErrorType::InvalidStunPacket = error.error_type else {
            panic!("Expected invalid STUN packet error");
        };
    }
}