summaryrefslogtreecommitdiffstats
path: root/toolkit/components/mediasniffer/test/unit/test_mediasniffer.js
blob: 13b3f6cf0cb7403eb6d5ce85c173bcfe1add0438 (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
/* 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/. */

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");

const PATH = "/file.meh";
var httpserver = new HttpServer();

// Each time, the data consist in a string that should be sniffed as Ogg.
const data = "OggS\0meeeh.";
var testRan = 0;

// If the content-type is not present, or if it's application/octet-stream, it
// should be sniffed to application/ogg by the media sniffer. Otherwise, it
// should not be changed.
const tests = [
  // Those three first case are the case of a media loaded in a media element.
  // All three should be sniffed.
  {
    contentType: "",
    expectedContentType: "application/ogg",
    flags:
      Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS |
      Ci.nsIChannel.LOAD_MEDIA_SNIFFER_OVERRIDES_CONTENT_TYPE,
  },
  {
    contentType: "application/octet-stream",
    expectedContentType: "application/ogg",
    flags:
      Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS |
      Ci.nsIChannel.LOAD_MEDIA_SNIFFER_OVERRIDES_CONTENT_TYPE,
  },
  {
    contentType: "application/something",
    expectedContentType: "application/ogg",
    flags:
      Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS |
      Ci.nsIChannel.LOAD_MEDIA_SNIFFER_OVERRIDES_CONTENT_TYPE,
  },
  // This last cases test the case of a channel opened while allowing content
  // sniffers to override the content-type, like in the docshell.
  {
    contentType: "application/octet-stream",
    expectedContentType: "application/ogg",
    flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS,
  },
  {
    contentType: "",
    expectedContentType: "application/ogg",
    flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS,
  },
  {
    contentType: "application/something",
    expectedContentType: "application/ogg",
    flags: Ci.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS,
  },
];

// A basic listener that reads checks the if we sniffed properly.
var listener = {
  onStartRequest(request) {
    Assert.equal(
      request.QueryInterface(Ci.nsIChannel).contentType,
      tests[testRan].expectedContentType
    );
  },

  onDataAvailable(request, stream, offset, count) {
    try {
      var bis = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
        Ci.nsIBinaryInputStream
      );
      bis.setInputStream(stream);
      bis.readByteArray(bis.available());
    } catch (ex) {
      do_throw("Error in onDataAvailable: " + ex);
    }
  },

  onStopRequest(request, status) {
    testRan++;
    runNext();
  },
};

function setupChannel(url, flags) {
  let uri = "http://localhost:" + httpserver.identity.primaryPort + url;
  var chan = NetUtil.newChannel({
    uri,
    loadUsingSystemPrincipal: true,
    contentPolicyType: Ci.nsIContentPolicy.TYPE_MEDIA,
  });
  chan.loadFlags |= flags;
  var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
  return httpChan;
}

function runNext() {
  if (testRan == tests.length) {
    do_test_finished();
    return;
  }
  var channel = setupChannel(PATH, tests[testRan].flags);
  httpserver.registerPathHandler(PATH, function(request, response) {
    response.setHeader("Content-Type", tests[testRan].contentType, false);
    response.bodyOutputStream.write(data, data.length);
  });
  channel.asyncOpen(listener);
}

function run_test() {
  httpserver.start(-1);
  do_test_pending();
  try {
    runNext();
  } catch (e) {
    print("ERROR - " + e + "\n");
  }
}