home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / trtable.js < prev    next >
Text File  |  2006-08-07  |  5KB  |  138 lines

  1. function PROT_TRTable(name) {
  2. this.debugZone = "trtable";
  3. this.enchashDecrypter_ = new PROT_EnchashDecrypter();
  4. var components = name.split("-");
  5. if (components.length != 3)
  6. throw new Error("TRTables have names like name-function-type");
  7. var handlerTable = {
  8. "domain" : this.lookupDomain_,
  9. "enchash" : this.lookupEnchash_,
  10. "url" : this.lookupURL_,
  11. "text" : this.lookupText_
  12. };
  13. this.find = handlerTable[components[2]];
  14. if (!this.find)
  15. throw new Error("Unknown table type: " + components[2]);
  16. this.name_ = name;
  17. }
  18. PROT_TRTable.prototype.find_ = function(key, callback) {
  19. var dbservice_ = Cc["@google.com/dbupdateservice;1"]
  20. .getService(Ci.GTBIDbUpdateService);
  21. dbservice_.exists(this.name_, key, callback);
  22. }
  23. PROT_TRTable.prototype.lookupText_ = function(key, callback) {
  24. G_Debug(this, "looking up " + key + " in " + this.name_);
  25. this.find_(key, callback);
  26. }
  27. PROT_TRTable.prototype.lookupURL_ = function(url, callback) {
  28. var canonicalized = PROT_URLCanonicalizer.canonicalizeURL_(url);
  29. this.find_(canonicalized, callback);
  30. }
  31. PROT_TRTable.prototype.lookupDomain_ = function(url, callback) {
  32. var ioService = Cc["@mozilla.org/network/io-service;1"]
  33. .getService(Ci.nsIIOService);
  34. var canonicalized = PROT_URLCanonicalizer.canonicalizeURL_(url);
  35. var urlObj = ioService.newURI(canonicalized, null, null);
  36. var host = '';
  37. try {
  38. host = urlObj.host;
  39. } catch (e) { }
  40. var hostComponents = host.split(".");
  41. var path = ""
  42. try {
  43. urlObj.QueryInterface(Ci.nsIURL);
  44. path = urlObj.filePath;
  45. } catch (e) { }
  46. var pathComponents = path.split("/");
  47. var possible = [];
  48. for (var i = 0; i < hostComponents.length - 1; i++) {
  49. host = hostComponents.slice(i).join(".");
  50. possible.push(host);
  51. if (pathComponents.length >= 2 && pathComponents[1].length > 0) {
  52. host = host + "/" + pathComponents[1];
  53. possible.push(host);
  54. }
  55. }
  56. (new PROT_ExistsMultiQuerier(possible, this.name_, callback)).run();
  57. }
  58. PROT_TRTable.prototype.lookupEnchash_ = function(url, callback) {
  59. var host = this.enchashDecrypter_.getCanonicalHost(url);
  60. var possible = [];
  61. for (var i = 0; i < PROT_EnchashDecrypter.MAX_DOTS + 1; i++) {
  62. possible.push(host);
  63. var index = host.indexOf(".");
  64. if (index == -1)
  65. break;
  66. host = host.substring(index + 1);
  67. }
  68. (new PROT_EnchashMultiQuerier(possible, this.name_, callback, url)).run();
  69. }
  70. function TEST_PROT_TRTable() {
  71. if (G_GDEBUG) {
  72. var z = "trtable UNITTEST";
  73. G_debugService.enableZone(z);
  74. G_Debug(z, "Starting");
  75. var dbservice = Cc["@google.com/dbupdateservice;1"]
  76. .getService(Ci.GTBIDbUpdateService);
  77. var url = "http://www.yahoo.com?foo=bar";
  78. var url2 = "http://168.188.99.26/.secure/www.ebay.com/";
  79. var data =
  80. "[test-foo-url 1.1]\n" +
  81. "+" + url + "\t1\n" +
  82. "+" + url2 + "\t1\n" +
  83. "\n[test-foo-domain 1.1]\n" +
  84. "+bar.com\t1\n" +
  85. "+amazon.co.uk\tc\n" +
  86. "+127.0.0.1\t1\n" +
  87. "+foo.com/path\t1\n";
  88. function createTrueCallback(msg) {
  89. return function(s) {
  90. if (!s) {
  91. G_Error(z, msg + " (" + s + ")");
  92. }
  93. }
  94. }
  95. function createFalseCallback(msg) {
  96. return function(s) {
  97. if (s) {
  98. G_Error(z, msg + " (" + s + ")");
  99. }
  100. }
  101. }
  102. function checkTables(version) {
  103. if (version == "[test-foo-url 1.1]") {
  104. G_Debug(z, "got first table callback");
  105. } else if (version == "[test-foo-domain 1.1]") {
  106. G_Debug(z, "checking first table");
  107. var urlTable = new PROT_TRTable("test-foo-url");
  108. urlTable.find(url, createTrueCallback("URL lookups broken"));
  109. urlTable.find(url2, createTrueCallback("URL lookups broken"));
  110. urlTable.find("http://%31%36%38%2e%31%38%38%2e%39%39%2e%32%36/%2E%73%65%63%75%72%65/%77%77%77%2E%65%62%61%79%2E%63%6F%6D/",
  111. createTrueCallback("Canonicalized URL lookups broken"));
  112. urlTable.find("about:config",
  113. createFalseCallback("shouldn't find about:config"));
  114. G_Debug(z, "checking second table");
  115. var domainTable = new PROT_TRTable("test-foo-domain");
  116. domainTable.find("http://www.bar.com/?zaz=asdf#url",
  117. createTrueCallback("Domain lookups broken (single dot)"));
  118. domainTable.find("http://www.amazon.co.uk/?z=af#url",
  119. createTrueCallback("Domain lookups broken (two dots)"));
  120. domainTable.find("http://127.0.0.1/?z=af#url",
  121. createTrueCallback("Domain lookups broken (IP)"));
  122. domainTable.find("http://%31%32%37%2e%30%2e%30%2e%31/?z=af#url",
  123. createTrueCallback("Canonicalized domain lookups broken"));
  124. domainTable.find("http://www.foo.com/?zaz=asdf#url",
  125. createFalseCallback("Domain lookups broken (path mismatch)"));
  126. domainTable.find("http://www.foo.com/path?zaz=asdf#url",
  127. createTrueCallback("Domain lookups broken (path mismatch)"));
  128. domainTable.find("http://www.foo.com/path/more?zaz=asdf#url",
  129. createTrueCallback("Domain lookups broken (long path mismatch)"));
  130. G_Debug(z, "PASSED");
  131. } else {
  132. G_Error(z, 'Unknown version string: ' + version);
  133. }
  134. }
  135. dbservice.updateTables(data, checkTables);
  136. }
  137. }
  138.