~ubuntu-branches/ubuntu/trusty/enigmail/trusty-updates

« back to all changes in this revision

Viewing changes to extensions/enigmail/ipc/tests/unit/test_ipcbuffer.js

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2011-06-07 14:35:53 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: package-import@ubuntu.com-20110607143553-fbgqhhvh8g8h6j1y
Tags: 2:1.2~a2~cvs20110606t2200-0ubuntu1
* Update to latest trunk snapshot for Thunderbird beta compat

* Remove build/pgo/profileserver.py from debian/clean. The new build
  system has a target depending on this
  - update debian/clean
* Drop debian/patches/autoconf.diff, just generate this at build time
* Refresh debian/patches/build_system_dont_link_libxul.diff
* libipc seems to be renamed to libipc-pipe. Fix genxpi and chrome.manifest
  to fix this 
  - add debian/patches/ipc-pipe_rename.diff
  - update debian/patches/series
* The makefiles in extensions/enigmail/ipc have an incorrect DEPTH
  attribute. Fix this so that they can find the rest of the build system
  - add debian/patches/makefile_depth.diff
  - update debian/patches/series
* Drop debian/patches/makefile-in-empty-xpcom-fix.diff - fixed in the
  current version
* Don't register a class ID multiple times, as this breaks enigmail entirely
  - add debian/patches/dont_register_cids_multiple_times.diff
  - update debian/patches/series
* Look for the Thunderbird 5 SDK
  - update debian/rules
  - update debian/control
* Run autoconf2.13 at build time
  - update debian/rules
  - update debian/control
* Add useless mesa-common-dev build-dep, just to satisfy the build system.
  We should just patch this out entirely really, but that's for another upload
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
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/
 
8
 *
 
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.
 
13
 *
 
14
 * The Original Code is ipc-pipe.
 
15
 *
 
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.
 
19
 *
 
20
 * Contributor(s):
 
21
 *
 
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 ***** */
 
34
 
 
35
/**
 
36
 * This file tests the implementation of nsIPCBuffer.cpp
 
37
 */
 
38
 
 
39
 
 
40
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 
41
 
 
42
const Cc = Components.classes;
 
43
const Ci = Components.interfaces;
 
44
 
 
45
function TestObserver(cbFunc, ipcBuffer) {
 
46
  this._cbFunc = cbFunc;
 
47
  this._ipcBuffer = ipcBuffer;
 
48
}
 
49
 
 
50
TestObserver.prototype = {
 
51
  QueryInterface: XPCOMUtils.generateQI([ Ci.nsIObserver ]),
 
52
 
 
53
  onStartRequest: function(aRequest, aContext) {
 
54
    if (gRequestCounter == 0)
 
55
      gRequestCounter = 1;
 
56
    else
 
57
      gRequestCounter = 3;
 
58
  },
 
59
 
 
60
  onStopRequest: function(aRequest, aContext, aStatusCode) {
 
61
    if (gRequestCounter == 1)
 
62
      gRequestCounter = 2;
 
63
    else
 
64
      gRequestCounter = 3;
 
65
 
 
66
    if (this._cbFunc) this._cbFunc(this._ipcBuffer);
 
67
    this._ipcBuffer = null;
 
68
  }
 
69
}
 
70
 
 
71
var gRequestCounter;
 
72
 
 
73
function prepareTestFile(file, data)
 
74
{
 
75
  if (file.exists()) {
 
76
    if (file.isDirectory() || !file.isWritable())
 
77
       do_throw("cannot write to file ipc-data.txt");
 
78
  }
 
79
 
 
80
  var fileFlags = 0x02 | 0x08 | 0x20; // create, write, truncate
 
81
  var fileStream = Cc["@mozilla.org/network/file-output-stream;1"].
 
82
      createInstance(Ci.nsIFileOutputStream);
 
83
 
 
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");
 
87
 
 
88
  fileStream.flush();
 
89
  fileStream.close();
 
90
}
 
91
 
 
92
function run_test()
 
93
{
 
94
  const NS_IPCBUFFER_CONTRACTID   = "@mozilla.org/ipc/ipc-buffer;1";
 
95
  const InputStream = new Components.Constructor(
 
96
    "@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream" );
 
97
 
 
98
  /////////////////////////////////////////////////////////////////
 
99
  // Test IPC buffer with maxBytes = 10
 
100
  /////////////////////////////////////////////////////////////////
 
101
 
 
102
  var ipcBuffer = Cc[NS_IPCBUFFER_CONTRACTID].createInstance(Ci.nsIIPCBuffer);
 
103
 
 
104
  var inputString="This string is testing IPCBuffer data flow\n";
 
105
  var maxBytes = 10;
 
106
 
 
107
  gRequestCounter = 0;
 
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);
 
114
 
 
115
  var totalBytes = ipcBuffer.totalBytes;
 
116
  do_check_eq(totalBytes, inputString.length);
 
117
 
 
118
  var isStopped = ipcBuffer.stopped;
 
119
  do_check_eq(isStopped, true);
 
120
 
 
121
  var data = ipcBuffer.getData();
 
