5 use Test::More qw(no_plan);
 
   7 use File::Spec::Functions qw(:DEFAULT rel2abs);
 
  11         # t-git-credential-netrc.sh kicks off our testing, so we have to go
 
  13         Test::More->builder->current_test(1);
 
  16 my @global_credential_args = @ARGV;
 
  17 my $scriptDir = dirname rel2abs $0;
 
  18 my ($netrc, $netrcGpg, $gcNetrc) = map { catfile $scriptDir, $_; }
 
  21                                           git-credential-netrc);
 
  22 local $ENV{PATH} = join ':'
 
  28 diag "Testing insecure file, nothing should be found\n";
 
  30 my $cred = run_credential(['-f', $netrc, 'get'],
 
  31                           { host => 'github.com' });
 
  33 ok(scalar keys %$cred == 0, "Got 0 keys from insecure file");
 
  35 diag "Testing missing file, nothing should be found\n";
 
  37 $cred = run_credential(['-f', '///nosuchfile///', 'get'],
 
  38                        { host => 'github.com' });
 
  40 ok(scalar keys %$cred == 0, "Got 0 keys from missing file");
 
  44 diag "Testing with invalid data\n";
 
  45 $cred = run_credential(['-f', $netrc, 'get'],
 
  47 ok(scalar keys %$cred == 4, "Got first found keys with bad data");
 
  49 diag "Testing netrc file for a missing corovamilkbar entry\n";
 
  50 $cred = run_credential(['-f', $netrc, 'get'],
 
  51                        { host => 'corovamilkbar' });
 
  53 ok(scalar keys %$cred == 0, "Got no corovamilkbar keys");
 
  55 diag "Testing netrc file for a github.com entry\n";
 
  56 $cred = run_credential(['-f', $netrc, 'get'],
 
  57                        { host => 'github.com' });
 
  59 ok(scalar keys %$cred == 2, "Got 2 Github keys");
 
  61 is($cred->{password}, 'carolknows', "Got correct Github password");
 
  62 is($cred->{username}, 'carol', "Got correct Github username");
 
  64 diag "Testing netrc file for a username-specific entry\n";
 
  65 $cred = run_credential(['-f', $netrc, 'get'],
 
  66                        { host => 'imap', username => 'bob' });
 
  68 ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
 
  70 is($cred->{password}, 'bobwillknow', "Got correct user-specific password");
 
  71 is($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
 
  73 diag "Testing netrc file for a host:port-specific entry\n";
 
  74 $cred = run_credential(['-f', $netrc, 'get'],
 
  75                        { host => 'imap2:1099' });
 
  77 ok(scalar keys %$cred == 2, "Got 2 host:port-specific keys");
 
  79 is($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
 
  80 is($cred->{username}, 'tzz', "Got correct host:port-specific username");
 
  82 diag "Testing netrc file that 'host:port kills host' entry\n";
 
  83 $cred = run_credential(['-f', $netrc, 'get'],
 
  86 ok(scalar keys %$cred == 2, "Got 2 'host:port kills host' keys");
 
  88 is($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
 
  89 is($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
 
  91 diag 'Testing netrc file decryption by git config gpg.program setting\n';
 
  92 $cred = run_credential( ['-f', $netrcGpg, 'get']
 
  93                       , { host => 'git-config-gpg' }
 
  96 ok(scalar keys %$cred == 2, 'Got keys decrypted by git config option');
 
  98 diag 'Testing netrc file decryption by gpg option\n';
 
  99 $cred = run_credential( ['-f', $netrcGpg, '-g', 'test.command-option-gpg', 'get']
 
 100                       , { host => 'command-option-gpg' }
 
 103 ok(scalar keys %$cred == 2, 'Got keys decrypted by command option');
 
 105 my $is_passing = eval { Test::More->is_passing };
 
 106 exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;
 
 112         my $pid = open2(my $chld_out, my $chld_in,
 
 113                         $gcNetrc, @global_credential_args,
 
 116         die "Couldn't open pipe to netrc credential helper: $!" unless $pid;
 
 118         if (ref $data eq 'HASH')
 
 120                 print $chld_in "$_=$data->{$_}\n" foreach sort keys %$data;
 
 124                 print $chld_in "$data\n";
 
 133                 next unless m/^([^=]+)=(.+)/;