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