Add browser_tests for FileAPI with Quota, and remove unlimited access for...

Add browser_tests for FileAPI with Quota, and remove unlimited access for file:/// when allow_file_access_from_files.


BUG=95120
TEST=browser_tests:FileSystemBrowserTest*


Review URL: http://codereview.chromium.org/7715024

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99726 0039d316-1c4b-4281-b951-d872f2087c98
parent fc0de267
...@@ -2619,6 +2619,7 @@ ...@@ -2619,6 +2619,7 @@
'../content/browser/child_process_security_policy_browsertest.cc', '../content/browser/child_process_security_policy_browsertest.cc',
'../content/browser/device_orientation/device_orientation_browsertest.cc', '../content/browser/device_orientation/device_orientation_browsertest.cc',
'../content/browser/download/mhtml_generation_browsertest.cc', '../content/browser/download/mhtml_generation_browsertest.cc',
'../content/browser/file_system/file_system_browsertest.cc',
'../content/browser/in_process_webkit/dom_storage_browsertest.cc', '../content/browser/in_process_webkit/dom_storage_browsertest.cc',
'../content/browser/in_process_webkit/indexed_db_browsertest.cc', '../content/browser/in_process_webkit/indexed_db_browsertest.cc',
'../content/browser/plugin_service_browsertest.cc', '../content/browser/plugin_service_browsertest.cc',
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function debug(message)
{
document.getElementById('status').innerHTML += '<br/>' + message;
}
function done(message)
{
if (document.location.hash == '#fail')
return;
if (message)
debug('PASS: ' + message);
else
debug('PASS');
document.location.hash = '#pass';
}
function fail(message)
{
debug('FAILED: ' + message);
document.location.hash = '#fail';
}
function getLog()
{
return '' + document.getElementById('status').innerHTML;
}
function fileErrorToString(e)
{
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
return 'QUOTA_EXCEEDED_ERR';
case FileError.NOT_FOUND_ERR:
return 'NOT_FOUND_ERR';
case FileError.SECURITY_ERR:
return 'SECURITY_ERR';
case FileError.INVALID_MODIFICATION_ERR:
return 'INVALID_MODIFICATION_ERR';
case FileError.INVALID_STATE_ERR:
return 'INVALID_STATE_ERR';
default:
return 'Unknown Error';
}
}
function unexpectedErrorCallback(e)
{
fail('unexpectedErrorCallback:' + fileErrorToString(e));
}
<html>
<head>
<title>FileAPI create test</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="create_test.js"></script>
</head>
<body onLoad="test()">
<div id="status">Starting...</div>
</body>
</html>
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function requestFileSystemSuccess(fs)
{
fs.root.getFile('foo', {create: true, exclusive: false}, done,
function(e) { fail('Open:' + fileErrorToString(e)); } );
}
function test()
{
debug('Requesting FileSystem');
window.webkitRequestFileSystem(
window.TEMPORARY,
1024 * 1024,
requestFileSystemSuccess,
unexpectedErrorCallback);
}
<html>
<head>
<title>FileAPI quota test</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="quota_test.js"></script>
</head>
<body onLoad="test()">
<div id="status">Starting...</div>
</body>
</html>
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function truncateFailByQuota(fs) {
fs.root.getFile('fd', {create: false, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
var failedInTruncate = false;
fileWriter.onerror = function(e) {
failedInTruncate = true;
};
fileWriter.onwriteend = function(e) {
if (failedInTruncate) {
fail(e.currentTarget.error);
} else {
done();
}
};
fileWriter.truncate(2500 * 1024);
}, unexpectedErrorCallback)
}, function(e) { fail('Open for 2nd truncate:' + fileErrorToString(e)); } );
}
function requestFileSystemSuccess(fs) {
fs.root.getFile('fd', {create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
var failedInTruncate = false;
fileWriter.onerror = function(e) {
debug(e.currentTarget.error);
failedInTruncate = true;
};
fileWriter.onwriteend = function() {
if (failedInTruncate) {
truncateFailByQuota(fs);
} else {
fail('Unexpectedly succeeded to truncate. It should fail by quota.');
}
};
fileWriter.truncate(10000 * 1024);
}, unexpectedErrorCallback)
}, function(e) { fail('Open for 1st truncate:' + fileErrorToString(e)); } );
}
function quotaSuccess(usage, quota) {
if (usage != 0)
fail('Usage is not zero: ' + usage);
if (quota != 5000 * 1024)
fail('Quota is not 5000KiB: ' + quota);
window.webkitRequestFileSystem(
window.TEMPORARY,
1024 * 1024,
requestFileSystemSuccess,
unexpectedErrorCallback);
}
function test() {
if (window.webkitStorageInfo) {
debug('Querying usage and quota.');
webkitStorageInfo.queryUsageAndQuota(webkitStorageInfo.TEMPORARY,
quotaSuccess,
unexpectedErrorCallback);
} else {
debug('This test requires window.webkitStorageInfo.');
}
}
<html>
<head>
<title>FileAPI request test</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="request_test.js"></script>
</head>
<body onLoad="test()">
<div id="status">Starting...</div>
</body>
</html>
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function requestFileSystemSuccess(fs)
{
debug('Requested successfully.');
done();
}
function test()
{
debug('Requesting FileSystem');
window.webkitRequestFileSystem(
window.TEMPORARY,
1024 * 1024,
requestFileSystemSuccess,
unexpectedErrorCallback);
}
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/test/thread_test_helper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/common/content_switches.h"
#include "webkit/quota/quota_manager.h"
using quota::QuotaManager;
// This browser test is aimed towards exercising the FileAPI bindings and
// the actual implementation that lives in the browser side.
class FileSystemBrowserTest : public InProcessBrowserTest {
public:
FileSystemBrowserTest() {
EnableDOMAutomation();
}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
}
GURL testUrl(const FilePath& file_path) {
const FilePath kTestDir(FILE_PATH_LITERAL("fileapi"));
return ui_test_utils::GetTestUrl(kTestDir, file_path);
}
void SimpleTest(const GURL& test_url, bool incognito = false) {
// The test page will perform tests on FileAPI, then navigate to either
// a #pass or #fail ref.
Browser* the_browser = incognito ? CreateIncognitoBrowser() : browser();
LOG(INFO) << "Navigating to URL and blocking.";
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
the_browser, test_url, 2);
LOG(INFO) << "Navigation done.";
std::string result = the_browser->GetSelectedTabContents()->GetURL().ref();
if (result != "pass") {
std::string js_result;
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
the_browser->GetSelectedTabContents()->render_view_host(), L"",
L"window.domAutomationController.send(getLog())", &js_result));
FAIL() << "Failed: " << js_result;
}
}
};
class FileSystemBrowserTestWithLowQuota : public FileSystemBrowserTest {
public:
virtual void SetUpOnMainThread() {
const int kInitialQuotaKilobytes = 5000;
const int kTemporaryStorageQuotaMaxSize =
kInitialQuotaKilobytes * 1024 * QuotaManager::kPerHostTemporaryPortion;
SetTempQuota(
kTemporaryStorageQuotaMaxSize, browser()->profile()->GetQuotaManager());
}
class SetTempQuotaCallback : public quota::QuotaCallback {
public:
void Run(quota::QuotaStatusCode, quota::StorageType, int64) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
}
void RunWithParams(const Tuple3<quota::QuotaStatusCode,
quota::StorageType,
int64>& params) {
Run(params.a, params.b, params.c);
}
};
static void SetTempQuota(int64 bytes, scoped_refptr<QuotaManager> qm) {
if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
NewRunnableFunction(&FileSystemBrowserTestWithLowQuota::SetTempQuota,
bytes, qm));
return;
}
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
qm->SetTemporaryGlobalQuota(bytes, new SetTempQuotaCallback);
// Don't return until the quota has been set.
scoped_refptr<base::ThreadTestHelper> helper(
new base::ThreadTestHelper(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
ASSERT_TRUE(helper->Run());
}
};
IN_PROC_BROWSER_TEST_F(FileSystemBrowserTest, RequestTest) {
SimpleTest(testUrl(FilePath(FILE_PATH_LITERAL("request_test.html"))));
}
IN_PROC_BROWSER_TEST_F(FileSystemBrowserTest, CreateTest) {
SimpleTest(testUrl(FilePath(FILE_PATH_LITERAL("create_test.html"))));
}
IN_PROC_BROWSER_TEST_F(FileSystemBrowserTestWithLowQuota, QuotaTest) {
SimpleTest(testUrl(FilePath(FILE_PATH_LITERAL("quota_test.html"))));
}
...@@ -62,7 +62,6 @@ bool FileSystemContext::IsStorageUnlimited(const GURL& origin) { ...@@ -62,7 +62,6 @@ bool FileSystemContext::IsStorageUnlimited(const GURL& origin) {
// is file, or if unlimited quota for this process was explicitly requested, // is file, or if unlimited quota for this process was explicitly requested,
// return true. // return true.
return unlimited_quota_ || return unlimited_quota_ ||
(allow_file_access_from_files_ && origin.SchemeIsFile()) ||
(special_storage_policy_.get() && (special_storage_policy_.get() &&
special_storage_policy_->IsStorageUnlimited(origin)); special_storage_policy_->IsStorageUnlimited(origin));
} }
......
...@@ -49,15 +49,6 @@ TEST(FileSystemContextTest, IsStorageUnlimited) { ...@@ -49,15 +49,6 @@ TEST(FileSystemContextTest, IsStorageUnlimited) {
EXPECT_FALSE(context->IsStorageUnlimited(GURL(kTestOrigins[i]))); EXPECT_FALSE(context->IsStorageUnlimited(GURL(kTestOrigins[i])));
} }
// With allow_file_access=true cases.
context = NewFileSystemContext(true, false, NULL);
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestOrigins); ++i) {
SCOPED_TRACE(testing::Message() << "IsStorageUnlimited /w "
"allow_file_access=true #" << i << " " << kTestOrigins[i]);
GURL origin(kTestOrigins[i]);
EXPECT_EQ(origin.SchemeIsFile(), context->IsStorageUnlimited(origin));
}
// With unlimited_quota=true cases. // With unlimited_quota=true cases.
context = NewFileSystemContext(false, true, NULL); context = NewFileSystemContext(false, true, NULL);
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestOrigins); ++i) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestOrigins); ++i) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment