1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Anonymous namespace
(function() {
/*
* A valid hash that can be set for a domain.
* @type {string}
*/
var VALID_HASH = 'sha1/Guzek9lMwR3KeIS8wwS9gBvVtIg=';
/*
* An invalid hash that can't be set for a domain.
* @type {string}
*/
var INVALID_HASH = 'invalid';
/*
* Possible results of an HSTS query.
* @enum {number}
*/
var QueryResultType = {
SUCCESS: 0,
NOT_FOUND: 1,
ERROR: 2
};
/**
* A Task that waits for the results of an HSTS query. Once the results are
* received, checks them before completing. Does not initiate the query.
* @param {string} domain The domain expected in the returned results.
* @param {bool} subdomains Whether or not the subdomains flag is expected to be
* set in the returned results. Ignored on error and not found results.
* @param {string} publicKeyHashes Expected public key hashes. Ignored on error
* error and not found results.
* @param {QueryResultType} queryResultType The expected result type of the
* results of the query.
* @extends {netInternalsTest.Task}
*/
function CheckQueryResultTask(domain, subdomains, publicKeyHashes,
queryResultType) {
this.domain_ = domain;
this.subdomains_ = subdomains;
this.publicKeyHashes_ = publicKeyHashes;
this.queryResultType_ = queryResultType;
netInternalsTest.Task.call(this);
}
CheckQueryResultTask.prototype = {
__proto__: netInternalsTest.Task.prototype,
/**
* Starts watching for the query results.
*/
start: function() {
g_browser.addHSTSObserver(this);
},
/**
* Callback from the BrowserBridge. Validates |result| and completes the
* task.
* @param {object} result Results from the query.
*/
onHSTSQueryResult: function(result) {
// Ignore results after |this| is finished.
if (!this.isDone()) {
expectEquals(this.domain_, $(HSTSView.QUERY_INPUT_ID).value);
// Each case has its own validation function because of the design of the
// test reporting infrastructure.
if (result.error != undefined) {
this.checkError_(result);
} else if (!result.result) {
this.checkNotFound_(result);
} else {
this.checkSuccess_(result);
}
this.running_ = false;
// Start the next task asynchronously, so it can add another HSTS observer
// without getting the current result.
window.setTimeout(this.onTaskDone.bind(this), 1);
}
},
/**
* On errors, checks the result.
* @param {object} result Results from the query.
*/
checkError_: function(result) {
expectEquals(QueryResultType.ERROR, this.queryResultType_);
expectEquals(result.error, $(HSTSView.QUERY_OUTPUT_DIV_ID).innerText);
},
/**
* Checks the result when the entry was not found.
* @param {object} result Results from the query.
*/
checkNotFound_: function(result) {
expectEquals(QueryResultType.NOT_FOUND, this.queryResultType_);
expectEquals('Not found', $(HSTSView.QUERY_OUTPUT_DIV_ID).innerText);
},
/**
* Checks successful results.
* @param {object} result Results from the query.
*/
checkSuccess_: function(result) {
expectEquals(QueryResultType.SUCCESS, this.queryResultType_);
expectEquals(this.subdomains_, result.subdomains);
expectEquals(this.publicKeyHashes_, result.public_key_hashes);
// Verify that the domain appears somewhere in the displayed text.
outputText = $(HSTSView.QUERY_OUTPUT_DIV_ID).innerText;
expectLE(0, outputText.search(this.domain_));
}
};
/**
* A Task to try and add an HSTS domain via the HTML form. The task will wait
* until the results from the automatically sent query have been received, and
* then checks them against the expected values.
* @param {string} domain The domain to send and expected to be returned.
* @param {bool} subdomains Whether the subdomain checkbox should be selected.
* Also the corresponding expected return value, in the success case.
* @param {string} publicKeyHashes Public key hash to send. Also the
* corresponding expected return value, on success. When this is the string
* INVALID_HASH, an empty string is expected to be received instead.
* @param {QueryResultType} queryResultType Expected result type.
* @extends {CheckQueryResultTask}
*/
function AddTask(domain, subdomains, publicKeyHashes, queryResultType) {
this.requestedPublicKeyHashes_ = publicKeyHashes;
if (publicKeyHashes == INVALID_HASH)
publicKeyHashes = '';
CheckQueryResultTask.call(this, domain, subdomains, publicKeyHashes,
queryResultType);
}
AddTask.prototype = {
__proto__: CheckQueryResultTask.prototype,
/**
* Fills out the add form, simulates a click to submit it, and starts
* listening for the results of the query that is automatically submitted.
*/
start: function() {
$(HSTSView.ADD_INPUT_ID).value = this.domain_;
$(HSTSView.ADD_CHECK_ID).checked = this.subdomains_;
$(HSTSView.ADD_PINS_ID).value = this.requestedPublicKeyHashes_;
$(HSTSView.ADD_SUBMIT_ID).click();
CheckQueryResultTask.prototype.start.call(this);
}
};
/**
* A Task to query a domain and wait for the results. Parameters mirror those
* of CheckQueryResultTask, except |domain| is also the name of the domain to
* query.
* @extends {CheckQueryResultTask}
*/
function QueryTask(domain, subdomains, publicKeyHashes, queryResultType) {
CheckQueryResultTask.call(this, domain, subdomains, publicKeyHashes,
queryResultType);
}
QueryTask.prototype = {
__proto__: CheckQueryResultTask.prototype,
/**
* Fills out the query form, simulates a click to submit it, and starts
* listening for the results.
*/
start: function() {
CheckQueryResultTask.prototype.start.call(this);
$(HSTSView.QUERY_INPUT_ID).value = this.domain_;
$(HSTSView.QUERY_SUBMIT_ID).click();
}
};
/**
* Task that deletes a single domain, then queries the deleted domain to make
* sure it's gone.
* @param {string} domain The domain to delete.
* @param {QueryResultType} queryResultType The result of the query. Can be
* QueryResultType.ERROR or QueryResultType.NOT_FOUND.
* @extends {QueryTask}
*/
function DeleteTask(domain, queryResultType) {
expectNotEquals(queryResultType, QueryResultType.SUCCESS);
this.domain_ = domain;
QueryTask.call(this, domain, false, '', queryResultType);
}
DeleteTask.prototype = {
__proto__: QueryTask.prototype,
/**
* Fills out the delete form and simulates a click to submit it. Then sends
* a query.
*/
start: function() {
$(HSTSView.DELETE_INPUT_ID).value = this.domain_;
$(HSTSView.DELETE_SUBMIT_ID).click();
QueryTask.prototype.start.call(this);
}
};
netInternalsTest.test('netInternalsHSTSViewQueryNotFound', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new QueryTask('somewhere.com', false, '',
QueryResultType.NOT_FOUND));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewQueryError', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new QueryTask('\u3024', false, '', QueryResultType.ERROR));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewDeleteNotFound', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new DeleteTask('somewhere.com', QueryResultType.NOT_FOUND));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewDeleteError', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new DeleteTask('\u3024', QueryResultType.ERROR));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewAddDelete', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new AddTask('somewhere.com', false, VALID_HASH,
QueryResultType.SUCCESS));
taskQueue.addTask(new DeleteTask('somewhere.com', QueryResultType.NOT_FOUND));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewAddFail', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new AddTask('~', false, '', QueryResultType.NOT_FOUND));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewAddError', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new AddTask('\u3024', false, '', QueryResultType.ERROR));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewAddInvalidHash', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new AddTask('somewhere.com', true, INVALID_HASH,
QueryResultType.SUCCESS));
taskQueue.addTask(new DeleteTask('somewhere.com', QueryResultType.NOT_FOUND));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewAddOverwrite', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new AddTask('somewhere.com', true, VALID_HASH,
QueryResultType.SUCCESS));
taskQueue.addTask(new AddTask('somewhere.com', false, '',
QueryResultType.SUCCESS));
taskQueue.addTask(new DeleteTask('somewhere.com', QueryResultType.NOT_FOUND));
taskQueue.run();
});
netInternalsTest.test('netInternalsHSTSViewAddTwice', function() {
netInternalsTest.switchToView('hsts');
taskQueue = new netInternalsTest.TaskQueue(true);
taskQueue.addTask(new AddTask('somewhere.com', false, VALID_HASH,
QueryResultType.SUCCESS));
taskQueue.addTask(new QueryTask('somewhereelse.com', false, '',
QueryResultType.NOT_FOUND));
taskQueue.addTask(new AddTask('somewhereelse.com', true, '',
QueryResultType.SUCCESS));
taskQueue.addTask(new QueryTask('somewhere.com', false, VALID_HASH,
QueryResultType.SUCCESS));
taskQueue.addTask(new DeleteTask('somewhere.com', QueryResultType.NOT_FOUND));
taskQueue.addTask(new QueryTask('somewhereelse.com', true, '',
QueryResultType.SUCCESS));
taskQueue.addTask(new DeleteTask('somewhereelse.com',
QueryResultType.NOT_FOUND));
taskQueue.run(true);
});
})(); // Anonymous namespace
|