| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * Unit tests for host_controller.js. | 7 * Unit tests for host_controller.js. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 (function() { | 10 (function() { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 /** @type {remoting.MockHostListApi} */ | 74 /** @type {remoting.MockHostListApi} */ |
| 75 var mockHostListApi; | 75 var mockHostListApi; |
| 76 | 76 |
| 77 QUnit.module('host_controller', { | 77 QUnit.module('host_controller', { |
| 78 beforeEach: function(/** QUnit.Assert */ assert) { | 78 beforeEach: function(/** QUnit.Assert */ assert) { |
| 79 chromeMocks.activate(['identity', 'runtime']); | 79 chromeMocks.activate(['identity', 'runtime']); |
| 80 chromeMocks.identity.mock$setToken(FAKE_IDENTITY_TOKEN); | 80 chromeMocks.identity.mock$setToken(FAKE_IDENTITY_TOKEN); |
| 81 remoting.settings = new remoting.Settings(); | 81 remoting.settings = new remoting.Settings(); |
| 82 remoting.identity = new remoting.Identity(); | 82 remoting.identity = new remoting.Identity(); |
| 83 mockHostListApi = new remoting.MockHostListApi; | 83 mockHostListApi = new remoting.MockHostListApi; |
| 84 mockHostListApi.registerResult = FAKE_AUTH_CODE; | 84 mockHostListApi.authCodeFromRegister = FAKE_AUTH_CODE; |
| 85 mockHostListApi.emailFromRegister = ''; |
| 85 remoting.HostListApi.setInstance(mockHostListApi); | 86 remoting.HostListApi.setInstance(mockHostListApi); |
| 86 base.debug.assert(remoting.oauth2 === null); | 87 base.debug.assert(remoting.oauth2 === null); |
| 87 remoting.oauth2 = new remoting.OAuth2(); | 88 remoting.oauth2 = new remoting.OAuth2(); |
| 88 base.debug.assert(remoting.hostList === null); | 89 base.debug.assert(remoting.hostList === null); |
| 89 remoting.hostList = /** @type {remoting.HostList} */ | 90 remoting.hostList = /** @type {remoting.HostList} */ |
| 90 (Object.create(remoting.HostList.prototype)); | 91 (Object.create(remoting.HostList.prototype)); |
| 91 | 92 |
| 92 // When the HostList's unregisterHostById method is called, make | 93 // When the HostList's unregisterHostById method is called, make |
| 93 // sure the argument is correct. | 94 // sure the argument is correct. |
| 94 unregisterHostByIdSpy = | 95 unregisterHostByIdSpy = |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); | 244 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); |
| 244 assert.equal(unregisterHostByIdSpy.callCount, 0); | 245 assert.equal(unregisterHostByIdSpy.callCount, 0); |
| 245 assert.equal(onLocalHostStartedSpy.callCount, 0); | 246 assert.equal(onLocalHostStartedSpy.callCount, 0); |
| 246 assert.equal(startDaemonSpy.callCount, 0); | 247 assert.equal(startDaemonSpy.callCount, 0); |
| 247 }); | 248 }); |
| 248 }); | 249 }); |
| 249 | 250 |
| 250 // Check what happens when the registry returns an HTTP when we try to | 251 // Check what happens when the registry returns an HTTP when we try to |
| 251 // register a host. | 252 // register a host. |
| 252 QUnit.test('start with host registration failure', function(assert) { | 253 QUnit.test('start with host registration failure', function(assert) { |
| 253 mockHostListApi.registerResult = null; | 254 mockHostListApi.authCodeFromRegister = null; |
| 254 return controller.start(FAKE_HOST_PIN, true).then(function() { | 255 return controller.start(FAKE_HOST_PIN, true).then(function() { |
| 255 throw 'test failed'; | 256 throw 'test failed'; |
| 256 }, function(/** remoting.Error */ e) { | 257 }, function(/** remoting.Error */ e) { |
| 257 assert.equal(e.getTag(), remoting.Error.Tag.REGISTRATION_FAILED); | 258 assert.equal(e.getTag(), remoting.Error.Tag.REGISTRATION_FAILED); |
| 258 assert.equal(unregisterHostByIdSpy.callCount, 0); | 259 assert.equal(unregisterHostByIdSpy.callCount, 0); |
| 259 assert.equal(onLocalHostStartedSpy.callCount, 0); | 260 assert.equal(onLocalHostStartedSpy.callCount, 0); |
| 260 assert.equal(startDaemonSpy.callCount, 0); | 261 assert.equal(startDaemonSpy.callCount, 0); |
| 261 }); | 262 }); |
| 262 }); | 263 }); |
| 263 | 264 |
| 264 // Check what happens when the HostDaemonFacade's | 265 // Check what happens when the HostDaemonFacade's |
| 265 // getCredentialsFromAuthCode method fails. | 266 // getCredentialsFromAuthCode method fails. |
| 266 QUnit.test('start with getCredentialsFromAuthCode failure', function(assert) { | 267 QUnit.test('start with getCredentialsFromAuthCode failure', function(assert) { |
| 267 mockHostDaemonFacade.useEmail = null; | 268 mockHostDaemonFacade.useEmail = null; |
| 268 mockHostDaemonFacade.refreshToken = null; | 269 mockHostDaemonFacade.refreshToken = null; |
| 269 return controller.start(FAKE_HOST_PIN, true).then(function() { | 270 return controller.start(FAKE_HOST_PIN, true).then(function() { |
| 270 throw 'test failed'; | 271 throw 'test failed'; |
| 271 }, function(/** remoting.Error */ e) { | 272 }, function(/** remoting.Error */ e) { |
| 272 assert.equal(e.getDetail(), 'getCredentialsFromAuthCode'); | 273 assert.equal(e.getDetail(), 'getCredentialsFromAuthCode'); |
| 273 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); | 274 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); |
| 274 assert.equal(getCredentialsFromAuthCodeSpy.callCount, 1); | 275 assert.equal(getCredentialsFromAuthCodeSpy.callCount, 1); |
| 275 assert.equal(onLocalHostStartedSpy.callCount, 0); | 276 assert.equal(onLocalHostStartedSpy.callCount, 0); |
| 276 assert.equal(startDaemonSpy.callCount, 0); | 277 assert.equal(startDaemonSpy.callCount, 0); |
| 277 }); | 278 }); |
| 278 }); | 279 }); |
| 279 | 280 |
| 280 // Check what happens when the HostDaemonFacade's getPinHash method | |
| 281 // fails, and verify that getPinHash is called when the registry | |
| 282 // does't return an auth code. | |
| 283 QUnit.test('start with getRefreshToken+getPinHash failure', function(assert) { | |
| 284 mockHostDaemonFacade.pinHashFunc = null; | |
| 285 mockHostListApi.registerResult = ''; | |
| 286 return controller.start(FAKE_HOST_PIN, true).then(function() { | |
| 287 throw 'test failed'; | |
| 288 }, function(/** remoting.Error */ e) { | |
| 289 assert.equal(e.getDetail(), 'getPinHash'); | |
| 290 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); | |
| 291 assert.equal(onLocalHostStartedSpy.callCount, 0); | |
| 292 assert.equal(startDaemonSpy.callCount, 0); | |
| 293 }); | |
| 294 }); | |
| 295 | |
| 296 // Check what happens when the SignalStrategy fails to connect. | 281 // Check what happens when the SignalStrategy fails to connect. |
| 297 QUnit.test('start with signalStrategy failure', function(assert) { | 282 QUnit.test('start with signalStrategy failure', function(assert) { |
| 298 stubSignalStrategyConnect(false); | 283 stubSignalStrategyConnect(false); |
| 299 return controller.start(FAKE_HOST_PIN, true).then(function() { | 284 return controller.start(FAKE_HOST_PIN, true).then(function() { |
| 300 throw 'test failed'; | 285 throw 'test failed'; |
| 301 }, function(/** remoting.Error */ e) { | 286 }, function(/** remoting.Error */ e) { |
| 302 assert.equal(e.getDetail(), 'setStateForTesting'); | 287 assert.equal(e.getDetail(), 'setStateForTesting'); |
| 303 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); | 288 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); |
| 304 assert.equal(unregisterHostByIdSpy.callCount, 1); | 289 assert.equal(unregisterHostByIdSpy.callCount, 1); |
| 305 }); | 290 }); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 }, function(/** remoting.Error */ e) { | 334 }, function(/** remoting.Error */ e) { |
| 350 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); | 335 assert.equal(e.getTag(), remoting.Error.Tag.UNEXPECTED); |
| 351 assert.equal(unregisterHostByIdSpy.callCount, 1); | 336 assert.equal(unregisterHostByIdSpy.callCount, 1); |
| 352 assert.equal(onLocalHostStartedSpy.callCount, 0); | 337 assert.equal(onLocalHostStartedSpy.callCount, 0); |
| 353 }); | 338 }); |
| 354 }); | 339 }); |
| 355 | 340 |
| 356 // Check what happens when the entire host registration process | 341 // Check what happens when the entire host registration process |
| 357 // succeeds. | 342 // succeeds. |
| 358 [false, true].forEach(function(/** boolean */ consent) { | 343 [false, true].forEach(function(/** boolean */ consent) { |
| 359 QUnit.test('start with auth code, consent=' + consent, function(assert) { | 344 QUnit.test('start with consent=' + consent, function(assert) { |
| 360 /** @const */ | 345 /** @const */ |
| 361 var fakePinHash = fakePinHashFunc(FAKE_HOST_ID, FAKE_HOST_PIN); | 346 var fakePinHash = fakePinHashFunc(FAKE_HOST_ID, FAKE_HOST_PIN); |
| 362 stubSignalStrategyConnect(true); | 347 stubSignalStrategyConnect(true); |
| 363 return controller.start(FAKE_HOST_PIN, consent).then(function() { | 348 return controller.start(FAKE_HOST_PIN, consent).then(function() { |
| 364 assert.equal(getCredentialsFromAuthCodeSpy.callCount, 1); | 349 assert.equal(getCredentialsFromAuthCodeSpy.callCount, 1); |
| 365 assert.deepEqual( | 350 assert.deepEqual( |
| 366 getCredentialsFromAuthCodeSpy.args[0][0], | 351 getCredentialsFromAuthCodeSpy.args[0][0], |
| 367 FAKE_AUTH_CODE); | 352 FAKE_AUTH_CODE); |
| 368 assert.equal(getPinHashSpy.callCount, 1); | 353 assert.equal(getPinHashSpy.callCount, 1); |
| 369 assert.deepEqual( | 354 assert.deepEqual( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 381 host_owner_email: FAKE_USER_EMAIL, | 366 host_owner_email: FAKE_USER_EMAIL, |
| 382 host_id: FAKE_HOST_ID, | 367 host_id: FAKE_HOST_ID, |
| 383 host_name: FAKE_HOST_NAME, | 368 host_name: FAKE_HOST_NAME, |
| 384 host_secret_hash: fakePinHash, | 369 host_secret_hash: fakePinHash, |
| 385 private_key: FAKE_PRIVATE_KEY | 370 private_key: FAKE_PRIVATE_KEY |
| 386 }, consent]); | 371 }, consent]); |
| 387 }); | 372 }); |
| 388 }); | 373 }); |
| 389 }); | 374 }); |
| 390 | 375 |
| 391 // Check alternative host registration without a registry-supplied | |
| 392 // auth code. | |
| 393 [false, true].forEach(function(/** boolean */ consent) { | |
| 394 QUnit.test('start without auth code, consent=' + consent, function(assert) { | |
| 395 /** @const */ | |
| 396 var fakePinHash = fakePinHashFunc(FAKE_HOST_ID, FAKE_HOST_PIN); | |
| 397 mockHostListApi.registerResult = ''; | |
| 398 stubSignalStrategyConnect(true); | |
| 399 return controller.start(FAKE_HOST_PIN, consent).then(function() { | |
| 400 assert.equal(getCredentialsFromAuthCodeSpy.callCount, 0); | |
| 401 assert.equal(getPinHashSpy.callCount, 1); | |
| 402 assert.deepEqual( | |
| 403 getPinHashSpy.args[0].slice(0, 2), | |
| 404 [FAKE_HOST_ID, FAKE_HOST_PIN]); | |
| 405 assert.equal(unregisterHostByIdSpy.callCount, 0); | |
| 406 assert.equal(onLocalHostStartedSpy.callCount, 1); | |
| 407 assert.equal(startDaemonSpy.callCount, 1); | |
| 408 assert.deepEqual( | |
| 409 startDaemonSpy.args[0].slice(0, 2), | |
| 410 [{ | |
| 411 xmpp_login: FAKE_USER_EMAIL, | |
| 412 oauth_refresh_token: FAKE_REFRESH_TOKEN, | |
| 413 host_id: FAKE_HOST_ID, | |
| 414 host_name: FAKE_HOST_NAME, | |
| 415 host_secret_hash: fakePinHash, | |
| 416 private_key: FAKE_PRIVATE_KEY | |
| 417 }, consent]); | |
| 418 }); | |
| 419 }); | |
| 420 }); | |
| 421 | |
| 422 // Check what happens when stopDaemon calls onError. | 376 // Check what happens when stopDaemon calls onError. |
| 423 // TODO(jrw): Should stopDaemon even have an onError callback? | 377 // TODO(jrw): Should stopDaemon even have an onError callback? |
| 424 QUnit.test('stop with stopDaemon failure', function(assert) { | 378 QUnit.test('stop with stopDaemon failure', function(assert) { |
| 425 mockHostDaemonFacade.stopDaemonResult = null; | 379 mockHostDaemonFacade.stopDaemonResult = null; |
| 426 return new Promise(function(resolve, reject) { | 380 return new Promise(function(resolve, reject) { |
| 427 controller.stop(function() { | 381 controller.stop(function() { |
| 428 reject('test failed'); | 382 reject('test failed'); |
| 429 }, function(/** remoting.Error */ e) { | 383 }, function(/** remoting.Error */ e) { |
| 430 assert.equal(e.getDetail(), 'stopDaemon'); | 384 assert.equal(e.getDetail(), 'stopDaemon'); |
| 431 // TODO(jrw): Is it really desirable to leave the host registered? | 385 // TODO(jrw): Is it really desirable to leave the host registered? |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 637 resolve(null); | 591 resolve(null); |
| 638 }); | 592 }); |
| 639 }); | 593 }); |
| 640 }); | 594 }); |
| 641 | 595 |
| 642 // Tests omitted for hasFeature, getPairedClients, deletePairedClient, | 596 // Tests omitted for hasFeature, getPairedClients, deletePairedClient, |
| 643 // and clearPairedClients because they simply call through to | 597 // and clearPairedClients because they simply call through to |
| 644 // HostDaemonFacade. | 598 // HostDaemonFacade. |
| 645 | 599 |
| 646 })(); | 600 })(); |
| OLD | NEW |