freeswitch-log-parser 0.17.1

Parser for FreeSWITCH log files — handles compressed .xz files, multi-line dumps, truncated buffers, and stateful UUID/timestamp tracking
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
//! Cross-session leg linking — originate, bridge and loopback pairing.

use super::*;
use crate::stream::{ParseWarning, SessionReading};

#[test]
fn originate_success_links_both_legs() {
    // "Originate Resulted in Success" contains both the A-leg UUID (line prefix)
    // and B-leg UUID (Peer UUID field). Both legs should learn about each other.
    let lines = vec![
        full_line(UUID2, TS1, "New Channel sofia/esinet1-v6-tcp/sip:target.example.com [b2c3d4e5-f6a7-8901-bcde-f12345678901]"),
        full_line(UUID1, TS2, "Originate Resulted in Success: [sofia/esinet1-v6-tcp/sip:target.example.com] Peer UUID: b2c3d4e5-f6a7-8901-bcde-f12345678901"),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        a_leg.other_leg_uuid.as_deref(),
        Some(UUID2),
        "A-leg other_leg_uuid set from Originate Resulted in Success"
    );

    let b_leg = tracker.sessions().get(UUID2).unwrap();
    assert_eq!(
        b_leg.other_leg_uuid.as_deref(),
        Some(UUID1),
        "B-leg other_leg_uuid points back to A-leg"
    );
}

#[test]
fn originate_success_channel_fallback_links_legs() {
    // FS 1.10.5-dev and similar omit `Peer UUID:` from "Originate Resulted in Success".
    // The b-leg's New Channel populates channel_name 3.5 s before originate; the
    // fallback path matches by channel name when the Peer UUID is absent.
    let lines = vec![
        full_line(
            UUID2,
            TS1,
            "New Channel sofia/internal/6244@192.0.2.72:50744 [b2c3d4e5-f6a7-8901-bcde-f12345678901]",
        ),
        full_line(
            UUID1,
            TS2,
            "Originate Resulted in Success: [sofia/internal/6244@192.0.2.72:50744]",
        ),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        a_leg.other_leg_uuid.as_deref(),
        Some(UUID2),
        "A-leg linked to B-leg via channel-name fallback when Peer UUID absent"
    );

    let b_leg = tracker.sessions().get(UUID2).unwrap();
    assert_eq!(
        b_leg.other_leg_uuid.as_deref(),
        Some(UUID1),
        "B-leg linked back to A-leg"
    );
}

/// The suffix is there, so the channel-name fallback — which exists for builds
/// that write none — must not stand in for a peer nobody can read.
#[test]
fn an_unreadable_peer_uuid_links_nothing_and_warns() {
    let entries = collect_enriched(vec![
        full_line(
            UUID2,
            TS1,
            "New Channel sofia/internal/6244@192.0.2.72:50744 [b2c3d4e5-f6a7-8901-bcde-f12345678901]",
        ),
        full_line(
            UUID1,
            TS2,
            "Originate Resulted in Success: [sofia/internal/6244@192.0.2.72:50744] Peer UUID: b2c3d4e5-f6a7-89",
        ),
    ]);

    let last = entries.last().unwrap();
    assert_eq!(
        last.entry.warnings,
        [ParseWarning::UnreadableValue {
            reading: SessionReading::PeerUuid,
            value: "b2c3d4e5-f6a7-89".to_string(),
        }]
    );
    assert_eq!(last.session.as_ref().unwrap().other_leg_uuid, None);
}

#[test]
fn originate_success_peer_uuid_wins_over_channel_fallback() {
    // When Peer UUID is present, channel-name fallback must not fire — even if
    // another session shares the channel name. Peer UUID is authoritative.
    let lines = vec![
        full_line(
            UUID2,
            TS1,
            "New Channel sofia/internal/6244@192.0.2.72:50744 [b2c3d4e5-f6a7-8901-bcde-f12345678901]",
        ),
        full_line(
            UUID3,
            TS1,
            "New Channel sofia/internal/6244@192.0.2.72:50744 [c3d4e5f6-a7b8-9012-cdef-234567890123]",
        ),
        full_line(
            UUID1,
            TS2,
            "Originate Resulted in Success: [sofia/internal/6244@192.0.2.72:50744] Peer UUID: b2c3d4e5-f6a7-8901-bcde-f12345678901",
        ),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        a_leg.other_leg_uuid.as_deref(),
        Some(UUID2),
        "Peer UUID wins over channel-name match"
    );

    let decoy = tracker.sessions().get(UUID3).unwrap();
    assert_eq!(
        decoy.other_leg_uuid, None,
        "Decoy session sharing channel name is not touched"
    );
}

