| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 (function() { | 5 (function() { |
| 6 | 6 |
| 7 'use strict'; | 7 'use strict'; |
| 8 | 8 |
| 9 /** @type {Function} */ | 9 /** @type {Function} */ |
| 10 var onStanzaStr = null; | 10 var onStanzaStr = null; |
| 11 | 11 |
| 12 /** @type {function(string):void} */ | 12 /** @type {function(string):void} */ |
| 13 var onError = function(msg) {}; | 13 var onError = function(msg) {}; |
| 14 | 14 |
| 15 /** @type {remoting.XmppStreamParser} */ | 15 /** @type {remoting.XmppStreamParser} */ |
| 16 var parser = null; | 16 var parser = null; |
| 17 | 17 |
| 18 module('XmppStreamParser', { | 18 QUnit.module('XmppStreamParser', { |
| 19 setup: function() { | 19 beforeEach: function() { |
| 20 onStanzaStr = sinon.spy(); | 20 onStanzaStr = sinon.spy(); |
| 21 onError = /** @type {function(string):void} */ (sinon.spy()); | 21 onError = /** @type {function(string):void} */ (sinon.spy()); |
| 22 /** @param {Element} stanza */ | 22 /** @param {Element} stanza */ |
| 23 function onStanza(stanza) { | 23 function onStanza(stanza) { |
| 24 onStanzaStr(new XMLSerializer().serializeToString(stanza)); | 24 onStanzaStr(new XMLSerializer().serializeToString(stanza)); |
| 25 } | 25 } |
| 26 parser = new remoting.XmppStreamParser(); | 26 parser = new remoting.XmppStreamParser(); |
| 27 parser.setCallbacks(onStanza, onError); | 27 parser.setCallbacks(onStanza, onError); |
| 28 } | 28 } |
| 29 }); | 29 }); |
| 30 | 30 |
| 31 | 31 |
| 32 test('should parse XMPP stream', function() { | 32 QUnit.test('should parse XMPP stream', function() { |
| 33 parser.appendData(base.encodeUtf8('<stream><iq>text</iq>')); | 33 parser.appendData(base.encodeUtf8('<stream><iq>text</iq>')); |
| 34 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); | 34 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); |
| 35 }); | 35 }); |
| 36 | 36 |
| 37 test('should handle multiple incoming stanzas', function() { | 37 QUnit.test('should handle multiple incoming stanzas', function() { |
| 38 parser.appendData(base.encodeUtf8('<stream><iq>text</iq><iq>more text</iq>')); | 38 parser.appendData(base.encodeUtf8('<stream><iq>text</iq><iq>more text</iq>')); |
| 39 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); | 39 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); |
| 40 sinon.assert.calledWith(onStanzaStr, '<iq>more text</iq>'); | 40 sinon.assert.calledWith(onStanzaStr, '<iq>more text</iq>'); |
| 41 }); | 41 }); |
| 42 | 42 |
| 43 test('should ignore whitespace between stanzas', function() { | 43 QUnit.test('should ignore whitespace between stanzas', function() { |
| 44 parser.appendData(base.encodeUtf8('<stream> <iq>text</iq>')); | 44 parser.appendData(base.encodeUtf8('<stream> <iq>text</iq>')); |
| 45 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); | 45 sinon.assert.calledWith(onStanzaStr, '<iq>text</iq>'); |
| 46 }); | 46 }); |
| 47 | 47 |
| 48 test('should assemble messages from small chunks', function() { | 48 QUnit.test('should assemble messages from small chunks', function() { |
| 49 parser.appendData(base.encodeUtf8('<stream><i')); | 49 parser.appendData(base.encodeUtf8('<stream><i')); |
| 50 parser.appendData(base.encodeUtf8('q>')); | 50 parser.appendData(base.encodeUtf8('q>')); |
| 51 | 51 |
| 52 // Split one UTF-8 sequence into two chunks | 52 // Split one UTF-8 sequence into two chunks |
| 53 var data = base.encodeUtf8('😃'); | 53 var data = base.encodeUtf8('😃'); |
| 54 parser.appendData(data.slice(0, 2)); | 54 parser.appendData(data.slice(0, 2)); |
| 55 parser.appendData(data.slice(2)); | 55 parser.appendData(data.slice(2)); |
| 56 | 56 |
| 57 parser.appendData(base.encodeUtf8('</iq>')); | 57 parser.appendData(base.encodeUtf8('</iq>')); |
| 58 | 58 |
| 59 sinon.assert.calledWith(onStanzaStr, '<iq>😃</iq>'); | 59 sinon.assert.calledWith(onStanzaStr, '<iq>😃</iq>'); |
| 60 }); | 60 }); |
| 61 | 61 |
| 62 test('should stop parsing on errors', function() { | 62 QUnit.test('should stop parsing on errors', function() { |
| 63 parser.appendData(base.encodeUtf8('<stream>error<iq>text</iq>')); | 63 parser.appendData(base.encodeUtf8('<stream>error<iq>text</iq>')); |
| 64 sinon.assert.calledWith(onError); | 64 sinon.assert.calledWith(onError); |
| 65 sinon.assert.notCalled(onStanzaStr); | 65 sinon.assert.notCalled(onStanzaStr); |
| 66 }); | 66 }); |
| 67 | 67 |
| 68 test('should fail on invalid stream header', function() { | 68 QUnit.test('should fail on invalid stream header', function() { |
| 69 parser.appendData(base.encodeUtf8('<stream p=\'>')); | 69 parser.appendData(base.encodeUtf8('<stream p=\'>')); |
| 70 sinon.assert.calledWith(onError); | 70 sinon.assert.calledWith(onError); |
| 71 }); | 71 }); |
| 72 | 72 |
| 73 test('should fail on loose text', function() { | 73 QUnit.test('should fail on loose text', function() { |
| 74 parser.appendData(base.encodeUtf8('stream')); | 74 parser.appendData(base.encodeUtf8('stream')); |
| 75 sinon.assert.calledWith(onError); | 75 sinon.assert.calledWith(onError); |
| 76 }); | 76 }); |
| 77 | 77 |
| 78 test('should fail on loose text with incomplete UTF-8 sequences', function() { | 78 QUnit.test('should fail on loose text with incomplete UTF-8 sequences', |
| 79 function() { |
| 79 var buffer = base.encodeUtf8('<stream>Ñ„') | 80 var buffer = base.encodeUtf8('<stream>Ñ„') |
| 80 // Crop last byte. | 81 // Crop last byte. |
| 81 buffer = buffer.slice(0, buffer.byteLength - 1); | 82 buffer = buffer.slice(0, buffer.byteLength - 1); |
| 82 parser.appendData(buffer); | 83 parser.appendData(buffer); |
| 83 sinon.assert.calledWith(onError); | 84 sinon.assert.calledWith(onError); |
| 84 }); | 85 }); |
| 85 | 86 |
| 86 test('should fail on incomplete UTF-8 sequences', function() { | 87 QUnit.test('should fail on incomplete UTF-8 sequences', function() { |
| 87 var buffer = base.encodeUtf8('<stream><iq>Ñ„') | 88 var buffer = base.encodeUtf8('<stream><iq>Ñ„') |
| 88 // Crop last byte. | 89 // Crop last byte. |
| 89 buffer = buffer.slice(0, buffer.byteLength - 1); | 90 buffer = buffer.slice(0, buffer.byteLength - 1); |
| 90 parser.appendData(buffer); | 91 parser.appendData(buffer); |
| 91 parser.appendData(base.encodeUtf8('</iq>')); | 92 parser.appendData(base.encodeUtf8('</iq>')); |
| 92 sinon.assert.calledWith(onError); | 93 sinon.assert.calledWith(onError); |
| 93 }); | 94 }); |
| 94 | 95 |
| 95 })(); | 96 })(); |
| OLD | NEW |