Bug 335545 - Count FD instead of looking for clipboardcache in test_bug1123480.xul r=mstange
☠☠ backed out by ffcfc5b57722 ☠ ☠
authorRob Wu <rob@robwu.nl>
Thu, 21 Sep 2017 14:17:27 +0200
changeset 433864 25a686779a940f1478aa2f4d1595f81344caac5c
parent 433863 b83ddb70c8b515b89b3e385d5b46f22b165792ed
child 433865 a44a81c459ab2145cb40dfccbbedd85668c247f4
push id8114
push userjlorenzo@mozilla.com
push dateThu, 02 Nov 2017 16:33:21 +0000
treeherdermozilla-beta@73e0d89a540f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs335545, 1123480
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 335545 - Count FD instead of looking for clipboardcache in test_bug1123480.xul r=mstange - Count open file descriptors instead of testing the existence of a file (because the clipboard is now only reachable through file descriptors, and not through a file path). - Use a fixed string instead of a random string. The previous way of generating a string was non-deterministic, and there was a very small chance that the generated string was not large enough to trigger the cache-to-disk-mode. MozReview-Commit-ID: KOkYOm280Oh
widget/tests/chrome.ini
widget/tests/test_bug1123480.xul
--- a/widget/tests/chrome.ini
+++ b/widget/tests/chrome.ini
@@ -97,10 +97,11 @@ support-files = chrome_context_menus_win
 [test_plugin_input_event.html]
 skip-if = toolkit != "windows"
 [test_mouse_scroll.xul]
 skip-if = toolkit != "windows"
 support-files = window_mouse_scroll_win.html
 
 # Privacy relevant
 [test_bug1123480.xul]
+skip-if = os == "win" # test uses /dev/fd, which is only available on *NIX
 subsuite = clipboard
 
--- a/widget/tests/test_bug1123480.xul
+++ b/widget/tests/test_bug1123480.xul
@@ -8,48 +8,55 @@ https://bugzilla.mozilla.org/show_bug.cg
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
         onload="RunTest();">
   <title>nsTransferable PBM Overflow Selection Test</title>
   <script type="application/javascript"
           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
 
   <script type="application/javascript">
   <![CDATA[
-  // Boilerplate constructs
-  var SmallDataset = 100000; // Hundred thousand bytes
+  // Create 1 Mo of sample garbage text
+  // Since JS strings are saved as UTF16 strings, the string size is 2 MB,
+  // which is well above the kLargeDatasetSize threshold in nsTransferable.h
+  var Ipsum = "0123456789".repeat(100000);
+  var SHORT_STRING_NO_CACHE = "short string that will never be cached to the disk";
 
-  // Create 1 Mo of sample garbage text
-  var Ipsum = ""; // Select text from this
-  for (var Iter = 0; Iter < SmallDataset; Iter++) {
-      Ipsum += Math.random().toString(36) + ' ';
+  // Get a list of open file descriptors, and ASSUME that any mutations in file
+  // descriptor counts are caused by our test. This is a bold assumption,
+  // but better than the alternatives (spawning lsof as a subprocess and parsing
+  // its output, or adding test-only code to nsAnonymousTemporaryFile).
+  function getOpenFileDescriptorCount() {
+    // Note: /dev/fd is only available on Linux and macOS, not Windows.
+    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsIFile);
+    file.initWithPath("/dev/fd");
+    var count = 0;
+    for (var de = file.directoryEntries; de.hasMoreElements(); de.getNext()) {
+      ++count;
+    }
+    return count;
   }
 
   function RunTest() {
-    // Construct a nsIFile object for access to file methods
-    Components.utils.import("resource://gre/modules/FileUtils.jsm");
-    var clipboardFile = FileUtils.getFile("TmpD", ["clipboardcache"]);
+    const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
 
     // Sanitize environment
-    if (clipboardFile.exists()) {
-        clipboardFile.remove(false);
-    }
-    ok(!clipboardFile.exists(), "failed to presanitize the environment");
+    gClipboardHelper.copyString(SHORT_STRING_NO_CACHE);
+
+    var initialFdCount = getOpenFileDescriptorCount();
+    var expectedFdCount = initialFdCount + 1;
 
     // Overflow a nsTransferable region by using the clipboard helper
-    const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
     gClipboardHelper.copyString(Ipsum);
 
     // Disabled private browsing mode should cache large selections to disk
-    ok(clipboardFile.exists(), "correctly saved memory by caching to disk");
+    is(getOpenFileDescriptorCount(), expectedFdCount, "correctly saved memory by caching to disk");
 
     // Sanitize environment again
-    if (clipboardFile.exists()) {
-        clipboardFile.remove(false);
-    }
-    ok(!clipboardFile.exists(), "failed to postsanitize the environment");
+    gClipboardHelper.copyString(SHORT_STRING_NO_CACHE);
+    is(getOpenFileDescriptorCount(), initialFdCount, "failed to evict the file descriptor of the clipboard cache");
 
     // Repeat procedure of plain text selection with private browsing enabled
     Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
     var Winpriv = window.open("about:blank", "_blank", "chrome, width=500, height=200, private");
     ok(Winpriv, "failed to open private window");
     ok(PrivateBrowsingUtils.isContentWindowPrivate(Winpriv), "correctly used a private window context");
 
     // Select plaintext in private channel
@@ -61,17 +68,17 @@ https://bugzilla.mozilla.org/show_bug.cg
     var Suppstr = nsSupportsString();
     Suppstr.data = Ipsum;
     Transfer.init(Loadctx);
     Transfer.addDataFlavor("text/plain");
     Transfer.setTransferData("text/plain", Suppstr, Ipsum.length);
     Services.clipboard.setData(Transfer, null, Services.clipboard.kGlobalClipboard);
 
     // Enabled private browsing mode should not cache any selection to disk
-    ok(!clipboardFile.exists(), "did not violate private browsing mode");
+    is(getOpenFileDescriptorCount(), initialFdCount, "did not violate private browsing mode");
   }
   ]]>
   </script>
 
   <!-- test results are displayed in the html:body -->
   <body xmlns="http://www.w3.org/1999/xhtml">
   <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1123480"
      target="_blank">Mozilla Bug 1123480</a>