rsiprtp 0.4.1

Modular SIP/RTP communications stack for Rust with Sans-IO state machines
Documentation
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
//! Call transfer functionality (REFER method, RFC 3515).
//!
//! Implements both blind and attended call transfers.
//!
//! # Overview
//!
//! Call transfer uses the SIP REFER method:
//!
//! - **Blind Transfer**: Transfer without consulting the target first
//!   - A calls B, then REFERs B to C
//!   - B hangs up with A and calls C
//!
//! - **Attended Transfer**: Transfer after consulting the target (Replaces)
//!   - A calls B, puts B on hold
//!   - A calls C (consultation call)
//!   - A REFERs B to C with Replaces header
//!   - B replaces A's call to C
//!
//! # REFER Headers
//!
//! ```text
//! REFER sip:bob@example.com SIP/2.0
//! Refer-To: <sip:carol@example.com>
//! Referred-By: <sip:alice@example.com>
//! ```
//!
//! # NOTIFY for Transfer Progress
//!
//! The transferee sends NOTIFY messages to report progress:
//! ```text
//! NOTIFY sip:alice@example.com SIP/2.0
//! Event: refer
//! Subscription-State: active
//! Content-Type: message/sipfrag
//!
//! SIP/2.0 180 Ringing
//! ```

use std::collections::HashMap;
use thiserror::Error;

/// Transfer-related errors.
#[derive(Debug, Error)]
pub enum TransferError {
    /// Call not found.
    #[error("call not found: {0}")]
    CallNotFound(String),

    /// Invalid state for transfer.
    #[error("invalid state for transfer: {0}")]
    InvalidState(String),

    /// Transfer rejected.
    #[error("transfer rejected: {code}")]
    Rejected {
        /// SIP response status code from the rejecting party.
        code: u16,
    },

    /// Transfer failed.
    #[error("transfer failed: {0}")]
    Failed(String),

    /// Invalid Refer-To URI.
    #[error("invalid Refer-To URI: {0}")]
    InvalidReferTo(String),
}

/// Type of call transfer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferType {
    /// Blind transfer (no consultation).
    Blind,
    /// Attended transfer (with consultation call).
    Attended,
}

/// Transfer state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferState {
    /// No transfer in progress.
    None,
    /// REFER sent, waiting for response.
    Pending,
    /// REFER accepted (202), waiting for progress.
    Accepted,
    /// Transfer in progress (got NOTIFY).
    InProgress,
    /// Transfer completed successfully.
    Completed,
    /// Transfer failed.
    Failed,
}

/// Transfer direction (who initiated).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferRole {
    /// We are the transferor (initiated the transfer).
    Transferor,
    /// We are the transferee (being transferred).
    Transferee,
    /// We are the transfer target (receiving the transferred call).
    Target,
}

/// Refer-To header value.
#[derive(Debug, Clone)]
pub struct ReferTo {
    /// Target URI.
    pub uri: String,
    /// Optional Replaces header value for attended transfer.
    pub replaces: Option<ReplacesHeader>,
}

impl ReferTo {
    /// Create a simple Refer-To for blind transfer.
    pub fn blind(uri: &str) -> Self {
        Self {
            uri: uri.to_string(),
            replaces: None,
        }
    }

    /// Create a Refer-To with Replaces for attended transfer.
    pub fn attended(uri: &str, replaces: ReplacesHeader) -> Self {
        Self {
            uri: uri.to_string(),
            replaces: Some(replaces),
        }
    }

    /// Parse from header value.
    ///
    /// Format: `<sip:target@host>?Replaces=call-id%3Bto-tag%3Dx%3Bfrom-tag%3Dy`
    pub fn parse(s: &str) -> Result<Self, TransferError> {
        let s = s.trim();

        // Remove angle brackets if present
        let s = s.trim_start_matches('<').trim_end_matches('>');

        // Check for Replaces parameter
        if let Some((uri, params)) = s.split_once('?') {
            let replaces = parse_replaces_param(params)?;
            Ok(Self {
                uri: uri.to_string(),
                replaces,
            })
        } else {
            Ok(Self {
                uri: s.to_string(),
                replaces: None,
            })
        }
    }

    /// Format as header value.
    pub fn to_header(&self) -> String {
        if let Some(ref replaces) = self.replaces {
            format!("<{}?{}>", self.uri, replaces.to_uri_param())
        } else {
            format!("<{}>", self.uri)
        }
    }
}

/// Replaces header for attended transfer (RFC 3891).
#[derive(Debug, Clone)]
pub struct ReplacesHeader {
    /// Call-ID of the call to replace.
    pub call_id: String,
    /// To-tag of the call to replace.
    pub to_tag: String,
    /// From-tag of the call to replace.
    pub from_tag: String,
    /// Early-only flag.
    pub early_only: bool,
}

