1#!/usr/bin/perl
   2use warnings;
   4use strict;
   5use Test::More qw(no_plan);
   6use File::Basename;
   7use File::Spec::Functions qw(:DEFAULT rel2abs);
   8use IPC::Open2;
   9BEGIN {
  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}
  15my @global_credential_args = @ARGV;
  17my $scriptDir = dirname rel2abs $0;
  18my ($netrc, $netrcGpg, $gcNetrc) = map { catfile $scriptDir, $_; }
  19                                       qw(test.netrc
  20                                          test.netrc.gpg
  21                                          git-credential-netrc);
  22local $ENV{PATH} = join ':'
  23                      , $scriptDir
  24                      , $ENV{PATH}
  25                      ? $ENV{PATH}
  26                      : ();
  27diag "Testing insecure file, nothing should be found\n";
  29chmod 0644, $netrc;
  30my $cred = run_credential(['-f', $netrc, 'get'],
  31                          { host => 'github.com' });
  32ok(scalar keys %$cred == 0, "Got 0 keys from insecure file");
  34diag "Testing missing file, nothing should be found\n";
  36chmod 0644, $netrc;
  37$cred = run_credential(['-f', '///nosuchfile///', 'get'],
  38                       { host => 'github.com' });
  39ok(scalar keys %$cred == 0, "Got 0 keys from missing file");
  41chmod 0600, $netrc;
  43diag "Testing with invalid data\n";
  45$cred = run_credential(['-f', $netrc, 'get'],
  46                       "bad data");
  47ok(scalar keys %$cred == 4, "Got first found keys with bad data");
  48diag "Testing netrc file for a missing corovamilkbar entry\n";
  50$cred = run_credential(['-f', $netrc, 'get'],
  51                       { host => 'corovamilkbar' });
  52ok(scalar keys %$cred == 0, "Got no corovamilkbar keys");
  54diag "Testing netrc file for a github.com entry\n";
  56$cred = run_credential(['-f', $netrc, 'get'],
  57                       { host => 'github.com' });
  58ok(scalar keys %$cred == 2, "Got 2 Github keys");
  60is($cred->{password}, 'carolknows', "Got correct Github password");
  62is($cred->{username}, 'carol', "Got correct Github username");
  63diag "Testing netrc file for a username-specific entry\n";
  65$cred = run_credential(['-f', $netrc, 'get'],
  66                       { host => 'imap', username => 'bob' });
  67ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
  69is($cred->{password}, 'bobwillknow', "Got correct user-specific password");
  71is($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
  72diag "Testing netrc file for a host:port-specific entry\n";
  74$cred = run_credential(['-f', $netrc, 'get'],
  75                       { host => 'imap2:1099' });
  76ok(scalar keys %$cred == 2, "Got 2 host:port-specific keys");
  78is($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
  80is($cred->{username}, 'tzz', "Got correct host:port-specific username");
  81diag "Testing netrc file that 'host:port kills host' entry\n";
  83$cred = run_credential(['-f', $netrc, 'get'],
  84                       { host => 'imap2' });
  85ok(scalar keys %$cred == 2, "Got 2 'host:port kills host' keys");
  87is($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
  89is($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
  90diag 'Testing netrc file decryption by git config gpg.program setting\n';
  92$cred = run_credential( ['-f', $netrcGpg, 'get']
  93                      , { host => 'git-config-gpg' }
  94                      );
  95ok(scalar keys %$cred == 2, 'Got keys decrypted by git config option');
  97diag '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' }
 101                      );
 102ok(scalar keys %$cred == 2, 'Got keys decrypted by command option');
 104my $is_passing = eval { Test::More->is_passing };
 106exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;
 107sub run_credential
 109{
 110        my $args = shift @_;
 111        my $data = shift @_;
 112        my $pid = open2(my $chld_out, my $chld_in,
 113                        $gcNetrc, @global_credential_args,
 114                        @$args);
 115        die "Couldn't open pipe to netrc credential helper: $!" unless $pid;
 117        if (ref $data eq 'HASH')
 119        {
 120                print $chld_in "$_=$data->{$_}\n" foreach sort keys %$data;
 121        }
 122        else
 123        {
 124                print $chld_in "$data\n";
 125        }
 126        close $chld_in;
 128        my %ret;
 129        while (<$chld_out>)
 131        {
 132                chomp;
 133                next unless m/^([^=]+)=(.+)/;
 134                $ret{$1} = $2;
 136        }
 137        return \%ret;
 139}