summaryrefslogtreecommitdiff
path: root/data/extensions/https-everywhere@eff.org/chrome/content/ruleset-tests.js
blob: 1aa94d8ecb43149983539fee357206835af1d161 (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
// load the HTTPS Everywhere component
var HTTPSEverywhere = null;
try {
  HTTPSEverywhere = Components.classes["@eff.org/https-everywhere;1"]
    .getService(Components.interfaces.nsISupports)
    .wrappedJSObject;
} catch(e) {
  // HTTPS Everywhere doesn't seem to be installed
}

// attach testRunner to the HTTPS Everywhere component so that status.js can run it
if(HTTPSEverywhere) {
  HTTPSEverywhere.httpseRulesetTests = {
    testRunner: testRunner
  };
}


function openStatus() {
  // make sure mixed content blocking preferences are correct
  Services.prefs.setBoolPref("security.mixed_content.block_display_content", false);
  Services.prefs.setBoolPref("security.mixed_content.block_active_content", true);
   
  // open the status tab
  var statusTab = gBrowser.addTab('chrome://https-everywhere/content/ruleset-tests-status.xul');
  gBrowser.selectedTab = statusTab;
}

function testRunner() {
  Components.utils.import("resource://gre/modules/PopupNotifications.jsm");
  
  const numTabs = 6;
  var finished = false;
  var output = [];
  var urls = [];
  var num = 0;
 
  for(var target in HTTPSEverywhere.https_rules.targets) {
    if(!target.indexOf("*") != -1)  {
      urls.push({ 
        url: 'https://'+target, 
        target: target, 
        ruleset_names: HTTPSEverywhere.https_rules.targets[target]
      });
    }
  }

  function test() {
    var i;
 
    HTTPSEverywhere.httpseRulesetTests.updateStatusBar(num, urls.length); 

    // start loading all the tabs
    window.focus
    for(i=0; i<numTabs; i++) {
      newTab(num);
    }
  }

  function newTab(number) {
    num +=1;
    // start a test in this tab
    if(urls.length) {

      // open a new tab
      var tab = gBrowser.addTab(urls[number].url);

      // wait for the page to load
      var intervalId = window.setTimeout(function(){

        // detect mixed content blocker
        if(PopupNotifications.getNotification("mixed-content-blocked", gBrowser.getBrowserForTab(tab))) {
          // build output to log
          ruleset_xmls = '';
          for(let i=0; i<urls[number].ruleset_names.length; i++) {
            ruleset_xmls += urls[number].ruleset_names[i].xmlName + ', ';
          }
          if(ruleset_xmls != '')
            ruleset_xmls = ruleset_xmls.substring(ruleset_xmls.length-2, 2);
          var output = 'MCB triggered: '+urls[number].url+' ('+ruleset_xmls+')';

          HTTPSEverywhere.httpseRulesetTests.updateLog(output);
        }

        // close this tab, and open another
        closeTab(tab);

      }, 10000);

    } else {

      //to run if urls is empty
      if (!finished) { 
        finished = true;
        window.setTimeout(function(){
          gBrowser.removeCurrentTab();
        }, 10000);
      }
    }
  }

  //closes tab
  function closeTab(tab) {
    HTTPSEverywhere.httpseRulesetTests.updateStatusBar(num, urls.length); 

    gBrowser.selectedTab = tab;
    gBrowser.removeCurrentTab();

    // open a new tab, if the tests haven't been canceled
    if(!HTTPSEverywhere.httpseRulesetTests.cancel) {
      newTab(num);
    }
  }

  //manages write out of output mochilog.txt, which contains sites that trigger mcb
  function writeout(weburl) {

    //initialize file
    var file = Components.classes["@mozilla.org/file/directory_service;1"].
    getService(Components.interfaces.nsIProperties).
    get("Home", Components.interfaces.nsIFile);
    writeoutfile = "mochilog.txt";
    file.append(writeoutfile);

    //create file if it does not already exist
    if(!file.exists()) {
      file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420);
    } 

    //initialize output stream
    var stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
      .createInstance(Components.interfaces.nsIFileOutputStream);

    //permissions are set to append (will not delete existing contents)
    stream.init(file, 0x02 | 0x08 | 0x10, 0666, 0);

    var content = weburl + "\n";

    //Deal with ascii text and write out
    var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
      createInstance(Components.interfaces.nsIConverterOutputStream);
    converter.init(stream, "UTF-8", 0, 0);
    converter.writeString(content);
    converter.close();

    //alternative write out if ascii is not a concern
    //stream.write(content,content.length);
    //stream.close();

  }
  test();
}