diff options
Diffstat (limited to 'dom/system/tests/ioutils/test_ioutils_stat_touch.html')
-rw-r--r-- | dom/system/tests/ioutils/test_ioutils_stat_touch.html | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/dom/system/tests/ioutils/test_ioutils_stat_touch.html b/dom/system/tests/ioutils/test_ioutils_stat_touch.html new file mode 100644 index 0000000000..c8b052fadb --- /dev/null +++ b/dom/system/tests/ioutils/test_ioutils_stat_touch.html @@ -0,0 +1,204 @@ +<!-- Any copyright is dedicated to the Public Domain. +- http://creativecommons.org/publicdomain/zero/1.0/ --> +<!DOCTYPE HTML> +<html> + +<head> + <meta charset="utf-8"> + <title>Test the IOUtils file I/O API</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css" /> + <script src="file_ioutils_test_fixtures.js"></script> + <script> + "use strict"; + + const { Assert } = ChromeUtils.import("resource://testing-common/Assert.jsm"); + const { ObjectUtils } = ChromeUtils.import("resource://gre/modules/ObjectUtils.jsm"); + const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); + + add_task(async function test_stat() { + info("Test attempt to stat a regular empty file"); + + const tmpDir = await PathUtils.getTempDir(); + + const emptyFileName = PathUtils.join(tmpDir, "test_stat_empty.tmp"); + await createFile(emptyFileName); + + const emptyFileInfo = await IOUtils.stat(emptyFileName); + is(emptyFileInfo.size, 0, "IOUtils::stat can get correct (empty) file size"); + is(emptyFileInfo.path, emptyFileName, "IOUtils::stat result contains the path"); + is(emptyFileInfo.type, "regular", "IOUtils::stat can stat regular (empty) files"); + Assert.less( + (emptyFileInfo.lastModified - new Date().valueOf()), + 1000, // Allow for 1 second deviation in case of slow tests. + "IOUtils::stat can get the last modification date for a regular file" + ); + + info("Test attempt to stat a regular binary file"); + const tempFileName = PathUtils.join(tmpDir, "test_stat_binary.tmp"); + const bytes = Uint8Array.of(...new Array(50).keys()); + await createFile(tempFileName, bytes); + + const fileInfo = await IOUtils.stat(tempFileName); + is(fileInfo.size, 50, "IOUtils::stat can get correct file size"); + is(fileInfo.path, tempFileName, "IOUtils::stat result contains the path"); + is(fileInfo.type, "regular", "IOUtils::stat can stat regular files"); + Assert.less( + (fileInfo.lastModified - new Date().valueOf()), + 1000, // Allow for 1 second deviation in case of slow tests. + "IOUtils::stat can get the last modification date for a regular file" + ); + + info("Test attempt to stat a directory"); + const tempDirName = PathUtils.join(tmpDir, "test_stat_dir.tmp.d"); + await IOUtils.makeDirectory(tempDirName); + + const dirInfo = await IOUtils.stat(tempDirName); + is(dirInfo.size, -1, "IOUtils::stat reports -1 size for directories") + is(fileInfo.path, tempFileName, "IOUtils::stat result contains the path"); + is(fileInfo.type, "regular", "IOUtils::stat can stat directories"); + Assert.less( + (fileInfo.lastModified - new Date().valueOf()), + 1000, // Allow for 1 second deviation in case of slow tests. + "IOUtils::stat can get the last modification date for a regular file" + ); + + await cleanup(emptyFileName, tempFileName, tempDirName) + }); + + add_task(async function test_stat_failures() { + info("Test attempt to stat a non-existing file"); + + const tmpDir = await PathUtils.getTempDir(); + + const notExistsFile = PathUtils.join(tmpDir, "test_stat_not_exists.tmp"); + + await Assert.rejects( + IOUtils.stat(notExistsFile), + /Could not stat file\(.*\) because it does not exist/, + "IOUtils::stat throws if the target file does not exist" + ); + }); + + add_task(async function test_touch_and_stat() { + info("Test attempt to touch a file"); + + const tmpDir = await PathUtils.getTempDir(); + + const tmpFileName = PathUtils.join(tmpDir, "test_touch_and_stat.tmp"); + await createFile(tmpFileName); + + const oldFileInfo = await IOUtils.stat(tmpFileName); + await sleep(500); + + // Now update the time stamp. + const stamp = await IOUtils.touch(tmpFileName); + const newFileInfo = await IOUtils.stat(tmpFileName); + + ok( + newFileInfo.lastModified > oldFileInfo.lastModified, + "IOUtils::touch can update the lastModified time stamp on the file system" + ); + is( + stamp, + newFileInfo.lastModified, + "IOUtils::touch returns the updated time stamp." + ); + + info("Test attempt to touch a directory"); + const tmpDirName = PathUtils.join(tmpDir, "test_touch_and_stat.tmp.d"); + await createDir(tmpDirName); + + await cleanup(tmpFileName, tmpDirName); + }); + + add_task(async function test_touch_custom_mod_time() { + const tmpDir = await PathUtils.getTempDir(); + + const tempFileName = PathUtils.join(tmpDir, "test_touch_custom_mod_time.tmp"); + await createFile(tempFileName); + const originalInfo = await IOUtils.stat(tempFileName); + const now = originalInfo.lastModified; + + const oneMinute = 60 * 1000; // milliseconds + + info("Test attempt to set modification time to the future"); + const future = now + oneMinute; + let newModTime = await IOUtils.touch(tempFileName, future); + const futureInfo = await IOUtils.stat(tempFileName); + Assert.less(originalInfo.lastModified, futureInfo.lastModified, "IOUtils::touch can set a future modification time for the file"); + + is(newModTime, futureInfo.lastModified, "IOUtils::touch returns the updated time stamp"); + is(newModTime, future, "IOUtils::touch return value matches the argument value exactly"); + + info("Test attempt to set modification time to the past"); + const past = now - 2 * oneMinute; + newModTime = await IOUtils.touch(tempFileName, past); + const pastInfo = await IOUtils.stat(tempFileName); + Assert.greater(originalInfo.lastModified, pastInfo.lastModified, "IOUtils::touch can set a past modification time for the file"); + + is(newModTime, pastInfo.lastModified, "IOUtils::touch returns the updated time stamp"); + is(newModTime, past, "IOUtils::touch return value matches the argument value exactly"); + + await cleanup(tempFileName); + }); + + add_task(async function test_stat_btime() { + if (["Darwin", "WINNT"].includes(Services.appinfo.OS)) { + const tmpDir = await PathUtils.getTempDir(); + + const tempFileName = PathUtils.join(tmpDir, "test_stat_btime.tmp"); + await createFile(tempFileName); + const originalInfo = await IOUtils.stat(tempFileName); + + const future = originalInfo.lastModified + 6000; + await IOUtils.touch(tempFileName, future); + const futureInfo = await IOUtils.stat(tempFileName); + + ok(originalInfo.hasOwnProperty("creationTime"), "originalInfo has creationTime field"); + ok(originalInfo.creationTime !== undefined && originalInfo.creationTime !== null, "originalInfo has non-null creationTime"); + + ok(futureInfo.hasOwnProperty("creationTime"), "futureInfo has creationTime field"); + ok(futureInfo.creationTime !== undefined && futureInfo.creationTime !== null, "futureInfo has non-null creationTime"); + + is(originalInfo.creationTime, futureInfo.creationTime, "creationTime matches"); + + await cleanup(tempFileName); + } else { + ok(true, `skipping test_stat_btime() on unsupported platform ${Services.appinfo.OS}`); + } + }); + + add_task(async function test_touch_failures() { + info("Test attempt to touch a non-existing file"); + const tmpDir = await PathUtils.getTempDir(); + const notExistsFile = PathUtils.join(tmpDir, "test_touch_not_exists.tmp"); + + await Assert.rejects( + IOUtils.touch(notExistsFile), + /Could not touch file\(.*\) because it does not exist/, + "IOUtils::touch throws if the target file does not exist" + ); + + info("Test attempt to set modification time to Epoch"); + const tempFileName = PathUtils.join(tmpDir, "test_touch_epoch.tmp"); + await createFile(tempFileName); + + await Assert.rejects( + IOUtils.touch(tempFileName, 0), + /Refusing to set the modification time of file\(.*\) to 0/, + "IOUtils::touch cannot set the file modification time to Epoch" + ); + + await cleanup(tempFileName); + }); + </script> +</head> + +<body> + <p id="display"></p> + <div id="content" style="display: none"></div> + <pre id="test"></pre> +</body> + +</html> |