impl ReplacesHeader {
    /// Create a new Replaces header.
    pub fn new(call_id: &str, to_tag: &str, from_tag: &str) -> Self {
        Self {
            call_id: call_id.to_string(),
            to_tag: to_tag.to_string(),
            from_tag: from_tag.to_string(),
            early_only: false,
        }
    }

    /// Parse from Replaces header value.
    ///
    /// Format: `call-id;to-tag=x;from-tag=y`
    pub fn parse(s: &str) -> Result<Self, TransferError> {
        let mut parts = s.split(';');

        let call_id = parts.next().unwrap_or("").trim();
        if call_id.is_empty() {
            return Err(TransferError::InvalidReferTo("missing call-id".into()));
        }
        let call_id = call_id.to_string();

        let mut to_tag = None;
        let mut from_tag = None;
        let mut early_only = false;

        for part in parts {
            let part = part.trim();
            if let Some((key, value)) = part.split_once('=') {
                match key.to_lowercase().as_str() {
                    "to-tag" => to_tag = Some(value.to_string()),
                    "from-tag" => from_tag = Some(value.to_string()),
                    _ => {}
                }
            } else if part.eq_ignore_ascii_case("early-only") {
                early_only = true;
            }
        }

        Ok(Self {
            call_id,
            to_tag: to_tag.ok_or_else(|| TransferError::InvalidReferTo("missing to-tag".into()))?,
            from_tag: from_tag
                .ok_or_else(|| TransferError::InvalidReferTo("missing from-tag".into()))?,
            early_only,
        })
    }

    /// Format as header value.
    pub fn to_header(&self) -> String {
        let mut s = format!(
            "{};to-tag={};from-tag={}",
            self.call_id, self.to_tag, self.from_tag
        );
        if self.early_only {
            s.push_str(";early-only");
        }
        s
    }

    /// Format for URI parameter (URL-encoded).
    pub fn to_uri_param(&self) -> String {
        // URL encode semicolons and equals
        let encoded = format!(
            "Replaces={}%3Bto-tag%3D{}%3Bfrom-tag%3D{}",
            url_encode(&self.call_id),
            url_encode(&self.to_tag),
            url_encode(&self.from_tag)
        );
        if self.early_only {
            format!("{}%3Bearly-only", encoded)
        } else {
            encoded
        }
    }
}

/// Parse Replaces from URI parameter.
fn parse_replaces_param(params: &str) -> Result<Option<ReplacesHeader>, TransferError> {
    for param in params.split('&') {
        if let Some(value) = param.strip_prefix("Replaces=") {
            // URL decode
            let decoded = url_decode(value);
            return Ok(Some(ReplacesHeader::parse(&decoded)?));
        }
    }
    Ok(None)
}

/// Simple URL encoding for dialog identifiers.
fn url_encode(s: &str) -> String {
    s.replace('%', "%25")
        .replace(';', "%3B")
        .replace('=', "%3D")
        .replace('&', "%26")
        .replace('@', "%40")
}

/// Simple URL decoding.
fn url_decode(s: &str) -> String {
    let mut result = String::new();
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '%' {
            let hex: String = chars.by_ref().take(2).collect();
            if let Ok(byte) = u8::from_str_radix(&hex, 16) {
                result.push(byte as char);
            } else {
                result.push('%');
                result.push_str(&hex);
            }
        } else {
            result.push(c);
        }
    }

    result
}

/// Transfer progress notification.
#[derive(Debug, Clone)]
pub struct TransferProgress {
    /// SIP response code from the sipfrag.
    pub status_code: u16,
    /// Reason phrase.
    pub reason: String,
    /// Whether this is the final notification.
    pub final_: bool,
}

impl TransferProgress {
    /// Parse from NOTIFY body (message/sipfrag).
    pub fn parse_sipfrag(body: &str) -> Option<Self> {
        let first_line = body.lines().next()?;
        let parts: Vec<&str> = first_line.split_whitespace().collect();

        if parts.len() < 3 || parts[0] != "SIP/2.0" {
            return None;
        }

        let status_code: u16 = parts[1].parse().ok()?;
        let reason = parts[2..].join(" ");

        Some(Self {
            status_code,
            reason,
            final_: status_code >= 200,
        })
    }

    /// Check if transfer succeeded.
    pub fn is_success(&self) -> bool {
        self.status_code >= 200 && self.status_code < 300
    }

    /// Check if transfer is still in progress.
    pub fn is_provisional(&self) -> bool {
        self.status_code >= 100 && self.status_code < 200
    }
}

/// Transfer info for a call.
#[derive(Debug, Clone)]
pub struct TransferInfo {
    /// Transfer state.
    pub state: TransferState,
    /// Our role in the transfer.
    pub role: TransferRole,
    /// Transfer type.
    pub transfer_type: TransferType,
    /// Target URI (where we're transferring to).
    pub target: Option<String>,
    /// Call-ID of the REFER subscription.
    pub subscription_id: Option<String>,
    /// Last progress notification.
    pub last_progress: Option<TransferProgress>,
}

