gecko-dev/dom/devicestorage/test/test_app_permissions.html

662 строки
14 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=805322
-->
<head>
<meta charset="utf-8">
<title>Permission test for Device Storage</title>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css"
href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
</head>
<body>
<a target="_blank"
href="https://bugzilla.mozilla.org/show_bug.cgi?id=805322">Mozilla Bug 805322</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript;version=1.7">
function randomFilename(l) {
var set = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
var result = "";
for (var i=0; i<l; i++) {
var r = Math.floor(set.length * Math.random());
result += set.substring(r, r + 1);
}
return result;
}
var MockPermissionPrompt = SpecialPowers.MockPermissionPrompt;
MockPermissionPrompt.init();
SimpleTest.waitForExplicitFinish();
function TestAdd(iframe, data) {
var storage = iframe.contentDocument.defaultView.navigator.getDeviceStorage(data.type);
isnot(storage, null, "Should be able to get storage object for " + data.type);
var blob = new Blob(["Kyle Huey is not a helicopter."], {type: data.mimeType});
request = storage.addNamed(blob, randomFilename(100) + "hi" + data.fileExtension);
isnot(request, null, "Should be able to get request");
request.onsuccess = function() {
is(data.shouldPass, true, "onsuccess was called for type " + data.type);
testComplete(iframe, data);
};
request.onerror = function(e) {
isnot(data.shouldPass, true, "onfailure was called for type " + data.type + " Error: " + e.target.error.name);
is(e.target.error.name, "SecurityError", "onerror should fire a SecurityError");
testComplete(iframe, data);
};
}
function TestGet(iframe, data) {
createTestFile(data.fileExtension);
var storage = iframe.contentDocument.defaultView.navigator.getDeviceStorage(data.type);
isnot(storage, null, "Should be able to get storage object for " + data.type);
request = storage.get("testfile" + data.fileExtension);
isnot(request, null, "Should be able to get request");
request.onsuccess = function() {
is(data.shouldPass, true, "onsuccess was called for type " + data.type);
testComplete(iframe, data);
};
request.onerror = function(e) {
isnot(data.shouldPass, true, "onfailure was called for type " + data.type + " Error: " + e.target.error.name);
testComplete(iframe, data);
};
}
function TestDelete(iframe, data) {
createTestFile(data.fileExtension);
var storage = iframe.contentDocument.defaultView.navigator.getDeviceStorage(data.type);
isnot(storage, null, "Should be able to get storage object for " + data.type);
request = storage.delete("testfile" + data.fileExtension);
isnot(request, null, "Should be able to get request");
request.onsuccess = function() {
is(data.shouldPass, true, "onsuccess was called for type " + data.type);
testComplete(iframe, data);
};
request.onerror = function(e) {
isnot(data.shouldPass, true, "onfailure was called for type " + data.type + " Error: " + e.target.error.name);
is(e.target.error.name, "SecurityError", "onerror should fire a SecurityError");
testComplete(iframe, data);
};
}
function TestEnumerate(iframe, data) {
createTestFile(data.fileExtension);
var storage = iframe.contentDocument.defaultView.navigator.getDeviceStorage(data.type);
isnot(storage, null, "Should be able to get storage object for " + data.type);
request = storage.enumerate();
isnot(request, null, "Should be able to get request");
request.onsuccess = function(e) {
is(data.shouldPass, true, "onsuccess was called for type " + data.type);
if (e.target.result == null) {
testComplete(iframe, data);
return;
}
e.target.continue();
};
request.onerror = function(e) {
isnot(data.shouldPass, true, "onfailure was called for type " + data.type + " Error: " + e.target.error.name);
is(e.target.error.name, "SecurityError", "onerror should fire a SecurityError");
testComplete(iframe, data);
};
}
var gTestUri = "https://example.com/tests/dom/devicestorage/test/test_app_permissions.html"
var gData = [
// Get
// Web applications with no permissions
{
type: 'pictures',
shouldPass: false,
fileExtension: '.png',
test: TestGet
},
{
type: 'videos',
shouldPass: false,
fileExtension: '.ogv',
test: TestGet
},
{
type: 'music',
shouldPass: false,
fileExtension: '.ogg',
test: TestGet
},
{
type: 'sdcard',
shouldPass: false,
fileExtension: '.txt',
test: TestGet
},
// Web applications with permission granted
{
type: 'pictures',
shouldPass: true,
fileExtension: '.png',
permissions: ["device-storage:pictures"],
test: TestGet
},
{
type: 'videos',
shouldPass: true,
fileExtension: '.ogv',
permissions: ["device-storage:videos"],
test: TestGet
},
{
type: 'music',
shouldPass: true,
fileExtension: '.ogg',
permissions: ["device-storage:music"],
test: TestGet
},
{
type: 'sdcard',
shouldPass: true,
fileExtension: '.txt',
permissions: ["device-storage:sdcard"],
test: TestGet
},
// Certified application with permision granted
{
type: 'pictures',
shouldPass: true,
fileExtension: '.png',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:pictures"],
test: TestGet
},
{
type: 'videos',
shouldPass: true,
fileExtension: '.ogv',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:videos"],
test: TestGet
},
{
type: 'music',
shouldPass: true,
fileExtension: '.ogg',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:music"],
test: TestGet
},
{
type: 'sdcard',
shouldPass: true,
fileExtension: '.txt',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:sdcard"],
test: TestGet
},
// Add
// Web applications with no permissions
{
type: 'pictures',
mimeType: 'image/png',
fileExtension: '.png',
shouldPass: false,
test: TestAdd
},
{
type: 'videos',
mimeType: 'video/ogv',
fileExtension: '.ogv',
shouldPass: false,
test: TestAdd
},
{
type: 'music',
mimeType: 'audio/ogg',
fileExtension: '.ogg',
shouldPass: false,
test: TestAdd
},
{
type: 'sdcard',
mimeType: 'text/plain',
fileExtension: '.txt',
shouldPass: false,
test: TestAdd
},
// Web applications with permission granted
{
type: 'pictures',
mimeType: 'image/png',
fileExtension: '.png',
shouldPass: true,
permissions: ["device-storage:pictures"],
test: TestAdd
},
{
type: 'videos',
mimeType: 'video/ogv',
fileExtension: '.ogv',
shouldPass: true,
permissions: ["device-storage:videos"],
test: TestAdd
},
{
type: 'music',
mimeType: 'audio/ogg',
fileExtension: '.ogg',
shouldPass: true,
permissions: ["device-storage:music"],
test: TestAdd
},
{
type: 'sdcard',
mimeType: 'text/plain',
fileExtension: '.txt',
shouldPass: true,
permissions: ["device-storage:sdcard"],
test: TestAdd
},
// Certified application with permision granted
{
type: 'pictures',
mimeType: 'image/png',
fileExtension: '.png',
shouldPass: true,
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:pictures"],
test: TestAdd
},
{
type: 'videos',
mimeType: 'video/ogv',
fileExtension: '.ogv',
shouldPass: true,
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:videos"],
test: TestAdd
},
{
type: 'music',
mimeType: 'audio/ogg',
fileExtension: '.ogg',
shouldPass: true,
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:music"],
test: TestAdd
},
{
type: 'sdcard',
mimeType: 'text/plain',
fileExtension: '.txt',
shouldPass: true,
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:sdcard"],
test: TestAdd
},
// Delete
// Web applications with no permissions
{
type: 'pictures',
shouldPass: false,
fileExtension: '.png',
test: TestDelete
},
{
type: 'videos',
shouldPass: false,
fileExtension: '.ogv',
test: TestDelete
},
{
type: 'music',
shouldPass: false,
fileExtension: '.ogg',
test: TestDelete
},
{
type: 'sdcard',
shouldPass: false,
fileExtension: '.txt',
test: TestDelete
},
// Web applications with permission granted
{
type: 'pictures',
shouldPass: true,
fileExtension: '.png',
permissions: ["device-storage:pictures"],
test: TestDelete
},
{
type: 'videos',
shouldPass: true,
fileExtension: '.ogv',
permissions: ["device-storage:videos"],
test: TestDelete
},
{
type: 'music',
shouldPass: true,
fileExtension: '.ogg',
permissions: ["device-storage:music"],
test: TestDelete
},
{
type: 'sdcard',
shouldPass: true,
fileExtension: '.txt',
permissions: ["device-storage:sdcard"],
test: TestDelete
},
// Certified application with permision granted
{
type: 'pictures',
shouldPass: true,
fileExtension: '.png',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:pictures"],
test: TestDelete
},
{
type: 'videos',
shouldPass: true,
fileExtension: '.ogv',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:videos"],
test: TestDelete
},
{
type: 'music',
shouldPass: true,
fileExtension: '.ogg',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:music"],
test: TestDelete
},
{
type: 'sdcard',
shouldPass: true,
fileExtension: '.txt',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:sdcard"],
test: TestDelete
},
// Enumeration
// Web applications with no permissions
{
type: 'pictures',
shouldPass: false,
fileExtension: '.png',
test: TestEnumerate
},
{
type: 'videos',
shouldPass: false,
fileExtension: '.ogv',
test: TestEnumerate
},
{
type: 'music',
shouldPass: false,
fileExtension: '.ogg',
test: TestEnumerate
},
{
type: 'sdcard',
shouldPass: false,
fileExtension: '.txt',
test: TestEnumerate
},
// Web applications with permission granted
{
type: 'pictures',
shouldPass: true,
fileExtension: '.png',
permissions: ["device-storage:pictures"],
test: TestEnumerate
},
{
type: 'videos',
shouldPass: true,
fileExtension: '.ogv',
permissions: ["device-storage:videos"],
test: TestEnumerate
},
{
type: 'music',
shouldPass: true,
fileExtension: '.ogg',
permissions: ["device-storage:music"],
test: TestEnumerate
},
{
type: 'sdcard',
shouldPass: true,
fileExtension: '.txt',
permissions: ["device-storage:sdcard"],
test: TestEnumerate
},
// Certified application with permision granted
{
type: 'pictures',
shouldPass: true,
fileExtension: '.png',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:pictures"],
test: TestEnumerate
},
{
type: 'videos',
shouldPass: true,
fileExtension: '.ogv',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:videos"],
test: TestEnumerate
},
{
type: 'music',
shouldPass: true,
fileExtension: '.ogg',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:music"],
test: TestEnumerate
},
{
type: 'sdcard',
shouldPass: true,
fileExtension: '.txt',
app: "https://example.com/manifest_cert.webapp",
permissions: ["device-storage:sdcard"],
test: TestEnumerate
},
];
function setupTest(iframe,data) {
if (data.permissions) {
for (var j in data.permissions) {
SpecialPowers.addPermission(data.permissions[j], true, iframe.contentDocument);
}
}
}
function testComplete(iframe, data) {
if (data.permissions) {
for (var j in data.permissions) {
SpecialPowers.removePermission(data.permissions[j], iframe.contentDocument);
}
}
document.getElementById('content').removeChild(iframe);
if (gData.length == 0) {
SimpleTest.finish();
} else {
gTestRunner.next();
}
}
function runTest() {
while (gData.length > 0) {
var iframe = document.createElement('iframe');
var data = gData.pop();
iframe.setAttribute('mozbrowser', '');
if (data.app) {
iframe.setAttribute('mozapp', data.app);
}
iframe.src = gTestUri;
iframe.addEventListener('load', function(e) {
setupTest(iframe, data)
data.test(iframe, data);
});
document.getElementById('content').appendChild(iframe);
yield undefined;
}
}
function createTestFile(extension) {
try {
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
var directoryService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
var f = directoryService.get("TmpD", Ci.nsIFile);
f.appendRelativePath("device-storage-testing");
f.remove(true);
f.appendRelativePath("testfile" + extension);
f.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
} catch(e) {}
}
createTestFile('.txt');
var gTestRunner = runTest();
SpecialPowers.addPermission("browser", true, gTestUri);
// We are more permissive with CSP in our testing environment....
const DEFAULT_CSP_PRIV = "default-src *; script-src 'self'; style-src 'self' 'unsafe-inline'; object-src 'none'";
const DEFAULT_CSP_CERT = "default-src *; script-src 'self'; style-src 'self'; object-src 'none'";
SpecialPowers.pushPrefEnv({'set': [["dom.mozBrowserFramesEnabled", true],
["device.storage.enabled", true],
["device.storage.testing", true],
["device.storage.prompt.testing", false],
["security.apps.privileged.CSP.default", DEFAULT_CSP_PRIV],
["security.apps.certified.CSP.default", DEFAULT_CSP_CERT]]},
function() { gTestRunner.next(); });
</script>
</pre>
</body>
</html>