1
//! Code for parsing the relay specific config options.
2

            
3
use tor_netdoc::types::Nickname;
4

            
5
/// Return the default relay nickname. Again, taken from C-tor.
6
pub(crate) fn default_nickname() -> Nickname {
7
    "Unnamed".parse().expect("Default nickname is invalid")
8
}
9

            
10
#[cfg(test)]
11
mod test {
12
    // @@ begin test lint list maintained by maint/add_warning @@
13
    #![allow(clippy::bool_assert_comparison)]
14
    #![allow(clippy::clone_on_copy)]
15
    #![allow(clippy::dbg_macro)]
16
    #![allow(clippy::mixed_attributes_style)]
17
    #![allow(clippy::print_stderr)]
18
    #![allow(clippy::print_stdout)]
19
    #![allow(clippy::single_char_pattern)]
20
    #![allow(clippy::unwrap_used)]
21
    #![allow(clippy::unchecked_time_subtraction)]
22
    #![allow(clippy::useless_vec)]
23
    #![allow(clippy::needless_pass_by_value)]
24
    #![allow(clippy::string_slice)] // See arti#2571
25
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
26

            
27
    use super::*;
28
    use tor_netdoc::types::ContactInfo;
29

            
30
    #[test]
31
    fn contact_valid() {
32
        for info in [
33
            "Arti relay team",
34
            "8096R/13371337 Rosa Park<rosa.park@awesome.com>",
35
            "trailing whitespace is fine  ",
36
            "Du français avec accent ééé, on aime!",
37
        ] {
38
            let contact: ContactInfo = info.parse().unwrap();
39
            assert_eq!(contact.to_string(), info);
40
        }
41
    }
42

            
43
    #[test]
44
    fn contact_invalid() {
45
        for info in [
46
            " leading whitespace", // Leading whitespace
47
            "\tleading tab",       // Same but with a tab
48
            "two\nlines",          // New line
49
            "trailing newline\n",  // Trailing new line
50
        ] {
51
            assert!(info.parse::<ContactInfo>().is_err());
52
        }
53
    }
54

            
55
    #[test]
56
    fn nickname_valid() {
57
        // Some basic names that are all valid.
58
        for name in ["Unnamed", "a", "42", "Op3nB0rd3rs", "A".repeat(19).as_str()] {
59
            let nickname: Nickname = name.parse().unwrap();
60
            assert_eq!(nickname.to_string(), name);
61
        }
62
    }
63

            
64
    #[test]
65
    fn nickname_invalid() {
66
        for name in [
67
            "",                      // Too short
68
            "A".repeat(20).as_str(), // Too long
69
            "with space",            // Windows spaces...
70
            "under_score",           // Non alphanumeric
71
            "dash-ing",              // Non alphanumeric
72
            "unicödé",               // The French... :P
73
        ] {
74
            assert!(name.parse::<Nickname>().is_err());
75
        }
76
    }
77
}