impl Default for TransferInfo {
    fn default() -> Self {
        Self {
            state: TransferState::None,
            role: TransferRole::Transferor,
            transfer_type: TransferType::Blind,
            target: None,
            subscription_id: None,
            last_progress: None,
        }
    }
}

/// Manages call transfers.
#[derive(Debug, Default)]
pub struct TransferManager {
    /// Transfer info per call.
    transfers: HashMap<String, TransferInfo>,
}

impl TransferManager {
    /// Create a new transfer manager.
    pub fn new() -> Self {
        Self::default()
    }

    /// Initiate a blind transfer.
    pub fn initiate_blind_transfer(
        &mut self,
        call_id: &str,
        target_uri: &str,
    ) -> Result<ReferTo, TransferError> {
        let info = self.transfers.entry(call_id.to_string()).or_default();

        if info.state != TransferState::None {
            return Err(TransferError::InvalidState(
                "transfer already in progress".into(),
            ));
        }

        info.state = TransferState::Pending;
        info.role = TransferRole::Transferor;
        info.transfer_type = TransferType::Blind;
        info.target = Some(target_uri.to_string());

        Ok(ReferTo::blind(target_uri))
    }

    /// Initiate an attended transfer.
    pub fn initiate_attended_transfer(
        &mut self,
        call_id: &str,
        target_uri: &str,
        replaces: ReplacesHeader,
    ) -> Result<ReferTo, TransferError> {
        let info = self.transfers.entry(call_id.to_string()).or_default();

        if info.state != TransferState::None {
            return Err(TransferError::InvalidState(
                "transfer already in progress".into(),
            ));
        }

        info.state = TransferState::Pending;
        info.role = TransferRole::Transferor;
        info.transfer_type = TransferType::Attended;
        info.target = Some(target_uri.to_string());

        Ok(ReferTo::attended(target_uri, replaces))
    }

    /// Handle REFER response.
    pub fn handle_refer_response(
        &mut self,
        call_id: &str,
        status_code: u16,
    ) -> Result<TransferState, TransferError> {
        let info = self
            .transfers
            .get_mut(call_id)
            .ok_or_else(|| TransferError::CallNotFound(call_id.to_string()))?;

        if (200..300).contains(&status_code) {
            info.state = TransferState::Accepted;
            Ok(TransferState::Accepted)
        } else if status_code >= 400 {
            info.state = TransferState::Failed;
            Err(TransferError::Rejected { code: status_code })
        } else {
            Ok(info.state)
        }
    }

    /// Handle incoming REFER (we are the transferee).
    pub fn handle_incoming_refer(
        &mut self,
        call_id: &str,
        refer_to: ReferTo,
    ) -> Result<&TransferInfo, TransferError> {
        let info = self.transfers.entry(call_id.to_string()).or_default();

        info.state = TransferState::InProgress;
        info.role = TransferRole::Transferee;
        info.transfer_type = if refer_to.replaces.is_some() {
            TransferType::Attended
        } else {
            TransferType::Blind
        };
        info.target = Some(refer_to.uri);

        Ok(info)
    }

    /// Handle NOTIFY with transfer progress.
    pub fn handle_notify(
        &mut self,
        call_id: &str,
        sipfrag: &str,
    ) -> Result<TransferProgress, TransferError> {
        let progress = TransferProgress::parse_sipfrag(sipfrag)
            .ok_or_else(|| TransferError::Failed("invalid sipfrag".into()))?;

        let info = self
            .transfers
            .get_mut(call_id)
            .ok_or_else(|| TransferError::CallNotFound(call_id.to_string()))?;

        if progress.is_success() {
            info.state = TransferState::Completed;
        } else if progress.final_ {
            info.state = TransferState::Failed;
        } else {
            info.state = TransferState::InProgress;
        }

        info.last_progress = Some(progress.clone());
        Ok(progress)
    }

    /// Get transfer state for a call.
    pub fn transfer_state(&self, call_id: &str) -> TransferState {
        self.transfers
            .get(call_id)
            .map(|i| i.state)
            .unwrap_or(TransferState::None)
    }

    /// Get transfer info for a call.
    pub fn transfer_info(&self, call_id: &str) -> Option<&TransferInfo> {
        self.transfers.get(call_id)
    }

    /// Clear transfer state for a call.
    pub fn clear(&mut self, call_id: &str) {
        self.transfers.remove(call_id);
    }
}

/// Build Refer-To header for blind transfer.
#[cfg(test)]
pub(crate) fn build_refer_to_blind(target_uri: &str) -> String {
    format!("Refer-To: <{}>", target_uri)
}

