tor-browser

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

test_local_network_access.js (14309B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function test_local_network_access_enabled() {
      7  // Test enabling LocalNetworkAccess policy (unlocked)
      8  await setupPolicyEngineWithJson({
      9    policies: {
     10      LocalNetworkAccess: {
     11        Enabled: true,
     12      },
     13    },
     14  });
     15 
     16  // Verify the preference is set correctly and unlocked
     17  equal(
     18    Services.prefs.getBoolPref("network.lna.enabled"),
     19    true,
     20    "network.lna.enabled should be true when LocalNetworkAccess is enabled"
     21  );
     22  equal(
     23    Services.prefs.getBoolPref("network.lna.block_trackers"),
     24    true,
     25    "network.lna.block_trackers should be true when LocalNetworkAccess is enabled"
     26  );
     27  equal(
     28    Services.prefs.getBoolPref("network.lna.blocking"),
     29    true,
     30    "network.lna.blocking should be true when LocalNetworkAccess is enabled"
     31  );
     32 
     33  equal(
     34    Services.prefs.prefIsLocked("network.lna.enabled"),
     35    false,
     36    "network.lna.enabled should not be locked when Locked is not specified"
     37  );
     38  equal(
     39    Services.prefs.prefIsLocked("network.lna.block_trackers"),
     40    false,
     41    "network.lna.block_trackers should not be locked when Locked is not specified"
     42  );
     43  equal(
     44    Services.prefs.prefIsLocked("network.lna.blocking"),
     45    false,
     46    "network.lna.blocking should not be locked when Locked is not specified"
     47  );
     48 });
     49 
     50 add_task(async function test_local_network_access_disabled() {
     51  // Test disabling LocalNetworkAccess policy (unlocked)
     52  await setupPolicyEngineWithJson({
     53    policies: {
     54      LocalNetworkAccess: {
     55        Enabled: false,
     56      },
     57    },
     58  });
     59 
     60  // Verify the preference is set correctly and unlocked
     61  equal(
     62    Services.prefs.getBoolPref("network.lna.enabled"),
     63    false,
     64    "network.lna.enabled should be false when LocalNetworkAccess is disabled"
     65  );
     66  equal(
     67    Services.prefs.getBoolPref("network.lna.block_trackers"),
     68    false,
     69    "network.lna.block_trackers should be false when LocalNetworkAccess is disabled"
     70  );
     71  equal(
     72    Services.prefs.getBoolPref("network.lna.blocking"),
     73    false,
     74    "network.lna.blocking should be false when LocalNetworkAccess is disabled"
     75  );
     76 
     77  equal(
     78    Services.prefs.prefIsLocked("network.lna.enabled"),
     79    false,
     80    "network.lna.enabled should not be locked when Locked is not specified"
     81  );
     82  equal(
     83    Services.prefs.prefIsLocked("network.lna.block_trackers"),
     84    false,
     85    "network.lna.block_trackers should not be locked when Locked is not specified"
     86  );
     87  equal(
     88    Services.prefs.prefIsLocked("network.lna.blocking"),
     89    false,
     90    "network.lna.blocking should not be locked when Locked is not specified"
     91  );
     92 });
     93 
     94 add_task(async function test_local_network_access_enabled_locked() {
     95  // Test enabling LocalNetworkAccess policy with locking
     96  await setupPolicyEngineWithJson({
     97    policies: {
     98      LocalNetworkAccess: {
     99        Enabled: true,
    100        Locked: true,
    101      },
    102    },
    103  });
    104 
    105  // Verify the preference is set correctly and locked
    106  equal(
    107    Services.prefs.getBoolPref("network.lna.enabled"),
    108    true,
    109    "network.lna.enabled should be true when LocalNetworkAccess is enabled"
    110  );
    111  equal(
    112    Services.prefs.getBoolPref("network.lna.block_trackers"),
    113    true,
    114    "network.lna.block_trackers should be true when LocalNetworkAccess is enabled"
    115  );
    116  equal(
    117    Services.prefs.getBoolPref("network.lna.blocking"),
    118    true,
    119    "network.lna.blocking should be true when LocalNetworkAccess is enabled"
    120  );
    121 
    122  equal(
    123    Services.prefs.prefIsLocked("network.lna.enabled"),
    124    true,
    125    "network.lna.enabled should be locked when Locked: true is specified"
    126  );
    127  equal(
    128    Services.prefs.prefIsLocked("network.lna.block_trackers"),
    129    true,
    130    "network.lna.block_trackers should be locked when Locked: true is specified"
    131  );
    132  equal(
    133    Services.prefs.prefIsLocked("network.lna.blocking"),
    134    true,
    135    "network.lna.blocking should be locked when Locked: true is specified"
    136  );
    137 });
    138 
    139 add_task(async function test_local_network_access_disabled_locked() {
    140  // Test disabling LocalNetworkAccess policy with locking
    141  await setupPolicyEngineWithJson({
    142    policies: {
    143      LocalNetworkAccess: {
    144        Enabled: false,
    145        Locked: true,
    146      },
    147    },
    148  });
    149 
    150  // Verify the preference is set correctly and locked
    151  equal(
    152    Services.prefs.getBoolPref("network.lna.enabled"),
    153    false,
    154    "network.lna.enabled should be false when LocalNetworkAccess is disabled"
    155  );
    156  equal(
    157    Services.prefs.getBoolPref("network.lna.block_trackers"),
    158    false,
    159    "network.lna.block_trackers should be false when LocalNetworkAccess is disabled"
    160  );
    161  equal(
    162    Services.prefs.getBoolPref("network.lna.blocking"),
    163    false,
    164    "network.lna.blocking should be false when LocalNetworkAccess is disabled"
    165  );
    166 
    167  equal(
    168    Services.prefs.prefIsLocked("network.lna.enabled"),
    169    true,
    170    "network.lna.enabled should be locked when Locked: true is specified"
    171  );
    172  equal(
    173    Services.prefs.prefIsLocked("network.lna.block_trackers"),
    174    true,
    175    "network.lna.block_trackers should be locked when Locked: true is specified"
    176  );
    177  equal(
    178    Services.prefs.prefIsLocked("network.lna.blocking"),
    179    true,
    180    "network.lna.blocking should be locked when Locked: true is specified"
    181  );
    182 
    183  // remove the pref locks
    184  await setupPolicyEngineWithJson({
    185    policies: {
    186      LocalNetworkAccess: {
    187        Enabled: false,
    188        Locked: false,
    189      },
    190    },
    191  });
    192 });
    193 
    194 add_task(
    195  async function test_local_network_access_fine_grained_block_trackers_only() {
    196    // Test enabling LNA with only tracker blocking, no prompting
    197    await setupPolicyEngineWithJson({
    198      policies: {
    199        LocalNetworkAccess: {
    200          Enabled: true,
    201          BlockTrackers: true,
    202          EnablePrompting: false,
    203        },
    204      },
    205    });
    206 
    207    equal(
    208      Services.prefs.getBoolPref("network.lna.enabled"),
    209      true,
    210      "network.lna.enabled should be true"
    211    );
    212    equal(
    213      Services.prefs.getBoolPref("network.lna.block_trackers"),
    214      true,
    215      "network.lna.block_trackers should be true when BlockTrackers is true"
    216    );
    217    equal(
    218      Services.prefs.getBoolPref("network.lna.blocking"),
    219      false,
    220      "network.lna.blocking should be false when EnablePrompting is false"
    221    );
    222  }
    223 );
    224 
    225 add_task(
    226  async function test_local_network_access_fine_grained_prompting_only() {
    227    // Test enabling LNA with only prompting, no tracker blocking
    228    await setupPolicyEngineWithJson({
    229      policies: {
    230        LocalNetworkAccess: {
    231          Enabled: true,
    232          BlockTrackers: false,
    233          EnablePrompting: true,
    234        },
    235      },
    236    });
    237 
    238    equal(
    239      Services.prefs.getBoolPref("network.lna.enabled"),
    240      true,
    241      "network.lna.enabled should be true"
    242    );
    243    equal(
    244      Services.prefs.getBoolPref("network.lna.block_trackers"),
    245      false,
    246      "network.lna.block_trackers should be false when BlockTrackers is false"
    247    );
    248    equal(
    249      Services.prefs.getBoolPref("network.lna.blocking"),
    250      true,
    251      "network.lna.blocking should be true when EnablePrompting is true"
    252    );
    253  }
    254 );
    255 
    256 add_task(
    257  async function test_local_network_access_enabled_only_backward_compatibility() {
    258    // Test backward compatibility: Enabled: true defaults fine-grained controls to true
    259    await setupPolicyEngineWithJson({
    260      policies: {
    261        LocalNetworkAccess: {
    262          Enabled: true,
    263        },
    264      },
    265    });
    266 
    267    equal(
    268      Services.prefs.getBoolPref("network.lna.enabled"),
    269      true,
    270      "network.lna.enabled should be true"
    271    );
    272    equal(
    273      Services.prefs.getBoolPref("network.lna.block_trackers"),
    274      true,
    275      "network.lna.block_trackers should default to true for backward compatibility"
    276    );
    277    equal(
    278      Services.prefs.getBoolPref("network.lna.blocking"),
    279      true,
    280      "network.lna.blocking should default to true for backward compatibility"
    281    );
    282  }
    283 );
    284 
    285 add_task(
    286  async function test_local_network_access_disabled_overrides_fine_grained() {
    287    // Test that Enabled: false overrides fine-grained settings
    288    await setupPolicyEngineWithJson({
    289      policies: {
    290        LocalNetworkAccess: {
    291          Enabled: false,
    292          BlockTrackers: true, // This should be ignored
    293          EnablePrompting: true, // This should be ignored
    294        },
    295      },
    296    });
    297 
    298    equal(
    299      Services.prefs.getBoolPref("network.lna.enabled"),
    300      false,
    301      "network.lna.enabled should be false when Enabled is false"
    302    );
    303    equal(
    304      Services.prefs.getBoolPref("network.lna.block_trackers"),
    305      false,
    306      "network.lna.block_trackers should be false when LNA is disabled, regardless of BlockTrackers setting"
    307    );
    308    equal(
    309      Services.prefs.getBoolPref("network.lna.blocking"),
    310      false,
    311      "network.lna.blocking should be false when LNA is disabled, regardless of EnablePrompting setting"
    312    );
    313  }
    314 );
    315 
    316 add_task(
    317  async function test_local_network_access_no_enabled_field_no_modification() {
    318    // Test that omitting "Enabled" field doesn't modify any prefs
    319    Services.prefs.setBoolPref("network.lna.enabled", false);
    320    Services.prefs.setBoolPref("network.lna.block_trackers", false);
    321    Services.prefs.setBoolPref("network.lna.blocking", true);
    322 
    323    // Apply policy with LocalNetworkAccess but no "Enabled" field
    324    await setupPolicyEngineWithJson({
    325      policies: {
    326        LocalNetworkAccess: {
    327          BlockTrackers: true, // Should be ignored
    328          EnablePrompting: false, // Should be ignored
    329        },
    330      },
    331    });
    332 
    333    // All prefs should remain at their original values
    334    equal(
    335      Services.prefs.getBoolPref("network.lna.enabled"),
    336      false,
    337      "network.lna.enabled should remain unchanged when Enabled is not specified"
    338    );
    339    equal(
    340      Services.prefs.getBoolPref("network.lna.block_trackers"),
    341      false,
    342      "network.lna.block_trackers should remain unchanged when Enabled is not specified"
    343    );
    344    equal(
    345      Services.prefs.getBoolPref("network.lna.blocking"),
    346      true,
    347      "network.lna.blocking should remain unchanged when Enabled is not specified"
    348    );
    349  }
    350 );
    351 
    352 add_task(async function test_local_network_access_fine_grained_locked() {
    353  // Test that fine-grained controls respect locking
    354  await setupPolicyEngineWithJson({
    355    policies: {
    356      LocalNetworkAccess: {
    357        Enabled: true,
    358        BlockTrackers: false,
    359        EnablePrompting: true,
    360        Locked: true,
    361      },
    362    },
    363  });
    364 
    365  equal(Services.prefs.getBoolPref("network.lna.enabled"), true);
    366  equal(Services.prefs.getBoolPref("network.lna.block_trackers"), false);
    367  equal(Services.prefs.getBoolPref("network.lna.blocking"), true);
    368 
    369  // Verify all prefs are locked
    370  equal(
    371    Services.prefs.prefIsLocked("network.lna.enabled"),
    372    true,
    373    "network.lna.enabled should be locked"
    374  );
    375  equal(
    376    Services.prefs.prefIsLocked("network.lna.block_trackers"),
    377    true,
    378    "network.lna.block_trackers should be locked"
    379  );
    380  equal(
    381    Services.prefs.prefIsLocked("network.lna.blocking"),
    382    true,
    383    "network.lna.blocking should be locked"
    384  );
    385 });
    386 
    387 add_task(async function test_local_network_access_policy_enforcement() {
    388  // Test that locked policy cannot be changed
    389  await setupPolicyEngineWithJson({
    390    policies: {
    391      LocalNetworkAccess: {
    392        Enabled: false,
    393        Locked: true,
    394      },
    395    },
    396  });
    397 
    398  // Verify initial state
    399  equal(Services.prefs.getBoolPref("network.lna.enabled"), false);
    400  equal(Services.prefs.prefIsLocked("network.lna.enabled"), true);
    401 
    402  // Try to change the preference - this should be ignored due to locking
    403  // In Firefox, setting a locked preference doesn't throw, it just gets ignored
    404  Services.prefs.setBoolPref("network.lna.enabled", true);
    405 
    406  // Verify the preference remains unchanged (setting was ignored)
    407  equal(
    408    Services.prefs.getBoolPref("network.lna.enabled"),
    409    false,
    410    "Locked preference should remain unchanged after attempted modification"
    411  );
    412 
    413  // Verify it's still locked
    414  equal(
    415    Services.prefs.prefIsLocked("network.lna.enabled"),
    416    true,
    417    "Preference should still be locked"
    418  );
    419 });
    420 
    421 add_task(async function test_local_network_access_skip_domains() {
    422  // Test SkipDomains policy
    423  await setupPolicyEngineWithJson({
    424    policies: {
    425      LocalNetworkAccess: {
    426        SkipDomains: ["example.com", "*.local", "localhost"],
    427      },
    428    },
    429  });
    430 
    431  equal(
    432    Services.prefs.getCharPref("network.lna.skip-domains"),
    433    "example.com,*.local,localhost",
    434    "network.lna.skip-domains should be set correctly"
    435  );
    436 
    437  equal(
    438    Services.prefs.prefIsLocked("network.lna.skip-domains"),
    439    false,
    440    "network.lna.skip-domains should not be locked when Locked is not specified"
    441  );
    442 });
    443 
    444 add_task(async function test_local_network_access_skip_domains_locked() {
    445  // Test SkipDomains policy with locking
    446  await setupPolicyEngineWithJson({
    447    policies: {
    448      LocalNetworkAccess: {
    449        SkipDomains: ["*.example.com", "server.local"],
    450        Locked: true,
    451      },
    452    },
    453  });
    454 
    455  equal(
    456    Services.prefs.getCharPref("network.lna.skip-domains"),
    457    "*.example.com,server.local",
    458    "network.lna.skip-domains should be set correctly"
    459  );
    460 
    461  equal(
    462    Services.prefs.prefIsLocked("network.lna.skip-domains"),
    463    true,
    464    "network.lna.skip-domains should be locked when Locked: true is specified"
    465  );
    466 });
    467 
    468 add_task(async function test_local_network_access_enabled_with_skip_domains() {
    469  // Test combining Enabled with SkipDomains
    470  await setupPolicyEngineWithJson({
    471    policies: {
    472      LocalNetworkAccess: {
    473        Enabled: true,
    474        SkipDomains: ["*"],
    475      },
    476    },
    477  });
    478 
    479  equal(
    480    Services.prefs.getBoolPref("network.lna.enabled"),
    481    true,
    482    "network.lna.enabled should be true"
    483  );
    484  equal(
    485    Services.prefs.getCharPref("network.lna.skip-domains"),
    486    "*",
    487    'network.lna.skip-domains should be "*" to skip all domains'
    488  );
    489 });
    490 
    491 add_task(async function test_local_network_access_skip_domains_empty_array() {
    492  // Test SkipDomains with empty array
    493  await setupPolicyEngineWithJson({
    494    policies: {
    495      LocalNetworkAccess: {
    496        SkipDomains: [],
    497      },
    498    },
    499  });
    500 
    501  equal(
    502    Services.prefs.getCharPref("network.lna.skip-domains"),
    503    "",
    504    "network.lna.skip-domains should be empty string for empty array"
    505  );
    506 });