extensions/cookie/test/unit/test_permmanager_load_invalid_entries.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 var DEBUG_TEST = false;
michael@0 5
michael@0 6 function run_test() {
michael@0 7 // Setup a profile directory.
michael@0 8 var dir = do_get_profile();
michael@0 9 // Get the db file.
michael@0 10 var file = dir.clone();
michael@0 11 file.append("permissions.sqlite");
michael@0 12
michael@0 13 var storage = Cc["@mozilla.org/storage/service;1"]
michael@0 14 .getService(Ci.mozIStorageService);
michael@0 15
michael@0 16 // Create database.
michael@0 17 var connection = storage.openDatabase(file);
michael@0 18 // The file should now exist.
michael@0 19 do_check_true(file.exists());
michael@0 20
michael@0 21 connection.schemaVersion = 3;
michael@0 22 connection.executeSimpleSQL(
michael@0 23 "CREATE TABLE moz_hosts (" +
michael@0 24 " id INTEGER PRIMARY KEY" +
michael@0 25 ",host TEXT" +
michael@0 26 ",type TEXT" +
michael@0 27 ",permission INTEGER" +
michael@0 28 ",expireType INTEGER" +
michael@0 29 ",expireTime INTEGER" +
michael@0 30 ",appId INTEGER" +
michael@0 31 ",isInBrowserElement INTEGER" +
michael@0 32 ")");
michael@0 33
michael@0 34 // Now we can inject garbadge in the database.
michael@0 35 var garbadge = [
michael@0 36 // Regular entry.
michael@0 37 { host: '42', type: '0', permission: 1, expireType: 0, expireTime: 0,
michael@0 38 appId: 0, isInBrowserElement: 0 },
michael@0 39
michael@0 40 // Special values in host (some being invalid).
michael@0 41 { host: 'scheme:file', type: '1', permission: 0, expireType: 0,
michael@0 42 expireTime: 0, appId: 0, isInBrowserElement: 0 },
michael@0 43 { host: '192.168.0.1', type: '2', permission: 0, expireType: 0,
michael@0 44 expireTime: 0, appId: 0, isInBrowserElement: 0 },
michael@0 45 { host: '2001:0db8:0000:0000:0000:ff00:0042:8329', type: '3', permission: 0,
michael@0 46 expireType: 0, expireTime: 0, appId: 0, isInBrowserElement: 0 },
michael@0 47 { host: '::1', type: '4', permission: 0, expireType: 0, expireTime: 0,
michael@0 48 appId: 0, isInBrowserElement: 0 },
michael@0 49
michael@0 50 // Permission is UNKNOWN_ACTION.
michael@0 51 { host: '42', type: '5', permission: Ci.nsIPermissionManager.UNKNOWN_ACTION,
michael@0 52 expireType: 0, expireTime: 0, appId: 0, isInBrowserElement: 0 },
michael@0 53
michael@0 54 // Permission is out of range.
michael@0 55 { host: '42', type: '6', permission: 100, expireType: 0, expireTime: 0,
michael@0 56 appId: 0, isInBrowserElement: 0 },
michael@0 57 { host: '42', type: '7', permission: -100, expireType: 0, expireTime: 0,
michael@0 58 appId: 0, isInBrowserElement: 0 },
michael@0 59
michael@0 60 // ExpireType is out of range.
michael@0 61 { host: '42', type: '8', permission: 1, expireType: -100, expireTime: 0,
michael@0 62 appId: 0, isInBrowserElement: 0 },
michael@0 63 { host: '42', type: '9', permission: 1, expireType: 100, expireTime: 0,
michael@0 64 appId: 0, isInBrowserElement: 0 },
michael@0 65
michael@0 66 // ExpireTime is at 0 with ExpireType = Time.
michael@0 67 { host: '42', type: '10', permission: 1,
michael@0 68 expireType: Ci.nsIPermissionManager.EXPIRE_TIME, expireTime: 0, appId: 0,
michael@0 69 isInBrowserElement: 0 },
michael@0 70
michael@0 71 // ExpireTime has a value with ExpireType != Time
michael@0 72 { host: '42', type: '11', permission: 1,
michael@0 73 expireType: Ci.nsIPermissionManager.EXPIRE_SESSION, expireTime: 1000,
michael@0 74 appId: 0, isInBrowserElement: 0 },
michael@0 75 { host: '42', type: '12', permission: 1,
michael@0 76 expireType: Ci.nsIPermissionManager.EXPIRE_NEVER, expireTime: 1000,
michael@0 77 appId: 0, isInBrowserElement: 0 },
michael@0 78
michael@0 79 // ExpireTime is negative.
michael@0 80 { host: '42', type: '13', permission: 1,
michael@0 81 expireType: Ci.nsIPermissionManager.EXPIRE_TIME, expireTime: -1,
michael@0 82 appId: 0, isInBrowserElement: 0 },
michael@0 83
michael@0 84 // AppId is negative.
michael@0 85 { host: '42', type: '14', permission: 1, expireType: 0, expireTime: 0,
michael@0 86 appId: -1, isInBrowserElement: 0 },
michael@0 87
michael@0 88 // IsInBrowserElement is negative or higher than 1.
michael@0 89 { host: '42', type: '15', permission: 1, expireType: 0, expireTime: 0,
michael@0 90 appId: 0, isInBrowserElement: -1 },
michael@0 91 { host: '42', type: '16', permission: 1, expireType: 0, expireTime: 0,
michael@0 92 appId: 0, isInBrowserElement: 10 },
michael@0 93
michael@0 94 // This insertion should be the last one. It is used to make sure we always
michael@0 95 // load it regardless of the previous entries validities.
michael@0 96 { host: 'example.org', type: 'test-load-invalid-entries',
michael@0 97 permission: Ci.nsIPermissionManager.ALLOW_ACTION, expireType: 0,
michael@0 98 expireTime: 0, appId: 0, isInBrowserElement: 0 },
michael@0 99 ];
michael@0 100
michael@0 101 for (var i=0; i<garbadge.length; ++i) {
michael@0 102 if (DEBUG_TEST) {
michael@0 103 dump("\n value #" + i + "\n\n");
michael@0 104 }
michael@0 105 var data = garbadge[i];
michael@0 106 connection.executeSimpleSQL(
michael@0 107 "INSERT INTO moz_hosts " +
michael@0 108 " (id, host, type, permission, expireType, expireTime, appId, isInBrowserElement) " +
michael@0 109 "VALUES (" + i + ", '" + data.host + "', '" + data.type + "', "
michael@0 110 + data.permission + ", " + data.expireType + ", "
michael@0 111 + data.expireTime + ", " + data.appId + ", "
michael@0 112 + data.isInBrowserElement + ")"
michael@0 113 );
michael@0 114 }
michael@0 115
michael@0 116 // Initialize the permission manager service
michael@0 117 var pm = Cc["@mozilla.org/permissionmanager;1"]
michael@0 118 .getService(Ci.nsIPermissionManager);
michael@0 119
michael@0 120 // The schema should still be 3. We want this test to be updated for each
michael@0 121 // schema update.
michael@0 122 do_check_eq(connection.schemaVersion, 3);
michael@0 123
michael@0 124 // This permission should always be there.
michael@0 125 let principal = Cc["@mozilla.org/scriptsecuritymanager;1"]
michael@0 126 .getService(Ci.nsIScriptSecurityManager)
michael@0 127 .getNoAppCodebasePrincipal(NetUtil.newURI("http://example.org"));
michael@0 128 do_check_eq(pm.testPermissionFromPrincipal(principal, 'test-load-invalid-entries'), Ci.nsIPermissionManager.ALLOW_ACTION);
michael@0 129 }

mercurial