#[test]
fn originate_success_channel_fallback_skips_when_ambiguous() {
    // Two b-leg candidates share the same channel name. The fallback must not
    // guess — correctness over coverage.
    let lines = vec![
        full_line(
            UUID2,
            TS1,
            "New Channel sofia/internal/6244@192.0.2.72:50744 [b2c3d4e5-f6a7-8901-bcde-f12345678901]",
        ),
        full_line(
            UUID3,
            TS1,
            "New Channel sofia/internal/6244@192.0.2.72:50744 [c3d4e5f6-a7b8-9012-cdef-234567890123]",
        ),
        full_line(
            UUID1,
            TS2,
            "Originate Resulted in Success: [sofia/internal/6244@192.0.2.72:50744]",
        ),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        a_leg.other_leg_uuid, None,
        "Ambiguous channel name yields no link"
    );
    assert_eq!(tracker.sessions().get(UUID2).unwrap().other_leg_uuid, None);
    assert_eq!(tracker.sessions().get(UUID3).unwrap().other_leg_uuid, None);
}

#[test]
fn originate_success_channel_fallback_skips_terminated_candidates() {
    // Two b-leg sessions share the same channel_name, but one is in
    // CS_DESTROY (stale prior call on the same registered phone). The
    // liveness filter must drop the terminated candidate so the live one
    // becomes the unambiguous match.
    let lines = vec![
        full_line(
            UUID2,
            TS1,
            "New Channel sofia/internal/6244@192.0.2.72:50744 [b2c3d4e5-f6a7-8901-bcde-f12345678901]",
        ),
        full_line(
            UUID2,
            TS1,
            "(sofia/internal/6244@192.0.2.72:50744) State Change CS_EXECUTE -> CS_DESTROY",
        ),
        full_line(
            UUID3,
            TS1,
            "New Channel sofia/internal/6244@192.0.2.72:50744 [c3d4e5f6-a7b8-9012-cdef-234567890123]",
        ),
        full_line(
            UUID1,
            TS2,
            "Originate Resulted in Success: [sofia/internal/6244@192.0.2.72:50744]",
        ),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        a_leg.other_leg_uuid.as_deref(),
        Some(UUID3),
        "Live b-leg wins over CS_DESTROY straggler"
    );

    let live_b = tracker.sessions().get(UUID3).unwrap();
    assert_eq!(
        live_b.other_leg_uuid.as_deref(),
        Some(UUID1),
        "Live b-leg points back to a-leg"
    );

    let stale_b = tracker.sessions().get(UUID2).unwrap();
    assert_eq!(
        stale_b.other_leg_uuid, None,
        "Terminated b-leg is not touched"
    );
}

#[test]
fn originate_success_channel_fallback_skips_when_no_match() {
    // a-leg fires Originate with a bracketed channel name no session has.
    // Must not panic, must not create a spurious link.
    let lines = vec![full_line(
        UUID1,
        TS2,
        "Originate Resulted in Success: [sofia/internal/6244@192.0.2.72:50744]",
    )];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(a_leg.other_leg_uuid, None);
    assert_eq!(a_leg.pending_bridge_target, None);
}

#[test]
fn bridge_origination_uuid_links_a_leg_immediately() {
    // bridge([origination_uuid=BLEG_UUID,...]) guarantees B-leg UUID from execute args alone.
    // A-leg knows B-leg immediately, B-leg learns A-leg when New Channel appears.
    let lines = vec![
        full_line(UUID1, TS1, "EXECUTE [depth=0] sofia/internal-v6/1232@[2001:db8::10] bridge([origination_uuid=b2c3d4e5-f6a7-8901-bcde-f12345678901,leg_timeout=2]sofia/esinet1-v6-tcp/sip:target.example.com)"),
        full_line(UUID2, TS1, "New Channel sofia/esinet1-v6-tcp/sip:target.example.com [b2c3d4e5-f6a7-8901-bcde-f12345678901]"),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        a_leg.other_leg_uuid.as_deref(),
        Some(UUID2),
        "A-leg knows B-leg UUID from origination_uuid in bridge args"
    );

    let b_leg = tracker.sessions().get(UUID2).unwrap();
    assert_eq!(
        b_leg.other_leg_uuid.as_deref(),
        Some(UUID1),
        "B-leg knows A-leg once New Channel correlates"
    );
}

