• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const punycode = require('punycode');
25const assert = require('assert');
26
27assert.strictEqual(punycode.encode('ü'), 'tda');
28assert.strictEqual(punycode.encode('Goethe'), 'Goethe-');
29assert.strictEqual(punycode.encode('Bücher'), 'Bcher-kva');
30assert.strictEqual(
31  punycode.encode(
32    'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'
33  ),
34  'Willst du die Blthe des frhen, die Frchte des spteren Jahres-x9e96lkal'
35);
36assert.strictEqual(punycode.encode('日本語'), 'wgv71a119e');
37assert.strictEqual(punycode.encode('��'), 'x73l');
38
39assert.strictEqual(punycode.decode('tda'), 'ü');
40assert.strictEqual(punycode.decode('Goethe-'), 'Goethe');
41assert.strictEqual(punycode.decode('Bcher-kva'), 'Bücher');
42assert.strictEqual(
43  punycode.decode(
44    'Willst du die Blthe des frhen, die Frchte des spteren Jahres-x9e96lkal'
45  ),
46  'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'
47);
48assert.strictEqual(punycode.decode('wgv71a119e'), '日本語');
49assert.strictEqual(punycode.decode('x73l'), '��');
50assert.throws(() => {
51  punycode.decode(' ');
52}, /^RangeError: Invalid input$/);
53assert.throws(() => {
54  punycode.decode('α-');
55}, /^RangeError: Illegal input >= 0x80 \(not a basic code point\)$/);
56assert.throws(() => {
57  punycode.decode('あ');
58}, /^RangeError: Overflow: input needs wider integers to process$/);
59
60// http://tools.ietf.org/html/rfc3492#section-7.1
61const tests = [
62  // (A) Arabic (Egyptian)
63  {
64    encoded: 'egbpdaj6bu4bxfgehfvwxn',
65    decoded: '\u0644\u064A\u0647\u0645\u0627\u0628\u062A\u0643\u0644\u0645' +
66      '\u0648\u0634\u0639\u0631\u0628\u064A\u061F'
67  },
68
69  // (B) Chinese (simplified)
70  {
71    encoded: 'ihqwcrb4cv8a8dqg056pqjye',
72    decoded: '\u4ED6\u4EEC\u4E3A\u4EC0\u4E48\u4E0D\u8BF4\u4E2D\u6587'
73  },
74
75  // (C) Chinese (traditional)
76  {
77    encoded: 'ihqwctvzc91f659drss3x8bo0yb',
78    decoded: '\u4ED6\u5011\u7232\u4EC0\u9EBD\u4E0D\u8AAA\u4E2D\u6587'
79  },
80
81  // (D) Czech: Pro<ccaron>prost<ecaron>nemluv<iacute><ccaron>esky
82  {
83    encoded: 'Proprostnemluvesky-uyb24dma41a',
84    decoded: '\u0050\u0072\u006F\u010D\u0070\u0072\u006F\u0073\u0074\u011B' +
85      '\u006E\u0065\u006D\u006C\u0075\u0076\u00ED\u010D\u0065\u0073\u006B\u0079'
86  },
87
88  // (E) Hebrew
89  {
90    encoded: '4dbcagdahymbxekheh6e0a7fei0b',
91    decoded: '\u05DC\u05DE\u05D4\u05D4\u05DD\u05E4\u05E9\u05D5\u05D8\u05DC' +
92      '\u05D0\u05DE\u05D3\u05D1\u05E8\u05D9\u05DD\u05E2\u05D1\u05E8\u05D9\u05EA'
93  },
94
95  // (F) Hindi (Devanagari)
96  {
97    encoded: 'i1baa7eci9glrd9b2ae1bj0hfcgg6iyaf8o0a1dig0cd',
98    decoded: '\u092F\u0939\u0932\u094B\u0917\u0939\u093F\u0928\u094D\u0926' +
99      '\u0940\u0915\u094D\u092F\u094B\u0902\u0928\u0939\u0940\u0902\u092C' +
100      '\u094B\u0932\u0938\u0915\u0924\u0947\u0939\u0948\u0902'
101  },
102
103  // (G) Japanese (kanji and hiragana)
104  {
105    encoded: 'n8jok5ay5dzabd5bym9f0cm5685rrjetr6pdxa',
106    decoded: '\u306A\u305C\u307F\u3093\u306A\u65E5\u672C\u8A9E\u3092\u8A71' +
107      '\u3057\u3066\u304F\u308C\u306A\u3044\u306E\u304B'
108  },
109
110  // (H) Korean (Hangul syllables)
111  {
112    encoded: '989aomsvi5e83db1d2a355cv1e0vak1dwrv93d5xbh15a0dt30a5jpsd879' +
113      'ccm6fea98c',
114    decoded: '\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774\uD55C' +
115      '\uAD6D\uC5B4\uB97C\uC774\uD574\uD55C\uB2E4\uBA74\uC5BC\uB9C8\uB098' +
116      '\uC88B\uC744\uAE4C'
117  },
118
119  // (I) Russian (Cyrillic)
120  {
121    encoded: 'b1abfaaepdrnnbgefbadotcwatmq2g4l',
122    decoded: '\u043F\u043E\u0447\u0435\u043C\u0443\u0436\u0435\u043E\u043D' +
123      '\u0438\u043D\u0435\u0433\u043E\u0432\u043E\u0440\u044F\u0442\u043F' +
124      '\u043E\u0440\u0443\u0441\u0441\u043A\u0438'
125  },
126
127  // (J) Spanish: Porqu<eacute>nopuedensimplementehablarenEspa<ntilde>ol
128  {
129    encoded: 'PorqunopuedensimplementehablarenEspaol-fmd56a',
130    decoded: '\u0050\u006F\u0072\u0071\u0075\u00E9\u006E\u006F\u0070\u0075' +
131      '\u0065\u0064\u0065\u006E\u0073\u0069\u006D\u0070\u006C\u0065\u006D' +
132      '\u0065\u006E\u0074\u0065\u0068\u0061\u0062\u006C\u0061\u0072\u0065' +
133      '\u006E\u0045\u0073\u0070\u0061\u00F1\u006F\u006C'
134  },
135
136  // (K) Vietnamese: T<adotbelow>isaoh<odotbelow>kh<ocirc>ngth
137  // <ecirchookabove>ch<ihookabove>n<oacute>iti<ecircacute>ngVi<ecircdotbelow>t
138  {
139    encoded: 'TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g',
140    decoded: '\u0054\u1EA1\u0069\u0073\u0061\u006F\u0068\u1ECD\u006B\u0068' +
141      '\u00F4\u006E\u0067\u0074\u0068\u1EC3\u0063\u0068\u1EC9\u006E\u00F3' +
142      '\u0069\u0074\u0069\u1EBF\u006E\u0067\u0056\u0069\u1EC7\u0074'
143  },
144
145  // (L) 3<nen>B<gumi><kinpachi><sensei>
146  {
147    encoded: '3B-ww4c5e180e575a65lsy2b',
148    decoded: '\u0033\u5E74\u0042\u7D44\u91D1\u516B\u5148\u751F'
149  },
150
151  // (M) <amuro><namie>-with-SUPER-MONKEYS
152  {
153    encoded: '-with-SUPER-MONKEYS-pc58ag80a8qai00g7n9n',
154    decoded: '\u5B89\u5BA4\u5948\u7F8E\u6075\u002D\u0077\u0069\u0074\u0068' +
155      '\u002D\u0053\u0055\u0050\u0045\u0052\u002D\u004D\u004F\u004E\u004B' +
156      '\u0045\u0059\u0053'
157  },
158
159  // (N) Hello-Another-Way-<sorezore><no><basho>
160  {
161    encoded: 'Hello-Another-Way--fc4qua05auwb3674vfr0b',
162    decoded: '\u0048\u0065\u006C\u006C\u006F\u002D\u0041\u006E\u006F\u0074' +
163      '\u0068\u0065\u0072\u002D\u0057\u0061\u0079\u002D\u305D\u308C\u305E' +
164      '\u308C\u306E\u5834\u6240'
165  },
166
167  // (O) <hitotsu><yane><no><shita>2
168  {
169    encoded: '2-u9tlzr9756bt3uc0v',
170    decoded: '\u3072\u3068\u3064\u5C4B\u6839\u306E\u4E0B\u0032'
171  },
172
173  // (P) Maji<de>Koi<suru>5<byou><mae>
174  {
175    encoded: 'MajiKoi5-783gue6qz075azm5e',
176    decoded: '\u004D\u0061\u006A\u0069\u3067\u004B\u006F\u0069\u3059\u308B' +
177      '\u0035\u79D2\u524D'
178  },
179
180  // (Q) <pafii>de<runba>
181  {
182    encoded: 'de-jg4avhby1noc0d',
183    decoded: '\u30D1\u30D5\u30A3\u30FC\u0064\u0065\u30EB\u30F3\u30D0'
184  },
185
186  // (R) <sono><supiido><de>
187  {
188    encoded: 'd9juau41awczczp',
189    decoded: '\u305D\u306E\u30B9\u30D4\u30FC\u30C9\u3067'
190  },
191
192  // (S) -> $1.00 <-
193  {
194    encoded: '-> $1.00 <--',
195    decoded: '\u002D\u003E\u0020\u0024\u0031\u002E\u0030\u0030\u0020\u003C' +
196      '\u002D'
197  },
198];
199
200let errors = 0;
201const handleError = (error, name) => {
202  console.error(
203    `FAIL: ${name} expected ${error.expected}, got ${error.actual}`
204  );
205  errors++;
206};
207
208const regexNonASCII = /[^\x20-\x7E]/;
209const testBattery = {
210  encode: (test) => assert.strictEqual(
211    punycode.encode(test.decoded),
212    test.encoded
213  ),
214  decode: (test) => assert.strictEqual(
215    punycode.decode(test.encoded),
216    test.decoded
217  ),
218  toASCII: (test) => assert.strictEqual(
219    punycode.toASCII(test.decoded),
220    regexNonASCII.test(test.decoded) ?
221      `xn--${test.encoded}` :
222      test.decoded
223  ),
224  toUnicode: (test) => assert.strictEqual(
225    punycode.toUnicode(
226      regexNonASCII.test(test.decoded) ?
227        `xn--${test.encoded}` :
228        test.decoded
229    ),
230    regexNonASCII.test(test.decoded) ?
231      test.decoded.toLowerCase() :
232      test.decoded
233  )
234};
235
236tests.forEach((testCase) => {
237  Object.keys(testBattery).forEach((key) => {
238    try {
239      testBattery[key](testCase);
240    } catch (error) {
241      handleError(error, key);
242    }
243  });
244});
245
246// BMP code point
247assert.strictEqual(punycode.ucs2.encode([0x61]), 'a');
248// Supplementary code point (surrogate pair)
249assert.strictEqual(punycode.ucs2.encode([0x1D306]), '\uD834\uDF06');
250// high surrogate
251assert.strictEqual(punycode.ucs2.encode([0xD800]), '\uD800');
252// High surrogate followed by non-surrogates
253assert.strictEqual(punycode.ucs2.encode([0xD800, 0x61, 0x62]), '\uD800ab');
254// low surrogate
255assert.strictEqual(punycode.ucs2.encode([0xDC00]), '\uDC00');
256// Low surrogate followed by non-surrogates
257assert.strictEqual(punycode.ucs2.encode([0xDC00, 0x61, 0x62]), '\uDC00ab');
258
259assert.strictEqual(errors, 0);
260
261// test map domain
262assert.strictEqual(punycode.toASCII('Bücher@日本語.com'),
263                   'Bücher@xn--wgv71a119e.com');
264assert.strictEqual(punycode.toUnicode('Bücher@xn--wgv71a119e.com'),
265                   'Bücher@日本語.com');
266