Coverage Report

Created: 2026-02-14 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/rust/src/x509/time.rs
Line
Count
Source
1
/* Copyright (C) 2023 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17
18
use std::os::raw::c_char;
19
use time::macros::format_description;
20
21
/// Format a timestamp in an ISO format suitable for TLS logging.
22
///
23
/// Negative timestamp values are used for dates prior to 1970.
24
7.36k
pub fn format_timestamp(timestamp: i64) -> Result<String, time::error::Error> {
25
7.36k
    let format = format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]");
26
7.36k
    let ts = time::OffsetDateTime::from_unix_timestamp(timestamp)?;
27
7.36k
    let formatted = ts.format(&format)?;
28
7.36k
    Ok(formatted)
29
7.36k
}
30
31
/// Format a x509 ISO timestamp into the provided C buffer.
32
///
33
/// Returns false if an error occurs, otherwise true is returned if
34
/// the timestamp is properly formatted into the provided buffer.
35
///
36
/// # Safety
37
///
38
/// Access buffers from C that are expected to be valid.
39
#[no_mangle]
40
3.20k
pub unsafe extern "C" fn sc_x509_format_timestamp(
41
3.20k
    timestamp: i64, buf: *mut c_char, size: usize,
42
3.20k
) -> bool {
43
3.20k
    let timestamp = match format_timestamp(timestamp) {
44
3.20k
        Ok(ts) => ts,
45
0
        Err(_) => return false,
46
    };
47
3.20k
    crate::ffi::strings::copy_to_c_char(timestamp, buf, size)
48
3.20k
}
49
50
#[cfg(test)]
51
mod test {
52
    use super::*;
53
54
    #[test]
55
    fn test_format_timestamp() {
56
        assert_eq!("1969-12-31T00:00:00", format_timestamp(-86400).unwrap());
57
        assert_eq!("2038-12-31T00:10:03", format_timestamp(2177367003).unwrap());
58
    }
59
}