这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit e6ecdca

Browse files
Added: Add testing framework via tests/termux-exec-tests.in and re-add some tests initially added in 5ea25ee and removed in xxxxx
- Move unit tests from `src/termux-exec.c` to `tests/TermuxExecUnitTests.c` since tests shouldn't be in source files. - Added testing framework via `tests/termux-exec-tests.in` and `lib/termux-exec_c/tests/libtermux-exec_c_tests.in` that calls `termux-exec/lib/termux-exec_c/tests/src/libtermux-exec_c_unit-binary-tests.c` to run unit tests, and `lib/termux-exec_c/tests/src/libtermux-exec_c_runtime-binary-tests.c` and `lib/termux-exec_c/tests/scripts/libtermux-exec_c_runtime-script-tests.in` for runtime tests. Old tests files in random places have been removed. The entire `exec()` family of functions is also tested by `lib/termux-exec_c/tests/src/termux/api/termux_exec/exec/ExecIntercept_RuntimeBinaryTests.c`. Docs will be added in a later commit.Tests can be run with `"${TERMUX__PREFIX:-$PREFIX}/libexec/installed-tests/termux-exec/termux-exec-tests -vv all"`.
1 parent 9f26d30 commit e6ecdca

29 files changed

+2384
-114
lines changed

Makefile

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ CLANG_TIDY ?= clang-tidy
231231
.NOTPARALLEL:
232232

233233
all: | pre-build build-libtermux-exec_c build-libtermux-exec-ld-preload
234+
@printf "\ntermux-exec-package: %s\n" "Building tests/*"
235+
@mkdir -p $(TESTS_BUILD_OUTPUT_DIR)
236+
find tests -maxdepth 1 -type f -name "*.in" -print0 | xargs -0 -n1 sh -c \
237+
'output_file="$(TESTS_BUILD_OUTPUT_DIR)/$$(printf "%s" "$$0" | sed -e "s|^tests/||" -e "s/\.in$$//")" && mkdir -p "$$(dirname "$$output_file")" && sed $(TERMUX__CONSTANTS__SED_ARGS) "$$0" > "$$output_file"'
238+
find $(TESTS_BUILD_OUTPUT_DIR) -maxdepth 1 -type f -exec chmod 700 "{}" \;
239+
240+
234241
@printf "\ntermux-exec-package: %s\n" "Building packaging/debian/*"
235242
@mkdir -p $(DEBIAN_PACKAGING_BUILD_OUTPUT_DIR)
236243
find packaging/debian -mindepth 1 -maxdepth 1 -type f -name "*.in" -exec sh -c \
@@ -276,6 +283,76 @@ build-libtermux-exec_c:
276283

