OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/test/chromedriver/util.h" | 5 #include "chrome/test/chromedriver/util.h" |
6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
7 #include "base/base64.h" | 10 #include "base/base64.h" |
8 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
9 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
11 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
12 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
13 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
14 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
15 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
17 #include "base/third_party/icu/icu_utf.h" | 20 #include "base/third_party/icu/icu_utf.h" |
18 #include "base/values.h" | 21 #include "base/values.h" |
19 #include "chrome/test/chromedriver/chrome/browser_info.h" | 22 #include "chrome/test/chromedriver/chrome/browser_info.h" |
20 #include "chrome/test/chromedriver/chrome/chrome.h" | 23 #include "chrome/test/chromedriver/chrome/chrome.h" |
21 #include "chrome/test/chromedriver/chrome/status.h" | 24 #include "chrome/test/chromedriver/chrome/status.h" |
22 #include "chrome/test/chromedriver/chrome/ui_events.h" | 25 #include "chrome/test/chromedriver/chrome/ui_events.h" |
23 #include "chrome/test/chromedriver/chrome/web_view.h" | 26 #include "chrome/test/chromedriver/chrome/web_view.h" |
24 #include "chrome/test/chromedriver/command_listener.h" | 27 #include "chrome/test/chromedriver/command_listener.h" |
25 #include "chrome/test/chromedriver/key_converter.h" | 28 #include "chrome/test/chromedriver/key_converter.h" |
26 #include "chrome/test/chromedriver/session.h" | 29 #include "chrome/test/chromedriver/session.h" |
27 #include "third_party/zlib/google/zip.h" | 30 #include "third_party/zlib/google/zip.h" |
28 | 31 |
29 std::string GenerateId() { | 32 std::string GenerateId() { |
30 uint64 msb = base::RandUint64(); | 33 uint64_t msb = base::RandUint64(); |
31 uint64 lsb = base::RandUint64(); | 34 uint64_t lsb = base::RandUint64(); |
32 return base::StringPrintf("%016" PRIx64 "%016" PRIx64, msb, lsb); | 35 return base::StringPrintf("%016" PRIx64 "%016" PRIx64, msb, lsb); |
33 } | 36 } |
34 | 37 |
35 namespace { | 38 namespace { |
36 | 39 |
37 Status FlattenStringArray(const base::ListValue* src, base::string16* dest) { | 40 Status FlattenStringArray(const base::ListValue* src, base::string16* dest) { |
38 base::string16 keys; | 41 base::string16 keys; |
39 for (size_t i = 0; i < src->GetSize(); ++i) { | 42 for (size_t i = 0; i < src->GetSize(); ++i) { |
40 base::string16 keys_list_part; | 43 base::string16 keys_list_part; |
41 if (!src->GetString(i, &keys_list_part)) | 44 if (!src->GetString(i, &keys_list_part)) |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 return Status(kUnknownError, "could not unzip archive"); | 105 return Status(kUnknownError, "could not unzip archive"); |
103 return Status(kOk); | 106 return Status(kOk); |
104 } | 107 } |
105 | 108 |
106 // Stream for writing binary data. | 109 // Stream for writing binary data. |
107 class DataOutputStream { | 110 class DataOutputStream { |
108 public: | 111 public: |
109 DataOutputStream() {} | 112 DataOutputStream() {} |
110 ~DataOutputStream() {} | 113 ~DataOutputStream() {} |
111 | 114 |
112 void WriteUInt16(uint16 data) { | 115 void WriteUInt16(uint16_t data) { WriteBytes(&data, sizeof(data)); } |
113 WriteBytes(&data, sizeof(data)); | |
114 } | |
115 | 116 |
116 void WriteUInt32(uint32 data) { | 117 void WriteUInt32(uint32_t data) { WriteBytes(&data, sizeof(data)); } |
117 WriteBytes(&data, sizeof(data)); | |
118 } | |
119 | 118 |
120 void WriteString(const std::string& data) { | 119 void WriteString(const std::string& data) { |
121 WriteBytes(data.c_str(), data.length()); | 120 WriteBytes(data.c_str(), data.length()); |
122 } | 121 } |
123 | 122 |
124 void WriteBytes(const void* bytes, int size) { | 123 void WriteBytes(const void* bytes, int size) { |
125 if (!size) | 124 if (!size) |
126 return; | 125 return; |
127 size_t next = buffer_.length(); | 126 size_t next = buffer_.length(); |
128 buffer_.resize(next + size); | 127 buffer_.resize(next + size); |
129 memcpy(&buffer_[next], bytes, size); | 128 memcpy(&buffer_[next], bytes, size); |
130 } | 129 } |
131 | 130 |
132 const std::string& buffer() const { return buffer_; } | 131 const std::string& buffer() const { return buffer_; } |
133 | 132 |
134 private: | 133 private: |
135 std::string buffer_; | 134 std::string buffer_; |
136 }; | 135 }; |
137 | 136 |
138 // Stream for reading binary data. | 137 // Stream for reading binary data. |
139 class DataInputStream { | 138 class DataInputStream { |
140 public: | 139 public: |
141 DataInputStream(const char* data, int size) | 140 DataInputStream(const char* data, int size) |
142 : data_(data), size_(size), iter_(0) {} | 141 : data_(data), size_(size), iter_(0) {} |
143 ~DataInputStream() {} | 142 ~DataInputStream() {} |
144 | 143 |
145 bool ReadUInt16(uint16* data) { | 144 bool ReadUInt16(uint16_t* data) { return ReadBytes(data, sizeof(*data)); } |
146 return ReadBytes(data, sizeof(*data)); | |
147 } | |
148 | 145 |
149 bool ReadUInt32(uint32* data) { | 146 bool ReadUInt32(uint32_t* data) { return ReadBytes(data, sizeof(*data)); } |
150 return ReadBytes(data, sizeof(*data)); | |
151 } | |
152 | 147 |
153 bool ReadString(std::string* data, int length) { | 148 bool ReadString(std::string* data, int length) { |
154 if (length < 0) | 149 if (length < 0) |
155 return false; | 150 return false; |
156 // Check here to make sure we don't allocate wastefully. | 151 // Check here to make sure we don't allocate wastefully. |
157 if (iter_ + length > size_) | 152 if (iter_ + length > size_) |
158 return false; | 153 return false; |
159 data->resize(length); | 154 data->resize(length); |
160 if (length == 0) | 155 if (length == 0) |
161 return true; | 156 return true; |
(...skipping 20 matching lines...) Expand all Loading... |
182 // guaranteed to be able to parse all types of zip entries. | 177 // guaranteed to be able to parse all types of zip entries. |
183 // See http://www.pkware.com/documents/casestudies/APPNOTE.TXT for the zip | 178 // See http://www.pkware.com/documents/casestudies/APPNOTE.TXT for the zip |
184 // file format. | 179 // file format. |
185 struct ZipEntry { | 180 struct ZipEntry { |
186 // The given bytes must contain the whole zip entry and only the entry, | 181 // The given bytes must contain the whole zip entry and only the entry, |
187 // although the entry may include a data descriptor. | 182 // although the entry may include a data descriptor. |
188 static bool FromBytes(const std::string& bytes, ZipEntry* zip, | 183 static bool FromBytes(const std::string& bytes, ZipEntry* zip, |
189 std::string* error_msg) { | 184 std::string* error_msg) { |
190 DataInputStream stream(bytes.c_str(), bytes.length()); | 185 DataInputStream stream(bytes.c_str(), bytes.length()); |
191 | 186 |
192 uint32 signature; | 187 uint32_t signature; |
193 if (!stream.ReadUInt32(&signature) || signature != kFileHeaderSignature) { | 188 if (!stream.ReadUInt32(&signature) || signature != kFileHeaderSignature) { |
194 *error_msg = "invalid file header signature"; | 189 *error_msg = "invalid file header signature"; |
195 return false; | 190 return false; |
196 } | 191 } |
197 if (!stream.ReadUInt16(&zip->version_needed)) { | 192 if (!stream.ReadUInt16(&zip->version_needed)) { |
198 *error_msg = "invalid version"; | 193 *error_msg = "invalid version"; |
199 return false; | 194 return false; |
200 } | 195 } |
201 if (!stream.ReadUInt16(&zip->bit_flag)) { | 196 if (!stream.ReadUInt16(&zip->bit_flag)) { |
202 *error_msg = "invalid bit flag"; | 197 *error_msg = "invalid bit flag"; |
203 return false; | 198 return false; |
204 } | 199 } |
205 if (!stream.ReadUInt16(&zip->compression_method)) { | 200 if (!stream.ReadUInt16(&zip->compression_method)) { |
206 *error_msg = "invalid compression method"; | 201 *error_msg = "invalid compression method"; |
207 return false; | 202 return false; |
208 } | 203 } |
209 if (!stream.ReadUInt16(&zip->mod_time)) { | 204 if (!stream.ReadUInt16(&zip->mod_time)) { |
210 *error_msg = "invalid file last modified time"; | 205 *error_msg = "invalid file last modified time"; |
211 return false; | 206 return false; |
212 } | 207 } |
213 if (!stream.ReadUInt16(&zip->mod_date)) { | 208 if (!stream.ReadUInt16(&zip->mod_date)) { |
214 *error_msg = "invalid file last modified date"; | 209 *error_msg = "invalid file last modified date"; |
215 return false; | 210 return false; |
216 } | 211 } |
217 if (!stream.ReadUInt32(&zip->crc)) { | 212 if (!stream.ReadUInt32(&zip->crc)) { |
218 *error_msg = "invalid crc"; | 213 *error_msg = "invalid crc"; |
219 return false; | 214 return false; |
220 } | 215 } |
221 uint32 compressed_size; | 216 uint32_t compressed_size; |
222 if (!stream.ReadUInt32(&compressed_size)) { | 217 if (!stream.ReadUInt32(&compressed_size)) { |
223 *error_msg = "invalid compressed size"; | 218 *error_msg = "invalid compressed size"; |
224 return false; | 219 return false; |
225 } | 220 } |
226 if (!stream.ReadUInt32(&zip->uncompressed_size)) { | 221 if (!stream.ReadUInt32(&zip->uncompressed_size)) { |
227 *error_msg = "invalid compressed size"; | 222 *error_msg = "invalid compressed size"; |
228 return false; | 223 return false; |
229 } | 224 } |
230 uint16 name_length; | 225 uint16_t name_length; |
231 if (!stream.ReadUInt16(&name_length)) { | 226 if (!stream.ReadUInt16(&name_length)) { |
232 *error_msg = "invalid name length"; | 227 *error_msg = "invalid name length"; |
233 return false; | 228 return false; |
234 } | 229 } |
235 uint16 field_length; | 230 uint16_t field_length; |
236 if (!stream.ReadUInt16(&field_length)) { | 231 if (!stream.ReadUInt16(&field_length)) { |
237 *error_msg = "invalid field length"; | 232 *error_msg = "invalid field length"; |
238 return false; | 233 return false; |
239 } | 234 } |
240 if (!stream.ReadString(&zip->name, name_length)) { | 235 if (!stream.ReadString(&zip->name, name_length)) { |
241 *error_msg = "invalid name"; | 236 *error_msg = "invalid name"; |
242 return false; | 237 return false; |
243 } | 238 } |
244 if (!stream.ReadString(&zip->fields, field_length)) { | 239 if (!stream.ReadString(&zip->fields, field_length)) { |
245 *error_msg = "invalid fields"; | 240 *error_msg = "invalid fields"; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 stream.WriteUInt16(mod_time); | 297 stream.WriteUInt16(mod_time); |
303 stream.WriteUInt16(mod_date); | 298 stream.WriteUInt16(mod_date); |
304 stream.WriteUInt32(crc); | 299 stream.WriteUInt32(crc); |
305 stream.WriteUInt32(compressed_data.length()); | 300 stream.WriteUInt32(compressed_data.length()); |
306 stream.WriteUInt32(uncompressed_size); | 301 stream.WriteUInt32(uncompressed_size); |
307 stream.WriteUInt16(name.length()); | 302 stream.WriteUInt16(name.length()); |
308 stream.WriteUInt16(fields.length()); | 303 stream.WriteUInt16(fields.length()); |
309 stream.WriteString(name); | 304 stream.WriteString(name); |
310 stream.WriteString(fields); | 305 stream.WriteString(fields); |
311 stream.WriteString(compressed_data); | 306 stream.WriteString(compressed_data); |
312 uint32 entry_size = stream.buffer().length(); | 307 uint32_t entry_size = stream.buffer().length(); |
313 | 308 |
314 // Write central directory. | 309 // Write central directory. |
315 stream.WriteUInt32(kCentralDirSignature); | 310 stream.WriteUInt32(kCentralDirSignature); |
316 stream.WriteUInt16(0x14); // Version made by. Unused at version 0. | 311 stream.WriteUInt16(0x14); // Version made by. Unused at version 0. |
317 stream.WriteUInt16(version_needed); | 312 stream.WriteUInt16(version_needed); |
318 stream.WriteUInt16(bit_flag); | 313 stream.WriteUInt16(bit_flag); |
319 stream.WriteUInt16(compression_method); | 314 stream.WriteUInt16(compression_method); |
320 stream.WriteUInt16(mod_time); | 315 stream.WriteUInt16(mod_time); |
321 stream.WriteUInt16(mod_date); | 316 stream.WriteUInt16(mod_date); |
322 stream.WriteUInt32(crc); | 317 stream.WriteUInt32(crc); |
323 stream.WriteUInt32(compressed_data.length()); | 318 stream.WriteUInt32(compressed_data.length()); |
324 stream.WriteUInt32(uncompressed_size); | 319 stream.WriteUInt32(uncompressed_size); |
325 stream.WriteUInt16(name.length()); | 320 stream.WriteUInt16(name.length()); |
326 stream.WriteUInt16(fields.length()); | 321 stream.WriteUInt16(fields.length()); |
327 stream.WriteUInt16(0); // Comment length. | 322 stream.WriteUInt16(0); // Comment length. |
328 stream.WriteUInt16(0); // Disk number where file starts. | 323 stream.WriteUInt16(0); // Disk number where file starts. |
329 stream.WriteUInt16(0); // Internal file attr. | 324 stream.WriteUInt16(0); // Internal file attr. |
330 stream.WriteUInt32(0); // External file attr. | 325 stream.WriteUInt32(0); // External file attr. |
331 stream.WriteUInt32(0); // Offset to file. | 326 stream.WriteUInt32(0); // Offset to file. |
332 stream.WriteString(name); | 327 stream.WriteString(name); |
333 stream.WriteString(fields); | 328 stream.WriteString(fields); |
334 uint32 cd_size = stream.buffer().length() - entry_size; | 329 uint32_t cd_size = stream.buffer().length() - entry_size; |
335 | 330 |
336 // End of central directory. | 331 // End of central directory. |
337 stream.WriteUInt32(kEndOfCentralDirSignature); | 332 stream.WriteUInt32(kEndOfCentralDirSignature); |
338 stream.WriteUInt16(0); // num of this disk | 333 stream.WriteUInt16(0); // num of this disk |
339 stream.WriteUInt16(0); // disk where cd starts | 334 stream.WriteUInt16(0); // disk where cd starts |
340 stream.WriteUInt16(1); // number of cds on this disk | 335 stream.WriteUInt16(1); // number of cds on this disk |
341 stream.WriteUInt16(1); // total cds | 336 stream.WriteUInt16(1); // total cds |
342 stream.WriteUInt32(cd_size); // size of cd | 337 stream.WriteUInt32(cd_size); // size of cd |
343 stream.WriteUInt32(entry_size); // offset of cd | 338 stream.WriteUInt32(entry_size); // offset of cd |
344 stream.WriteUInt16(0); // comment len | 339 stream.WriteUInt16(0); // comment len |
345 | 340 |
346 return stream.buffer(); | 341 return stream.buffer(); |
347 } | 342 } |
348 | 343 |
349 static const uint32 kFileHeaderSignature; | 344 static const uint32_t kFileHeaderSignature; |
350 static const uint32 kDataDescriptorSignature; | 345 static const uint32_t kDataDescriptorSignature; |
351 static const uint32 kCentralDirSignature; | 346 static const uint32_t kCentralDirSignature; |
352 static const uint32 kEndOfCentralDirSignature; | 347 static const uint32_t kEndOfCentralDirSignature; |
353 uint16 version_needed; | 348 uint16_t version_needed; |
354 uint16 bit_flag; | 349 uint16_t bit_flag; |
355 uint16 compression_method; | 350 uint16_t compression_method; |
356 uint16 mod_time; | 351 uint16_t mod_time; |
357 uint16 mod_date; | 352 uint16_t mod_date; |
358 uint32 crc; | 353 uint32_t crc; |
359 uint32 uncompressed_size; | 354 uint32_t uncompressed_size; |
360 std::string name; | 355 std::string name; |
361 std::string fields; | 356 std::string fields; |
362 std::string compressed_data; | 357 std::string compressed_data; |
363 }; | 358 }; |
364 | 359 |
365 const uint32 ZipEntry::kFileHeaderSignature = 0x04034b50; | 360 const uint32_t ZipEntry::kFileHeaderSignature = 0x04034b50; |
366 const uint32 ZipEntry::kDataDescriptorSignature = 0x08074b50; | 361 const uint32_t ZipEntry::kDataDescriptorSignature = 0x08074b50; |
367 const uint32 ZipEntry::kCentralDirSignature = 0x02014b50; | 362 const uint32_t ZipEntry::kCentralDirSignature = 0x02014b50; |
368 const uint32 ZipEntry::kEndOfCentralDirSignature = 0x06054b50; | 363 const uint32_t ZipEntry::kEndOfCentralDirSignature = 0x06054b50; |
369 | 364 |
370 Status UnzipEntry(const base::FilePath& unzip_dir, | 365 Status UnzipEntry(const base::FilePath& unzip_dir, |
371 const std::string& bytes) { | 366 const std::string& bytes) { |
372 ZipEntry entry; | 367 ZipEntry entry; |
373 std::string zip_error_msg; | 368 std::string zip_error_msg; |
374 if (!ZipEntry::FromBytes(bytes, &entry, &zip_error_msg)) | 369 if (!ZipEntry::FromBytes(bytes, &entry, &zip_error_msg)) |
375 return Status(kUnknownError, zip_error_msg); | 370 return Status(kUnknownError, zip_error_msg); |
376 std::string archive = entry.ToZip(); | 371 std::string archive = entry.ToZip(); |
377 return UnzipArchive(unzip_dir, archive); | 372 return UnzipArchive(unzip_dir, archive); |
378 } | 373 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 if (session->chrome) { | 427 if (session->chrome) { |
433 const BrowserInfo* browser_info = session->chrome->GetBrowserInfo(); | 428 const BrowserInfo* browser_info = session->chrome->GetBrowserInfo(); |
434 status.AddDetails("Session info: " + browser_info->browser_name + "=" + | 429 status.AddDetails("Session info: " + browser_info->browser_name + "=" + |
435 browser_info->browser_version); | 430 browser_info->browser_version); |
436 } | 431 } |
437 return status; | 432 return status; |
438 } | 433 } |
439 } | 434 } |
440 return Status(kOk); | 435 return Status(kOk); |
441 } | 436 } |
OLD | NEW |