tremor-codec 0.13.0-rc.33

Tremor Script Interpreter
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
// Copyright 2020-2021, The Tremor Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The `syslog` codec supports marshalling the [IETF](https://datatracker.ietf.org/doc/html/rfc5424#section-6) and [BSD](https://www.ietf.org/rfc/rfc3164.txt) syslog formats.
//!
//! ## BSD Example
//!
//! A syslog message following the BSD format as follows:              
//!
//! ```text
//! <13>Jan  5 15:33:03 74794bfb6795 root[8539]: i am foobar
//! ```
//!
//! The equivalent representation as a tremor value
//!
//! ```json
//! {
//!   "severity": "notice",
//!   "facility": "user",
//!   "hostname": "74794bfb6795",
//!   "appname": "root",
//!   "msg": "i am foobar",
//!   "procid": 8539,
//!   "msgid": null,
//!   "protocol": "RFC3164",
//!   "protocol_version": null,
//!   "structured_data": null,
//!   "timestamp": 1609860783000000000
//! }
//! ```
//!
//! ## IETF example
//!
//! A syslog message following IETF standard as follows:
//!
//! ```text
//! <165>1 2021-03-18T20:30:00.123Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut=\"3\" eventSource=\"Application\" eventID=\"1011\"] BOMAn application event log entry..."
//! ```
//!
//! The equivalent representation as a tremor value.
//!
//! ```json
//! {
//!   "severity": "notice",
//!   "facility": "local4",
//!   "hostname": "mymachine.example.com",
//!   "appname": "evntsog",
//!   "msg": "BOMAn application event log entry...",
//!   "procid": null,
//!   "msgid": "ID47",
//!   "protocol": "RFC5424",
//!   "protocol_version": 1,
//!   "structured_data": {
//!               "exampleSDID@32473" :
//!               [
//!                 {"iut": "3"},
//!                 {"eventSource": "Application"},
//!                 {"eventID": "1011"}
//!               ]
//!             },
//!   "timestamp": 1616099400123000000
//! }
//! ```
//!
//! ## Considerations
//!
//! Malformed syslog messages are treated per `rfc3164` protocol semantics resulting in the entire string being
//! dumped into the `msg` of the result record.

use crate::prelude::*;
use chrono::{DateTime, Datelike, Offset, TimeZone, Utc};
use syslog_loose::{IncompleteDate, ProcId, Protocol, SyslogFacility, SyslogSeverity, Variant};

const DEFAULT_PRI: i32 = 13;

pub trait Now: Send + Sync + Clone {
    fn now(&self) -> DateTime<Utc>;
}

#[derive(Clone)]
pub struct UtcNow {}
impl Now for UtcNow {
    fn now(&self) -> DateTime<Utc> {
        Utc::now()
    }
}

#[derive(Clone)]
pub struct Syslog<N>
where
    N: Now,
{
    now: N,
}

impl Syslog<UtcNow> {
    /// construct a Syslog codec
    /// that adds the current time in UTC during encoding if none was provided in the event payload
    pub fn utcnow() -> Self {
        Self { now: UtcNow {} }
    }
}

