summaryrefslogtreecommitdiffstats
path: root/netwerk/test/httpserver/test/test_default_index_handler.js
blob: 3ee25e95d1e66983cae5d5510d5132b3cc042fc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// checks for correct output with the default index handler, mostly to do
// escaping checks -- highly dependent on the default index handler output
// format

var srv, dir, gDirEntries;

XPCOMUtils.defineLazyGetter(this, "BASE_URL", function () {
  return "http://localhost:" + srv.identity.primaryPort + "/";
});

function run_test() {
  createTestDirectory();

  srv = createServer();
  srv.registerDirectory("/", dir);

  var nameDir = do_get_file("data/name-scheme/");
  srv.registerDirectory("/bar/", nameDir);

  srv.start(-1);

  function done() {
    do_test_pending();
    destroyTestDirectory();
    srv.stop(function () {
      do_test_finished();
    });
  }

  runHttpTests(tests, done);
}

function createTestDirectory() {
  dir = Services.dirsvc.get("TmpD", Ci.nsIFile);
  dir.append("index_handler_test_" + Math.random());
  dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o744);

  // populate with test directories, files, etc.
  // Files must be in expected order of display on the index page!

  var files = [];

  makeFile("aa_directory", true, dir, files);
  makeFile("Ba_directory", true, dir, files);
  makeFile("bb_directory", true, dir, files);
  makeFile("foo", true, dir, files);
  makeFile("a_file", false, dir, files);
  makeFile("B_file", false, dir, files);
  makeFile("za'z", false, dir, files);
  makeFile("zb&z", false, dir, files);
  makeFile("zc<q", false, dir, files);
  makeFile('zd"q', false, dir, files);
  makeFile("ze%g", false, dir, files);
  makeFile("zf%200h", false, dir, files);
  makeFile("zg>m", false, dir, files);

  gDirEntries = [files];

  var subdir = dir.clone();
  subdir.append("foo");

  files = [];

  makeFile("aa_dir", true, subdir, files);
  makeFile("b_dir", true, subdir, files);
  makeFile("AA_file.txt", false, subdir, files);
  makeFile("test.txt", false, subdir, files);

  gDirEntries.push(files);
}

function destroyTestDirectory() {
  dir.remove(true);
}

/** ***********
 * UTILITIES *
 *************/

/** Verifies data in bytes for the trailing-caret path above. */
function hiddenDataCheck(bytes, uri, path) {
  var data = String.fromCharCode.apply(null, bytes);

  var parser = new DOMParser();

  // Note: the index format isn't XML -- it's actually HTML -- but we require
  //       the index format also be valid XML, albeit XML without namespaces,
  //       XML declarations, etc.  Doing this simplifies output checking.
  try {
    var doc = parser.parseFromString(data, "application/xml");
  } catch (e) {
    do_throw("document failed to parse as XML");
  }

  var body = doc.documentElement.getElementsByTagName("body");
  Assert.equal(body.length, 1);
  body = body[0];

  // header
  var header = body.getElementsByTagName("h1");
  Assert.equal(header.length, 1);

  Assert.equal(header[0].textContent, path);

  // files
  var lst = body.getElementsByTagName("ol");
  Assert.equal(lst.length, 1);
  var items = lst[0].getElementsByTagName("li");

  var top = Services.io.newURI(uri);

  // N.B. No ERROR_IF_SEE_THIS.txt^ file!
  var dirEntries = [
    { name: "file.txt", isDirectory: false },
    { name: "SHOULD_SEE_THIS.txt^", isDirectory: false },
  ];

  for (var i = 0; i < items.length; i++) {
    var link = items[i].childNodes[0];
    var f = dirEntries[i];

    var sep = f.isDirectory ? "/" : "";

    Assert.equal(link.textContent, f.name + sep);

    uri = Services.io.newURI(link.getAttribute("href"), null, top);
    Assert.equal(decodeURIComponent(uri.pathQueryRef), path + f.name + sep);
  }
}

/**
 * Verifies data in bytes (an array of bytes) represents an index page for the
 * given URI and path, which should be a page listing the given directory
 * entries, in order.
 *
 * @param bytes
 *   array of bytes representing the index page's contents
 * @param uri
 *   string which is the URI of the index page
 * @param path
 *   the path portion of uri
 * @param dirEntries
 *   sorted (in the manner the directory entries should be sorted) array of
 *   objects, each of which has a name property (whose value is the file's name,
 *   without / if it's a directory) and an isDirectory property (with expected
 *   value)
 */
function dataCheck(bytes, uri, path, dirEntries) {
  var data = String.fromCharCode.apply(null, bytes);

  var parser = new DOMParser();

  // Note: the index format isn't XML -- it's actually HTML -- but we require
  //       the index format also be valid XML, albeit XML without namespaces,
  //       XML declarations, etc.  Doing this simplifies output checking.
  try {
    var doc = parser.parseFromString(data, "application/xml");
  } catch (e) {
    do_throw("document failed to parse as XML");
  }

  var body = doc.documentElement.getElementsByTagName("body");
  Assert.equal(body.length, 1);
  body = body[0];

  // header
  var header = body.getElementsByTagName("h1");
  Assert.equal(header.length, 1);

  Assert.equal(header[0].textContent, path);

  // files
  var lst = body.getElementsByTagName("ol");
  Assert.equal(lst.length, 1);
  var items = lst[0].getElementsByTagName("li");
  var top = Services.io.newURI(uri);

  for (var i = 0; i < items.length; i++) {
    var link = items[i].childNodes[0];
    var f = dirEntries[i];

    var sep = f.isDirectory ? "/" : "";

    Assert.equal(link.textContent, f.name + sep);

    uri = Services.io.newURI(link.getAttribute("href"), null, top);
    Assert.equal(decodeURIComponent(uri.pathQueryRef), path + f.name + sep);
  }
}

/**
 * Create a file/directory with the given name underneath parentDir, and
 * append an object with name/isDirectory properties to lst corresponding
 * to it if the file/directory could be created.
 */
function makeFile(name, isDirectory, parentDir, lst) {
  var type = Ci.nsIFile[isDirectory ? "DIRECTORY_TYPE" : "NORMAL_FILE_TYPE"];
  var file = parentDir.clone();

  try {
    file.append(name);
    file.create(type, 0o755);
    lst.push({ name, isDirectory });
  } catch (e) {
    /* OS probably doesn't like file name, skip */
  }
}

/** *******
 * TESTS *
 *********/

XPCOMUtils.defineLazyGetter(this, "tests", function () {
  return [
    new Test(BASE_URL, null, start, stopRootDirectory),
    new Test(BASE_URL + "foo/", null, start, stopFooDirectory),
    new Test(
      BASE_URL + "bar/folder^/",
      null,
      start,
      stopTrailingCaretDirectory
    ),
  ];
});

// check top-level directory listing
function start(ch) {
  Assert.equal(ch.getResponseHeader("Content-Type"), "text/html;charset=utf-8");
}
function stopRootDirectory(ch, status, data) {
  dataCheck(data, BASE_URL, "/", gDirEntries[0]);
}

// check non-top-level, too
function stopFooDirectory(ch, status, data) {
  dataCheck(data, BASE_URL + "foo/", "/foo/", gDirEntries[1]);
}

// trailing-caret leaf with hidden files
function stopTrailingCaretDirectory(ch, status, data) {
  hiddenDataCheck(data, BASE_URL + "bar/folder^/", "/bar/folder^/");
}