OSSL_STORE: Avoid testing with URIs on the mingw command line
[openssl.git] / test / recipes / 90-test_store.t
index 0db70a40e7b54922f22fb6aa8f1f131e361c84f5..9c240a051839272875dab65a812c0a9e5ee37042 100644 (file)
@@ -7,12 +7,39 @@
 # https://www.openssl.org/source/license.html
 
 use File::Spec;
+use File::Copy;
 use MIME::Base64;
 use OpenSSL::Test qw(:DEFAULT srctop_file srctop_dir bldtop_file data_file);
 
 my $test_name = "test_store";
 setup($test_name);
 
+# The MSYS2 run-time convert arguments that look like paths when executing
+# a program unless that application is linked with the MSYS run-time.  The
+# exact conversion rules are listed here:
+#
+#       http://www.mingw.org/wiki/Posix_path_conversion
+#
+# With the built-in configurations (all having names starting with "mingw"),
+# the openssl application is not linked with the MSYS2 run-time, and therefore,
+# it will receive possibly converted arguments from the process that executes
+# it.  This conversion is fine for normal path arguments, but when those
+# arguments are URIs, they sometimes aren't converted right (typically for
+# URIs without an authority component, 'cause the conversion mechanism doesn't
+# recognise them as URIs) or aren't converted at all (which gives perfectly
+# normal absolute paths from the MSYS viewpoint, but don't work for the
+# Windows run-time we're linked with).
+#
+# It's also possible to avoid conversion by defining MSYS2_ARG_CONV_EXCL with
+# some suitable pattern ("*" to avoid conversions entirely), but that will
+# again give us unconverted paths that don't work with the Windows run-time
+# we're linked with.
+#
+# Checking for both msys perl operating environment and that the target name
+# starts with "mingw", we're doing what we can to assure that other configs
+# that might link openssl.exe with the MSYS run-time are not disturbed.
+my $msys_mingw = ($^O eq 'msys') && (config('target') =~ m|^mingw|);
+
 my @noexist_files =
     ( "test/blahdiblah.pem",
       "test/blahdibleh.der" );
@@ -32,7 +59,6 @@ my @generated_files =
      "testcrl.der",
 
      ### generated locally
-     ### These examples were pilfered from OpenConnect's test suite
 
      "rsa-key-pkcs1.pem", "rsa-key-pkcs1.der",
      "rsa-key-pkcs1-aes128.pem",
@@ -56,11 +82,25 @@ my @generated_files =
      "ec-key-pkcs8-pbes2-sha1.pem", "ec-key-pkcs8-pbes2-sha1.der",
      "ec-key-aes256-cbc-sha256.p12",
     );
+my %generated_file_files =
+    $^O eq 'linux'
+    ? ( "test/testx509.pem" => "file:testx509.pem",
+        "test/testrsa.pem" => "file:testrsa.pem",
+        "test/testrsapub.pem" => "file:testrsapub.pem",
+        "test/testcrl.pem" => "file:testcrl.pem",
+        "apps/server.pem" => "file:server.pem" )
+    : ();
+my @noexist_file_files =
+    ( "file:blahdiblah.pem",
+      "file:test/blahdibleh.der" );
+
 
-my $n = (2 * scalar @noexist_files)
-    + (5 * scalar @src_files)
-    + (3 * scalar @generated_files)
-    + 2;
+my $n = (3 * scalar @noexist_files)
+    + (6 * scalar @src_files)
+    + (4 * scalar @generated_files)
+    + (scalar keys %generated_file_files)
+    + (scalar @noexist_file_files)
+    + 4;
 
 plan tests => $n;
 
