@ -1,83 +1,99 @@
#!/usr/bin/perl
#!/usr/bin/perl
use lib (split(/:/, $ENV{GITPERLLIB}));
use warnings;
use warnings;
use strict;
use strict;
use Test;
use Test::More qw(no_plan);
use File::Basename;
use File::Spec::Functions qw(:DEFAULT rel2abs);
use IPC::Open2;
use IPC::Open2;
BEGIN { plan tests => 15 }
BEGIN {
# t-git-credential-netrc.sh kicks off our testing, so we have to go from there.
Test::More->builder->current_test(1);
Test::More->builder->no_ending(1);
}
my @global_credential_args = @ARGV;
my @global_credential_args = @ARGV;
my $netrc = './test.netrc';
my $scriptDir = dirname rel2abs $0;
print "# Testing insecure file, nothing should be found\n";
my $netrc = catfile $scriptDir, 'test.netrc';
my $gcNetrc = catfile $scriptDir, 'git-credential-netrc';
local $ENV{PATH} = join ':'
, $scriptDir
, $ENV{PATH}
? $ENV{PATH}
: ();
diag "Testing insecure file, nothing should be found\n";
chmod 0644, $netrc;
chmod 0644, $netrc;
my $cred = run_credential(['-f', $netrc, 'get'],
my $cred = run_credential(['-f', $netrc, 'get'],
{ host => 'github.com' });
{ host => 'github.com' });
ok(scalar keys %$cred, 0, "Got 0 keys from insecure file");
ok(scalar keys %$cred == 0, "Got 0 keys from insecure file");
print "# Testing missing file, nothing should be found\n";
diag "Testing missing file, nothing should be found\n";
chmod 0644, $netrc;
chmod 0644, $netrc;
$cred = run_credential(['-f', '///nosuchfile///', 'get'],
$cred = run_credential(['-f', '///nosuchfile///', 'get'],
{ host => 'github.com' });
{ host => 'github.com' });
ok(scalar keys %$cred, 0, "Got 0 keys from missing file");
ok(scalar keys %$cred == 0, "Got 0 keys from missing file");
chmod 0600, $netrc;
chmod 0600, $netrc;
print "# Testing with invalid data\n";
diag "Testing with invalid data\n";
$cred = run_credential(['-f', $netrc, 'get'],
$cred = run_credential(['-f', $netrc, 'get'],
"bad data");
"bad data");
ok(scalar keys %$cred, 4, "Got first found keys with bad data");
ok(scalar keys %$cred == 4, "Got first found keys with bad data");
print "# Testing netrc file for a missing corovamilkbar entry\n";
diag "Testing netrc file for a missing corovamilkbar entry\n";
$cred = run_credential(['-f', $netrc, 'get'],
$cred = run_credential(['-f', $netrc, 'get'],
{ host => 'corovamilkbar' });
{ host => 'corovamilkbar' });
ok(scalar keys %$cred, 0, "Got no corovamilkbar keys");
ok(scalar keys %$cred == 0, "Got no corovamilkbar keys");
print "# Testing netrc file for a github.com entry\n";
diag "Testing netrc file for a github.com entry\n";
$cred = run_credential(['-f', $netrc, 'get'],
$cred = run_credential(['-f', $netrc, 'get'],
{ host => 'github.com' });
{ host => 'github.com' });
ok(scalar keys %$cred, 2, "Got 2 Github keys");
ok(scalar keys %$cred == 2, "Got 2 Github keys");
ok($cred->{password}, 'carolknows', "Got correct Github password");
is($cred->{password}, 'carolknows', "Got correct Github password");
ok($cred->{username}, 'carol', "Got correct Github username");
is($cred->{username}, 'carol', "Got correct Github username");
print "# Testing netrc file for a username-specific entry\n";
diag "Testing netrc file for a username-specific entry\n";
$cred = run_credential(['-f', $netrc, 'get'],
$cred = run_credential(['-f', $netrc, 'get'],
{ host => 'imap', username => 'bob' });
{ host => 'imap', username => 'bob' });
ok(scalar keys %$cred, 2, "Got 2 username-specific keys");
ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
ok($cred->{password}, 'bobwillknow', "Got correct user-specific password");
is($cred->{password}, 'bobwillknow', "Got correct user-specific password");
ok($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
is($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
print "# Testing netrc file for a host:port-specific entry\n";
diag "Testing netrc file for a host:port-specific entry\n";
$cred = run_credential(['-f', $netrc, 'get'],
$cred = run_credential(['-f', $netrc, 'get'],
{ host => 'imap2:1099' });
{ host => 'imap2:1099' });
ok(scalar keys %$cred, 2, "Got 2 host:port-specific keys");
ok(scalar keys %$cred == 2, "Got 2 host:port-specific keys");
ok($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
is($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
ok($cred->{username}, 'tzz', "Got correct host:port-specific username");
is($cred->{username}, 'tzz', "Got correct host:port-specific username");
print "# Testing netrc file that 'host:port kills host' entry\n";
diag "Testing netrc file that 'host:port kills host' entry\n";
$cred = run_credential(['-f', $netrc, 'get'],
$cred = run_credential(['-f', $netrc, 'get'],
{ host => 'imap2' });
{ host => 'imap2' });
ok(scalar keys %$cred, 2, "Got 2 'host:port kills host' keys");
ok(scalar keys %$cred == 2, "Got 2 'host:port kills host' keys");
is($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
is($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
ok($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
ok($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
sub run_credential
sub run_credential
{
{
my $args = shift @_;
my $args = shift @_;
my $data = shift @_;
my $data = shift @_;
my $pid = open2(my $chld_out, my $chld_in,
my $pid = open2(my $chld_out, my $chld_in,
'./git-credential-netrc', @global_credential_args,
$gcNetrc, @global_credential_args,
@$args);
@$args);
die "Couldn't open pipe to netrc credential helper: $!" unless $pid;
die "Couldn't open pipe to netrc credential helper: $!" unless $pid;