impl<N> Syslog<N>
where
    N: Now,
{
    /// encode structured data `sd` into `result`
    fn encode_sd(sd: &Value, result: &mut Vec<String>) -> Result<()> {
        let sd = sd.as_object().ok_or_else(|| {
            Error::from(ErrorKind::InvalidSyslogData(
                "Invalid structured data: structured data not an object",
            ))
        })?;
        let mut elem = String::with_capacity(16);
        for (id, params) in sd {
            elem.push('[');
            elem.push_str(id);
            let params = params.as_array().ok_or_else(|| {
                Error::from(ErrorKind::InvalidSyslogData(
                    "Invalid structured data: params not an array of objects",
                ))
            })?;
            for key_value in params.iter() {
                let kv_map = key_value.as_object().ok_or_else(|| {
                    Error::from(ErrorKind::InvalidSyslogData(
                        "Invalid structured data: param's key value pair not an object",
                    ))
                })?;
                for (k, v) in kv_map {
                    let value = v.as_str().ok_or_else(|| {
                        Error::from(ErrorKind::InvalidSyslogData(
                            "Invalid structured data: param's key value pair not an object",
                        ))
                    })?;
                    elem.push(' ');
                    elem.push_str(k);
                    elem.push('=');
                    elem.push('"');
                    elem.push_str(value);
                    elem.push('"');
                }
            }
            elem.push(']');
        }
        result.push(elem);

        Ok(())
    }

    fn encode_rfc3164(&self, data: &Value) -> Result<Vec<String>> {
        let mut result = Vec::with_capacity(4);
        let f = data.get_str("facility");
        let s = data.get_str("severity");

        let pri = match f.zip(s) {
            Some((f, s)) => compose_pri(to_facility(f)?, to_severity(s)?),
            None => DEFAULT_PRI, // https://datatracker.ietf.org/doc/html/rfc3164#section-4.3.3
        };
        let datetime = data
            .get_i64("timestamp")
            .map_or_else(|| self.now.now(), |t| Utc.timestamp_nanos(t));
        result.push(format!("<{pri}>{}", datetime.format("%b %e %H:%M:%S")));

        result.push(
            data.get_str("hostname")
                .map_or_else(Self::nil, ToOwned::to_owned),
        );
        result.push(data.get_str("appname").map_or_else(
            || String::from(":"),
            |appname| {
                data.get_str("procid").map_or_else(
                    || format!("{appname}:"),
                    |procid| format!("{appname}[{procid}]:"),
                )
            },
        ));

        // structured data shouldnt pop up in this format, but syslog_loose parses it anyways
        if let Some(sd) = data.get("structured_data") {
            Self::encode_sd(sd, &mut result)?;
        }
        if let Some(msg) = data.get_str("msg") {
            result.push(msg.to_owned());
        }
        Ok(result)
    }

    fn nil() -> String {
        String::from("-")
    }

    fn encode_rfc5424(data: &Value, version: u32) -> Result<Vec<String>> {
        let mut result = Vec::with_capacity(10); // reserving 3 slots for structured data
        let f = data
            .get_str("facility")
            .ok_or_else(|| Error::from(ErrorKind::InvalidSyslogData("facility missing")))?;
        let s = data
            .get_str("severity")
            .ok_or_else(|| Error::from(ErrorKind::InvalidSyslogData("severity missing")))?;
        result.push(format!(
            "<{}>{}",
            compose_pri(to_facility(f)?, to_severity(s)?),
            version
        ));

        result.push(data.get_i64("timestamp").map_or_else(Self::nil, |t| {
            let datetime = Utc.timestamp_nanos(t);
            datetime.to_rfc3339()
        }));

        result.push(
            data.get_str("hostname")
                .map_or_else(Self::nil, ToOwned::to_owned),
        );
        result.push(
            data.get_str("appname")
                .map_or_else(Self::nil, ToOwned::to_owned),
        );
        result.push(
            data.get_str("procid")
                .map_or_else(Self::nil, ToOwned::to_owned),
        );
        result.push(
            data.get_str("msgid")
                .map_or_else(Self::nil, ToOwned::to_owned),
        );
        if let Some(sd) = data.get("structured_data") {
            Self::encode_sd(sd, &mut result)?;
        } else {
            result.push(Self::nil());
        }

        if let Some(msg) = data.get_str("msg") {
            result.push(msg.to_owned());
        }
        Ok(result)
    }
}

