1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
#! /usr/bin/perl
use strict;
use warnings;
use Test::More;
use IPC::Open3;
use Symbol 'gensym';
use File::Temp qw(tempdir tempfile);
require v5.8.0;
my $testdir = tempdir(CLEANUP => 1);
my $openssl = $ENV{OPENSSL} || "openssl";
chdir $testdir;
plan tests => 33;
help_tests(); # 1..4
create_ca_tests(); # 5..14
email_test(); # 15..17
days_test(); # 18..20
pass_test(); # 21..23
subject_test(); # 24..27
bits_test(); # 28-33
sub help_tests
{
my ($infd, $outfd, $errfd, $pid, $rc);
$errfd = gensym;
$pid = open3($infd, $outfd, $errfd, "grid-ca-create", "-help");
ok($pid > 0, "grid-ca-create -help runs");
local $/;
close($infd);
waitpid($pid, 0);
ok($? == 0, "grid-ca-create exits with 0");
my ($out, $err) = (<$outfd>, <$errfd>);
$out =~ s/^/# /mg if $out;
print STDERR "# stdout: \n$out" if $out;
$err =~ s/^/# /mg if ($err);
print STDERR "# stderr: \n$err" if $err;
ok($out ne "", "grid-ca-create -help prints help");
ok(!$err, "grid-ca-create -help doesn't error");
}
sub create_ca_tests
{
my ($infd, $outfd, $errfd, $pid, $rc);
$errfd = gensym;
# Try to create a CA
$pid = open3($infd, $outfd, $errfd, "grid-ca-create",
"-dir", "$testdir/create", "-pass", "stdin");
ok($pid > 0, "grid-ca-create -dir \$testdir/create");
local $/;
print $infd "\n\n\n";
close($infd);
SKIP: {
skip "Didn't run grid-ca-create", 10 unless $pid > 0;
waitpid($pid, 0);
ok($? == 0, "grid-ca-create exits with 0");
my ($out, $err) = (<$outfd>, <$errfd>);
$out =~ s/^/# /mg if $out;
print STDERR "# stdout: \n$out" if $out;
$err =~ s/^/# /mg if ($err);
print STDERR "# stderr: \n$err" if $err;
ok($out ne "", "grid-ca-create -noint prints output");
ok(!$err, "grid-ca-create -noint doesn't error");
# Try to create a CA in an already populated dir
$errfd = gensym;
$pid = open3($infd, $outfd, $errfd, "grid-ca-create",
"-dir", "$testdir/create", "-pass", "stdin");
ok($pid > 0, "grid-ca-create -dir \$testdir/create");
local $/;
print $infd "\n\n\n";
close($infd);
waitpid($pid, 0);
ok($? != 0, "grid-ca-create to existing dir should fail");
# Try to create a CA in an already populated dir with force
$errfd = gensym;
$pid = open3($infd, $outfd, $errfd, "grid-ca-create",
"-dir", "$testdir/create", "-force", "-pass", "stdin");
ok($pid > 0, "grid-ca-create -noint -dir \$testdir/create -force");
local $/;
print $infd "\n\n\n";
close($infd);
waitpid($pid, 0);
ok($? == 0, "grid-ca-create -noint -force exits with 0");
($out, $err) = (<$outfd>, <$errfd>);
$out =~ s/^/# /mg if $out;
print STDERR "# stdout: \n$out" if $out;
$err =~ s/^/# /mg if ($err);
print STDERR "# stderr: \n$err" if $err;
ok($out ne "", "grid-ca-create -noint -force prints output");
ok(!$err, "grid-ca-create -noint -force doesn't error");
}
}
sub email_test
{
my ($infd, $outfd, $errfd, $pid, $rc, $email);
my $reference_email = "foo\@bar.com";
$errfd = gensym;
# Try to create a CA
$pid = open3($infd, $outfd, $errfd, "grid-ca-create", "-noint",
"-dir", "$testdir/emailtest", "-email", $reference_email);
ok($pid > 0, "grid-ca-create -noint -dir \$testdir/emailtest -email $reference_email");
SKIP: {
skip "Unable to run grid-ca-create", 2 unless ($pid > 0);
local $/;
close($infd);
waitpid($pid, 0);
ok($? == 0, "grid-ca-create -noint -email exits with 0");
$email = `. $testdir/emailtest/grid-security.conf; echo \$DEFAULT_GSI_CA_EMAIL_ADDR`;
chop($email);
ok($email eq $reference_email, "Email $email matches $reference_email");
}
}
sub days_test
{
my ($infd, $outfd, $errfd, $pid, $rc, @enddate);
my @reference_enddate = gmtime(time() + 5*60*60*24); # 5 days
$errfd = gensym;
# Try to create a CA
$pid = open3($infd, $outfd, $errfd, "grid-ca-create", "-noint",
"-dir", "$testdir/datetest", "-days", "5");
ok($pid > 0, "grid-ca-create -noint -dir \$testdir/datetest -days 5");
SKIP: {
skip "Unable to run grid-ca-create", 2 unless ($pid > 0);
local $/;
close($infd);
waitpid($pid, 0);
ok($? == 0, "grid-ca-create -noint -days exits with 0");
my $datestring = `$openssl x509 -enddate -in $testdir/datetest/cacert.pem -noout`;
@enddate = gmtime(chomp($datestring));
ok($enddate[7] - $reference_enddate[7] < 2, "enddate match");
}
}
sub pass_test
{
my ($infd, $outfd, $errfd, $pid, $rc);
my $password = "12345";
$errfd = gensym;
# Try to create a CA
$pid = open3($infd, $outfd, $errfd, "grid-ca-create", "-noint",
"-dir", "$testdir/passtest", "-pass", $password);
ok($pid > 0, "grid-ca-create -noint -dir \$testdir/passtest");
SKIP: {
skip "Unable to run grid-ca-create", 3 unless ($pid > 0);
local $/;
close($infd);
waitpid($pid, 0);
ok($? == 0, "grid-ca-create -noint -pass $password exits with 0");
my $stderr = `$openssl rsa -in $testdir/passtest/private/cakey.pem -noout -passin pass:$password 2>&1 1>/dev/null`;
ok($stderr eq '', "Check passphrase works with no output");
ok($? == 0, "Check passphrase works with 0 exit code");
}
}
sub subject_test
{
my ($infd, $outfd, $errfd, $pid, $rc, $subject);
my $reference_subject = "CN=My Test CA,OU=Test,O=Globus,C=US";
$errfd = gensym;
# Try to create a CA
$pid = open3($infd, $outfd, $errfd, "grid-ca-create", "-noint",
"-dir", "$testdir/subjecttest", "-subject", $reference_subject);
ok($pid > 0, "grid-ca-create -noint -dir \$testdir/subjecttest -subject $reference_subject");
SKIP: {
skip "Unable to run grid-ca-create", 2 unless ($pid > 0);
local $/;
close($infd);
waitpid($pid, 0);
ok($? == 0, "grid-ca-create -noint -subject exits with 0");
$subject = `$openssl x509 -in $testdir/subjecttest/cacert.pem -noout -subject -nameopt rfc2253`;
$subject =~ s/^subject= *//;
chop($subject);
ok($subject eq $reference_subject, "subject $subject and $reference_subject match");
}
}
sub bits_test
{
my ($infd, $outfd, $errfd, $pid, $rc, $bits);
my $reference_bits = '4096';
my $bitsdir = "$testdir/bitstest.$reference_bits";
$errfd = gensym;
# Try to create a CA with default 4096 bit key
$pid = open3($infd, $outfd, $errfd, "grid-ca-create", "-noint",
"-dir", "$bitsdir");
ok($pid > 0, "grid-ca-create -noint -dir \$bitsdir");
SKIP: {
skip "Unable to run grid-ca-create", 2 unless ($pid > 0);
local $/;
close($infd);
waitpid($pid, 0);
ok($? == 0, "grid-ca-create -noint -bits exits with 0");
$bits = `$openssl x509 -in $bitsdir/cacert.pem -text -noout | grep -E 'RSA Public Key|Public-Key' | tr -cd 0-9`;
ok($bits eq $reference_bits, "Bits $bits matches $reference_bits");
}
$reference_bits = '2048';
$bitsdir = "$testdir/bitstest.$reference_bits";
# Try to create a CA with 2048 bit key
$pid = open3($infd, $outfd, $errfd, "grid-ca-create", "-noint",
"-dir", "$bitsdir", "-bits", $reference_bits);
ok($pid > 0, "grid-ca-create -noint -dir \$bitsdir -bits $reference_bits");
SKIP: {
skip "Unable to run grid-ca-create", 2 unless ($pid > 0);
local $/;
close($infd);
waitpid($pid, 0);
ok($? == 0, "grid-ca-create -noint -bits exits with 0");
$bits = `$openssl x509 -in $bitsdir/cacert.pem -text -noout | grep -E 'RSA Public Key|Public-Key' | tr -cd 0-9`;
ok($bits eq $reference_bits, "Bits $bits matches $reference_bits");
}
}
chdir '/';
|