@@ -69,34 +109,77 @@ indir "store_$$" => sub {
     {
         skip "failed initialisation", $n unless init();
 
+        # test PEM_read_bio_PrivateKey
+        ok(run(app(["openssl", "rsa", "-in", "rsa-key-pkcs8-pbes2-sha256.pem",
+                    "-passin", "pass:password"])));
+
         foreach (@noexist_files) {
             my $file = srctop_file($_);
-            ok(!run(app(["openssl", "storeutl", $file])));
-            ok(!run(app(["openssl", "storeutl", to_file_uri($file)])));
+        SKIP: {
+                ok(!run(app(["openssl", "storeutl", $file])));
+                ok(!run(app(["openssl", "storeutl", to_abs_file($file)])));
+
+                skip "No test of URI form of $file for mingw", 1 if $msys_mingw;
+
+                ok(!run(app(["openssl", "storeutl", to_abs_file_uri($file)])));
+            }
         }
         foreach (@src_files) {
             my $file = srctop_file($_);
-            ok(run(app(["openssl", "storeutl", $file])));
-            ok(run(app(["openssl", "storeutl", to_file_uri($file)])));
-            ok(run(app(["openssl", "storeutl", to_file_uri($file, 0,
-                                                           "")])));
-            ok(run(app(["openssl", "storeutl", to_file_uri($file, 0,
-                                                           "localhost")])));
-            ok(!run(app(["openssl", "storeutl", to_file_uri($file, 0,
-                                                            "dummy")])));
+        SKIP: {
+                ok(run(app(["openssl", "storeutl", $file])));
+                ok(run(app(["openssl", "storeutl", to_abs_file($file)])));
+
+                skip "No test of URI form of $file for mingw", 4 if $msys_mingw;
+
+                ok(run(app(["openssl", "storeutl", to_abs_file_uri($file)])));
+                ok(run(app(["openssl", "storeutl",
+                            to_abs_file_uri($file, 0, "")])));
+                ok(run(app(["openssl", "storeutl",
+                            to_abs_file_uri($file, 0, "localhost")])));
+                ok(!run(app(["openssl", "storeutl",
+                             to_abs_file_uri($file, 0, "dummy")])));
+            }
         }
         foreach (@generated_files) {
-            ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
-                        $_])));
-            ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
-                        to_file_uri($_)])));
-            ok(!run(app(["openssl", "storeutl", "-passin", "pass:password",
-                         to_rel_file_uri($_)])));
+        SKIP: {
+                ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
+                            $_])));
+                ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
+                            to_abs_file($_)])));
+
+                skip "No test of URI form of $_ for mingw", 2 if $msys_mingw;
+
+                ok(run(app(["openssl", "storeutl", "-passin", "pass:password",
+                            to_abs_file_uri($_)])));
+                ok(!run(app(["openssl", "storeutl", "-passin", "pass:password",
+                             to_file_uri($_)])));
+            }
+        }
+        foreach (values %generated_file_files) {
+        SKIP: {
+                skip "No test of $_ for mingw", 1 if $msys_mingw;
+
+                ok(run(app(["openssl", "storeutl", $_])));
+            }
+        }
+        foreach (@noexist_file_files) {
+        SKIP: {
+                skip "No test of $_ for mingw", 1 if $msys_mingw;
+
+                ok(!run(app(["openssl", "storeutl", $_])));
+            }
         }
         {
             my $dir = srctop_dir("test", "certs");
-            ok(run(app(["openssl", "storeutl", $dir])));
-            ok(run(app(["openssl", "storeutl", to_file_uri($dir, 1)])));
+        SKIP: {
+                ok(run(app(["openssl", "storeutl", $dir])));
+                ok(run(app(["openssl", "storeutl", to_abs_file($dir, 1)])));
+
+                skip "No test of URI form of $dir for mingw", 1 if $msys_mingw;
+
+                ok(run(app(["openssl", "storeutl", to_abs_file_uri($dir, 1)])));
+            }
         }
     }
 }, create => 1, cleanup => 1;
@@ -283,6 +366,16 @@ sub init {
                           close $outfh;
                           return 1;
                       }, grep(/\.der$/, @generated_files))
+            && runall(sub {
+                          my $srcfile = shift;
+                          my $dstfile = $generated_file_files{$srcfile};
+
+                          unless (copy srctop_file($srcfile), $dstfile) {
+                              warn "$!\n";
+                              return 0;
+                          }
+                          return 1;
+                      }, keys %generated_file_files)
            );
 }
 
@@ -297,12 +390,12 @@ sub runall {
 
 # According to RFC8089, a relative file: path is invalid.  We still produce
 # them for testing purposes.
-sub to_rel_file_uri {
+sub to_file_uri {
     my ($file, $isdir, $authority) = @_;
     my $vol;
     my $dir;
 
-    die "to_rel_file_uri: No file given\n" if !defined($file) || $file eq '';
+    die "to_file_uri: No file given\n" if !defined($file) || $file eq '';
 
     ($vol, $dir, $file) = File::Spec->splitpath($file, $isdir // 0);
 
@@ -342,9 +435,15 @@ sub to_rel_file_uri {
     return "file:$file";
 }
 
-sub to_file_uri {
+sub to_abs_file {
+    my ($file) = @_;
+
+    return File::Spec->rel2abs($file);
+}
+
+sub to_abs_file_uri {
     my ($file, $isdir, $authority) = @_;
 
-    die "to_file_uri: No file given\n" if !defined($file) || $file eq '';
-    return to_rel_file_uri(File::Spec->rel2abs($file), $isdir, $authority);
+    die "to_abs_file_uri: No file given\n" if !defined($file) || $file eq '';
+    return to_file_uri(to_abs_file($file), $isdir, $authority);
 }