tor-browser

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

NDEFReader_make-read-only.https.window.js (6331B)


      1 // META: script=resources/nfc-helpers.js
      2 
      3 // NDEFReader.makeReadOnly method
      4 // https://w3c.github.io/web-nfc/#dom-ndefreader-makereadonly
      5 
      6 'use strict';
      7 
      8 const invalid_signals = ['string', 123, {}, true, Symbol(), () => {}, self];
      9 
     10 nfc_test(async t => {
     11  await test_driver.set_permission({name: 'nfc'}, 'denied');
     12  const ndef = new NDEFReader();
     13  await promise_rejects_dom(t, 'NotAllowedError', ndef.makeReadOnly());
     14 }, 'NDEFReader.makeReadOnly should fail if user permission is not granted.');
     15 
     16 // We do not provide NFC mock here to simulate that there has no available
     17 // implementation for NFC Mojo interface.
     18 nfc_test(async (t, mockNFC) => {
     19  mockNFC.simulateClosedPipe();
     20  const ndef = new NDEFReader();
     21  await promise_rejects_dom(t, 'NotSupportedError', ndef.makeReadOnly());
     22 }, 'NDEFReader.makeReadOnly should fail if no implementation for NFC Mojo interface is available.');
     23 
     24 nfc_test(async (t, mockNFC) => {
     25  const ndef = new NDEFReader();
     26  const controller = new AbortController();
     27 
     28  // Make sure makeReadOnly is pending
     29  mockNFC.setPendingMakeReadOnlyCompleted(false);
     30  const p = ndef.makeReadOnly({signal: controller.signal});
     31  const rejected = promise_rejects_dom(t, 'AbortError', p);
     32  let callback_called = false;
     33  await new Promise(resolve => {
     34    t.step_timeout(() => {
     35      callback_called = true;
     36      controller.abort();
     37      resolve();
     38    }, 10);
     39  });
     40  await rejected;
     41  assert_true(callback_called, 'timeout should have caused the abort');
     42 }, 'NDEFReader.makeReadOnly should fail if request is aborted before makeReadOnly happends.');
     43 
     44 nfc_test(async t => {
     45  const ndef = new NDEFReader();
     46  const controller = new AbortController();
     47  assert_false(controller.signal.aborted);
     48  controller.abort();
     49  assert_true(controller.signal.aborted);
     50  await promise_rejects_dom(
     51      t, 'AbortError', ndef.makeReadOnly({signal: controller.signal}));
     52 }, 'NDEFReader.makeReadOnly should fail if signal is already aborted.');
     53 
     54 nfc_test(async t => {
     55  const ndef = new NDEFReader();
     56  const promises = [];
     57  invalid_signals.forEach(invalid_signal => {
     58    promises.push(promise_rejects_js(
     59        t, TypeError, ndef.makeReadOnly({signal: invalid_signal})));
     60  });
     61  await Promise.all(promises);
     62 }, 'NDEFReader.write should fail if signal is not an AbortSignal.');
     63 
     64 nfc_test(async (t, mockNFC) => {
     65  const ndef1 = new NDEFReader();
     66  const ndef2 = new NDEFReader();
     67  const controller = new AbortController();
     68  const p1 = ndef1.makeReadOnly({signal: controller.signal});
     69 
     70  // Even though makeReadOnly request is grantable,
     71  // this abort should be processed synchronously.
     72  controller.abort();
     73  await promise_rejects_dom(t, 'AbortError', p1);
     74 
     75  await ndef2.makeReadOnly();
     76 }, 'Synchronously signaled abort.');
     77 
     78 nfc_test(async (t, mockNFC) => {
     79  const ndef = new NDEFReader();
     80  mockNFC.setHWStatus(NFCHWStatus.DISABLED);
     81  await promise_rejects_dom(t, 'NotReadableError', ndef.makeReadOnly());
     82 }, 'NDEFReader.makeReadOnly should fail when NFC HW is disabled.');
     83 
     84 nfc_test(async (t, mockNFC) => {
     85  const ndef = new NDEFReader();
     86  mockNFC.setHWStatus(NFCHWStatus.NOT_SUPPORTED);
     87  await promise_rejects_dom(t, 'NotSupportedError', ndef.makeReadOnly());
     88 }, 'NDEFReader.makeReadOnly should fail when NFC HW is not supported.');
     89 
     90 nfc_test(async () => {
     91  await new Promise((resolve, reject) => {
     92    const iframe = document.createElement('iframe');
     93    iframe.srcdoc = `<script>
     94                      window.onmessage = async (message) => {
     95                        if (message.data === "Ready") {
     96                          try {
     97                            const ndef = new NDEFReader();
     98                            await ndef.makeReadOnly();
     99                            parent.postMessage("Failure", "*");
    100                          } catch (error) {
    101                            if (error.name == "InvalidStateError") {
    102                              parent.postMessage("Success", "*");
    103                            } else {
    104                              parent.postMessage("Failure", "*");
    105                            }
    106                          }
    107                        }
    108                      };
    109                    </script>`;
    110    iframe.onload = () => iframe.contentWindow.postMessage('Ready', '*');
    111    document.body.appendChild(iframe);
    112    window.onmessage = message => {
    113      if (message.data == 'Success') {
    114        resolve();
    115      } else if (message.data == 'Failure') {
    116        reject();
    117      }
    118    }
    119  });
    120 }, 'Test that WebNFC API is not accessible from iframe context.');
    121 
    122 nfc_test(async () => {
    123  const ndef = new NDEFReader();
    124  await ndef.makeReadOnly();
    125 }, 'NDEFReader.makeReadOnly should succeed when NFC HW is enabled');
    126 
    127 nfc_test(async (t, mockNFC) => {
    128  const ndef1 = new NDEFReader();
    129  const ndef2 = new NDEFReader();
    130 
    131  // Make sure the first makeReadOnly will be pending.
    132  mockNFC.setPendingMakeReadOnlyCompleted(false);
    133 
    134  const p1 = ndef1.makeReadOnly();
    135  const p2 = ndef2.makeReadOnly();
    136 
    137  await promise_rejects_dom(t, 'AbortError', p1);
    138  await p2;
    139 }, 'NDEFReader.makeReadOnly should replace all previously configured makeReadOnly operations.');
    140 
    141 nfc_test(async () => {
    142  const ndef = new NDEFReader();
    143 
    144  const controller1 = new AbortController();
    145  await ndef.makeReadOnly({signal: controller1.signal});
    146 
    147  const controller2 = new AbortController();
    148  const promise = ndef.makeReadOnly({signal: controller2.signal});
    149  controller1.abort();
    150  await promise;
    151 }, 'NDEFReader.makeReadOnly signals are independent.');
    152 
    153 nfc_test(async (t, mockNFC) => {
    154  // Make sure the makeReadOnly will be pending in the mock.
    155  mockNFC.setPendingMakeReadOnlyCompleted(false);
    156 
    157  const ndef1 = new NDEFReader();
    158  const promise = ndef1.makeReadOnly();
    159 
    160  // Just to make sure the makeReadOnly() request has already reached to the
    161  // mock.
    162  const ndef2 = new NDEFReader();
    163  await ndef2.scan();
    164 
    165  mockNFC.simulateNonNDEFTagDiscovered();
    166  await promise_rejects_dom(t, 'NotSupportedError', promise);
    167 }, 'NDEFReader.makeReadOnly should fail when the NFC device coming up does not expose \
    168 NDEF technology.');
    169 
    170 nfc_test(async (t, mockNFC) => {
    171  const ndef = new NDEFReader();
    172  mockNFC.simulateDataTransferFails();
    173  await promise_rejects_dom(t, 'NetworkError', ndef.makeReadOnly());
    174 }, 'NDEFReader.makeReadOnly should fail with NetworkError when NFC data transfer fails.');