1
0
Эх сурвалжийг харах

Merge pull request #195 from teodesian/master

Fix issues with paths containing .. in upload path (see #194)
Daniel Gempesaw 10 жил өмнө
parent
commit
7aac6170c9

+ 3 - 0
.gitignore

@@ -10,3 +10,6 @@ cover_db
 Selenium-Remote-Driver-*
 Selenium-Remote-Driver-*
 .prove
 .prove
 _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::Spec::Functions ();
 use File::Basename ();
 use File::Basename ();
 use Sub::Install ();
 use Sub::Install ();
+use Cwd ();
+use MIME::Base64 ();
 
 
 use constant FINDERS => {
 use constant FINDERS => {
     class             => 'class name',
     class             => 'class name',
@@ -1417,7 +1419,6 @@ sub capture_screenshot {
     my ( $self, $filename ) = @_;
     my ( $self, $filename ) = @_;
     croak '$filename is required' unless $filename;
     croak '$filename is required' unless $filename;
 
 
-    require MIME::Base64;
     open( my $fh, '>', $filename );
     open( my $fh, '>', $filename );
     binmode $fh;
     binmode $fh;
     print $fh MIME::Base64::decode_base64( $self->screenshot() );
     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
     Passing raw data as an argument past the filename will upload
     that rather than the file's contents.
     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:
  Usage:
     my $remote_fname = $driver->upload_file( $fname );
     my $remote_fname = $driver->upload_file( $fname );
     my $element = $driver->find_element( '//input[@id="file"]' );
     my $element = $driver->find_element( '//input[@id="file"]' );
@@ -2455,6 +2460,9 @@ sub upload_file {
         file => $raw_content
         file => $raw_content
     };
     };
 
 
+    #Apparently zip chokes on non-canonical paths, creating double submissions sometimes
+    $filename = Cwd::abs_path($filename);
+
     #Otherwise, zip/base64 it.
     #Otherwise, zip/base64 it.
     $params = $self->_prepare_file($filename) if !defined($raw_content);
     $params = $self->_prepare_file($filename) if !defined($raw_content);
 
 
@@ -2476,7 +2484,6 @@ sub _prepare_file {
     my $string = "";    # buffer
     my $string = "";    # buffer
     zip $filename => \$string
     zip $filename => \$string
       or die "zip failed: $ZipError\n";    # compress the file into string
       or die "zip failed: $ZipError\n";    # compress the file into string
-    require MIME::Base64;
 
 
     return {
     return {
         file => MIME::Base64::encode_base64($string)          # base64-encoded string
         file => MIME::Base64::encode_base64($string)          # base64-encoded string

+ 20 - 1
t/01-driver.t

@@ -506,9 +506,28 @@ UPLOAD: {
     like( $driver->upload_file('uploadTest',$testFile),qr/uploadTest$/,'upload_file returns FULL path to the file: cwd');
     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',$otherTestFile),qr/uploadTest$/,'upload_file returns FULL path to the file: subdir');
 
 
+    my $fake_driver;
+    if ($harness->record) {
+        $fake_driver = $driver;
+    } else {
+        #Going to have to use a custom UA to test this, since bogosity ensues
+        my $ftua = Test::LWP::UserAgent->new;
+        my $fake_header = bless( {'content-type' => 'application/json; charset=utf-8'}, 'HTTP::Headers' );
+        #Seems a bit heavy handed, but shouldn't be a problem.
+        $ftua->map_response(qr{.*}, HTTP::Response->new(200, 'OK', $fake_header, '{"sessionId":"89726c41-409f-421e-95a8-8b1fa482fa33","status":0,"state":"success","value":"/tmp/89726c41-409f-421e-95a8-8b1fa482fa33/upload1105744174029267337file/uploadTest","class":"org.openqa.selenium.remote.Response","hCode":516517658}'));
+        $fake_driver = Selenium::Remote::Driver->new(
+            ua => $ftua
+        );
+
+    }
+
+    #In the case this is not mocked, it tests a real issue (.. in paths), if not, it makes sure the zip/base64 bits at least don't make us explode.
+    like( $fake_driver->upload_file('t/uploadTest'),qr/uploadTest$/,'upload_file: zip/base64 branch' );
+    like( $fake_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
     #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");
     like( exception { $driver->upload_file('@@@SomeFileThatDoesNotExist@@@')},qr/no such file/,"Passing missing file terminates program");
-  SKIP: {
+    SKIP: {
         skip 'purposefully excluding this test from the recording', 1
         skip 'purposefully excluding this test from the recording', 1
           if $harness->record;
           if $harness->record;
         like( exception { $driver->upload_file(__FILE__) },qr/501/,"Passing this file rightly fails due to mock not being present");
         like( exception { $driver->upload_file(__FILE__) },qr/501/,"Passing this file rightly fails due to mock not being present");

+ 0 - 0
t/uploadTest