#[async_trait::async_trait]
impl<N> Codec for Syslog<N>
where
    N: Now + 'static,
{
    fn name(&self) -> &str {
        "syslog"
    }

    async fn decode<'input>(
        &mut self,
        data: &'input mut [u8],
        _ingest_ns: u64,
        meta: Value<'input>,
    ) -> Result<Option<(Value<'input>, Value<'input>)>> {
        let line: &str = std::str::from_utf8(data)?;
        let parsed = syslog_loose::parse_message_with_year_tz(
            line,
            resolve_year,
            Some(chrono::offset::Utc.fix()),
            Variant::Either,
        );

        let mut decoded = Value::object_with_capacity(11);
        if let Some(hostname) = parsed.hostname {
            decoded.try_insert("hostname", hostname);
        }
        if let Some(severity) = parsed.severity {
            decoded.try_insert("severity", severity.as_str());
        }
        if let Some(facility) = parsed.facility {
            decoded.try_insert("facility", facility.as_str());
        }
        if let Some(timestamp) = parsed.timestamp.and_then(|t| t.timestamp_nanos_opt()) {
            decoded.try_insert("timestamp", timestamp);
        }
        decoded.try_insert(
            "protocol",
            match parsed.protocol {
                Protocol::RFC3164 => "RFC3164",
                Protocol::RFC5424(_) => "RFC5424",
            },
        );
        if let Protocol::RFC5424(version) = parsed.protocol {
            decoded.try_insert("protocol_version", version);
        }
        if let Some(appname) = parsed.appname {
            decoded.try_insert("appname", appname);
        }
        if let Some(msgid) = parsed.msgid {
            decoded.try_insert("msgid", msgid);
        }

        if !parsed.structured_data.is_empty() {
            let mut temp = Value::object_with_capacity(parsed.structured_data.len());
            for element in parsed.structured_data {
                let mut e = Vec::with_capacity(element.params.len());
                for (name, value) in element.params {
                    let mut param = Value::object_with_capacity(1);
                    param.try_insert(name, value);
                    e.push(param);
                }
                temp.try_insert(element.id, Value::from(e));
            }
            decoded.try_insert("structured_data", temp);
        }

        if let Some(procid) = parsed.procid {
            let value: Value = match procid {
                ProcId::PID(pid) => pid.to_string().into(),
                ProcId::Name(name) => name.to_string().into(),
            };
            decoded.try_insert("procid", value);
        }
        if !parsed.msg.is_empty() {
            decoded.try_insert("msg", Value::from(parsed.msg));
        }

        Ok(Some((decoded, meta)))
    }

    async fn encode(&mut self, data: &Value, _meta: &Value) -> Result<Vec<u8>> {
        let protocol = match (data.get_str("protocol"), data.get_u32("protocol_version")) {
            (Some("RFC3164"), _) => Ok(Protocol::RFC3164),
            (Some("RFC5424"), Some(version)) => Ok(Protocol::RFC5424(version)),
            (Some("RFC5424"), None) => Err("Missing protocol version"),
            (None, Some(_)) => Err("Missing protocol type"),
            (Some(&_), _) => Err("Invalid protocol type"),
            (None, None) => Ok(Protocol::RFC5424(1_u32)),
        }
        .map_err(ErrorKind::InvalidSyslogData)?;
        let result = match protocol {
            Protocol::RFC3164 => self.encode_rfc3164(data)?,
            Protocol::RFC5424(version) => Self::encode_rfc5424(data, version)?,
        };

        Ok(result.join(" ").as_bytes().to_vec())
    }

    fn boxed_clone(&self) -> Box<dyn Codec> {
        Box::new(self.clone())
    }
}

// Function used to resolve the year for syslog messages that don't include the year.
// If the current month is January, and the syslog message is for December, it will take the previous year.
// Otherwise, take the current year.
fn resolve_year((month, _date, _hour, _min, _sec): IncompleteDate) -> i32 {
    let now = Utc::now();
    if now.month() == 1 && month == 12 {
        now.year() - 1
    } else {
        now.year()
    }
}

// Convert string to its respective syslog severity representation.
fn to_severity(s: &str) -> Result<SyslogSeverity> {
    match s {
        "emerg" => Ok(SyslogSeverity::SEV_EMERG),
        "alert" => Ok(SyslogSeverity::SEV_ALERT),
        "crit" => Ok(SyslogSeverity::SEV_CRIT),
        "err" => Ok(SyslogSeverity::SEV_ERR),
        "warning" => Ok(SyslogSeverity::SEV_WARNING),
        "notice" => Ok(SyslogSeverity::SEV_NOTICE),
        "info" => Ok(SyslogSeverity::SEV_INFO),
        "debug" => Ok(SyslogSeverity::SEV_DEBUG),
        _ => Err(ErrorKind::InvalidSyslogData("invalid severity").into()),
    }
}

// Convert string to its respective syslog facility representation.
fn to_facility(s: &str) -> Result<SyslogFacility> {
    match s {
        "kern" => Ok(SyslogFacility::LOG_KERN),
        "user" => Ok(SyslogFacility::LOG_USER),
        "mail" => Ok(SyslogFacility::LOG_MAIL),
        "daemon" => Ok(SyslogFacility::LOG_DAEMON),
        "auth" => Ok(SyslogFacility::LOG_AUTH),
        "syslog" => Ok(SyslogFacility::LOG_SYSLOG),
        "lpr" => Ok(SyslogFacility::LOG_LPR),
        "news" => Ok(SyslogFacility::LOG_NEWS),
        "uucp" => Ok(SyslogFacility::LOG_UUCP),
        "cron" => Ok(SyslogFacility::LOG_CRON),
        "authpriv" => Ok(SyslogFacility::LOG_AUTHPRIV),
        "ftp" => Ok(SyslogFacility::LOG_FTP),
        "ntp" => Ok(SyslogFacility::LOG_NTP),
        "audit" => Ok(SyslogFacility::LOG_AUDIT),
        "alert" => Ok(SyslogFacility::LOG_ALERT),
        "clockd" => Ok(SyslogFacility::LOG_CLOCKD),
        "local0" => Ok(SyslogFacility::LOG_LOCAL0),
        "local1" => Ok(SyslogFacility::LOG_LOCAL1),
        "local2" => Ok(SyslogFacility::LOG_LOCAL2),
        "local3" => Ok(SyslogFacility::LOG_LOCAL3),
        "local4" => Ok(SyslogFacility::LOG_LOCAL4),
        "local5" => Ok(SyslogFacility::LOG_LOCAL5),
        "local6" => Ok(SyslogFacility::LOG_LOCAL6),
        "local7" => Ok(SyslogFacility::LOG_LOCAL7),
        _ => Err(ErrorKind::InvalidSyslogData("invalid facility").into()),
    }
}

