danwent / Perspectives

Perspectives Firefox Extension
http://perspectives-project.org
66 stars 19 forks source link

Extract test cases which require no chrome privileges #130

Open ghost opened 10 years ago

ghost commented 10 years ago

Right now we need to make test and reinstall the extensions for every change to the test cases because some test cases require chrome privileges (e.g. to set browser notifications). Some test cases however don't require those privileges (e.g. client_policy.js) thus we could extract them and run in a plain Javascript runtime.

ghost commented 10 years ago

Okay, turns out this is pretty easy.

test.html head:

<script type="text/javascript">
    Perspectives =
    { root_prefs :
      { getIntPref  : function(key) { return Perspectives.root_prefs[key]; }
      , getCharPref : function(key) { return Perspectives.root_prefs[key]; }
      , getBoolPref : function(key) { return Perspectives.root_prefs[key]; }
      , setIntPref  : function(key, value) { Perspectives.root_prefs[key] = value; }
      , setCharPref : function(key, value) { Perspectives.root_prefs[key] = value; }
      , setBoolPref : function(key, value) { Perspectives.root_prefs[key] = value; }
      }
    }

    pref = function(key, value) {
        console.log(key + ": " + value);
    }
</script>

<script type="text/javascript" src="../plugin/defaults/preferences/prefs.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/about.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/base64.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/client_policy.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/common.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/generate_svg.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/notaries.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/notify.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/preferences_dialog.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/report.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/results_dialog.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/statusbar.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/whitelist_dialog.js"></script>
<script type="text/javascript" src="../plugin/chrome/content/xml_notary_client.js"></script>

run_tests():

function run_tests() {
    if(true || typeof Perspectives != "undefined") {
        // print debug info to console by default
        Pers_debug.d_print_flags["error"] = true;

        try {
            //Perspectives.init_data();

            clear();
            meta_tests();
            client_sanity();
            //nonrouted_ips();
            quorum_basics();
            quorum_oldkey();
            //notary_parsing();
            notary_replies();

            write_string("Finished: " + g_pass_count + "/" + g_test_count + " successes, "
                    + g_fail_count + " failures. ");
            if(g_test_count != (g_pass_count + g_fail_count)) {
                error("Count totals did not match!");
            }
        } catch(e) {
            alert(e);
            error("tests did not finish: '" + e + "'.");
        }
    } else {
        error("You are probably running the test from browser context, but you need to run it from chrome context. Use 'make test' to to build and install the Perspectives extension and open chrome://perspectives/content/test/test.html");
    }
}

This runs everything except nonrouted_ips which relies on notaries.js Perspectives.is_nonrouted_ip and notary_parsing which relies on notaries.js Perspectives.getNotaryList. Both could be defined as independent functions.

ghost commented 10 years ago

What I did here is added includes that are relative to the path when running test.html directly (i.e. non-privileged). If the code runs privileged (i.e. as a plugin) these includes will fail silently and vice versa. I also implemented a stub for the pref object because some test require it.

daveschaefer commented 10 years ago

Re-opening ticket to indicate that the work still needs to be merged with the master branch and released.

ghost commented 10 years ago

Before we close this issue we should restructure the code so the following tests can also be run from within the browser:

This runs everything except nonrouted_ips which relies on notaries.js Perspectives.is_nonrouted_ip and notary_parsing which relies on notaries.js Perspectives.getNotaryList. Both could be defined as independent functions.

daveschaefer commented 10 years ago

Sounds good. I also have some local changes for tests I would like to add. I'll try not to stomp on your toes. I can post them as a pull request but wait for your review if you prefer.