/// Build Refer-To header for attended transfer.
#[cfg(test)]
pub(crate) fn build_refer_to_attended(target_uri: &str, replaces: &ReplacesHeader) -> String {
    format!("Refer-To: <{}?{}>", target_uri, replaces.to_uri_param())
}

/// Build Referred-By header.
#[cfg(test)]
pub(crate) fn build_referred_by(transferor_uri: &str) -> String {
    format!("Referred-By: <{}>", transferor_uri)
}

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

    // TransferError tests
    #[test]
    fn test_transfer_error_call_not_found() {
        let err = TransferError::CallNotFound("call-123".to_string());
        assert!(err.to_string().contains("call not found"));
        assert!(err.to_string().contains("call-123"));
    }

    #[test]
    fn test_transfer_error_invalid_state() {
        let err = TransferError::InvalidState("already in progress".to_string());
        assert!(err.to_string().contains("invalid state"));
        assert!(err.to_string().contains("already in progress"));
    }

    #[test]
    fn test_transfer_error_rejected() {
        let err = TransferError::Rejected { code: 486 };
        assert!(err.to_string().contains("rejected"));
        assert!(err.to_string().contains("486"));
    }

    #[test]
    fn test_transfer_error_failed() {
        let err = TransferError::Failed("timeout".to_string());
        assert!(err.to_string().contains("transfer failed"));
        assert!(err.to_string().contains("timeout"));
    }

    #[test]
    fn test_transfer_error_invalid_refer_to() {
        let err = TransferError::InvalidReferTo("bad uri".to_string());
        assert!(err.to_string().contains("invalid Refer-To URI"));
    }

    #[test]
    fn test_transfer_error_debug() {
        let err = TransferError::CallNotFound("x".to_string());
        let debug = format!("{:?}", err);
        assert!(debug.contains("CallNotFound"));
    }

    // TransferType tests
    #[test]
    fn test_transfer_type_blind() {
        let t = TransferType::Blind;
        assert_eq!(t, TransferType::Blind);
        assert_ne!(t, TransferType::Attended);
    }

    #[test]
    fn test_transfer_type_attended() {
        let t = TransferType::Attended;
        assert_eq!(t, TransferType::Attended);
    }

    #[test]
    fn test_transfer_type_debug() {
        assert!(format!("{:?}", TransferType::Blind).contains("Blind"));
        assert!(format!("{:?}", TransferType::Attended).contains("Attended"));
    }

    #[test]
    fn test_transfer_type_clone() {
        let t = TransferType::Blind;
        let cloned = t;
        assert_eq!(t, cloned);
    }

    // TransferState tests
    #[test]
    fn test_transfer_state_all_variants() {
        assert_eq!(TransferState::None, TransferState::None);
        assert_eq!(TransferState::Pending, TransferState::Pending);
        assert_eq!(TransferState::Accepted, TransferState::Accepted);
        assert_eq!(TransferState::InProgress, TransferState::InProgress);
        assert_eq!(TransferState::Completed, TransferState::Completed);
        assert_eq!(TransferState::Failed, TransferState::Failed);
    }

    #[test]
    fn test_transfer_state_debug() {
        assert!(format!("{:?}", TransferState::None).contains("None"));
        assert!(format!("{:?}", TransferState::Pending).contains("Pending"));
        assert!(format!("{:?}", TransferState::Accepted).contains("Accepted"));
        assert!(format!("{:?}", TransferState::InProgress).contains("InProgress"));
        assert!(format!("{:?}", TransferState::Completed).contains("Completed"));
        assert!(format!("{:?}", TransferState::Failed).contains("Failed"));
    }

    #[test]
    fn test_transfer_state_clone_copy() {
        let s = TransferState::InProgress;
        let cloned = s;
        assert_eq!(s, cloned);
    }

    // TransferRole tests
    #[test]
    fn test_transfer_role_all_variants() {
        assert_eq!(TransferRole::Transferor, TransferRole::Transferor);
        assert_eq!(TransferRole::Transferee, TransferRole::Transferee);
        assert_eq!(TransferRole::Target, TransferRole::Target);
    }

    #[test]
    fn test_transfer_role_debug() {
        assert!(format!("{:?}", TransferRole::Transferor).contains("Transferor"));
        assert!(format!("{:?}", TransferRole::Transferee).contains("Transferee"));
        assert!(format!("{:?}", TransferRole::Target).contains("Target"));
    }

    #[test]
    fn test_transfer_role_clone_copy() {
        let r = TransferRole::Target;
        let cloned = r;
        assert_eq!(r, cloned);
    }

    // ReferTo tests
    #[test]
    fn test_refer_to_blind() {
        let refer_to = ReferTo::blind("sip:carol@example.com");
        assert_eq!(refer_to.to_header(), "<sip:carol@example.com>");
        assert!(refer_to.replaces.is_none());
    }

    #[test]
    fn test_refer_to_attended() {
        let replaces = ReplacesHeader::new("call-123", "to-tag", "from-tag");
        let refer_to = ReferTo::attended("sip:carol@example.com", replaces);
        assert!(refer_to.replaces.is_some());
        let header = refer_to.to_header();
        assert!(header.starts_with("<sip:carol@example.com?"));
        assert!(header.contains("Replaces="));
    }

    #[test]
    fn test_refer_to_parse_blind() {
        let refer_to = ReferTo::parse("<sip:carol@example.com>").unwrap();
        assert_eq!(refer_to.uri, "sip:carol@example.com");
        assert!(refer_to.replaces.is_none());
    }

    #[test]
    fn test_refer_to_parse_without_brackets() {
        let refer_to = ReferTo::parse("sip:carol@example.com").unwrap();
        assert_eq!(refer_to.uri, "sip:carol@example.com");
    }

    #[test]
    fn test_refer_to_parse_with_whitespace() {
        let refer_to = ReferTo::parse("  <sip:carol@example.com>  ").unwrap();
        assert_eq!(refer_to.uri, "sip:carol@example.com");
    }

    #[test]
    fn test_refer_to_parse_with_replaces() {
        let uri = "<sip:carol@example.com?Replaces=call-id%3Bto-tag%3Dto1%3Bfrom-tag%3Dfrom1>";
        let refer_to = ReferTo::parse(uri).unwrap();
        assert_eq!(refer_to.uri, "sip:carol@example.com");
        assert!(refer_to.replaces.is_some());
        let replaces = refer_to.replaces.unwrap();
        assert_eq!(replaces.call_id, "call-id");
        assert_eq!(replaces.to_tag, "to1");
        assert_eq!(replaces.from_tag, "from1");
    }

    #[test]
    fn test_refer_to_parse_invalid_replaces() {
        let uri = "<sip:carol@example.com?Replaces=call-id%3Bto-tag%3Dto1>";
        let err = ReferTo::parse(uri).unwrap_err();
        assert!(err.to_string().contains("invalid Refer-To"));
    }

    #[test]
    fn test_refer_to_debug() {
        let refer_to = ReferTo::blind("sip:test@example.com");
        let debug = format!("{:?}", refer_to);
        assert!(debug.contains("ReferTo"));
    }

    #[test]
    fn test_refer_to_clone() {
        let refer_to = ReferTo::blind("sip:test@example.com");
        let cloned = refer_to.clone();
        assert_eq!(cloned.uri, "sip:test@example.com");
    }

    // ReplacesHeader tests
    #[test]
    fn test_replaces_header() {
        let replaces = ReplacesHeader::new("abc123", "to1", "from1");
        let header = replaces.to_header();
        assert!(header.contains("abc123"));
        assert!(header.contains("to-tag=to1"));
        assert!(header.contains("from-tag=from1"));

        let parsed = ReplacesHeader::parse(&header).unwrap();
        assert_eq!(parsed.call_id, "abc123");
        assert_eq!(parsed.to_tag, "to1");
        assert_eq!(parsed.from_tag, "from1");
    }

    #[test]
    fn test_replaces_header_with_early_only() {
        let mut replaces = ReplacesHeader::new("abc123", "to1", "from1");
        replaces.early_only = true;
        let header = replaces.to_header();
        assert!(header.contains("early-only"));

        let param = replaces.to_uri_param();
        assert!(param.contains("early-only"));
    }

    #[test]
    fn test_replaces_header_parse_early_only() {
        let s = "call-123;to-tag=to1;from-tag=from1;early-only";
        let replaces = ReplacesHeader::parse(s).unwrap();
        assert!(replaces.early_only);
    }

    #[test]
    fn test_replaces_header_parse_unknown_token() {
        let s = "call-123;to-tag=to1;from-tag=from1;unknown";
        let replaces = ReplacesHeader::parse(s).unwrap();
        assert!(!replaces.early_only);
    }

    #[test]
    fn test_replaces_header_parse_missing_to_tag() {
        let s = "call-123;from-tag=from1";
        let result = ReplacesHeader::parse(s);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("invalid Refer-To"));
    }

    #[test]
    fn test_replaces_header_parse_missing_call_id() {
        let result = ReplacesHeader::parse("");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("missing call-id"));
    }

    #[test]
    fn test_replaces_header_parse_missing_from_tag() {
        let s = "call-123;to-tag=to1";
        let result = ReplacesHeader::parse(s);
        assert!(result.is_err());
    }

    #[test]
    fn test_replaces_header_parse_case_insensitive() {
        let s = "call-123;TO-TAG=to1;FROM-TAG=from1";
        let replaces = ReplacesHeader::parse(s).unwrap();
        assert_eq!(replaces.to_tag, "to1");
        assert_eq!(replaces.from_tag, "from1");
    }

    #[test]
    fn test_replaces_header_parse_with_whitespace() {
        let s = " call-123 ; to-tag=to1 ; from-tag=from1 ";
        let replaces = ReplacesHeader::parse(s).unwrap();
        assert_eq!(replaces.call_id, "call-123");
    }

    #[test]
    fn test_replaces_header_parse_unknown_param() {
        let s = "call-123;to-tag=to1;from-tag=from1;unknown=value";
        let replaces = ReplacesHeader::parse(s).unwrap();
        assert_eq!(replaces.to_tag, "to1");
    }

    #[test]
    fn test_replaces_uri_encoding() {
        let replaces = ReplacesHeader::new("abc;123", "to=1", "from&1");
        let param = replaces.to_uri_param();

        // Should be URL encoded
        assert!(param.contains("abc%3B123"));
        assert!(param.contains("to%3D1"));
        assert!(param.contains("from%261"));
    }

    #[test]
    fn test_replaces_debug() {
        let replaces = ReplacesHeader::new("call-123", "to1", "from1");
        let debug = format!("{:?}", replaces);
        assert!(debug.contains("ReplacesHeader"));
    }

    #[test]
    fn test_replaces_clone() {
        let replaces = ReplacesHeader::new("call-123", "to1", "from1");
        let cloned = replaces.clone();
        assert_eq!(cloned.call_id, "call-123");
    }

    // URL encoding/decoding tests
    #[test]
    fn test_url_encode_special_chars() {
        let encoded = url_encode("hello;world=test&more@addr");
        assert!(encoded.contains("%3B"));
        assert!(encoded.contains("%3D"));
        assert!(encoded.contains("%26"));
        assert!(encoded.contains("%40"));
    }

    #[test]
    fn test_url_encode_percent() {
        let encoded = url_encode("50%off");
        assert!(encoded.contains("%25"));
    }

    #[test]
    fn test_url_decode_special_chars() {
        let decoded = url_decode("hello%3Bworld%3Dtest%26more%40addr");
        assert_eq!(decoded, "hello;world=test&more@addr");
    }

    #[test]
    fn test_url_decode_invalid_hex() {
        let decoded = url_decode("hello%GG%world");
        // Invalid hex should be preserved with %
        assert!(decoded.contains("%"));
    }

    #[test]
    fn test_url_decode_truncated() {
        // When only one hex char available, it still parses (e.g. "3" -> 0x3)
        let decoded = url_decode("hello%3");
        assert!(decoded.starts_with("hello"));
    }

    // TransferProgress tests
    #[test]
    fn test_transfer_progress() {
        let sipfrag = "SIP/2.0 180 Ringing\r\n";
        let progress = TransferProgress::parse_sipfrag(sipfrag).unwrap();
        assert_eq!(progress.status_code, 180);
        assert_eq!(progress.reason, "Ringing");
        assert!(!progress.final_);
        assert!(progress.is_provisional());

        let sipfrag = "SIP/2.0 200 OK\r\n";
        let progress = TransferProgress::parse_sipfrag(sipfrag).unwrap();
        assert_eq!(progress.status_code, 200);
        assert!(progress.final_);
        assert!(progress.is_success());
    }

    #[test]
    fn test_transfer_progress_is_provisional_false() {
        let sipfrag = "SIP/2.0 302 Moved";
        let progress = TransferProgress::parse_sipfrag(sipfrag).unwrap();
        assert!(!progress.is_provisional());
    }

    #[test]
    fn test_transfer_progress_below_100_not_provisional() {
        let progress = TransferProgress {
            status_code: 99,
            reason: "Informational".to_string(),
            final_: false,
        };
        assert!(!progress.is_provisional());
    }

    #[test]
    fn test_transfer_progress_100_trying() {
        let sipfrag = "SIP/2.0 100 Trying";
        let progress = TransferProgress::parse_sipfrag(sipfrag).unwrap();
        assert_eq!(progress.status_code, 100);
        assert!(progress.is_provisional());
        assert!(!progress.is_success());
    }

    #[test]
    fn test_transfer_progress_failure() {
        let sipfrag = "SIP/2.0 486 Busy Here";
        let progress = TransferProgress::parse_sipfrag(sipfrag).unwrap();
        assert_eq!(progress.status_code, 486);
        assert!(progress.final_);
        assert!(!progress.is_success());
        assert!(!progress.is_provisional());
    }

    #[test]
    fn test_transfer_progress_long_reason() {
        let sipfrag = "SIP/2.0 603 Decline The Call";
        let progress = TransferProgress::parse_sipfrag(sipfrag).unwrap();
        assert_eq!(progress.reason, "Decline The Call");
    }

    #[test]
    fn test_transfer_progress_invalid_not_sip() {
        let sipfrag = "HTTP/1.1 200 OK";
        let progress = TransferProgress::parse_sipfrag(sipfrag);
        assert!(progress.is_none());
    }

    #[test]
    fn test_transfer_progress_invalid_too_short() {
        let sipfrag = "SIP/2.0 200";
        let progress = TransferProgress::parse_sipfrag(sipfrag);
        assert!(progress.is_none());
    }

    #[test]
    fn test_transfer_progress_invalid_status_code() {
        let sipfrag = "SIP/2.0 XYZ Bad";
        let progress = TransferProgress::parse_sipfrag(sipfrag);
        assert!(progress.is_none());
    }

    #[test]
    fn test_transfer_progress_empty() {
        let progress = TransferProgress::parse_sipfrag("");
        assert!(progress.is_none());
    }

    #[test]
    fn test_transfer_progress_debug() {
        let sipfrag = "SIP/2.0 200 OK";
        let progress = TransferProgress::parse_sipfrag(sipfrag).unwrap();
        let debug = format!("{:?}", progress);
        assert!(debug.contains("TransferProgress"));
    }

    #[test]
    fn test_transfer_progress_clone() {
        let sipfrag = "SIP/2.0 200 OK";
        let progress = TransferProgress::parse_sipfrag(sipfrag).unwrap();
        let cloned = progress.clone();
        assert_eq!(cloned.status_code, 200);
    }

    // TransferInfo tests
    #[test]
    fn test_transfer_info_default() {
        let info = TransferInfo::default();
        assert_eq!(info.state, TransferState::None);
        assert_eq!(info.role, TransferRole::Transferor);
        assert_eq!(info.transfer_type, TransferType::Blind);
        assert!(info.target.is_none());
        assert!(info.subscription_id.is_none());
        assert!(info.last_progress.is_none());
    }

    #[test]
    fn test_transfer_info_debug() {
        let info = TransferInfo::default();
        let debug = format!("{:?}", info);
        assert!(debug.contains("TransferInfo"));
    }

    #[test]
    fn test_transfer_info_clone() {
        let info = TransferInfo {
            target: Some("sip:test@example.com".to_string()),
            ..Default::default()
        };
        let cloned = info.clone();
        assert_eq!(cloned.target, Some("sip:test@example.com".to_string()));
    }

    // TransferManager tests
    #[test]
    fn test_transfer_manager_new() {
        let manager = TransferManager::new();
        assert_eq!(manager.transfer_state("nonexistent"), TransferState::None);
    }

    #[test]
    fn test_transfer_manager_default() {
        let manager = TransferManager::default();
        assert!(manager.transfer_info("x").is_none());
    }

    #[test]
    fn test_transfer_manager_debug() {
        let manager = TransferManager::new();
        let debug = format!("{:?}", manager);
        assert!(debug.contains("TransferManager"));
    }

    #[test]
    fn test_transfer_manager_blind() {
        let mut manager = TransferManager::new();

        let refer_to = manager
            .initiate_blind_transfer("call-1", "sip:carol@example.com")
            .unwrap();

        assert_eq!(manager.transfer_state("call-1"), TransferState::Pending);
        assert!(refer_to.replaces.is_none());

        // REFER accepted
        manager.handle_refer_response("call-1", 202).unwrap();
        assert_eq!(manager.transfer_state("call-1"), TransferState::Accepted);

        // Progress notification
        manager
            .handle_notify("call-1", "SIP/2.0 180 Ringing\r\n")
            .unwrap();
        assert_eq!(manager.transfer_state("call-1"), TransferState::InProgress);

        // Success
        manager
            .handle_notify("call-1", "SIP/2.0 200 OK\r\n")
            .unwrap();
        assert_eq!(manager.transfer_state("call-1"), TransferState::Completed);
    }

    #[test]
    fn test_transfer_manager_attended() {
        let mut manager = TransferManager::new();

        let replaces = ReplacesHeader::new("consultation-call", "tag1", "tag2");
        let refer_to = manager
            .initiate_attended_transfer("call-1", "sip:carol@example.com", replaces)
            .unwrap();

        assert!(refer_to.replaces.is_some());
        assert_eq!(
            manager.transfer_info("call-1").unwrap().transfer_type,
            TransferType::Attended
        );
    }

    #[test]
    fn test_transfer_manager_blind_already_in_progress() {
        let mut manager = TransferManager::new();
        manager
            .initiate_blind_transfer("call-1", "sip:carol@example.com")
            .unwrap();
        let result = manager.initiate_blind_transfer("call-1", "sip:dave@example.com");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("invalid state"));
    }

    #[test]
    fn test_transfer_manager_attended_already_in_progress() {
        let mut manager = TransferManager::new();
        manager
            .initiate_blind_transfer("call-1", "sip:carol@example.com")
            .unwrap();
        let replaces = ReplacesHeader::new("call-2", "tag1", "tag2");
        let result = manager.initiate_attended_transfer("call-1", "sip:dave@example.com", replaces);
        assert!(result.is_err());
    }

    #[test]
    fn test_transfer_manager_refer_response_rejected() {
        let mut manager = TransferManager::new();
        manager
            .initiate_blind_transfer("call-1", "sip:carol@example.com")
            .unwrap();
        let result = manager.handle_refer_response("call-1", 486);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("transfer rejected"));
        assert_eq!(manager.transfer_state("call-1"), TransferState::Failed);
    }

    #[test]
    fn test_transfer_manager_refer_response_provisional() {
        let mut manager = TransferManager::new();
        manager
            .initiate_blind_transfer("call-1", "sip:carol@example.com")
            .unwrap();
        let state = manager.handle_refer_response("call-1", 100).unwrap();
        assert_eq!(state, TransferState::Pending); // Still pending for provisional
    }

    #[test]
    fn test_transfer_manager_refer_response_call_not_found() {
        let mut manager = TransferManager::new();
        let result = manager.handle_refer_response("nonexistent", 200);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("call not found"));
    }

    #[test]
    fn test_transfer_manager_notify_failed() {
        let mut manager = TransferManager::new();
        manager
            .initiate_blind_transfer("call-1", "sip:carol@example.com")
            .unwrap();
        manager.handle_refer_response("call-1", 202).unwrap();

        let progress = manager
            .handle_notify("call-1", "SIP/2.0 486 Busy Here")
            .unwrap();
        assert!(!progress.is_success());
        assert_eq!(manager.transfer_state("call-1"), TransferState::Failed);
    }

    #[test]
    fn test_transfer_manager_notify_invalid_sipfrag() {
        let mut manager = TransferManager::new();
        manager
            .initiate_blind_transfer("call-1", "sip:carol@example.com")
            .unwrap();
        let result = manager.handle_notify("call-1", "invalid sipfrag");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("transfer failed"));
    }

    #[test]
    fn test_transfer_manager_notify_call_not_found() {
        let mut manager = TransferManager::new();
        let result = manager.handle_notify("nonexistent", "SIP/2.0 200 OK");
        assert!(result.is_err());
    }

    #[test]
    fn test_incoming_refer() {
        let mut manager = TransferManager::new();

        let refer_to = ReferTo::blind("sip:carol@example.com");
        let info = manager.handle_incoming_refer("call-1", refer_to).unwrap();

        assert_eq!(info.role, TransferRole::Transferee);
        assert_eq!(info.state, TransferState::InProgress);
    }

    #[test]
    fn test_incoming_refer_attended() {
        let mut manager = TransferManager::new();
        let replaces = ReplacesHeader::new("call-2", "tag1", "tag2");
        let refer_to = ReferTo::attended("sip:carol@example.com", replaces);
        let info = manager.handle_incoming_refer("call-1", refer_to).unwrap();

        assert_eq!(info.transfer_type, TransferType::Attended);
        assert_eq!(info.target, Some("sip:carol@example.com".to_string()));
    }

    #[test]
    fn test_transfer_manager_clear() {
        let mut manager = TransferManager::new();
        manager
            .initiate_blind_transfer("call-1", "sip:carol@example.com")
            .unwrap();
        assert!(manager.transfer_info("call-1").is_some());

        manager.clear("call-1");
        assert!(manager.transfer_info("call-1").is_none());
        assert_eq!(manager.transfer_state("call-1"), TransferState::None);
    }

    #[test]
    fn test_transfer_manager_clear_nonexistent() {
        let mut manager = TransferManager::new();
        manager.clear("nonexistent"); // Should not panic
    }

    // Helper function tests
    #[test]
    fn test_build_refer_to_blind() {
        let header = build_refer_to_blind("sip:carol@example.com");
        assert_eq!(header, "Refer-To: <sip:carol@example.com>");
    }

    #[test]
    fn test_build_refer_to_attended() {
        let replaces = ReplacesHeader::new("call-123", "to1", "from1");
        let header = build_refer_to_attended("sip:carol@example.com", &replaces);
        assert!(header.starts_with("Refer-To: <sip:carol@example.com?"));
        assert!(header.contains("Replaces="));
    }

    #[test]
    fn test_build_referred_by() {
        let header = build_referred_by("sip:alice@example.com");
        assert_eq!(header, "Referred-By: <sip:alice@example.com>");
    }

    // Parse replaces param tests
    #[test]
    fn test_parse_replaces_param_no_replaces() {
        let result = parse_replaces_param("other=value").unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_parse_replaces_param_with_other_params() {
        let result = parse_replaces_param(
            "foo=bar&Replaces=call-123%3Bto-tag%3Dto1%3Bfrom-tag%3Dfrom1&baz=qux",
        )
        .unwrap();
        assert!(result.is_some());
        let replaces = result.unwrap();
        assert_eq!(replaces.call_id, "call-123");
    }
}