about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nix_daemon/de/int.rs
blob: eecf641cfe9943e2b49d05108f906c7850413a5a (plain) (blame)
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
use super::{Error, NixDeserialize, NixRead};

impl NixDeserialize for u64 {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        reader.try_read_number().await
    }
}

impl NixDeserialize for usize {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        if let Some(value) = reader.try_read_number().await? {
            value.try_into().map_err(R::Error::invalid_data).map(Some)
        } else {
            Ok(None)
        }
    }
}

impl NixDeserialize for bool {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        Ok(reader.try_read_number().await?.map(|v| v != 0))
    }
}
impl NixDeserialize for i64 {
    async fn try_deserialize<R>(reader: &mut R) -> Result<Option<Self>, R::Error>
    where
        R: ?Sized + NixRead + Send,
    {
        Ok(reader.try_read_number().await?.map(|v| v as i64))
    }
}

#[cfg(test)]
mod test {
    use hex_literal::hex;
    use rstest::rstest;
    use tokio_test::io::Builder;

    use crate::nix_daemon::de::{NixRead, NixReader};

    #[rstest]
    #[case::simple_false(false, &hex!("0000 0000 0000 0000"))]
    #[case::simple_true(true, &hex!("0100 0000 0000 0000"))]
    #[case::other_true(true, &hex!("1234 5600 0000 0000"))]
    #[case::max_true(true, &hex!("FFFF FFFF FFFF FFFF"))]
    #[tokio::test]
    async fn test_read_bool(#[case] expected: bool, #[case] data: &[u8]) {
        let mock = Builder::new().read(data).build();
        let mut reader = NixReader::new(mock);
        let actual: bool = reader.read_value().await.unwrap();
        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case::zero(0, &hex!("0000 0000 0000 0000"))]
    #[case::one(1, &hex!("0100 0000 0000 0000"))]
    #[case::other(0x563412, &hex!("1234 5600 0000 0000"))]
    #[case::max_value(u64::MAX, &hex!("FFFF FFFF FFFF FFFF"))]
    #[tokio::test]
    async fn test_read_u64(#[case] expected: u64, #[case] data: &[u8]) {
        let mock = Builder::new().read(data).build();
        let mut reader = NixReader::new(mock);
        let actual: u64 = reader.read_value().await.unwrap();
        assert_eq!(actual, expected);
    }

    #[rstest]
    #[case::zero(0, &hex!("0000 0000 0000 0000"))]
    #[case::one(1, &hex!("0100 0000 0000 0000"))]
    #[case::other(0x563412, &hex!("1234 5600 0000 0000"))]
    #[case::max_value(usize::MAX, &usize::MAX.to_le_bytes())]
    #[tokio::test]
    async fn test_read_usize(#[case] expected: usize, #[case] data: &[u8]) {
        let mock = Builder::new().read(data).build();
        let mut reader = NixReader::new(mock);
        let actual: usize = reader.read_value().await.unwrap();
        assert_eq!(actual, expected);
    }

    // FUTUREWORK: Test this on supported hardware
    #[tokio::test]
    #[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
    async fn test_read_usize_overflow() {
        let mock = Builder::new().read(&u64::MAX.to_le_bytes()).build();
        let mut reader = NixReader::new(mock);
        assert_eq!(
            std::io::ErrorKind::InvalidData,
            reader.read_value::<usize>().await.unwrap_err().kind()
        );
    }
}