734417
From 1d3aa3540da1a751c047e84fbc5dca793e411b2e Mon Sep 17 00:00:00 2001
734417
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
734417
Date: Wed, 21 Mar 2018 13:48:53 +0100
734417
Subject: [PATCH 2/4] added tests for Net::SMTP SSL, save arguments in
734417
 Net::SMTP to apply them on starttls
734417
MIME-Version: 1.0
734417
Content-Type: text/plain; charset=UTF-8
734417
Content-Transfer-Encoding: 8bit
734417
734417
Ported to Net::SMTP 2.31 as found in Perl 5.16.3 from libnet upstream:
734417
734417
commit 34436780081aa8793b16f51864bce42a7cdd7e8b
734417
Author: Steffen Ullrich <Steffen_Ullrich@genua.de>
734417
Date:   Fri May 16 21:55:20 2014 +0200
734417
734417
    added tests for Net::SMTP SSL and IPv6, save arguments in Net::SMTP to apply them on starttls
734417
734417
IPv6 support excluded.
734417
734417
Check for IO::Socket::SSL version removed because we have default sane
734417
default CA list since perl-IO-Socket-SSL-1.94-7.el7.
734417
734417
Signed-off-by: Petr Písař <ppisar@redhat.com>
734417
---
734417
 MANIFEST                 |   1 +
734417
 cpan/libnet/Net/SMTP.pm  |   6 ++-
734417
 cpan/libnet/t/smtp_ssl.t | 120 +++++++++++++++++++++++++++++++++++++++++++++++
734417
 3 files changed, 126 insertions(+), 1 deletion(-)
734417
 create mode 100644 cpan/libnet/t/smtp_ssl.t
734417
734417
diff --git a/MANIFEST b/MANIFEST
734417
index b649ddb..aa117d3 100644
734417
--- a/MANIFEST
734417
+++ b/MANIFEST
734417
@@ -1456,6 +1456,7 @@ cpan/libnet/t/libnet_t.pl	libnet
734417
 cpan/libnet/t/netrc.t		libnet
734417
 cpan/libnet/t/nntp.t		libnet
734417
 cpan/libnet/t/require.t		libnet
734417
+cpan/libnet/t/smtp_ssl.t	libnet
734417
 cpan/libnet/t/smtp.t		libnet
734417
 cpan/libnet/t/time.t		libnet
734417
 cpan/List-Util/Changes			Util extension
