1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "MPL"); you may not use this file
6
* except in compliance with the MPL. You may obtain a copy of
7
* the MPL at http://www.mozilla.org/MPL/
9
* Software distributed under the MPL is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the MPL for the specific language governing
12
* rights and limitations under the MPL.
14
* The Original Code is ipc-pipe.
16
* The Initial Developer of the Original Code is Patrick Brunschwig.
17
* Portions created by Patrick Brunschwig <patrick@mozilla-enigmail.org> are
18
* Copyright (C) 2010 Patrick Brunschwig. All Rights Reserved.
22
* Alternatively, the contents of this file may be used under the terms of
23
* either the GNU General Public License Version 2 or later (the "GPL"), or
24
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25
* in which case the provisions of the GPL or the LGPL are applicable instead
26
* of those above. If you wish to allow use of your version of this file only
27
* under the terms of either the GPL or the LGPL, and not to allow others to
28
* use your version of this file under the terms of the MPL, indicate your
29
* decision by deleting the provisions above and replace them with the notice
30
* and other provisions required by the GPL or the LGPL. If you do not delete
31
* the provisions above, a recipient may use your version of this file under
32
* the terms of any one of the MPL, the GPL or the LGPL.
33
* ***** END LICENSE BLOCK ***** */
36
* This file tests the implementation of nsIPCBuffer.cpp
40
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
42
const Cc = Components.classes;
43
const Ci = Components.interfaces;
45
function TestObserver(cbFunc, ipcBuffer) {
46
this._cbFunc = cbFunc;
47
this._ipcBuffer = ipcBuffer;
50
TestObserver.prototype = {
51
QueryInterface: XPCOMUtils.generateQI([ Ci.nsIObserver ]),
53
onStartRequest: function(aRequest, aContext) {
54
if (gRequestCounter == 0)
60
onStopRequest: function(aRequest, aContext, aStatusCode) {
61
if (gRequestCounter == 1)
66
if (this._cbFunc) this._cbFunc(this._ipcBuffer);
67
this._ipcBuffer = null;
73
function prepareTestFile(file, data)
76
if (file.isDirectory() || !file.isWritable())
77
do_throw("cannot write to file ipc-data.txt");
80
var fileFlags = 0x02 | 0x08 | 0x20; // create, write, truncate
81
var fileStream = Cc["@mozilla.org/network/file-output-stream;1"].
82
createInstance(Ci.nsIFileOutputStream);
84
fileStream.init(file, fileFlags, 0600, 0);
85
if (fileStream.write(data, data.length) != data.length)
86
do_throw("Could not correctly write test file");
94
const NS_IPCBUFFER_CONTRACTID = "@mozilla.org/ipc/ipc-buffer;1";
95
const InputStream = new Components.Constructor(
96
"@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream" );
98
/////////////////////////////////////////////////////////////////
99
// Test IPC buffer with maxBytes = 10
100
/////////////////////////////////////////////////////////////////
102
var ipcBuffer = Cc[NS_IPCBUFFER_CONTRACTID].createInstance(Ci.nsIIPCBuffer);
104
var inputString="This string is testing IPCBuffer data flow\n";
108
var o = new TestObserver();
109
ipcBuffer.open(maxBytes, true);
110
ipcBuffer.observe(o, null);
111
ipcBuffer.onStartRequest(null, null);
112
ipcBuffer.write(inputString);
113
ipcBuffer.onStopRequest(null, null, 0);
115
var totalBytes = ipcBuffer.totalBytes;
116
do_check_eq(totalBytes, inputString.length);
118
var isStopped = ipcBuffer.stopped;
119
do_check_eq(isStopped, true);
121
var data = ipcBuffer.getData();
122
do_check_eq(maxBytes, data.length);
124
var rawInStream = ipcBuffer.openInputStream();
125
var scriptableInStream = new InputStream();
126
scriptableInStream.init(rawInStream);
128
var available = scriptableInStream.available();
129
do_check_eq(inputString.length, available);
131
var bufData = scriptableInStream.read(available);
134
do_check_eq(bufData, inputString);
136
ipcBuffer.shutdown();
137
do_check_eq(gRequestCounter, 2);
139
/////////////////////////////////////////////////////////////////
140
// Test IPC buffer with maxBytes = -1 (unlimited buffer size)
141
/////////////////////////////////////////////////////////////////
143
ipcBuffer = Cc[NS_IPCBUFFER_CONTRACTID].createInstance(Ci.nsIIPCBuffer);
148
o = new TestObserver();
149
ipcBuffer.open(maxBytes, true);
150
ipcBuffer.observe(o, null);
151
ipcBuffer.onStartRequest(null, null);
152
ipcBuffer.write(inputString);
153
ipcBuffer.onStopRequest(null, null, 0);
155
totalBytes = ipcBuffer.totalBytes;
156
do_check_eq(totalBytes, inputString.length);
158
isStopped = ipcBuffer.stopped;
159
do_check_eq(isStopped, true);
161
data = ipcBuffer.getData();
162
do_check_eq(inputString.length, data.length);
164
rawInStream = ipcBuffer.openInputStream();
165
scriptableInStream = new InputStream();
166
scriptableInStream.init(rawInStream);
168
available = scriptableInStream.available();
169
do_check_eq(inputString.length, available);
171
bufData = scriptableInStream.read(available);
174
do_check_eq(bufData, inputString);
176
ipcBuffer.shutdown();
177
do_check_eq(gRequestCounter, 2);
179
/////////////////////////////////////////////////////////////////
180
// Test IPC buffer with openURI - synchronous reading
181
/////////////////////////////////////////////////////////////////
185
var testFile = do_get_file("ipc-data.txt");
186
prepareTestFile(testFile, inputString);
190
var ioSvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
191
testURI = ioSvc.newFileURI(testFile);
193
ipcBuffer = Cc[NS_IPCBUFFER_CONTRACTID].createInstance(Ci.nsIIPCBuffer);
196
ipcBuffer.openURI(testURI, maxBytes, true, o, null, true);
198
isStopped = ipcBuffer.stopped;
199
do_check_eq(isStopped, true);
201
data = ipcBuffer.getData();
202
do_check_eq(inputString.substr(0,10), data);
204
rawInStream = ipcBuffer.openInputStream();
205
scriptableInStream = new InputStream();
206
scriptableInStream.init(rawInStream);
208
available = scriptableInStream.available();
209
do_check_eq(inputString.length, available);
211
bufData = scriptableInStream.read(available);
214
do_check_eq(bufData, inputString);
216
ipcBuffer.shutdown();
217
do_check_eq(gRequestCounter, 2);
219
/////////////////////////////////////////////////////////////////
220
// Test IPC buffer with openURI - asynchronous reading
221
/////////////////////////////////////////////////////////////////
223
ipcBuffer = Cc[NS_IPCBUFFER_CONTRACTID].createInstance(Ci.nsIIPCBuffer);
227
var cbFunc = function(ipcBuffer)
229
do_check_eq(gRequestCounter, 2);
231
do_check_eq(ipcBuffer.totalBytes, inputString.length);
233
do_check_eq(ipcBuffer.stopped, true);
235
let data = ipcBuffer.getData();
236
do_check_eq(inputString, data);
237
ipcBuffer.shutdown();
240
o = new TestObserver(cbFunc, ipcBuffer);
242
ipcBuffer.openURI(testURI, maxBytes, false, o, null, false);