summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_content_encoding_gzip.js
blob: 1137a306e5416502e4b989aeb62fb9991c00e356 (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
"use strict";

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

var httpserver = new HttpServer();
var index = 0;
var tests = [
  {
    url: "/test/cegzip1",
    flags: CL_EXPECT_GZIP,
    ce: "gzip",
    body: [
      0x1f, 0x8b, 0x08, 0x08, 0x5a, 0xa0, 0x31, 0x4f, 0x00, 0x03, 0x74, 0x78,
      0x74, 0x00, 0x2b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x92, 0xd4, 0xe2,
      0x12, 0x43, 0x2e, 0x00, 0xb9, 0x23, 0xd7, 0x3b, 0x0e, 0x00, 0x00, 0x00,
    ],
    datalen: 14, // the data length of the uncompressed document
  },

  {
    url: "/test/cegzip2",
    flags: CL_EXPECT_GZIP,
    ce: "gzip, gzip",
    body: [
      0x1f, 0x8b, 0x08, 0x00, 0x72, 0xa1, 0x31, 0x4f, 0x00, 0x03, 0x93, 0xef,
      0xe6, 0xe0, 0x88, 0x5a, 0x60, 0xe8, 0xcf, 0xc0, 0x5c, 0x52, 0x51, 0xc2,
      0xa0, 0x7d, 0xf2, 0x84, 0x4e, 0x18, 0xc3, 0xa2, 0x49, 0x57, 0x1e, 0x09,
      0x39, 0xeb, 0x31, 0xec, 0x54, 0xbe, 0x6e, 0xcd, 0xc7, 0xc0, 0xc0, 0x00,
      0x00, 0x6e, 0x90, 0x7a, 0x85, 0x24, 0x00, 0x00, 0x00,
    ],
    datalen: 14, // the data length of the uncompressed document
  },

  {
    url: "/test/cebrotli1",
    flags: CL_EXPECT_GZIP,
    ce: "br",
    body: [0x0b, 0x02, 0x80, 0x74, 0x65, 0x73, 0x74, 0x0a, 0x03],

    datalen: 5, // the data length of the uncompressed document
  },

  // this is a truncated brotli document, producing no output bytes
  {
    url: "/test/cebrotli2",
    flags: CL_EXPECT_GZIP,
    ce: "br",
    body: [0x0b, 0x0a, 0x09],
    datalen: 0,
  },

  // this is brotli but should come through as identity due to prefs
  {
    url: "/test/cebrotli3",
    flags: 0,
    ce: "br",
    body: [0x0b, 0x02, 0x80, 0x74, 0x65, 0x73, 0x74, 0x0a, 0x03],

    datalen: 9,
  },

  // this is not a brotli document
  {
    url: "/test/cebrotli4",
    flags: CL_EXPECT_GZIP | CL_EXPECT_FAILURE,
    ce: "br",
    body: [
      0x01, 0xe6, 0x00, 0x76, 0x42, 0x10, 0x01, 0x1c, 0x24, 0x24, 0x3c, 0xd7,
      0xd7, 0xd7, 0x01, 0x1c,
    ],
    datalen: 16,
  },

  // this is a brotli document
  {
    url: "/test/cebrotli5",
    flags: CL_EXPECT_GZIP,
    ce: "br",
    body: [0x0b, 0x00, 0x80, 0x4e, 0x03],
    datalen: 1,
  },

  // this a truncated brotli document (missing the end marker)
  // producing one output byte
  {
    url: "/test/cebrotli6",
    flags: CL_EXPECT_GZIP,
    ce: "br",
    body: [0x0b, 0x00, 0x80, 0x4e],
    datalen: 1,
  },
];

function setupChannel(url) {
  return NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + url,
    loadUsingSystemPrincipal: true,
  });
}

function startIter() {
  if (tests[index].url === "/test/cebrotli3") {
    // this test wants to make sure we don't do brotli when not in a-e
    prefs.setCharPref("network.http.accept-encoding", "gzip, deflate");
  } else {
    prefs.setCharPref("network.http.accept-encoding", "gzip, deflate, br");
  }
  var channel = setupChannel(tests[index].url);
  channel.asyncOpen(
    new ChannelListener(completeIter, channel, tests[index].flags)
  );
}

function completeIter(request, data) {
  if (!(tests[index].flags & CL_EXPECT_FAILURE)) {
    Assert.equal(data.length, tests[index].datalen, "test " + index);
  }
  if (++index < tests.length) {
    startIter();
  } else {
    httpserver.stop(do_test_finished);
    prefs.setCharPref("network.http.accept-encoding", cePref);
  }
}

var prefs;
var cePref;
function run_test() {
  prefs = Services.prefs;
  cePref = prefs.getCharPref("network.http.accept-encoding");
  prefs.setBoolPref("network.http.encoding.trustworthy_is_https", false);

  httpserver.registerPathHandler("/test/cegzip1", handler);
  httpserver.registerPathHandler("/test/cegzip2", handler);
  httpserver.registerPathHandler("/test/cebrotli1", handler);
  httpserver.registerPathHandler("/test/cebrotli2", handler);
  httpserver.registerPathHandler("/test/cebrotli3", handler);
  httpserver.registerPathHandler("/test/cebrotli4", handler);
  httpserver.registerPathHandler("/test/cebrotli5", handler);
  httpserver.registerPathHandler("/test/cebrotli6", handler);
  httpserver.start(-1);

  startIter();
  do_test_pending();
}

function handler(metadata, response) {
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Content-Encoding", tests[index].ce, false);
  response.setHeader("Content-Length", "" + tests[index].body.length, false);

  var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
    Ci.nsIBinaryOutputStream
  );
  bos.setOutputStream(response.bodyOutputStream);

  response.processAsync();
  bos.writeByteArray(tests[index].body);
  response.finish();
}