summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/unittests/xmpp_connection_unittest.js
blob: f99f94c008295e7e678797bc0ba09c8090c42f4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(function() {

'use strict';

var testUsername = 'testUsername@gmail.com';
var testToken = 'testToken';
var socketId = 3;

/** @type {(sinon.Spy|function(remoting.SignalStrategy.State):void)} */
var onStateChange = function() {};

var onStanzaStr = null;

/** @type {remoting.XmppConnection} */
var connection = null;

module('XmppConnection', {
  setup: function() {
    onStateChange = sinon.spy();
    onStanzaStr = sinon.spy();
    /** @param {Element} stanza */
    function onStanza(stanza) {
      onStanzaStr(new XMLSerializer().serializeToString(stanza));
    }

    sinon.$setupStub(chrome.socket, 'create');
    sinon.$setupStub(chrome.socket, 'connect');
    sinon.$setupStub(chrome.socket, 'write');
    sinon.$setupStub(chrome.socket, 'read');
    sinon.$setupStub(chrome.socket, 'destroy');
    sinon.$setupStub(chrome.socket, 'secure');

    connection = new remoting.XmppConnection();
    connection.setStateChangedCallback(
        /** @type {function(remoting.SignalStrategy.State):void} */
            (onStateChange));
    connection.setIncomingStanzaCallback(onStanza);
  },

  teardown: function() {
    chrome.socket.create.$testStub.restore();
    chrome.socket.connect.$testStub.restore();
    chrome.socket.write.$testStub.restore();
    chrome.socket.read.$testStub.restore();
    chrome.socket.destroy.$testStub.restore();
    chrome.socket.secure.$testStub.restore();
  }
});

test('should go to FAILED state when failed to connect', function() {
  connection.connect(
      'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken');
  sinon.assert.calledWith(onStateChange,
                          remoting.SignalStrategy.State.CONNECTING);
  sinon.assert.calledWith(chrome.socket.create, "tcp", {});
  chrome.socket.create.$testStub.getCall(0).args[2]({socketId: socketId});

  sinon.assert.calledWith(
      chrome.socket.connect, socketId, "xmpp.example.com", 123);
  chrome.socket.connect.$testStub.getCall(0).args[3](-1);

  QUnit.equal(connection.getError().tag, remoting.Error.Tag.NETWORK_FAILURE);
});

test('should use XmppLoginHandler to complete handshake and read data',
     function() {
  connection.connect(
      'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken');
  sinon.assert.calledWith(onStateChange,
                          remoting.SignalStrategy.State.CONNECTING);
  sinon.assert.calledWith(chrome.socket.create, "tcp", {});
  chrome.socket.create.$testStub.getCall(0).args[2]({socketId: socketId});

  sinon.assert.calledWith(
      chrome.socket.connect, socketId, "xmpp.example.com", 123);
  chrome.socket.connect.$testStub.getCall(0).args[3](0);

  sinon.assert.calledWith(onStateChange,
                          remoting.SignalStrategy.State.HANDSHAKE);

  var parser = new remoting.XmppStreamParser();
  var parserMock = sinon.mock(parser);
  var setCallbacksCalled = parserMock.expects('setCallbacks').once();
  var handshakeDoneCallback =
      connection.loginHandler_.getHandshakeDoneCallbackForTesting();
  handshakeDoneCallback('test@example.com/123123', parser);
  sinon.assert.calledWith(onStateChange,
                          remoting.SignalStrategy.State.CONNECTED);
  setCallbacksCalled.verify();

  // Simulate read() callback with |data|. It should be passed to the parser.
  var data = base.encodeUtf8('<iq id="1">hello</iq>');
  sinon.assert.calledWith(chrome.socket.read, socketId);
  var appendDataCalled = parserMock.expects('appendData').once().withArgs(data);
  chrome.socket.read.$testStub.getCall(0).args[1]({resultCode: 0, data: data});
  appendDataCalled.verify();
});

})();