summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/base/js/dns_blackhole_checker_unittest.js
blob: fc6ce8f025b88d574e79cf441fe7c2c35319c4f5 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2015 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.

/**
 * @fileoverview
 * TODO(garykac): Create interface for SignalStrategy.
 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility}
 */

(function() {

'use strict';

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

/** @type {(sinon.Spy|function(Element):void)} */
var onIncomingStanzaCallback = null;

/** @type {remoting.DnsBlackholeChecker} */
var checker = null;

/** @type {remoting.MockSignalStrategy} */
var signalStrategy = null;

/** @type {sinon.FakeXhr} */
var fakeXhr = null;

QUnit.module('dns_blackhole_checker', {
  beforeEach: function(assert) {
    sinon.useFakeXMLHttpRequest().onCreate = function(xhr) {
      QUnit.equal(fakeXhr, null, 'exactly one XHR is issued');
      fakeXhr = xhr;
    };

    remoting.settings = new remoting.Settings();
    onStateChange = sinon.spy();
    onIncomingStanzaCallback = sinon.spy();
    signalStrategy = new remoting.MockSignalStrategy();
    sinon.stub(signalStrategy, 'connect', base.doNothing);
    checker = new remoting.DnsBlackholeChecker(signalStrategy);

    checker.setStateChangedCallback(onStateChange);
    checker.setIncomingStanzaCallback(onIncomingStanzaCallback);

    sinon.assert.notCalled(onStateChange);
    sinon.assert.notCalled(signalStrategy.connect);
    checker.connect('server', 'username', 'authToken');
    sinon.assert.calledWith(signalStrategy.connect, 'server', 'username',
                            'authToken');

    assert.equal(
        fakeXhr.url, checker.url_,
        'the correct URL is requested');
  },
  afterEach: function() {
    base.dispose(checker);
    sinon.assert.calledWith(onStateChange,
                            remoting.SignalStrategy.State.CLOSED);
    remoting.settings = null;
    onStateChange = null;
    onIncomingStanzaCallback = null;
    checker = null;
    fakeXhr = null;
  }
});

QUnit.test('success',
  function(assert) {
    function checkState(state) {
      signalStrategy.setStateForTesting(state);
      sinon.assert.calledWith(onStateChange, state);
      assert.equal(checker.getState(), state);
    }

    return base.SpyPromise.run(function() {
      fakeXhr.respond(200);
    }).then(function() {
      sinon.assert.notCalled(onStateChange);
      checkState(remoting.SignalStrategy.State.CONNECTING);
      checkState(remoting.SignalStrategy.State.HANDSHAKE);
      checkState(remoting.SignalStrategy.State.CONNECTED);
    });
  });

QUnit.test('http response after connected',
  function(assert) {
    function checkState(state) {
      signalStrategy.setStateForTesting(state);
      sinon.assert.calledWith(onStateChange, state);
      assert.equal(checker.getState(), state);
    }

    checkState(remoting.SignalStrategy.State.CONNECTING);
    checkState(remoting.SignalStrategy.State.HANDSHAKE);
    onStateChange.reset();

    // Verify that DnsBlackholeChecker stays in HANDSHAKE state even if the
    // signal strategy has connected.
    return base.SpyPromise.run(function() {
      signalStrategy.setStateForTesting(
          remoting.SignalStrategy.State.CONNECTED);
    }).then(function() {
      sinon.assert.notCalled(onStateChange);
    assert.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);

      // Verify that DnsBlackholeChecker goes to CONNECTED state after the
      // the HTTP request has succeeded.
      return base.SpyPromise.run(function() {
        fakeXhr.respond(200);
      });
    }).then(function() {
      sinon.assert.calledWith(onStateChange,
                              remoting.SignalStrategy.State.CONNECTED);
    });
  });

QUnit.test('connect failed',
  function(assert) {
    function checkState(state) {
      signalStrategy.setStateForTesting(state);
      sinon.assert.calledWith(onStateChange, state);
    };

    return base.SpyPromise.run(function() {
      fakeXhr.respond(200);
    }).then(function() {
      sinon.assert.notCalled(onStateChange);
      checkState(remoting.SignalStrategy.State.CONNECTING);
      checkState(remoting.SignalStrategy.State.FAILED);
    });
  });

QUnit.test('blocked',
  function(assert) {
    function checkState(state) {
    assert.equal(checker.getError().getTag(),
                 remoting.Error.Tag.NOT_AUTHORIZED);
      onStateChange.reset();
      signalStrategy.setStateForTesting(state);
      sinon.assert.notCalled(onStateChange);
      assert.equal(checker.getState(),
          checker.getState(),
          remoting.SignalStrategy.State.FAILED,
          'checker state is still FAILED');
    };

    return base.SpyPromise.run(function() {
      fakeXhr.respond(400);
    }).then(function() {
      sinon.assert.calledWith(
          onStateChange, remoting.SignalStrategy.State.FAILED);
      assert.equal(
          checker.getError().getTag(),
          remoting.Error.Tag.NOT_AUTHORIZED,
          'checker error is NOT_AUTHORIZED');
      checkState(remoting.SignalStrategy.State.CONNECTING);
      checkState(remoting.SignalStrategy.State.HANDSHAKE);
      checkState(remoting.SignalStrategy.State.FAILED);
    });
  });

QUnit.test('blocked after connected',
  function(assert) {
    function checkState(state) {
      signalStrategy.setStateForTesting(state);
      sinon.assert.calledWith(onStateChange, state);
      assert.equal(checker.getState(), state);
    };

    checkState(remoting.SignalStrategy.State.CONNECTING);
    checkState(remoting.SignalStrategy.State.HANDSHAKE);
    onStateChange.reset();

    // Verify that DnsBlackholeChecker stays in HANDSHAKE state even
    // if the signal strategy has connected.
    return base.SpyPromise.run(function() {
      signalStrategy.setStateForTesting(
          remoting.SignalStrategy.State.CONNECTED);
    }).then(function() {
      sinon.assert.notCalled(onStateChange);
    assert.equal(checker.getState(), remoting.SignalStrategy.State.HANDSHAKE);

      // Verify that DnsBlackholeChecker goes to FAILED state after it
      // gets the blocked HTTP response.
      return base.SpyPromise.run(function() {
        fakeXhr.respond(400);
      });
    }).then(function() {
      sinon.assert.calledWith(onStateChange,
                              remoting.SignalStrategy.State.FAILED);
    assert.ok(checker.getError().hasTag(remoting.Error.Tag.NOT_AUTHORIZED));
    });
  }
);

})();