// Compose the facility and severity as a single integer.
fn compose_pri(facility: SyslogFacility, severity: SyslogSeverity) -> i32 {
    ((facility as i32) << 3) + (severity as i32)
}

#[cfg(test)]
mod test {
    use super::*;
    use chrono::LocalResult;
    use futures::executor::block_on;
    use test_case::test_case;
    use tremor_value::literal;

    #[derive(Clone)]
    struct TestNow {}
    impl Now for TestNow {
        fn now(&self) -> DateTime<Utc> {
            if let LocalResult::Single(utc) = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0) {
                utc
            } else {
                panic!("Literal epoch date should be valid.");
            }
        }
    }

    fn test_codec() -> Syslog<TestNow> {
        Syslog { now: TestNow {} }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_syslog_codec() -> Result<()> {
        let mut s = b"<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut=\"3\" eventSource= \"Application\" eventID=\"1011\"][examplePriority@32473 class=\"high\"] BOMAn application event log entry...".to_vec();

        let mut codec = test_codec();
        let decoded = codec
            .decode(s.as_mut_slice(), 0, Value::object())
            .await?
            .unwrap_or_default()
            .0;
        let mut a = codec.encode(&decoded, &Value::const_null()).await?;
        let b = codec
            .decode(a.as_mut_slice(), 0, Value::object())
            .await?
            .unwrap_or_default()
            .0;
        assert_eq!(decoded, b);
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_decode_empty() -> Result<()> {
        let mut s = b"<191>1 2021-03-18T20:30:00.123Z - - - - - message".to_vec();
        let mut codec = test_codec();
        let decoded = codec
            .decode(s.as_mut_slice(), 0, Value::object())
            .await?
            .unwrap_or_default()
            .0;
        let expected = literal!({
            "severity": "debug",
            "facility": "local7",
            "msg": "message",
            "protocol": "RFC5424",
            "protocol_version": 1,
            "timestamp": 1_616_099_400_123_000_000_u64
        });
        assert_eq!(
            tremor_value::utils::sorted_serialize(&expected)?,
            tremor_value::utils::sorted_serialize(&decoded)?
        );
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn decode_invalid_message() -> Result<()> {
        let mut s = b"an invalid message".to_vec();
        let mut codec = test_codec();
        let decoded = codec
            .decode(s.as_mut_slice(), 0, Value::object())
            .await?
            .unwrap_or_default()
            .0;
        let expected = literal!({
            "msg": "an invalid message",
            "protocol": "RFC3164",
        });
        assert_eq!(
            tremor_value::utils::sorted_serialize(&expected)?,
            tremor_value::utils::sorted_serialize(&decoded)?
        );
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn encode_empty_rfc5424() -> Result<()> {
        let mut codec = test_codec();
        let msg = literal!({
            "severity": "notice",
            "facility": "local4",
            "msg": "test message",
            "protocol": "RFC5424",
            "protocol_version": 1,
            "timestamp": 0_u64
        });
        let mut encoded = codec.encode(&msg, &Value::const_null()).await?;
        let expected = "<165>1 1970-01-01T00:00:00+00:00 - - - - - test message";
        assert_eq!(std::str::from_utf8(&encoded)?, expected);
        let decoded = codec
            .decode(&mut encoded, 0, Value::object())
            .await?
            .unwrap_or_default()
            .0;
        assert_eq!(msg, decoded);

        Ok(())
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn encode_invalid_facility() {
        let mut codec = test_codec();
        let msg = literal!({
            "severity": "notice",
            "facility": "snot",
            "msg": "test message",
            "protocol": "RFC5424",
            "protocol_version": 1,
            "timestamp": 0_u64
        });
        assert!(codec.encode(&msg, &Value::const_null()).await.is_err());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn encode_invalid_severity() {
        let mut codec = test_codec();
        let msg = literal!({
            "severity": "snot",
            "facility": "local4",
            "msg": "test message",
            "protocol": "RFC5424",
            "protocol_version": 1,
            "timestamp": 0_u64
        });
        assert!(codec.encode(&msg, &Value::const_null()).await.is_err());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn encode_rfc5424_missing_version() {
        let mut codec = test_codec();
        let msg = literal!({
            "severity": "debug",
            "facility": "local5",
            "msg": "test message",
            "protocol": "RFC5424",
            "timestamp": 0_u64
        });
        assert!(codec.encode(&msg, &Value::const_null()).await.is_err());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn encode_missing_protocol() {
        let mut codec = test_codec();
        let msg = literal!({
            "severity": "notice",
            "facility": "local6",
            "msg": "test message",
            "protocol_version": 1,
            "timestamp": 0_u64
        });
        assert!(codec.encode(&msg, &Value::const_null()).await.is_err());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn encode_invalid_protocol() {
        let mut codec = test_codec();
        let msg = literal!({
            "severity": "notice",
            "facility": "local7",
            "msg": "test message",
            "protocol": "snot",
            "timestamp": 0_u64
        });
        assert!(codec.encode(&msg, &Value::const_null()).await.is_err());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn encode_empty_rfc3164() -> Result<()> {
        let mut codec = test_codec();
        let msg = Value::from(simd_json::json!({
            "severity": "notice",
            "facility": "local4",
            "msg": "test message",
            "protocol": "RFC3164"
        }));
        let encoded = codec.encode(&msg, &Value::const_null()).await?;
        let expected = "<165>Jan  1 00:00:00 - : test message";
        assert_eq!(std::str::from_utf8(&encoded)?, expected);
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_incorrect_sd() -> Result<()> {
        let mut s =
            b"<13>1 2021-03-18T20:30:00.123Z 74794bfb6795 root 8449 - [incorrect x] message"
                .to_vec();
        let mut codec = test_codec();
        let decoded = codec
            .decode(s.as_mut_slice(), 0, Value::object())
            .await?
            .unwrap_or_default()
            .0;
        let expected = literal!({
            "hostname": "74794bfb6795",
            "severity": "notice",
            "facility": "user",
            "appname": "root",
            "msg": "message",
            "procid": "8449",
            "protocol": "RFC5424",
            "protocol_version": 1,
            "timestamp": 1_616_099_400_123_000_000_u64
        });
        assert_eq!(
            tremor_value::utils::sorted_serialize(&expected)?,
            tremor_value::utils::sorted_serialize(&decoded)?
        );
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_invalid_sd_3164() -> Result<()> {
        let mut s: Vec<u8> = r#"<46>Jan  5 15:33:03 plertrood-ThinkPad-X220 rsyslogd:  [software="rsyslogd" swVersion="8.32.0"] message"#.as_bytes().to_vec();
        let mut codec = test_codec();
        let decoded = codec
            .decode(s.as_mut_slice(), 0, Value::object())
            .await?
            .unwrap_or_default()
            .0;
        // we use the current year for this shitty old format
        // to not have to change this test every year, we use the current one for the expected timestamp
        let year = chrono::Utc::now().year();
        let timestamp =
            chrono::DateTime::parse_from_rfc3339(format!("{year}-01-05T15:33:03Z").as_str())?;
        let expected = literal!({
            "hostname": "plertrood-ThinkPad-X220",
            "severity": "info",
            "facility": "syslog",
            "appname": "rsyslogd",
            "msg": "[software=\"rsyslogd\" swVersion=\"8.32.0\"] message",
            "protocol": "RFC3164",
            "timestamp": timestamp.timestamp_nanos_opt().unwrap_or_default()
        });
        assert_eq!(
            tremor_value::utils::sorted_serialize(&expected)?,
            tremor_value::utils::sorted_serialize(&decoded)?
        );
        Ok(())
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn errors() -> Result<()> {
        let mut o = Value::object();
        let mut codec = test_codec();
        assert_eq!(
            codec
                .encode(&o, &Value::const_null())
                .await
                .err()
                .map(|e| e.to_string())
                .unwrap_or_default(),
            "Invalid Syslog Protocol data: facility missing"
        );

        o.insert("facility", "cron")?;
        assert_eq!(
            codec
                .encode(&o, &Value::const_null())
                .await
                .err()
                .map(|e| e.to_string())
                .unwrap_or_default(),
            "Invalid Syslog Protocol data: severity missing"
        );

        o.insert("severity", "info")?;
        o.insert("structured_data", "sd")?;
        assert_eq!(
            codec
                .encode(&o, &Value::const_null())
                .await
                .err()
                .map(|e| e.to_string())
                .unwrap_or_default(),
            "Invalid Syslog Protocol data: Invalid structured data: structured data not an object"
        );
        Ok(())
    }

    #[test_case("<34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8", None => Ok(()); "Example 1")]
    // error during encoding step, as important data is missing
    #[test_case("Use the BFG!", Some("<13>Jan  1 00:00:00 - : Use the BFG!") => Ok(()); "Example 2")]
    //
    #[test_case("<165>Aug 24 05:34:00 CST 1987 mymachine myproc[10]: %% It's time to make the do-nuts.  %%  Ingredients: Mix=OK, Jelly=OK # Devices: Mixer=OK, Jelly_Injector=OK, Frier=OK # Transport:Conveyer1=OK, Conveyer2=OK # %%", Some("<165>Aug 24 05:34:00 CST 1987: mymachine myproc[10]: %% It's time to make the do-nuts.  %%  Ingredients: Mix=OK, Jelly=OK # Devices: Mixer=OK, Jelly_Injector=OK, Frier=OK # Transport:Conveyer1=OK, Conveyer2=OK # %%") => Ok(()); "Example 3")]
    #[test_case("<0>1990 Oct 22 10:52:01 TZ-6 scapegoat.dmz.example.org 10.1.2.3 sched[0]: That's All Folks!", Some("<13>Jan  1 00:00:00 - : <0>1990 Oct 22 10:52:01 TZ-6 scapegoat.dmz.example.org 10.1.2.3 sched[0]: That's All Folks!") => Ok(()); "Example 4")]
    fn rfc3164_examples(sample: &'static str, expected: Option<&'static str>) -> Result<()> {
        let mut codec = test_codec();
        let mut vec = sample.as_bytes().to_vec();
        let decoded = block_on(codec.decode(&mut vec, 0, Value::object()))?
            .unwrap_or_default()
            .0;
        let a = block_on(codec.encode(&decoded, &Value::const_null()))?;
        if let Some(expected) = expected {
            // compare against expected output
            assert_eq!(expected, std::str::from_utf8(&a)?);
        } else {
            // compare against sample
            assert_eq!(sample, std::str::from_utf8(&a)?);
        }
        Ok(())
    }

    // date formatting is a bit different
    #[test_case("<34>1 2003-10-11T22:14:15.003Z mymachine.example.com su - ID47 - \u{FEFF}'su root' failed for lonvick on /dev/pts/8",
           "<34>1 2003-10-11T22:14:15.003+00:00 mymachine.example.com su - ID47 - \u{feff}'su root' failed for lonvick on /dev/pts/8" => Ok(()); "Example 1")]
    // we always parse to UTC date - so encoding a decoded msg will always give UTC
    #[test_case("<165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1 myproc 8710 - - %% It's time to make the do-nuts.",
           "<165>1 2003-08-24T12:14:15.000003+00:00 192.0.2.1 myproc 8710 - - %% It's time to make the do-nuts." => Ok(()); "Example 2")]
    #[test_case("<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut=\"3\" eventSource=\"Application\" eventID=\"1011\"] \u{FEFF}An application event log entry...",
           "<165>1 2003-10-11T22:14:15.003+00:00 mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut=\"3\" eventSource=\"Application\" eventID=\"1011\"] \u{feff}An application event log entry..." => Ok(()); "Example 3")]
    #[test_case(r#"<165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"][examplePriority@32473 class="high"]"#,
           r#"<165>1 2003-10-11T22:14:15.003+00:00 mymachine.example.com evntslog - ID47 [exampleSDID@32473 iut="3" eventSource="Application" eventID="1011"][examplePriority@32473 class="high"]"# => Ok(()); "Example 4")]
    fn rfc5424_examples(sample: &'static str, expected: &'static str) -> Result<()> {
        let mut codec = test_codec();
        let mut vec = sample.as_bytes().to_vec();
        let decoded = block_on(codec.decode(&mut vec, 0, Value::object()))?
            .unwrap_or_default()
            .0;
        let a = block_on(codec.encode(&decoded, &Value::const_null()))?;
        assert_eq!(expected, std::str::from_utf8(&a)?);
        Ok(())
    }
}