freeswitch-log-parser 0.13.0

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
//! CHANNEL_DATA and SDP block detection, and multi-line value reassembly.

use freeswitch_types::variables::SofiaVariable;
use freeswitch_types::ChannelVariable;

use super::*;

// --- New: Block detection tests ---

#[test]
fn channel_data_block_fields_and_variables() {
    let lines = vec![
        full_line(UUID1, TS1, "CHANNEL_DATA:"),
        format!("{UUID1} Channel-Name: [sofia/internal/+15550001234@192.0.2.1]"),
        format!("{UUID1} Channel-State: [CS_EXECUTE]"),
        format!("{UUID1} Unique-ID: [{UUID1}]"),
        "variable_sip_call_id: [test123@192.0.2.1]".to_string(),
        "variable_direction: [inbound]".to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    assert_eq!(entries[0].message_kind, MessageKind::ChannelData);
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::ChannelData { fields, variables } => {
            assert_eq!(fields.len(), 3);
            assert_eq!(
                fields[0],
                (
                    "Channel-Name".to_string(),
                    "sofia/internal/+15550001234@192.0.2.1".to_string()
                )
            );
            assert_eq!(
                fields[1],
                ("Channel-State".to_string(), "CS_EXECUTE".to_string())
            );
            assert_eq!(fields[2], ("Unique-ID".to_string(), UUID1.to_string()));
            assert_eq!(variables.len(), 2);
            assert_eq!(
                variables[0],
                (
                    "variable_sip_call_id".to_string(),
                    "test123@192.0.2.1".to_string()
                )
            );
            assert_eq!(
                variables[1],
                ("variable_direction".to_string(), "inbound".to_string())
            );
        }
        other => panic!("expected ChannelData block, got {other:?}"),
    }
}

#[test]
fn channel_data_multiline_variable_reassembly() {
    let lines = vec![
        full_line(UUID1, TS1, "CHANNEL_DATA:"),
        format!("{UUID1} Channel-Name: [sofia/internal/+15550001234@192.0.2.1]"),
        "variable_switch_r_sdp: [v=0".to_string(),
        "o=- 1234 5678 IN IP4 192.0.2.1".to_string(),
        "s=-".to_string(),
        "c=IN IP4 192.0.2.1".to_string(),
        "m=audio 47758 RTP/AVP 0 101".to_string(),
        "a=rtpmap:0 PCMU/8000".to_string(),
        "]".to_string(),
        "variable_direction: [inbound]".to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::ChannelData { fields, variables } => {
            assert_eq!(fields.len(), 1);
            assert_eq!(variables.len(), 2);
            assert_eq!(variables[0].0, "variable_switch_r_sdp");
            assert!(variables[0].1.starts_with("v=0\n"));
            assert!(variables[0].1.contains("m=audio 47758 RTP/AVP 0 101"));
            assert!(!variables[0].1.ends_with(']'));
            assert_eq!(
                variables[1],
                ("variable_direction".to_string(), "inbound".to_string())
            );
        }
        other => panic!("expected ChannelData block, got {other:?}"),
    }
    assert_eq!(entries[0].attached.len(), 9);
}

#[test]
fn sdp_block_detection() {
    let lines = vec![
        full_line(UUID1, TS1, "Local SDP:"),
        "v=0".to_string(),
        "o=- 1234 5678 IN IP4 192.0.2.1".to_string(),
        "s=-".to_string(),
        "c=IN IP4 192.0.2.1".to_string(),
        "m=audio 10000 RTP/AVP 0".to_string(),
        "a=rtpmap:0 PCMU/8000".to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    match &entries[0].message_kind {
        MessageKind::SdpMarker { direction } => assert_eq!(*direction, SdpDirection::Local),
        other => panic!("expected SdpMarker, got {other:?}"),
    }
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::Sdp { direction, body } => {
            assert_eq!(*direction, SdpDirection::Local);
            assert_eq!(body.len(), 6);
            assert_eq!(body[0], "v=0");
            assert_eq!(body[5], "a=rtpmap:0 PCMU/8000");
        }
        other => panic!("expected Sdp block, got {other:?}"),
    }
}

#[test]
fn sdp_block_terminated_by_primary_line() {
    let lines = vec![
        full_line(UUID1, TS1, "Remote SDP:"),
        "v=0".to_string(),
        "m=audio 10000 RTP/AVP 0".to_string(),
        full_line(UUID1, TS2, "Next event"),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 2);
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::Sdp { direction, body } => {
            assert_eq!(*direction, SdpDirection::Remote);
            assert_eq!(body.len(), 2);
        }
        other => panic!("expected Sdp block, got {other:?}"),
    }
    assert!(entries[1].block.is_none());
}

