unitbis.rs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. use idna::uts46::AsciiDenyList;
  2. use idna::uts46::DnsLength;
  3. use idna::uts46::Hyphens;
  4. /// https://github.com/servo/rust-url/issues/373
  5. #[test]
  6. fn test_punycode_prefix_with_length_check() {
  7. let config = idna::uts46::Uts46::new();
  8. assert!(config
  9. .to_ascii(
  10. b"xn--",
  11. AsciiDenyList::STD3,
  12. Hyphens::Check,
  13. DnsLength::Verify
  14. )
  15. .is_err());
  16. assert!(config
  17. .to_ascii(
  18. b"xn---",
  19. AsciiDenyList::STD3,
  20. Hyphens::Check,
  21. DnsLength::Verify
  22. )
  23. .is_err());
  24. assert!(config
  25. .to_ascii(
  26. b"xn-----",
  27. AsciiDenyList::STD3,
  28. Hyphens::Check,
  29. DnsLength::Verify,
  30. )
  31. .is_err());
  32. assert!(config
  33. .to_ascii(
  34. b"xn--.",
  35. AsciiDenyList::STD3,
  36. Hyphens::Check,
  37. DnsLength::Verify
  38. )
  39. .is_err());
  40. assert!(config
  41. .to_ascii(
  42. b"xn--...",
  43. AsciiDenyList::STD3,
  44. Hyphens::Check,
  45. DnsLength::Verify,
  46. )
  47. .is_err());
  48. assert!(config
  49. .to_ascii(
  50. b".xn--",
  51. AsciiDenyList::STD3,
  52. Hyphens::Check,
  53. DnsLength::Verify,
  54. )
  55. .is_err());
  56. assert!(config
  57. .to_ascii(
  58. b"...xn--",
  59. AsciiDenyList::STD3,
  60. Hyphens::Check,
  61. DnsLength::Verify,
  62. )
  63. .is_err());
  64. assert!(config
  65. .to_ascii(
  66. b"xn--.xn--",
  67. AsciiDenyList::STD3,
  68. Hyphens::Check,
  69. DnsLength::Verify,
  70. )
  71. .is_err());
  72. assert!(config
  73. .to_ascii(
  74. b"xn--.example.org",
  75. AsciiDenyList::STD3,
  76. Hyphens::Check,
  77. DnsLength::Verify,
  78. )
  79. .is_err());
  80. }
  81. /// https://github.com/servo/rust-url/issues/373
  82. #[test]
  83. fn test_punycode_prefix_without_length_check() {
  84. let config = idna::uts46::Uts46::new();
  85. assert!(config
  86. .to_ascii(
  87. b"xn--",
  88. AsciiDenyList::URL,
  89. Hyphens::Allow,
  90. DnsLength::Ignore
  91. )
  92. .is_err());
  93. assert!(config
  94. .to_ascii(
  95. b"xn---",
  96. AsciiDenyList::URL,
  97. Hyphens::Allow,
  98. DnsLength::Ignore
  99. )
  100. .is_err());
  101. assert!(config
  102. .to_ascii(
  103. b"xn-----",
  104. AsciiDenyList::URL,
  105. Hyphens::Allow,
  106. DnsLength::Ignore
  107. )
  108. .is_err());
  109. assert!(config
  110. .to_ascii(
  111. b"xn--.",
  112. AsciiDenyList::URL,
  113. Hyphens::Allow,
  114. DnsLength::Ignore
  115. )
  116. .is_err());
  117. assert!(config
  118. .to_ascii(
  119. b"xn--...",
  120. AsciiDenyList::URL,
  121. Hyphens::Allow,
  122. DnsLength::Ignore
  123. )
  124. .is_err());
  125. assert!(config
  126. .to_ascii(
  127. b".xn--",
  128. AsciiDenyList::URL,
  129. Hyphens::Allow,
  130. DnsLength::Ignore
  131. )
  132. .is_err());
  133. assert!(config
  134. .to_ascii(
  135. b"...xn--",
  136. AsciiDenyList::URL,
  137. Hyphens::Allow,
  138. DnsLength::Ignore
  139. )
  140. .is_err());
  141. assert!(config
  142. .to_ascii(
  143. b"xn--.xn--",
  144. AsciiDenyList::URL,
  145. Hyphens::Allow,
  146. DnsLength::Ignore
  147. )
  148. .is_err());
  149. assert!(config
  150. .to_ascii(
  151. b"xn--.example.org",
  152. AsciiDenyList::URL,
  153. Hyphens::Allow,
  154. DnsLength::Ignore
  155. )
  156. .is_err());
  157. }
  158. /*
  159. // http://www.unicode.org/reports/tr46/#Table_Example_Processing
  160. #[test]
  161. fn test_examples() {
  162. let codec = idna::uts46bis::Uts46::new();
  163. let mut out = String::new();
  164. assert_matches!(codec.to_unicode("Bloß.de", &mut out), Ok(()));
  165. assert_eq!(out, "bloß.de");
  166. out.clear();
  167. assert_matches!(codec.to_unicode("xn--blo-7ka.de", &mut out), Ok(()));
  168. assert_eq!(out, "bloß.de");
  169. out.clear();
  170. assert_matches!(codec.to_unicode("u\u{308}.com", &mut out), Ok(()));
  171. assert_eq!(out, "ü.com");
  172. out.clear();
  173. assert_matches!(codec.to_unicode("xn--tda.com", &mut out), Ok(()));
  174. assert_eq!(out, "ü.com");
  175. out.clear();
  176. assert_matches!(codec.to_unicode("xn--u-ccb.com", &mut out), Err(_));
  177. out.clear();
  178. assert_matches!(codec.to_unicode("a⒈com", &mut out), Err(_));
  179. out.clear();
  180. assert_matches!(codec.to_unicode("xn--a-ecp.ru", &mut out), Err(_));
  181. out.clear();
  182. assert_matches!(codec.to_unicode("xn--0.pt", &mut out), Err(_));
  183. out.clear();
  184. assert_matches!(codec.to_unicode("日本語。JP", &mut out), Ok(()));
  185. assert_eq!(out, "日本語.jp");
  186. out.clear();
  187. assert_matches!(codec.to_unicode("☕.us", &mut out), Ok(()));
  188. assert_eq!(out, "☕.us");
  189. }
  190. */
  191. #[test]
  192. fn test_v5() {
  193. let config = idna::uts46::Uts46::new();
  194. // IdnaTest:784 蔏。𑰺
  195. assert!(config
  196. .to_ascii(
  197. "\u{11C3A}".as_bytes(),
  198. AsciiDenyList::STD3,
  199. Hyphens::Check,
  200. DnsLength::Verify,
  201. )
  202. .is_err());
  203. assert!(config
  204. .to_ascii(
  205. "\u{850f}.\u{11C3A}".as_bytes(),
  206. AsciiDenyList::STD3,
  207. Hyphens::Check,
  208. DnsLength::Verify,
  209. )
  210. .is_err());
  211. assert!(config
  212. .to_ascii(
  213. "\u{850f}\u{ff61}\u{11C3A}".as_bytes(),
  214. AsciiDenyList::STD3,
  215. Hyphens::Check,
  216. DnsLength::Verify,
  217. )
  218. .is_err());
  219. }
  220. #[test]
  221. fn test_v8_bidi_rules() {
  222. let config = idna::uts46::Uts46::new();
  223. assert_eq!(
  224. config
  225. .to_ascii(
  226. b"abc",
  227. AsciiDenyList::STD3,
  228. Hyphens::Check,
  229. DnsLength::Verify,
  230. )
  231. .unwrap(),
  232. "abc"
  233. );
  234. assert_eq!(
  235. config
  236. .to_ascii(
  237. b"123",
  238. AsciiDenyList::STD3,
  239. Hyphens::Check,
  240. DnsLength::Verify,
  241. )
  242. .unwrap(),
  243. "123"
  244. );
  245. assert_eq!(
  246. config
  247. .to_ascii(
  248. "אבּג".as_bytes(),
  249. AsciiDenyList::STD3,
  250. Hyphens::Check,
  251. DnsLength::Verify,
  252. )
  253. .unwrap(),
  254. "xn--kdb3bdf"
  255. );
  256. assert_eq!(
  257. config
  258. .to_ascii(
  259. "ابج".as_bytes(),
  260. AsciiDenyList::STD3,
  261. Hyphens::Check,
  262. DnsLength::Verify,
  263. )
  264. .unwrap(),
  265. "xn--mgbcm"
  266. );
  267. assert_eq!(
  268. config
  269. .to_ascii(
  270. "abc.ابج".as_bytes(),
  271. AsciiDenyList::STD3,
  272. Hyphens::Check,
  273. DnsLength::Verify,
  274. )
  275. .unwrap(),
  276. "abc.xn--mgbcm"
  277. );
  278. assert_eq!(
  279. config
  280. .to_ascii(
  281. "אבּג.ابج".as_bytes(),
  282. AsciiDenyList::STD3,
  283. Hyphens::Check,
  284. DnsLength::Verify,
  285. )
  286. .unwrap(),
  287. "xn--kdb3bdf.xn--mgbcm"
  288. );
  289. // Bidi domain names cannot start with digits
  290. assert!(config
  291. .to_ascii(
  292. "0a.\u{05D0}".as_bytes(),
  293. AsciiDenyList::STD3,
  294. Hyphens::Check,
  295. DnsLength::Verify,
  296. )
  297. .is_err());
  298. assert!(config
  299. .to_ascii(
  300. "0à.\u{05D0}".as_bytes(),
  301. AsciiDenyList::STD3,
  302. Hyphens::Check,
  303. DnsLength::Verify,
  304. )
  305. .is_err());
  306. // Bidi chars may be punycode-encoded
  307. assert!(config
  308. .to_ascii(
  309. b"xn--0ca24w",
  310. AsciiDenyList::STD3,
  311. Hyphens::Check,
  312. DnsLength::Verify,
  313. )
  314. .is_err());
  315. }
  316. #[test]
  317. fn emoji_domains() {
  318. // HOT BEVERAGE is allowed here...
  319. let config = idna::uts46::Uts46::new();
  320. assert_eq!(
  321. config
  322. .to_ascii(
  323. "☕.com".as_bytes(),
  324. AsciiDenyList::STD3,
  325. Hyphens::Check,
  326. DnsLength::Verify,
  327. )
  328. .unwrap(),
  329. "xn--53h.com"
  330. );
  331. }
  332. #[test]
  333. fn unicode_before_delimiter() {
  334. let config = idna::uts46::Uts46::new();
  335. assert!(config
  336. .to_ascii(
  337. "xn--f\u{34a}-PTP".as_bytes(),
  338. AsciiDenyList::STD3,
  339. Hyphens::Check,
  340. DnsLength::Verify,
  341. )
  342. .is_err());
  343. }
  344. #[test]
  345. fn upper_case_ascii_in_punycode() {
  346. let config = idna::uts46::Uts46::new();
  347. let (unicode, result) =
  348. config.to_unicode("xn--A-1ga".as_bytes(), AsciiDenyList::STD3, Hyphens::Check);
  349. assert!(result.is_ok());
  350. assert_eq!(&unicode, "aö");
  351. }