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