2 use lib (split(/:/, $ENV{GITPERLLIB}));
6 use Test::More qw(no_plan);
8 use File::Spec::Functions qw(:DEFAULT rel2abs);
12 # t-git-credential-netrc.sh kicks off our testing, so we have to go
14 Test::More->builder->current_test(1);
15 Test::More->builder->no_ending(1);
18 my @global_credential_args = @ARGV;
19 my $scriptDir = dirname rel2abs $0;
20 my ($netrc, $netrcGpg, $gcNetrc) = map { catfile $scriptDir, $_; }
23 git-credential-netrc);
24 local $ENV{PATH} = join ':'
30 diag "Testing insecure file, nothing should be found\n";
32 my $cred = run_credential(['-f', $netrc, 'get'],
33 { host => 'github.com' });
35 ok(scalar keys %$cred == 0, "Got 0 keys from insecure file");
37 diag "Testing missing file, nothing should be found\n";
39 $cred = run_credential(['-f', '///nosuchfile///', 'get'],
40 { host => 'github.com' });
42 ok(scalar keys %$cred == 0, "Got 0 keys from missing file");
46 diag "Testing with invalid data\n";
47 $cred = run_credential(['-f', $netrc, 'get'],
49 ok(scalar keys %$cred == 4, "Got first found keys with bad data");
51 diag "Testing netrc file for a missing corovamilkbar entry\n";
52 $cred = run_credential(['-f', $netrc, 'get'],
53 { host => 'corovamilkbar' });
55 ok(scalar keys %$cred == 0, "Got no corovamilkbar keys");
57 diag "Testing netrc file for a github.com entry\n";
58 $cred = run_credential(['-f', $netrc, 'get'],
59 { host => 'github.com' });
61 ok(scalar keys %$cred == 2, "Got 2 Github keys");
63 is($cred->{password}, 'carolknows', "Got correct Github password");
64 is($cred->{username}, 'carol', "Got correct Github username");
66 diag "Testing netrc file for a username-specific entry\n";
67 $cred = run_credential(['-f', $netrc, 'get'],
68 { host => 'imap', username => 'bob' });
70 ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
72 is($cred->{password}, 'bobwillknow', "Got correct user-specific password");
73 is($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
75 diag "Testing netrc file for a host:port-specific entry\n";
76 $cred = run_credential(['-f', $netrc, 'get'],
77 { host => 'imap2:1099' });
79 ok(scalar keys %$cred == 2, "Got 2 host:port-specific keys");
81 is($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
82 is($cred->{username}, 'tzz', "Got correct host:port-specific username");
84 diag "Testing netrc file that 'host:port kills host' entry\n";
85 $cred = run_credential(['-f', $netrc, 'get'],
88 ok(scalar keys %$cred == 2, "Got 2 'host:port kills host' keys");
90 is($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
91 is($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
93 diag 'Testing netrc file decryption by git config gpg.program setting\n';
94 $cred = run_credential( ['-f', $netrcGpg, 'get']
95 , { host => 'git-config-gpg' }
98 ok(scalar keys %$cred == 2, 'Got keys decrypted by git config option');
100 diag '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' }
105 ok(scalar keys %$cred == 2, 'Got keys decrypted by command option');
111 my $pid = open2(my $chld_out, my $chld_in,
112 $gcNetrc, @global_credential_args,
115 die "Couldn't open pipe to netrc credential helper: $!" unless $pid;
117 if (ref $data eq 'HASH')
119 print $chld_in "$_=$data->{$_}\n" foreach sort keys %$data;
123 print $chld_in "$data\n";
132 next unless m/^([^=]+)=(.+)/;