|
| 1 | +#!/usr/bin/env bash |
| 2 | +# gotproxy git proxy integration test (HTTPS over TCP) |
| 3 | +# Usage: sudo ./scripts/test_git.sh |
| 4 | +# Requires: built gotproxy, git; root (CAP_BPF). |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 10 | +GOTPROXY_BIN="${GOTPROXY_BIN:-$REPO_ROOT/gotproxy}" |
| 11 | +PROXY_PORT="${PROXY_PORT:-18001}" |
| 12 | + |
| 13 | +# Pick a public repo that supports anonymous HTTPS. |
| 14 | +TEST_REMOTE_URL="${TEST_REMOTE_URL:-https://github.com/Dream95/gotproxy}" |
| 15 | + |
| 16 | +LOG_FILE="" |
| 17 | +GOTPROXY_PID="" |
| 18 | +PASSED=0 |
| 19 | +FAILED=0 |
| 20 | + |
| 21 | +info() { echo "[INFO] $*"; } |
| 22 | +ok() { echo "[OK] $*"; ((PASSED++)) || true; } |
| 23 | +fail() { echo "[FAIL] $*"; ((FAILED++)) || true; } |
| 24 | +abort() { echo "[ABORT] $*"; stop_gotproxy 2>/dev/null; exit 1; } |
| 25 | + |
| 26 | +start_gotproxy() { |
| 27 | + local extra_args=("$@") |
| 28 | + LOG_FILE=$(mktemp) |
| 29 | + "$GOTPROXY_BIN" --p-port "$PROXY_PORT" "${extra_args[@]}" >"$LOG_FILE" 2>&1 & |
| 30 | + GOTPROXY_PID=$! |
| 31 | + for i in {1..30}; do |
| 32 | + if grep -q "listening on" "$LOG_FILE" 2>/dev/null; then |
| 33 | + return 0 |
| 34 | + fi |
| 35 | + sleep 0.2 |
| 36 | + done |
| 37 | + abort "gotproxy did not start in time, see $LOG_FILE" |
| 38 | +} |
| 39 | + |
| 40 | +stop_gotproxy() { |
| 41 | + if [[ -n "$GOTPROXY_PID" ]] && kill -0 "$GOTPROXY_PID" 2>/dev/null; then |
| 42 | + kill "$GOTPROXY_PID" 2>/dev/null || true |
| 43 | + wait "$GOTPROXY_PID" 2>/dev/null || true |
| 44 | + fi |
| 45 | + GOTPROXY_PID="" |
| 46 | + [[ -n "$LOG_FILE" && -f "$LOG_FILE" ]] && rm -f "$LOG_FILE" |
| 47 | + LOG_FILE="" |
| 48 | +} |
| 49 | + |
| 50 | +count_original_dest() { |
| 51 | + [[ -z "$LOG_FILE" || ! -f "$LOG_FILE" ]] && echo 0 && return |
| 52 | + local c |
| 53 | + c=$(grep -c "Original destination:" "$LOG_FILE" 2>/dev/null) |
| 54 | + echo "${c:-0}" |
| 55 | +} |
| 56 | + |
| 57 | +check_env() { |
| 58 | + if [[ "$(id -u)" -ne 0 ]]; then |
| 59 | + abort "Please run as root: sudo $0" |
| 60 | + fi |
| 61 | + if [[ ! -x "$GOTPROXY_BIN" ]]; then |
| 62 | + abort "gotproxy not found or not executable: $GOTPROXY_BIN. Run make build-bpf && make first." |
| 63 | + fi |
| 64 | + if ! command -v git &>/dev/null; then |
| 65 | + abort "git not found. Please install git." |
| 66 | + fi |
| 67 | + info "Using gotproxy=$GOTPROXY_BIN, port=$PROXY_PORT" |
| 68 | + info "Using TEST_REMOTE_URL=$TEST_REMOTE_URL" |
| 69 | +} |
| 70 | + |
| 71 | +git_env() { |
| 72 | + # Avoid any interactive prompts |
| 73 | + export GIT_TERMINAL_PROMPT=0 |
| 74 | + export GIT_ASKPASS=true |
| 75 | +} |
| 76 | + |
| 77 | +test_git_ls_remote_proxied() { |
| 78 | + info "Test: git ls-remote via gotproxy (--cmd git)" |
| 79 | + start_gotproxy |
| 80 | + local n0 n1 |
| 81 | + n0=$(count_original_dest) |
| 82 | + |
| 83 | + git_env |
| 84 | + if git ls-remote --heads --tags "$TEST_REMOTE_URL" &>/dev/null; then |
| 85 | + n1=$(count_original_dest) |
| 86 | + if [[ "$n1" -gt "$n0" ]]; then |
| 87 | + ok "git ls-remote succeeded and traffic appears proxied (log increased: $n0 -> $n1)" |
| 88 | + else |
| 89 | + fail "git ls-remote succeeded but proxy log did not show forwarding (log: $n0 -> $n1)" |
| 90 | + fi |
| 91 | + else |
| 92 | + local log_snapshot="" |
| 93 | + [[ -n "$LOG_FILE" && -f "$LOG_FILE" ]] && log_snapshot=$(cat "$LOG_FILE") |
| 94 | + stop_gotproxy |
| 95 | + fail "git ls-remote failed (see gotproxy log below)" |
| 96 | + [[ -n "$log_snapshot" ]] && { info "gotproxy log:"; echo "$log_snapshot"; } |
| 97 | + return |
| 98 | + fi |
| 99 | + |
| 100 | + stop_gotproxy |
| 101 | +} |
| 102 | + |
| 103 | +test_git_clone_proxied() { |
| 104 | + info "Test: git clone --depth 1 via gotproxy (--cmd git)" |
| 105 | + start_gotproxy |
| 106 | + local n0 n1 |
| 107 | + n0=$(count_original_dest) |
| 108 | + |
| 109 | + git_env |
| 110 | + local tmp_dir repo_dir |
| 111 | + tmp_dir=$(mktemp -d) |
| 112 | + repo_dir="$tmp_dir/repo" |
| 113 | + # Ensure cleanup |
| 114 | + trap 'rm -rf "$tmp_dir"' RETURN |
| 115 | + |
| 116 | + if git clone --depth 1 "$TEST_REMOTE_URL" "$repo_dir" &>/dev/null; then |
| 117 | + n1=$(count_original_dest) |
| 118 | + if [[ -d "$repo_dir/.git" ]] && [[ "$n1" -gt "$n0" ]]; then |
| 119 | + ok "git clone succeeded and traffic appears proxied (log increased: $n0 -> $n1)" |
| 120 | + elif [[ -d "$repo_dir/.git" ]]; then |
| 121 | + fail "git clone succeeded but proxy log did not show forwarding (log: $n0 -> $n1)" |
| 122 | + else |
| 123 | + fail "git clone reported success but repo dir looks wrong: $repo_dir" |
| 124 | + fi |
| 125 | + else |
| 126 | + local log_snapshot="" |
| 127 | + [[ -n "$LOG_FILE" && -f "$LOG_FILE" ]] && log_snapshot=$(cat "$LOG_FILE") |
| 128 | + stop_gotproxy |
| 129 | + fail "git clone failed (see gotproxy log below)" |
| 130 | + [[ -n "$log_snapshot" ]] && { info "gotproxy log:"; echo "$log_snapshot"; } |
| 131 | + return |
| 132 | + fi |
| 133 | + |
| 134 | + stop_gotproxy |
| 135 | +} |
| 136 | + |
| 137 | +main() { |
| 138 | + check_env |
| 139 | + echo "==========================================" |
| 140 | + echo " gotproxy git tests" |
| 141 | + echo "==========================================" |
| 142 | + test_git_ls_remote_proxied |
| 143 | + test_git_clone_proxied |
| 144 | + echo "==========================================" |
| 145 | + echo " Passed: $PASSED Failed: $FAILED" |
| 146 | + echo "==========================================" |
| 147 | + [[ "$FAILED" -eq 0 ]] && exit 0 || exit 1 |
| 148 | +} |
| 149 | + |
| 150 | +trap 'stop_gotproxy; exit 130' INT TERM |
| 151 | +main "$@" |
0 commit comments