~ubuntu-branches/ubuntu/trusty/xulrunner/trusty

« back to all changes in this revision

Viewing changes to xpcom/tests/unit/test_storagestream.js

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2008-08-25 13:04:18 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20080825130418-ck1i2ms384tzb9m0
Tags: 1.8.1.16+nobinonly-0ubuntu1
* New upstream release (taken from upstream CVS), LP: #254618.
* Fix MFSA 2008-35, MFSA 2008-34, MFSA 2008-33, MFSA 2008-32, MFSA 2008-31,
  MFSA 2008-30, MFSA 2008-29, MFSA 2008-28, MFSA 2008-27, MFSA 2008-25,
  MFSA 2008-24, MFSA 2008-23, MFSA 2008-22, MFSA 2008-21, MFSA 2008-26 also
  known as CVE-2008-2933, CVE-2008-2785, CVE-2008-2811, CVE-2008-2810,
  CVE-2008-2809, CVE-2008-2808, CVE-2008-2807, CVE-2008-2806, CVE-2008-2805,
  CVE-2008-2803, CVE-2008-2802, CVE-2008-2801, CVE-2008-2800, CVE-2008-2798.
* Drop 89_bz419350_attachment_306066 patch, merged upstream.
* Bump Standards-Version to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* vim:set ts=2 sw=2 sts=2 et: */
 
3
/* ***** BEGIN LICENSE BLOCK *****
 
4
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
5
 *
 
6
 * The contents of this file are subject to the Mozilla Public License Version
 
7
 * 1.1 (the "License"); you may not use this file except in compliance with
 
8
 * the License. You may obtain a copy of the License at
 
9
 * http://www.mozilla.org/MPL/
 
10
 *
 
11
 * Software distributed under the License is distributed on an "AS IS" basis,
 
12
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
13
 * for the specific language governing rights and limitations under the
 
14
 * License.
 
15
 *
 
16
 * The Original Code is XPCOM unit tests.
 
17
 *
 
18
 * The Initial Developer of the Original Code is
 
19
 * Jeff Walden <jwalden+code@mit.edu>.
 
20
 * Portions created by the Initial Developer are Copyright (C) 2007
 
21
 * the Initial Developer. All Rights Reserved.
 
22
 *
 
23
 * Contributor(s):
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the MPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the MPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
const Cc = Components.classes;
 
40
const Ci = Components.interfaces;
 
41
const Cr = Components.results;
 
42
 
 
43
function run_test()
 
44
{
 
45
  test1();
 
46
  test2();
 
47
  test3();
 
48
  test4();
 
49
}
 
50
 
 
51
/**
 
52
 * Checks that getting an input stream from a storage stream which has never had
 
53
 * anything written to it throws a not-initialized exception.
 
54
 */
 
55
function test1()
 
56
{
 
57
  var ss = Cc["@mozilla.org/storagestream;1"]
 
58
             .createInstance(Ci.nsIStorageStream);
 
59
  ss.init(1024, 1024, null);
 
60
 
 
61
  var out = ss.getOutputStream(0);
 
62
  var inp2 = ss.newInputStream(0);
 
63
  do_check_eq(inp2.available(), 0);
 
64
  do_check_eq(inp2.isNonBlocking(), true);
 
65
 
 
66
  var sis =
 
67
      Cc["@mozilla.org/scriptableinputstream;1"]
 
68
        .createInstance(Ci.nsIScriptableInputStream);
 
69
  sis.init(inp2);
 
70
 
 
71
  try {
 
72
    sis.read(1);
 
73
  } catch (ex if ex.result == Cr.NS_BASE_STREAM_WOULD_BLOCK) {
 
74
    return;
 
75
  }
 
76
  do_check_throw("read should have thrown NS_BASE_STREAM_WOULD_BLOCK");
 
77
}
 
78
 
 
79
/**
 
80
 * Checks that getting an input stream from a storage stream to which 0 bytes of
 
81
 * data have been explicitly written doesn't throw an exception.
 
82
 */
 
