toolkit/crashreporter/test/browser/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/test/browser/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +function create_subdir(dir, subdirname) {
     1.5 +  let subdir = dir.clone();
     1.6 +  subdir.append(subdirname);
     1.7 +  if (subdir.exists()) {
     1.8 +    subdir.remove(true);
     1.9 +  }
    1.10 +  subdir.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
    1.11 +  return subdir;
    1.12 +}
    1.13 +
    1.14 +// need to hold on to this to unregister for cleanup
    1.15 +let _provider = null;
    1.16 +
    1.17 +function make_fake_appdir() {
    1.18 +  // Create a directory inside the profile and register it as UAppData, so
    1.19 +  // we can stick fake crash reports inside there. We put it inside the profile
    1.20 +  // just because we know that will get cleaned up after the mochitest run.
    1.21 +  let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
    1.22 +               .getService(Ci.nsIProperties);
    1.23 +  let profD = dirSvc.get("ProfD", Ci.nsILocalFile);
    1.24 +  // create a subdir just to keep our files out of the way
    1.25 +  let appD = create_subdir(profD, "UAppData");
    1.26 +
    1.27 +  let crashesDir = create_subdir(appD, "Crash Reports");
    1.28 +  create_subdir(crashesDir, "pending");
    1.29 +  create_subdir(crashesDir, "submitted");
    1.30 +
    1.31 +  _provider = {
    1.32 +    getFile: function(prop, persistent) {
    1.33 +      persistent.value = true;
    1.34 +      if (prop == "UAppData") {
    1.35 +        return appD.clone();
    1.36 +      }
    1.37 +      throw Components.results.NS_ERROR_FAILURE;
    1.38 +    },
    1.39 +    QueryInterface: function(iid) {
    1.40 +      if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
    1.41 +          iid.equals(Ci.nsISupports)) {
    1.42 +        return this;
    1.43 +      }
    1.44 +      throw Components.results.NS_ERROR_NO_INTERFACE;
    1.45 +    }
    1.46 +  };
    1.47 +  // register our new provider
    1.48 +  dirSvc.QueryInterface(Ci.nsIDirectoryService)
    1.49 +        .registerProvider(_provider);
    1.50 +  // and undefine the old value
    1.51 +  try {
    1.52 +    dirSvc.undefine("UAppData");
    1.53 +  } catch(ex) {} // it's ok if this fails, the value might not be cached yet
    1.54 +  return appD.clone();
    1.55 +}
    1.56 +
    1.57 +function cleanup_fake_appdir() {
    1.58 +  let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
    1.59 +               .getService(Ci.nsIProperties);
    1.60 +  dirSvc.QueryInterface(Ci.nsIDirectoryService)
    1.61 +        .unregisterProvider(_provider);
    1.62 +  // undefine our value so future calls get the real value
    1.63 +  try {
    1.64 +    dirSvc.undefine("UAppData");
    1.65 +  } catch(ex) {
    1.66 +    dump("cleanup_fake_appdir: dirSvc.undefine failed: " + ex.message +"\n");
    1.67 +  }
    1.68 +}
    1.69 +
    1.70 +function add_fake_crashes(crD, count) {
    1.71 +  let results = [];
    1.72 +  let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]
    1.73 +                      .getService(Ci.nsIUUIDGenerator);
    1.74 +  let submitdir = crD.clone();
    1.75 +  submitdir.append("submitted");
    1.76 +  // create them from oldest to newest, to ensure that about:crashes
    1.77 +  // displays them in the correct order
    1.78 +  let date = Date.now() - count * 60000;
    1.79 +  for (let i = 0; i < count; i++) {
    1.80 +    let uuid = uuidGenerator.generateUUID().toString();
    1.81 +    // ditch the {}
    1.82 +    uuid = "bp-" + uuid.substring(1, uuid.length - 2);
    1.83 +    let fn = uuid + ".txt";
    1.84 +    let file = submitdir.clone();
    1.85 +    file.append(fn);
    1.86 +    file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    1.87 +    file.lastModifiedTime = date;
    1.88 +    results.push({'id': uuid, 'date': date, 'pending': false});
    1.89 +
    1.90 +    date += 60000;
    1.91 +  }
    1.92 +  // we want them sorted newest to oldest, since that's the order
    1.93 +  // that about:crashes lists them in
    1.94 +  results.sort(function(a,b) b.date - a.date);
    1.95 +  return results;
    1.96 +}
    1.97 +
    1.98 +function writeDataToFile(file, data) {
    1.99 +  var fstream = Cc["@mozilla.org/network/file-output-stream;1"]
   1.100 +                .createInstance(Ci.nsIFileOutputStream);
   1.101 +  // open, write, truncate
   1.102 +  fstream.init(file, -1, -1, 0);
   1.103 +  var os = Cc["@mozilla.org/intl/converter-output-stream;1"]
   1.104 +           .createInstance(Ci.nsIConverterOutputStream);
   1.105 +  os.init(fstream, "UTF-8", 0, 0x0000);
   1.106 +  os.writeString(data);
   1.107 +  os.close();
   1.108 +  fstream.close();
   1.109 +}
   1.110 +
   1.111 +function addPendingCrashreport(crD, date, extra) {
   1.112 +  let pendingdir = crD.clone();
   1.113 +  pendingdir.append("pending");
   1.114 +  let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]
   1.115 +                      .getService(Ci.nsIUUIDGenerator);
   1.116 +  let uuid = uuidGenerator.generateUUID().toString();
   1.117 +  // ditch the {}
   1.118 +  uuid = uuid.substring(1, uuid.length - 1);
   1.119 +  let dumpfile = pendingdir.clone();
   1.120 +  dumpfile.append(uuid + ".dmp");
   1.121 +  writeDataToFile(dumpfile, "MDMP"); // that's the start of a valid minidump, anyway
   1.122 +  let extrafile = pendingdir.clone();
   1.123 +  extrafile.append(uuid + ".extra");
   1.124 +  let extradata = "";
   1.125 +  for (let x in extra) {
   1.126 +    extradata += x + "=" + extra[x] + "\n";
   1.127 +  }
   1.128 +  writeDataToFile(extrafile, extradata);
   1.129 +  dumpfile.lastModifiedTime = date;
   1.130 +  extrafile.lastModifiedTime = date;
   1.131 +  return {'id': uuid, 'date': date, 'pending': true, 'extra': extra};
   1.132 +}

mercurial