1#!/bin/sh
2
3test_description='credential-cache tests'
4. ./test-lib.sh
5. "$TEST_DIRECTORY"/lib-credential.sh
6
7test -z "$NO_UNIX_SOCKETS" || {
8	skip_all='skipping credential-cache tests, unix sockets not available'
9	test_done
10}
11
12uname_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
27# don't leave a stale daemon running
28test_atexit 'git credential-cache exit'
29
30# test that the daemon works with no special setup
31helper_test cache
32
33test_expect_success 'socket defaults to ~/.cache/git/credential/socket' '
34	test_when_finished "
35		git credential-cache exit &&
36		rmdir -p .cache/git/credential/
37	" &&
38	test_path_is_missing "$HOME/.git-credential-cache" &&
39	test_path_is_socket "$HOME/.cache/git/credential/socket"
40'
41
42XDG_CACHE_HOME="$HOME/xdg"
43export XDG_CACHE_HOME
44# test behavior when XDG_CACHE_HOME is set
45helper_test cache
46
47test_expect_success "use custom XDG_CACHE_HOME if set and default sockets are not created" '
48	test_when_finished "git credential-cache exit" &&
49	test_path_is_socket "$XDG_CACHE_HOME/git/credential/socket" &&
50	test_path_is_missing "$HOME/.git-credential-cache/socket" &&
51	test_path_is_missing "$HOME/.cache/git/credential/socket"
52'
53unset XDG_CACHE_HOME
54
55test_expect_success 'credential-cache --socket option overrides default location' '
56	test_when_finished "
57		git credential-cache exit --socket \"\$HOME/dir/socket\" &&
58		rmdir \"\$HOME/dir\"
59	" &&
60	check approve "cache --socket \"\$HOME/dir/socket\"" <<-\EOF &&
61	protocol=https
62	host=example.com
63	username=store-user
64	password=store-pass
65	EOF
66	test_path_is_socket "$HOME/dir/socket"
67'
68
69test_expect_success "use custom XDG_CACHE_HOME even if xdg socket exists" '
70	test_when_finished "
71		git credential-cache exit &&
72		sane_unset XDG_CACHE_HOME
73	" &&
74	check approve cache <<-\EOF &&
75	protocol=https
76	host=example.com
77	username=store-user
78	password=store-pass
79	EOF
80	test_path_is_socket "$HOME/.cache/git/credential/socket" &&
81	XDG_CACHE_HOME="$HOME/xdg" &&
82	export XDG_CACHE_HOME &&
83	check approve cache <<-\EOF &&
84	protocol=https
85	host=example.com
86	username=store-user
87	password=store-pass
88	EOF
89	test_path_is_socket "$XDG_CACHE_HOME/git/credential/socket"
90'
91
92test_expect_success 'use user socket if user directory exists' '
93	test_when_finished "
94		git credential-cache exit &&
95		rmdir \"\$HOME/.git-credential-cache/\"
96	" &&
97	mkdir -p "$HOME/.git-credential-cache/" &&
98	chmod 700 "$HOME/.git-credential-cache/" &&
99	check approve cache <<-\EOF &&
100	protocol=https
101	host=example.com
102	username=store-user
103	password=store-pass
104	EOF
105	test_path_is_socket "$HOME/.git-credential-cache/socket"
106'
107
108test_expect_success SYMLINKS 'use user socket if user directory is a symlink to a directory' '
109	test_when_finished "
110		git credential-cache exit &&
111		rmdir \"\$HOME/dir/\" &&
112		rm \"\$HOME/.git-credential-cache\"
113	" &&
114	mkdir -p -m 700 "$HOME/dir/" &&
115	ln -s "$HOME/dir" "$HOME/.git-credential-cache" &&
116	check approve cache <<-\EOF &&
117	protocol=https
118	host=example.com
119	username=store-user
120	password=store-pass
121	EOF
122	test_path_is_socket "$HOME/.git-credential-cache/socket"
123'
124
125helper_test_timeout cache --timeout=1
126
127test_done
128