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