summaryrefslogtreecommitdiffstats
path: root/third_party/rust/cookie/src/secure/macros.rs
blob: dfbddcf928c37201b7dea0dcebfccadc69666d4a (plain)
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
#[cfg(test)]
macro_rules! assert_simple_behaviour {
    ($clear:expr, $secure:expr) => ({
        assert_eq!($clear.iter().count(), 0);

        $secure.add(Cookie::new("name", "val"));
        assert_eq!($clear.iter().count(), 1);
        assert_eq!($secure.get("name").unwrap().value(), "val");
        assert_ne!($clear.get("name").unwrap().value(), "val");

        $secure.add(Cookie::new("another", "two"));
        assert_eq!($clear.iter().count(), 2);

        $clear.remove(Cookie::named("another"));
        assert_eq!($clear.iter().count(), 1);

        $secure.remove(Cookie::named("name"));
        assert_eq!($clear.iter().count(), 0);
    })
}

#[cfg(test)]
macro_rules! assert_secure_behaviour {
    ($clear:expr, $secure:expr) => ({
        $secure.add(Cookie::new("secure", "secure"));
        assert!($clear.get("secure").unwrap().value() != "secure");
        assert!($secure.get("secure").unwrap().value() == "secure");

        let mut cookie = $clear.get("secure").unwrap().clone();
        let new_val = format!("{}l", cookie.value());
        cookie.set_value(new_val);
        $clear.add(cookie);
        assert!($secure.get("secure").is_none());

        let mut cookie = $clear.get("secure").unwrap().clone();
        cookie.set_value("foobar");
        $clear.add(cookie);
        assert!($secure.get("secure").is_none());
    })
}

// This is courtesty of `static_assertions`. That library is Copyright (c) 2017
// Nikolai Vazquez. See https://github.com/nvzqz/static-assertions-rs for more.
macro_rules! const_assert {
    ($x:expr $(,)?) => {
        #[allow(unknown_lints, clippy::eq_op)]
        const _: [(); 0 - !{ const ASSERT: bool = $x; ASSERT } as usize] = [];
    };
}