tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_SpecialPowersPushPermissions.html (10013B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Test for SpecialPowers extension</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      7 </head>
      8 <body onload="starttest();">
      9 
     10 <pre id="test">
     11 <script class="testbody" type="text/javascript">
     12 const ALLOW_ACTION = SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION;
     13 const DENY_ACTION = SpecialPowers.Ci.nsIPermissionManager.DENY_ACTION;
     14 const UNKNOWN_ACTION = SpecialPowers.Ci.nsIPermissionManager.UNKNOWN_ACTION;
     15 const PROMPT_ACTION = SpecialPowers.Ci.nsIPermissionManager.PROMPT_ACTION;
     16 const ACCESS_SESSION = SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION;
     17 
     18 const EXPIRE_TIME = SpecialPowers.Ci.nsIPermissionManager.EXPIRE_TIME;
     19 // expire Setting:
     20 //     start                 expire time point
     21 //   ----|------------------------|-----------
     22 //       <------------------------>
     23 //                 PERIOD
     24 var start;
     25 // PR_Now() that called in PermissionManager to get the system time
     26 // is sometimes 100ms~600s more than Date.now() on Android 4.3 API11.
     27 // Thus, the PERIOD should be larger than 600ms in this test.
     28 const PERIOD = 900;
     29 var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('specialPowers_framescript.js'));
     30 SimpleTest.requestFlakyTimeout("untriaged");
     31 
     32 function starttest(){
     33  SpecialPowers.addPermission("pPROMPT", PROMPT_ACTION, document);
     34  SpecialPowers.addPermission("pALLOW", ALLOW_ACTION, document);
     35  SpecialPowers.addPermission("pDENY", DENY_ACTION, document);
     36  SpecialPowers.addPermission("pREMOVE", ALLOW_ACTION, document);
     37  SpecialPowers.addPermission("pSESSION", ACCESS_SESSION, document);
     38 
     39  setTimeout(test1, 0);
     40 }
     41 
     42 SimpleTest.waitForExplicitFinish();
     43 
     44 async function test1() {
     45  if (!await SpecialPowers.testPermission('pALLOW', ALLOW_ACTION, document)) {
     46    dump('/**** allow not set ****/\n');
     47    setTimeout(test1, 0);
     48  } else if (!await SpecialPowers.testPermission('pDENY', DENY_ACTION, document)) {
     49    dump('/**** deny not set ****/\n');
     50    setTimeout(test1, 0);
     51  } else if (!await SpecialPowers.testPermission('pPROMPT', PROMPT_ACTION, document)) {
     52    dump('/**** prompt not set ****/\n');
     53    setTimeout(test1, 0);
     54  } else if (!await SpecialPowers.testPermission('pREMOVE', ALLOW_ACTION, document)) {
     55    dump('/**** remove not set ****/\n');
     56    setTimeout(test1, 0);
     57  } else if (!await SpecialPowers.testPermission('pSESSION', ACCESS_SESSION, document)) {
     58    dump('/**** ACCESS_SESSION not set ****/\n');
     59    setTimeout(test1, 0);
     60  } else {
     61    test2();
     62  }
     63 }
     64 
     65 async function test2() {
     66  ok(await SpecialPowers.testPermission('pUNKNOWN', UNKNOWN_ACTION, document), 'pUNKNOWN value should have UNKOWN permission');
     67  SpecialPowers.pushPermissions([
     68    {'type': 'pUNKNOWN', 'allow': true, 'context': document},
     69    {'type': 'pALLOW', 'allow': false, 'context': document},
     70    {'type': 'pDENY', 'allow': true, 'context': document},
     71    {'type': 'pPROMPT', 'allow': true, 'context': document},
     72    {'type': 'pSESSION', 'allow': true, 'context': document},
     73    {'type': 'pREMOVE', 'remove': true, 'context': document},
     74  ], test3);
     75 }
     76 
     77 async function test3() {
     78  ok(await SpecialPowers.testPermission('pUNKNOWN', ALLOW_ACTION, document), 'pUNKNOWN value should have ALLOW permission');
     79  ok(await SpecialPowers.testPermission('pPROMPT', ALLOW_ACTION, document), 'pPROMPT value should have ALLOW permission');
     80  ok(await SpecialPowers.testPermission('pALLOW', DENY_ACTION, document), 'pALLOW should have DENY permission');
     81  ok(await SpecialPowers.testPermission('pDENY', ALLOW_ACTION, document), 'pDENY should have ALLOW permission');
     82  ok(await SpecialPowers.testPermission('pREMOVE', UNKNOWN_ACTION, document), 'pREMOVE should have REMOVE permission');
     83  ok(await SpecialPowers.testPermission('pSESSION', ALLOW_ACTION, document), 'pSESSION should have ALLOW permission');
     84 
     85  // only pPROMPT (last one) is different, the other stuff is just to see if it doesn't cause test failures
     86  SpecialPowers.pushPermissions([
     87    {'type': 'pUNKNOWN', 'allow': true, 'context': document},
     88    {'type': 'pALLOW', 'allow': false, 'context': document},
     89    {'type': 'pDENY', 'allow': true, 'context': document},
     90    {'type': 'pPROMPT', 'allow': false, 'context': document},
     91    {'type': 'pREMOVE', 'remove': true, 'context': document},
     92  ], test3b);
     93 }
     94 
     95 async function test3b() {
     96  ok(await SpecialPowers.testPermission('pPROMPT', DENY_ACTION, document), 'pPROMPT value should have DENY permission');
     97  SpecialPowers.pushPermissions([
     98    {'type': 'pUNKNOWN', 'allow': DENY_ACTION, 'context': document},
     99    {'type': 'pALLOW', 'allow': PROMPT_ACTION, 'context': document},
    100    {'type': 'pDENY', 'allow': PROMPT_ACTION, 'context': document},
    101    {'type': 'pPROMPT', 'allow': ALLOW_ACTION, 'context': document},
    102  ], test4);
    103 }
    104 
    105 async function test4() {
    106  ok(await SpecialPowers.testPermission('pUNKNOWN', DENY_ACTION, document), 'pUNKNOWN value should have DENY permission');
    107  ok(await SpecialPowers.testPermission('pPROMPT', ALLOW_ACTION, document), 'pPROMPT value should have ALLOW permission');
    108  ok(await SpecialPowers.testPermission('pALLOW', PROMPT_ACTION, document), 'pALLOW should have PROMPT permission');
    109  ok(await SpecialPowers.testPermission('pDENY', PROMPT_ACTION, document), 'pDENY should have PROMPT permission');
    110  //this should reset all the permissions to before all the pushPermissions calls
    111  SpecialPowers.flushPermissions(test5);
    112 }
    113 
    114 async function test5() {
    115  ok(await SpecialPowers.testPermission('pUNKNOWN', UNKNOWN_ACTION, document), 'pUNKNOWN should have UNKNOWN permission');
    116  ok(await SpecialPowers.testPermission('pALLOW', ALLOW_ACTION, document), 'pALLOW should have ALLOW permission');
    117  ok(await SpecialPowers.testPermission('pDENY', DENY_ACTION, document), 'pDENY should have DENY permission');
    118  ok(await SpecialPowers.testPermission('pPROMPT', PROMPT_ACTION, document), 'pPROMPT should have PROMPT permission');
    119  ok(await SpecialPowers.testPermission('pREMOVE', ALLOW_ACTION, document), 'pREMOVE should have ALLOW permission');
    120  ok(await SpecialPowers.testPermission('pSESSION', ACCESS_SESSION, document), 'pSESSION should have ACCESS_SESSION permission');
    121 
    122  SpecialPowers.removePermission("pPROMPT", document);
    123  SpecialPowers.removePermission("pALLOW", document);
    124  SpecialPowers.removePermission("pDENY", document);
    125  SpecialPowers.removePermission("pREMOVE", document);
    126  SpecialPowers.removePermission("pSESSION", document);
    127 
    128  setTimeout(test6, 0);
    129 }
    130 
    131 async function test6() {
    132  if (!await SpecialPowers.testPermission('pALLOW', UNKNOWN_ACTION, document)) {
    133    dump('/**** allow still set ****/\n');
    134    setTimeout(test6, 0);
    135  } else if (!await SpecialPowers.testPermission('pDENY', UNKNOWN_ACTION, document)) {
    136    dump('/**** deny still set ****/\n');
    137    setTimeout(test6, 0);
    138  } else if (!await SpecialPowers.testPermission('pPROMPT', UNKNOWN_ACTION, document)) {
    139    dump('/**** prompt still set ****/\n');
    140    setTimeout(test6, 0);
    141  } else if (!await SpecialPowers.testPermission('pREMOVE', UNKNOWN_ACTION, document)) {
    142    dump('/**** remove still set ****/\n');
    143    setTimeout(test6, 0);
    144  } else if (!await SpecialPowers.testPermission('pSESSION', UNKNOWN_ACTION, document)) {
    145    dump('/**** pSESSION still set ****/\n');
    146    setTimeout(test6, 0);
    147  } else {
    148    test7();
    149  }
    150 }
    151 
    152 function test7() {
    153  afterPermissionChanged('pEXPIRE', 'deleted', test8);
    154  afterPermissionChanged('pEXPIRE', 'added', permissionPollingCheck);
    155  start = Number(Date.now());
    156  SpecialPowers.addPermission('pEXPIRE',
    157                              true,
    158                              document,
    159                              EXPIRE_TIME,
    160                              (start + PERIOD + getPlatformInfo().timeCompensation));
    161 }
    162 
    163 function test8() {
    164  afterPermissionChanged('pEXPIRE', 'deleted', SimpleTest.finish);
    165  afterPermissionChanged('pEXPIRE', 'added', permissionPollingCheck);
    166  start = Number(Date.now());
    167  SpecialPowers.pushPermissions([
    168    { 'type': 'pEXPIRE',
    169      'allow': true,
    170      'expireType': EXPIRE_TIME,
    171      'expireTime': (start + PERIOD + getPlatformInfo().timeCompensation),
    172      'context': document
    173    }], function() {
    174      info("Wait for permission-changed signal!");
    175    }
    176  );
    177 }
    178 
    179 function afterPermissionChanged(type, op, callback) {
    180  // handle the message from specialPowers_framescript.js
    181  gScript.addMessageListener('perm-changed', function onChange(msg) {
    182    if (msg.type == type && msg.op == op) {
    183      gScript.removeMessageListener('perm-changed', onChange);
    184      callback();
    185    }
    186  });
    187 }
    188 
    189 async function permissionPollingCheck() {
    190  var now = Number(Date.now());
    191  if (now < (start + PERIOD)) {
    192    if (await SpecialPowers.testPermission('pEXPIRE', ALLOW_ACTION, document)) {
    193      // To make sure that permission will be expired in next round,
    194      // the next permissionPollingCheck calling will be fired 100ms later after
    195      // permission is out-of-period.
    196      setTimeout(permissionPollingCheck, PERIOD + 100);
    197      return;
    198    }
    199 
    200    errorHandler('unexpired permission should be allowed!');
    201  }
    202 
    203  // The permission is already expired!
    204  if (await SpecialPowers.testPermission('pEXPIRE', ALLOW_ACTION, document)) {
    205    errorHandler('expired permission should be removed!');
    206  }
    207 }
    208 
    209 function getPlatformInfo() {
    210  var version = SpecialPowers.Services.sysinfo.getProperty('version');
    211  version = parseFloat(version);
    212 
    213  // PR_Now() that called in PermissionManager to get the system time and
    214  // Date.now() are out of sync on win32 platform(XP/win7). The PR_Now() is
    215  // 15~20ms less than Date.now(). Unfortunately, this time skew can't be
    216  // avoided, so it needs to add a time buffer to compensate.
    217  // Version 5.1 is win XP, 6.1 is win7
    218  if (navigator.platform.startsWith('Win32') && (version <= 6.1)) {
    219    return { platform: "Win32", timeCompensation: -100 };
    220  }
    221 
    222  return { platform: "NoMatter", timeCompensation: 0 };
    223 }
    224 
    225 function errorHandler(msg) {
    226  ok(false, msg);
    227  SimpleTest.finish();
    228 }
    229 </script>
    230 </pre>
    231 </body>
    232 </html>