#[test]
fn bridge_target_matches_new_channel() {
    // bridge() without origination_uuid — B-leg UUID is auto-generated by FS.
    // Match via bridge target channel matching next New Channel with same target.
    let lines = vec![
        full_line(UUID1, TS1, "EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 bridge(sofia/gateway/carrier/+15559876543)"),
        full_line(UUID1, TS1, "Parsing session specific variables"),
        full_line(UUID2, TS1, "New Channel sofia/gateway/carrier/+15559876543 [b2c3d4e5-f6a7-8901-bcde-f12345678901]"),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        a_leg.other_leg_uuid.as_deref(),
        Some(UUID2),
        "A-leg linked to B-leg via bridge target matching New Channel"
    );

    let b_leg = tracker.sessions().get(UUID2).unwrap();
    assert_eq!(
        b_leg.other_leg_uuid.as_deref(),
        Some(UUID1),
        "B-leg linked back to A-leg"
    );
}

#[test]
fn concurrent_bridges_to_one_target_link_nothing() {
    // Two A legs waiting on the same target string: the New Channel that follows
    // belongs to one of them and the log does not say which.
    let lines = vec![
        full_line(UUID1, TS1, "EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 bridge(sofia/gateway/carrier/+15559876543)"),
        full_line(UUID3, TS1, "EXECUTE [depth=0] sofia/internal/+15550005678@192.0.2.1 bridge(sofia/gateway/carrier/+15559876543)"),
        full_line(UUID2, TS2, "New Channel sofia/gateway/carrier/+15559876543 [b2c3d4e5-f6a7-8901-bcde-f12345678901]"),
    ];
    let tracker = track(lines);

    for uuid in [UUID1, UUID2, UUID3] {
        assert_eq!(
            tracker.sessions().get(uuid).unwrap().other_leg_uuid,
            None,
            "{uuid} must not be linked on an ambiguous target"
        );
    }
}

#[test]
fn originate_success_corrects_wrong_target_match() {
    // Bridge target matching guessed UUID2 as B-leg, but originate success reveals
    // the actual B-leg is UUID3. The authoritative success message must override.
    let lines = vec![
        full_line(UUID1, TS1, "EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 bridge(sofia/gateway/carrier/+15559876543)"),
        full_line(UUID2, TS1, "New Channel sofia/gateway/carrier/+15559876543 [b2c3d4e5-f6a7-8901-bcde-f12345678901]"),
        full_line(UUID1, TS2, "Originate Resulted in Success: [sofia/gateway/carrier/+15559876543] Peer UUID: c3d4e5f6-a7b8-9012-cdef-234567890123"),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        a_leg.other_leg_uuid.as_deref(),
        Some(UUID3),
        "Originate success overrides earlier target-match guess"
    );

    let real_b_leg = tracker.sessions().get(UUID3).unwrap();
    assert_eq!(
        real_b_leg.other_leg_uuid.as_deref(),
        Some(UUID1),
        "Real B-leg points back to A-leg"
    );
}

#[test]
fn channel_data_other_leg_uuid() {
    let lines = vec![
        full_line(UUID1, TS1, "CHANNEL_DATA:"),
        format!("{UUID1} Other-Leg-Unique-ID: [{UUID2}]"),
    ];
    let tracker = track(lines);

    let state = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(
        state.other_leg_uuid.as_deref(),
        Some(UUID2),
        "other_leg_uuid set from Other-Leg-Unique-ID CHANNEL_DATA field"
    );
}

