test_dns_originAttributes.js (2576B)
1 "use strict"; 2 3 var prefs = Services.prefs; 4 var mainThread = Services.tm.currentThread; 5 6 var listener1 = { 7 onLookupComplete(inRequest, inRecord, inStatus) { 8 Assert.equal(inStatus, Cr.NS_OK); 9 inRecord.QueryInterface(Ci.nsIDNSAddrRecord); 10 var answer = inRecord.getNextAddrAsString(); 11 Assert.ok(answer == "127.0.0.1" || answer == "::1"); 12 test2(); 13 do_test_finished(); 14 }, 15 }; 16 17 var listener2 = { 18 onLookupComplete(inRequest, inRecord, inStatus) { 19 Assert.equal(inStatus, Cr.NS_OK); 20 inRecord.QueryInterface(Ci.nsIDNSAddrRecord); 21 var answer = inRecord.getNextAddrAsString(); 22 Assert.ok(answer == "127.0.0.1" || answer == "::1"); 23 test3(); 24 do_test_finished(); 25 }, 26 }; 27 28 var listener3 = { 29 onLookupComplete(inRequest, inRecord, inStatus) { 30 Assert.equal(inStatus, Cr.NS_ERROR_OFFLINE); 31 cleanup(); 32 do_test_finished(); 33 }, 34 }; 35 36 const firstOriginAttributes = { userContextId: 1 }; 37 const secondOriginAttributes = { userContextId: 2 }; 38 39 // First, we resolve the address normally for first originAttributes. 40 function run_test() { 41 do_test_pending(); 42 prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true); 43 prefs.setBoolPref( 44 "network.proxy.testing_localhost_is_secure_when_hijacked", 45 false 46 ); 47 48 Services.dns.asyncResolve( 49 "localhost", 50 Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT, 51 0, 52 null, // resolverInfo 53 listener1, 54 mainThread, 55 firstOriginAttributes 56 ); 57 } 58 59 // Second, we resolve the same address offline to see whether its DNS cache works 60 // correctly. 61 function test2() { 62 do_test_pending(); 63 Services.dns.asyncResolve( 64 "localhost", 65 Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT, 66 Ci.nsIDNSService.RESOLVE_OFFLINE, 67 null, // resolverInfo 68 listener2, 69 mainThread, 70 firstOriginAttributes 71 ); 72 } 73 74 // Third, we resolve the same address offline again with different originAttributes. 75 // This resolving should fail since the DNS cache of the given address is not exist 76 // for this originAttributes. 77 function test3() { 78 do_test_pending(); 79 try { 80 Services.dns.asyncResolve( 81 "localhost", 82 Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT, 83 Ci.nsIDNSService.RESOLVE_OFFLINE, 84 null, // resolverInfo 85 listener3, 86 mainThread, 87 secondOriginAttributes 88 ); 89 } catch (e) { 90 Assert.equal(e.result, Cr.NS_ERROR_OFFLINE); 91 cleanup(); 92 do_test_finished(); 93 } 94 } 95 96 function cleanup() { 97 prefs.clearUserPref("network.proxy.allow_hijacking_localhost"); 98 prefs.clearUserPref( 99 "network.proxy.testing_localhost_is_secure_when_hijacked" 100 ); 101 }