Merge branch 'jc/doc-commit-only'
[git] / contrib / credential / netrc / test.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use Test;
6 use IPC::Open2;
7
8 BEGIN { plan tests => 15 }
9
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' });
16
17 ok(scalar keys %$cred, 0, "Got 0 keys from insecure file");
18
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' });
23
24 ok(scalar keys %$cred, 0, "Got 0 keys from missing file");
25
26 chmod 0600, $netrc;
27
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");
32
33 print "# Testing netrc file for a missing corovamilkbar entry\n";
34 $cred = run_credential(['-f', $netrc, 'get'],
35                        { host => 'corovamilkbar' });
36
37 ok(scalar keys %$cred, 0, "Got no corovamilkbar keys");
38
39 print "# Testing netrc file for a github.com entry\n";
40 $cred = run_credential(['-f', $netrc, 'get'],
41                        { host => 'github.com' });
42
43 ok(scalar keys %$cred, 2, "Got 2 Github keys");
44
45 ok($cred->{password}, 'carolknows', "Got correct Github password");
46 ok($cred->{username}, 'carol', "Got correct Github username");
47
48 print "# Testing netrc file for a username-specific entry\n";
49 $cred = run_credential(['-f', $netrc, 'get'],
50                        { host => 'imap', username => 'bob' });
51
52 ok(scalar keys %$cred, 2, "Got 2 username-specific keys");
53
54 ok($cred->{password}, 'bobwillknow', "Got correct user-specific password");
55 ok($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
56
57 print "# Testing netrc file for a host:port-specific entry\n";
58 $cred = run_credential(['-f', $netrc, 'get'],
59                        { host => 'imap2:1099' });
60
61 ok(scalar keys %$cred, 2, "Got 2 host:port-specific keys");
62
63 ok($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
64 ok($cred->{username}, 'tzz', "Got correct host:port-specific username");
65
66 print "# Testing netrc file that 'host:port kills host' entry\n";
67 $cred = run_credential(['-f', $netrc, 'get'],
68                        { host => 'imap2' });
69
70 ok(scalar keys %$cred, 2, "Got 2 'host:port kills host' keys");
71
72 ok($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
73 ok($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
74
75 sub 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 }