#[test]
fn sdp_from_uuid_continuation() {
    let lines = vec![
        format!("{UUID1} Local SDP:"),
        format!("{UUID1} v=0"),
        format!("{UUID1} o=FreeSWITCH 1234 5678 IN IP4 192.0.2.1"),
        format!("{UUID1} s=FreeSWITCH"),
        format!("{UUID1} c=IN IP4 192.0.2.1"),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::Sdp { direction, body } => {
            assert_eq!(*direction, SdpDirection::Local);
            assert_eq!(body.len(), 4);
            assert_eq!(body[0], "v=0");
        }
        other => panic!("expected Sdp block, got {other:?}"),
    }
}

#[test]
fn channel_data_interrupted_by_different_uuid() {
    let lines = vec![
        full_line(UUID1, TS1, "CHANNEL_DATA:"),
        format!("{UUID1} Channel-Name: [sofia/internal/+15550001234@192.0.2.1]"),
        format!("{UUID2} Dialplan: sofia/internal/+15559999999@192.0.2.1 parsing [public]"),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 2);
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::ChannelData { fields, .. } => {
            assert_eq!(fields.len(), 1);
        }
        other => panic!("expected ChannelData, got {other:?}"),
    }
}

#[test]
fn no_block_for_non_block_message() {
    let lines = vec![full_line(UUID1, TS1, "some random freeswitch log message")];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    assert!(entries[0].block.is_none());
    assert_eq!(entries[0].message_kind, MessageKind::General);
}

#[test]
fn message_kind_on_execute() {
    let lines = vec![
        full_line(UUID1, TS1, "First"),
        format!("{UUID1} EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 set(foo=bar)"),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 2);
    match &entries[1].message_kind {
        MessageKind::Execute {
            application,
            arguments,
            ..
        } => {
            assert_eq!(application, "set");
            assert_eq!(arguments, "foo=bar");
        }
        other => panic!("expected Execute, got {other:?}"),
    }
}

#[test]
fn block_accessors_hide_the_variable_prefix() {
    let lines = vec![
        full_line(UUID1, TS1, "CHANNEL_DATA:"),
        format!("{UUID1} Channel-Name: [sofia/internal/1000@192.0.2.1]"),
        "variable_sip_call_id: [test123@192.0.2.1]".to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    let block = entries[0].block.as_ref().expect("should have block");

    assert_eq!(
        block.field("Channel-Name"),
        Some("sofia/internal/1000@192.0.2.1")
    );
    assert_eq!(block.field("Channel-State"), None);
    assert_eq!(
        block.variable(SofiaVariable::SipCallId),
        Some("test123@192.0.2.1"),
        "the caller never spells the variable_ prefix a dump stores"
    );
    assert_eq!(block.variable(ChannelVariable::Direction), None);
}

/// The closing `]` is one delimiter, not a run of them — a value whose own last
/// character is `]` keeps it.
#[test]
fn channel_data_multiline_variable_keeps_a_trailing_bracket() {
    let lines = vec![
        full_line(UUID1, TS1, "CHANNEL_DATA:"),
        "variable_json_payload: [{".to_string(),
        r#""targets":[1,2]]"#.to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::ChannelData { variables, .. } => {
            assert_eq!(variables[0].1, "{\n\"targets\":[1,2]");
        }
        other => panic!("expected ChannelData block, got {other:?}"),
    }
}

#[test]
fn channel_data_multiline_variable_spans_many_lines() {
    let lines = vec![
        full_line(UUID1, TS1, "CHANNEL_DATA:"),
        format!("{UUID1} Channel-Name: [sofia/internal/+15550001234@192.0.2.1]"),
        "variable_switch_r_sdp: [v=0".to_string(),
        "o=- 1234 5678 IN IP4 192.0.2.1".to_string(),
        "s=-".to_string(),
        "c=IN IP4 192.0.2.1".to_string(),
        "t=0 0".to_string(),
        "m=audio 47758 RTP/AVP 0 8 101".to_string(),
        "a=rtpmap:0 PCMU/8000".to_string(),
        "a=rtpmap:8 PCMA/8000".to_string(),
        "a=rtpmap:101 telephone-event/8000".to_string(),
        "a=fmtp:101 0-16".to_string(),
        "]".to_string(),
        "variable_direction: [inbound]".to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::ChannelData { fields, variables } => {
            assert_eq!(fields.len(), 1);
            assert_eq!(variables.len(), 2);
            assert_eq!(variables[0].0, "variable_switch_r_sdp");
            let sdp = &variables[0].1;
            assert!(sdp.starts_with("v=0\n"));
            assert!(sdp.contains("a=fmtp:101 0-16"));
            assert!(!sdp.ends_with(']'));
            assert_eq!(variables[1].0, "variable_direction");
        }
        other => panic!("expected ChannelData block, got {other:?}"),
    }
}

