Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import filecmp | 6 import filecmp |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import shutil | 9 import shutil |
| 10 import sys | 10 import sys |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 """Prepare for downloading the given path. | 54 """Prepare for downloading the given path. |
| 55 | 55 |
| 56 Clears the given path and the corresponding .crdownload, to prepare it to | 56 Clears the given path and the corresponding .crdownload, to prepare it to |
| 57 be downloaded. | 57 be downloaded. |
| 58 """ | 58 """ |
| 59 os.path.exists(path) and os.remove(path) | 59 os.path.exists(path) and os.remove(path) |
| 60 crdownload = path + '.crdownload' | 60 crdownload = path + '.crdownload' |
| 61 os.path.exists(crdownload) and os.remove(crdownload) | 61 os.path.exists(crdownload) and os.remove(crdownload) |
| 62 | 62 |
| 63 def _GetDangerousDownload(self): | 63 def _GetDangerousDownload(self): |
| 64 """Returns the file url for a dangerous download for this OS.""" | 64 """Returns the file path for a dangerous download for this OS.""" |
| 65 sub_path = os.path.join(self.DataDir(), 'downloads', 'dangerous') | 65 sub_path = os.path.join(self.DataDir(), 'downloads', 'dangerous') |
| 66 if self.IsMac(): | 66 if self.IsMac(): |
| 67 return os.path.join(sub_path, 'invalid-dummy.dmg') | 67 return os.path.join(sub_path, 'invalid-dummy.dmg') |
| 68 return os.path.join(sub_path, 'dangerous.exe') | 68 return os.path.join(sub_path, 'dangerous.exe') |
| 69 | 69 |
| 70 def _EqualFileContents(self, file1, file2): | 70 def _EqualFileContents(self, file1, file2): |
| 71 """Determine if 2 given files have the same contents.""" | 71 """Determine if 2 given files have the same contents.""" |
| 72 if not (os.path.exists(file1) and os.path.exists(file2)): | 72 if not (os.path.exists(file1) and os.path.exists(file2)): |
| 73 return False | 73 return False |
| 74 return filecmp.cmp(file1, file2, shallow=False) | 74 return filecmp.cmp(file1, file2, shallow=False) |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 # since the download manager's list doesn't contain it. | 151 # since the download manager's list doesn't contain it. |
| 152 # Using WaitUntil is the only resort. | 152 # Using WaitUntil is the only resort. |
| 153 self.NavigateToURL(file_url, 1, 0) | 153 self.NavigateToURL(file_url, 1, 0) |
| 154 self.assertTrue(self.WaitUntil(lambda: os.path.exists(downloaded_pkg))) | 154 self.assertTrue(self.WaitUntil(lambda: os.path.exists(downloaded_pkg))) |
| 155 self.assertTrue(self._EqualFileContents(file_path, downloaded_pkg)) | 155 self.assertTrue(self._EqualFileContents(file_path, downloaded_pkg)) |
| 156 self.assertTrue(self.IsDownloadShelfVisible(1)) | 156 self.assertTrue(self.IsDownloadShelfVisible(1)) |
| 157 | 157 |
| 158 def testSaveDangerousFile(self): | 158 def testSaveDangerousFile(self): |
| 159 """Verify that we can download and save a dangerous file.""" | 159 """Verify that we can download and save a dangerous file.""" |
| 160 file_path = self._GetDangerousDownload() | 160 file_path = self._GetDangerousDownload() |
| 161 file_url = self.GetFileURLForPath(file_path) | |
| 162 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), | 161 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), |
| 163 os.path.basename(file_path)) | 162 os.path.basename(file_path)) |
| 164 self._ClearLocalDownloadState(downloaded_pkg) | 163 self._ClearLocalDownloadState(downloaded_pkg) |
| 165 | 164 self._TriggerUnsafeDownload(os.path.basename(file_path)) |
| 166 self.DownloadAndWaitForStart(file_url) | |
| 167 self.PerformActionOnDownload(self._GetDownloadId(), | 165 self.PerformActionOnDownload(self._GetDownloadId(), |
| 168 'save_dangerous_download') | 166 'save_dangerous_download') |
| 169 self.WaitForDownloadToComplete(downloaded_pkg) | 167 self.WaitForDownloadToComplete(downloaded_pkg) |
| 170 | 168 |
| 171 # Verify that the file was downloaded. | 169 # Verify that the file was downloaded. |
| 172 self.assertTrue(os.path.exists(downloaded_pkg)) | 170 self.assertTrue(os.path.exists(downloaded_pkg)) |
| 173 self.assertTrue(self._EqualFileContents(file_path, downloaded_pkg)) | 171 self.assertTrue(self._EqualFileContents(file_path, downloaded_pkg)) |
| 174 self._DeleteAfterShutdown(downloaded_pkg) | 172 self._DeleteAfterShutdown(downloaded_pkg) |
| 175 | 173 |
| 176 def testDeclineDangerousDownload(self): | 174 def testDeclineDangerousDownload(self): |
| 177 """Verify that we can decline dangerous downloads""" | 175 """Verify that we can decline dangerous downloads""" |
| 178 file_path = self._GetDangerousDownload() | 176 file_path = self._GetDangerousDownload() |
| 179 file_url = self.GetFileURLForPath(file_path) | |
| 180 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), | 177 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), |
| 181 os.path.basename(file_path)) | 178 os.path.basename(file_path)) |
| 182 self._ClearLocalDownloadState(downloaded_pkg) | 179 self._ClearLocalDownloadState(downloaded_pkg) |
| 183 | 180 self._TriggerUnsafeDownload(os.path.basename(file_path)) |
| 184 self.DownloadAndWaitForStart(file_url) | |
| 185 self.PerformActionOnDownload(self._GetDownloadId(), | 181 self.PerformActionOnDownload(self._GetDownloadId(), |
| 186 'decline_dangerous_download') | 182 'decline_dangerous_download') |
| 187 self.assertFalse(os.path.exists(downloaded_pkg)) | 183 self.assertFalse(os.path.exists(downloaded_pkg)) |
| 188 self.assertFalse(self.GetDownloadsInfo().Downloads()) | 184 self.assertFalse(self.GetDownloadsInfo().Downloads()) |
| 189 self.assertFalse(self.IsDownloadShelfVisible()) | 185 self.assertFalse(self.IsDownloadShelfVisible()) |
| 190 | 186 |
| 191 def testRemoveDownload(self): | 187 def testRemoveDownload(self): |
| 192 """Verify that we can remove a download.""" | 188 """Verify that we can remove a download.""" |
| 193 test_dir = os.path.join(os.path.abspath(self.DataDir()), 'downloads') | 189 test_dir = os.path.join(os.path.abspath(self.DataDir()), 'downloads') |
| 194 file_path = os.path.join(test_dir, 'a_zip_file.zip') | 190 file_path = os.path.join(test_dir, 'a_zip_file.zip') |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 self.assertEqual(len(downloads), len(crazy_filenames)) | 296 self.assertEqual(len(downloads), len(crazy_filenames)) |
| 301 | 297 |
| 302 for filename in crazy_filenames: | 298 for filename in crazy_filenames: |
| 303 downloaded_file = os.path.join(download_dir, filename) | 299 downloaded_file = os.path.join(download_dir, filename) |
| 304 self.assertTrue(os.path.exists(downloaded_file)) | 300 self.assertTrue(os.path.exists(downloaded_file)) |
| 305 self.assertTrue( # Verify file contents. | 301 self.assertTrue( # Verify file contents. |
| 306 self._EqualFileContents(downloaded_file, | 302 self._EqualFileContents(downloaded_file, |
| 307 os.path.join(temp_dir, filename))) | 303 os.path.join(temp_dir, filename))) |
| 308 os.path.exists(downloaded_file) and os.remove(downloaded_file) | 304 os.path.exists(downloaded_file) and os.remove(downloaded_file) |
| 309 | 305 |
| 306 def _TriggerUnsafeDownload(self, filename, tab_index=0, windex=0): | |
| 307 """Trigger download of an unsafe/dangerous filetype. | |
| 308 | |
| 309 Files explictly requested by the user (like navigating to a package, or | |
| 310 clicking on a link) aren't marked unsafe. | |
| 311 Only the ones where the user didn't directly initiate a download are | |
| 312 marked unsafe. | |
| 313 | |
| 314 Navigates to download-dangerous.html which triggers the download. | |
|
kkania
2010/12/14 01:14:48
How about navigating to an already existing file:/
Nirnimesh
2010/12/14 01:17:20
I think the current approach better mirrors the us
| |
| 315 Waits until the download starts. | |
| 316 | |
| 317 Args: | |
| 318 filename: the name of the file to trigger the download. | |
| 319 This should exist in the 'dangerous' directory. | |
| 320 tab_index: tab index. Default 0. | |
| 321 windex: window index. Default 0. | |
| 322 """ | |
| 323 dangerous_dir = os.path.join( | |
| 324 self.DataDir(), 'downloads', 'dangerous') | |
| 325 assert os.path.isfile(os.path.join(dangerous_dir, filename)) | |
| 326 file_url = self.GetFileURLForDataPath(os.path.join( | |
| 327 dangerous_dir, 'download-dangerous.html')) + '?' + filename | |
| 328 num_downloads = len(self.GetDownloadsInfo().Downloads()) | |
| 329 self.NavigateToURL(file_url, windex, tab_index) | |
| 330 # It might take a while for the download to kick in, hold on until then. | |
| 331 self.assertTrue(self.WaitUntil( | |
| 332 lambda: len(self.GetDownloadsInfo().Downloads()) == num_downloads + 1)) | |
| 333 | |
| 310 def testNoUnsafeDownloadsOnRestart(self): | 334 def testNoUnsafeDownloadsOnRestart(self): |
| 311 """Verify that unsafe file should not show up on session restart.""" | 335 """Verify that unsafe file should not show up on session restart.""" |
| 312 file_path = self._GetDangerousDownload() | 336 file_path = self._GetDangerousDownload() |
| 313 file_url = self.GetFileURLForPath(file_path) | |
| 314 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), | 337 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), |
| 315 os.path.basename(file_path)) | 338 os.path.basename(file_path)) |
| 316 self._ClearLocalDownloadState(downloaded_pkg) | 339 self._ClearLocalDownloadState(downloaded_pkg) |
| 317 self.DownloadAndWaitForStart(file_url) | 340 self._TriggerUnsafeDownload(os.path.basename(file_path)) |
| 318 self.assertTrue(self.IsDownloadShelfVisible()) | 341 self.assertTrue(self.IsDownloadShelfVisible()) |
| 319 # Restart the browser and assert that the download was removed. | 342 # Restart the browser and assert that the download was removed. |
| 320 self.RestartBrowser(clear_profile=False) | 343 self.RestartBrowser(clear_profile=False) |
| 321 self.assertFalse(os.path.exists(downloaded_pkg)) | 344 self.assertFalse(os.path.exists(downloaded_pkg)) |
| 322 self.assertFalse(self.IsDownloadShelfVisible()) | 345 self.assertFalse(self.IsDownloadShelfVisible()) |
| 323 self.NavigateToURL("chrome://downloads") | 346 self.NavigateToURL("chrome://downloads") |
| 324 self.assertFalse(self.GetDownloadsInfo().Downloads()) | 347 self.assertFalse(self.GetDownloadsInfo().Downloads()) |
| 325 | 348 |
| 326 def testPauseAndResume(self): | 349 def testPauseAndResume(self): |
| 327 """Verify that pause and resume work while downloading a file. | 350 """Verify that pause and resume work while downloading a file. |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 428 self.PerformActionOnDownload(self._GetDownloadId(), | 451 self.PerformActionOnDownload(self._GetDownloadId(), |
| 429 'save_dangerous_download') | 452 'save_dangerous_download') |
| 430 # Wait for the theme to be set automatically. | 453 # Wait for the theme to be set automatically. |
| 431 self.assertTrue(self.WaitUntilDownloadedThemeSet('camo theme')) | 454 self.assertTrue(self.WaitUntilDownloadedThemeSet('camo theme')) |
| 432 self.assertTrue(self.WaitUntil(lambda path: not os.path.exists(path), | 455 self.assertTrue(self.WaitUntil(lambda path: not os.path.exists(path), |
| 433 args=[downloaded_pkg])) | 456 args=[downloaded_pkg])) |
| 434 | 457 |
| 435 | 458 |
| 436 if __name__ == '__main__': | 459 if __name__ == '__main__': |
| 437 pyauto_functional.Main() | 460 pyauto_functional.Main() |
| OLD | NEW |