Bug 1326483 - Part 2, test cases. r=drno draft
authorShih-Chiang Chien <schien@mozilla.com>
Thu, 05 Jan 2017 14:42:47 +0800
changeset 459487 e78bc411625cfe46980e45091a7a2a8c0191eae7
parent 459486 257b80aaf7af653ef0adbc2f92e64607527c7121
child 541906 8199437ee4704900953949d5e884070cd88b5eca
push id41232
push userschien@mozilla.com
push dateThu, 12 Jan 2017 01:41:37 +0000
reviewersdrno
bugs1326483
milestone53.0a1
Bug 1326483 - Part 2, test cases. r=drno MozReview-Commit-ID: IH9nT0LNS7S
netwerk/test/unit/test_udpsocket_offline.js
netwerk/test/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/netwerk/test/unit/test_udpsocket_offline.js
@@ -0,0 +1,81 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+/* jshint esnext:true, globalstrict:true, moz:true, undef:true, unused:true */
+/* globals Cc, Cu, Ci, Assert, run_next_test, add_test, do_register_cleanup */
+'use strict';
+
+/* globals Services */
+Cu.import('resource://gre/modules/Services.jsm');
+
+add_test(function test_ipv4_any() {
+  let socket = Cc['@mozilla.org/network/udp-socket;1'].createInstance(Ci.nsIUDPSocket);
+
+  Assert.throws(() => {
+    socket.init(-1, false, Services.scriptSecurityManager.getSystemPrincipal());
+  }, /NS_ERROR_OFFLINE/);
+
+  run_next_test();
+});
+
+add_test(function test_ipv6_any() {
+  let socket = Cc['@mozilla.org/network/udp-socket;1'].createInstance(Ci.nsIUDPSocket);
+
+  Assert.throws(() => {
+    socket.init2('::', -1, Services.scriptSecurityManager.getSystemPrincipal());
+  }, /NS_ERROR_OFFLINE/);
+
+  run_next_test();
+});
+
+add_test(function test_ipv4() {
+  let socket = Cc['@mozilla.org/network/udp-socket;1'].createInstance(Ci.nsIUDPSocket);
+
+  Assert.throws(() => {
+    socket.init2('240.0.0.1', -1, Services.scriptSecurityManager.getSystemPrincipal());
+  }, /NS_ERROR_OFFLINE/);
+
+  run_next_test();
+});
+
+add_test(function test_ipv6() {
+  let socket = Cc['@mozilla.org/network/udp-socket;1'].createInstance(Ci.nsIUDPSocket);
+
+  Assert.throws(() => {
+    socket.init2('2001:db8::1', -1, Services.scriptSecurityManager.getSystemPrincipal());
+  }, /NS_ERROR_OFFLINE/);
+
+  run_next_test();
+});
+
+add_test(function test_ipv4_loopback() {
+  let socket = Cc['@mozilla.org/network/udp-socket;1'].createInstance(Ci.nsIUDPSocket);
+
+  try {
+    socket.init2('127.0.0.1', -1, Services.scriptSecurityManager.getSystemPrincipal(), true);
+  } catch (e) {
+    Assert.ok(false, 'unexpected exception: ' + e);
+  }
+
+  run_next_test();
+});
+
+add_test(function test_ipv6_loopback() {
+  let socket = Cc['@mozilla.org/network/udp-socket;1'].createInstance(Ci.nsIUDPSocket);
+
+  try {
+    socket.init2('::1', -1, Services.scriptSecurityManager.getSystemPrincipal(), true);
+  } catch (e) {
+    Assert.ok(false, 'unexpected exception: ' + e);
+  }
+
+  run_next_test();
+});
+
+function run_test(){ // jshint ignore:line
+  Services.io.offline = true;
+  do_register_cleanup(() => {
+    Services.io.offline = false;
+  });
+  run_next_test();
+}
--- a/netwerk/test/unit/xpcshell.ini
+++ b/netwerk/test/unit/xpcshell.ini
@@ -169,16 +169,17 @@ skip-if = os == "android"
 [test_bug770243.js]
 [test_bug894586.js]
 # Allocating 4GB might actually succeed on 64 bit machines
 skip-if = bits != 32
 [test_bug935499.js]
 [test_bug1064258.js]
 [test_bug1218029.js]
 [test_udpsocket.js]
+[test_udpsocket_offline.js]
 [test_doomentry.js]
 [test_cacheflags.js]
 [test_cache_jar.js]
 [test_channel_close.js]
 [test_compareURIs.js]
 [test_compressappend.js]
 [test_content_encoding_gzip.js]
 [test_content_sniffer.js]