277284
@printf "\ntermux-exec-package: %s\n" "Building lib/libtermux-exec_c.a"
278285
$(AR) rcs $(LIB_BUILD_OUTPUT_DIR)/libtermux-exec_c.a $(LIBTERMUX_EXEC__C__OBJECT_FILES)
286+
287+
288+
289+
@printf "\ntermux-exec-package: %s\n" "Building lib/termux-exec_c/tests/*"
290+
@mkdir -p $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)
291+
292+
293+
@printf "\ntermux-exec-package: %s\n" "Building lib/termux-exec_c/tests/libtermux-exec_c_tests"
294+
$(call replace-termux-constants,lib/termux-exec_c/tests/libtermux-exec_c_tests,$(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR))
295+
chmod 700 $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/libtermux-exec_c_tests
296+
297+
298+
@printf "\ntermux-exec-package: %s\n" "Building lib/termux-exec_c/tests/bin/libtermux-exec_c_unit-binary-tests"
299+
@mkdir -p $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin
300+
301+
@# `nm --demangle --defined-only --extern-only /home/builder/.termux-build/termux-exec/src/build/output/usr/libexec/installed-tests/termux-exec/lib/termux-exec_c/bin/libtermux-exec_c_unit-binary-tests-fsanitize`
302+
$(TERMUX_EXEC_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \
303+
$(FSANTIZE_FLAGS) \
304+
-o $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-exec_c_unit-binary-tests-fsanitize \
305+
lib/termux-exec_c/tests/src/libtermux-exec_c_unit-binary-tests.c \
306+
$(TERMUX_EXEC_EXECUTABLE__C__POST_LDFLAGS)
307+
308+
@# `nm --demangle --defined-only --extern-only /home/builder/.termux-build/termux-exec/src/build/output/usr/libexec/installed-tests/termux-exec/lib/termux-exec_c/bin/libtermux-exec_c_unit-binary-tests-nofsanitize`
309+
$(TERMUX_EXEC_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \
310+
-o $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-exec_c_unit-binary-tests-nofsanitize \
311+
lib/termux-exec_c/tests/src/libtermux-exec_c_unit-binary-tests.c \
312+
$(TERMUX_EXEC_EXECUTABLE__C__POST_LDFLAGS)
313+
314+
315+
@printf "\ntermux-exec-package: %s\n" "Building lib/termux-exec_c/tests/bin/libtermux-exec_c_runtime-binary-tests$(TERMUX_EXEC_PKG__TESTS__API_LEVEL)"
316+
@mkdir -p $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin
317+
318+
$(TERMUX_EXEC_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \
319+
$(FSANTIZE_FLAGS) \
320+
-o $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-exec_c_runtime-binary-tests-fsanitize$(TERMUX_EXEC_PKG__TESTS__API_LEVEL) \
321+
lib/termux-exec_c/tests/src/libtermux-exec_c_runtime-binary-tests.c \
322+
$(TERMUX_EXEC_EXECUTABLE__C__POST_LDFLAGS)
323+
$(TERMUX_EXEC_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \
324+
-o $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-exec_c_runtime-binary-tests-nofsanitize$(TERMUX_EXEC_PKG__TESTS__API_LEVEL) \
325+
lib/termux-exec_c/tests/src/libtermux-exec_c_runtime-binary-tests.c \
326+
$(TERMUX_EXEC_EXECUTABLE__C__POST_LDFLAGS)
327+
328+
329+
@printf "\ntermux-exec-package: %s\n" "Building lib/termux-exec_c/tests/scripts/*"
330+
@mkdir -p $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/scripts
331+
find lib/termux-exec_c/tests/scripts -type f -name '*.c' -print0 | xargs -0 -n1 sh -c \
332+
'output_file="$(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/scripts/$$(printf "%s" "$$0" | sed -e "s|^lib/termux-exec_c/tests/scripts/||" -e "s/\.c$$//")" && mkdir -p "$$(dirname "$$output_file")" && $(CC) $(CFLAGS) -O0 -fPIE -pie $(LDFLAGS) -g "$$0" -o "$$output_file"'
333+
find lib/termux-exec_c/tests/scripts -type f -name '*.sh' -print0 | xargs -0 -n1 sh -c \
334+
'output_file="$(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/scripts/$$(printf "%s" "$$0" | sed -e "s|^lib/termux-exec_c/tests/scripts/||")" && mkdir -p "$$(dirname "$$output_file")" && cp -a "$$0" "$$output_file"'
335+
find lib/termux-exec_c/tests/scripts -type f -name "*.in" -print0 | xargs -0 -n1 sh -c \
336+
'output_file="$(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/scripts/$$(printf "%s" "$$0" | sed -e "s|^lib/termux-exec_c/tests/scripts/||" -e "s/\.in$$//")" && mkdir -p "$$(dirname "$$output_file")" && sed $(TERMUX__CONSTANTS__SED_ARGS) "$$0" > "$$output_file"'
337+
find lib/termux-exec_c/tests/scripts -type l -print0 | xargs -0 -n1 sh -c \
338+
'output_file="$(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/scripts/$$(printf "%s" "$$0" | sed -e "s|^lib/termux-exec_c/tests/scripts/||")" && mkdir -p "$$(dirname "$$output_file")" && cp -a "$$0" "$$output_file"'
339+
find $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/scripts -type f -exec chmod 700 "{}" \;
340+
341+
342+
build-libtermux-exec_c_runtime-binary-tests:
343+
@printf "termux-exec-package: %s\n" "Building lib/termux-exec_c/tests/bin/libtermux-exec_c_runtime-binary-tests$(TERMUX_EXEC_PKG__TESTS__API_LEVEL)"
344+
@mkdir -p $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin
345+
346+
$(TERMUX_EXEC_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \
347+
$(FSANTIZE_FLAGS) \
348+
-o $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-exec_c_runtime-binary-tests-fsanitize$(TERMUX_EXEC_PKG__TESTS__API_LEVEL) \
349+
lib/termux-exec_c/tests/src/libtermux-exec_c_runtime-binary-tests.c \
350+
$(TERMUX_EXEC_EXECUTABLE__C__POST_LDFLAGS)
351+
$(TERMUX_EXEC_EXECUTABLE__C__BUILD_COMMAND) -O0 -g \
352+
-o $(LIBTERMUX_EXEC__C__TESTS_BUILD_OUTPUT_DIR)/bin/libtermux-exec_c_runtime-binary-tests-nofsanitize$(TERMUX_EXEC_PKG__TESTS__API_LEVEL) \
353+
lib/termux-exec_c/tests/src/libtermux-exec_c_runtime-binary-tests.c \
354+
$(TERMUX_EXEC_EXECUTABLE__C__POST_LDFLAGS)
355+
279356
build-libtermux-exec-ld-preload:
280357
@# Unlike `libtermux-exec_c.so` and `libtermux-exec_c.a`, all
281358
@# symbols are hidden, except the exported functions with
@@ -324,6 +401,10 @@ install:
324401
find $(TERMUX_EXEC_PKG__INSTALL_PREFIX)/include/termux-exec -type f -exec chmod 600 {} \;
325402

326403

404+
rm -rf $(TERMUX_EXEC_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-exec
405+
install -d $(TERMUX_EXEC_PKG__INSTALL_PREFIX)/libexec/installed-tests
406+
cp -a $(TESTS_BUILD_OUTPUT_DIR) $(TERMUX_EXEC_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-exec
407+
327408
@printf "\ntermux-exec-package: %s\n\n" "Install termux-exec-package successful"
328409

329410
uninstall:
@@ -338,6 +419,9 @@ uninstall:
338419
rm -f $(TERMUX_EXEC_PKG__INSTALL_PREFIX)/lib/libtermux-exec-ld-preload.so
339420
rm -f $(TERMUX_EXEC_PKG__INSTALL_PREFIX)/lib/libtermux-exec.so
340421

422+
423+
rm -rf $(TERMUX_EXEC_PKG__INSTALL_PREFIX)/libexec/installed-tests/termux-exec
424+
341425
@printf "\ntermux-exec-package: %s\n\n" "Uninstall termux-exec-package successful"
342426

343427

@@ -347,6 +431,32 @@ packaging-debian-build: all
347431

348432

349433

434+
test: all
435+
$(MAKE) TERMUX_EXEC_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install
436+
437+
@printf "\ntermux-exec-package: %s\n" "Executing termux-exec-package tests"
438+
bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-exec/termux-exec-tests \
439+
--tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-exec" \
440+
--ld-preload="$(PREFIX_BUILD_INSTALL_DIR)/lib/libtermux-exec-ld-preload.so" \
441+
-vvv all
442+
443+
test-unit: all
444+
$(MAKE) TERMUX_EXEC_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install
445+
446+
@printf "\ntermux-exec-package: %s\n" "Executing termux-exec-package unit tests"
447+
bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-exec/termux-exec-tests \
448+
--tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-exec" \
449+
-vvv unit
450+
451+
test-runtime: all
452+
$(MAKE) TERMUX_EXEC_PKG__INSTALL_PREFIX=$(PREFIX_BUILD_INSTALL_DIR) install
453+
454+
@printf "\ntermux-exec-package: %s\n" "Executing termux-exec-package runtime tests"
455+
bash $(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-exec/termux-exec-tests \
456+
--tests-path="$(PREFIX_BUILD_INSTALL_DIR)/libexec/installed-tests/termux-exec" \
457+
--ld-preload="$(PREFIX_BUILD_INSTALL_DIR)/lib/libtermux-exec-ld-preload.so" \
458+
-vvv runtime
459+
350460

351461

352462
format:
@@ -360,4 +470,4 @@ check:
360470

361471

362472

363-
.PHONY: all pre-build build-libtermux-exec_c build-libtermux-exec-ld-preload clean install uninstall packaging-debian-build format check
473+
.PHONY: all pre-build build-libtermux-exec_c build-libtermux-exec_c_runtime-binary-tests build-libtermux-exec-ld-preload clean install uninstall packaging-debian-build test test-unit test-runtime format check

lib/termux-exec_c/include/termux/termux_exec__c/v1/termux/shell/command/environment/termux_exec/TermuxExecShellEnvironment.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,36 @@ static const int E_DEF_VAL__TERMUX_EXEC__SYSTEM_LINKER_EXEC = 1;
8181

8282

8383

84+
/*
85+
* Environment for `termux-exec-tests`.
86+
*/
87+
88+
/**
89+
* Environment variable for the log level for `termux-exec-tests`.
90+
*
91+
* Type: `int`
92+
* Default key: `TERMUX_EXEC__TESTS__LOG_LEVEL`
93+
* Default value: DEFAULT_LOG_LEVEL
94+
* Values:
95+
* - `0` (`OFF`) - Log nothing.
96+
* - `1` (`NORMAL`) - Log error, warn and info messages and stacktraces.
97+
* - `2` (`DEBUG`) - Log debug messages.
98+
* - `3` (`VERBOSE`) - Log verbose messages.
99+
* - `4` (`VVERBOSE`) - Log very verbose messages.
100+
*/
101+
#define ENV__TERMUX_EXEC__TESTS__LOG_LEVEL TERMUX_ENV__S_TERMUX_EXEC__TESTS "LOG_LEVEL"
102+
103+
104+
105+
/**
106+
* Environment variable for the path to the termux-exec tests.
107+
*/
108+
#define ENV__TERMUX_EXEC__TESTS__TESTS_PATH TERMUX_ENV__S_TERMUX_EXEC__TESTS "TESTS_PATH"
109+
110+
111+
112+
113+
84114
/**
85115
* Returns the `termux-exec` config for `Logger` log level
86116
* based on the `ENV__TERMUX_EXEC__LOG_LEVEL` env variable.
@@ -115,6 +145,17 @@ int getTermuxExecSystemLinkerExecConfig();
115145

116146

117147

148+
/**
149+
* Returns the `termux-exec-tests` config for `Logger` log level
150+
* based on the `ENV__TERMUX_EXEC__TESTS__LOG_LEVEL` env variable.
151+
*
152+
* @return Return the value if `ENV__TERMUX_EXEC__TESTS__LOG_LEVEL` is
153+
* set, otherwise defaults to `DEFAULT_LOG_LEVEL`.
154+
*/
155+
int getTermuxExecTestsLogLevel();
156+
157+
158+
118159
#ifdef __cplusplus
119160
}
120161
#endif

lib/termux-exec_c/src/termux/shell/command/environment/termux_exec/TermuxExecShellEnvironment.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ int getTermuxExecSystemLinkerExecConfig() {
3636
}
3737
return def;
3838
}
39+
40+
41+
42+
int getTermuxExecTestsLogLevel() {
43+
return getLogLevelFromEnv(ENV__TERMUX_EXEC__TESTS__LOG_LEVEL);
44+
}

0 commit comments

Comments
 (0)