summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_oblivious_http.js
blob: 14b3dc7f81c4b7940473fd681713136995669fd2 (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
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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

class ObliviousHttpTestRequest {
  constructor(method, uri, headers, content) {
    this.method = method;
    this.uri = uri;
    this.headers = headers;
    this.content = content;
  }
}

class ObliviousHttpTestResponse {
  constructor(status, headers, content) {
    this.status = status;
    this.headers = headers;
    this.content = content;
  }
}

class ObliviousHttpTestCase {
  constructor(request, response) {
    this.request = request;
    this.response = response;
  }
}

add_task(async function test_oblivious_http() {
  let testcases = [
    new ObliviousHttpTestCase(
      new ObliviousHttpTestRequest(
        "GET",
        NetUtil.newURI("https://example.com"),
        { "X-Some-Header": "header value" },
        ""
      ),
      new ObliviousHttpTestResponse(200, {}, "Hello, World!")
    ),
    new ObliviousHttpTestCase(
      new ObliviousHttpTestRequest(
        "POST",
        NetUtil.newURI("http://example.test"),
        { "X-Some-Header": "header value", "X-Some-Other-Header": "25" },
        "Posting some content..."
      ),
      new ObliviousHttpTestResponse(
        418,
        { "X-Teapot": "teapot" },
        "I'm a teapot"
      )
    ),
    new ObliviousHttpTestCase(
      new ObliviousHttpTestRequest(
        "GET",
        NetUtil.newURI("http://example.test/404"),
        { "X-Some-Header": "header value", "X-Some-Other-Header": "25" },
        ""
      ),
      undefined // 404 relay
    ),
  ];

  for (let testcase of testcases) {
    await run_one_testcase(testcase);
  }
});

async function run_one_testcase(testcase) {
  let ohttp = Cc["@mozilla.org/network/oblivious-http;1"].getService(
    Ci.nsIObliviousHttp
  );
  let ohttpServer = ohttp.server();

  let httpServer = new HttpServer();
  httpServer.registerPathHandler("/", function (request, response) {
    let inputStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
      Ci.nsIScriptableInputStream
    );
    inputStream.init(request.bodyInputStream);
    let requestBody = inputStream.readBytes(inputStream.available());
    let ohttpResponse = ohttpServer.decapsulate(stringToBytes(requestBody));
    let bhttp = Cc["@mozilla.org/network/binary-http;1"].getService(
      Ci.nsIBinaryHttp
    );
    let decodedRequest = bhttp.decodeRequest(ohttpResponse.request);
    equal(decodedRequest.method, testcase.request.method);
    equal(decodedRequest.scheme, testcase.request.uri.scheme);
    equal(decodedRequest.authority, testcase.request.uri.hostPort);
    equal(decodedRequest.path, testcase.request.uri.pathQueryRef);
    for (
      let i = 0;
      i < decodedRequest.headerNames.length &&
      i < decodedRequest.headerValues.length;
      i++
    ) {
      equal(
        decodedRequest.headerValues[i],
        testcase.request.headers[decodedRequest.headerNames[i]]
      );
    }
    equal(bytesToString(decodedRequest.content), testcase.request.content);

    let responseHeaderNames = ["content-type"];
    let responseHeaderValues = ["text/plain"];
    for (let headerName of Object.keys(testcase.response.headers)) {
      responseHeaderNames.push(headerName);
      responseHeaderValues.push(testcase.response.headers[headerName]);
    }
    let binaryResponse = new BinaryHttpResponse(
      testcase.response.status,
      responseHeaderNames,
      responseHeaderValues,
      stringToBytes(testcase.response.content)
    );
    let responseBytes = bhttp.encodeResponse(binaryResponse);
    let encResponse = ohttpResponse.encapsulate(responseBytes);
    response.setStatusLine(request.httpVersion, 200, "OK");
    response.setHeader("Content-Type", "message/ohttp-res", false);
    response.write(bytesToString(encResponse));
  });
  httpServer.start(-1);

  let ohttpService = Cc[
    "@mozilla.org/network/oblivious-http-service;1"
  ].getService(Ci.nsIObliviousHttpService);
  let relayURI = NetUtil.newURI(
    `http://localhost:${httpServer.identity.primaryPort}/`
  );
  if (!testcase.response) {
    relayURI = NetUtil.newURI(
      `http://localhost:${httpServer.identity.primaryPort}/404`
    );
  }
  let obliviousHttpChannel = ohttpService
    .newChannel(relayURI, testcase.request.uri, ohttpServer.encodedConfig)
    .QueryInterface(Ci.nsIHttpChannel);
  for (let headerName of Object.keys(testcase.request.headers)) {
    obliviousHttpChannel.setRequestHeader(
      headerName,
      testcase.request.headers[headerName],
      false
    );
  }
  if (testcase.request.method == "POST") {
    let uploadChannel = obliviousHttpChannel.QueryInterface(
      Ci.nsIUploadChannel2
    );
    ok(uploadChannel);
    let bodyStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
      Ci.nsIStringInputStream
    );
    bodyStream.setData(
      testcase.request.content,
      testcase.request.content.length
    );
    uploadChannel.explicitSetUploadStream(
      bodyStream,
      null,
      -1,
      testcase.request.method,
      false
    );
  }
  let response = await new Promise(resolve => {
    NetUtil.asyncFetch(obliviousHttpChannel, function (inputStream) {
      let scriptableInputStream = Cc[
        "@mozilla.org/scriptableinputstream;1"
      ].createInstance(Ci.nsIScriptableInputStream);
      scriptableInputStream.init(inputStream);
      try {
        // If decoding failed just return undefined.
        inputStream.available();
      } catch (e) {
        resolve(undefined);
        return;
      }
      let responseBody = scriptableInputStream.readBytes(
        inputStream.available()
      );
      resolve(responseBody);
    });
  });
  if (testcase.response) {
    equal(response, testcase.response.content);
    for (let headerName of Object.keys(testcase.response.headers)) {
      equal(
        obliviousHttpChannel.getResponseHeader(headerName),
        testcase.response.headers[headerName]
      );
    }
  } else {
    let relayChannel = obliviousHttpChannel.QueryInterface(
      Ci.nsIObliviousHttpChannel
    ).relayChannel;
    equal(relayChannel.responseStatus, 404);
  }
  await new Promise(resolve => {
    httpServer.stop(resolve);
  });
}