1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Include test fixture.
6 GEN_INCLUDE(['net_internals_test.js']);
12 * Checks the display on the DNS tab against the information it should be
14 * @param {object} hostResolverInfo Results from a host resolver info query.
16 function checkDisplay(hostResolverInfo) {
17 var family = hostResolverInfo.default_address_family;
18 expectEquals(addressFamilyToString(family),
19 $(DnsView.DEFAULT_FAMILY_SPAN_ID).innerText);
20 expectEquals(family == AddressFamily.ADDRESS_FAMILY_IPV4,
21 NetInternalsTest.isDisplayed($(DnsView.IPV6_DISABLED_SPAN_ID)));
23 expectEquals(hostResolverInfo.cache.capacity,
24 parseInt($(DnsView.CAPACITY_SPAN_ID).innerText));
26 var entries = hostResolverInfo.cache.entries;
28 // Don't check exact displayed values, to avoid any races, but make sure
29 // values are non-negative and have the correct sum.
30 var active = parseInt($(DnsView.ACTIVE_SPAN_ID).innerText);
31 var expired = parseInt($(DnsView.EXPIRED_SPAN_ID).innerText);
34 expectEquals(entries.length, active + expired);
36 var tableId = DnsView.CACHE_TBODY_ID;
37 NetInternalsTest.checkTbodyRows(tableId, entries.length);
39 // Rather than check the exact string in every position, just make sure every
40 // entry is not empty, and does not have 'undefined' anywhere, which should
41 // find a fair number of potential output errors, without duplicating the
42 // entire corresponding function of DnsView.
43 for (var row = 0; row < entries.length; ++row) {
44 for (column = 0; column < 4; ++column) {
45 var text = NetInternalsTest.getTbodyText(tableId, row, column);
46 expectNotEquals(text, '');
47 expectFalse(/undefined/i.test(text));
53 * Finds an entry with the specified host name in the |hostResolverInfo| cache,
54 * and returns its index.
55 * @param {object} hostResolverInfo Results to search.
56 * @param {object} hostname The host name to find.
57 * @return {int} Index of the specified host. -1 if not found.
59 function findEntry(hostResolverInfo, hostname) {
60 var entries = hostResolverInfo.cache.entries;
61 for (var i = 0; i < entries.length; ++i) {
62 if (entries[i].hostname == hostname)
69 * A Task that adds a hostname to the cache and waits for it to appear in the
70 * data we receive from the cache.
71 * @param {string} hostname Name of host address we're waiting for.
72 * @param {string} ipAddress IP address we expect it to have. Null if we expect
73 * a net error other than OK.
74 * @param {int} netError The expected network error code.
75 * @param {bool} expired True if we expect the entry to be expired. The added
76 * entry will have an expiration time far enough away from the current time
77 * that there will be no chance of any races.
78 * @extends {NetInternalsTest.Task}
80 function AddCacheEntryTask(hostname, ipAddress, netError, expired) {
81 this.hostname_ = hostname;
82 this.ipAddress_ = ipAddress;
83 this.netError_ = netError;
84 this.expired_ = expired;
85 NetInternalsTest.Task.call(this);
88 AddCacheEntryTask.prototype = {
89 __proto__: NetInternalsTest.Task.prototype,
92 * Adds an entry to the cache and starts waiting to received the results from
93 * the browser process.
96 var addCacheEntryParams = [
100 this.expired_ ? -2 : 2
102 chrome.send('addCacheEntry', addCacheEntryParams);
103 g_browser.addHostResolverInfoObserver(this, false);
107 * Callback from the BrowserBridge. Checks if |hostResolverInfo| has the
108 * DNS entry specified on creation. If so, validates it and completes the
109 * task. If not, continues running.
110 * @param {object} hostResolverInfo Results a host resolver info query.
112 onHostResolverInfoChanged: function(hostResolverInfo) {
113 if (!this.isDone()) {
114 checkDisplay(hostResolverInfo);
116 var index = findEntry(hostResolverInfo, this.hostname_);
118 var entry = hostResolverInfo.cache.entries[index];
119 if (this.netError_) {
120 this.checkError_(entry);
122 this.checkSuccess_(entry);
124 var expirationDate = timeutil.convertTimeTicksToDate(entry.expiration);
125 expectEquals(this.expired_, expirationDate < new Date());
127 // Expect at least one active or expired entry, depending on |expired_|.
128 // To avoid any chance of a race, exact values are not tested.
129 var activeMin = this.expired_ ? 0 : 1;
130 var expiredMin = this.expired_ ? 1 : 0;
131 expectLE(activeMin, parseInt($(DnsView.ACTIVE_SPAN_ID).innerText));
132 expectLE(expiredMin, parseInt($(DnsView.EXPIRED_SPAN_ID).innerText));
134 // Text for the expiration time of the entry should contain 'Expired'
135 // only if |expired_| is true. Only checked for entries we add
136 // ourselves to avoid any expiration time race.
138 NetInternalsTest.getTbodyText(DnsView.CACHE_TBODY_ID, index, 3);
139 expectEquals(this.expired_, /expired/i.test(expirationText));
146 checkError_: function(entry) {
147 expectEquals(this.netError_, entry.error);
150 checkSuccess_: function(entry) {
151 expectEquals(undefined, entry.error);
152 expectEquals(1, entry.addresses.length);
153 expectEquals(0, entry.addresses[0].search(this.ipAddress_));
158 * A Task clears the cache by simulating a button click.
159 * @extends {NetInternalsTest.Task}
161 function ClearCacheTask() {
162 NetInternalsTest.Task.call(this);
165 ClearCacheTask.prototype = {
166 __proto__: NetInternalsTest.Task.prototype,
169 $(DnsView.CLEAR_CACHE_BUTTON_ID).onclick();
175 * A Task that waits for the specified hostname entry to disappear from the
177 * @param {string} hostname Name of host we're waiting to be removed.
178 * @extends {NetInternalsTest.Task}
180 function WaitForEntryDestructionTask(hostname) {
181 this.hostname_ = hostname;
182 NetInternalsTest.Task.call(this);
185 WaitForEntryDestructionTask.prototype = {
186 __proto__: NetInternalsTest.Task.prototype,
189 * Starts waiting to received the results from the browser process.
192 g_browser.addHostResolverInfoObserver(this, false);
196 * Callback from the BrowserBridge. Checks if the entry has been removed.
197 * If so, the task completes.
198 * @param {object} hostResolverInfo Results a host resolver info query.
200 onHostResolverInfoChanged: function(hostResolverInfo) {
201 if (!this.isDone()) {
202 checkDisplay(hostResolverInfo);
204 var entry = findEntry(hostResolverInfo, this.hostname_);
212 * Adds a successful lookup to the DNS cache, then clears the cache.
214 TEST_F('NetInternalsTest', 'netInternalsDnsViewSuccess', function() {
215 NetInternalsTest.switchToView('dns');
216 var taskQueue = new NetInternalsTest.TaskQueue(true);
217 taskQueue.addTask(new AddCacheEntryTask(
218 'somewhere.com', '1.2.3.4', 0, false));
219 taskQueue.addTask(new ClearCacheTask());
220 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com'));
225 * Adds a failed lookup to the DNS cache, then clears the cache.
227 TEST_F('NetInternalsTest', 'netInternalsDnsViewFail', function() {
228 NetInternalsTest.switchToView('dns');
229 var taskQueue = new NetInternalsTest.TaskQueue(true);
230 taskQueue.addTask(new AddCacheEntryTask(
231 'nowhere.com', '', NetError.ERR_NAME_NOT_RESOLVED, false));
232 taskQueue.addTask(new ClearCacheTask());
233 taskQueue.addTask(new WaitForEntryDestructionTask('nowhere.com'));
238 * Adds an expired successful lookup to the DNS cache, then clears the cache.
240 TEST_F('NetInternalsTest', 'netInternalsDnsViewExpired', function() {
241 NetInternalsTest.switchToView('dns');
242 var taskQueue = new NetInternalsTest.TaskQueue(true);
243 taskQueue.addTask(new AddCacheEntryTask(
244 'somewhere.com', '1.2.3.4', 0, true));
245 taskQueue.addTask(new ClearCacheTask());
246 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com'));
251 * Adds two entries to the DNS cache, clears the cache, and then repeats.
253 TEST_F('NetInternalsTest', 'netInternalsDnsViewAddTwoTwice', function() {
254 NetInternalsTest.switchToView('dns');
255 var taskQueue = new NetInternalsTest.TaskQueue(true);
256 for (var i = 0; i < 2; ++i) {
257 taskQueue.addTask(new AddCacheEntryTask(
258 'somewhere.com', '1.2.3.4', 0, false));
259 taskQueue.addTask(new AddCacheEntryTask(
260 'nowhere.com', '', NetError.ERR_NAME_NOT_RESOLVED, true));
261 taskQueue.addTask(new ClearCacheTask());
262 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com'));
263 taskQueue.addTask(new WaitForEntryDestructionTask('nowhere.com'));
269 * Makes sure that openning and then closing an incognito window clears the
270 * DNS cache. To keep things simple, we add a fake cache entry ourselves,
271 * rather than having the incognito browser create one.
273 TEST_F('NetInternalsTest', 'netInternalsDnsViewIncognitoClears', function() {
274 NetInternalsTest.switchToView('dns');
275 var taskQueue = new NetInternalsTest.TaskQueue(true);
276 taskQueue.addTask(new NetInternalsTest.CreateIncognitoBrowserTask());
277 taskQueue.addTask(new AddCacheEntryTask(
278 'somewhere.com', '1.2.3.4', 0, true));
279 taskQueue.addTask(NetInternalsTest.getCloseIncognitoBrowserTask());
280 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com'));
284 })(); // Anonymous namespace