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