-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathrun_tests.sh
More file actions
executable file
·143 lines (124 loc) · 3.71 KB
/
Copy pathrun_tests.sh
File metadata and controls
executable file
·143 lines (124 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
# Check if sshpass is installed
if ! command -v sshpass &> /dev/null; then
echo "Error: sshpass is not installed. Please install it and try again." >&2
exit 1
fi
# Check if docker is installed
if ! command -v docker &> /dev/null; then
echo "Error: docker is not installed. Please install it and try again." >&2
exit 1
fi
# Fire up all the containers
echo ""
echo "Starting the project"
docker compose up -d --build --force-recreate
echo "Project started"
# Wait for the dashboard to initialize
sleep 2
LABUSER='root'
LABPASS='ByteThem123'
LABHOST='127.0.0.1'
LABPORT=2222
CHALLENGES_DIR="challenges"
CAMPAIGNS_DIR="campaigns"
echo ""
echo "Starting all challenges"
response=$(curl -s -X POST http://127.0.0.1/api/challenges/start/all)
if [ "$response" != "All started! 🎉" ]; then
echo "Error starting all the challenges, got response: $response"
exit 3
fi
echo "All challenges started"
failed=()
solve () {
local chal_dir=$1
if [[ "$chal_dir" == *"template"* ]]; then
# skip template
return 0
fi
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "Testing $chal_dir:"
local solve_script="$chal_dir/auto-solve.sh"
local solve_deps="$chal_dir/auto-solve"
if [ -f "$solve_script" ]; then
# Copy auto-solve script into /tmp dir in hackerlab container
# We use ssh instead of docker exec to test also the SSH connection
sshpass -p "$LABPASS" scp -O \
-o LogLevel=error \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-P $LABPORT \
$solve_script \
$LABUSER@$LABHOST:/tmp/auto-solve.sh
# also copy all potential dependencies of the solve script
if [ -d "$solve_deps" ]; then
sshpass -p "$LABPASS" scp -O \
-o LogLevel=error \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-P $LABPORT \
-r \
$solve_deps \
$LABUSER@$LABHOST:/tmp/
fi
# Run the auto-solve script from within the hackerlab container
sshpass -p "$LABPASS" ssh \
-o LogLevel=error \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
-p $LABPORT \
$LABUSER@$LABHOST \
'cd /tmp && bash auto-solve.sh'
local retVal=$?
if [ $retVal -ne 0 ]; then
return 1
fi
else
echo "WARNING - missing $solve_script script"
return 0
fi
printf "\n\n\n"
}
for chal_dir in "$CHALLENGES_DIR"/*/; do
if ! solve "$chal_dir"; then
failed+=("$chal_dir")
fi
done
for campaign_dir in "$CAMPAIGNS_DIR"/*/; do
if [[ "$campaign_dir" == *"example"* ]]; then
# skip example
continue
fi
for chal_dir in "$campaign_dir"*/; do
if [[ "$chal_dir" == *"pages"* ]]; then
# skip static pages
continue
fi
if ! solve "$chal_dir"; then
failed+=("$chal_dir")
fi
done
done
echo ""
echo "Stopping all challenges"
response=$(curl -s -X POST http://127.0.0.1/api/challenges/stop/all)
if [ "$response" != "All stopped! 🎉" ]; then
echo "Error stopping all the challenges, got response: $response"
exit 3
fi
echo "All challenges stopped"
echo ""
echo "Stopping the project"
docker compose down
echo "Project stopped"
echo ""
if (( ${#failed[@]} != 0 )); then
echo "❌ TEST FAILED - some auto-solve.sh script(s) failed:"
for f in "${failed[@]}"; do
echo " - $f"
done
exit 2
else
echo "✅ ALL TESTS PASSED"
fi