File: test_file_overwrite.sh

package info (click to toggle)
sscg 4.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 592 kB
  • sloc: ansic: 5,956; sh: 712; makefile: 11
file content (309 lines) | stat: -rwxr-xr-x 9,044 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later WITH cryptsetup-OpenSSL-exception

# This file is part of sscg.
#
# sscg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# sscg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with sscg.  If not, see <http://www.gnu.org/licenses/>.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the
# OpenSSL library under certain conditions as described in each
# individual source file, and distribute linked combinations
# including the two.
# You must obey the GNU General Public License in all respects
# for all of the code used other than OpenSSL.  If you modify
# file(s) with this exception, you may extend this exception to your
# version of the file(s), but you are not obligated to do so.  If you
# do not wish to do so, delete this exception statement from your
# version.  If you delete this exception statement from all source
# files in the program, then also delete it here.
#
# Copyright 2025 by Stephen Gallagher <sgallagh@redhat.com>

# Test for file overwrite behavior
# This tests that sscg correctly handles pre-existing files both with
# and without the --force flag.

# Note: We don't use 'set -e' here because we expect some commands to fail
set +e

# If we're running in a CI environment, use the workspace directory
TMPDIR=$(mktemp --directory --tmpdir=${GITHUB_WORKSPACE:-/tmp} sscg_overwrite_test_XXXXXX)

function cleanup {
    exitcode=$?
    rm -rf "$TMPDIR"
    return $exitcode
}

trap cleanup EXIT

failed_tests=0
total_tests=7

# Helper function to get file hash
function get_hash {
    local file="$1"
    if command -v sha256sum >/dev/null 2>&1; then
        sha256sum "$file" | cut -d' ' -f1
    else
        md5sum "$file" | cut -d' ' -f1
    fi
}

# Helper function to check if file is non-zero
function is_nonzero {
    local file="$1"
    [ -f "$file" ] && [ -s "$file" ]
}

echo "Running file overwrite behavior tests..."
echo "=========================================="
echo

# Test 1: Clean directory - files should be created
echo "Test 1: Clean directory - all files should be created"
TEST_DIR="$TMPDIR/test1"
mkdir -p "$TEST_DIR"
pushd "$TEST_DIR" >/dev/null

"${MESON_BUILD_ROOT}/sscg" --debug >/dev/null 2>&1
exit_code=$?

if [ $exit_code -ne 0 ]; then
    echo "  FAIL: sscg failed with exit code $exit_code"
    ((failed_tests++))
elif ! is_nonzero "ca.crt"; then
    echo "  FAIL: ca.crt was not created or is empty"
    ((failed_tests++))
elif ! is_nonzero "service-key.pem"; then
    echo "  FAIL: service-key.pem was not created or is empty"
    ((failed_tests++))
elif ! is_nonzero "service.pem"; then
    echo "  FAIL: service.pem was not created or is empty"
    ((failed_tests++))
else
    echo "  PASS: All files created successfully"
fi

popd >/dev/null
echo

# Test 2: Pre-existing ca.crt WITHOUT --force
echo "Test 2: Pre-existing ca.crt WITHOUT --force - should fail, file unchanged"
TEST_DIR="$TMPDIR/test2"
mkdir -p "$TEST_DIR"
pushd "$TEST_DIR" >/dev/null

echo "Original ca.crt content" > ca.crt
original_hash=$(get_hash ca.crt)

"${MESON_BUILD_ROOT}/sscg" --debug >/dev/null 2>&1
exit_code=$?

new_hash=$(get_hash ca.crt)

if [ $exit_code -ne 17 ]; then
    echo "  FAIL: Expected exit code 17 (EEXIST), got $exit_code"
    ((failed_tests++))
elif [ "$original_hash" != "$new_hash" ]; then
    echo "  FAIL: ca.crt content was modified"
    ((failed_tests++))
else
    echo "  PASS: Command failed correctly, file unchanged"
fi

popd >/dev/null
echo

# Test 3: Pre-existing service-key.pem WITHOUT --force
echo "Test 3: Pre-existing service-key.pem WITHOUT --force - should fail, file unchanged"
TEST_DIR="$TMPDIR/test3"
mkdir -p "$TEST_DIR"
pushd "$TEST_DIR" >/dev/null

echo "Original service-key.pem content" > service-key.pem
original_hash=$(get_hash service-key.pem)

"${MESON_BUILD_ROOT}/sscg" --debug >/dev/null 2>&1
exit_code=$?

new_hash=$(get_hash service-key.pem)

if [ $exit_code -ne 17 ]; then
    echo "  FAIL: Expected exit code 17 (EEXIST), got $exit_code"
    ((failed_tests++))
elif [ "$original_hash" != "$new_hash" ]; then
    echo "  FAIL: service-key.pem content was modified"
    ((failed_tests++))
else
    echo "  PASS: Command failed correctly, file unchanged"
fi

popd >/dev/null
echo

# Test 4: Pre-existing service.pem WITHOUT --force
echo "Test 4: Pre-existing service.pem WITHOUT --force - should fail, file unchanged"
TEST_DIR="$TMPDIR/test4"
mkdir -p "$TEST_DIR"
pushd "$TEST_DIR" >/dev/null