#[test]
fn relink_removes_stale_by_other_leg_entry() {
    // B first points at C (Other-Leg-Unique-ID), then an authoritative
    // Peer UUID relinks B to A. The superseded C-keyed by_other_leg entry
    // must be removed — otherwise a later New Channel on C back-links to
    // B and clobbers the authoritative A<->B pair.
    let lines = vec![
        full_line(UUID2, TS1, "CHANNEL_DATA:"),
        format!("{UUID2} Other-Leg-Unique-ID: [{UUID3}]"),
        full_line(
            UUID1,
            TS2,
            &format!(
                "Originate Resulted in Success: [sofia/internal/6244@192.0.2.72:50744] Peer UUID: {UUID2}"
            ),
        ),
        full_line(
            UUID3,
            TS2,
            &format!("New Channel sofia/external/dest@192.0.2.9 [{UUID3}]"),
        ),
    ];
    let tracker = track(lines);

    let a_leg = tracker.sessions().get(UUID1).unwrap();
    assert_eq!(a_leg.other_leg_uuid.as_deref(), Some(UUID2));

    let b_leg = tracker.sessions().get(UUID2).unwrap();
    assert_eq!(
        b_leg.other_leg_uuid.as_deref(),
        Some(UUID1),
        "authoritative Peer UUID link must survive the unrelated New Channel"
    );

    let c_leg = tracker.sessions().get(UUID3).unwrap();
    assert_eq!(
        c_leg.other_leg_uuid, None,
        "New Channel on C must not back-link via the superseded index entry"
    );
}

#[test]
fn loopback_b_leg_links_to_its_a_leg() {
    let tracker = track(vec![
        full_line(UUID1, TS1, "New Channel loopback/tty-a [ignored]"),
        full_line(UUID2, TS1, "New Channel loopback/tty-b [ignored]"),
    ]);
    assert_eq!(
        tracker.sessions()[UUID1].other_leg_uuid.as_deref(),
        Some(UUID2)
    );
    assert_eq!(
        tracker.sessions()[UUID2].other_leg_uuid.as_deref(),
        Some(UUID1)
    );
}

#[test]
fn concurrent_loopbacks_to_one_destination_do_not_link() {
    let tracker = track(vec![
        full_line(UUID1, TS1, "New Channel loopback/tty-a [ignored]"),
        full_line(UUID2, TS1, "New Channel loopback/tty-a [ignored]"),
        full_line(UUID3, TS1, "New Channel loopback/tty-b [ignored]"),
    ]);
    assert!(
        tracker.sessions()[UUID3].other_leg_uuid.is_none(),
        "two live A legs share the name; picking one would be a guess"
    );
}

#[test]
fn removing_a_session_sweeps_the_link_pointed_at_it() {
    let mut tracker = SessionTracker::new(LogStream::new(
        vec![
            full_line(UUID2, TS1, "New Channel sofia/internal/one [ignored]"),
            full_line(
                UUID1,
                TS1,
                &format!("Originate Resulted in Success: [sofia/internal/one] Peer UUID: {UUID2}"),
            ),
            full_line(UUID1, TS2, "New Channel sofia/internal/two [ignored]"),
        ]
        .into_iter(),
    ));
    tracker.by_ref().take(2).for_each(drop);
    tracker.remove_session(UUID1);
    for _ in tracker.by_ref() {}

    assert!(
        tracker.sessions()[UUID1].other_leg_uuid.is_none(),
        "a new channel on a removed session's uuid must not inherit its old pairing"
    );
}

#[test]
fn relinking_a_leg_keeps_a_later_pairs_back_link() {
    let tracker = track(vec![
        full_line(
            UUID1,
            TS1,
            &format!("Originate Resulted in Success: [sofia/internal/one] Peer UUID: {UUID3}"),
        ),
        full_line(
            UUID2,
            TS1,
            &format!("Originate Resulted in Success: [sofia/internal/two] Peer UUID: {UUID3}"),
        ),
        full_line(
            UUID1,
            TS2,
            &format!("Originate Resulted in Success: [sofia/internal/three] Peer UUID: {UUID4}"),
        ),
    ]);
    assert_eq!(
        tracker.by_other_leg.get(UUID3).map(String::as_str),
        Some(UUID2),
        "the second pair owns UUID3's back-link; the first leg moving on must not cut it"
    );
    assert_eq!(
        tracker.by_other_leg.get(UUID4).map(String::as_str),
        Some(UUID1)
    );
}