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