OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/strings/pattern.h" | 6 #include "base/strings/pattern.h" |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
10 #include "content/public/common/content_switches.h" | 10 #include "content/public/common/content_switches.h" |
11 #include "content/public/common/resource_type.h" | 11 #include "content/public/common/resource_type.h" |
12 #include "content/public/test/browser_test_utils.h" | 12 #include "content/public/test/browser_test_utils.h" |
13 #include "content/public/test/content_browser_test.h" | 13 #include "content/public/test/content_browser_test.h" |
14 #include "content/public/test/content_browser_test_utils.h" | 14 #include "content/public/test/content_browser_test_utils.h" |
15 #include "content/shell/browser/shell.h" | 15 #include "content/shell/browser/shell.h" |
16 #include "net/test/spawned_test_server/spawned_test_server.h" | 16 #include "net/test/spawned_test_server/spawned_test_server.h" |
17 #include "testing/gmock/include/gmock/gmock-generated-matchers.h" | |
twifkak
2015/07/23 21:21:54
Ditto here, just gmock.h.
ncarter (slow)
2015/07/23 21:50:43
Done.
| |
18 #include "testing/gmock/include/gmock/gmock-matchers.h" | |
17 | 19 |
18 namespace content { | 20 namespace content { |
19 | 21 |
20 // These tests simulate exploited renderer processes, which can fetch arbitrary | 22 // These tests simulate exploited renderer processes, which can fetch arbitrary |
21 // resources from other websites, not constrained by the Same Origin Policy. We | 23 // resources from other websites, not constrained by the Same Origin Policy. We |
22 // are trying to verify that the renderer cannot fetch any cross-site document | 24 // are trying to verify that the renderer cannot fetch any cross-site document |
23 // responses even when the Same Origin Policy is turned off inside the renderer. | 25 // responses even when the Same Origin Policy is turned off inside the renderer. |
24 class SiteIsolationStatsGathererBrowserTest : public ContentBrowserTest { | 26 class SiteIsolationStatsGathererBrowserTest : public ContentBrowserTest { |
25 public: | 27 public: |
26 SiteIsolationStatsGathererBrowserTest() {} | 28 SiteIsolationStatsGathererBrowserTest() {} |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 } else if (base::MatchPattern(resource_name, "xml*")) { | 67 } else if (base::MatchPattern(resource_name, "xml*")) { |
66 bucket += ".XML"; | 68 bucket += ".XML"; |
67 } | 69 } |
68 } else { | 70 } else { |
69 FAIL(); | 71 FAIL(); |
70 } | 72 } |
71 FetchHistogramsFromChildProcesses(); | 73 FetchHistogramsFromChildProcesses(); |
72 | 74 |
73 // A few histograms are incremented unconditionally. | 75 // A few histograms are incremented unconditionally. |
74 histograms.ExpectUniqueSample("SiteIsolation.AllResponses", 1, 1); | 76 histograms.ExpectUniqueSample("SiteIsolation.AllResponses", 1, 1); |
75 histograms.ExpectTotalCount("SiteIsolation.XSD.DataLength", 1); | 77 base::HistogramTester::CountsMap expected_metrics; |
76 histograms.ExpectUniqueSample("SiteIsolation.XSD.MimeType", mime_type, 1); | 78 expected_metrics["SiteIsolation.XSD.DataLength"] = 1; |
79 expected_metrics["SiteIsolation.XSD.MimeType"] = 1; | |
77 | 80 |
78 // Inspect the appropriate conditionally-incremented histogram[s]. | 81 // Determine the appropriate conditionally-incremented histograms. |
79 std::set<std::string> expected_metrics; | 82 std::string base = "SiteIsolation.XSD." + bucket; |
80 std::string base_metric = "SiteIsolation.XSD." + bucket; | |
81 base_metric += should_be_blocked ? ".Blocked" : ".NotBlocked"; | |
82 expected_metrics.insert(base_metric); | |
83 if (should_be_blocked) { | 83 if (should_be_blocked) { |
84 expected_metrics.insert(base_metric + ".RenderableStatusCode"); | 84 expected_metrics[base + ".Blocked"] = 1; |
85 } else if (base::MatchPattern(resource_name, "*js.*")) { | 85 expected_metrics[base + ".Blocked.RenderableStatusCode"] = 1; |
86 expected_metrics.insert(base_metric + ".MaybeJS"); | 86 } else { |
87 } | 87 expected_metrics[base + ".NotBlocked"] = 1; |
88 | 88 if (base::MatchPattern(resource_name, "*js.*")) { |
89 for (std::string metric : expected_metrics) { | 89 expected_metrics[base + ".NotBlocked.MaybeJS"] = 1; |
90 if (base::MatchPattern(metric, "*.RenderableStatusCode")) { | |
91 histograms.ExpectUniqueSample(metric, RESOURCE_TYPE_XHR, 1); | |
92 } else { | |
93 histograms.ExpectUniqueSample(metric, 1, 1); | |
94 } | 90 } |
95 } | 91 } |
96 | 92 |
97 // Make sure no other conditionally-incremented histograms were touched. | 93 // Make sure that the expected metrics, and only those metrics, were |
98 const char* all_metrics[] = { | 94 // incremented. |
99 "SiteIsolation.XSD.HTML.Blocked", | 95 EXPECT_THAT(histograms.GetTotalCountsForPrefix("SiteIsolation.XSD."), |
100 "SiteIsolation.XSD.HTML.Blocked.NonRenderableStatusCode", | 96 testing::ContainerEq(expected_metrics)) |
101 "SiteIsolation.XSD.HTML.Blocked.RenderableStatusCode", | 97 << "For resource_name=" << resource_name |
102 "SiteIsolation.XSD.HTML.NoSniffBlocked", | 98 << ", should_be_blocked=" << should_be_blocked; |
103 "SiteIsolation.XSD.HTML.NoSniffBlocked.NonRenderableStatusCode", | |
104 "SiteIsolation.XSD.HTML.NoSniffBlocked.RenderableStatusCode", | |
105 "SiteIsolation.XSD.HTML.NotBlocked", | |
106 "SiteIsolation.XSD.HTML.NotBlocked.MaybeJS", | |
107 "SiteIsolation.XSD.JSON.Blocked", | |
108 "SiteIsolation.XSD.JSON.Blocked.NonRenderableStatusCode", | |
109 "SiteIsolation.XSD.JSON.Blocked.RenderableStatusCode", | |
110 "SiteIsolation.XSD.JSON.NoSniffBlocked", | |
111 "SiteIsolation.XSD.JSON.NoSniffBlocked.NonRenderableStatusCode", | |
112 "SiteIsolation.XSD.JSON.NoSniffBlocked.RenderableStatusCode", | |
113 "SiteIsolation.XSD.JSON.NotBlocked", | |
114 "SiteIsolation.XSD.JSON.NotBlocked.MaybeJS", | |
115 "SiteIsolation.XSD.Plain.HTML.Blocked", | |
116 "SiteIsolation.XSD.Plain.HTML.Blocked.NonRenderableStatusCode", | |
117 "SiteIsolation.XSD.Plain.HTML.Blocked.RenderableStatusCode", | |
118 "SiteIsolation.XSD.Plain.JSON.Blocked", | |
119 "SiteIsolation.XSD.Plain.JSON.Blocked.NonRenderableStatusCode", | |
120 "SiteIsolation.XSD.Plain.JSON.Blocked.RenderableStatusCode", | |
121 "SiteIsolation.XSD.Plain.NoSniffBlocked", | |
122 "SiteIsolation.XSD.Plain.NoSniffBlocked.NonRenderableStatusCode", | |
123 "SiteIsolation.XSD.Plain.NoSniffBlocked.RenderableStatusCode", | |
124 "SiteIsolation.XSD.Plain.NotBlocked", | |
125 "SiteIsolation.XSD.Plain.NotBlocked.MaybeJS", | |
126 "SiteIsolation.XSD.Plain.XML.Blocked", | |
127 "SiteIsolation.XSD.Plain.XML.Blocked.NonRenderableStatusCode", | |
128 "SiteIsolation.XSD.Plain.XML.Blocked.RenderableStatusCode", | |
129 "SiteIsolation.XSD.XML.Blocked", | |
130 "SiteIsolation.XSD.XML.Blocked.NonRenderableStatusCode", | |
131 "SiteIsolation.XSD.XML.Blocked.RenderableStatusCode", | |
132 "SiteIsolation.XSD.XML.NoSniffBlocked", | |
133 "SiteIsolation.XSD.XML.NoSniffBlocked.NonRenderableStatusCode", | |
134 "SiteIsolation.XSD.XML.NoSniffBlocked.RenderableStatusCode", | |
135 "SiteIsolation.XSD.XML.NotBlocked", | |
136 "SiteIsolation.XSD.XML.NotBlocked.MaybeJS"}; | |
137 | 99 |
138 for (const char* metric : all_metrics) { | 100 EXPECT_THAT(histograms.GetAllSamples("SiteIsolation.XSD.MimeType"), |
139 if (!expected_metrics.count(metric)) { | 101 testing::ElementsAre(base::Bucket(mime_type, 1))) |
140 histograms.ExpectTotalCount(metric, 0); | 102 << "The wrong mime type bucket was incremented."; |
141 } | 103 if (should_be_blocked) { |
104 static_assert(13 == RESOURCE_TYPE_XHR, "Histogram enums mustn't change."); | |
105 EXPECT_THAT( | |
106 histograms.GetAllSamples(base + ".Blocked.RenderableStatusCode"), | |
107 testing::ElementsAre(base::Bucket(RESOURCE_TYPE_XHR, 1))) | |
108 << "The wrong RenderableStatusCode bucket was incremented."; | |
142 } | 109 } |
twifkak
2015/07/23 21:21:54
IIUC, this doesn't replicate line 93 of the left s
ncarter (slow)
2015/07/23 21:50:43
Yes, it's intended -- line 93 in the original was
| |
143 } | 110 } |
144 | 111 |
145 private: | 112 private: |
146 DISALLOW_COPY_AND_ASSIGN(SiteIsolationStatsGathererBrowserTest); | 113 DISALLOW_COPY_AND_ASSIGN(SiteIsolationStatsGathererBrowserTest); |
147 }; | 114 }; |
148 | 115 |
149 // TODO(dsjang): we cannot run these tests on Android since SetUpCommandLine() | 116 // TODO(dsjang): we cannot run these tests on Android since SetUpCommandLine() |
150 // is executed before the I/O thread is created on Android. After this bug | 117 // is executed before the I/O thread is created on Android. After this bug |
151 // (crbug.com/278425) is resolved, we can enable this test case on Android. | 118 // (crbug.com/278425) is resolved, we can enable this test case on Android. |
152 #if defined(OS_ANDROID) | 119 #if defined(OS_ANDROID) |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 // length is different from what's described in "content-length" for such | 213 // length is different from what's described in "content-length" for such |
247 // different targets. | 214 // different targets. |
248 | 215 |
249 // TODO(nick): Split up these cases, and add positive assertions here about | 216 // TODO(nick): Split up these cases, and add positive assertions here about |
250 // what actually happens in these various resource-block cases. | 217 // what actually happens in these various resource-block cases. |
251 GURL foo("http://foo.com/files/cross_site_document_request_target.html"); | 218 GURL foo("http://foo.com/files/cross_site_document_request_target.html"); |
252 NavigateToURL(shell(), foo); | 219 NavigateToURL(shell(), foo); |
253 } | 220 } |
254 | 221 |
255 } // namespace content | 222 } // namespace content |
OLD | NEW |