734417
diff --git a/cpan/libnet/Net/SMTP.pm b/cpan/libnet/Net/SMTP.pm
734417
index 6143990..7dbf3df 100644
734417
--- a/cpan/libnet/Net/SMTP.pm
734417
+++ b/cpan/libnet/Net/SMTP.pm
734417
@@ -70,6 +70,7 @@ sub new {
734417
   return undef
734417
     unless defined $obj;
734417
 
734417
+  ${*$obj}{'net_smtp_arg'} = \%arg;
734417
   if ($arg{SSL}) {
734417
     Net::SMTP::_SSLified->start_SSL($obj,SSL_verifycn_name => $host,%arg)
734417
       or return;
734417
@@ -216,7 +217,10 @@ sub starttls {
734417
   my $self = shift;
734417
   $ssl_class or die $nossl_warn;
734417
   $self->_STARTTLS or return;
734417
-  Net::SMTP::_SSLified->start_SSL($self,@_) or return;
734417
+  Net::SMTP::_SSLified->start_SSL($self,
734417
+    %{ ${*$self}{'net_smtp_arg'} }, # (ssl) args given in new
734417
+    @_   # more (ssl) args
734417
+  ) or return;
734417
 
734417
   # another hello after starttls to read new ESMTP capabilities
734417
   return $self->hello(${*$self}{net_smtp_hello_domain});
734417
diff --git a/cpan/libnet/t/smtp_ssl.t b/cpan/libnet/t/smtp_ssl.t
734417
new file mode 100644
734417
index 0000000..e817d00
734417
--- /dev/null
734417
+++ b/cpan/libnet/t/smtp_ssl.t
734417
@@ -0,0 +1,120 @@
734417
+use strict;
734417
+use warnings;
734417
+use Test::More;
734417
+use File::Temp 'tempfile';
734417
+use Net::SMTP;
734417
+
734417
+my $debug = 0; # Net::SMTP Debug => ..
734417
+
734417
+plan skip_all => "no SSL support found in Net::SMTP" if ! Net::SMTP->can_ssl;
734417
+
734417
+plan skip_all => "fork not supported on this platform"
734417
+  if grep { $^O =~m{$_} } qw(MacOS VOS vmesa riscos amigaos);
734417
+
734417
+plan skip_all => "incomplete or to old version of IO::Socket::SSL" if ! eval {
734417
+  require IO::Socket::SSL
734417
+    && require IO::Socket::SSL::Utils
734417
+    && defined &IO::Socket::SSL::Utils::CERT_create;
734417
+};
734417
+
734417
+my $srv = IO::Socket::INET->new(
734417
+  LocalAddr => '127.0.0.1',
734417
+  Listen => 10
734417
+);
734417
+plan skip_all => "cannot create listener on localhost: $!" if ! $srv;
734417
+my $saddr = $srv->sockhost.':'.$srv->sockport;
734417
+
734417
+plan tests => 2;
734417
+
734417
+my ($ca,$key) = IO::Socket::SSL::Utils::CERT_create( CA => 1 );
734417
+my ($fh,$cafile) = tempfile();
734417
+print $fh IO::Socket::SSL::Utils::PEM_cert2string($ca);
734417
+close($fh);
734417
+
734417
+my $parent = $$;
734417
+END { unlink($cafile) if $$ == $parent }
734417
+
734417
+my ($cert) = IO::Socket::SSL::Utils::CERT_create(
734417
+  subject => { CN => 'smtp.example.com' },
734417
+  issuer_cert => $ca, issuer_key => $key,
734417
+  key => $key
734417
+);
734417
+
734417
+test(1); # direct ssl
734417
+test(0); # starttls
734417
+
734417
+
734417
+sub test {
734417
+  my $ssl = shift;
734417
+  defined( my $pid = fork()) or die "fork failed: $!";
734417
+  exit(smtp_server($ssl)) if ! $pid;
734417
+  smtp_client($ssl);
734417
+  wait;
734417
+}
734417
+
734417
+
734417
+sub smtp_client {
734417
+  my $ssl = shift;
734417
+  my %sslopt = (
734417
+    SSL_verifycn_name => 'smtp.example.com',
734417
+    SSL_ca_file => $cafile
734417
+  );
734417
+  $sslopt{SSL} = 1 if $ssl;
734417
+  my $cl = Net::SMTP->new($saddr, %sslopt, Debug => $debug);
734417
+  diag("created Net::SMTP object");
734417
+  if (!$cl) {
734417
+    fail( ($ssl ? "SSL ":"" )."SMTP connect failed");
734417
+  } elsif ($ssl) {
734417
+    $cl->quit;
734417
+    pass("SSL SMTP connect success");
734417
+  } elsif ( ! $cl->starttls ) {
734417
+    fail("starttls failed: $IO::Socket::SSL::SSL_ERROR");
734417
+  } else {
734417
+    $cl->quit;
734417
+    pass("starttls success");
734417
+  }
734417
+}
734417
+
734417
+sub smtp_server {
734417
+  my $ssl = shift;
734417
+  my $cl = $srv->accept or die "accept failed: $!";
734417
+  my %sslargs = (
734417
+    SSL_server => 1,
734417
+    SSL_cert => $cert,
734417
+    SSL_key => $key,
734417
+  );
734417
+  if ( $ssl ) {
734417
+    if ( ! IO::Socket::SSL->start_SSL($cl, %sslargs)) {
734417
+      diag("initial ssl handshake with client failed");
734417
+      return;
734417
+    }
734417
+  }
734417
+
734417
+  print $cl "220 welcome\r\n";
734417
+  while (<$cl>) {
734417
+    my ($cmd,$arg) = m{^(\S+)(?: +(.*))?\r\n} or die $_;
734417
+    $cmd = uc($cmd);
734417
+    if ($cmd eq 'QUIT' ) {
734417
+      print $cl "250 bye\r\n";
734417
+      last;
734417
+    } elsif ( $cmd eq 'HELO' ) {
734417
+      print $cl "250 localhost\r\n";
734417
+    } elsif ( $cmd eq 'EHLO' ) {
734417
+      print $cl "250-localhost\r\n".
734417
+	( $ssl ? "" : "250-STARTTLS\r\n" ).
734417
+	"250 HELP\r\n";
734417
+    } elsif ( ! $ssl and $cmd eq 'STARTTLS' ) {
734417
+      print $cl "250 starting ssl\r\n";
734417
+      if ( ! IO::Socket::SSL->start_SSL($cl, %sslargs)) {
734417
+	diag("initial ssl handshake with client failed");
734417
+	return;
734417
+      }
734417
+      $ssl = 1;
734417
+    } else {
734417
+      diag("received unknown command: $cmd");
734417
+      print "500 unknown cmd\r\n";
734417
+    }
734417
+  }
734417
+
734417
+  diag("SMTP dialog done");
734417
+}
734417
-- 
734417
2.14.3
734417