test_headers.js
Go to the documentation of this file.
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 httpd.js code.
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) 2006
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 // tests for header storage in httpd.js; nsHttpHeaders is an *internal* data
40 // structure and is not to be used directly outside of httpd.js itself except
41 // for testing purposes
42 
43 
54 function assertValidHeader(fieldName, fieldValue, headers)
55 {
56  try
57  {
58  headers.setHeader(fieldName, fieldValue, false);
59  }
60  catch (e)
61  {
62  do_throw("Unexpected exception thrown: " + e);
63  }
64 }
65 
76 function assertInvalidHeader(fieldName, fieldValue, headers)
77 {
78  try
79  {
80  headers.setHeader(fieldName, fieldValue, false);
81  throw "Setting (" + fieldName + ", " +
82  fieldValue + ") as header succeeded!";
83  }
84  catch (e)
85  {
86  if (e !== Cr.NS_ERROR_INVALID_ARG)
87  do_throw("Unexpected exception thrown: " + e);
88  }
89 }
90 
91 
92 function run_test()
93 {
95  testGetHeader();
97  testHasHeader();
98 }
99 
101 {
102  var headers = new nsHttpHeaders();
103 
104  assertInvalidHeader("f o", "bar", headers);
105  assertInvalidHeader("f\0n", "bar", headers);
106  assertInvalidHeader("foo:", "bar", headers);
107  assertInvalidHeader("f\\o", "bar", headers);
108  assertInvalidHeader("@xml", "bar", headers);
109  assertInvalidHeader("fiz(", "bar", headers);
110  assertInvalidHeader("HTTP/1.1", "bar", headers);
111  assertInvalidHeader("b\"b", "bar", headers);
112  assertInvalidHeader("ascsd\t", "bar", headers);
113  assertInvalidHeader("{fds", "bar", headers);
114  assertInvalidHeader("baz?", "bar", headers);
115  assertInvalidHeader("a\\b\\c", "bar", headers);
116  assertInvalidHeader("\0x7F", "bar", headers);
117  assertInvalidHeader("\0x1F", "bar", headers);
118  assertInvalidHeader("f\n", "bar", headers);
119  assertInvalidHeader("foo", "b\nar", headers);
120  assertInvalidHeader("foo", "b\rar", headers);
121  assertInvalidHeader("foo", "b\0", headers);
122 
123  // request splitting, fwiw -- we're actually immune to this type of attack so
124  // long as we don't implement persistent connections
125  assertInvalidHeader("f\r\nGET /badness HTTP/1.1\r\nFoo", "bar", headers);
126 
127  assertValidHeader("f'", "baz", headers);
128  assertValidHeader("f`", "baz", headers);
129  assertValidHeader("f.", "baz", headers);
130  assertValidHeader("f---", "baz", headers);
131  assertValidHeader("---", "baz", headers);
132  assertValidHeader("~~~", "baz", headers);
133  assertValidHeader("~~~", "b\r\n bar", headers);
134  assertValidHeader("~~~", "b\r\n\tbar", headers);
135 }
136 
137 function testGetHeader()
138 {
139  var headers = new nsHttpHeaders();
140 
141  headers.setHeader("Content-Type", "text/html", false);
142  var c = headers.getHeader("content-type");
143  do_check_eq(c, "text/html");
144 
145  headers.setHeader("test", "FOO", false);
146  var c = headers.getHeader("test");
147  do_check_eq(c, "FOO");
148 
149  try
150  {
151  headers.getHeader(":");
152  throw "Failed to throw for invalid header";
153  }
154  catch (e)
155  {
156  if (e !== Cr.NS_ERROR_INVALID_ARG)
157  do_throw("headers.getHeader(':') must throw invalid arg");
158  }
159 
160  try
161  {
162  headers.getHeader("valid");
163  throw 'header doesn\'t exist';
164  }
165  catch (e)
166  {
167  if (e !== Cr.NS_ERROR_NOT_AVAILABLE)
168  do_throw("shouldn't be a header named 'valid' in headers!");
169  }
170 }
171 
173 {
174  var headers = new nsHttpHeaders();
175 
176  var heads =
177  {
178  "foo": "17",
179  "baz": "two six niner",
180  "decaf": "class Program { int .7; int main(){ .7 = 5; return 7 - .7; } }"
181  };
182 
183  for (var i in heads)
184  headers.setHeader(i, heads[i], false);
185 
186  var en = headers.enumerator;
187  while (en.hasMoreElements())
188  {
189  var it = en.getNext().QueryInterface(Ci.nsISupportsString).data;
190  do_check_true(it.toLowerCase() in heads);
191  delete heads[it.toLowerCase()];
192  }
193 
194  for (var i in heads)
195  do_throw("still have properties in heads!?!?");
196 
197 }
198 
199 function testHasHeader()
200 {
201  var headers = new nsHttpHeaders();
202 
203  headers.setHeader("foo", "bar", false);
204  do_check_true(headers.hasHeader("foo"));
205  do_check_true(headers.hasHeader("fOo"));
206  do_check_false(headers.hasHeader("not-there"));
207 
208  headers.setHeader("f`'~", "bar", false);
209  do_check_true(headers.hasHeader("F`'~"));
210 
211  try
212  {
213  headers.hasHeader(":");
214  throw "failed to throw";
215  }
216  catch (e)
217  {
218  if (e !== Cr.NS_ERROR_INVALID_ARG)
219  do_throw(".hasHeader for an invalid name should throw");
220  }
221 }
do_check_eq(typeof PlacesUtils,"object")
function run_test()
Definition: test_headers.js:92
function nsHttpHeaders()
Definition: httpd.js:4285
function assertValidHeader(fieldName, fieldValue, headers)
Definition: test_headers.js:54
function testHeaderValidity()
function testHeaderEnumerator()
function testHasHeader()
function assertInvalidHeader(fieldName, fieldValue, headers)
Definition: test_headers.js:76
const Cr
function testGetHeader()
const Ci
_getSelectedPageStyle s i