Browse Source

Fix issues with paths containing .. in upload path (see PR 194 for discussion)

George S. Baugh 10 years ago
parent
commit
26888844e8
5 changed files with 21 additions and 8 deletions
  1. 3 0
      .gitignore
  2. 9 2
      lib/Selenium/Remote/Driver.pm
  3. 2 0
      t/01-driver.t
  4. 7 6
      t/mock-recordings/01-driver-mock-linux.json
  5. 0 0
      t/uploadTest

+ 3 - 0
.gitignore

@@ -10,3 +10,6 @@ cover_db
 Selenium-Remote-Driver-*
 .prove
 _prove
+#vim swapfiles
+*.swo
+*.swp

+ 9 - 2
lib/Selenium/Remote/Driver.pm

@@ -23,6 +23,8 @@ use Selenium::Remote::WebElement;
 use File::Spec::Functions ();
 use File::Basename ();
 use Sub::Install ();
+use Cwd ();
+use MIME::Base64 ();
 
 use constant FINDERS => {
     class             => 'class name',
@@ -1417,7 +1419,6 @@ sub capture_screenshot {
     my ( $self, $filename ) = @_;
     croak '$filename is required' unless $filename;
 
-    require MIME::Base64;
     open( my $fh, '>', $filename );
     binmode $fh;
     print $fh MIME::Base64::decode_base64( $self->screenshot() );
@@ -2437,6 +2438,10 @@ sub button_up {
     Passing raw data as an argument past the filename will upload
     that rather than the file's contents.
 
+    When passing raw data, be advised that it expects a zipped
+    and then base64 encoded version of a single file.
+    Multiple files are not supported by the remote server.
+
  Usage:
     my $remote_fname = $driver->upload_file( $fname );
     my $element = $driver->find_element( '//input[@id="file"]' );
@@ -2455,6 +2460,9 @@ sub upload_file {
         file => $raw_content
     };
 
+    #Apparently zip chokes on non-canonical paths, creating double submissions sometimes
+    $filename = Cwd::abs_path($filename);
+
     #Otherwise, zip/base64 it.
     $params = $self->_prepare_file($filename) if !defined($raw_content);
 
@@ -2476,7 +2484,6 @@ sub _prepare_file {
     my $string = "";    # buffer
     zip $filename => \$string
       or die "zip failed: $ZipError\n";    # compress the file into string
-    require MIME::Base64;
 
     return {
         file => MIME::Base64::encode_base64($string)          # base64-encoded string

+ 2 - 0
t/01-driver.t

@@ -505,6 +505,8 @@ UPLOAD: {
     my $otherTestFile = "UEsDBBQACAAIAFtuNEYAAAAAAAAAAAAAAAAMABUAdC91cGxvYWRUZXN0VVQJAAOesb5UnrG+VFV4\nBADoA+gDK0ktLgEAUEsHCAx+f9gGAAAABAAAAFBLAQIUAxQACAAIAFtuNEYMfn/YBgAAAAQAAAAM\nAA0AAAAAAAAAAACkgQAAAAB0L3VwbG9hZFRlc3RVVAUAAZ6xvlRVeAAAUEsFBgAAAAABAAEARwAA\nAFUAAAAAAA==\n";
     like( $driver->upload_file('uploadTest',$testFile),qr/uploadTest$/,'upload_file returns FULL path to the file: cwd');
     like( $driver->upload_file('t/uploadTest',$otherTestFile),qr/uploadTest$/,'upload_file returns FULL path to the file: subdir');
+    like( $driver->upload_file('t/uploadTest'),qr/uploadTest$/,'upload_file: zip/base64 branch' );
+    like( $driver->upload_file('t/../t/uploadTest'),qr/uploadTest$/,'upload_file: zip/base64 branch with .. in path' );
 
     #Negative tests to verify that our expected behavior codepath is travelled by tests
     like( exception { $driver->upload_file('@@@SomeFileThatDoesNotExist@@@')},qr/no such file/,"Passing missing file terminates program");

File diff suppressed because it is too large
+ 7 - 6
t/mock-recordings/01-driver-mock-linux.json


+ 0 - 0
t/uploadTest


Some files were not shown because too many files changed in this diff