| 1 |
// Integration tests from https://tools.ietf.org/html/draft-josefsson-idn-test-vectors-00 |
| 2 |
extern crate stringprep; |
| 3 |
|
| 4 |
use stringprep::{nameprep, Error}; |
| 5 |
|
| 6 |
fn assert_prohibited_character<T>(result: Result<T, Error>) { |
| 7 |
assert!(result.is_err()); |
| 8 |
} |
| 9 |
|
| 10 |
fn assert_prohibited_bidirectional_text<T>(result: Result<T, Error>) { |
| 11 |
assert!(result.is_err()); |
| 12 |
} |
| 13 |
|
| 14 |
// Additional examples from http://josefsson.org/idn.php |
| 15 |
#[test] |
| 16 |
fn test_nameprep() { |
| 17 |
assert_eq!( |
| 18 |
"安室奈美恵-with-super-monkeys", |
| 19 |
nameprep("安室奈美恵-with-SUPER-MONKEYS").unwrap() |
| 20 |
); |
| 21 |
assert_eq!("미술", nameprep("미술").unwrap()); // Korean |
| 22 |
assert_eq!("ليهمابتكلموشعربي؟", nameprep("ليهمابتكلموشعربي؟").unwrap()); // Egyptian |
| 23 |
assert_eq!( |
| 24 |
"他们为什么不说中文", |
| 25 |
nameprep("他们为什么不说中文").unwrap() |
| 26 |
); // Chinese |
| 27 |
assert_eq!( |
| 28 |
"למההםפשוטלאמדבריםעברית", |
| 29 |
nameprep("למההםפשוטלאמדבריםעברית").unwrap() |
| 30 |
); // Hebrew |
| 31 |
assert_eq!( |
| 32 |
"почемужеонинеговорятпорусски", |
| 33 |
nameprep("почемужеонинеговорятпорусски").unwrap() |
| 34 |
); // Russian |
| 35 |
assert_eq!( |
| 36 |
"tạisaohọkhôngthểchỉnóitiếngviệt", |
| 37 |
nameprep("TạisaohọkhôngthểchỉnóitiếngViệt").unwrap() |
| 38 |
); // Vietnamese |
| 39 |
assert_eq!("ひとつ屋根の下2", nameprep("ひとつ屋根の下2").unwrap()); // Japanese |
| 40 |
assert_eq!( |
| 41 |
"pročprostěnemluvíčesky", |
| 42 |
nameprep("Pročprostěnemluvíčesky").unwrap() |
| 43 |
); // Czech |
| 44 |
assert_eq!( |
| 45 |
"यहलोगहिन्दीक्योंनहींबोलसकतेहैं", |
| 46 |
nameprep("यहलोगहिन्दीक्योंनहींबोलसकतेहैं").unwrap() |
| 47 |
); // Hindi |
| 48 |
assert_eq!("ພາສາລາວ", nameprep("ພາສາລາວ").unwrap()); // Lao |
| 49 |
assert_eq!("bonġusaħħa", nameprep("bonġusaħħa").unwrap()); // Maltese |
| 50 |
assert_eq!("ελληνικά", nameprep("ελληνικά").unwrap()); // Greek |
| 51 |
} |
| 52 |
|
| 53 |
// 4.1 Map to nothing |
| 54 |
#[test] |
| 55 |
fn should_map_to_nothing() { |
| 56 |
let input = |
| 57 |
"foo\u{00ad}\u{034f}\u{1806}\u{180b}bar\u{200b}\u{2060}baz\u{fe00}\u{fe08}\u{fe0f}\u{feff}"; |
| 58 |
assert_eq!("foobarbaz", nameprep(input).unwrap()); |
| 59 |
} |
| 60 |
|
| 61 |
// 4.2 Case folding ASCII U+0043 U+0041 U+0046 U+0045 |
| 62 |
#[test] |
| 63 |
fn should_case_fold_ascii() { |
| 64 |
assert_eq!("cafe", nameprep("CAFE").unwrap()); |
| 65 |
} |
| 66 |
|
| 67 |
// 4.3 Case folding 8bit U+00DF (german sharp s) |
| 68 |
#[test] |
| 69 |
fn should_case_fold_8bit() { |
| 70 |
assert_eq!("ss", nameprep("\u{00df}").unwrap()); |
| 71 |
} |
| 72 |
|
| 73 |
// 4.4 Case folding U+0130 (turkish capital I with dot) |
| 74 |
#[test] |
| 75 |
fn should_case_fold_16bit() { |
| 76 |
assert_eq!("\u{0069}\u{0307}", nameprep("\u{0130}").unwrap()); |
| 77 |
} |
| 78 |
|
| 79 |
// 4.5 Case folding multibyte U+0143 U+037A |
| 80 |
#[test] |
| 81 |
fn should_case_fold_multibyte() { |
| 82 |
let input = "\u{0143}\u{037a}"; |
| 83 |
let output = "\u{0144} \u{03b9}"; |
| 84 |
assert_eq!(output, nameprep(input).unwrap()); |
| 85 |
} |
| 86 |
|
| 87 |
// 4.6 Case folding U+2121 U+33C6 U+1D7BB |
| 88 |
#[test] |
| 89 |
fn should_case_fold() { |
| 90 |
let input = "\u{2121}\u{33c6}\u{1d7bb}"; |
| 91 |
let output = "telc\u{2215}\u{006b}\u{0067}\u{03c3}"; |
| 92 |
assert_eq!(output, nameprep(input).unwrap()); |
| 93 |
} |
| 94 |
|
| 95 |
// 4.7 Normalization of U+006a U+030c U+00A0 U+00AA |
| 96 |
#[test] |
| 97 |
fn should_normalize() { |
| 98 |
let input = "j\u{030c}\u{00a0}\u{00aa}"; |
| 99 |
let output = "\u{01f0} a"; |
| 100 |
assert_eq!(output, nameprep(input).unwrap()); |
| 101 |
} |
| 102 |
|
| 103 |
// 4.8 Case folding U+1FB7 and normalization |
| 104 |
#[test] |
| 105 |
fn should_case_fold_and_normalize() { |
| 106 |
let input = "\u{1fb7}"; |
| 107 |
let output = "\u{1fb6}\u{03b9}"; |
| 108 |
assert_eq!(output, nameprep(input).unwrap()); |
| 109 |
} |
| 110 |
|
| 111 |
// 4.9 Self-reverting case folding U+01F0 and normalization |
| 112 |
// 4.10 Self-reverting case folding U+0390 and normalization |
| 113 |
// 4.11 Self-reverting case folding U+03B0 and normalization |
| 114 |
// 4.12 Self-reverting case folding U+1E96 and normalization |
| 115 |
// 4.13 Self-reverting case folding U+1F56 and normalization |
| 116 |
#[test] |
| 117 |
fn should_revert_case_fold_and_normalization() { |
| 118 |
let inputs = ["\u{01f0}", "\u{0390}", "\u{03b0}", "\u{1e96}", "\u{1f56}"]; |
| 119 |
for input in inputs.iter() { |
| 120 |
assert_eq!(*input, nameprep(input).unwrap()); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// 4.14 ASCII space character U+0020 |
| 125 |
#[test] |
| 126 |
fn should_permit_ascii_space() { |
| 127 |
assert_eq!(" ", nameprep(" ").unwrap()); |
| 128 |
} |
| 129 |
|
| 130 |
// 4.15 Non-ASCII 8bit space character U+00A0 |
| 131 |
#[test] |
| 132 |
fn should_map_8bit_space() { |
| 133 |
assert_eq!(" ", nameprep("\u{00a0}").unwrap()); |
| 134 |
} |
| 135 |
|
| 136 |
// 4.16 Non-ASCII multibyte space character U+1680 |
| 137 |
#[test] |
| 138 |
fn should_prohibit_multibyte_space() { |
| 139 |
assert_prohibited_character(nameprep("\u{1680}")); |
| 140 |
} |
| 141 |
|
| 142 |
// 4.17 Non-ASCII multibyte space character U+2000 |
| 143 |
#[test] |
| 144 |
fn should_map_multibyte_space1() { |
| 145 |
assert_eq!(" ", nameprep("\u{2000}").unwrap()); |
| 146 |
} |
| 147 |
|
| 148 |
// 4.18 Zero Width Space U+200b |
| 149 |
#[test] |
| 150 |
fn should_drop_zero_width_space() { |
| 151 |
assert_eq!("", nameprep("\u{200b}").unwrap()); |
| 152 |
} |
| 153 |
|
| 154 |
// 4.19 Non-ASCII multibyte space character U+3000 |
| 155 |
#[test] |
| 156 |
fn should_map_multibyte_space2() { |
| 157 |
assert_eq!(" ", nameprep("\u{3000}").unwrap()); |
| 158 |
} |
| 159 |
|
| 160 |
// 4.20 ASCII control characters U+0010 U+007F |
| 161 |
#[test] |
| 162 |
fn should_permit_ascii_control() { |
| 163 |
assert_eq!("\u{0010}\u{007f}", nameprep("\u{0010}\u{007f}").unwrap()); |
| 164 |
} |
| 165 |
|
| 166 |
// 4.21 Non-ASCII 8bit control character U+0085 |
| 167 |
#[test] |
| 168 |
fn should_prohibit_8bit_control() { |
| 169 |
assert_prohibited_character(nameprep("\u{0085}")); |
| 170 |
} |
| 171 |
|
| 172 |
// 4.22 Non-ASCII multibyte control character U+180E |
| 173 |
#[test] |
| 174 |
fn should_prohibit_multibyte_control() { |
| 175 |
assert_prohibited_character(nameprep("\u{180e}")); |
| 176 |
} |
| 177 |
|
| 178 |
// 4.23 Zero Width No-Break Space U+FEFF |
| 179 |
#[test] |
| 180 |
fn should_drop_zero_width_no_break_space() { |
| 181 |
assert_eq!("", nameprep("\u{feff}").unwrap()); |
| 182 |
} |
| 183 |
|
| 184 |
// 4.24 Non-ASCII control character U+1D175 |
| 185 |
#[test] |
| 186 |
fn should_prohibit_non_ascii_control() { |
| 187 |
assert_prohibited_character(nameprep("\u{1d175}")); |
| 188 |
} |
| 189 |
|
| 190 |
// 4.25 Plane 0 private use character U+F123 |
| 191 |
#[test] |
| 192 |
fn should_prohibit_plane0_private_use() { |
| 193 |
assert_prohibited_character(nameprep("\u{f123}")); |
| 194 |
} |
| 195 |
|
| 196 |
// 4.26 Plane 15 private use character U+F1234 |
| 197 |
#[test] |
| 198 |
fn should_prohibit_plane15_private_use() { |
| 199 |
assert_prohibited_character(nameprep("\u{f1234}")); |
| 200 |
} |
| 201 |
|
| 202 |
// 4.27 Plane 16 private use character U+10F234 |
| 203 |
#[test] |
| 204 |
fn should_prohibit_plane16_private_use() { |
| 205 |
assert_prohibited_character(nameprep("\u{10f234}")); |
| 206 |
} |
| 207 |
|
| 208 |
// 4.28 Non-character code point U+8FFFE |
| 209 |
#[test] |
| 210 |
fn should_prohibit_non_character1() { |
| 211 |
assert_prohibited_character(nameprep("\u{8fffe}")); |
| 212 |
} |
| 213 |
|
| 214 |
// 4.29 Non-character code point U+10FFFF |
| 215 |
#[test] |
| 216 |
fn should_prohibit_non_character2() { |
| 217 |
assert_prohibited_character(nameprep("\u{10ffff}")); |
| 218 |
} |
| 219 |
|
| 220 |
// 4.31 Non-plain text character U+FFFD |
| 221 |
#[test] |
| 222 |
fn should_prohibit_non_plain_text() { |
| 223 |
assert_prohibited_character(nameprep("\u{fffd}")); |
| 224 |
} |
| 225 |
|
| 226 |
// 4.32 Ideographic description character U+2FF5 |
| 227 |
#[test] |
| 228 |
fn should_prohibit_ideographic_description() { |
| 229 |
assert_prohibited_character(nameprep("\u{2ff5}")); |
| 230 |
} |
| 231 |
|
| 232 |
// 4.33 Display property character U+0341 |
| 233 |
#[test] |
| 234 |
fn should_normalize_display_property() { |
| 235 |
assert_eq!("\u{0301}", nameprep("\u{0341}").unwrap()); |
| 236 |
} |
| 237 |
|
| 238 |
// 4.34 Left-to-right mark U+200E |
| 239 |
#[test] |
| 240 |
fn should_prohibit_left_to_right_mark() { |
| 241 |
assert_prohibited_character(nameprep("\u{200e}")); |
| 242 |
} |
| 243 |
|
| 244 |
// 4.35 Deprecated U+202A |
| 245 |
#[test] |
| 246 |
fn should_prohibit_deprecated() { |
| 247 |
assert_prohibited_character(nameprep("\u{202a}")); |
| 248 |
} |
| 249 |
|
| 250 |
// 4.36 Language tagging character U+E0001 |
| 251 |
#[test] |
| 252 |
fn should_prohibit_language_tagging1() { |
| 253 |
assert_prohibited_character(nameprep("\u{e0001}")); |
| 254 |
} |
| 255 |
|
| 256 |
// 4.37 Language tagging character U+E0042 |
| 257 |
#[test] |
| 258 |
fn should_prohibit_language_tagging2() { |
| 259 |
assert_prohibited_character(nameprep("\u{e0042}")); |
| 260 |
} |
| 261 |
|
| 262 |
// 4.38 Bidi: RandALCat character U+05BE and LCat characters |
| 263 |
#[test] |
| 264 |
fn should_prohibit_randalcat_with_lcat1() { |
| 265 |
assert_prohibited_bidirectional_text(nameprep("foo\u{05be}bar")); |
| 266 |
} |
| 267 |
|
| 268 |
// 4.39 Bidi: RandALCat character U+FD50 and LCat characters |
| 269 |
#[test] |
| 270 |
fn should_prohibit_randalcat_with_lcat2() { |
| 271 |
assert_prohibited_bidirectional_text(nameprep("foo\u{fd50}bar")); |
| 272 |
} |
| 273 |
|
| 274 |
// 4.40 Bidi: RandALCat character U+FB38 and LCat characters |
| 275 |
#[test] |
| 276 |
fn should_permit_randalcat1() { |
| 277 |
assert_eq!("foo \u{064e}bar", nameprep("foo\u{fe76}bar").unwrap()); |
| 278 |
} |
| 279 |
|
| 280 |
// 4.41 Bidi: RandALCat without trailing RandALCat U+0627 U+0031 |
| 281 |
#[test] |
| 282 |
fn should_prohibit_mixed_randalcat() { |
| 283 |
assert_prohibited_bidirectional_text(nameprep("\u{0672}\u{0031}")); |
| 284 |
} |
| 285 |
|
| 286 |
// 4.42 Bidi: RandALCat character U+0627 U+0031 U+0628 |
| 287 |
#[test] |
| 288 |
fn should_permit_randalcat2() { |
| 289 |
assert_eq!( |
| 290 |
"\u{0627}\u{0031}\u{0628}", |
| 291 |
nameprep("\u{0627}\u{0031}\u{0628}").unwrap() |
| 292 |
); |
| 293 |
} |
| 294 |
|
| 295 |
// 4.43 Unassigned code point U+E0002 |
| 296 |
#[test] |
| 297 |
fn should_prohibit_unassigned_code_point() { |
| 298 |
assert_prohibited_character(nameprep("\u{e0002}")); |
| 299 |
} |
| 300 |
|
| 301 |
// 4.44 Larger test (shrinking) |
| 302 |
#[test] |
| 303 |
fn should_shrink() { |
| 304 |
let input = "X\u{00ad}\u{00df}\u{0130}\u{2121}j\u{030c}\u{00a0}\u{00aa}\u{03b0}\u{2000}"; |
| 305 |
let output = "xssi\u{0307}tel\u{01f0} a\u{03b0}\u{0020}"; |
| 306 |
assert_eq!(output, nameprep(input).unwrap()); |
| 307 |
} |
| 308 |
|
| 309 |
// 4.45 Larger test (expanding) |
| 310 |
#[test] |
| 311 |
fn should_expand() { |
| 312 |
let input = "X\u{00df}\u{3316}\u{0130}\u{2121}\u{249f}\u{3300}"; |
| 313 |
let output = "xss\u{30ad}\u{30ed}\u{30e1}\u{30fc}\u{30c8}\u{30eb}\u{0069}\u{0307}\u{0074}\u{0065}\u{006c}\u{0028}\u{0064}\u{0029}\u{30a2}\u{30d1}\u{30fc}\u{30c8}"; |
| 314 |
assert_eq!(output, nameprep(input).unwrap()); |
| 315 |
} |