File: saslprep_tests.rs

package info (click to toggle)
rust-stringprep 0.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,141 bytes parent folder | download
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
// Integration tests from https://github.com/reklatsmasters/saslprep (MIT License)
extern crate stringprep;

use stringprep::{saslprep, Error};

fn assert_prohibited_character<T>(result: Result<T, Error>) {
    assert!(result.is_err());
}

fn assert_prohibited_bidirectional_text<T>(result: Result<T, Error>) {
    assert!(result.is_err());
}

#[test]
fn should_work_with_latin_letters() {
    assert_eq!(saslprep("user").unwrap(), "user");
}

#[test]
fn should_preserve_case() {
    assert_eq!(saslprep("USER").unwrap(), "USER");
}

#[test]
fn should_remove_mapped_to_nothing() {
    assert_eq!(saslprep("I\u{00AD}X").unwrap(), "IX");
}

#[test]
fn should_replace_non_ascii_space() {
    assert_eq!(saslprep("a\u{00A0}b").unwrap(), "a\u{0020}b");
}

#[test]
fn should_normalize_as_nfkc() {
    assert_eq!(saslprep("\u{00AA}").unwrap(), "a");
    assert_eq!(saslprep("\u{2168}").unwrap(), "IX");
}

#[test]
fn should_not_allow_prohibited_characters() {
    // C.2.1 ASCII control characters
    assert_prohibited_character(saslprep("a\u{007F}b"));

    // C.2.2 Non-ASCII control characters
    assert_prohibited_character(saslprep("a\u{06DD}b"));

    // C.3 Private use
    assert_prohibited_character(saslprep("a\u{E000}b"));

    // C.4 Non-character code points
    assert_prohibited_character(saslprep("a\u{1FFFE}b"));

    // C.5 Surrogate codes
    // forbidden by rust

    // C.6 Inappropriate for plain text
    assert_prohibited_character(saslprep("a\u{FFF9}b"));

    // C.7 Inappropriate for canonical representation
    assert_prohibited_character(saslprep("a\u{2FF0}b"));

    // C.8 Change display properties or are deprecated
    assert_prohibited_character(saslprep("a\u{200E}b"));

    // C.9 Tagging characters
    assert_prohibited_character(saslprep("a\u{E0001}b"));
}

#[test]
fn randalcat_should_be_first_and_last() {
    assert_eq!(
        saslprep("\u{0627}\u{0031}\u{0628}").unwrap(),
        "\u{0627}\u{0031}\u{0628}"
    );
    assert_prohibited_bidirectional_text(saslprep("\u{0627}\u{0031}"));
}

#[test]
fn should_handle_unassigned_code_points() {
    assert_prohibited_character(saslprep("a\u{0487}"));
}