echo "Original service.pem content" > service.pem
original_hash=$(get_hash service.pem)

"${MESON_BUILD_ROOT}/sscg" --debug >/dev/null 2>&1
exit_code=$?

new_hash=$(get_hash service.pem)

if [ $exit_code -ne 17 ]; then
    echo "  FAIL: Expected exit code 17 (EEXIST), got $exit_code"
    ((failed_tests++))
elif [ "$original_hash" != "$new_hash" ]; then
    echo "  FAIL: service.pem content was modified"
    ((failed_tests++))
else
    echo "  PASS: Command failed correctly, file unchanged"
fi

popd >/dev/null
echo

# Test 5: Pre-existing ca.crt WITH --force
echo "Test 5: Pre-existing ca.crt WITH --force - should succeed, file changed"
TEST_DIR="$TMPDIR/test5"
mkdir -p "$TEST_DIR"
pushd "$TEST_DIR" >/dev/null

echo "Original ca.crt content" > ca.crt
original_hash=$(get_hash ca.crt)

"${MESON_BUILD_ROOT}/sscg" --debug --force >/dev/null 2>&1
exit_code=$?

new_hash=$(get_hash ca.crt)

if [ $exit_code -ne 0 ]; then
    echo "  FAIL: sscg failed with exit code $exit_code"
    ((failed_tests++))
elif [ "$original_hash" = "$new_hash" ]; then
    echo "  FAIL: ca.crt content was not changed"
    ((failed_tests++))
elif ! is_nonzero "ca.crt"; then
    echo "  FAIL: ca.crt is empty or missing"
    ((failed_tests++))
elif ! is_nonzero "service-key.pem"; then
    echo "  FAIL: service-key.pem is empty or missing"
    ((failed_tests++))
elif ! is_nonzero "service.pem"; then
    echo "  FAIL: service.pem is empty or missing"
    ((failed_tests++))
else
    echo "  PASS: All files created/overwritten successfully"
fi

popd >/dev/null
echo

# Test 6: Pre-existing service-key.pem WITH --force
echo "Test 6: Pre-existing service-key.pem WITH --force - should succeed, file changed"
TEST_DIR="$TMPDIR/test6"
mkdir -p "$TEST_DIR"
pushd "$TEST_DIR" >/dev/null

echo "Original service-key.pem content" > service-key.pem
original_hash=$(get_hash service-key.pem)

"${MESON_BUILD_ROOT}/sscg" --debug --force >/dev/null 2>&1
exit_code=$?

new_hash=$(get_hash service-key.pem)

if [ $exit_code -ne 0 ]; then
    echo "  FAIL: sscg failed with exit code $exit_code"
    ((failed_tests++))
elif [ "$original_hash" = "$new_hash" ]; then
    echo "  FAIL: service-key.pem content was not changed"
    ((failed_tests++))
elif ! is_nonzero "ca.crt"; then
    echo "  FAIL: ca.crt is empty or missing"
    ((failed_tests++))
elif ! is_nonzero "service-key.pem"; then
    echo "  FAIL: service-key.pem is empty or missing"
    ((failed_tests++))
elif ! is_nonzero "service.pem"; then
    echo "  FAIL: service.pem is empty or missing"
    ((failed_tests++))
else
    echo "  PASS: All files created/overwritten successfully"
fi

popd >/dev/null
echo

# Test 7: Pre-existing service.pem WITH --force
echo "Test 7: Pre-existing service.pem WITH --force - should succeed, file changed"
TEST_DIR="$TMPDIR/test7"
mkdir -p "$TEST_DIR"
pushd "$TEST_DIR" >/dev/null

echo "Original service.pem content" > service.pem
original_hash=$(get_hash service.pem)

"${MESON_BUILD_ROOT}/sscg" --debug --force >/dev/null 2>&1
exit_code=$?

new_hash=$(get_hash service.pem)

if [ $exit_code -ne 0 ]; then
    echo "  FAIL: sscg failed with exit code $exit_code"
    ((failed_tests++))
elif [ "$original_hash" = "$new_hash" ]; then
    echo "  FAIL: service.pem content was not changed"
    ((failed_tests++))
elif ! is_nonzero "ca.crt"; then
    echo "  FAIL: ca.crt is empty or missing"
    ((failed_tests++))
elif ! is_nonzero "service-key.pem"; then
    echo "  FAIL: service-key.pem is empty or missing"
    ((failed_tests++))
elif ! is_nonzero "service.pem"; then
    echo "  FAIL: service.pem is empty or missing"
    ((failed_tests++))
else
    echo "  PASS: All files created/overwritten successfully"
fi

popd >/dev/null
echo

# Summary
echo "=========================================="
echo "Test Summary:"
echo "============="
echo "Total tests: $total_tests"
echo "Failed tests: $failed_tests"
echo "Passed tests: $((total_tests - failed_tests))"

if [ "$failed_tests" -gt 0 ]; then
    echo
    echo "Some tests failed!"
    exit 1
else
    echo
    echo "All tests passed!"
    exit 0
fi