blob: c02a3b5969c3d2265d1552132464df7135cd522b [file] [log] [blame]
Jeff Kinge2770972011-12-10 05:34:14 -05001#!/bin/sh
2
3test_description='credential-cache tests'
4. ./test-lib.sh
5. "$TEST_DIRECTORY"/lib-credential.sh
6
Johannes Sixt63203582011-12-12 22:12:56 +01007test -z "$NO_UNIX_SOCKETS" || {
8 skip_all='skipping credential-cache tests, unix sockets not available'
9 test_done
10}
11
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -070012uname_s=$(uname -s)
13case $uname_s in
14*MINGW*)
15 test_path_is_socket () {
16 # `test -S` cannot detect Win10's Unix sockets
17 test_path_exists "$1"
18 }
19 ;;
20*)
21 test_path_is_socket () {
22 test -S "$1"
23 }
24 ;;
25esac
26
Jeff Kinge2770972011-12-10 05:34:14 -050027# don't leave a stale daemon running
SZEDER Gábor3bc27022019-03-13 13:24:14 +010028test_atexit 'git credential-cache exit'
Jeff Kinge2770972011-12-10 05:34:14 -050029
Devin Lehmacher612c49e2017-03-17 08:36:34 -040030# test that the daemon works with no special setup
Jeff Kinge2770972011-12-10 05:34:14 -050031helper_test cache
M Hickforda5c76562023-04-21 09:47:59 +000032helper_test_oauth_refresh_token cache
Devin Lehmacher612c49e2017-03-17 08:36:34 -040033
34test_expect_success 'socket defaults to ~/.cache/git/credential/socket' '
35 test_when_finished "
36 git credential-cache exit &&
37 rmdir -p .cache/git/credential/
38 " &&
39 test_path_is_missing "$HOME/.git-credential-cache" &&
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -070040 test_path_is_socket "$HOME/.cache/git/credential/socket"
Devin Lehmacher612c49e2017-03-17 08:36:34 -040041'
42
43XDG_CACHE_HOME="$HOME/xdg"
44export XDG_CACHE_HOME
45# test behavior when XDG_CACHE_HOME is set
46helper_test cache
47
48test_expect_success "use custom XDG_CACHE_HOME if set and default sockets are not created" '
49 test_when_finished "git credential-cache exit" &&
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -070050 test_path_is_socket "$XDG_CACHE_HOME/git/credential/socket" &&
Devin Lehmacher612c49e2017-03-17 08:36:34 -040051 test_path_is_missing "$HOME/.git-credential-cache/socket" &&
52 test_path_is_missing "$HOME/.cache/git/credential/socket"
53'
54unset XDG_CACHE_HOME
55
56test_expect_success 'credential-cache --socket option overrides default location' '
57 test_when_finished "
58 git credential-cache exit --socket \"\$HOME/dir/socket\" &&
59 rmdir \"\$HOME/dir\"
60 " &&
61 check approve "cache --socket \"\$HOME/dir/socket\"" <<-\EOF &&
62 protocol=https
63 host=example.com
64 username=store-user
65 password=store-pass
66 EOF
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -070067 test_path_is_socket "$HOME/dir/socket"
Devin Lehmacher612c49e2017-03-17 08:36:34 -040068'
69
70test_expect_success "use custom XDG_CACHE_HOME even if xdg socket exists" '
71 test_when_finished "
72 git credential-cache exit &&
73 sane_unset XDG_CACHE_HOME
74 " &&
75 check approve cache <<-\EOF &&
76 protocol=https
77 host=example.com
78 username=store-user
79 password=store-pass
80 EOF
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -070081 test_path_is_socket "$HOME/.cache/git/credential/socket" &&
Devin Lehmacher612c49e2017-03-17 08:36:34 -040082 XDG_CACHE_HOME="$HOME/xdg" &&
83 export XDG_CACHE_HOME &&
84 check approve cache <<-\EOF &&
85 protocol=https
86 host=example.com
87 username=store-user
88 password=store-pass
89 EOF
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -070090 test_path_is_socket "$XDG_CACHE_HOME/git/credential/socket"
Devin Lehmacher612c49e2017-03-17 08:36:34 -040091'
92
93test_expect_success 'use user socket if user directory exists' '
94 test_when_finished "
95 git credential-cache exit &&
96 rmdir \"\$HOME/.git-credential-cache/\"
97 " &&
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -070098 mkdir -p "$HOME/.git-credential-cache/" &&
99 chmod 700 "$HOME/.git-credential-cache/" &&
Devin Lehmacher612c49e2017-03-17 08:36:34 -0400100 check approve cache <<-\EOF &&
101 protocol=https
102 host=example.com
103 username=store-user
104 password=store-pass
105 EOF
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -0700106 test_path_is_socket "$HOME/.git-credential-cache/socket"
Devin Lehmacher612c49e2017-03-17 08:36:34 -0400107'
108
109test_expect_success SYMLINKS 'use user socket if user directory is a symlink to a directory' '
110 test_when_finished "
111 git credential-cache exit &&
112 rmdir \"\$HOME/dir/\" &&
113 rm \"\$HOME/.git-credential-cache\"
114 " &&
115 mkdir -p -m 700 "$HOME/dir/" &&
116 ln -s "$HOME/dir" "$HOME/.git-credential-cache" &&
117 check approve cache <<-\EOF &&
118 protocol=https
119 host=example.com
120 username=store-user
121 password=store-pass
122 EOF
Carlo Marcelo Arenas Belón0fdcfa22021-09-14 00:25:58 -0700123 test_path_is_socket "$HOME/.git-credential-cache/socket"
Devin Lehmacher612c49e2017-03-17 08:36:34 -0400124'
125
Jeff Kinge2770972011-12-10 05:34:14 -0500126helper_test_timeout cache --timeout=1
127
Jeff Kinge2770972011-12-10 05:34:14 -0500128test_done