| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use warnings; |
| 4 |
use strict; |
| 5 |
use Test::More qw(no_plan); |
| 6 |
use File::Basename; |
| 7 |
use File::Spec::Functions qw(:DEFAULT rel2abs); |
| 8 |
use IPC::Open2; |
| 9 |
|
| 10 |
BEGIN { |
| 11 |
# t-git-credential-netrc.sh kicks off our testing, so we have to go |
| 12 |
# from there. |
| 13 |
Test::More->builder->current_test(1); |
| 14 |
} |
| 15 |
|
| 16 |
my @global_credential_args = @ARGV; |
| 17 |
my $scriptDir = dirname rel2abs $0; |
| 18 |
my ($netrc, $netrcGpg) = map { catfile $scriptDir, $_; } |
| 19 |
qw(test.netrc |
| 20 |
test.netrc.gpg); |
| 21 |
my $gcNetrc = $ENV{CREDENTIAL_NETRC_PATH} || catfile $scriptDir, qw(git-credential-netrc); |
| 22 |
|
| 23 |
local $ENV{PATH} = join ':' |
| 24 |
, $scriptDir |
| 25 |
, $ENV{PATH} |
| 26 |
? $ENV{PATH} |
| 27 |
: (); |
| 28 |
|
| 29 |
diag "Testing insecure file, nothing should be found\n"; |
| 30 |
chmod 0644, $netrc; |
| 31 |
my $cred = run_credential(['-f', $netrc, 'get'], |
| 32 |
{ host => 'github.com' }); |
| 33 |
|
| 34 |
ok(scalar keys %$cred == 0, "Got 0 keys from insecure file"); |
| 35 |
|
| 36 |
diag "Testing missing file, nothing should be found\n"; |
| 37 |
chmod 0644, $netrc; |
| 38 |
$cred = run_credential(['-f', '///nosuchfile///', 'get'], |
| 39 |
{ host => 'github.com' }); |
| 40 |
|
| 41 |
ok(scalar keys %$cred == 0, "Got 0 keys from missing file"); |
| 42 |
|
| 43 |
chmod 0600, $netrc; |
| 44 |
|
| 45 |
diag "Testing with invalid data\n"; |
| 46 |
$cred = run_credential(['-f', $netrc, 'get'], |
| 47 |
"bad data"); |
| 48 |
ok(scalar keys %$cred == 3, "Got first found keys with bad data"); |
| 49 |
|
| 50 |
diag "Testing netrc file for a missing corovamilkbar entry\n"; |
| 51 |
$cred = run_credential(['-f', $netrc, 'get'], |
| 52 |
{ host => 'corovamilkbar' }); |
| 53 |
|
| 54 |
ok(scalar keys %$cred == 0, "Got no corovamilkbar keys"); |
| 55 |
|
| 56 |
diag "Testing netrc file for a github.com entry\n"; |
| 57 |
$cred = run_credential(['-f', $netrc, 'get'], |
| 58 |
{ host => 'github.com' }); |
| 59 |
|
| 60 |
ok(scalar keys %$cred == 2, "Got 2 Github keys"); |
| 61 |
|
| 62 |
is($cred->{password}, 'carolknows', "Got correct Github password"); |
| 63 |
is($cred->{username}, 'carol', "Got correct Github username"); |
| 64 |
|
| 65 |
diag "Testing netrc file for a username-specific entry\n"; |
| 66 |
$cred = run_credential(['-f', $netrc, 'get'], |
| 67 |
{ host => 'imap:993', username => 'bob' }); |
| 68 |
|
| 69 |
# Only the password field gets returned. |
| 70 |
ok(scalar keys %$cred == 1, "Got 1 username-specific keys"); |
| 71 |
|
| 72 |
is($cred->{password}, 'bobwillknow', "Got correct user-specific password"); |
| 73 |
|
| 74 |
diag "Testing netrc file for a host:port-specific entry\n"; |
| 75 |
$cred = run_credential(['-f', $netrc, 'get'], |
| 76 |
{ host => 'imap2:1099' }); |
| 77 |
|
| 78 |
ok(scalar keys %$cred == 2, "Got 2 host:port-specific keys"); |
| 79 |
|
| 80 |
is($cred->{password}, 'tzzknow', "Got correct host:port-specific password"); |
| 81 |
is($cred->{username}, 'tzz', "Got correct host:port-specific username"); |
| 82 |
|
| 83 |
diag "Testing netrc file that 'host:port kills host' entry\n"; |
| 84 |
$cred = run_credential(['-f', $netrc, 'get'], |
| 85 |
{ host => 'imap2' }); |
| 86 |
|
| 87 |
ok(scalar keys %$cred == 2, "Got 2 'host:port kills host' keys"); |
| 88 |
|
| 89 |
is($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password"); |
| 90 |
is($cred->{username}, 'bob', "Got correct 'host:port kills host' username"); |
| 91 |
|
| 92 |
diag 'Testing netrc file decryption by git config gpg.program setting\n'; |
| 93 |
$cred = run_credential( ['-f', $netrcGpg, 'get'] |
| 94 |
, { host => 'git-config-gpg' } |
| 95 |
); |
| 96 |
|
| 97 |
ok(scalar keys %$cred == 2, 'Got keys decrypted by git config option'); |
| 98 |
|
| 99 |
diag 'Testing netrc file decryption by gpg option\n'; |
| 100 |
$cred = run_credential( ['-f', $netrcGpg, '-g', 'test.command-option-gpg', 'get'] |
| 101 |
, { host => 'command-option-gpg' } |
| 102 |
); |
| 103 |
|
| 104 |
ok(scalar keys %$cred == 2, 'Got keys decrypted by command option'); |
| 105 |
|
| 106 |
my $is_passing = eval { Test::More->is_passing }; |
| 107 |
exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/; |
| 108 |
|
| 109 |
sub run_credential |
| 110 |
{ |
| 111 |
my $args = shift @_; |
| 112 |
my $data = shift @_; |
| 113 |
my $pid = open2(my $chld_out, my $chld_in, |
| 114 |
$gcNetrc, @global_credential_args, |
| 115 |
@$args); |
| 116 |
|
| 117 |
die "Couldn't open pipe to netrc credential helper: $!" unless $pid; |
| 118 |
|
| 119 |
if (ref $data eq 'HASH') |
| 120 |
{ |
| 121 |
print $chld_in "$_=$data->{$_}\n" foreach sort keys %$data; |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
print $chld_in "$data\n"; |
| 126 |
} |
| 127 |
|
| 128 |
close $chld_in; |
| 129 |
my %ret; |
| 130 |
|
| 131 |
while (<$chld_out>) |
| 132 |
{ |
| 133 |
chomp; |
| 134 |
next unless m/^([^=]+)=(.+)/; |
| 135 |
|
| 136 |
$ret{$1} = $2; |
| 137 |
} |
| 138 |
|
| 139 |
return \%ret; |
| 140 |
} |