Merge branch 'mb/fast-import-delete-root' into maint
[git.git] / contrib / credential / netrc / test.pl
blob169b6463c3011fbef721d926396fa20b5d1616f7
1 #!/usr/bin/perl
3 use warnings;
4 use strict;
5 use Test;
6 use IPC::Open2;
8 BEGIN { plan tests => 15 }
10 my @global_credential_args = @ARGV;
11 my $netrc = './test.netrc';
12 print "# Testing insecure file, nothing should be found\n";
13 chmod 0644, $netrc;
14 my $cred = run_credential(['-f', $netrc, 'get'],
15 { host => 'github.com' });
17 ok(scalar keys %$cred, 0, "Got 0 keys from insecure file");
19 print "# Testing missing file, nothing should be found\n";
20 chmod 0644, $netrc;
21 $cred = run_credential(['-f', '///nosuchfile///', 'get'],
22 { host => 'github.com' });
24 ok(scalar keys %$cred, 0, "Got 0 keys from missing file");
26 chmod 0600, $netrc;
28 print "# Testing with invalid data\n";
29 $cred = run_credential(['-f', $netrc, 'get'],
30 "bad data");
31 ok(scalar keys %$cred, 4, "Got first found keys with bad data");
33 print "# Testing netrc file for a missing corovamilkbar entry\n";
34 $cred = run_credential(['-f', $netrc, 'get'],
35 { host => 'corovamilkbar' });
37 ok(scalar keys %$cred, 0, "Got no corovamilkbar keys");
39 print "# Testing netrc file for a github.com entry\n";
40 $cred = run_credential(['-f', $netrc, 'get'],
41 { host => 'github.com' });
43 ok(scalar keys %$cred, 2, "Got 2 Github keys");
45 ok($cred->{password}, 'carolknows', "Got correct Github password");
46 ok($cred->{username}, 'carol', "Got correct Github username");
48 print "# Testing netrc file for a username-specific entry\n";
49 $cred = run_credential(['-f', $netrc, 'get'],
50 { host => 'imap', username => 'bob' });
52 ok(scalar keys %$cred, 2, "Got 2 username-specific keys");
54 ok($cred->{password}, 'bobwillknow', "Got correct user-specific password");
55 ok($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
57 print "# Testing netrc file for a host:port-specific entry\n";
58 $cred = run_credential(['-f', $netrc, 'get'],
59 { host => 'imap2:1099' });
61 ok(scalar keys %$cred, 2, "Got 2 host:port-specific keys");
63 ok($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
64 ok($cred->{username}, 'tzz', "Got correct host:port-specific username");
66 print "# Testing netrc file that 'host:port kills host' entry\n";
67 $cred = run_credential(['-f', $netrc, 'get'],
68 { host => 'imap2' });
70 ok(scalar keys %$cred, 2, "Got 2 'host:port kills host' keys");
72 ok($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
73 ok($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
75 sub run_credential
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);
83 die "Couldn't open pipe to netrc credential helper: $!" unless $pid;
85 if (ref $data eq 'HASH')
87 print $chld_in "$_=$data->{$_}\n" foreach sort keys %$data;
89 else
91 print $chld_in "$data\n";
94 close $chld_in;
95 my %ret;
97 while (<$chld_out>)
99 chomp;
100 next unless m/^([^=]+)=(.+)/;
102 $ret{$1} = $2;
105 return \%ret;