xref: /aosp_15_r20/external/ltp/doc/old/C-Test-Case-Tutorial.asciidoc (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard WorkerC Test Case Tutorial
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
8*49cdfc7eSAndroid Build Coastguard WorkerThis is a step-by-step tutorial on writing a simple C LTP test, where topics
9*49cdfc7eSAndroid Build Coastguard Workerof the LTP and Linux kernel testing will be introduced gradually using a
10*49cdfc7eSAndroid Build Coastguard Workerconcrete example. Most sections will include exercises, some trivial and
11*49cdfc7eSAndroid Build Coastguard Workerothers not so much. If you find an exercise is leading you off at too much of
12*49cdfc7eSAndroid Build Coastguard Workera tangent, just leave it for later and move on.
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard WorkerLTP tests can be written in C or Shell script. This tutorial is only for tests
15*49cdfc7eSAndroid Build Coastguard Workerwritten in C using the new LTP test API. Note that while we go into some
16*49cdfc7eSAndroid Build Coastguard Workerdetail on using Git, this is not intended as a canonical or complete guide
17*49cdfc7eSAndroid Build Coastguard Workerfor Git.
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker0. Assumptions & Feedback
20*49cdfc7eSAndroid Build Coastguard Worker-------------------------
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard WorkerWe assume the reader is familiar with C, Git and common Unix/Linux/GNU tools
23*49cdfc7eSAndroid Build Coastguard Workerand has some general knowledge of Operating Systems. Experienced Linux
24*49cdfc7eSAndroid Build Coastguard Workerdevelopers may find it too verbose while people new to system level Linux
25*49cdfc7eSAndroid Build Coastguard Workerdevelopment may find it overwhelming.
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard WorkerComments and feedback are welcome, please direct them to
28*49cdfc7eSAndroid Build Coastguard Workerhttps://lists.linux.it/listinfo/ltp[the mailing list].
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker1. Getting Started
31*49cdfc7eSAndroid Build Coastguard Worker------------------
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard WorkerGit-clone the main LTP repository as described in
34*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp#quick-guide-to-running-the-tests[the +README.md+]
35*49cdfc7eSAndroid Build Coastguard Workerand change directory to the checked-out Git repository. We recommend installing the LTP
36*49cdfc7eSAndroid Build Coastguard Workerand running one of the tests mentioned in the Quick guide (in the +README.md+) to
37*49cdfc7eSAndroid Build Coastguard Workerensure you are starting from a good state.
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard WorkerWe also recommended cloning the Linux kernel repository for reference, this
40*49cdfc7eSAndroid Build Coastguard Workerguide will refer to files and directories within the mainline kernel 4.12.
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
43*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
44*49cdfc7eSAndroid Build Coastguard Worker$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
45*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard WorkerThere are a number of other repositories which are useful for reference as
48*49cdfc7eSAndroid Build Coastguard Workerwell, including the GNU C library +glibc+ and the alternative C library
49*49cdfc7eSAndroid Build Coastguard Worker+musl+. Some system calls are partially or even entirely implemented in user
50*49cdfc7eSAndroid Build Coastguard Workerland as part of the standard C library. So in these cases, the C library is an
51*49cdfc7eSAndroid Build Coastguard Workerimportant reference. +glibc+ is the most common C library for Linux, however
52*49cdfc7eSAndroid Build Coastguard Worker+musl+ is generally easier to understand.
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard WorkerHow system calls are implemented varies from one architecture to another and
55*49cdfc7eSAndroid Build Coastguard Workeracross kernel and C library versions. To find out whether a system call is
56*49cdfc7eSAndroid Build Coastguard Workeractually accessing the kernel (whether it is actually a system call) on any
57*49cdfc7eSAndroid Build Coastguard Workergiven machine you can use the +strace+ utility. This intercepts system calls
58*49cdfc7eSAndroid Build Coastguard Workermade by an executable and prints them. We will use this later in the tutorial.
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker2. Choose a System Call to test
61*49cdfc7eSAndroid Build Coastguard Worker-------------------------------
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard WorkerWe will use the +statx()+ system call, to provide a concrete example of a
64*49cdfc7eSAndroid Build Coastguard Workertest. At the time of writing there is no test for this call which was
65*49cdfc7eSAndroid Build Coastguard Workerintroduced in Linux kernel version 4.11.
66*49cdfc7eSAndroid Build Coastguard Worker
67*49cdfc7eSAndroid Build Coastguard WorkerLinux system call specific tests are primarily contained in
68*49cdfc7eSAndroid Build Coastguard Worker+testcases/kernel/syscalls+, but you should also +git grep+ the entire LTP
69*49cdfc7eSAndroid Build Coastguard Workerrepository to check for any existing usages of a system call.
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard WorkerOne way to find a system call which is not currently tested by the LTP is to
72*49cdfc7eSAndroid Build Coastguard Workerlook at +include/linux/syscalls.h+ in the kernel tree.
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard WorkerSomething the LTP excels at is ensuring bug-fixes are back ported to
75*49cdfc7eSAndroid Build Coastguard Workermaintenance releases, so targeting a specific regression is another
76*49cdfc7eSAndroid Build Coastguard Workeroption.
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker2.1. Find an untested System call
79*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard WorkerTry to find an untested system call which has a manual page (i.e. +man
82*49cdfc7eSAndroid Build Coastguard Workersyscall+ produces a result). It is a good idea to Git-clone the latest kernel
83*49cdfc7eSAndroid Build Coastguard Workerman-pages repository.
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
86*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
87*49cdfc7eSAndroid Build Coastguard Worker$ git clone git://git.kernel.org/pub/scm/docs/man-pages/man-pages.git
88*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard WorkerAt the time of writing the difference between the latest man-pages release and
91*49cdfc7eSAndroid Build Coastguard Workerthe +HEAD+ of the repository (usually the latest commit) is well over 100
92*49cdfc7eSAndroid Build Coastguard Workercommits. This represents about 9 weeks of changes. If you are using a stable
93*49cdfc7eSAndroid Build Coastguard WorkerLinux distribution, your man-pages package may well be years old. So as with
94*49cdfc7eSAndroid Build Coastguard Workerthe kernel, it is best to have the Git repository as a reference.
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard WorkerYou could also find a system call with untested parameters or use whatever it
97*49cdfc7eSAndroid Build Coastguard Workeris you are planning to use the LTP for.
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker3. Create the test skeleton
100*49cdfc7eSAndroid Build Coastguard Worker---------------------------
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard WorkerI shall call my test +statx01.c+, by the time you read this that file name
103*49cdfc7eSAndroid Build Coastguard Workerwill probably be taken, so increment the number in the file name as
104*49cdfc7eSAndroid Build Coastguard Workerappropriate or replace +statx+ with the system call chosen in exercise 2.1.
105*49cdfc7eSAndroid Build Coastguard Worker
106*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
107*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
108*49cdfc7eSAndroid Build Coastguard Worker$ mkdir testcases/kernel/syscalls/statx
109*49cdfc7eSAndroid Build Coastguard Worker$ cd testcases/kernel/syscalls/statx
110*49cdfc7eSAndroid Build Coastguard Worker$ echo statx >> .gitignore
111*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard WorkerNext open +statx01.c+ and add the following boilerplate. Make sure to change
114*49cdfc7eSAndroid Build Coastguard Workerthe copy right notice to your name/company, correct the test name and minimum
115*49cdfc7eSAndroid Build Coastguard Workerkernel version if necessary. I will explain what the code does below.
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker[source,c]
118*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
119*49cdfc7eSAndroid Build Coastguard Worker// SPDX-License-Identifier: GPL-2.0-or-later
120*49cdfc7eSAndroid Build Coastguard Worker/*
121*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2017 Instruction Ignorer <"can't"@be.bothered.com>
122*49cdfc7eSAndroid Build Coastguard Worker */
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker/*\
125*49cdfc7eSAndroid Build Coastguard Worker * [Description]
126*49cdfc7eSAndroid Build Coastguard Worker *
127*49cdfc7eSAndroid Build Coastguard Worker * All tests should start with a description of _what_ we are testing.
128*49cdfc7eSAndroid Build Coastguard Worker * Non-trivial explanations of _how_ the code works should also go here.
129*49cdfc7eSAndroid Build Coastguard Worker * Include relevant links, Git commit hashes and CVE numbers.
130*49cdfc7eSAndroid Build Coastguard Worker * Inline comments should be avoided.
131*49cdfc7eSAndroid Build Coastguard Worker */
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h"
134*49cdfc7eSAndroid Build Coastguard Worker
135*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void)
136*49cdfc7eSAndroid Build Coastguard Worker{
137*49cdfc7eSAndroid Build Coastguard Worker	tst_res(TPASS, "Doing hardly anything is easy");
138*49cdfc7eSAndroid Build Coastguard Worker}
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = {
141*49cdfc7eSAndroid Build Coastguard Worker	.test_all = run,
142*49cdfc7eSAndroid Build Coastguard Worker	.min_kver = "4.11",
143*49cdfc7eSAndroid Build Coastguard Worker};
144*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard WorkerStarting with the +#include+ statement we copy in the main LTP test library
147*49cdfc7eSAndroid Build Coastguard Workerheaders. This includes the most common test API functions and the test harness
148*49cdfc7eSAndroid Build Coastguard Workerinitialisation code. It is important to note that this is a completely
149*49cdfc7eSAndroid Build Coastguard Workerordinary, independent C program, however +main()+ is missing because it is
150*49cdfc7eSAndroid Build Coastguard Workerimplemented in +tst_test.h+.
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard WorkerWe specify what code we want to run as part of the test using the +tst_test
153*49cdfc7eSAndroid Build Coastguard Workertest+ structure. Various callbacks can be set by the test writer, including
154*49cdfc7eSAndroid Build Coastguard Worker+test.test_all+, which we have set to +run()+. The test harness will execute
155*49cdfc7eSAndroid Build Coastguard Workerthis callback in a separate process (using +fork()+), forcibly terminating it
156*49cdfc7eSAndroid Build Coastguard Workerif it does not return after +test.timeout+ seconds.
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard WorkerWe have also set +test.min_kver+ to the kernel version where +statx+ was
159*49cdfc7eSAndroid Build Coastguard Workerintroduced. The test library will determine the kernel version at runtime. If
160*49cdfc7eSAndroid Build Coastguard Workerthe version is less than 4.11 then the test harness will return +TCONF+,
161*49cdfc7eSAndroid Build Coastguard Workerindicating that this test is not suitable for the current system
162*49cdfc7eSAndroid Build Coastguard Workerconfiguration.
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard WorkerOccasionally features are back ported to older kernel versions, so +statx+ may
165*49cdfc7eSAndroid Build Coastguard Workerexist on kernels with a lower version. However we don't need to worry about
166*49cdfc7eSAndroid Build Coastguard Workerthat unless there is evidence of it happening.
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard WorkerAs mentioned in the code itself, you should specify what you are testing and
169*49cdfc7eSAndroid Build Coastguard Workerthe expected outcome, even if it is relatively simple. If your program flow is
170*49cdfc7eSAndroid Build Coastguard Workernecessarily complex and difficult to understand (which is often the case when
171*49cdfc7eSAndroid Build Coastguard Workertrying to manipulate the kernel into doing something bad), then a detailed
172*49cdfc7eSAndroid Build Coastguard Workerexplanation of how the code works is welcome.
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard WorkerWhat you should not do, is use inline comments or include the same level of
175*49cdfc7eSAndroid Build Coastguard Workerexplanation which is written here. As a general rule, if something is easy to
176*49cdfc7eSAndroid Build Coastguard Workerdocument, then the code should also be easy to read. So don't document the easy
177*49cdfc7eSAndroid Build Coastguard Workerstuff (except for the basic test specification).
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard WorkerBefore continuing we should compile this and check that the basics work. In
180*49cdfc7eSAndroid Build Coastguard Workerorder to compile the test we need a +Makefile+ in the same subdirectory. If
181*49cdfc7eSAndroid Build Coastguard Workerone already exists, then nothing needs to be done, otherwise add one with the
182*49cdfc7eSAndroid Build Coastguard Workerfollowing contents.
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker[source,make]
185*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
186*49cdfc7eSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0-or-later
187*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) 2019 Linux Test Project
188*49cdfc7eSAndroid Build Coastguard Worker
189*49cdfc7eSAndroid Build Coastguard Workertop_srcdir		?= ../../../..
190*49cdfc7eSAndroid Build Coastguard Worker
191*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/testcases.mk
192*49cdfc7eSAndroid Build Coastguard Worker
193*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/generic_leaf_target.mk
194*49cdfc7eSAndroid Build Coastguard Worker
195*49cdfc7eSAndroid Build Coastguard Worker------------------------------------------------------------------------------
196*49cdfc7eSAndroid Build Coastguard Worker
197*49cdfc7eSAndroid Build Coastguard WorkerThis will automatically add +statx01.c+ as a build target producing a
198*49cdfc7eSAndroid Build Coastguard Worker+statx01+ executable. Unless you have heavily deviated from the tutorial, and
199*49cdfc7eSAndroid Build Coastguard Workerprobably need to change +top_srcdir+, nothing else needs to be done.
200*49cdfc7eSAndroid Build Coastguard Worker
201*49cdfc7eSAndroid Build Coastguard WorkerNormally, if you were starting a Makefile from scratch, then you would need to
202*49cdfc7eSAndroid Build Coastguard Workeradd +statx01+ as a build target. Specifying that you would like to run some
203*49cdfc7eSAndroid Build Coastguard Workerprogram (e.g. +gcc+ or +clang+) to transform +statx01.c+ into +statx01+. Here
204*49cdfc7eSAndroid Build Coastguard Workerwe don't need to do that, but sometimes it is still necessary. For example, if
205*49cdfc7eSAndroid Build Coastguard Workerwe needed to link to the POSIX threading library, then we could add the
206*49cdfc7eSAndroid Build Coastguard Workerfollowing line after +testcases.mk+.
207*49cdfc7eSAndroid Build Coastguard Worker
208*49cdfc7eSAndroid Build Coastguard Worker[source,make]
209*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
210*49cdfc7eSAndroid Build Coastguard Workerstatx01: CFLAGS += -pthread
211*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard WorkerAssuming you are in the test's subdirectory +testcases/kernel/syscalls/statx+,
214*49cdfc7eSAndroid Build Coastguard Workerdo
215*49cdfc7eSAndroid Build Coastguard Worker
216*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
217*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
218*49cdfc7eSAndroid Build Coastguard Worker$ make
219*49cdfc7eSAndroid Build Coastguard Worker$ ./statx01
220*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard WorkerThis should build the test and then run it. However, even though the test is
223*49cdfc7eSAndroid Build Coastguard Workerin the +syscalls+ directory it won't be automatically ran as part of the
224*49cdfc7eSAndroid Build Coastguard Worker_syscalls_ test group (remember +./runltp -f syscalls+ from the +README.md+?). For
225*49cdfc7eSAndroid Build Coastguard Workerthis we need to add it to the +runtest+ file. So open +runtest/syscalls+ and add
226*49cdfc7eSAndroid Build Coastguard Workerthe lines starting with a +++.
227*49cdfc7eSAndroid Build Coastguard Worker
228*49cdfc7eSAndroid Build Coastguard Worker[source,diff]
229*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
230*49cdfc7eSAndroid Build Coastguard Worker statvfs01 statvfs01
231*49cdfc7eSAndroid Build Coastguard Worker statvfs02 statvfs02
232*49cdfc7eSAndroid Build Coastguard Worker
233*49cdfc7eSAndroid Build Coastguard Worker+statx01 statx01
234*49cdfc7eSAndroid Build Coastguard Worker+
235*49cdfc7eSAndroid Build Coastguard Worker stime01 stime01
236*49cdfc7eSAndroid Build Coastguard Worker stime02 stime02
237*49cdfc7eSAndroid Build Coastguard Worker
238*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
239*49cdfc7eSAndroid Build Coastguard Worker
240*49cdfc7eSAndroid Build Coastguard WorkerThe +runtest+ files are in a two column format. The first column is the test
241*49cdfc7eSAndroid Build Coastguard Workername, which is mainly used by test runners for reporting and filtering. It is
242*49cdfc7eSAndroid Build Coastguard Workerjust a single string of text with no spaces. The second column, which can
243*49cdfc7eSAndroid Build Coastguard Workercontain spaces, is passed to the shell in order to execute the test. Often it
244*49cdfc7eSAndroid Build Coastguard Workeris just the executable name, but some tests also take arguments (the LTP has a
245*49cdfc7eSAndroid Build Coastguard Workerlibrary for argument parsing, by the way).
246*49cdfc7eSAndroid Build Coastguard Worker
247*49cdfc7eSAndroid Build Coastguard WorkerIf you haven't done so already, we should add all these new files to Git. It
248*49cdfc7eSAndroid Build Coastguard Workeris vitally important that you do not make changes to the master branch. If you
249*49cdfc7eSAndroid Build Coastguard Workerdo then pulling changes from upstream becomes a major issue. So first of all
250*49cdfc7eSAndroid Build Coastguard Workercreate a new branch.
251*49cdfc7eSAndroid Build Coastguard Worker
252*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
253*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
254*49cdfc7eSAndroid Build Coastguard Worker$ git checkout -b statx01 master
255*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
256*49cdfc7eSAndroid Build Coastguard Worker
257*49cdfc7eSAndroid Build Coastguard WorkerNow we want to add the files we have created or modified, but before doing a
258*49cdfc7eSAndroid Build Coastguard Workercommit make sure you have configured Git correctly. You need to at least set
259*49cdfc7eSAndroid Build Coastguard Workeryour Name and e-mail address in +~/.gitconfig+, but there are some other
260*49cdfc7eSAndroid Build Coastguard Workersettings which come in handy too. My relatively simple configuration is similar to
261*49cdfc7eSAndroid Build Coastguard Workerthe below
262*49cdfc7eSAndroid Build Coastguard Worker
263*49cdfc7eSAndroid Build Coastguard Worker[source,conf]
264*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
265*49cdfc7eSAndroid Build Coastguard Worker[user]
266*49cdfc7eSAndroid Build Coastguard Worker	name = Sarah Jane
267*49cdfc7eSAndroid Build Coastguard Worker	email = [email protected]
268*49cdfc7eSAndroid Build Coastguard Worker[core]
269*49cdfc7eSAndroid Build Coastguard Worker	editor = emacs
270*49cdfc7eSAndroid Build Coastguard Worker[sendemail]
271*49cdfc7eSAndroid Build Coastguard Worker	smtpServer = smtp.server.address
272*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
273*49cdfc7eSAndroid Build Coastguard Worker
274*49cdfc7eSAndroid Build Coastguard WorkerObviously you need to at least change your name and e-mail. The SMTP server is
275*49cdfc7eSAndroid Build Coastguard Workeruseful for +git send-email+, which we will discuss later. The editor value is
276*49cdfc7eSAndroid Build Coastguard Workerused for things like writing commits (without the +-m+ option).
277*49cdfc7eSAndroid Build Coastguard Worker
278*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
279*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
280*49cdfc7eSAndroid Build Coastguard Worker$ git add -v :/testcases/kernel/syscalls/statx :/runtest/syscalls
281*49cdfc7eSAndroid Build Coastguard Worker$ git commit -m "statx01: Add new test for statx syscall"
282*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
283*49cdfc7eSAndroid Build Coastguard Worker
284*49cdfc7eSAndroid Build Coastguard WorkerThis should add all the new files in the +statx+ directory and the +runtest+
285*49cdfc7eSAndroid Build Coastguard Workerfile. It is good practice to commit early and often. Later on we will do a
286*49cdfc7eSAndroid Build Coastguard WorkerGit-rebase, which allows us to clean up the commit history. So don't worry
287*49cdfc7eSAndroid Build Coastguard Workerabout how presentable your commit log is for now. Also don't hesitate to
288*49cdfc7eSAndroid Build Coastguard Workercreate a new branch when doing the exercises or experimenting. This will allow
289*49cdfc7eSAndroid Build Coastguard Workeryou to diverge from the tutorial and then easily come back again.
290*49cdfc7eSAndroid Build Coastguard Worker
291*49cdfc7eSAndroid Build Coastguard WorkerI can't emphasize enough that Git makes things easy through branching and that
292*49cdfc7eSAndroid Build Coastguard Workerthings quickly get complicated if you don't do it. However if you do get into
293*49cdfc7eSAndroid Build Coastguard Workera mess, Git-reflog and Git-reset, will usually get you out of it. If you also
294*49cdfc7eSAndroid Build Coastguard Workermess that up then it may be possible to cherry pick 'dangling' commits out of
295*49cdfc7eSAndroid Build Coastguard Workerthe database into a branch.
296*49cdfc7eSAndroid Build Coastguard Worker
297*49cdfc7eSAndroid Build Coastguard Worker3.1 Report TCONF instead of TPASS
298*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
299*49cdfc7eSAndroid Build Coastguard Worker
300*49cdfc7eSAndroid Build Coastguard WorkerMaybe the test should report "TCONF: Not implemented" instead or perhaps
301*49cdfc7eSAndroid Build Coastguard Worker+TBROK+. Try changing it do so (see +doc/Test-Writing-Guidelines.asciidoc+ or
302*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[the
303*49cdfc7eSAndroid Build Coastguard WorkerWiki]).
304*49cdfc7eSAndroid Build Coastguard Worker
305*49cdfc7eSAndroid Build Coastguard Worker3.2 Check Git ignores the executable
306*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
307*49cdfc7eSAndroid Build Coastguard Worker
308*49cdfc7eSAndroid Build Coastguard WorkerIs your +.gitignore+ correct?
309*49cdfc7eSAndroid Build Coastguard Worker
310*49cdfc7eSAndroid Build Coastguard Worker3.3 Run make check
311*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~
312*49cdfc7eSAndroid Build Coastguard Worker
313*49cdfc7eSAndroid Build Coastguard WorkerCheck coding style with `make check`
314*49cdfc7eSAndroid Build Coastguard Worker (more in https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines#21-c-coding-style[C coding style])
315*49cdfc7eSAndroid Build Coastguard Worker
316*49cdfc7eSAndroid Build Coastguard Worker3.4 Install the LTP and run the test with runtest
317*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
318*49cdfc7eSAndroid Build Coastguard Worker
319*49cdfc7eSAndroid Build Coastguard WorkerRun +statx01+ on its own; similar to the +madvise+ tests in the +README.md+.
320*49cdfc7eSAndroid Build Coastguard Worker
321*49cdfc7eSAndroid Build Coastguard Worker4. Call the system call
322*49cdfc7eSAndroid Build Coastguard Worker-----------------------
323*49cdfc7eSAndroid Build Coastguard Worker
324*49cdfc7eSAndroid Build Coastguard WorkerAt the time of writing +statx+ has no +glibc+ wrapper. It is also fairly common
325*49cdfc7eSAndroid Build Coastguard Workerfor a distribution's C library version to be older than its kernel or it may use a
326*49cdfc7eSAndroid Build Coastguard Workercut down C library in comparison to the GNU one. So we must call +statx()+
327*49cdfc7eSAndroid Build Coastguard Workerusing the general +syscall()+ interface.
328*49cdfc7eSAndroid Build Coastguard Worker
329*49cdfc7eSAndroid Build Coastguard WorkerThe LTP contains a library for dealing with the +syscall+ interface, which is
330*49cdfc7eSAndroid Build Coastguard Workerlocated in +include/lapi+. System call numbers are listed against the relevant
331*49cdfc7eSAndroid Build Coastguard Workercall in the +*.in+ files (e.g. +x86_64.in+) which are used to generate
332*49cdfc7eSAndroid Build Coastguard Worker+syscalls.h+, which is the header you should include. On rare occasions you
333*49cdfc7eSAndroid Build Coastguard Workermay find the system call number is missing from the +*.in+ files and will need
334*49cdfc7eSAndroid Build Coastguard Workerto add it (see +include/lapi/syscalls/strip_syscall.awk+).
335*49cdfc7eSAndroid Build Coastguard Worker
336*49cdfc7eSAndroid Build Coastguard WorkerSystem call numbers vary between architectures, hence there are multiple
337*49cdfc7eSAndroid Build Coastguard Worker+*.in+ files for each architecture. You can find the various values for the
338*49cdfc7eSAndroid Build Coastguard Worker+statx+ system call across a number of +unistd.h+ files in the Linux kernel.
339*49cdfc7eSAndroid Build Coastguard Worker
340*49cdfc7eSAndroid Build Coastguard WorkerNote that we don't use the system-call-identifier value available in
341*49cdfc7eSAndroid Build Coastguard Worker+/usr/include/linux/uinstd.h+ because the kernel might be much newer than the
342*49cdfc7eSAndroid Build Coastguard Workeruser land development packages.
343*49cdfc7eSAndroid Build Coastguard Worker
344*49cdfc7eSAndroid Build Coastguard WorkerFor +statx+ we had to add +statx 332+ to +include/lapi/syscalls/x86_64.in+,
345*49cdfc7eSAndroid Build Coastguard Worker+statx 383+ to +include/lapi/syscalls/powerpc.in+, etc.  Now lets look at
346*49cdfc7eSAndroid Build Coastguard Workerthe code, which I will explain in more detail further down.
347*49cdfc7eSAndroid Build Coastguard Worker
348*49cdfc7eSAndroid Build Coastguard Worker[source,c]
349*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
350*49cdfc7eSAndroid Build Coastguard Worker/*
351*49cdfc7eSAndroid Build Coastguard Worker * Test statx
352*49cdfc7eSAndroid Build Coastguard Worker *
353*49cdfc7eSAndroid Build Coastguard Worker * Check if statx exists and what error code it returns when we give it dodgy
354*49cdfc7eSAndroid Build Coastguard Worker * data.
355*49cdfc7eSAndroid Build Coastguard Worker */
356*49cdfc7eSAndroid Build Coastguard Worker
357*49cdfc7eSAndroid Build Coastguard Worker#include <stdint.h>
358*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h"
359*49cdfc7eSAndroid Build Coastguard Worker#include "lapi/syscalls.h"
360*49cdfc7eSAndroid Build Coastguard Worker
361*49cdfc7eSAndroid Build Coastguard Workerstruct statx_timestamp {
362*49cdfc7eSAndroid Build Coastguard Worker	int64_t	       tv_sec;
363*49cdfc7eSAndroid Build Coastguard Worker	uint32_t       tv_nsec;
364*49cdfc7eSAndroid Build Coastguard Worker	int32_t	       __reserved;
365*49cdfc7eSAndroid Build Coastguard Worker};
366*49cdfc7eSAndroid Build Coastguard Worker
367*49cdfc7eSAndroid Build Coastguard Workerstruct statx {
368*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_mask;
369*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_blksize;
370*49cdfc7eSAndroid Build Coastguard Worker	uint64_t	stx_attributes;
371*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_nlink;
372*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_uid;
373*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_gid;
374*49cdfc7eSAndroid Build Coastguard Worker	uint16_t	stx_mode;
375*49cdfc7eSAndroid Build Coastguard Worker	uint16_t	__spare0[1];
376*49cdfc7eSAndroid Build Coastguard Worker	uint64_t	stx_ino;
377*49cdfc7eSAndroid Build Coastguard Worker	uint64_t	stx_size;
378*49cdfc7eSAndroid Build Coastguard Worker	uint64_t	stx_blocks;
379*49cdfc7eSAndroid Build Coastguard Worker	uint64_t	stx_attributes_mask;
380*49cdfc7eSAndroid Build Coastguard Worker	struct statx_timestamp	stx_atime;
381*49cdfc7eSAndroid Build Coastguard Worker	struct statx_timestamp	stx_btime;
382*49cdfc7eSAndroid Build Coastguard Worker	struct statx_timestamp	stx_ctime;
383*49cdfc7eSAndroid Build Coastguard Worker	struct statx_timestamp	stx_mtime;
384*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_rdev_major;
385*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_rdev_minor;
386*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_dev_major;
387*49cdfc7eSAndroid Build Coastguard Worker	uint32_t	stx_dev_minor;
388*49cdfc7eSAndroid Build Coastguard Worker	uint64_t	__spare2[14];
389*49cdfc7eSAndroid Build Coastguard Worker};
390*49cdfc7eSAndroid Build Coastguard Worker
391*49cdfc7eSAndroid Build Coastguard Workerstatic int sys_statx(int dirfd, const char *pathname, int flags,
392*49cdfc7eSAndroid Build Coastguard Worker		     unsigned int mask, struct statx *statxbuf)
393*49cdfc7eSAndroid Build Coastguard Worker{
394*49cdfc7eSAndroid Build Coastguard Worker	return tst_syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
395*49cdfc7eSAndroid Build Coastguard Worker}
396*49cdfc7eSAndroid Build Coastguard Worker
397*49cdfc7eSAndroid Build Coastguard Worker...
398*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
399*49cdfc7eSAndroid Build Coastguard Worker
400*49cdfc7eSAndroid Build Coastguard WorkerSo the top part of the code is now boiler plate for calling +statx+. It is
401*49cdfc7eSAndroid Build Coastguard Workercommon for the kernel to be newer than the user land libraries and headers. So
402*49cdfc7eSAndroid Build Coastguard Workerfor new system calls like +statx+, we copy, with a few modifications, the
403*49cdfc7eSAndroid Build Coastguard Workerrelevant definitions into the LTP. This is somewhat like 'vendoring', although
404*49cdfc7eSAndroid Build Coastguard Workerwe are usually just copying headers required for interacting with the Kernel's
405*49cdfc7eSAndroid Build Coastguard WorkerABI (Application Binary Interface), rather than internalising actual
406*49cdfc7eSAndroid Build Coastguard Workerfunctionality.
407*49cdfc7eSAndroid Build Coastguard Worker
408*49cdfc7eSAndroid Build Coastguard WorkerSo from the top we include the +stdint.h+ library which gives us the standard
409*49cdfc7eSAndroid Build Coastguard Worker+(u)int*_t+ type definitions. We use these in place of the Kernel type
410*49cdfc7eSAndroid Build Coastguard Workerdefinitions such as +__u64+ in +linux/types.h+. We then have a couple of
411*49cdfc7eSAndroid Build Coastguard Workerstructure definitions which form part of the +statx+ API. These were copied
412*49cdfc7eSAndroid Build Coastguard Workerfrom +include/uapi/linux/stat.h+ in the Kernel tree.
413*49cdfc7eSAndroid Build Coastguard Worker
414*49cdfc7eSAndroid Build Coastguard WorkerAfter that, there is a wrapper function, which saves us from writing
415*49cdfc7eSAndroid Build Coastguard Worker+tst_syscall(__NR_statx, ...+, every time we want to make a call to
416*49cdfc7eSAndroid Build Coastguard Worker+statx+. This also provides a stub for when +statx+ is eventually integrated
417*49cdfc7eSAndroid Build Coastguard Workerinto the LTP library and also implemented by the C library. At that point we
418*49cdfc7eSAndroid Build Coastguard Workercan switch to using the C library implementation if available or fallback to
419*49cdfc7eSAndroid Build Coastguard Workerour own.
420*49cdfc7eSAndroid Build Coastguard Worker
421*49cdfc7eSAndroid Build Coastguard WorkerThe advantage of using the C library implementation is that it will often be
422*49cdfc7eSAndroid Build Coastguard Workerbetter supported across multiple architectures. It will also mean we are using
423*49cdfc7eSAndroid Build Coastguard Workerthe system call in the same way most real programs would. Sometimes there are
424*49cdfc7eSAndroid Build Coastguard Workeradvantages to bypassing the C library, but in general it should not be our
425*49cdfc7eSAndroid Build Coastguard Workerfirst choice.
426*49cdfc7eSAndroid Build Coastguard Worker
427*49cdfc7eSAndroid Build Coastguard WorkerThe final test should do a check during configuration (i.e. when we run
428*49cdfc7eSAndroid Build Coastguard Worker+./configure+ before building) which checks if the +statx+ system call and
429*49cdfc7eSAndroid Build Coastguard Workerassociated structures exists. This requires writing an +m4+ file for use with
430*49cdfc7eSAndroid Build Coastguard Worker+configure.ac+ which is processed during +make autotools+ and produces the
431*49cdfc7eSAndroid Build Coastguard Workerconfigure script.
432*49cdfc7eSAndroid Build Coastguard Worker
433*49cdfc7eSAndroid Build Coastguard WorkerFor the time being though we shall just ignore this. All you need to know for
434*49cdfc7eSAndroid Build Coastguard Workernow is that this is a problem which eventually needs to be dealt with and that
435*49cdfc7eSAndroid Build Coastguard Workerthere is a system in place to handle it.
436*49cdfc7eSAndroid Build Coastguard Worker
437*49cdfc7eSAndroid Build Coastguard Worker[source,c]
438*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
439*49cdfc7eSAndroid Build Coastguard Worker...
440*49cdfc7eSAndroid Build Coastguard Worker
441*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void)
442*49cdfc7eSAndroid Build Coastguard Worker{
443*49cdfc7eSAndroid Build Coastguard Worker	struct statx statxbuf = { 0 };
444*49cdfc7eSAndroid Build Coastguard Worker
445*49cdfc7eSAndroid Build Coastguard Worker	TEST(sys_statx(0, NULL, 0, 0, &statxbuf));
446*49cdfc7eSAndroid Build Coastguard Worker
447*49cdfc7eSAndroid Build Coastguard Worker	if (TST_RET == 0)
448*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL, "statx thinks it can stat NULL");
449*49cdfc7eSAndroid Build Coastguard Worker	else if (TST_ERR == EFAULT)
450*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TPASS, "statx set errno to EFAULT as expected");
451*49cdfc7eSAndroid Build Coastguard Worker	else
452*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL | TERRNO, "statx set errno to some unexpected value");
453*49cdfc7eSAndroid Build Coastguard Worker}
454*49cdfc7eSAndroid Build Coastguard Worker
455*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = {
456*49cdfc7eSAndroid Build Coastguard Worker	.test_all = run,
457*49cdfc7eSAndroid Build Coastguard Worker	.min_kver = "4.11",
458*49cdfc7eSAndroid Build Coastguard Worker};
459*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
460*49cdfc7eSAndroid Build Coastguard Worker
461*49cdfc7eSAndroid Build Coastguard WorkerThe +TEST+ macro sets +TST_RET+ to the return value of +tst_statx()+ and
462*49cdfc7eSAndroid Build Coastguard Worker+TST_ERR+ to the value of +errno+ immediately after the functions
463*49cdfc7eSAndroid Build Coastguard Workerreturn. This is mainly just for convenience, although it potentially could
464*49cdfc7eSAndroid Build Coastguard Workerhave other uses.
465*49cdfc7eSAndroid Build Coastguard Worker
466*49cdfc7eSAndroid Build Coastguard WorkerWe check whether the return value indicates success and if it doesn't also
467*49cdfc7eSAndroid Build Coastguard Workercheck the value of +errno+. The last call to +tst_res+ includes +TERRNO+,
468*49cdfc7eSAndroid Build Coastguard Workerwhich will print the current error number and associated description in
469*49cdfc7eSAndroid Build Coastguard Workeraddition to the message we have provided. Note that it uses the current value
470*49cdfc7eSAndroid Build Coastguard Workerof +errno+ not +TST_ERR+.
471*49cdfc7eSAndroid Build Coastguard Worker
472*49cdfc7eSAndroid Build Coastguard WorkerWhat we should have done in the example above is use +TTERRNO+ which takes the
473*49cdfc7eSAndroid Build Coastguard Workervalue of +TST_ERR+.
474*49cdfc7eSAndroid Build Coastguard Worker
475*49cdfc7eSAndroid Build Coastguard WorkerIf we try to run the test on a kernel where +statx+ does not exist, then
476*49cdfc7eSAndroid Build Coastguard Worker+tst_syscall+ will cause it to fail gracefully with +TCONF+. Where +TCONF+
477*49cdfc7eSAndroid Build Coastguard Workerindicates the test is not applicable to our configuration.
478*49cdfc7eSAndroid Build Coastguard Worker
479*49cdfc7eSAndroid Build Coastguard WorkerThe function +tst_syscall+ calls +tst_brk(TCONF,...)+ on failure. +tst_brk+
480*49cdfc7eSAndroid Build Coastguard Workercauses the test to exit immediately, which prevents any further test code from
481*49cdfc7eSAndroid Build Coastguard Workerbeing run.
482*49cdfc7eSAndroid Build Coastguard Worker
483*49cdfc7eSAndroid Build Coastguard Worker4.1 What are the differences between tst_brk and tst_res?
484*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
485*49cdfc7eSAndroid Build Coastguard Worker
486*49cdfc7eSAndroid Build Coastguard WorkerSee +include/tst_test.h+ and the
487*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[test
488*49cdfc7eSAndroid Build Coastguard Workerwriting guide]. Also what do they have in common?
489*49cdfc7eSAndroid Build Coastguard Worker
490*49cdfc7eSAndroid Build Coastguard Worker4.2 What happens if you call tst_res(TINFO, ...) after sys_statx?
491*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
492*49cdfc7eSAndroid Build Coastguard Worker
493*49cdfc7eSAndroid Build Coastguard WorkerDoes the test still function correctly?
494*49cdfc7eSAndroid Build Coastguard Worker
495*49cdfc7eSAndroid Build Coastguard Worker4.3 Extend the test to handle other basic error conditions.
496*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
497*49cdfc7eSAndroid Build Coastguard Worker
498*49cdfc7eSAndroid Build Coastguard WorkerFor example, see if you can trigger +ENOENT+ instead. You shouldn't
499*49cdfc7eSAndroid Build Coastguard Workerhave to create any files, which is discussed in the next section.
500*49cdfc7eSAndroid Build Coastguard Worker
501*49cdfc7eSAndroid Build Coastguard Worker5. Setup, Cleanup and files
502*49cdfc7eSAndroid Build Coastguard Worker---------------------------
503*49cdfc7eSAndroid Build Coastguard Worker
504*49cdfc7eSAndroid Build Coastguard WorkerSome tests require resources to be allocated, or system settings to be
505*49cdfc7eSAndroid Build Coastguard Workerchanged, before the test begins. This 'setup' only has to be done once at the
506*49cdfc7eSAndroid Build Coastguard Workerbeginning and at the end of the test needs to be removed or reverted. The
507*49cdfc7eSAndroid Build Coastguard Worker'cleanup' also has to be done regardless of whether the test breaks.
508*49cdfc7eSAndroid Build Coastguard Worker
509*49cdfc7eSAndroid Build Coastguard WorkerFortunately, like most test libraries, we have setup and cleanup (teardown)
510*49cdfc7eSAndroid Build Coastguard Workercallbacks. +setup+ is called once before +run+ and +cleanup+ is called once
511*49cdfc7eSAndroid Build Coastguard Workerafterwards. Note that +run+ itself can be called multiple times by the test
512*49cdfc7eSAndroid Build Coastguard Workerharness, but that +setup+ and +cleanup+ are only called once.
513*49cdfc7eSAndroid Build Coastguard Worker
514*49cdfc7eSAndroid Build Coastguard WorkerIf either your code, a +SAFE_*+ macro or a library function such as
515*49cdfc7eSAndroid Build Coastguard Worker+tst_syscall+ call +tst_brk+, then +run+ will exit immediately and the
516*49cdfc7eSAndroid Build Coastguard Worker+cleanup+ function is then called. Once 'cleanup' is completed, the test
517*49cdfc7eSAndroid Build Coastguard Workerexecutable will then exit altogether abandoning any remaining iterations of
518*49cdfc7eSAndroid Build Coastguard Worker+run+.
519*49cdfc7eSAndroid Build Coastguard Worker
520*49cdfc7eSAndroid Build Coastguard WorkerFor +statx+ we would like to create some files or file like objects which we
521*49cdfc7eSAndroid Build Coastguard Workerhave control over. Deciding where to create the files is easy, we just create
522*49cdfc7eSAndroid Build Coastguard Workerit in the current working directory and let the LTP test harness handle where
523*49cdfc7eSAndroid Build Coastguard Workerthat should be by setting +.needs_tmpdir = 1+.
524*49cdfc7eSAndroid Build Coastguard Worker
525*49cdfc7eSAndroid Build Coastguard Worker[source,c]
526*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
527*49cdfc7eSAndroid Build Coastguard Worker/*
528*49cdfc7eSAndroid Build Coastguard Worker * Test statx
529*49cdfc7eSAndroid Build Coastguard Worker *
530*49cdfc7eSAndroid Build Coastguard Worker * Check if statx exists and what error code it returns when we give it dodgy
531*49cdfc7eSAndroid Build Coastguard Worker * data. Then stat a file and check it returns success.
532*49cdfc7eSAndroid Build Coastguard Worker */
533*49cdfc7eSAndroid Build Coastguard Worker
534*49cdfc7eSAndroid Build Coastguard Worker#include <stdint.h>
535*49cdfc7eSAndroid Build Coastguard Worker#include "tst_test.h"
536*49cdfc7eSAndroid Build Coastguard Worker#include "lapi/syscalls.h"
537*49cdfc7eSAndroid Build Coastguard Worker#include "lapi/fcntl.h"
538*49cdfc7eSAndroid Build Coastguard Worker
539*49cdfc7eSAndroid Build Coastguard Worker#define FNAME "file_to_stat"
540*49cdfc7eSAndroid Build Coastguard Worker#define STATX_BASIC_STATS 0x000007ffU
541*49cdfc7eSAndroid Build Coastguard Worker
542*49cdfc7eSAndroid Build Coastguard Worker/*************** statx structure and wrapper goes here ! ***************/
543*49cdfc7eSAndroid Build Coastguard Worker
544*49cdfc7eSAndroid Build Coastguard Worker...
545*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
546*49cdfc7eSAndroid Build Coastguard Worker
547*49cdfc7eSAndroid Build Coastguard WorkerWe have added an extra include +lapi/fcntl.h+ which wraps the system header by
548*49cdfc7eSAndroid Build Coastguard Workerthe same name (+#include <fcntl.h>+). This header ensures we have definitions
549*49cdfc7eSAndroid Build Coastguard Workerfor recently added macros such as +AT_FDCWD+ by providing fall backs if the
550*49cdfc7eSAndroid Build Coastguard Workersystem header does not have them. The +lapi+ directory contains a number of
551*49cdfc7eSAndroid Build Coastguard Workerheaders like this.
552*49cdfc7eSAndroid Build Coastguard Worker
553*49cdfc7eSAndroid Build Coastguard WorkerAt some point we may wish to add +lapi/stat.h+ to provide a fall back for
554*49cdfc7eSAndroid Build Coastguard Workermacros such as +STATX_BASIC_STATS+. However for the time being we have just
555*49cdfc7eSAndroid Build Coastguard Workerdefined it in the test.
556*49cdfc7eSAndroid Build Coastguard Worker
557*49cdfc7eSAndroid Build Coastguard Worker[source,c]
558*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
559*49cdfc7eSAndroid Build Coastguard Worker...
560*49cdfc7eSAndroid Build Coastguard Worker
561*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void)
562*49cdfc7eSAndroid Build Coastguard Worker{
563*49cdfc7eSAndroid Build Coastguard Worker	SAFE_TOUCH(FNAME, 0777, NULL);
564*49cdfc7eSAndroid Build Coastguard Worker}
565*49cdfc7eSAndroid Build Coastguard Worker
566*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void)
567*49cdfc7eSAndroid Build Coastguard Worker{
568*49cdfc7eSAndroid Build Coastguard Worker	struct statx statxbuf = { 0 };
569*49cdfc7eSAndroid Build Coastguard Worker
570*49cdfc7eSAndroid Build Coastguard Worker	TEST(sys_statx(0, NULL, 0, 0, &statxbuf));
571*49cdfc7eSAndroid Build Coastguard Worker	if (TST_RET == 0)
572*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL, "statx thinks it can stat NULL");
573*49cdfc7eSAndroid Build Coastguard Worker	else if (TST_ERR == EFAULT)
574*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TPASS, "statx set errno to EFAULT as expected");
575*49cdfc7eSAndroid Build Coastguard Worker	else
576*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL | TERRNO, "statx set errno to some unexpected value");
577*49cdfc7eSAndroid Build Coastguard Worker
578*49cdfc7eSAndroid Build Coastguard Worker	TEST(sys_statx(AT_FDCWD, FNAME, 0, STATX_BASIC_STATS, &statxbuf));
579*49cdfc7eSAndroid Build Coastguard Worker	if (TST_RET == 0)
580*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TPASS, "It returned zero so it must have worked!");
581*49cdfc7eSAndroid Build Coastguard Worker	else
582*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL | TERRNO, "statx can not stat a basic file");
583*49cdfc7eSAndroid Build Coastguard Worker}
584*49cdfc7eSAndroid Build Coastguard Worker
585*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = {
586*49cdfc7eSAndroid Build Coastguard Worker	.setup = setup,
587*49cdfc7eSAndroid Build Coastguard Worker	.test_all = run,
588*49cdfc7eSAndroid Build Coastguard Worker	.min_kver = "4.11",
589*49cdfc7eSAndroid Build Coastguard Worker	.needs_tmpdir = 1
590*49cdfc7eSAndroid Build Coastguard Worker};
591*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
592*49cdfc7eSAndroid Build Coastguard Worker
593*49cdfc7eSAndroid Build Coastguard WorkerThe +setup+ callback uses one of the LTP's +SAFE+ functions to create an empty
594*49cdfc7eSAndroid Build Coastguard Workerfile +file_to_stat+. Because we have set +.needs_tmpdir+, we can just create
595*49cdfc7eSAndroid Build Coastguard Workerthis file in the present working directory. We don't need to create a
596*49cdfc7eSAndroid Build Coastguard Worker+cleanup+ callback yet because the LTP test harness will recursively delete
597*49cdfc7eSAndroid Build Coastguard Workerthe temporary directory and its contents.
598*49cdfc7eSAndroid Build Coastguard Worker
599*49cdfc7eSAndroid Build Coastguard WorkerThe +run+ function can be called multiple times by the test harness, however
600*49cdfc7eSAndroid Build Coastguard Worker+setup+ and +cleanup+ callbacks will only be ran once.
601*49cdfc7eSAndroid Build Coastguard Worker
602*49cdfc7eSAndroid Build Coastguard Worker[WARNING]
603*49cdfc7eSAndroid Build Coastguard WorkerBy this point you may have begun to explore the LTP library headers or older
604*49cdfc7eSAndroid Build Coastguard Workertests. In which case you will have come across functions from the old API such
605*49cdfc7eSAndroid Build Coastguard Workeras +tst_brkm+. The old API is being phased out, so you should not use these
606*49cdfc7eSAndroid Build Coastguard Workerfunctions.
607*49cdfc7eSAndroid Build Coastguard Worker
608*49cdfc7eSAndroid Build Coastguard WorkerSo far we haven't had to do any clean up. So our example doesn't answer the
609*49cdfc7eSAndroid Build Coastguard Workerquestion "what happens if part of the clean up fails?". To answer this we are
610*49cdfc7eSAndroid Build Coastguard Workergoing to modify the test to ask the (highly contrived) question "What happens
611*49cdfc7eSAndroid Build Coastguard Workerif I create and open a file, then create a hard-link to it, then call open
612*49cdfc7eSAndroid Build Coastguard Workeragain on the hard-link, then 'stat' the file".
613*49cdfc7eSAndroid Build Coastguard Worker
614*49cdfc7eSAndroid Build Coastguard Worker[source,c]
615*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
616*49cdfc7eSAndroid Build Coastguard Worker#define LNAME "file_to_stat_link"
617*49cdfc7eSAndroid Build Coastguard Worker
618*49cdfc7eSAndroid Build Coastguard Worker...
619*49cdfc7eSAndroid Build Coastguard Worker
620*49cdfc7eSAndroid Build Coastguard Workerstatic void setup(void)
621*49cdfc7eSAndroid Build Coastguard Worker{
622*49cdfc7eSAndroid Build Coastguard Worker	fd = SAFE_OPEN(FNAME, O_CREAT, 0777);
623*49cdfc7eSAndroid Build Coastguard Worker	SAFE_LINK(FNAME, LNAME);
624*49cdfc7eSAndroid Build Coastguard Worker	lfd = SAFE_OPEN(LNAME, 0);
625*49cdfc7eSAndroid Build Coastguard Worker}
626*49cdfc7eSAndroid Build Coastguard Worker
627*49cdfc7eSAndroid Build Coastguard Workerstatic void cleanup(void)
628*49cdfc7eSAndroid Build Coastguard Worker{
629*49cdfc7eSAndroid Build Coastguard Worker	if (lfd != 0)
630*49cdfc7eSAndroid Build Coastguard Worker		SAFE_CLOSE(lfd);
631*49cdfc7eSAndroid Build Coastguard Worker
632*49cdfc7eSAndroid Build Coastguard Worker	if (fd != 0)
633*49cdfc7eSAndroid Build Coastguard Worker		SAFE_CLOSE(fd);
634*49cdfc7eSAndroid Build Coastguard Worker}
635*49cdfc7eSAndroid Build Coastguard Worker
636*49cdfc7eSAndroid Build Coastguard Workerstatic void run(void)
637*49cdfc7eSAndroid Build Coastguard Worker{
638*49cdfc7eSAndroid Build Coastguard Worker        ...
639*49cdfc7eSAndroid Build Coastguard Worker
640*49cdfc7eSAndroid Build Coastguard Worker	TEST(sys_statx(AT_FDCWD, LNAME, 0, STATX_BASIC_STATS, &statxbuf));
641*49cdfc7eSAndroid Build Coastguard Worker	if (TST_RET == 0)
642*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TPASS, "It returned zero so it must have worked!");
643*49cdfc7eSAndroid Build Coastguard Worker	else
644*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL | TERRNO, "statx can not stat a basic file");
645*49cdfc7eSAndroid Build Coastguard Worker}
646*49cdfc7eSAndroid Build Coastguard Worker
647*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = {
648*49cdfc7eSAndroid Build Coastguard Worker	.setup = setup,
649*49cdfc7eSAndroid Build Coastguard Worker	.cleanup = cleanup,
650*49cdfc7eSAndroid Build Coastguard Worker	.test_all = run,
651*49cdfc7eSAndroid Build Coastguard Worker	.tcnt = 2,
652*49cdfc7eSAndroid Build Coastguard Worker	.min_kver = "4.11",
653*49cdfc7eSAndroid Build Coastguard Worker	.needs_tmpdir = 1
654*49cdfc7eSAndroid Build Coastguard Worker};
655*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
656*49cdfc7eSAndroid Build Coastguard Worker
657*49cdfc7eSAndroid Build Coastguard WorkerBecause we are now opening a file, we need a +cleanup+ function to close the
658*49cdfc7eSAndroid Build Coastguard Workerfile descriptors. We have to manually close the files to ensure the temporary
659*49cdfc7eSAndroid Build Coastguard Workerdirectory is deleted by the test harness (see the
660*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[test
661*49cdfc7eSAndroid Build Coastguard Workerwriting guidelines] for details).
662*49cdfc7eSAndroid Build Coastguard Worker
663*49cdfc7eSAndroid Build Coastguard WorkerAs a matter of good practice, the file descriptors are closed in reverse
664*49cdfc7eSAndroid Build Coastguard Workerorder. In some circumstances the order in which clean up is performed is
665*49cdfc7eSAndroid Build Coastguard Workersignificant. In that case resources created towards the end of 'setup' are
666*49cdfc7eSAndroid Build Coastguard Workerdependent on ones near the beginning. So during 'cleanup' we remove the
667*49cdfc7eSAndroid Build Coastguard Workerdependants before their dependencies.
668*49cdfc7eSAndroid Build Coastguard Worker
669*49cdfc7eSAndroid Build Coastguard WorkerIf, for some reason, the file descriptor +lfd+ became invalid during the test,
670*49cdfc7eSAndroid Build Coastguard Workerbut +fd+ was still open, we do not want +SAFE_CLOSE(lfd)+ to cause the
671*49cdfc7eSAndroid Build Coastguard Worker+cleanup+ function to exit prematurely. If it did, then +fd+ would remain open
672*49cdfc7eSAndroid Build Coastguard Workerwhich would cause problems on some file systems.
673*49cdfc7eSAndroid Build Coastguard Worker
674*49cdfc7eSAndroid Build Coastguard WorkerNor do we want to call +cleanup+ recursively. So during 'cleanup' +tst_brk+,
675*49cdfc7eSAndroid Build Coastguard Workerand consequently the +SAFE+ functions, do not cause the test to exit with
676*49cdfc7eSAndroid Build Coastguard Worker+TBROK+. Instead they just print an error message with +TWARN+.
677*49cdfc7eSAndroid Build Coastguard Worker
678*49cdfc7eSAndroid Build Coastguard WorkerIt is not entirely necessary to check if the file descriptors have a none zero
679*49cdfc7eSAndroid Build Coastguard Workervalue before attempting to close them. However it avoids a bunch of spurious
680*49cdfc7eSAndroid Build Coastguard Workerwarning messages if we fail to open +file_to_stat+. Test case failures can be
681*49cdfc7eSAndroid Build Coastguard Workerdifficult to interpret at the best of times, so avoid filling the log with
682*49cdfc7eSAndroid Build Coastguard Workernoise.
683*49cdfc7eSAndroid Build Coastguard Worker
684*49cdfc7eSAndroid Build Coastguard Worker5.1 Check statx returns the correct number of hard links
685*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
686*49cdfc7eSAndroid Build Coastguard Worker
687*49cdfc7eSAndroid Build Coastguard WorkerThe field +statx.stx_nlink+ should be equal to 2, right?
688*49cdfc7eSAndroid Build Coastguard Worker
689*49cdfc7eSAndroid Build Coastguard Worker5.2 Git-branch
690*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~
691*49cdfc7eSAndroid Build Coastguard Worker
692*49cdfc7eSAndroid Build Coastguard WorkerWe are about to make some organisational changes to the test, so now would be
693*49cdfc7eSAndroid Build Coastguard Workera good time to branch. Then we can switch between the old and new versions, to
694*49cdfc7eSAndroid Build Coastguard Workercheck the behavior has not been changed by accident.
695*49cdfc7eSAndroid Build Coastguard Worker
696*49cdfc7eSAndroid Build Coastguard Worker6. Split the test
697*49cdfc7eSAndroid Build Coastguard Worker-----------------
698*49cdfc7eSAndroid Build Coastguard Worker
699*49cdfc7eSAndroid Build Coastguard WorkerIn our current test, we have essentially rolled two different test cases into
700*49cdfc7eSAndroid Build Coastguard Workerone. Firstly we check if an error is returned when bad arguments are provided
701*49cdfc7eSAndroid Build Coastguard Workerand secondly we check what happens when we stat an actual file. Quite often it
702*49cdfc7eSAndroid Build Coastguard Workermakes sense to call +tst_res+ multiple times in a single test case because we
703*49cdfc7eSAndroid Build Coastguard Workerare checking different properties of the same result, but here we are clearly
704*49cdfc7eSAndroid Build Coastguard Workertesting two different scenarios.
705*49cdfc7eSAndroid Build Coastguard Worker
706*49cdfc7eSAndroid Build Coastguard WorkerSo we should split the test in two. One obvious way to do this is to create
707*49cdfc7eSAndroid Build Coastguard Worker+statx02.c+, but that seems like overkill in order to separate two simple test
708*49cdfc7eSAndroid Build Coastguard Workercases. So, for now at least, we are going to do it a different way.
709*49cdfc7eSAndroid Build Coastguard Worker
710*49cdfc7eSAndroid Build Coastguard Worker[source,c]
711*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
712*49cdfc7eSAndroid Build Coastguard Worker...
713*49cdfc7eSAndroid Build Coastguard Worker
714*49cdfc7eSAndroid Build Coastguard Workerstatic void run_stat_null(void)
715*49cdfc7eSAndroid Build Coastguard Worker{
716*49cdfc7eSAndroid Build Coastguard Worker	struct statx statxbuf = { 0 };
717*49cdfc7eSAndroid Build Coastguard Worker
718*49cdfc7eSAndroid Build Coastguard Worker	TEST(sys_statx(0, NULL, 0, 0, &statxbuf));
719*49cdfc7eSAndroid Build Coastguard Worker	if (TST_RET == 0)
720*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL, "statx thinks it can stat NULL");
721*49cdfc7eSAndroid Build Coastguard Worker	else if (TST_ERR == EFAULT)
722*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TPASS, "statx set errno to EFAULT as expected");
723*49cdfc7eSAndroid Build Coastguard Worker	else
724*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL | TERRNO, "statx set errno to some unexpected value");
725*49cdfc7eSAndroid Build Coastguard Worker}
726*49cdfc7eSAndroid Build Coastguard Worker
727*49cdfc7eSAndroid Build Coastguard Workerstatic void run_stat_symlink(void)
728*49cdfc7eSAndroid Build Coastguard Worker{
729*49cdfc7eSAndroid Build Coastguard Worker	struct statx statxbuf = { 0 };
730*49cdfc7eSAndroid Build Coastguard Worker
731*49cdfc7eSAndroid Build Coastguard Worker	TEST(sys_statx(AT_FDCWD, LNAME, 0, STATX_BASIC_STATS, &statxbuf));
732*49cdfc7eSAndroid Build Coastguard Worker	if (TST_RET == 0)
733*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TPASS, "It returned zero so it must have worked!");
734*49cdfc7eSAndroid Build Coastguard Worker	else
735*49cdfc7eSAndroid Build Coastguard Worker		tst_res(TFAIL | TERRNO, "statx can not stat a basic file");
736*49cdfc7eSAndroid Build Coastguard Worker}
737*49cdfc7eSAndroid Build Coastguard Worker
738*49cdfc7eSAndroid Build Coastguard Workerstatic void run(unsigned int i)
739*49cdfc7eSAndroid Build Coastguard Worker{
740*49cdfc7eSAndroid Build Coastguard Worker	switch(i) {
741*49cdfc7eSAndroid Build Coastguard Worker	case 0: run_stat_null();
742*49cdfc7eSAndroid Build Coastguard Worker	case 1: run_stat_symlink();
743*49cdfc7eSAndroid Build Coastguard Worker	}
744*49cdfc7eSAndroid Build Coastguard Worker}
745*49cdfc7eSAndroid Build Coastguard Worker
746*49cdfc7eSAndroid Build Coastguard Workerstatic struct tst_test test = {
747*49cdfc7eSAndroid Build Coastguard Worker	.setup = setup,
748*49cdfc7eSAndroid Build Coastguard Worker	.cleanup = cleanup,
749*49cdfc7eSAndroid Build Coastguard Worker	.test = run,
750*49cdfc7eSAndroid Build Coastguard Worker	.tcnt = 2,
751*49cdfc7eSAndroid Build Coastguard Worker	.min_kver = "4.11",
752*49cdfc7eSAndroid Build Coastguard Worker	.needs_tmpdir = 1
753*49cdfc7eSAndroid Build Coastguard Worker};
754*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
755*49cdfc7eSAndroid Build Coastguard Worker
756*49cdfc7eSAndroid Build Coastguard WorkerSo we have used an alternative form of the +test+ or +run+ callback which
757*49cdfc7eSAndroid Build Coastguard Workeraccepts an index. Some tests use this index with an array of parameters and
758*49cdfc7eSAndroid Build Coastguard Workerexpected return values. Others do something similar to the above. The index
759*49cdfc7eSAndroid Build Coastguard Workercan be used how you want so long as each iteration calls +tst_res+ in a
760*49cdfc7eSAndroid Build Coastguard Workermeaningful way.
761*49cdfc7eSAndroid Build Coastguard Worker
762*49cdfc7eSAndroid Build Coastguard WorkerIf an iteration fails to return a result (i.e. call +tst_res+ with a value
763*49cdfc7eSAndroid Build Coastguard Workerother than +TINFO+) then the test harness will report +TBROK+ and print the
764*49cdfc7eSAndroid Build Coastguard Workeriteration which failed. This prevents a scenario in your test from silently
765*49cdfc7eSAndroid Build Coastguard Workerfailing due to some faulty logic.
766*49cdfc7eSAndroid Build Coastguard Worker
767*49cdfc7eSAndroid Build Coastguard Worker6.1 What is wrong with the switch statement?
768*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
769*49cdfc7eSAndroid Build Coastguard Worker
770*49cdfc7eSAndroid Build Coastguard WorkerWere you paying attention? Also see the output of +make check+.
771*49cdfc7eSAndroid Build Coastguard Worker
772*49cdfc7eSAndroid Build Coastguard Worker6.2 Test a feature unique to statx
773*49cdfc7eSAndroid Build Coastguard Worker~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
774*49cdfc7eSAndroid Build Coastguard Worker
775*49cdfc7eSAndroid Build Coastguard WorkerSo far we have not tested anything which is unique to +statx+. So, for
776*49cdfc7eSAndroid Build Coastguard Workerexample, you could check stx_btime is correct (possibly only to within a
777*49cdfc7eSAndroid Build Coastguard Workermargin of error) and that it differs from +stx_mtime+ after writing to the
778*49cdfc7eSAndroid Build Coastguard Workerfile.
779*49cdfc7eSAndroid Build Coastguard Worker
780*49cdfc7eSAndroid Build Coastguard WorkerAlternatively you could check that +stx_dev_major+ and +stx_dev_minor+ are set
781*49cdfc7eSAndroid Build Coastguard Workercorrectly. Note that the LTP has helper functions for creating devices and
782*49cdfc7eSAndroid Build Coastguard Workerfile systems (see
783*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines#2214-testing-with-a-block-device[section
784*49cdfc7eSAndroid Build Coastguard Worker2.2.14] of the Test Writing Guidelines).
785*49cdfc7eSAndroid Build Coastguard Worker
786*49cdfc7eSAndroid Build Coastguard WorkerThis could be quite a challenging exercise. You may wish to tackle an
787*49cdfc7eSAndroid Build Coastguard Workeraltogether different test scenario instead. If you get stuck just move onto
788*49cdfc7eSAndroid Build Coastguard Workerthe next section and come back later.
789*49cdfc7eSAndroid Build Coastguard Worker
790*49cdfc7eSAndroid Build Coastguard Worker7. Submitting the test for review
791*49cdfc7eSAndroid Build Coastguard Worker---------------------------------
792*49cdfc7eSAndroid Build Coastguard Worker
793*49cdfc7eSAndroid Build Coastguard WorkerIgnoring the fact we should probably create +lapi/stat.h+ along with a bunch
794*49cdfc7eSAndroid Build Coastguard Workerof fallback logic in the build system. We can now get our test ready for
795*49cdfc7eSAndroid Build Coastguard Workersubmission.
796*49cdfc7eSAndroid Build Coastguard Worker
797*49cdfc7eSAndroid Build Coastguard WorkerThe first thing you need to do before considering submitting your test is run
798*49cdfc7eSAndroid Build Coastguard Worker+make check-statx01+ or + make check+ in the test's directory. Again, we use
799*49cdfc7eSAndroid Build Coastguard Workerthe kernel style guidelines where possible. Next you should create a new
800*49cdfc7eSAndroid Build Coastguard Workerbranch, this will allow you to reshape your commit history without fear.
801*49cdfc7eSAndroid Build Coastguard Worker
802*49cdfc7eSAndroid Build Coastguard WorkerAfter that we have the pleasure of doing an interactive 'rebase' to clean up
803*49cdfc7eSAndroid Build Coastguard Workerour commit history. In its current form the test only really needs a single
804*49cdfc7eSAndroid Build Coastguard Workercommit, but if you have been using Git correctly then you should have
805*49cdfc7eSAndroid Build Coastguard Workermany. The main reason we want to compress it to a single commit, is to make
806*49cdfc7eSAndroid Build Coastguard Workerthe LTP's Git-log readable. It also allows us to write a coherent description
807*49cdfc7eSAndroid Build Coastguard Workerof the work as a whole in retrospective. Although, when adding a new test, the
808*49cdfc7eSAndroid Build Coastguard Workertest description in the code will probably make the commit message redundant.
809*49cdfc7eSAndroid Build Coastguard Worker
810*49cdfc7eSAndroid Build Coastguard WorkerAnyway, as an example, we shall look at my personal commit history from this
811*49cdfc7eSAndroid Build Coastguard Workertutorial and 'rebase' it. You should try following along with your own
812*49cdfc7eSAndroid Build Coastguard Workerrepository. First lets look at the commit history since we branched from
813*49cdfc7eSAndroid Build Coastguard Workermaster.
814*49cdfc7eSAndroid Build Coastguard Worker
815*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
816*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
817*49cdfc7eSAndroid Build Coastguard Worker$ git log --oneline master..HEAD
818*49cdfc7eSAndroid Build Coastguard Worker152d39fe7 (HEAD -> tutorial-rebase2, tutorial-rebase) tutorial: Start Submitting patch section
819*49cdfc7eSAndroid Build Coastguard Worker70f7ce7ce statx01: Stop checkpatch from complaining
820*49cdfc7eSAndroid Build Coastguard Workerbb0332bd7 tutorial: Fix review problems
821*49cdfc7eSAndroid Build Coastguard Worker6a87a084a statx01: Fix review problems
822*49cdfc7eSAndroid Build Coastguard Workerd784b1e85 test-writing-guidelines: Remove old API argument
823*49cdfc7eSAndroid Build Coastguard Workerc26e1be7a fixup! tutorial
824*49cdfc7eSAndroid Build Coastguard Worker1e24a5fb5 (me/tutorial-rebase) fixup! tutorial
825*49cdfc7eSAndroid Build Coastguard Worker568a3f7be fixup! tutorial
826*49cdfc7eSAndroid Build Coastguard Worker09dd2c829 statx: stage 6
827*49cdfc7eSAndroid Build Coastguard Workerbfeef7902 statx: stage 5b
828*49cdfc7eSAndroid Build Coastguard Worker76e03d714 statx: stage 5a
829*49cdfc7eSAndroid Build Coastguard Worker98f5bc7ac statx: stage 4
830*49cdfc7eSAndroid Build Coastguard Worker6f8c16438 statx: stage 3 (Add statx01)
831*49cdfc7eSAndroid Build Coastguard Worker5d93b84d8 Add statx and other syscall numbers
832*49cdfc7eSAndroid Build Coastguard Worker5ca627b78 tutorial: Add a step-by-step C test tutorial
833*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
834*49cdfc7eSAndroid Build Coastguard Worker
835*49cdfc7eSAndroid Build Coastguard WorkerSo we have told git to show all the commits which don't exist in 'master', but
836*49cdfc7eSAndroid Build Coastguard Workerare in +HEAD+, where +HEAD+ is the top of the current branch. The current
837*49cdfc7eSAndroid Build Coastguard Workerbranch is +tutorial-rebase2+ which I just created. I have already done one
838*49cdfc7eSAndroid Build Coastguard Worker'rebase' and submitted a patch for review, so my original branch was just called
839*49cdfc7eSAndroid Build Coastguard Worker+tutorial+.
840*49cdfc7eSAndroid Build Coastguard Worker
841*49cdfc7eSAndroid Build Coastguard WorkerAs usual my commit history is starting to look like a bit of mess! There is
842*49cdfc7eSAndroid Build Coastguard Workereven a commit in there which should not be in the this branch (Remove old API
843*49cdfc7eSAndroid Build Coastguard Workerargument), however it can be ignored for now and 'cherry picked' into a new branch
844*49cdfc7eSAndroid Build Coastguard Workerlater.
845*49cdfc7eSAndroid Build Coastguard Worker
846*49cdfc7eSAndroid Build Coastguard WorkerFor my patch I actually need at least two commits, one which contains the
847*49cdfc7eSAndroid Build Coastguard Workertutorial text and one which contains the test and associated files. So first
848*49cdfc7eSAndroid Build Coastguard Workerof all I want to 'squash' (amalgamate) all the commits appended with
849*49cdfc7eSAndroid Build Coastguard Worker+tutorial:+ into the bottom commit.
850*49cdfc7eSAndroid Build Coastguard Worker
851*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
852*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
853*49cdfc7eSAndroid Build Coastguard Worker$ git rebase -i 5ca627b78\^
854*49cdfc7eSAndroid Build Coastguard Worker...
855*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
856*49cdfc7eSAndroid Build Coastguard Worker
857*49cdfc7eSAndroid Build Coastguard WorkerThis begins an interactive 'rebase' where commit 5ca6427b78 is the earliest
858*49cdfc7eSAndroid Build Coastguard Workercommit we want to edit. The +^+ symbol after the commit hash, specifies the
859*49cdfc7eSAndroid Build Coastguard Workercommit before this one. The interactive 'rebase' command takes the last commit
860*49cdfc7eSAndroid Build Coastguard Workerwe want to keep unaltered as it's argument (in other words it takes a
861*49cdfc7eSAndroid Build Coastguard Workernon-inclusive range).
862*49cdfc7eSAndroid Build Coastguard Worker
863*49cdfc7eSAndroid Build Coastguard WorkerUpon entering a similar command you will be presented with a text file
864*49cdfc7eSAndroid Build Coastguard Workersimilar to the following. The file should be displayed in your text editor of
865*49cdfc7eSAndroid Build Coastguard Workerchoice, if it doesn't, then you may change the editor variable in +.gitconfig+
866*49cdfc7eSAndroid Build Coastguard Workerwhich was shown in section 3.
867*49cdfc7eSAndroid Build Coastguard Worker
868*49cdfc7eSAndroid Build Coastguard Worker[source,rebase]
869*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
870*49cdfc7eSAndroid Build Coastguard Workerpick 5ca627b78 tutorial: Add a step-by-step C test tutorial
871*49cdfc7eSAndroid Build Coastguard Workerpick 5d93b84d8 Add statx and other syscall numbers
872*49cdfc7eSAndroid Build Coastguard Workerpick 6f8c16438 statx: stage 3 (Add statx01)
873*49cdfc7eSAndroid Build Coastguard Workerpick 98f5bc7ac statx: stage 4
874*49cdfc7eSAndroid Build Coastguard Workerpick 76e03d714 statx: stage 5a
875*49cdfc7eSAndroid Build Coastguard Workerpick bfeef7902 statx: stage 5b
876*49cdfc7eSAndroid Build Coastguard Workerpick 09dd2c829 statx: stage 6
877*49cdfc7eSAndroid Build Coastguard Workerpick 568a3f7be fixup! tutorial
878*49cdfc7eSAndroid Build Coastguard Workerpick 1e24a5fb5 fixup! tutorial
879*49cdfc7eSAndroid Build Coastguard Workerpick c26e1be7a fixup! tutorial
880*49cdfc7eSAndroid Build Coastguard Workerpick d784b1e85 test-writing-guidelines: Remove old API argument
881*49cdfc7eSAndroid Build Coastguard Workerpick 6a87a084a statx01: Fix review problems
882*49cdfc7eSAndroid Build Coastguard Workerpick bb0332bd7 tutorial: Fix review problems
883*49cdfc7eSAndroid Build Coastguard Workerpick 70f7ce7ce statx01: Stop checkpatch from complaining
884*49cdfc7eSAndroid Build Coastguard Workerpick 152d39fe7 tutorial: Start Submitting patch section
885*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
886*49cdfc7eSAndroid Build Coastguard Worker
887*49cdfc7eSAndroid Build Coastguard WorkerThe last commit from Git-log is shown at the top. The left hand column
888*49cdfc7eSAndroid Build Coastguard Workercontains the commands we want to run on each commit. +pick+ just means we
889*49cdfc7eSAndroid Build Coastguard Workerre-apply the commit as-is. We can reorder the lines to apply the commits in a
890*49cdfc7eSAndroid Build Coastguard Workerdifferent order, but we need to be careful when reordering commits to the same
891*49cdfc7eSAndroid Build Coastguard Workerfile. If your 'rebase' results in a merge conflict, then you have probably
892*49cdfc7eSAndroid Build Coastguard Workerreordered some commits which contained changes to the same piece of code.
893*49cdfc7eSAndroid Build Coastguard Worker
894*49cdfc7eSAndroid Build Coastguard WorkerPerhaps a better name for the interactive 'rebase' command would be 'replay'. As
895*49cdfc7eSAndroid Build Coastguard Workerwe pick a point in the commit history, undo all those commits before that
896*49cdfc7eSAndroid Build Coastguard Workerpoint, then reapply them one at a time. During the replay we can reorder the
897*49cdfc7eSAndroid Build Coastguard Workercommits, drop, merge, split and edit them, creating a new history.
898*49cdfc7eSAndroid Build Coastguard Worker
899*49cdfc7eSAndroid Build Coastguard WorkerThe commands I am going to use are +reword+ and +fixup+. The +reword+ command
900*49cdfc7eSAndroid Build Coastguard Workerallows you to edit a single commit's message. The 'fixup' command 'squashes' a
901*49cdfc7eSAndroid Build Coastguard Workercommit into the commit above/preceding it, merging the two commits into
902*49cdfc7eSAndroid Build Coastguard Workerone. The commit which has +fixup+ applied has its commit message deleted. If
903*49cdfc7eSAndroid Build Coastguard Workeryou think a commit might have something useful in its message then you can use
904*49cdfc7eSAndroid Build Coastguard Worker+squash+ instead.
905*49cdfc7eSAndroid Build Coastguard Worker
906*49cdfc7eSAndroid Build Coastguard Worker[source,rebase]
907*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
908*49cdfc7eSAndroid Build Coastguard Workerreword 5ca627b78 tutorial: Add a step-by-step C test tutorial
909*49cdfc7eSAndroid Build Coastguard Workerfixup 568a3f7be fixup! tutorial
910*49cdfc7eSAndroid Build Coastguard Workerfixup 1e24a5fb5 fixup! tutorial
911*49cdfc7eSAndroid Build Coastguard Workerfixup c26e1be7a fixup! tutorial
912*49cdfc7eSAndroid Build Coastguard Workerfixup bb0332bd7 tutorial: Fix review problems
913*49cdfc7eSAndroid Build Coastguard Workerfixup 152d39fe7 tutorial: Start Submitting patch section
914*49cdfc7eSAndroid Build Coastguard Workerfixup 276edecab tutorial: Save changes before rebase
915*49cdfc7eSAndroid Build Coastguard Workerpick 5d93b84d8 Add statx and other syscall numbers
916*49cdfc7eSAndroid Build Coastguard Workerpick 6f8c16438 statx: stage 3 (Add statx01)
917*49cdfc7eSAndroid Build Coastguard Workerpick 98f5bc7ac statx: stage 4
918*49cdfc7eSAndroid Build Coastguard Workerpick 76e03d714 statx: stage 5a
919*49cdfc7eSAndroid Build Coastguard Workerpick bfeef7902 statx: stage 5b
920*49cdfc7eSAndroid Build Coastguard Workerpick 09dd2c829 statx: stage 6
921*49cdfc7eSAndroid Build Coastguard Workerpick d784b1e85 test-writing-guidelines: Remove old API argument
922*49cdfc7eSAndroid Build Coastguard Workerpick 6a87a084a statx01: Fix review problems
923*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
924*49cdfc7eSAndroid Build Coastguard Worker
925*49cdfc7eSAndroid Build Coastguard WorkerSo all the commits marked with +fixup+ will be re-played by Git immediately
926*49cdfc7eSAndroid Build Coastguard Workerafter 5ca62 at the top. A new commit will then be created with the amalgamated
927*49cdfc7eSAndroid Build Coastguard Workerchanges of all the commits and 5ca62's log message. It turns out that I didn't
928*49cdfc7eSAndroid Build Coastguard Workerneed to reword anything, but there is no harm in checking. It is easy to
929*49cdfc7eSAndroid Build Coastguard Workerforget the +Signed-off-by:+ line.
930*49cdfc7eSAndroid Build Coastguard Worker
931*49cdfc7eSAndroid Build Coastguard WorkerI could now do the same for the commits to the +statx+ test, making the commit
932*49cdfc7eSAndroid Build Coastguard Workermessage prefixes consistent. However I am not actually going to submit the
933*49cdfc7eSAndroid Build Coastguard Workertest (yet).
934*49cdfc7eSAndroid Build Coastguard Worker
935*49cdfc7eSAndroid Build Coastguard WorkerI won't attempt to show you this, but if you need to do the opposite and split
936*49cdfc7eSAndroid Build Coastguard Workerapart a commit. It is also possible using Git-rebase by marking a line with
937*49cdfc7eSAndroid Build Coastguard Worker+edit+. This will pause Git just after replaying the marked commit. You can
938*49cdfc7eSAndroid Build Coastguard Workerthen use a 'soft' Git-reset to bring the selected commit's changes back into
939*49cdfc7eSAndroid Build Coastguard Workerthe 'index' where you are then able to un-stage some parts before
940*49cdfc7eSAndroid Build Coastguard Workerre-committing.
941*49cdfc7eSAndroid Build Coastguard Worker
942*49cdfc7eSAndroid Build Coastguard WorkerYou can also use +edit+ and +git commit --amend+ together to change a commit
943*49cdfc7eSAndroid Build Coastguard Workerdeep in your history, but without resetting the 'index'. The 'index' contains
944*49cdfc7eSAndroid Build Coastguard Workerchanges which you have staged with +git add+, but not yet committed.
945*49cdfc7eSAndroid Build Coastguard Worker
946*49cdfc7eSAndroid Build Coastguard WorkerSo now that the commit history has been cleaned up, we need to submit a patch
947*49cdfc7eSAndroid Build Coastguard Workerto the mailing list or make a pull request on GitHub. The mailing list is the
948*49cdfc7eSAndroid Build Coastguard Workerpreferred place to make submissions and is more difficult for most people, so
949*49cdfc7eSAndroid Build Coastguard WorkerI will only cover that method.
950*49cdfc7eSAndroid Build Coastguard Worker
951*49cdfc7eSAndroid Build Coastguard WorkerJust before we create the patch, we need to check that our changes will still
952*49cdfc7eSAndroid Build Coastguard Workerapply to the master branch without problems. To do this we can use another
953*49cdfc7eSAndroid Build Coastguard Workertype of 'rebase' and then try rebuilding and running the test.
954*49cdfc7eSAndroid Build Coastguard Worker
955*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
956*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
957*49cdfc7eSAndroid Build Coastguard Worker$ git checkout master
958*49cdfc7eSAndroid Build Coastguard Worker$ git pull origin
959*49cdfc7eSAndroid Build Coastguard Worker$ git checkout tutorial-rebase2
960*49cdfc7eSAndroid Build Coastguard Worker$ git rebase master
961*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
962*49cdfc7eSAndroid Build Coastguard Worker
963*49cdfc7eSAndroid Build Coastguard WorkerAbove, I update the master branch and then replay our changes onto it using
964*49cdfc7eSAndroid Build Coastguard Worker+git rebase master+. You may find that after the rebase there is a merge
965*49cdfc7eSAndroid Build Coastguard Workerconflict. This will result in something which looks like the following (taken
966*49cdfc7eSAndroid Build Coastguard Workerfrom a Makefile conflict which was caused by reordering commits in a 'rebase').
967*49cdfc7eSAndroid Build Coastguard Worker
968*49cdfc7eSAndroid Build Coastguard Worker[source,diff]
969*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
970*49cdfc7eSAndroid Build Coastguard Worker<<<<<<< HEAD
971*49cdfc7eSAndroid Build Coastguard Workercve-2016-7117:	LDFLAGS += -lpthread
972*49cdfc7eSAndroid Build Coastguard Worker=======
973*49cdfc7eSAndroid Build Coastguard Workercve-2014-0196:	LDFLAGS += -lpthread -lutil -lrt
974*49cdfc7eSAndroid Build Coastguard Workercve-2016-7117:	LDFLAGS += -lpthread -lrt
975*49cdfc7eSAndroid Build Coastguard Worker>>>>>>> 4dbfb8e79... Add -lrt
976*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
977*49cdfc7eSAndroid Build Coastguard Worker
978*49cdfc7eSAndroid Build Coastguard WorkerThe first line tells us this is the beginning of a conflict. The third line
979*49cdfc7eSAndroid Build Coastguard Workerseparates the two conflicting pieces of content and the last line is the end
980*49cdfc7eSAndroid Build Coastguard Workerof the conflict. Usually, all you need to do is remove the lines you don't
981*49cdfc7eSAndroid Build Coastguard Workerwant, stage the changes and continue the 'rebase' with +git rebase
982*49cdfc7eSAndroid Build Coastguard Worker--continue+.
983*49cdfc7eSAndroid Build Coastguard Worker
984*49cdfc7eSAndroid Build Coastguard WorkerIn order to create a patch e-mail we use https://git-scm.com/docs/git-format-patch[+git format-patch+],
985*49cdfc7eSAndroid Build Coastguard Workerwe can then send that e-mail using https://git-scm.com/docs/git-send-email[+git send-email+].
986*49cdfc7eSAndroid Build Coastguard WorkerIt is also possible to import the patch (+mbox+) file into a number of e-mail programs.
987*49cdfc7eSAndroid Build Coastguard Worker
988*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
989*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
990*49cdfc7eSAndroid Build Coastguard Worker$ git format-patch -1 -v 2 -o output --to [email protected] fd3cc8596
991*49cdfc7eSAndroid Build Coastguard Workeroutput/v2-0001-tutorial-Add-a-step-by-step-C-test-tutorial.patch
992*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
993*49cdfc7eSAndroid Build Coastguard Worker
994*49cdfc7eSAndroid Build Coastguard WorkerThe first argument +-1+ specifies we want one commit from fd3cc8596
995*49cdfc7eSAndroid Build Coastguard Workeronwards. If we wanted this commit and the one after it we could specify +-2+
996*49cdfc7eSAndroid Build Coastguard Workerinstead.
997*49cdfc7eSAndroid Build Coastguard Worker
998*49cdfc7eSAndroid Build Coastguard WorkerThis is my second patch submission so I have used +-v 2+, which indicates this
999*49cdfc7eSAndroid Build Coastguard Workeris the second version of a patch set. The +-o+ option specifies the output
1000*49cdfc7eSAndroid Build Coastguard Workerdirectory (literally called +output+). The +--to+ option adds the +To:+ e-mail
1001*49cdfc7eSAndroid Build Coastguard Workerheader, which I have set to the LTP mailing list.
1002*49cdfc7eSAndroid Build Coastguard Worker
1003*49cdfc7eSAndroid Build Coastguard WorkerWe can then send this patch with the following command sans +--dry-run+.
1004*49cdfc7eSAndroid Build Coastguard Worker
1005*49cdfc7eSAndroid Build Coastguard Worker[source,shell]
1006*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
1007*49cdfc7eSAndroid Build Coastguard Worker$ git send-email --dry-run output/v2-0001-tutorial-Add-a-step-by-step-C-test-tutorial.patch
1008*49cdfc7eSAndroid Build Coastguard Worker--------------------------------------------------------------------------------
1009*49cdfc7eSAndroid Build Coastguard Worker
1010*49cdfc7eSAndroid Build Coastguard WorkerGit will ask some questions (which you can ignore) and then tell you what it
1011*49cdfc7eSAndroid Build Coastguard Workerwould do if this weren't a dry-run. In order for this to work you have to have
1012*49cdfc7eSAndroid Build Coastguard Workera valid SMTP server set in +.gitconfig+ and also be signed up to the LTP
1013*49cdfc7eSAndroid Build Coastguard Workermailing list under the same e-mail address you have configured in Git. You can
1014*49cdfc7eSAndroid Build Coastguard Workersign up at https://lists.linux.it/listinfo/ltp.
1015*49cdfc7eSAndroid Build Coastguard Worker
1016*49cdfc7eSAndroid Build Coastguard Worker8. Doing code review
1017*49cdfc7eSAndroid Build Coastguard Worker--------------------
1018*49cdfc7eSAndroid Build Coastguard Worker
1019*49cdfc7eSAndroid Build Coastguard WorkerWhile waiting for your test to be reviewed, you are invited and encouraged to
1020*49cdfc7eSAndroid Build Coastguard Workerreview other contributors' code. This may seem bizarre when you are completely
1021*49cdfc7eSAndroid Build Coastguard Workernew to the project, but there are two important ways in which you can
1022*49cdfc7eSAndroid Build Coastguard Workercontribute here:
1023*49cdfc7eSAndroid Build Coastguard Worker
1024*49cdfc7eSAndroid Build Coastguard WorkerA.   Point out logical errors in the code.
1025*49cdfc7eSAndroid Build Coastguard WorkerB.   Improve your own understanding
1026*49cdfc7eSAndroid Build Coastguard Worker
1027*49cdfc7eSAndroid Build Coastguard WorkerIt doesn't matter whether you know the canonical way of writing an LTP test in
1028*49cdfc7eSAndroid Build Coastguard WorkerC. An error of logic, when properly explained, is usually indisputable. These
1029*49cdfc7eSAndroid Build Coastguard Workerare the most important errors to find as they always result in false test
1030*49cdfc7eSAndroid Build Coastguard Workerresults. Once someone points out such an error it is usually obvious to
1031*49cdfc7eSAndroid Build Coastguard Workereveryone that it is a bug and needs to be fixed.
1032*49cdfc7eSAndroid Build Coastguard Worker
1033*49cdfc7eSAndroid Build Coastguard WorkerObviously testing the patch is one way of finding errors. You can apply
1034*49cdfc7eSAndroid Build Coastguard Workerpatches using +git am+. Then it is just a case of compiling and running the
1035*49cdfc7eSAndroid Build Coastguard Workertests.
1036*49cdfc7eSAndroid Build Coastguard Worker
1037*49cdfc7eSAndroid Build Coastguard WorkerFinally, reading and attempting to comment on other peoples patches, gives
1038*49cdfc7eSAndroid Build Coastguard Workeryou a better understanding of the reviewers perspective. This is better for
1039*49cdfc7eSAndroid Build Coastguard Workerthe project and for you.
1040*49cdfc7eSAndroid Build Coastguard Worker
1041*49cdfc7eSAndroid Build Coastguard WorkerStyle and organisational issues are best left to after you have found logical
1042*49cdfc7eSAndroid Build Coastguard Workererrors.
1043*49cdfc7eSAndroid Build Coastguard Worker
1044*49cdfc7eSAndroid Build Coastguard Worker9. Final notes
1045*49cdfc7eSAndroid Build Coastguard Worker--------------
1046*49cdfc7eSAndroid Build Coastguard Worker
1047*49cdfc7eSAndroid Build Coastguard WorkerHopefully you can now grasp the structure of an LTP test and have some idea of
1048*49cdfc7eSAndroid Build Coastguard Workerwhat is available in the LTP test library. There are a vast number of library
1049*49cdfc7eSAndroid Build Coastguard Workerfunctions available (mainly located in include and lib), some of which are
1050*49cdfc7eSAndroid Build Coastguard Workerdocumented in the test writing guidelines and many of which are not.
1051*49cdfc7eSAndroid Build Coastguard Worker
1052*49cdfc7eSAndroid Build Coastguard WorkerWe have only scratched the surface of the immense technical complexity of
1053*49cdfc7eSAndroid Build Coastguard Workersystems programming across multiple Kernel and C lib versions as well as
1054*49cdfc7eSAndroid Build Coastguard Workerdifferent hardware architectures. The important thing to take away from this
1055*49cdfc7eSAndroid Build Coastguard Workeris that you have to be conscientious of what will happen on systems different
1056*49cdfc7eSAndroid Build Coastguard Workerfrom yours. The LTP has a huge and varied user base, so situations you may
1057*49cdfc7eSAndroid Build Coastguard Workerthink are unlikely can and do happen to somebody.
1058*49cdfc7eSAndroid Build Coastguard Worker
1059*49cdfc7eSAndroid Build Coastguard WorkerOf course you don't want to spend time allowing for situations which may never
1060*49cdfc7eSAndroid Build Coastguard Workerarise either, so you have to do your research and think about each situation
1061*49cdfc7eSAndroid Build Coastguard Workercritically. The more systems you can test on before submitting your changes,
1062*49cdfc7eSAndroid Build Coastguard Workerthe better, although we understand not everyone has access to a lab.
1063*49cdfc7eSAndroid Build Coastguard Worker
1064*49cdfc7eSAndroid Build Coastguard WorkerOne important topic which has not been covered by this tutorial, is
1065*49cdfc7eSAndroid Build Coastguard Workermulti-process or multi-threaded testing. The LTP library functions work inside
1066*49cdfc7eSAndroid Build Coastguard Workerchild processes and threads, but their semantics change slightly. There are
1067*49cdfc7eSAndroid Build Coastguard Workeralso various helper functions for synchronising and forking processes. For
1068*49cdfc7eSAndroid Build Coastguard Workermore information see
1069*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/C-Test-API[C Test API],
1070*49cdfc7eSAndroid Build Coastguard Workerin particular sections
1071*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/C-Test-API#17-fork-ing[1.7 Fork()-ing] to
1072*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/C-Test-API#110-signals-and-signal-handlers[1.10 Signals and signal handlers] and
1073*49cdfc7eSAndroid Build Coastguard Workerhttps://github.com/linux-test-project/ltp/wiki/C-Test-API#114-thread-safety-in-the-ltp-library[1.14 Thread-safety in the LTP library].
1074*49cdfc7eSAndroid Build Coastguard Worker
1075*49cdfc7eSAndroid Build Coastguard WorkerWhen it comes time to submit a test, the preferred way to do it is on the
1076*49cdfc7eSAndroid Build Coastguard Workermailing list although you can also use GitHub. The LTP follows similar rules
1077*49cdfc7eSAndroid Build Coastguard Workerto the kernel for formatting and submitting patches. Generally speaking the
1078*49cdfc7eSAndroid Build Coastguard Workerreview cycle is easier for small patches, so try to make small changes or
1079*49cdfc7eSAndroid Build Coastguard Workeradditions where possible.
1080