contrib / credential / netrc / test.plon commit Merge branch 'jk/upload-pack-keepalive' (7b8315b)
   1#!/usr/bin/perl
   2
   3use warnings;
   4use strict;
   5use Test;
   6use IPC::Open2;
   7
   8BEGIN { plan tests => 15 }
   9
  10my @global_credential_args = @ARGV;
  11my $netrc = './test.netrc';
  12print "# Testing insecure file, nothing should be found\n";
  13chmod 0644, $netrc;
  14my $cred = run_credential(['-f', $netrc, 'get'],
  15                          { host => 'github.com' });
  16
  17ok(scalar keys %$cred, 0, "Got 0 keys from insecure file");
  18
  19print "# Testing missing file, nothing should be found\n";
  20chmod 0644, $netrc;
  21$cred = run_credential(['-f', '///nosuchfile///', 'get'],
  22                       { host => 'github.com' });
  23
  24ok(scalar keys %$cred, 0, "Got 0 keys from missing file");
  25
  26chmod 0600, $netrc;
  27
  28print "# Testing with invalid data\n";
  29$cred = run_credential(['-f', $netrc, 'get'],
  30                       "bad data");
  31ok(scalar keys %$cred, 4, "Got first found keys with bad data");
  32
  33print "# Testing netrc file for a missing corovamilkbar entry\n";
  34$cred = run_credential(['-f', $netrc, 'get'],
  35                       { host => 'corovamilkbar' });
  36
  37ok(scalar keys %$cred, 0, "Got no corovamilkbar keys");
  38
  39print "# Testing netrc file for a github.com entry\n";
  40$cred = run_credential(['-f', $netrc, 'get'],
  41                       { host => 'github.com' });
  42
  43ok(scalar keys %$cred, 2, "Got 2 Github keys");
  44
  45ok($cred->{password}, 'carolknows', "Got correct Github password");
  46ok($cred->{username}, 'carol', "Got correct Github username");
  47
  48print "# Testing netrc file for a username-specific entry\n";
  49$cred = run_credential(['-f', $netrc, 'get'],
  50                       { host => 'imap', username => 'bob' });
  51
  52ok(scalar keys %$cred, 2, "Got 2 username-specific keys");
  53
  54ok($cred->{password}, 'bobwillknow', "Got correct user-specific password");
  55ok($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
  56
  57print "# Testing netrc file for a host:port-specific entry\n";
  58$cred = run_credential(['-f', $netrc, 'get'],
  59                       { host => 'imap2:1099' });
  60
  61ok(scalar keys %$cred, 2, "Got 2 host:port-specific keys");
  62
  63ok($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
  64ok($cred->{username}, 'tzz', "Got correct host:port-specific username");
  65
  66print "# Testing netrc file that 'host:port kills host' entry\n";
  67$cred = run_credential(['-f', $netrc, 'get'],
  68                       { host => 'imap2' });
  69
  70ok(scalar keys %$cred, 2, "Got 2 'host:port kills host' keys");
  71
  72ok($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
  73ok($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
  74
  75sub run_credential
  76{
  77        my $args = shift @_;
  78        my $data = shift @_;
  79        my $pid = open2(my $chld_out, my $chld_in,
  80                        './git-credential-netrc', @global_credential_args,
  81                        @$args);
  82
  83        die "Couldn't open pipe to netrc credential helper: $!" unless $pid;
  84
  85        if (ref $data eq 'HASH')
  86        {
  87                print $chld_in "$_=$data->{$_}\n" foreach sort keys %$data;
  88        }
  89        else
  90        {
  91                print $chld_in "$data\n";
  92        }
  93
  94        close $chld_in;
  95        my %ret;
  96
  97        while (<$chld_out>)
  98        {
  99                chomp;
 100                next unless m/^([^=]+)=(.+)/;
 101
 102                $ret{$1} = $2;
 103        }
 104
 105        return \%ret;
 106}