| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.net.xpc.NativeMessagingTransportTest'); |
| 16 |
goog.setTestOnly('goog.net.xpc.NativeMessagingTransportTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.events'); |
| 20 |
goog.require('goog.net.xpc'); |
| 21 |
goog.require('goog.net.xpc.CfgFields'); |
| 22 |
goog.require('goog.net.xpc.CrossPageChannel'); |
| 23 |
goog.require('goog.net.xpc.CrossPageChannelRole'); |
| 24 |
goog.require('goog.net.xpc.NativeMessagingTransport'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
|
| 27 |
// This test only tests the native messaing transport protocol version 2. |
| 28 |
// Testing of previous versions and of backward/forward compatibility is done |
| 29 |
// in crosspagechannel_test.html. |
| 30 |
|
| 31 |
|
| 32 |
function tearDown() { |
| 33 |
goog.net.xpc.NativeMessagingTransport.activeCount_ = {}; |
| 34 |
goog.events.removeAll(window.postMessage ? window : document, 'message'); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
function testConstructor() { |
| 39 |
var xpc = getTestChannel(); |
| 40 |
|
| 41 |
var t = new goog.net.xpc.NativeMessagingTransport( |
| 42 |
xpc, 'http://g.com:80', undefined /* opt_domHelper */, |
| 43 |
false /* opt_oneSidedHandshake */, 2 /* opt_protocolVersion */); |
| 44 |
assertEquals('http://g.com:80', t.peerHostname_); |
| 45 |
|
| 46 |
var t = new goog.net.xpc.NativeMessagingTransport( |
| 47 |
xpc, null /* peerHostName */, undefined /* opt_domHelper */, |
| 48 |
false /* opt_oneSidedHandshake */, 2 /* opt_protocolVersion */); |
| 49 |
assertEquals('*', t.peerHostname_); |
| 50 |
t.dispose(); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
function testConstructorDom() { |
| 55 |
var xpc = getTestChannel(); |
| 56 |
|
| 57 |
var t = new goog.net.xpc.NativeMessagingTransport( |
| 58 |
xpc, 'http://g.com:80', goog.dom.getDomHelper(), |
| 59 |
false /* opt_oneSidedHandshake */, 2 /* opt_protocolVersion */); |
| 60 |
assertEquals('http://g.com:80', t.peerHostname_); |
| 61 |
|
| 62 |
var t = new goog.net.xpc.NativeMessagingTransport( |
| 63 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 64 |
2 /* opt_protocolVersion */); |
| 65 |
assertEquals('*', t.peerHostname_); |
| 66 |
t.dispose(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
function testDispose() { |
| 71 |
var xpc = getTestChannel(); |
| 72 |
var listenedObj = window.postMessage ? window : document; |
| 73 |
|
| 74 |
var t0 = new goog.net.xpc.NativeMessagingTransport( |
| 75 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 76 |
2 /* opt_protocolVersion */); |
| 77 |
assertEquals(0, goog.events.removeAll(listenedObj, 'message')); |
| 78 |
t0.dispose(); |
| 79 |
assertEquals(0, goog.events.removeAll(listenedObj, 'message')); |
| 80 |
|
| 81 |
var t1 = new goog.net.xpc.NativeMessagingTransport( |
| 82 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 83 |
2 /* opt_protocolVersion */); |
| 84 |
t1.connect(); |
| 85 |
t1.dispose(); |
| 86 |
assertEquals(0, goog.events.removeAll(listenedObj, 'message')); |
| 87 |
|
| 88 |
var t2 = new goog.net.xpc.NativeMessagingTransport( |
| 89 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 90 |
2 /* opt_protocolVersion */); |
| 91 |
var t3 = new goog.net.xpc.NativeMessagingTransport( |
| 92 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 93 |
2 /* opt_protocolVersion */); |
| 94 |
t2.connect(); |
| 95 |
t3.connect(); |
| 96 |
t2.dispose(); |
| 97 |
assertEquals(1, goog.events.removeAll(listenedObj, 'message')); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
function testDisposeWithDom() { |
| 102 |
var xpc = getTestChannel(goog.dom.getDomHelper()); |
| 103 |
var listenedObj = window.postMessage ? window : document; |
| 104 |
|
| 105 |
var t0 = new goog.net.xpc.NativeMessagingTransport( |
| 106 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 107 |
2 /* opt_protocolVersion */); |
| 108 |
assertEquals(0, goog.events.removeAll(listenedObj, 'message')); |
| 109 |
t0.dispose(); |
| 110 |
assertEquals(0, goog.events.removeAll(listenedObj, 'message')); |
| 111 |
|
| 112 |
var t1 = new goog.net.xpc.NativeMessagingTransport( |
| 113 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 114 |
2 /* opt_protocolVersion */); |
| 115 |
t1.connect(); |
| 116 |
t1.dispose(); |
| 117 |
assertEquals(0, goog.events.removeAll(listenedObj, 'message')); |
| 118 |
|
| 119 |
var t2 = new goog.net.xpc.NativeMessagingTransport( |
| 120 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 121 |
2 /* opt_protocolVersion */); |
| 122 |
var t3 = new goog.net.xpc.NativeMessagingTransport( |
| 123 |
xpc, null /* peerHostName */, false /* opt_oneSidedHandshake */, |
| 124 |
2 /* opt_protocolVersion */); |
| 125 |
t2.connect(); |
| 126 |
t3.connect(); |
| 127 |
t2.dispose(); |
| 128 |
assertEquals(1, goog.events.removeAll(listenedObj, 'message')); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
function testBogusMessages() { |
| 133 |
var e = createMockEvent('origin_unknown', 'bogus_message'); |
| 134 |
assertFalse(goog.net.xpc.NativeMessagingTransport.messageReceived_(e)); |
| 135 |
|
| 136 |
e = createMockEvent('origin_unknown', 'bogus|message'); |
| 137 |
assertFalse(goog.net.xpc.NativeMessagingTransport.messageReceived_(e)); |
| 138 |
|
| 139 |
e = createMockEvent('origin_unknown', 'bogus|message:data'); |
| 140 |
assertFalse(goog.net.xpc.NativeMessagingTransport.messageReceived_(e)); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
function testSendingMessagesToUnconnectedInnerPeer() { |
| 145 |
var xpc = getTestChannel(); |
| 146 |
|
| 147 |
var serviceResult, payloadResult; |
| 148 |
xpc.xpcDeliver = function(service, payload) { |
| 149 |
serviceResult = service; |
| 150 |
payloadResult = payload; |
| 151 |
}; |
| 152 |
|
| 153 |
// Construct an unconnected inner peer. |
| 154 |
xpc.getRole = function() { return goog.net.xpc.CrossPageChannelRole.INNER; }; |
| 155 |
xpc.isConnected = function() { return false; }; |
| 156 |
var t = new goog.net.xpc.NativeMessagingTransport( |
| 157 |
xpc, 'http://g.com', false /* opt_oneSidedHandshake */, |
| 158 |
2 /* opt_protocolVersion */); |
| 159 |
|
| 160 |
// Test a valid message. |
| 161 |
var e = createMockEvent( |
| 162 |
'origin_unknown', 'test_channel|test_service:test_payload'); |
| 163 |
assertTrue(goog.net.xpc.NativeMessagingTransport.messageReceived_(e)); |
| 164 |
assertEquals('test_service', serviceResult); |
| 165 |
assertEquals('test_payload', payloadResult); |
| 166 |
assertEquals( |
| 167 |
'Ensure channel name has not been changed.', 'test_channel', |
| 168 |
t.channel_.name); |
| 169 |
|
| 170 |
// Test that sending a SETUP message from an untrusted origin doesn't update |
| 171 |
// the channel name. This is a regression test for b/33746803. |
| 172 |
var e = createMockEvent('untrusted_origin', 'new_channel|tp:SETUP'); |
| 173 |
assertFalse(goog.net.xpc.NativeMessagingTransport.messageReceived_(e)); |
| 174 |
assertEquals( |
| 175 |
'Channel name should not change from untrusted origin', 'test_channel', |
| 176 |
t.channel_.name); |
| 177 |
|
| 178 |
// Test updating a stale inner peer. |
| 179 |
var e = createMockEvent('trusted_origin', 'new_channel|tp:SETUP'); |
| 180 |
assertTrue(goog.net.xpc.NativeMessagingTransport.messageReceived_(e)); |
| 181 |
assertEquals('tp', serviceResult); |
| 182 |
assertEquals('SETUP', payloadResult); |
| 183 |
assertEquals( |
| 184 |
'Ensure channel name has been updated.', 'new_channel', t.channel_.name); |
| 185 |
t.dispose(); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
function testSignalConnected_innerFrame() { |
| 190 |
checkSignalConnected(false /* oneSidedHandshake */, true /* innerFrame */); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
function testSignalConnected_outerFrame() { |
| 195 |
checkSignalConnected(false /* oneSidedHandshake */, false /* innerFrame */); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
function testSignalConnected_singleSided_innerFrame() { |
| 200 |
checkSignalConnected(true /* oneSidedHandshake */, true /* innerFrame */); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
function testSignalConnected_singleSided_outerFrame() { |
| 205 |
checkSignalConnected(true /* oneSidedHandshake */, false /* innerFrame */); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
function checkSignalConnected( |
| 210 |
oneSidedHandshake, innerFrame, peerProtocolVersion, protocolVersion) { |
| 211 |
var xpc = getTestChannel(); |
| 212 |
var connected = false; |
| 213 |
xpc.notifyConnected = function() { |
| 214 |
if (connected) { |
| 215 |
fail(); |
| 216 |
} else { |
| 217 |
connected = true; |
| 218 |
} |
| 219 |
}; |
| 220 |
xpc.getRole = function() { |
| 221 |
return innerFrame ? goog.net.xpc.CrossPageChannelRole.INNER : |
| 222 |
goog.net.xpc.CrossPageChannelRole.OUTER; |
| 223 |
}; |
| 224 |
xpc.isConnected = function() { return false; }; |
| 225 |
|
| 226 |
var transport = new goog.net.xpc.NativeMessagingTransport( |
| 227 |
xpc, 'http://g.com', undefined /* opt_domHelper */, |
| 228 |
oneSidedHandshake /* opt_oneSidedHandshake */, 2 /* protocolVerion */); |
| 229 |
var sentPayloads = []; |
| 230 |
transport.send = function(service, payload) { |
| 231 |
assertEquals(goog.net.xpc.TRANSPORT_SERVICE_, service); |
| 232 |
sentPayloads.push(payload); |
| 233 |
}; |
| 234 |
function assertSent(payloads) { |
| 235 |
assertArrayEquals(payloads, sentPayloads); |
| 236 |
sentPayloads = []; |
| 237 |
} |
| 238 |
var endpointId = transport.endpointId_; |
| 239 |
var peerEndpointId1 = 'abc123'; |
| 240 |
var peerEndpointId2 = 'def234'; |
| 241 |
|
| 242 |
assertFalse(connected); |
| 243 |
if (!oneSidedHandshake || innerFrame) { |
| 244 |
transport.transportServiceHandler( |
| 245 |
goog.net.xpc.SETUP_NTPV2 + ',' + peerEndpointId1); |
| 246 |
transport.transportServiceHandler(goog.net.xpc.SETUP); |
| 247 |
assertSent([goog.net.xpc.SETUP_ACK_NTPV2]); |
| 248 |
assertFalse(connected); |
| 249 |
transport.transportServiceHandler(goog.net.xpc.SETUP_ACK_NTPV2); |
| 250 |
assertSent([]); |
| 251 |
assertTrue(connected); |
| 252 |
} else { |
| 253 |
transport.transportServiceHandler(goog.net.xpc.SETUP_ACK_NTPV2); |
| 254 |
assertSent([]); |
| 255 |
assertFalse(connected); |
| 256 |
transport.transportServiceHandler( |
| 257 |
goog.net.xpc.SETUP_NTPV2 + ',' + peerEndpointId1); |
| 258 |
transport.transportServiceHandler(goog.net.xpc.SETUP); |
| 259 |
assertSent([goog.net.xpc.SETUP_ACK_NTPV2]); |
| 260 |
assertTrue(connected); |
| 261 |
} |
| 262 |
|
| 263 |
// Verify that additional transport service traffic doesn't cause duplicate |
| 264 |
// notifications. |
| 265 |
transport.transportServiceHandler( |
| 266 |
goog.net.xpc.SETUP_NTPV2 + ',' + peerEndpointId1); |
| 267 |
transport.transportServiceHandler(goog.net.xpc.SETUP); |
| 268 |
assertSent([goog.net.xpc.SETUP_ACK_NTPV2]); |
| 269 |
transport.transportServiceHandler(goog.net.xpc.SETUP_ACK_NTPV2); |
| 270 |
assertSent([]); |
| 271 |
|
| 272 |
// Simulate a reconnection by sending a SETUP message from a frame with a |
| 273 |
// different endpoint id. No further connection callbacks should fire, but |
| 274 |
// a new SETUP message should be triggered. |
| 275 |
transport.transportServiceHandler( |
| 276 |
goog.net.xpc.SETUP_NTPV2 + ',' + peerEndpointId2); |
| 277 |
transport.transportServiceHandler(goog.net.xpc.SETUP); |
| 278 |
assertSent([ |
| 279 |
goog.net.xpc.SETUP_ACK_NTPV2, goog.net.xpc.SETUP_NTPV2 + ',' + endpointId |
| 280 |
]); |
| 281 |
transport.transportServiceHandler(goog.net.xpc.SETUP_ACK_NTPV2); |
| 282 |
assertSent([]); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
/** |
| 287 |
* Creates a Mock Event object used to test browser events. |
| 288 |
* @param {string} origin The URI origin, or '*', of the event. |
| 289 |
* @param {string} data The data to associate with the event. |
| 290 |
* @return {Object} The created object representing a browser event. |
| 291 |
*/ |
| 292 |
function createMockEvent(origin, data) { |
| 293 |
var event = {}; |
| 294 |
event.getBrowserEvent = function() { |
| 295 |
return {origin: origin, data: data}; |
| 296 |
}; |
| 297 |
return event; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
function getTestChannel(opt_domHelper) { |
| 302 |
var cfg = {}; |
| 303 |
cfg[goog.net.xpc.CfgFields.CHANNEL_NAME] = 'test_channel'; |
| 304 |
cfg[goog.net.xpc.CfgFields.PEER_HOSTNAME] = 'trusted_origin'; |
| 305 |
return new goog.net.xpc.CrossPageChannel( |
| 306 |
cfg, opt_domHelper, undefined /* opt_domHelper */, |
| 307 |
false /* opt_oneSidedHandshake */, 2 /* opt_protocolVersion */); |
| 308 |
} |