1f08c3bdfSopenharmony_ciLTP Library API Writing Guidelines 2f08c3bdfSopenharmony_ci================================== 3f08c3bdfSopenharmony_ci 4f08c3bdfSopenharmony_ciNOTE: See also 5f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[Test Writing Guidelines], 6f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/C-Test-API[C Test API], 7f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/Shell-Test-API[Shell Test API]. 8f08c3bdfSopenharmony_ci 9f08c3bdfSopenharmony_ci1. General Rules 10f08c3bdfSopenharmony_ci---------------- 11f08c3bdfSopenharmony_ci 12f08c3bdfSopenharmony_ciFor extending library API it applies the same general rules as for writing tests, 13f08c3bdfSopenharmony_ci(see https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[Test Writing Guidelines], 14f08c3bdfSopenharmony_cioffline: 'doc/test-writing-guidelines.txt'), 15f08c3bdfSopenharmony_ciwith strong focus on readability and simplicity. 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ciLibrary tests are in 'lib/newlib_tests' directory. 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ciDon't forget to update docs when you change the API. 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ciEnvironment variables should be listed in 22f08c3bdfSopenharmony_cihttps://github.com/linux-test-project/ltp/wiki/User-Guidelines[LTP User Guidelines] 23f08c3bdfSopenharmony_ciand in help output (`-h`) for both C and shell API. 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci2. C API 26f08c3bdfSopenharmony_ci-------- 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci2.1 LTP-001: Sources have tst_ prefix 29f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ciAPI source code is in headers in 'include/{empty}*.h', 'include/lapi/{empty}*.h' 32f08c3bdfSopenharmony_ci(backward compatibility for old kernel and libc) and C sources in 'lib/{empty}*.c'. 33f08c3bdfSopenharmony_ciFiles have `tst_` prefix. 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci2.2 LTP-002: TST_RET and TST_ERR are not modified 36f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ciThe test author is guaranteed that the test API will not modify these 39f08c3bdfSopenharmony_civariables. This prevents silent errors where the return value and 40f08c3bdfSopenharmony_cierrno are overwritten before the test has chance to check them. 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ciThe macros which are clearly intended to update these variables. That 43f08c3bdfSopenharmony_ciis `TEST` and those in 'tst_test_macros.h'. Are of course allowed to 44f08c3bdfSopenharmony_ciupdate these variables. 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ci2.3 LTP-003: Externally visible library symbols have the tst_ prefix 47f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ciFunctions, types and variables in the public test API should have the 50f08c3bdfSopenharmony_citst_ prefix. With some exceptions for symbols already prefixed with 51f08c3bdfSopenharmony_ci`safe_` or `ltp_`. 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ciStatic (private) symbols should not have the prefix. 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci3. Shell API 57f08c3bdfSopenharmony_ci------------ 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ciAPI source code is in 'tst_test.sh', 'tst_security.sh' and 'tst_net.sh' 60f08c3bdfSopenharmony_ci(all in 'testcases/lib' directory). 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ciChanges in the shell API should not introduce uncommon dependencies 63f08c3bdfSopenharmony_ci(use basic commands installed everywhere by default). 64f08c3bdfSopenharmony_ci 65f08c3bdfSopenharmony_ci3.1 Shell libraries 66f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~ 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ciAside from shell API libraries in 'testcases/lib', it's worth putting 69f08c3bdfSopenharmony_cicommon code for a group of tests into a shell library. The filename 70f08c3bdfSopenharmony_cishould end with '_lib.sh' and the library should load 'tst_test.sh' or 71f08c3bdfSopenharmony_ci'tst_net.sh'. 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ciShell libraries should have conditional expansion for 'TST_SETUP' or 'TST_CLEANUP', 74f08c3bdfSopenharmony_cito avoid surprises when test specific setup/cleanup function is redefined by 75f08c3bdfSopenharmony_cishell library. 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci[source,sh] 78f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 79f08c3bdfSopenharmony_ci# ipsec_lib.sh 80f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later 81f08c3bdfSopenharmony_ciTST_SETUP="${TST_SETUP:-ipsec_lib_setup}" 82f08c3bdfSopenharmony_ci... 83f08c3bdfSopenharmony_ci. tst_test.sh 84f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 85