apps/lib/s_socket.c and 80-test_cmp_http.t: Make ACCEPT port reporting more robust
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Sat, 22 May 2021 09:59:44 +0000 (11:59 +0200)
committerDr. David von Oheimb <dev@ddvo.net>
Sat, 29 May 2021 05:47:03 +0000 (07:47 +0200)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15417)

apps/lib/s_socket.c
test/recipes/80-test_cmp_http.t
test/recipes/80-test_cmp_http_data/Mock/server.cnf
test/recipes/80-test_cmp_http_data/Mock/test.cnf

index f543551bf13892b86252f58722991421656c441f..fbe913e37a78afdf4ec81142bd23cdb37e6755dd 100644 (file)
@@ -195,6 +195,8 @@ int report_server_accept(BIO *out, int asock, int with_address)
 {
     int success = 0;
 
+    if (BIO_printf(out, "ACCEPT") <= 0)
+        return 0;
     if (with_address) {
         union BIO_sock_info_u info;
         char *hostname = NULL;
@@ -206,16 +208,17 @@ int report_server_accept(BIO *out, int asock, int with_address)
             && (service = BIO_ADDR_service_string(info.addr, 1)) != NULL
             && BIO_printf(out,
                           strchr(hostname, ':') == NULL
-                          ? /* IPv4 */ "ACCEPT %s:%s\n"
-                          : /* IPv6 */ "ACCEPT [%s]:%s\n",
+                          ? /* IPv4 */ " %s:%s\n"
+                          : /* IPv6 */ " [%s]:%s\n",
                           hostname, service) > 0)
             success = 1;
+        else
+            (void)BIO_printf(out, "unknown:error\n");
 
         OPENSSL_free(hostname);
         OPENSSL_free(service);
         BIO_ADDR_free(info.addr);
-    } else {
-        (void)BIO_printf(out, "ACCEPT\n");
+    } else if (BIO_printf(out, "\n") > 0) {
         success = 1;
     }
     (void)BIO_flush(out);
index c74a5faf039594a602f67cbbf7c7e5e49aee4053..106f580636ba2b8173118ed9a6b8b9bd96760bb8 100644 (file)
@@ -187,8 +187,7 @@ indir data_dir() => sub {
             if ($server_name eq "Mock") {
                 indir "Mock" => sub {
                     $pid = start_mock_server("");
-                    skip "Cannot start or find the started CMP mock server",
-                        scalar @all_aspects unless $pid;
+                    die "Cannot start or find the started CMP mock server" unless $pid;
                 }
             }
             foreach my $aspect (@all_aspects) {
@@ -275,20 +274,23 @@ sub start_mock_server {
     my $cmd = bldtop_dir($app) . " -config server.cnf $args";
     print "Current directory is ".getcwd()."\n";
     print "Launching mock server: $cmd\n";
+    die "Invalid port: $server_port" unless $server_port =~ m/^\d+$/;
     my $pid = open($server_fh, "$cmd|") or die "Trying to $cmd";
     print "Pid is: $pid\n";
-    # Find out the actual server port
-    while (<$server_fh>) {
-        print;
-        s/\R$//;                # Better chomp
-        next unless (/^ACCEPT\s.*:(\d+)$/);
-        $server_port = $1;
-        $server_tls = $1;
-        $kur_port = $1;
-        $pbm_port = $1;
-        last;
+    if ($server_port eq "0") {
+        # Find out the actual server port
+        while (<$server_fh>) {
+            print;
+            s/\R$//;                # Better chomp
+            next unless (/^ACCEPT/);
+            $server_port = $server_tls = $kur_port = $pbm_port = $1
+                if m/^ACCEPT\s.*?:(\d+)$/;
+            last;
+        }
     }
-    return $pid;
+    return $pid if $server_port =~ m/^(\d+)$/;
+    stop_mock_server($pid);
+    return 0;
 }
 
 sub stop_mock_server {
index 24a6ebb9f62fb29264e22fcbe19d5ef13f38fd30..633dc9230b553fb9235afd81afe40a2ff22400e5 100644 (file)
@@ -1,7 +1,6 @@
 [cmp] # mock server configuration
 
-# port 0 means that a random available port will be used
-port = 0
+port = 0 # 0 means that the server should choose a random available port
 srv_cert = server.crt
 srv_key = server.key
 srv_secret = pass:test
index 8c8913b3c9a7f26d209422c8d61cf414f8050ae3..87dd575a8aa32284d22285c1254068620a2bd7bc 100644 (file)
@@ -17,6 +17,7 @@ policies = certificatePolicies
 [Mock] # the built-in OpenSSL CMP mock server
 no_check_time = 1
 server_host = 127.0.0.1 # localhost
+# server_port = 0 means that the port is determined by the server
 server_port = 0
 server_tls = $server_port
 server_cert = server.crt