122
  do_check_eq(maxBytes, data.length);
 
123
 
 
124
  var rawInStream = ipcBuffer.openInputStream();
 
125
  var scriptableInStream = new InputStream();
 
126
  scriptableInStream.init(rawInStream);
 
127
 
 
128
  var available = scriptableInStream.available();
 
129
  do_check_eq(inputString.length, available);
 
130
 
 
131
  var bufData = scriptableInStream.read(available);
 
132
  rawInStream.close();
 
133
 
 
134
  do_check_eq(bufData, inputString);
 
135
 
 
136
  ipcBuffer.shutdown();
 
137
  do_check_eq(gRequestCounter, 2);
 
138
 
 
139
  /////////////////////////////////////////////////////////////////
 
140
  // Test IPC buffer with maxBytes = -1 (unlimited buffer size)
 
141
  /////////////////////////////////////////////////////////////////
 
142
 
 
143
  ipcBuffer = Cc[NS_IPCBUFFER_CONTRACTID].createInstance(Ci.nsIIPCBuffer);
 
144
 
 
145
  maxBytes = -1;
 
146
  gRequestCounter = 0;
 
147
 
 
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);
 
154
 
 
155
  totalBytes = ipcBuffer.totalBytes;
 
156
  do_check_eq(totalBytes, inputString.length);
 
157
 
 
158
  isStopped = ipcBuffer.stopped;
 
159
  do_check_eq(isStopped, true);
 
160
 
 
161
  data = ipcBuffer.getData();
 
162
  do_check_eq(inputString.length, data.length);
 
163
 
 
164
  rawInStream = ipcBuffer.openInputStream();
 
165
  scriptableInStream = new InputStream();
 
166
  scriptableInStream.init(rawInStream);
 
167
 
 
168
  available = scriptableInStream.available();
 
169
  do_check_eq(inputString.length, available);
 
170
 
 
171
  bufData = scriptableInStream.read(available);
 
172
  rawInStream.close();
 
173
 
 
174
  do_check_eq(bufData, inputString);
 
175
 
 
176
  ipcBuffer.shutdown();
 
177
  do_check_eq(gRequestCounter, 2);
 
178
 
 
179
  /////////////////////////////////////////////////////////////////
 
180
  // Test IPC buffer with openURI - synchronous reading
 
181
  /////////////////////////////////////////////////////////////////
 
182
 
 
183
  // prepare test file
 
184
 
 
185
  var testFile = do_get_file("ipc-data.txt");
 
186
  prepareTestFile(testFile, inputString);
 
187
 
 
188
  // actual test
 
189
 
 
190
  var ioSvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
 
191
  testURI = ioSvc.newFileURI(testFile);
 
192
 
 
193
  ipcBuffer = Cc[NS_IPCBUFFER_CONTRACTID].createInstance(Ci.nsIIPCBuffer);
 
194
  maxBytes = 10;
 
195
  gRequestCounter = 0;
 
196
  ipcBuffer.openURI(testURI, maxBytes, true, o, null, true);
 
197
 
 
198
  isStopped = ipcBuffer.stopped;
 
199
  do_check_eq(isStopped, true);
 
200
 
 
201
  data = ipcBuffer.getData();
 
202
  do_check_eq(inputString.substr(0,10), data);
 
203
 
 
204
  rawInStream = ipcBuffer.openInputStream();
 
205
  scriptableInStream = new InputStream();
 
206
  scriptableInStream.init(rawInStream);
 
207
 
 
208
  available = scriptableInStream.available();
 
209
  do_check_eq(inputString.length, available);
 
210
 
 
211
  bufData = scriptableInStream.read(available);
 
212
  rawInStream.close();
 
213
 
 
214
  do_check_eq(bufData, inputString);
 
215
 
 
216
  ipcBuffer.shutdown();
 
217
  do_check_eq(gRequestCounter, 2);
 
218
 
 
219
  /////////////////////////////////////////////////////////////////
 
220
  // Test IPC buffer with openURI - asynchronous reading
 
221
  /////////////////////////////////////////////////////////////////
 
222
 
 
223
  ipcBuffer = Cc[NS_IPCBUFFER_CONTRACTID].createInstance(Ci.nsIIPCBuffer);
 
224
  maxBytes = -1;
 
225
  gRequestCounter = 0;
 
226
 
 
227
  var cbFunc = function(ipcBuffer)
 
228
  {
 
229
    do_check_eq(gRequestCounter, 2);
 
230
 
 
231
    do_check_eq(ipcBuffer.totalBytes, inputString.length);
 
232
 
 
233
    do_check_eq(ipcBuffer.stopped, true);
 
234
 
 
235
    let data = ipcBuffer.getData();
 
236
    do_check_eq(inputString, data);
 
237
    ipcBuffer.shutdown();
 
238
    do_test_finished();
 
239
  }
 
240
  o = new TestObserver(cbFunc, ipcBuffer);
 
241
 
 
242
  ipcBuffer.openURI(testURI, maxBytes, false, o, null, false);
 
243
  do_test_pending();
 
244
 
 
245
}