#[test]
fn sdp_from_verto_update_media() {
    let lines = vec![
        full_line(UUID1, TS1, "updateMedia: Local SDP"),
        "v=0".to_string(),
        "o=- 1234 5678 IN IP4 192.0.2.1".to_string(),
        "m=audio 10000 RTP/AVP 0".to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    match &entries[0].message_kind {
        MessageKind::SdpMarker { direction } => assert_eq!(*direction, SdpDirection::Local),
        other => panic!("expected SdpMarker, got {other:?}"),
    }
    let block = entries[0].block.as_ref().expect("should have block");
    match block {
        Block::Sdp { direction, body } => {
            assert_eq!(*direction, SdpDirection::Local);
            assert_eq!(body.len(), 3);
        }
        other => panic!("expected Sdp block, got {other:?}"),
    }
}

/// `sofia_glue.c` logs the invite line and the marker from one format string, so
/// the marker is never the entry's own message.
#[test]
fn sdp_marker_on_a_continuation_opens_the_block() {
    let lines = vec![
        full_line(
            UUID1,
            TS1,
            "sofia/internal/+15550001234@192.0.2.1 sending invite version: 1.10.13",
        ),
        format!("{UUID1} Local SDP:"),
        format!("{UUID1} v=0"),
        format!("{UUID1} o=FreeSWITCH 1234 5678 IN IP4 192.0.2.1"),
        format!("{UUID1} m=audio 10000 RTP/AVP 0"),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    match &entries[0].message_kind {
        MessageKind::SipInvite { .. } => {}
        other => panic!("expected SipInvite, got {other:?}"),
    }
    match entries[0].block.as_ref().expect("should have block") {
        Block::Sdp { direction, body } => {
            assert_eq!(*direction, SdpDirection::Local);
            assert_eq!(body.len(), 3, "the marker line is not part of the body");
            assert_eq!(body[0], "v=0");
        }
        other => panic!("expected Sdp block, got {other:?}"),
    }
}

#[test]
fn a_markerless_body_opens_on_its_version_line() {
    let lines = vec![
        full_line(
            UUID1,
            TS1,
            "Oreka SIP Packet (SELECTED RTP PORT: R=10000, W=10002, SOURCE=DEFAULT):",
        ),
        format!("{UUID1} INVITE sip:rec@192.0.2.1:5060 SIP/2.0\r"),
        format!("{UUID1} Subject: BEGIN RX recording of Pat Doe\r"),
        format!("{UUID1} \r"),
        format!("{UUID1} v=0\r"),
        format!("{UUID1} o=freeswitch 1234 1 IN IP4 192.0.2.1\r"),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    match entries[0].block.as_ref().expect("should have block") {
        Block::Sdp { direction, body } => {
            assert_eq!(*direction, SdpDirection::Unknown);
            assert_eq!(body.len(), 2, "the SIP headers ahead of it are not body");
            assert_eq!(body[0], "v=0\r");
        }
        other => panic!("expected Sdp block, got {other:?}"),
    }
}

#[test]
fn a_markerless_body_without_a_uuid_opens_no_block() {
    let lines = vec![
        full_line(UUID1, TS1, "Entry that owns no block"),
        "recv 1024 bytes from udp/[192.0.2.9]:5060:".to_string(),
        "v=0".to_string(),
        "m=audio 10000 RTP/AVP 0".to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    assert!(entries[0].block.is_none());
}

#[test]
fn an_embedded_sdp_value_stays_in_the_channel_data_block() {
    let lines = vec![
        full_line(UUID1, TS1, "CHANNEL_DATA:"),
        format!("{UUID1} variable_switch_r_sdp: [v=0"),
        format!("{UUID1} o=- 1234 5678 IN IP4 192.0.2.1"),
        format!("{UUID1} m=audio 10000 RTP/AVP 0]"),
        format!("{UUID1} variable_direction: [inbound]"),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    match entries[0].block.as_ref().expect("should have block") {
        Block::ChannelData { variables, .. } => {
            assert_eq!(variables.len(), 2);
            assert!(variables[0].1.starts_with("v=0\n"));
            assert_eq!(variables[1].0, "variable_direction");
        }
        other => panic!("expected ChannelData block, got {other:?}"),
    }
}

#[test]
fn duplicate_sdp_marker() {
    let lines = vec![
        full_line(UUID1, TS1, "Duplicate SDP"),
        "v=0".to_string(),
        "m=audio 10000 RTP/AVP 0".to_string(),
    ];
    let entries: Vec<_> = LogStream::new(lines.into_iter()).collect();
    assert_eq!(entries.len(), 1);
    match &entries[0].message_kind {
        MessageKind::SdpMarker { direction } => assert_eq!(*direction, SdpDirection::Unknown),
        other => panic!("expected SdpMarker, got {other:?}"),
    }
    assert!(entries[0].block.is_some());
}