83
function test2()
 
84
{
 
85
  var ss = Cc["@mozilla.org/storagestream;1"]
 
86
             .createInstance(Ci.nsIStorageStream);
 
87
  ss.init(1024, 1024, null);
 
88
 
 
89
  var out = ss.getOutputStream(0);
 
90
  out.write("", 0);
 
91
  try
 
92
  {
 
93
    var inp2 = ss.newInputStream(0);
 
94
  }
 
95
  catch (e)
 
96
  {
 
97
    do_throw("shouldn't throw exception when new input stream created");
 
98
  }
 
99
}
 
100
 
 
101
/**
 
102
 * Checks that reading any non-zero amount of data from a storage stream
 
103
 * which has had 0 bytes written to it explicitly works correctly.
 
104
 */
 
105
function test3()
 
106
{
 
107
  var ss = Cc["@mozilla.org/storagestream;1"]
 
108
             .createInstance(Ci.nsIStorageStream);
 
109
  ss.init(1024, 1024, null);
 
110
 
 
111
  var out = ss.getOutputStream(0);
 
112
  out.write("", 0);
 
113
  try
 
114
  {
 
115
    var inp = ss.newInputStream(0);
 
116
  }
 
117
  catch (e)
 
118
  {
 
119
    do_throw("newInputStream(0) shouldn't throw if write() is called: " + e);
 
120
  }
 
121
 
 
122
  if (!inp.isNonBlocking())
 
123
    do_throw("next test expects a non-blocking stream");
 
124
 
 
125
  try
 
126
  {
 
127
    var bis = BIS(inp);
 
128
    var dummy = bis.readByteArray(5);
 
129
  }
 
130
  catch (e)
 
131
  {
 
132
    if (e.result != Cr.NS_BASE_STREAM_WOULD_BLOCK)
 
133
      do_throw("wrong error thrown: " + e);
 
134
    return;
 
135
  }
 
136
  do_throw("should have thrown (nsStorageInputStream is nonblocking)");
 
137
}
 
138
 
 
139
/**
 
140
 * Basic functionality test for storagestream: write data to it, get an input
 
141
 * stream, and read the data back to see that it matches.
 
142
 */
 
143
function test4()
 
144
{
 
145
  var bytes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74];
 
146
 
 
147
  var ss = Cc["@mozilla.org/storagestream;1"]
 
148
             .createInstance(Ci.nsIStorageStream);
 
149
  ss.init(1024, 1024, null);
 
150
 
 
151
  var outStream = ss.getOutputStream(0);
 
152
 
 
153
  var bos = Cc["@mozilla.org/binaryoutputstream;1"]
 
154
              .createInstance(Ci.nsIBinaryOutputStream);
 
155
  bos.setOutputStream(outStream);
 
156
 
 
157
  bos.writeByteArray(bytes, bytes.length);
 
158
  bos.close();
 
159
 
 
160
  var inp = ss.newInputStream(0);
 
161
  var bis = BIS(inp);
 
162
 
 
163
  var count = 0;
 
164
  while (count < bytes.length)
 
165
  {
 
166
    var data = bis.read8(1);
 
167
    do_check_eq(data, bytes[count++]);
 
168
  }
 
169
 
 
170
  var threw = false;
 
171
  try
 
172
  {
 
173
    data = bis.read8(1);
 
174
  }
 
175
  catch (e)
 
176
  {
 
177
    if (e.result != Cr.NS_ERROR_FAILURE)
 
178
      do_throw("wrong error thrown: " + e);
 
179
    threw = true;
 
180
  }
 
181
  if (!threw)
 
182
    do_throw("should have thrown but instead returned: '" + data + "'");
 
183
}
 
184
 
 
185
 
 
186
function BIS(input)
 
187
{
 
188
  var bis = Cc["@mozilla.org/binaryinputstream;1"]
 
189
              .createInstance(Ci.nsIBinaryInputStream);
 
190
  bis.setInputStream(input);
 
191
  return bis;
 
192
}