Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(451)

Side by Side Diff: chrome/browser/extensions/api/debugger/debugger_apitest.cc

Issue 910053002: Validate debuggee.targetId before use in chrome.debugger (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments #6 Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/extensions/api/debugger/debugger_api.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 <string> 5 #include <string>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
(...skipping 24 matching lines...) Expand all
35 // Run the attach function. If |expected_error| is not empty, then the 35 // Run the attach function. If |expected_error| is not empty, then the
36 // function should fail with the error. Otherwise, the function is expected 36 // function should fail with the error. Otherwise, the function is expected
37 // to succeed. 37 // to succeed.
38 testing::AssertionResult RunAttachFunction(const GURL& url, 38 testing::AssertionResult RunAttachFunction(const GURL& url,
39 const std::string& expected_error); 39 const std::string& expected_error);
40 40
41 const Extension* extension() const { return extension_.get(); } 41 const Extension* extension() const { return extension_.get(); }
42 base::CommandLine* command_line() const { return command_line_; } 42 base::CommandLine* command_line() const { return command_line_; }
43 43
44 private: 44 private:
45 testing::AssertionResult RunAttachFunctionOnTarget(
46 const std::string& debuggee_target, const std::string& expected_error);
47
45 // The command-line for the test process, preserved in order to modify 48 // The command-line for the test process, preserved in order to modify
46 // mid-test. 49 // mid-test.
47 base::CommandLine* command_line_; 50 base::CommandLine* command_line_;
48 51
49 // A basic extension with the debugger permission. 52 // A basic extension with the debugger permission.
50 scoped_refptr<const Extension> extension_; 53 scoped_refptr<const Extension> extension_;
51 }; 54 };
52 55
53 void DebuggerApiTest::SetUpCommandLine(base::CommandLine* command_line) { 56 void DebuggerApiTest::SetUpCommandLine(base::CommandLine* command_line) {
54 ExtensionApiTest::SetUpCommandLine(command_line); 57 ExtensionApiTest::SetUpCommandLine(command_line);
(...skipping 10 matching lines...) Expand all
65 .Set("manifest_version", 2) 68 .Set("manifest_version", 2)
66 .Set("permissions", 69 .Set("permissions",
67 ListBuilder().Append("debugger"))).Build(); 70 ListBuilder().Append("debugger"))).Build();
68 } 71 }
69 72
70 testing::AssertionResult DebuggerApiTest::RunAttachFunction( 73 testing::AssertionResult DebuggerApiTest::RunAttachFunction(
71 const GURL& url, const std::string& expected_error) { 74 const GURL& url, const std::string& expected_error) {
72 ui_test_utils::NavigateToURL(browser(), url); 75 ui_test_utils::NavigateToURL(browser(), url);
73 content::WebContents* web_contents = 76 content::WebContents* web_contents =
74 browser()->tab_strip_model()->GetActiveWebContents(); 77 browser()->tab_strip_model()->GetActiveWebContents();
78
79 // Attach by tabId.
75 int tab_id = SessionTabHelper::IdForTab(web_contents); 80 int tab_id = SessionTabHelper::IdForTab(web_contents);
81 std::string debugee_by_tab = base::StringPrintf("{\"tabId\": %d}", tab_id);
82 testing::AssertionResult result =
83 RunAttachFunctionOnTarget(debugee_by_tab, expected_error);
84 if (!result)
85 return result;
86
87 // Attach by targetId.
88 scoped_refptr<DebuggerGetTargetsFunction> get_targets_function =
89 new DebuggerGetTargetsFunction();
90 scoped_ptr<base::Value> value(
91 extension_function_test_utils::RunFunctionAndReturnSingleResult(
92 get_targets_function.get(), "[]", browser()));
93 base::ListValue* targets = nullptr;
94 EXPECT_TRUE(value->GetAsList(&targets));
95
96 std::string debugger_target_id;
97 for (size_t i = 0; i < targets->GetSize(); ++i) {
98 base::DictionaryValue* target_dict = nullptr;
99 EXPECT_TRUE(targets->GetDictionary(i, &target_dict));
100 int id = -1;
101 if (target_dict->GetInteger("tabId", &id) && id == tab_id) {
102 EXPECT_TRUE(target_dict->GetString("id", &debugger_target_id));
103 break;
104 }
105 }
106 EXPECT_TRUE(!debugger_target_id.empty());
107
108 std::string debugee_by_target_id =
109 base::StringPrintf("{\"targetId\": \"%s\"}", debugger_target_id.c_str());
110 return RunAttachFunctionOnTarget(debugee_by_target_id, expected_error);
111 }
112
113 testing::AssertionResult DebuggerApiTest::RunAttachFunctionOnTarget(
114 const std::string& debuggee_target, const std::string& expected_error) {
76 scoped_refptr<DebuggerAttachFunction> attach_function = 115 scoped_refptr<DebuggerAttachFunction> attach_function =
77 new DebuggerAttachFunction(); 116 new DebuggerAttachFunction();
78 attach_function->set_extension(extension_.get()); 117 attach_function->set_extension(extension_.get());
79 std::string args = base::StringPrintf("[{\"tabId\": %d}, \"1.1\"]", tab_id);
80 118
81 if (!expected_error.empty()) { 119 std::string actual_error;
82 std::string actual_error = 120 if (!RunFunction(attach_function.get(),
83 extension_function_test_utils::RunFunctionAndReturnError( 121 base::StringPrintf("[%s, \"1.1\"]", debuggee_target.c_str()),
84 attach_function.get(), args, browser()); 122 browser(),
85 if (actual_error != expected_error) { 123 extension_function_test_utils::NONE)) {
86 return testing::AssertionFailure() << "Did not get correct error: " 124 actual_error = attach_function->GetError();
87 << "expected: " << expected_error << ", found: " << actual_error;
88 }
89 } else { 125 } else {
90 if (!RunFunction(attach_function.get(),
91 args,
92 browser(),
93 extension_function_test_utils::NONE)) {
94 return testing::AssertionFailure() << "Could not run function: "
95 << attach_function->GetError();
96 }
97
98 // Clean up and detach. 126 // Clean up and detach.
99 scoped_refptr<DebuggerDetachFunction> detach_function = 127 scoped_refptr<DebuggerDetachFunction> detach_function =
100 new DebuggerDetachFunction(); 128 new DebuggerDetachFunction();
101 detach_function->set_extension(extension_.get()); 129 detach_function->set_extension(extension_.get());
102 if (!RunFunction(detach_function.get(), 130 if (!RunFunction(detach_function.get(),
103 base::StringPrintf("[{\"tabId\": %d}]", tab_id), 131 base::StringPrintf("[%s]", debuggee_target.c_str()),
104 browser(), 132 browser(),
105 extension_function_test_utils::NONE)) { 133 extension_function_test_utils::NONE)) {
106 return testing::AssertionFailure() << "Could not detach: " 134 return testing::AssertionFailure() << "Could not detach from "
107 << detach_function->GetError(); 135 << debuggee_target << " : " << detach_function->GetError();
108 } 136 }
109 } 137 }
138
139 if (expected_error.empty() && !actual_error.empty()) {
140 return testing::AssertionFailure() << "Could not attach to "
141 << debuggee_target << " : " << actual_error;
142 } else if (actual_error != expected_error) {
143 return testing::AssertionFailure() << "Did not get correct error upon "
144 << "attach to " << debuggee_target << " : "
145 << "expected: " << expected_error << ", found: " << actual_error;
146 }
110 return testing::AssertionSuccess(); 147 return testing::AssertionSuccess();
111 } 148 }
112 149
113 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Debugger) { 150 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Debugger) {
114 ASSERT_TRUE(RunExtensionTest("debugger")) << message_; 151 ASSERT_TRUE(RunExtensionTest("debugger")) << message_;
115 } 152 }
116 153
117 IN_PROC_BROWSER_TEST_F(DebuggerApiTest, 154 IN_PROC_BROWSER_TEST_F(DebuggerApiTest,
118 DebuggerNotAllowedOnOtherExtensionPages) { 155 DebuggerNotAllowedOnOtherExtensionPages) {
119 // Load another arbitrary extension with an associated resource (popup.html). 156 // Load another arbitrary extension with an associated resource (popup.html).
(...skipping 17 matching lines...) Expand all
137 extension()->id().c_str())), 174 extension()->id().c_str())),
138 std::string())); 175 std::string()));
139 176
140 // Append extensions on chrome urls switch. The extension should now be able 177 // Append extensions on chrome urls switch. The extension should now be able
141 // to debug any extension. 178 // to debug any extension.
142 command_line()->AppendSwitch(switches::kExtensionsOnChromeURLs); 179 command_line()->AppendSwitch(switches::kExtensionsOnChromeURLs);
143 EXPECT_TRUE(RunAttachFunction(other_ext_url, std::string())); 180 EXPECT_TRUE(RunAttachFunction(other_ext_url, std::string()));
144 } 181 }
145 182
146 } // namespace extensions 183 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/debugger/debugger_api.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698