B2G/gecko/netwerk/test/unit/test_filestreams.js
/**
 * Tests for files being accessed at the right time. Streams that use
 * DEFER_OPEN should only open or create the file when an operation is
 * done, and not during Init().
 *
 * Note that for writing, we check for actual writing in test_NetUtil (async)
 * and in sync_operations in this file (sync), whereas in this function we
 * just check that the file is *not* created during init.
 *
 * @param aContractId
 *        The contract ID to use for the output stream
 * @param aDeferOpen
 *        Whether to check with DEFER_OPEN or not
 * @param aTrickDeferredOpen
 *        Whether we try to 'trick' deferred opens by changing the file object before
 *        the actual open. The stream should have a clone, so changes to the file
 *        object after Init and before Open should not affect it.
 */
Tests for files being accessed at the right time. Streams that use
DEFER_OPEN should only open or create the file when an operation is
done, and not during Init().

Note that for writing, we check for actual writing in test_NetUtil (async)
and in sync_operations in this file (sync), whereas in this function we
just check that the file is *not* created during init.

@param aContractId
The contract ID to use for the output stream
@param aDeferOpen
Whether to check with DEFER_OPEN or not
@param aTrickDeferredOpen
Whether we try to 'trick' deferred opens by changing the file object before
the actual open. The stream should have a clone, so changes to the file
object after Init and before Open should not affect it.
Tests for files being accessed at the right time . Streams that use DEFER_OPEN should only open or create the file when an operation is done and not during Init () . Note that for writing we check for actual writing in test_NetUtil ( async ) and in sync_operations in this file ( sync ) whereas in this function we just check that the file is * not * created during init . @param aContractId The contract ID to use for the output stream @param aDeferOpen Whether to check with DEFER_OPEN or not @param aTrickDeferredOpen Whether we try to ' trick ' deferred opens by changing the file object before the actual open . The stream should have a clone so changes to the file object after Init and before Open should not affect it .
function check_access(aContractId, aDeferOpen, aTrickDeferredOpen)
{
  const LEAF_NAME = "filestreams-test-file.tmp";
  const TRICKY_LEAF_NAME = "BetYouDidNotExpectThat.tmp";
  let file = Cc["@mozilla.org/file/directory_service;1"].
             getService(Ci.nsIProperties).
             get("ProfD", Ci.nsIFile);
  file.append(LEAF_NAME);
  ensure_unique(file);
  let ostream = Cc[aContractId].createInstance(Ci.nsIFileOutputStream);
  ostream.init(file, -1, -1, aDeferOpen ? Ci.nsIFileOutputStream.DEFER_OPEN : 0);
  do_check_eq(aDeferOpen, !file.clone().exists()); 
  if (aDeferOpen) {
    if (aTrickDeferredOpen) {
      file.leafName = TRICKY_LEAF_NAME;
    }
    ostream.write("data", 4);
    if (aTrickDeferredOpen) {
      file.leafName = LEAF_NAME;
    }
    do_check_true(file.clone().exists());
  }
  ostream.close();
  ensure_unique(file);
  let istream = Cc["@mozilla.org/network/file-input-stream;1"].
                createInstance(Ci.nsIFileInputStream);
  var initOk, getOk;
  try {
    istream.init(file, -1, 0, aDeferOpen ? Ci.nsIFileInputStream.DEFER_OPEN : 0);
    initOk = true;
  }
  catch(e) {
    initOk = false;
  }
  try {
    let fstream = Cc["@mozilla.org/network/file-input-stream;1"].
                  createInstance(Ci.nsIFileInputStream);
    fstream.init(aFile, -1, 0, 0);
    getOk = true;
  }
  catch(e) {
    getOk = false;
  }
  do_check_true( (aDeferOpen && initOk && !getOk) ||
                 (!aDeferOpen && !initOk && !getOk) );
  istream.close();
}
function check_access ( aContractId , aDeferOpen , aTrickDeferredOpen ) { const LEAF_NAME = "filestreams-test-file.tmp" ; const TRICKY_LEAF_NAME = "BetYouDidNotExpectThat.tmp" ; let file = Cc [ "@mozilla.org/file/directory_service;1" ] . getService ( Ci . nsIProperties ) . get ( "ProfD" , Ci . nsIFile ) ; file . append ( LEAF_NAME ) ; ensure_unique ( file ) ; let ostream = Cc [ aContractId ] . createInstance ( Ci . nsIFileOutputStream ) ; ostream . init ( file , - 1 , - 1 , aDeferOpen ? Ci . nsIFileOutputStream . DEFER_OPEN : 0 ) ; do_check_eq ( aDeferOpen , ! file . clone ( ) . exists ( ) ) ; if ( aDeferOpen ) { if ( aTrickDeferredOpen ) { file . leafName = TRICKY_LEAF_NAME ; } ostream . write ( "data" , 4 ) ; if ( aTrickDeferredOpen ) { file . leafName = LEAF_NAME ; } do_check_true ( file . clone ( ) . exists ( ) ) ; } ostream . close ( ) ; ensure_unique ( file ) ; let istream = Cc [ "@mozilla.org/network/file-input-stream;1" ] . createInstance ( Ci . nsIFileInputStream ) ; var initOk , getOk ; try { istream . init ( file , - 1 , 0 , aDeferOpen ? Ci . nsIFileInputStream . DEFER_OPEN : 0 ) ; initOk = true ; } catch ( e ) { initOk = false ; } try { let fstream = Cc [ "@mozilla.org/network/file-input-stream;1" ] . createInstance ( Ci . nsIFileInputStream ) ; fstream . init ( aFile , - 1 , 0 , 0 ) ; getOk = true ; } catch ( e ) { getOk = false ; } do_check_true ( ( aDeferOpen && initOk && ! getOk ) || ( ! aDeferOpen && ! initOk && ! getOk ) ) ; istream . close ( ) ; }
Tests for files being accessed at the right time.
Tests for files being accessed at the right time .
// Writing // If defer, should not exist and vice versa // File should appear when we do write to it. // See |@param aDeferOpen| in the JavaDoc comment for this function // We did a write, so the file should now exist // Reading // If the open is deferred, then Init should succeed even though the file we // intend to read does not exist, and then trying to read from it should // fail. The other case is where the open is not deferred, and there we should // get an error when we Init (and also when we try to read).