git-credential-netrc: minor whitespace cleanup in test script
[git/git.git] / contrib / credential / netrc / test.pl
1 #!/usr/bin/perl
2 use lib (split(/:/, $ENV{GITPERLLIB}));
3
4 use warnings;
5 use strict;
6 use Test::More qw(no_plan);
7 use File::Basename;
8 use File::Spec::Functions qw(:DEFAULT rel2abs);
9 use IPC::Open2;
10
11 BEGIN {
12 # t-git-credential-netrc.sh kicks off our testing, so we have to go
13 # from there.
14 Test::More->builder->current_test(1);
15 Test::More->builder->no_ending(1);
16 }
17
18 my @global_credential_args = @ARGV;
19 my $scriptDir = dirname rel2abs $0;
20 my ($netrc, $netrcGpg, $gcNetrc) = map { catfile $scriptDir, $_; }
21 qw(test.netrc
22 test.netrc.gpg
23 git-credential-netrc);
24 local $ENV{PATH} = join ':'
25 , $scriptDir
26 , $ENV{PATH}
27 ? $ENV{PATH}
28 : ();
29
30 diag "Testing insecure file, nothing should be found\n";
31 chmod 0644, $netrc;
32 my $cred = run_credential(['-f', $netrc, 'get'],
33 { host => 'github.com' });
34
35 ok(scalar keys %$cred == 0, "Got 0 keys from insecure file");
36
37 diag "Testing missing file, nothing should be found\n";
38 chmod 0644, $netrc;
39 $cred = run_credential(['-f', '///nosuchfile///', 'get'],
40 { host => 'github.com' });
41
42 ok(scalar keys %$cred == 0, "Got 0 keys from missing file");
43
44 chmod 0600, $netrc;
45
46 diag "Testing with invalid data\n";
47 $cred = run_credential(['-f', $netrc, 'get'],
48 "bad data");
49 ok(scalar keys %$cred == 4, "Got first found keys with bad data");
50
51 diag "Testing netrc file for a missing corovamilkbar entry\n";
52 $cred = run_credential(['-f', $netrc, 'get'],
53 { host => 'corovamilkbar' });
54
55 ok(scalar keys %$cred == 0, "Got no corovamilkbar keys");
56
57 diag "Testing netrc file for a github.com entry\n";
58 $cred = run_credential(['-f', $netrc, 'get'],
59 { host => 'github.com' });
60
61 ok(scalar keys %$cred == 2, "Got 2 Github keys");
62
63 is($cred->{password}, 'carolknows', "Got correct Github password");
64 is($cred->{username}, 'carol', "Got correct Github username");
65
66 diag "Testing netrc file for a username-specific entry\n";
67 $cred = run_credential(['-f', $netrc, 'get'],
68 { host => 'imap', username => 'bob' });
69
70 ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
71
72 is($cred->{password}, 'bobwillknow', "Got correct user-specific password");
73 is($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
74
75 diag "Testing netrc file for a host:port-specific entry\n";
76 $cred = run_credential(['-f', $netrc, 'get'],
77 { host => 'imap2:1099' });
78
79 ok(scalar keys %$cred == 2, "Got 2 host:port-specific keys");
80
81 is($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
82 is($cred->{username}, 'tzz', "Got correct host:port-specific username");
83
84 diag "Testing netrc file that 'host:port kills host' entry\n";
85 $cred = run_credential(['-f', $netrc, 'get'],
86 { host => 'imap2' });
87
88 ok(scalar keys %$cred == 2, "Got 2 'host:port kills host' keys");
89
90 is($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
91 is($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
92
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' }
96 );
97
98 ok(scalar keys %$cred == 2, 'Got keys decrypted by git config option');
99
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' }
103 );
104
105 ok(scalar keys %$cred == 2, 'Got keys decrypted by command option');
106
107 sub run_credential
108 {
109 my $args = shift @_;
110 my $data = shift @_;
111 my $pid = open2(my $chld_out, my $chld_in,
112 $gcNetrc, @global_credential_args,
113 @$args);
114
115 die "Couldn't open pipe to netrc credential helper: $!" unless $pid;
116
117 if (ref $data eq 'HASH')
118 {
119 print $chld_in "$_=$data->{$_}\n" foreach sort keys %$data;
120 }
121 else
122 {
123 print $chld_in "$data\n";
124 }
125
126 close $chld_in;
127 my %ret;
128
129 while (<$chld_out>)
130 {
131 chomp;
132 next unless m/^([^=]+)=(.+)/;
133
134 $ret{$1} = $2;
135 }
136
137 return \%ret;
138 }