modules/libjar/test/chrome/test_bug386153.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/libjar/test/chrome/test_bug386153.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,95 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=386153
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 386153</title>
    1.11 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.12 +  <script type="application/javascript" src="chrome://mochikit/content/chrome-harness.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
    1.14 +</head>
    1.15 +<body>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=386153">Mozilla Bug 386153</a>
    1.17 +<p id="display"></p>
    1.18 +<div id="content" style="display: none">
    1.19 +  
    1.20 +</div>
    1.21 +<pre id="test">
    1.22 +<script type="application/javascript">
    1.23 +
    1.24 +/** Test for Bug 386153 **/
    1.25 +
    1.26 +const Cc = Components.classes;
    1.27 +const Ci = Components.interfaces;
    1.28 +
    1.29 +// Opens a zip file from the test directory.
    1.30 +function openZip(path) {
    1.31 +
    1.32 +  var location = window.location.href;
    1.33 +  location = getRootDirectory(location);
    1.34 +  var jar = getJar(location);
    1.35 +  if (jar != null) {
    1.36 +    var resolved = extractJarToTmp(jar);
    1.37 +  } else {
    1.38 +    var resolvedURI = getResolvedURI(window.location.href);
    1.39 +    var resolved = getChromeDir(resolvedURI);
    1.40 +  }
    1.41 +  resolved.append(path);
    1.42 +
    1.43 +  var zip = Cc["@mozilla.org/libjar/zip-reader;1"].
    1.44 +            createInstance(Ci.nsIZipReader);
    1.45 +  zip.open(resolved);
    1.46 +  return zip;
    1.47 +}
    1.48 +
    1.49 +// Gets the pretty name from the signing cert or null if the zip is unsigned.
    1.50 +function getSigner(zip) {
    1.51 +  var principal = zip.getCertificatePrincipal(null);
    1.52 +  if (principal && principal.hasCertificate)
    1.53 +    return principal.prettyName;
    1.54 +  return null;
    1.55 +}
    1.56 +
    1.57 +function verifySigning(zip) {
    1.58 +  var principal = zip.getCertificatePrincipal(null);
    1.59 +  var count = 0;
    1.60 +  var entries = zip.findEntries(null);
    1.61 +  while (entries.hasMore()) {
    1.62 +    var entry = entries.getNext();
    1.63 +    // Nothing in META-INF is in the manifest.
    1.64 +    if (entry.substr(0, 9) == "META-INF/")
    1.65 +      continue;
    1.66 +    // Directory entries aren't in the manifest.
    1.67 +    if (entry.substr(-1) == "/")
    1.68 +      continue;
    1.69 +    count++;
    1.70 +    var entryPrincipal = zip.getCertificatePrincipal(entry);
    1.71 +    if (!entryPrincipal || !principal.equals(entryPrincipal))
    1.72 +      return false;
    1.73 +  }
    1.74 +  return zip.manifestEntriesCount == count;
    1.75 +}
    1.76 +
    1.77 +var zip = openZip("unsigned.zip");
    1.78 +is(getSigner(zip), null, "Should not be signed");
    1.79 +
    1.80 +zip = openZip("signed.zip");
    1.81 +is(getSigner(zip), "Mozilla Testing", "Should be signed by the right cert");
    1.82 +ok(verifySigning(zip), "Should be correctly signed");
    1.83 +
    1.84 +zip = openZip("signed-added.zip");
    1.85 +is(getSigner(zip), "Mozilla Testing", "Should be signed by the right cert");
    1.86 +ok(!verifySigning(zip), "Should be incorrectly signed");
    1.87 +
    1.88 +zip = openZip("signed-tampered.zip");
    1.89 +is(getSigner(zip), "Mozilla Testing", "Should be signed by the right cert");
    1.90 +ok(!verifySigning(zip), "Should be incorrectly signed");
    1.91 +
    1.92 +zip = openZip("signed-badca.zip");
    1.93 +is(getSigner(zip), null, "Should not appear to be signed");
    1.94 +
    1.95 +</script>
    1.96 +</pre>
    1.97 +</body>
    1.98 +</html>

mercurial