index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. 'use strict';
  2. const Punycode = require('punycode');
  3. const Code = require('@hapi/code');
  4. const Address = require('..');
  5. const Lab = require('@hapi/lab');
  6. const internals = {};
  7. const { describe, it } = exports.lab = Lab.script();
  8. const expect = Code.expect;
  9. describe('email', () => {
  10. it('available as direct require', () => {
  11. expect(require('../lib/email').isValid('test@example.com')).to.be.true();
  12. });
  13. describe('analyze()', () => {
  14. it('identifies error', () => {
  15. const tests = [
  16. ['', 'Address must be a non-empty string'],
  17. ['êjness@iana.org', 'Address contains forbidden Unicode characters', { allowUnicode: false }],
  18. ['test@test@test', 'Address cannot contain more than one @ character'],
  19. ['test', 'Address must contain one @ character'],
  20. ['@example.com', 'Address local part cannot be empty'],
  21. ['test@', 'Domain must be a non-empty string'],
  22. ['1234567890@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz.com', 'Address too long'],
  23. ['1234567890123456789012345678901234567890123456789012345678901234567890@example.com', 'Address local part too long'],
  24. ['x..y@example.com', 'Address local part contains empty dot-separated segment'],
  25. ['x:y@example.com', 'Address local part contains invalid character'],
  26. ['ê:y@example.com', 'Address local part contains invalid character'],
  27. ['test@com', 'Domain lacks the minimum required number of segments'],
  28. ['test@x.no-such-tld', 'Domain uses forbidden TLD'],
  29. ['test@example..com', 'Domain contains empty dot-separated segment'],
  30. ['test@1234567890123456789012345678901234567890123456789012345678901234567890.com', 'Domain contains dot-separated segment that is too long'],
  31. ['test@example+.com', 'Domain contains invalid character', { tlds: false }],
  32. ['test@example.com_', 'Domain contains invalid tld character', { tlds: false }]
  33. ];
  34. for (let i = 0; i < tests.length; ++i) {
  35. const email = tests[i];
  36. const output = Address.email.analyze(email[0], email[2]);
  37. const result = email[1];
  38. if (!output ||
  39. output.error !== result) {
  40. console.log(i, email[0]);
  41. }
  42. expect(output.error).to.equal(result);
  43. }
  44. });
  45. it('validates options', () => {
  46. const tests = [
  47. ['test@example.com', 'Invalid options: tlds must be a boolean or an object', { tlds: 1 }],
  48. ['test@example.com', 'Invalid options: tlds.allow must be a Set object or true', { tlds: { allow: ['test'] } }],
  49. ['test@example.com', 'Invalid options: tlds.deny must be a Set object', { tlds: { deny: ['test'] } }],
  50. ['test@example.com', 'Invalid options: cannot specify both tlds.allow and tlds.deny lists', { tlds: { allow: new Set(), deny: new Set() } }],
  51. [1, 'Invalid input: email must be a string']
  52. ];
  53. for (let i = 0; i < tests.length; ++i) {
  54. const email = tests[i];
  55. expect(() => Address.email.analyze(email[0], email[2])).to.throw(email[1]);
  56. }
  57. });
  58. describe('validated TLD', () => {
  59. it('applies built-in list', () => {
  60. expect(Address.email.analyze('test@example.com')).to.not.exist();
  61. expect(Address.email.analyze('test@example.com', { tlds: true })).to.not.exist();
  62. expect(Address.email.analyze('test@example.com', { tlds: { allow: true } })).to.not.exist();
  63. });
  64. it('ignores built-in list', () => {
  65. expect(Address.email.analyze('test@example.invalid-top', { tlds: false })).to.not.exist();
  66. });
  67. it('denies listed tls', () => {
  68. expect(Address.email.analyze('test@example.com', { tlds: { deny: new Set(['test']) } })).to.not.exist();
  69. expect(Address.email.analyze('test@example.com', { tlds: { deny: new Set(['com']) } })).to.equal({ error: 'Domain uses forbidden TLD' });
  70. });
  71. });
  72. });
  73. describe('isValid()', () => {
  74. it('validates email', () => {
  75. // Tests adapted from https://github.com/skeggse/isemail
  76. // Copyright (c) 2008-2019, Eli Skeggs, Dominic Sayers, GlobeSherpa
  77. const tests = [
  78. ['\r', false],
  79. ['test', false],
  80. ['@', false],
  81. ['test@', false],
  82. ['test@io', false],
  83. ['test@io', true, { minDomainSegments: 1 }],
  84. ['@io', false],
  85. ['@iana.org', false],
  86. ['test@iana.org', true],
  87. ['test@nominet.org.uk', true],
  88. ['test@about.museum', true],
  89. ['a@iana.org', true],
  90. ['êjness@iana.org', true],
  91. ['ñoñó1234@iana.org', true],
  92. ['ñoñó1234@something.com', true],
  93. ['伊昭傑@郵件.商務', true, { tlds: { allow: new Set([Punycode.toASCII('商務')]) } }],
  94. ['\ud801\udc37\ud852\udf62@iana.org', true],
  95. ['test.test@iana.org', true],
  96. ['.test@iana.org', false],
  97. ['test.@iana.org', false],
  98. ['test..iana.org', false],
  99. ['test_exa-mple.com', false],
  100. ['!#$%&`*+/=?^`{|}~@iana.org', true],
  101. ['test\\@test@iana.org', false],
  102. ['123@iana.org', true],
  103. ['test@123.com', true],
  104. ['test@iana.123', false],
  105. ['test@255.255.255.255', false],
  106. ['abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@iana.org', true],
  107. ['abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklmn@iana.org', false],
  108. ['\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06@iana.org', false],
  109. ['test@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm', false],
  110. ['test@\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06.org', true],
  111. ['test@abcdefghijklmnopqrstuvwxyzabcdefghijklmno\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06.org', false],
  112. ['test@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm.com', false],
  113. ['test@mason-dixon.com', true],
  114. ['test@-iana.org', false],
  115. ['test@iana-.com', false],
  116. ['test@.iana.org', false],
  117. ['test@iana.org.', false],
  118. ['test@iana..com', false],
  119. ['abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmno', false],
  120. ['abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.\ud83d\ude06\ud83d\ude06\ud83d\ude06\ud83d\ude06', false],
  121. ['abcdef@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef.hijklmnopqrstuv', false],
  122. ['abcdef@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghi.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd\ud83d\ude06', false],
  123. ['abcdef@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghi.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\ud83d\ude06', false],
  124. ['a@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijkl.hijk', false],
  125. ['a@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijkl.\ud83d\ude06', false],
  126. ['\"\r', false],
  127. ['\"test\"@iana.org', false],
  128. ['\"\"@iana.org', false],
  129. ['\"\"\"@iana.org', false],
  130. ['\"\\a\"@iana.org', false],
  131. ['\"\\\"\"@iana.org', false],
  132. ['\"\\\"@iana.org', false],
  133. ['\"\\\\\"@iana.org', false],
  134. ['test\"@iana.org', false],
  135. ['\"test@iana.org', false],
  136. ['\"test\"test@iana.org', false],
  137. ['test\"text\"@iana.org', false],
  138. ['\"test\"\"test\"@iana.org', false],
  139. ['\"test\".\"test\"@iana.org', false],
  140. ['\"test\\ test\"@iana.org', false],
  141. ['\"test\".test@iana.org', false],
  142. ['\"test\u0000\"@iana.org', false],
  143. ['\"test\\\u0000\"@iana.org', false],
  144. ['\"test\r\n test\"@iana.org', false],
  145. ['\"abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghj\"@iana.org', false],
  146. ['\"abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefg\\h\"@iana.org', false],
  147. ['test@[255.255.255.255]', false],
  148. ['test@a[255.255.255.255]', false],
  149. ['test@[255.255.255]', false],
  150. ['test@[255.255.255.255.255]', false],
  151. ['test@[255.255.255.256]', false],
  152. ['test@[1111:2222:3333:4444:5555:6666:7777:8888]', false],
  153. ['test@[IPv6:1111:2222:3333:4444:5555:6666:7777]', false],
  154. ['test@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888]', false],
  155. ['test@[IPv6:1111:2222:3333:4444:5555:6666:7777:8888:9999]', false],
  156. ['test@[IPv6:1111:2222:3333:4444:5555:6666:7777:888G]', false],
  157. ['test@[IPv6:1111:2222:3333:4444:5555:6666::8888]', false],
  158. ['test@[IPv6:1111:2222:3333:4444:5555::8888]', false],
  159. ['test@[IPv6:1111:2222:3333:4444:5555:6666::7777:8888]', false],
  160. ['test@[IPv6::3333:4444:5555:6666:7777:8888]', false],
  161. ['test@[IPv6:::3333:4444:5555:6666:7777:8888]', false],
  162. ['test@[IPv6:1111::4444:5555::8888]', false],
  163. ['test@[IPv6:::]', false],
  164. ['test@[IPv6:1111:2222:3333:4444:5555:255.255.255.255]', false],
  165. ['test@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255]', false],
  166. ['test@[IPv6:1111:2222:3333:4444:5555:6666:7777:255.255.255.255]', false],
  167. ['test@[IPv6:1111:2222:3333:4444::255.255.255.255]', false],
  168. ['test@[IPv6:1111:2222:3333:4444:5555:6666::255.255.255.255]', false],
  169. ['test@[IPv6:1111:2222:3333:4444:::255.255.255.255]', false],
  170. ['test@[IPv6::255.255.255.255]', false],
  171. ['test@[255.255.255.255].local', false],
  172. ['test@local.[255.255.255.255]', false],
  173. ['test@local.[255.255.255.255].local', false],
  174. ['test@local.(comment)[255.255.255.255].local', false],
  175. ['test@local. [255.255.255.255].local', false],
  176. ['test@local.[255.255.255.255](comment).local', false],
  177. ['test@local.[255.255.255.255] .local', false],
  178. [' test @iana.org', false],
  179. ['test@ iana .com', false],
  180. ['test . test@iana.org', false],
  181. ['\r\n test@iana.org', false],
  182. ['\r\n \r\n test@iana.org', false],
  183. ['(\r', false],
  184. ['(comment)test@iana.org', false],
  185. ['((comment)test@iana.org', false],
  186. ['(comment(comment))test@iana.org', false],
  187. ['test@(comment)iana.org', false],
  188. ['test(comment)@iana.org', false],
  189. ['test(comment)test@iana.org', false],
  190. ['test@(comment)[255.255.255.255]', false],
  191. ['(comment)abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghiklm@iana.org', false],
  192. ['test@(comment)abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.com', false],
  193. ['(comment)test@abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg.abcdefghijklmnopqrstuvwxyzabcdefghijk.abcdefghijklmnopqrst', false],
  194. ['test@iana.org\n', false],
  195. ['xn--test@iana.org', true],
  196. ['test@iana.org-', false],
  197. ['\"test@iana.org', false],
  198. ['(test@iana.org', false],
  199. ['test@(iana.org', false],
  200. ['test@[1.2.3.4', false],
  201. ['\"test\\\"@iana.org', false],
  202. ['(comment\\)test@iana.org', false],
  203. ['test@iana.org(comment\\)', false],
  204. ['test@iana.org(comment\\', false],
  205. ['test@[RFC-5322-domain-literal]', false],
  206. ['test@[RFC-5322-郵件ñó-domain-literal]', false],
  207. ['test@[RFC-5322]-domain-literal]', false],
  208. ['test@[RFC-5322].domain-literal]', false],
  209. ['test@[RFC-5322-[domain-literal]', false],
  210. ['test@[€', false],
  211. ['test@[\u0007]', false],
  212. ['test@[RFC-5322-\\\u0007-domain-literal]', false],
  213. ['test@[RFC-5322-\\\t-domain-literal]', false],
  214. ['test@[RFC-5322-\\]-domain-literal]', false],
  215. ['test@[RFC-5322-\\郵-no-domain-literal]', false],
  216. ['test@[RFC-5322--domain-literal]', false],
  217. ['test@[RFC-5322-domain-literal\\]', false],
  218. ['test@[RFC-5322-domain-literal\\', false],
  219. ['test@[RFC 5322 domain literal]', false],
  220. ['test@[RFC-5322-domain-literal] (comment)', false],
  221. ['@iana.org', false],
  222. ['test@.org', false],
  223. ['\"\"@iana.org', false],
  224. ['\"€\"@iana.org', false],
  225. ['\"\\\"@iana.org', false],
  226. ['()test@iana.org', false],
  227. ['(€)test@iana.org', false],
  228. ['test@iana.org\r', false],
  229. ['\rtest@iana.org', false],
  230. ['\"\rtest\"@iana.org', false],
  231. ['(\r)test@iana.org', false],
  232. ['test@iana.org(\r)', false],
  233. ['test@<iana>.org', false],
  234. ['\ntest@iana.org', false],
  235. ['\"\n\"@iana.org', false],
  236. ['\"\\\n\"@iana.org', false],
  237. ['(\n)test@iana.org', false],
  238. ['\u0007@iana.org', false],
  239. ['test@\u0007.org', false],
  240. ['\"\u0007\"@iana.org', false],
  241. ['\"\\\u0007\"@iana.org', false],
  242. ['(\u0007)test@iana.org', false],
  243. ['\r\ntest@iana.org', false],
  244. ['\r\n \r\ntest@iana.org', false],
  245. [' \r\ntest@iana.org', false],
  246. [' \r\n test@iana.org', false],
  247. [' \r\n \r\ntest@iana.org', false],
  248. [' \r\n\r\ntest@iana.org', false],
  249. [' \r\n\r\n test@iana.org', false],
  250. ['test@iana.org\r\n ', false],
  251. ['test@iana.org\r\n \r\n ', false],
  252. ['test@iana.org\r\n', false],
  253. ['test@iana.org \r', false],
  254. ['test@iana.org\r\n \r\n', false],
  255. ['test@iana.org \r\n', false],
  256. ['test@iana.org \r\n ', false],
  257. ['test@iana.org \r\n \r\n', false],
  258. ['test@iana.org \r\n\r\n', false],
  259. ['test@iana.org \r\n\r\n ', false],
  260. ['test@iana. org', false],
  261. ['test@[\r', false],
  262. ['test@[\r\n', false],
  263. [' test@iana.org', false],
  264. ['test@iana.org ', false],
  265. ['test@[IPv6:1::2:]', false],
  266. ['\"test\\\u0094\"@iana.org', false],
  267. ['test@iana/icann.org', false],
  268. ['test@iana!icann.org', false],
  269. ['test@iana?icann.org', false],
  270. ['test@iana^icann.org', false],
  271. ['test@iana{icann}.org', false],
  272. ['test.(comment)test@iana.org', false],
  273. ['test@iana.(comment)org', false],
  274. ['test@iana(comment)iana.org', false],
  275. ['(comment\r\n comment)test@iana.org', false],
  276. ['test@org', true, { minDomainSegments: 1 }],
  277. ['test\ud800@invalid', false],
  278. ['\"\ud800\"@invalid', false],
  279. ['\"\\\ud800\"@invalid', false],
  280. ['(\ud800)thing@invalid', false],
  281. ['\"\\\ud800\"@invalid', false],
  282. ['test@\ud800\udfffñoñó郵件ñoñó郵件.郵件ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.noñó郵件ñoñó郵.商務', false, { tlds: { allow: new Set([Punycode.toASCII('商務')]) } }],
  283. ['test@\ud800\udfffñoñó郵件ñoñó郵件.郵件ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.ñoñó郵件ñoñó郵件.oñó郵件ñoñó郵件ñoñó郵件.商務', false, { tlds: { allow: new Set([Punycode.toASCII('商務')]) } }],
  284. ['test@ñoñoñó郵件\ud83d\ude06ñoñ.oñó郵件\uc138ñoñ.oñó郵件\u0644\u4eec\u010dñoñoñó郵件\u05dcño.ñoñó郵件\u092f\u672cñoñoñó郵件\uc138añoñ.oñó郵件\ud83d\ude06bc\uc138郵\ud83d\ude06ño.ñoñó郵件ñoñoñó郵件\ud83d\ude06ñoñoñó郵件\uc138ñoñ.oñó郵件\u0644\u4eecñoñoñó.郵件\ud83d\ude06ñoñoñó郵件郵\uc138ñoñoñó郵件\u0644\u4eecñoñoñó郵件.\ud83d\ude06ñoñoñó郵件郵\uc138\u0644\u4eec.郵件\ud83d\ude06ñoñoñó郵.件郵\uc138\u4eec\ud83d\ude06ñoñoñó件郵\uc138ñoñoñó郵件', false, { tlds: { allow: new Set([Punycode.toASCII('商務')]) } }],
  285. ['test@ñoñó郵件ñoñó郵件ñoñó郵件ñoñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件ñoñó郵件.商務', false, { tlds: { allow: new Set([Punycode.toASCII('商務')]) } }],
  286. ['\ud83d\ude06ñoñó郵件ñoñó郵件ñoñó\ud83d\ude06郵件ñoñoñó郵@\ud83d\ude06郵件ñoñó郵件ñoñó.\ud83d\ude06郵件ñoñó郵件ñoñó.\ud83d\ude06郵件ñoñó郵件ñoñó.郵件ñoñó郵件ñoñó\ud83d\ude06.郵件ñoñó郵件ñoñó.郵件ñoñó郵件.ñoñó郵件ñoñó.郵件ñoñó郵件.\ud83d\ude06郵件ñoñó郵件ñoñó.\ud83d\ude06郵件ñoñó郵件ñoñó.\ud83d\ude06商務.郵件ñoñó郵件ñoñó郵件.\ud83d\ude06商務.\ud83d\ude06商務.\ud83d\ude06商務', false, { tlds: { allow: new Set([Punycode.toASCII('商務')]) } }],
  287. ['test@[\0', false],
  288. ['(\0)test@example.com', false],
  289. ['shouldbe@invalid', false],
  290. ['shouldbe@INVALID', false],
  291. ['shouldbe@example.com', true],
  292. ['shouldbe@example.COM', true],
  293. ['apple-touch-icon-60x60@2x.png', false],
  294. ['shouldbe@XN--UNUP4Y', true, { minDomainSegments: 1 }],
  295. ['shouldbe@xn--unup4y', true, { minDomainSegments: 1 }],
  296. ['shouldbe@\u6e38\u620f', true, { minDomainSegments: 1 }],
  297. ['æøå', false],
  298. ['1234567890abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw@xyz.com', true, { ignoreLength: true }],
  299. ['test@example.com@example.com', false],
  300. ['test@example.com/path', false],
  301. ['test@example.com:123', false]
  302. ];
  303. for (let i = 0; i < tests.length; ++i) {
  304. const email = tests[i];
  305. const valid = Address.email.isValid(email[0], email[2]);
  306. const result = email[1];
  307. if (valid !== result) {
  308. const outcome = Address.email.analyze(email[0], email[2]);
  309. if (outcome) {
  310. console.log(i, email[0], outcome.error);
  311. }
  312. else {
  313. console.log(i, email[0]);
  314. }
  315. }
  316. expect(valid).to.equal(result);
  317. }
  318. });
  319. });
  320. });
  321. describe('domain', () => {
  322. it('available as direct require', () => {
  323. expect(require('../lib/domain').isValid('example.com')).to.be.true();
  324. });
  325. describe('analyze()', () => {
  326. it('identifies error', () => {
  327. const tests = [
  328. ['', 'Domain must be a non-empty string'],
  329. ['êiana.org', 'Domain contains forbidden Unicode characters', { allowUnicode: false }],
  330. ['abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz.com', 'Domain too long'],
  331. ['com', 'Domain lacks the minimum required number of segments'],
  332. ['x.no-such-tld', 'Domain uses forbidden TLD'],
  333. ['example..com', 'Domain contains empty dot-separated segment'],
  334. ['1234567890123456789012345678901234567890123456789012345678901234567890.com', 'Domain contains dot-separated segment that is too long'],
  335. ['example+.com', 'Domain contains invalid character', { tlds: false }],
  336. ['example.com_', 'Domain contains invalid tld character', { tlds: false }]
  337. ];
  338. for (let i = 0; i < tests.length; ++i) {
  339. const domain = tests[i];
  340. const output = Address.domain.analyze(domain[0], domain[2]);
  341. const result = domain[1];
  342. if (!output ||
  343. output.error !== result) {
  344. console.log(i, domain[0]);
  345. }
  346. expect(output.error).to.equal(result);
  347. }
  348. });
  349. it('validates options', () => {
  350. const tests = [
  351. ['example.com', 'Invalid options: tlds must be a boolean or an object', { tlds: 1 }],
  352. ['example.com', 'Invalid options: tlds.allow must be a Set object or true', { tlds: { allow: ['test'] } }],
  353. ['example.com', 'Invalid options: tlds.deny must be a Set object', { tlds: { deny: ['test'] } }],
  354. ['example.com', 'Invalid options: cannot specify both tlds.allow and tlds.deny lists', { tlds: { allow: new Set(), deny: new Set() } }],
  355. [1, 'Invalid input: domain must be a string']
  356. ];
  357. for (let i = 0; i < tests.length; ++i) {
  358. const domain = tests[i];
  359. expect(() => Address.domain.analyze(domain[0], domain[2])).to.throw(domain[1]);
  360. }
  361. });
  362. });
  363. describe('isValid()', () => {
  364. it('validates domain', () => {
  365. const tests = [
  366. ['\r', false],
  367. ['test', false],
  368. ['@', false],
  369. ['iana.org', true],
  370. ['nominet.org.uk', true],
  371. ['about.museum', true],
  372. ['x.商務', true, { tlds: { allow: new Set([Punycode.toASCII('商務')]) } }],
  373. ['iana.123', false],
  374. ['255.255.255.255', false],
  375. ['XN--UNUP4Y', true, { minDomainSegments: 1 }],
  376. ['test@example.com', false],
  377. ['test:example.com', false],
  378. ['example.com:123', false],
  379. ['example.com/path', false]
  380. ];
  381. for (let i = 0; i < tests.length; ++i) {
  382. const domain = tests[i];
  383. const valid = Address.domain.isValid(domain[0], domain[2]);
  384. const result = domain[1];
  385. if (valid !== result) {
  386. const outcome = Address.domain.analyze(domain[0], domain[2]);
  387. if (outcome) {
  388. console.log(i, domain[0], outcome.error);
  389. }
  390. else {
  391. console.log(i, domain[0]);
  392. }
  393. }
  394. expect(valid).to.equal(result);
  395. }
  396. });
  397. });
  398. });