OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/base64.h" | |
6 #include "base/file_util.h" | |
7 #include "base/files/file_path.h" | |
8 #include "base/files/scoped_temp_dir.h" | |
9 #include "base/values.h" | |
10 #include "chrome/common/chrome_switches.h" | |
11 #include "chrome/test/webdriver/webdriver_capabilities_parser.h" | |
12 #include "chrome/test/webdriver/webdriver_logging.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "third_party/zlib/google/zip.h" | |
15 | |
16 using base::DictionaryValue; | |
17 using base::ListValue; | |
18 using base::Value; | |
19 | |
20 namespace webdriver { | |
21 | |
22 TEST(CapabilitiesParser, NoCaps) { | |
23 Capabilities caps; | |
24 DictionaryValue dict; | |
25 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
26 ASSERT_FALSE(parser.Parse()); | |
27 } | |
28 | |
29 TEST(CapabilitiesParser, SimpleCaps) { | |
30 DictionaryValue dict; | |
31 DictionaryValue* options = new DictionaryValue(); | |
32 dict.Set("chromeOptions", options); | |
33 | |
34 options->SetString("binary", "binary"); | |
35 options->SetString("channel", "channel"); | |
36 options->SetBoolean("detach", true); | |
37 options->SetBoolean("loadAsync", true); | |
38 | |
39 Capabilities caps; | |
40 base::ScopedTempDir temp_dir; | |
41 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
42 CapabilitiesParser parser(&dict, temp_dir.path(), Logger(), &caps); | |
43 ASSERT_FALSE(parser.Parse()); | |
44 EXPECT_EQ(FILE_PATH_LITERAL("binary"), caps.command.GetProgram().value()); | |
45 EXPECT_STREQ("channel", caps.channel.c_str()); | |
46 EXPECT_TRUE(caps.detach); | |
47 EXPECT_TRUE(caps.load_async); | |
48 } | |
49 | |
50 TEST(CapabilitiesParser, Args) { | |
51 DictionaryValue dict; | |
52 DictionaryValue* options = new DictionaryValue(); | |
53 dict.Set("chromeOptions", options); | |
54 | |
55 ListValue* args = new ListValue(); | |
56 args->Append(new base::StringValue("arg1")); | |
57 args->Append(new base::StringValue("arg2=val")); | |
58 args->Append(new base::StringValue("arg3='a space'")); | |
59 options->Set("args", args); | |
60 | |
61 Capabilities caps; | |
62 base::ScopedTempDir temp_dir; | |
63 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
64 CapabilitiesParser parser(&dict, temp_dir.path(), Logger(), &caps); | |
65 ASSERT_FALSE(parser.Parse()); | |
66 EXPECT_TRUE(caps.command.HasSwitch("arg1")); | |
67 EXPECT_STREQ("val", caps.command.GetSwitchValueASCII("arg2").c_str()); | |
68 EXPECT_STREQ("'a space'", caps.command.GetSwitchValueASCII("arg3").c_str()); | |
69 } | |
70 | |
71 TEST(CapabilitiesParser, Extensions) { | |
72 DictionaryValue dict; | |
73 DictionaryValue* options = new DictionaryValue(); | |
74 dict.Set("chromeOptions", options); | |
75 | |
76 ListValue* extensions = new ListValue(); | |
77 extensions->Append(new base::StringValue("TWFu")); | |
78 extensions->Append(new base::StringValue("TWFuTWFu")); | |
79 options->Set("extensions", extensions); | |
80 | |
81 Capabilities caps; | |
82 base::ScopedTempDir temp_dir; | |
83 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
84 CapabilitiesParser parser(&dict, temp_dir.path(), Logger(), &caps); | |
85 ASSERT_FALSE(parser.Parse()); | |
86 ASSERT_EQ(2u, caps.extensions.size()); | |
87 std::string contents; | |
88 ASSERT_TRUE(base::ReadFileToString(caps.extensions[0], &contents)); | |
89 EXPECT_STREQ("Man", contents.c_str()); | |
90 contents.clear(); | |
91 ASSERT_TRUE(base::ReadFileToString(caps.extensions[1], &contents)); | |
92 EXPECT_STREQ("ManMan", contents.c_str()); | |
93 } | |
94 | |
95 TEST(CapabilitiesParser, Profile) { | |
96 DictionaryValue dict; | |
97 DictionaryValue* options = new DictionaryValue(); | |
98 dict.Set("chromeOptions", options); | |
99 | |
100 base::ScopedTempDir temp_dir; | |
101 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
102 base::FilePath folder = temp_dir.path().AppendASCII("folder"); | |
103 ASSERT_TRUE(file_util::CreateDirectory(folder)); | |
104 ASSERT_EQ(4, file_util::WriteFile( | |
105 folder.AppendASCII("data"), "data", 4)); | |
106 base::FilePath zip = temp_dir.path().AppendASCII("data.zip"); | |
107 ASSERT_TRUE(zip::Zip(folder, zip, false /* include_hidden_files */)); | |
108 std::string contents; | |
109 ASSERT_TRUE(base::ReadFileToString(zip, &contents)); | |
110 std::string base64; | |
111 ASSERT_TRUE(base::Base64Encode(contents, &base64)); | |
112 options->SetString("profile", base64); | |
113 | |
114 Capabilities caps; | |
115 CapabilitiesParser parser(&dict, temp_dir.path(), Logger(), &caps); | |
116 ASSERT_FALSE(parser.Parse()); | |
117 std::string new_contents; | |
118 ASSERT_TRUE(base::ReadFileToString( | |
119 caps.profile.AppendASCII("data"), &new_contents)); | |
120 EXPECT_STREQ("data", new_contents.c_str()); | |
121 } | |
122 | |
123 TEST(CapabilitiesParser, UnknownCap) { | |
124 Capabilities caps; | |
125 DictionaryValue dict; | |
126 dict.SetString("chromeOptions.nosuchcap", "none"); | |
127 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
128 ASSERT_TRUE(parser.Parse()); | |
129 } | |
130 | |
131 TEST(CapabilitiesParser, ProxyCap) { | |
132 Capabilities caps; | |
133 DictionaryValue dict; | |
134 DictionaryValue* options = new DictionaryValue(); | |
135 dict.Set("proxy", options); | |
136 | |
137 const char kPacUrl[] = "test.wpad"; | |
138 options->SetString("proxyType", "PAC"); | |
139 options->SetString("proxyAutoconfigUrl", kPacUrl); | |
140 | |
141 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
142 ASSERT_FALSE(parser.Parse()); | |
143 EXPECT_STREQ(kPacUrl, | |
144 caps.command.GetSwitchValueASCII(switches::kProxyPacUrl).c_str()); | |
145 } | |
146 | |
147 TEST(CapabilitiesParser, ProxyTypeCapIncompatiblePac) { | |
148 Capabilities caps; | |
149 DictionaryValue dict; | |
150 DictionaryValue* options = new DictionaryValue(); | |
151 dict.Set("proxy", options); | |
152 | |
153 options->SetString("proxyType", "pac"); | |
154 options->SetString("httpProxy", "http://localhost:8001"); | |
155 | |
156 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
157 ASSERT_TRUE(parser.Parse()); | |
158 } | |
159 | |
160 TEST(CapabilitiesParser, ProxyTypeCapIncompatibleManual) { | |
161 Capabilities caps; | |
162 DictionaryValue dict; | |
163 DictionaryValue* options = new DictionaryValue(); | |
164 dict.Set("proxy", options); | |
165 | |
166 options->SetString("proxyType", "manual"); | |
167 | |
168 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
169 ASSERT_TRUE(parser.Parse()); | |
170 } | |
171 | |
172 TEST(CapabilitiesParser, ProxyTypeCapNullValue) { | |
173 Capabilities caps; | |
174 DictionaryValue dict; | |
175 DictionaryValue* options = new DictionaryValue(); | |
176 dict.Set("proxy", options); | |
177 | |
178 options->Set("proxyType", base::Value::CreateNullValue()); | |
179 | |
180 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
181 ASSERT_TRUE(parser.Parse()); | |
182 } | |
183 | |
184 TEST(CapabilitiesParser, ProxyTypeManualCap) { | |
185 const char kProxyServers[] = "ftp=localhost:9001;http=localhost:8001"; | |
186 Capabilities caps; | |
187 DictionaryValue dict; | |
188 DictionaryValue* options = new DictionaryValue(); | |
189 dict.Set("proxy", options); | |
190 | |
191 options->SetString("proxyType", "manual"); | |
192 options->SetString("httpProxy", "localhost:8001"); | |
193 options->SetString("ftpProxy", "localhost:9001"); | |
194 | |
195 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
196 ASSERT_FALSE(parser.Parse()); | |
197 EXPECT_STREQ(kProxyServers, | |
198 caps.command.GetSwitchValueASCII(switches::kProxyServer).c_str()); | |
199 } | |
200 | |
201 TEST(CapabilitiesParser, ProxyBypassListCap) { | |
202 const char kBypassList[] = "google.com, youtube.com"; | |
203 Capabilities caps; | |
204 DictionaryValue dict; | |
205 DictionaryValue* options = new DictionaryValue(); | |
206 dict.Set("proxy", options); | |
207 | |
208 options->SetString("proxyType", "manual"); | |
209 options->SetString("noProxy", kBypassList); | |
210 | |
211 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
212 ASSERT_FALSE(parser.Parse()); | |
213 EXPECT_STREQ(kBypassList, | |
214 caps.command.GetSwitchValueASCII(switches::kProxyBypassList).c_str()); | |
215 } | |
216 | |
217 TEST(CapabilitiesParser, ProxyBypassListCapNullValue) { | |
218 Capabilities caps; | |
219 DictionaryValue dict; | |
220 DictionaryValue* options = new DictionaryValue(); | |
221 dict.Set("proxy", options); | |
222 | |
223 options->SetString("proxyType", "manual"); | |
224 options->Set("noProxy", base::Value::CreateNullValue()); | |
225 options->SetString("httpProxy", "localhost:8001"); | |
226 | |
227 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
228 ASSERT_FALSE(parser.Parse()); | |
229 EXPECT_FALSE(caps.command.HasSwitch(switches::kProxyBypassList)); | |
230 } | |
231 | |
232 TEST(CapabilitiesParser, UnknownProxyCap) { | |
233 Capabilities caps; | |
234 DictionaryValue dict; | |
235 DictionaryValue* options = new DictionaryValue(); | |
236 dict.Set("proxy", options); | |
237 | |
238 options->SetString("proxyType", "DIRECT"); | |
239 options->SetString("badProxyCap", "error"); | |
240 | |
241 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
242 ASSERT_FALSE(parser.Parse()); | |
243 } | |
244 | |
245 TEST(CapabilitiesParser, ProxyFtpServerCapNullValue) { | |
246 Capabilities caps; | |
247 DictionaryValue dict; | |
248 DictionaryValue* options = new DictionaryValue(); | |
249 dict.Set("proxy", options); | |
250 | |
251 options->SetString("proxyType", "manual"); | |
252 options->SetString("httpProxy", "localhost:8001"); | |
253 options->Set("ftpProxy", base::Value::CreateNullValue()); | |
254 | |
255 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
256 ASSERT_FALSE(parser.Parse()); | |
257 EXPECT_STREQ("http=localhost:8001", | |
258 caps.command.GetSwitchValueASCII(switches::kProxyServer).c_str()); | |
259 } | |
260 | |
261 TEST(CapabilitiesParser, DriverLoggingCapString) { | |
262 Capabilities caps; | |
263 DictionaryValue dict; | |
264 DictionaryValue* options = new DictionaryValue(); | |
265 dict.Set("loggingPrefs", options); | |
266 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
267 | |
268 // A string as the driver logging level works. | |
269 options->SetString("driver", "INFO"); | |
270 ASSERT_FALSE(parser.Parse()); | |
271 | |
272 // An integer (here, an enum LogLevel value) doesn't work. | |
273 options->SetInteger("driver", kInfoLogLevel); | |
274 ASSERT_TRUE(parser.Parse()); | |
275 } | |
276 | |
277 TEST(CapabilitiesParser, ExcludeSwitches) { | |
278 DictionaryValue dict; | |
279 DictionaryValue* options = new DictionaryValue(); | |
280 dict.Set("chromeOptions", options); | |
281 | |
282 ListValue* switches = new ListValue(); | |
283 switches->Append(new base::StringValue(switches::kNoFirstRun)); | |
284 switches->Append(new base::StringValue(switches::kDisableSync)); | |
285 switches->Append(new base::StringValue(switches::kDisableTranslate)); | |
286 options->Set("excludeSwitches", switches); | |
287 | |
288 Capabilities caps; | |
289 CapabilitiesParser parser(&dict, base::FilePath(), Logger(), &caps); | |
290 ASSERT_FALSE(parser.Parse()); | |
291 | |
292 const std::set<std::string>& rm_set = caps.exclude_switches; | |
293 EXPECT_EQ(static_cast<size_t>(3), rm_set.size()); | |
294 ASSERT_TRUE(rm_set.find(switches::kNoFirstRun) != rm_set.end()); | |
295 ASSERT_TRUE(rm_set.find(switches::kDisableSync) != rm_set.end()); | |
296 ASSERT_TRUE(rm_set.find(switches::kDisableTranslate) != rm_set.end()); | |
297 } | |
298 | |
299 } // namespace webdriver | |
OLD | NEW |