test_udpsocket_offline.js (3415B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ */ 4 5 "use strict"; 6 7 add_test(function test_ipv4_any() { 8 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance( 9 Ci.nsIUDPSocket 10 ); 11 12 Assert.throws(() => { 13 socket.init(-1, false, Services.scriptSecurityManager.getSystemPrincipal()); 14 }, /NS_ERROR_OFFLINE/); 15 16 run_next_test(); 17 }); 18 19 add_test(function test_ipv6_any() { 20 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance( 21 Ci.nsIUDPSocket 22 ); 23 24 Assert.throws(() => { 25 socket.init2("::", -1, Services.scriptSecurityManager.getSystemPrincipal()); 26 }, /NS_ERROR_OFFLINE/); 27 28 run_next_test(); 29 }); 30 31 add_test(function test_ipv4() { 32 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance( 33 Ci.nsIUDPSocket 34 ); 35 36 Assert.throws(() => { 37 socket.init2( 38 "240.0.0.1", 39 -1, 40 Services.scriptSecurityManager.getSystemPrincipal() 41 ); 42 }, /NS_ERROR_OFFLINE/); 43 44 run_next_test(); 45 }); 46 47 add_test(function test_ipv6() { 48 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance( 49 Ci.nsIUDPSocket 50 ); 51 52 Assert.throws(() => { 53 socket.init2( 54 "2001:db8::1", 55 -1, 56 Services.scriptSecurityManager.getSystemPrincipal() 57 ); 58 }, /NS_ERROR_OFFLINE/); 59 60 run_next_test(); 61 }); 62 63 add_test(function test_ipv4_loopback() { 64 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance( 65 Ci.nsIUDPSocket 66 ); 67 68 try { 69 socket.init2( 70 "127.0.0.1", 71 -1, 72 Services.scriptSecurityManager.getSystemPrincipal(), 73 true 74 ); 75 } catch (e) { 76 Assert.ok(false, "unexpected exception: " + e); 77 } 78 79 // Now with localhost connections disabled in offline mode. 80 Services.prefs.setBoolPref("network.disable-localhost-when-offline", true); 81 socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance( 82 Ci.nsIUDPSocket 83 ); 84 85 Assert.throws(() => { 86 socket.init2( 87 "127.0.0.1", 88 -1, 89 Services.scriptSecurityManager.getSystemPrincipal(), 90 true 91 ); 92 }, /NS_ERROR_OFFLINE/); 93 94 Services.prefs.setBoolPref("network.disable-localhost-when-offline", false); 95 96 run_next_test(); 97 }); 98 99 add_test(function test_ipv6_loopback() { 100 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance( 101 Ci.nsIUDPSocket 102 ); 103 104 try { 105 socket.init2( 106 "::1", 107 -1, 108 Services.scriptSecurityManager.getSystemPrincipal(), 109 true 110 ); 111 } catch (e) { 112 Assert.ok(false, "unexpected exception: " + e); 113 } 114 115 // Now with localhost connections disabled in offline mode. 116 Services.prefs.setBoolPref("network.disable-localhost-when-offline", true); 117 socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance( 118 Ci.nsIUDPSocket 119 ); 120 121 Assert.throws(() => { 122 socket.init2( 123 "::1", 124 -1, 125 Services.scriptSecurityManager.getSystemPrincipal(), 126 true 127 ); 128 }, /NS_ERROR_OFFLINE/); 129 130 Services.prefs.setBoolPref("network.disable-localhost-when-offline", false); 131 132 run_next_test(); 133 }); 134 135 function run_test() { 136 // jshint ignore:line 137 Services.io.offline = true; 138 Services.prefs.setBoolPref("network.disable-localhost-when-offline", false); 139 registerCleanupFunction(() => { 140 Services.io.offline = false; 141 Services.prefs.clearUserPref("network.disable-localhost-when-offline"); 142 }); 143 run_next_test(); 144 }