Pass CreateDirectory errors up to IndexedDB.
[chromium-blink-merge.git] / net / http / http_security_headers_unittest.cc
blob0dd286b3ed348574bec2a5b306f52079377b4404
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 "base/base64.h"
6 #include "base/sha1.h"
7 #include "base/strings/string_piece.h"
8 #include "crypto/sha2.h"
9 #include "net/base/net_log.h"
10 #include "net/base/test_completion_callback.h"
11 #include "net/http/http_security_headers.h"
12 #include "net/http/http_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace net {
17 namespace {
19 HashValue GetTestHashValue(uint8 label, HashValueTag tag) {
20 HashValue hash_value(tag);
21 memset(hash_value.data(), label, hash_value.size());
22 return hash_value;
25 std::string GetTestPin(uint8 label, HashValueTag tag) {
26 HashValue hash_value = GetTestHashValue(label, tag);
27 std::string base64;
28 base::Base64Encode(base::StringPiece(
29 reinterpret_cast<char*>(hash_value.data()), hash_value.size()), &base64);
31 switch (hash_value.tag) {
32 case HASH_VALUE_SHA1:
33 return std::string("pin-sha1=\"") + base64 + "\"";
34 case HASH_VALUE_SHA256:
35 return std::string("pin-sha256=\"") + base64 + "\"";
36 default:
37 NOTREACHED() << "Unknown HashValueTag " << hash_value.tag;
38 return std::string("ERROR");
45 class HttpSecurityHeadersTest : public testing::Test {
49 TEST_F(HttpSecurityHeadersTest, BogusHeaders) {
50 base::TimeDelta max_age;
51 bool include_subdomains = false;
53 EXPECT_FALSE(
54 ParseHSTSHeader(std::string(), &max_age, &include_subdomains));
55 EXPECT_FALSE(ParseHSTSHeader(" ", &max_age, &include_subdomains));
56 EXPECT_FALSE(ParseHSTSHeader("abc", &max_age, &include_subdomains));
57 EXPECT_FALSE(ParseHSTSHeader(" abc", &max_age, &include_subdomains));
58 EXPECT_FALSE(ParseHSTSHeader(" abc ", &max_age, &include_subdomains));
59 EXPECT_FALSE(ParseHSTSHeader("max-age", &max_age, &include_subdomains));
60 EXPECT_FALSE(ParseHSTSHeader(" max-age", &max_age,
61 &include_subdomains));
62 EXPECT_FALSE(ParseHSTSHeader(" max-age ", &max_age,
63 &include_subdomains));
64 EXPECT_FALSE(ParseHSTSHeader("max-age=", &max_age, &include_subdomains));
65 EXPECT_FALSE(ParseHSTSHeader(" max-age=", &max_age,
66 &include_subdomains));
67 EXPECT_FALSE(ParseHSTSHeader(" max-age =", &max_age,
68 &include_subdomains));
69 EXPECT_FALSE(ParseHSTSHeader(" max-age= ", &max_age,
70 &include_subdomains));
71 EXPECT_FALSE(ParseHSTSHeader(" max-age = ", &max_age,
72 &include_subdomains));
73 EXPECT_FALSE(ParseHSTSHeader(" max-age = xy", &max_age,
74 &include_subdomains));
75 EXPECT_FALSE(ParseHSTSHeader(" max-age = 3488a923", &max_age,
76 &include_subdomains));
77 EXPECT_FALSE(ParseHSTSHeader("max-age=3488a923 ", &max_age,
78 &include_subdomains));
79 EXPECT_FALSE(ParseHSTSHeader("max-ag=3488923", &max_age,
80 &include_subdomains));
81 EXPECT_FALSE(ParseHSTSHeader("max-aged=3488923", &max_age,
82 &include_subdomains));
83 EXPECT_FALSE(ParseHSTSHeader("max-age==3488923", &max_age,
84 &include_subdomains));
85 EXPECT_FALSE(ParseHSTSHeader("amax-age=3488923", &max_age,
86 &include_subdomains));
87 EXPECT_FALSE(ParseHSTSHeader("max-age=-3488923", &max_age,
88 &include_subdomains));
89 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923;", &max_age,
90 &include_subdomains));
91 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923 e", &max_age,
92 &include_subdomains));
93 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923 includesubdomain",
94 &max_age, &include_subdomains));
95 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923includesubdomains",
96 &max_age, &include_subdomains));
97 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923=includesubdomains",
98 &max_age, &include_subdomains));
99 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923 includesubdomainx",
100 &max_age, &include_subdomains));
101 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923 includesubdomain=",
102 &max_age, &include_subdomains));
103 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923 includesubdomain=true",
104 &max_age, &include_subdomains));
105 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923 includesubdomainsx",
106 &max_age, &include_subdomains));
107 EXPECT_FALSE(ParseHSTSHeader("max-age=3488923 includesubdomains x",
108 &max_age, &include_subdomains));
109 EXPECT_FALSE(ParseHSTSHeader("max-age=34889.23 includesubdomains",
110 &max_age, &include_subdomains));
111 EXPECT_FALSE(ParseHSTSHeader("max-age=34889 includesubdomains",
112 &max_age, &include_subdomains));
114 // Check the out args were not updated by checking the default
115 // values for its predictable fields.
116 EXPECT_EQ(0, max_age.InSeconds());
117 EXPECT_FALSE(include_subdomains);
120 static void TestBogusPinsHeaders(HashValueTag tag) {
121 base::TimeDelta max_age;
122 HashValueVector hashes;
123 HashValueVector chain_hashes;
125 // Set some fake "chain" hashes
126 chain_hashes.push_back(GetTestHashValue(1, tag));
127 chain_hashes.push_back(GetTestHashValue(2, tag));
128 chain_hashes.push_back(GetTestHashValue(3, tag));
130 // The good pin must be in the chain, the backup pin must not be
131 std::string good_pin = GetTestPin(2, tag);
132 std::string backup_pin = GetTestPin(4, tag);
134 EXPECT_FALSE(
135 ParseHPKPHeader(std::string(), chain_hashes, &max_age, &hashes));
136 EXPECT_FALSE(ParseHPKPHeader(" ", chain_hashes, &max_age, &hashes));
137 EXPECT_FALSE(ParseHPKPHeader("abc", chain_hashes, &max_age, &hashes));
138 EXPECT_FALSE(ParseHPKPHeader(" abc", chain_hashes, &max_age, &hashes));
139 EXPECT_FALSE(ParseHPKPHeader(" abc ", chain_hashes, &max_age,
140 &hashes));
141 EXPECT_FALSE(ParseHPKPHeader("max-age", chain_hashes, &max_age,
142 &hashes));
143 EXPECT_FALSE(ParseHPKPHeader(" max-age", chain_hashes, &max_age,
144 &hashes));
145 EXPECT_FALSE(ParseHPKPHeader(" max-age ", chain_hashes, &max_age,
146 &hashes));
147 EXPECT_FALSE(ParseHPKPHeader("max-age=", chain_hashes, &max_age,
148 &hashes));
149 EXPECT_FALSE(ParseHPKPHeader(" max-age=", chain_hashes, &max_age,
150 &hashes));
151 EXPECT_FALSE(ParseHPKPHeader(" max-age =", chain_hashes, &max_age,
152 &hashes));
153 EXPECT_FALSE(ParseHPKPHeader(" max-age= ", chain_hashes, &max_age,
154 &hashes));
155 EXPECT_FALSE(ParseHPKPHeader(" max-age = ", chain_hashes,
156 &max_age, &hashes));
157 EXPECT_FALSE(ParseHPKPHeader(" max-age = xy", chain_hashes,
158 &max_age, &hashes));
159 EXPECT_FALSE(ParseHPKPHeader(" max-age = 3488a923",
160 chain_hashes, &max_age, &hashes));
161 EXPECT_FALSE(ParseHPKPHeader("max-age=3488a923 ", chain_hashes,
162 &max_age, &hashes));
163 EXPECT_FALSE(ParseHPKPHeader("max-ag=3488923pins=" + good_pin + "," +
164 backup_pin,
165 chain_hashes, &max_age, &hashes));
166 EXPECT_FALSE(ParseHPKPHeader("max-aged=3488923" + backup_pin,
167 chain_hashes, &max_age, &hashes));
168 EXPECT_FALSE(ParseHPKPHeader("max-aged=3488923; " + backup_pin,
169 chain_hashes, &max_age, &hashes));
170 EXPECT_FALSE(ParseHPKPHeader("max-aged=3488923; " + backup_pin + ";" +
171 backup_pin,
172 chain_hashes, &max_age, &hashes));
173 EXPECT_FALSE(ParseHPKPHeader("max-aged=3488923; " + good_pin + ";" +
174 good_pin,
175 chain_hashes, &max_age, &hashes));
176 EXPECT_FALSE(ParseHPKPHeader("max-aged=3488923; " + good_pin,
177 chain_hashes, &max_age, &hashes));
178 EXPECT_FALSE(ParseHPKPHeader("max-age==3488923", chain_hashes, &max_age,
179 &hashes));
180 EXPECT_FALSE(ParseHPKPHeader("amax-age=3488923", chain_hashes, &max_age,
181 &hashes));
182 EXPECT_FALSE(ParseHPKPHeader("max-age=-3488923", chain_hashes, &max_age,
183 &hashes));
184 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923;", chain_hashes, &max_age,
185 &hashes));
186 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923 e", chain_hashes,
187 &max_age, &hashes));
188 EXPECT_FALSE(ParseHPKPHeader("max-age=3488923 includesubdomain",
189 chain_hashes, &max_age, &hashes));
190 EXPECT_FALSE(ParseHPKPHeader("max-age=34889.23", chain_hashes, &max_age,
191 &hashes));
193 // Check the out args were not updated by checking the default
194 // values for its predictable fields.
195 EXPECT_EQ(0, max_age.InSeconds());
196 EXPECT_EQ(hashes.size(), (size_t)0);
199 TEST_F(HttpSecurityHeadersTest, ValidSTSHeaders) {
200 base::TimeDelta max_age;
201 base::TimeDelta expect_max_age;
202 bool include_subdomains = false;
204 EXPECT_TRUE(ParseHSTSHeader("max-age=243", &max_age,
205 &include_subdomains));
206 expect_max_age = base::TimeDelta::FromSeconds(243);
207 EXPECT_EQ(expect_max_age, max_age);
208 EXPECT_FALSE(include_subdomains);
210 EXPECT_TRUE(ParseHSTSHeader(" Max-agE = 567", &max_age,
211 &include_subdomains));
212 expect_max_age = base::TimeDelta::FromSeconds(567);
213 EXPECT_EQ(expect_max_age, max_age);
214 EXPECT_FALSE(include_subdomains);
216 EXPECT_TRUE(ParseHSTSHeader(" mAx-aGe = 890 ", &max_age,
217 &include_subdomains));
218 expect_max_age = base::TimeDelta::FromSeconds(890);
219 EXPECT_EQ(expect_max_age, max_age);
220 EXPECT_FALSE(include_subdomains);
222 EXPECT_TRUE(ParseHSTSHeader("max-age=123;incLudesUbdOmains", &max_age,
223 &include_subdomains));
224 expect_max_age = base::TimeDelta::FromSeconds(123);
225 EXPECT_EQ(expect_max_age, max_age);
226 EXPECT_TRUE(include_subdomains);
228 EXPECT_TRUE(ParseHSTSHeader("incLudesUbdOmains; max-age=123", &max_age,
229 &include_subdomains));
230 expect_max_age = base::TimeDelta::FromSeconds(123);
231 EXPECT_EQ(expect_max_age, max_age);
232 EXPECT_TRUE(include_subdomains);
234 EXPECT_TRUE(ParseHSTSHeader(" incLudesUbdOmains; max-age=123",
235 &max_age, &include_subdomains));
236 expect_max_age = base::TimeDelta::FromSeconds(123);
237 EXPECT_EQ(expect_max_age, max_age);
238 EXPECT_TRUE(include_subdomains);
240 EXPECT_TRUE(ParseHSTSHeader(
241 " incLudesUbdOmains; max-age=123; pumpkin=kitten", &max_age,
242 &include_subdomains));
243 expect_max_age = base::TimeDelta::FromSeconds(123);
244 EXPECT_EQ(expect_max_age, max_age);
245 EXPECT_TRUE(include_subdomains);
247 EXPECT_TRUE(ParseHSTSHeader(
248 " pumpkin=894; incLudesUbdOmains; max-age=123 ", &max_age,
249 &include_subdomains));
250 expect_max_age = base::TimeDelta::FromSeconds(123);
251 EXPECT_EQ(expect_max_age, max_age);
252 EXPECT_TRUE(include_subdomains);
254 EXPECT_TRUE(ParseHSTSHeader(
255 " pumpkin; incLudesUbdOmains; max-age=123 ", &max_age,
256 &include_subdomains));
257 expect_max_age = base::TimeDelta::FromSeconds(123);
258 EXPECT_EQ(expect_max_age, max_age);
259 EXPECT_TRUE(include_subdomains);
261 EXPECT_TRUE(ParseHSTSHeader(
262 " pumpkin; incLudesUbdOmains; max-age=\"123\" ", &max_age,
263 &include_subdomains));
264 expect_max_age = base::TimeDelta::FromSeconds(123);
265 EXPECT_EQ(expect_max_age, max_age);
266 EXPECT_TRUE(include_subdomains);
268 EXPECT_TRUE(ParseHSTSHeader(
269 "animal=\"squirrel; distinguished\"; incLudesUbdOmains; max-age=123",
270 &max_age, &include_subdomains));
271 expect_max_age = base::TimeDelta::FromSeconds(123);
272 EXPECT_EQ(expect_max_age, max_age);
273 EXPECT_TRUE(include_subdomains);
275 EXPECT_TRUE(ParseHSTSHeader("max-age=394082; incLudesUbdOmains",
276 &max_age, &include_subdomains));
277 expect_max_age = base::TimeDelta::FromSeconds(394082);
278 EXPECT_EQ(expect_max_age, max_age);
279 EXPECT_TRUE(include_subdomains);
281 EXPECT_TRUE(ParseHSTSHeader(
282 "max-age=39408299 ;incLudesUbdOmains", &max_age,
283 &include_subdomains));
284 expect_max_age = base::TimeDelta::FromSeconds(
285 std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(39408299))));
286 EXPECT_EQ(expect_max_age, max_age);
287 EXPECT_TRUE(include_subdomains);
289 EXPECT_TRUE(ParseHSTSHeader(
290 "max-age=394082038 ; incLudesUbdOmains", &max_age,
291 &include_subdomains));
292 expect_max_age = base::TimeDelta::FromSeconds(
293 std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(394082038))));
294 EXPECT_EQ(expect_max_age, max_age);
295 EXPECT_TRUE(include_subdomains);
297 EXPECT_TRUE(ParseHSTSHeader(
298 " max-age=0 ; incLudesUbdOmains ", &max_age,
299 &include_subdomains));
300 expect_max_age = base::TimeDelta::FromSeconds(0);
301 EXPECT_EQ(expect_max_age, max_age);
302 EXPECT_TRUE(include_subdomains);
304 EXPECT_TRUE(ParseHSTSHeader(
305 " max-age=999999999999999999999999999999999999999999999 ;"
306 " incLudesUbdOmains ", &max_age, &include_subdomains));
307 expect_max_age = base::TimeDelta::FromSeconds(
308 kMaxHSTSAgeSecs);
309 EXPECT_EQ(expect_max_age, max_age);
310 EXPECT_TRUE(include_subdomains);
313 static void TestValidPinsHeaders(HashValueTag tag) {
314 base::TimeDelta max_age;
315 base::TimeDelta expect_max_age;
316 HashValueVector hashes;
317 HashValueVector chain_hashes;
319 // Set some fake "chain" hashes into chain_hashes
320 chain_hashes.push_back(GetTestHashValue(1, tag));
321 chain_hashes.push_back(GetTestHashValue(2, tag));
322 chain_hashes.push_back(GetTestHashValue(3, tag));
324 // The good pin must be in the chain, the backup pin must not be
325 std::string good_pin = GetTestPin(2, tag);
326 std::string backup_pin = GetTestPin(4, tag);
328 EXPECT_TRUE(ParseHPKPHeader(
329 "max-age=243; " + good_pin + ";" + backup_pin,
330 chain_hashes, &max_age, &hashes));
331 expect_max_age = base::TimeDelta::FromSeconds(243);
332 EXPECT_EQ(expect_max_age, max_age);
334 EXPECT_TRUE(ParseHPKPHeader(
335 " " + good_pin + "; " + backup_pin + " ; Max-agE = 567",
336 chain_hashes, &max_age, &hashes));
337 expect_max_age = base::TimeDelta::FromSeconds(567);
338 EXPECT_EQ(expect_max_age, max_age);
340 EXPECT_TRUE(ParseHPKPHeader(
341 good_pin + ";" + backup_pin + " ; mAx-aGe = 890 ",
342 chain_hashes, &max_age, &hashes));
343 expect_max_age = base::TimeDelta::FromSeconds(890);
344 EXPECT_EQ(expect_max_age, max_age);
346 EXPECT_TRUE(ParseHPKPHeader(
347 good_pin + ";" + backup_pin + "; max-age=123;IGNORED;",
348 chain_hashes, &max_age, &hashes));
349 expect_max_age = base::TimeDelta::FromSeconds(123);
350 EXPECT_EQ(expect_max_age, max_age);
352 EXPECT_TRUE(ParseHPKPHeader(
353 "max-age=394082;" + backup_pin + ";" + good_pin + "; ",
354 chain_hashes, &max_age, &hashes));
355 expect_max_age = base::TimeDelta::FromSeconds(394082);
356 EXPECT_EQ(expect_max_age, max_age);
358 EXPECT_TRUE(ParseHPKPHeader(
359 "max-age=39408299 ;" + backup_pin + ";" + good_pin + "; ",
360 chain_hashes, &max_age, &hashes));
361 expect_max_age = base::TimeDelta::FromSeconds(
362 std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(39408299))));
363 EXPECT_EQ(expect_max_age, max_age);
365 EXPECT_TRUE(ParseHPKPHeader(
366 "max-age=39408038 ; cybers=39408038 ; " +
367 good_pin + ";" + backup_pin + "; ",
368 chain_hashes, &max_age, &hashes));
369 expect_max_age = base::TimeDelta::FromSeconds(
370 std::min(kMaxHSTSAgeSecs, static_cast<int64>(GG_INT64_C(394082038))));
371 EXPECT_EQ(expect_max_age, max_age);
373 EXPECT_TRUE(ParseHPKPHeader(
374 " max-age=0 ; " + good_pin + ";" + backup_pin,
375 chain_hashes, &max_age, &hashes));
376 expect_max_age = base::TimeDelta::FromSeconds(0);
377 EXPECT_EQ(expect_max_age, max_age);
379 EXPECT_TRUE(ParseHPKPHeader(
380 " max-age=999999999999999999999999999999999999999999999 ; " +
381 backup_pin + ";" + good_pin + "; ",
382 chain_hashes, &max_age, &hashes));
383 expect_max_age = base::TimeDelta::FromSeconds(kMaxHSTSAgeSecs);
384 EXPECT_EQ(expect_max_age, max_age);
387 TEST_F(HttpSecurityHeadersTest, BogusPinsHeadersSHA1) {
388 TestBogusPinsHeaders(HASH_VALUE_SHA1);
391 TEST_F(HttpSecurityHeadersTest, BogusPinsHeadersSHA256) {
392 TestBogusPinsHeaders(HASH_VALUE_SHA256);
395 TEST_F(HttpSecurityHeadersTest, ValidPinsHeadersSHA1) {
396 TestValidPinsHeaders(HASH_VALUE_SHA1);
399 TEST_F(HttpSecurityHeadersTest, ValidPinsHeadersSHA256) {
400 TestValidPinsHeaders(HASH_VALUE_SHA256);
403 }; // namespace net