LTP 第三章 开发系统调用测试集

来源:互联网 发布:一键4g网络解锁助手 编辑:程序博客网 时间:2024/06/05 10:56

LTP系列链接:

本文同步发布于我的个人网站:LTP教程

第一章 LTP介绍及内部机制

第二章 开发Shell测试集

第三章 开发系统调用测试集

第四章 开发_exit()测试集

第五章 开发IO操作测试集

第六章 开发IO阻塞测试集


3 开发系统调用测试集

在第二章中我们写了Shell测试集,在本章,我们将开发基于C的系统调用测试集,使用新API重写既有的测试case,整个流程与上一章基本相同,不再赘述。

在本章中,我们将编写以下几个测试:

  • Convert getpagesize01

  • Convert getpid01

  • Convert unlink05

  • Convert getppid02

在开始之前,保证工作区的干净:

[wxs@bogon ltp]$ git status# 位于分支 master无文件要提交,干净的工作区

3.1 Convert getpagesize01

3.1.1 重写代码

进入getpagesize目录,删除原有getpagesize01文件,并重写getpagesize01.c

[wxs@bogon ltp]$ cd testcases/kernel/syscalls/getpagesize/[wxs@bogon getpagesize]$ lsgetpagesize01  getpagesize01.c  Makefile[wxs@bogon getpagesize]$ rm getpagesize01*[wxs@bogon getpagesize]$ vim getpagesize01.c
/* * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it would be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write the Free Software Foundation, Inc. *//********************************************************** * *    TEST IDENTIFIER   : getpagesize01 * *    TEST TITLE        : Basic tests for getpagesize(2) * *    TEST CASE TOTAL   : 1 * *    AUTHOR            : jitwxs *                        <jitwxs@foxmail.com> * *    DESCRIPTION *      This is a Phase I test for the getpagesize(2) system call. *      It is intended to provide a limited exposure of the system call. * **********************************************************/#include <errno.h>#include "tst_test.h"static void testGetpagesize(void){    int size, ret_sysconf;    TEST(getpagesize());    if (TEST_RETURN == -1) {        tst_res(TFAIL | TTERRNO, "getpagesize failed");    } else {        size = getpagesize();        tst_res(TINFO, "Page Size is %d", size);        ret_sysconf = sysconf(_SC_PAGESIZE);        if (size == ret_sysconf)            tst_res(TPASS,                    "getpagesize - Page size returned %d",                    ret_sysconf);        else            tst_res(TFAIL,                    "getpagesize - Page size returned %d",                    ret_sysconf);    }}static struct tst_test test = {    .test_all = testGetpagesize};

make并测试正确性:

[wxs@bogon getpagesize]$ makemake -C "/home/wxs/ltp/lib" -f "/home/wxs/ltp/lib/Makefile" allmake[1]: 进入目录“/home/wxs/ltp/lib”make[2]: 进入目录“/home/wxs/ltp/lib/newlib_tests”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/home/wxs/ltp/lib/newlib_tests”make[2]: 进入目录“/home/wxs/ltp/lib/tests”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/home/wxs/ltp/lib/tests”make[1]: 离开目录“/home/wxs/ltp/lib”gcc -g -O2 -g -O2 -fno-strict-aliasing -pipe -Wall -W -Wold-style-definition -D_FORTIFY_SOURCE=2 -I../../../../include -I../../../../include -I../../../../include/old/   -L../../../../lib  getpagesize01.c   -lltp -o getpagesize01[wxs@bogon getpagesize]$ ./getpagesize01 tst_test.c:934: INFO: Timeout per run is 0h 05m 00sgetpagesize01.c:44: INFO: Page Size is 4096getpagesize01.c:49: PASS: getpagesize - Page size returned 4096Summary:passed   1failed   0skipped  0warnings 0

3.1.2 提交git

[wxs@bogon getpagesize]$ cd ~/ltp[wxs@bogon ltp]$ git status# 位于分支 mybranch# 尚未暂存以备提交的变更:#   (使用 "git add <file>..." 更新要提交的内容)#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)##   修改:      testcases/kernel/syscalls/getpagesize/getpagesize01.c## 未跟踪的文件:#   (使用 "git add <file>..." 以包含要提交的内容)##   testcases/kernel/log修改尚未加入提交(使用 "git add" 和/或 "git commit -a")[wxs@bogon ltp]$ git add testcases/kernel/syscalls/getpagesize/getpagesize01.c[wxs@bogon ltp]$ git commit -s -m "syscalls/getpagesize: Convert to new API for test getpagesize(2)"[mybranch 3c7efcb] syscalls/getpagesize: Convert to new API for test getpagesize(2) 1 file changed, 60 insertions(+), 105 deletions(-) rewrite testcases/kernel/syscalls/getpagesize/getpagesize01.c (90%)

3.1.3 生成patch并校验

[wxs@bogon ltp]$ git format-patch -10001-syscalls-getpagesize-Convert-to-new-API-for-test-get.patch[wxs@bogon ltp]$ ../linux/scripts/checkpatch.pl 0001-syscalls-getpagesize-Convert-to-new-API-for-test-get.patch total: 0 errors, 0 warnings, 132 lines checked0001-syscalls-getpagesize-Convert-to-new-API-for-test-get.patch has no obvious style problems and is ready for submission.

3.2 Convert getpid01

3.2.1 重写代码

进入getpagesize目录,删除原有getpid01文件,并重写getpid01.c

[wxs@bogon ltp]$ cd testcases/kernel/syscalls/getpid/[wxs@bogon getpid]$ lsgetpid01  getpid01.c  getpid02  getpid02.c  Makefile[wxs@bogon getpid]$ rm getpid01*[wxs@bogon getpid]$ vim getpid01.c
/* * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it would be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write the Free Software Foundation, Inc. *//********************************************************** * *    TEST IDENTIFIER   : getpid01 * *    TEST TITLE        : Basic tests for getpid(2) * *    TEST CASE TOTAL   : 1 * *    AUTHOR            : jitwxs *                        <jitwxs@foxmail.com> * *    DESCRIPTION *      Testcase to check the basic functionality of getpid(). * **********************************************************/#include <errno.h>#include "tst_test.h"static void test_getpid(void){    TEST(getpid());    if (TEST_RETURN == -1)        tst_res(TFAIL | TTERRNO, "getpid failed");    else        tst_res(TPASS, "getpid returned %ld", TEST_RETURN);}static struct tst_test test = {    .test_all = test_getpid};

make并测试正确性:

[wxs@bogon getpid]$ makemake -C "/home/wxs/ltp/lib" -f "/home/wxs/ltp/lib/Makefile" allmake[1]: 进入目录“/home/wxs/ltp/lib”make[2]: 进入目录“/home/wxs/ltp/lib/newlib_tests”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/home/wxs/ltp/lib/newlib_tests”make[2]: 进入目录“/home/wxs/ltp/lib/tests”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/home/wxs/ltp/lib/tests”make[1]: 离开目录“/home/wxs/ltp/lib”gcc -g -O2 -g -O2 -fno-strict-aliasing -pipe -Wall -W -Wold-style-definition -D_FORTIFY_SOURCE=2 -I../../../../include -I../../../../include -I../../../../include/old/   -L../../../../lib  getpid01.c   -lltp -o getpid01[wxs@bogon getpid]$ ./getpid01 tst_test.c:934: INFO: Timeout per run is 0h 05m 00sgetpid01.c:41: PASS: getpid returned 17650Summary:passed   1failed   0skipped  0warnings 0

3.2.2 提交git

[wxs@bogon getpid]$ cd ~/ltp/[wxs@bogon ltp]$ git status# 位于分支 mybranch# 尚未暂存以备提交的变更:#   (使用 "git add <file>..." 更新要提交的内容)#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)##   修改:      testcases/kernel/syscalls/getpid/getpid01.c## 未跟踪的文件:#   (使用 "git add <file>..." 以包含要提交的内容)##   testcases/kernel/log修改尚未加入提交(使用 "git add" 和/或 "git commit -a")[wxs@bogon ltp]$ git add testcases/kernel/syscalls/getpid/getpid01.c[wxs@bogon ltp]$ git commit -s -m "syscalls/getpid: Convert to new API for test getpid(2)"[mybranch 6b82447] syscalls/getpid: Convert to new API for test getpid(2) 1 file changed, 47 insertions(+), 158 deletions(-) rewrite testcases/kernel/syscalls/getpid/getpid01.c (83%) ```#### 3.2.3 生成patch并校验

[wxs@bogon ltp]gitformatpatch20001syscallsgetpagesizeConverttonewAPIfortestget.patch0002syscallsgetpidConverttonewAPIfortestgetpid2.patch[wxs@bogonltp] ../linux/scripts/checkpatch.pl 0002-syscalls-getpid-Convert-to-new-API-for-test-getpid-2.patch
total: 0 errors, 0 warnings, 173 lines checked

0002-syscalls-getpid-Convert-to-new-API-for-test-getpid-2.patch has no obvious style problems and is ready for submission.

### 3.3 Convert unlink05#### 3.3.1 重写代码进入`unlink`目录,删除原有unlink05文件,并重写`unlink05.c`

[wxs@bogon ltp]cdtestcases/kernel/syscalls/unlink[wxs@bogonunlink] ls
Makefile unlink05.c unlink06.c unlink07.c unlink08.c
unlink05 unlink06 unlink07 unlink08
[wxs@bogon unlink]rmunlink05[wxs@bogonunlink] vim unlink05.c

```c/* * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it would be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write the Free Software Foundation, Inc. *//********************************************************** * *    TEST IDENTIFIER   : unlink05 * *    TEST TITLE        : Basic tests for unlink(1) * *    TEST CASE TOTAL   : 1 * *    AUTHOR            : jitwxs *                        <jitwxs@foxmail.com> * *    DESCRIPTION *      It should/will be extended when full functional tests are written for *      unlink(2). * **********************************************************/<div class="se-preview-section-delimiter"></div>#include <errno.h><div class="se-preview-section-delimiter"></div>#include "tst_test.h"<div class="se-preview-section-delimiter"></div>#define TMPFILE "tmpfile"static void testUnlink(void){    TEST(unlink(TMPFILE));    if (TEST_RETURN == -1 || access(TMPFILE, F_OK) == 0)        tst_res(TFAIL | TTERRNO, "unlink(%s) Failed, errno=%d : %s",                TMPFILE, TEST_ERRNO, strerror(TEST_ERRNO));    else        tst_res(TPASS, "unlink(%s) returned %ld",                TMPFILE, TEST_RETURN);}static void setup(void){    SAFE_TOUCH(TMPFILE, 0777, NULL);}static struct tst_test test = {    .needs_tmpdir = 1,    .setup = setup,    .test_all = testUnlink};<div class="se-preview-section-delimiter"></div>

make并测试正确性:

[wxs@bogon unlink]$ makemake -C "/home/wxs/ltp/lib" -f "/home/wxs/ltp/lib/Makefile" allmake[1]: 进入目录“/home/wxs/ltp/lib”make[2]: 进入目录“/home/wxs/ltp/lib/newlib_tests”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/home/wxs/ltp/lib/newlib_tests”make[2]: 进入目录“/home/wxs/ltp/lib/tests”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/home/wxs/ltp/lib/tests”make[1]: 离开目录“/home/wxs/ltp/lib”gcc -g -O2 -g -O2 -fno-strict-aliasing -pipe -Wall -W -Wold-style-definition -D_FORTIFY_SOURCE=2 -I../../../../include -I../../../../include -I../../../../include/old/   -L../../../../lib  unlink05.c   -lltp -o unlink05[wxs@bogon unlink]$ ./unlink05 tst_test.c:934: INFO: Timeout per run is 0h 05m 00sunlink05.c:45: PASS: unlink(tmpfile) returned 0Summary:passed   1failed   0skipped  0warnings 0<div class="se-preview-section-delimiter"></div>

3.3.2 提交git

[wxs@bogon unlink]$ cd ~/ltp/[wxs@bogon ltp]$ git status<div class="se-preview-section-delimiter"></div># 位于分支 mybranch<div class="se-preview-section-delimiter"></div># 尚未暂存以备提交的变更:<div class="se-preview-section-delimiter"></div>#   (使用 "git add <file>..." 更新要提交的内容)<div class="se-preview-section-delimiter"></div>#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)#<div class="se-preview-section-delimiter"></div>#   修改:      testcases/kernel/syscalls/unlink/unlink05.c#<div class="se-preview-section-delimiter"></div># 未跟踪的文件:<div class="se-preview-section-delimiter"></div>#   (使用 "git add <file>..." 以包含要提交的内容)#<div class="se-preview-section-delimiter"></div>#   testcases/kernel/log修改尚未加入提交(使用 "git add" 和/或 "git commit -a")[wxs@bogon ltp]$ git add testcases/kernel/syscalls/unlink/unlink05.c[wxs@bogon ltp]$ git commit -s -m "syscalls/unlink: Convert to new API for test unlink(1)"[mybranch 860a098] syscalls/unlink: Convert to new API for test unlink(1) 1 file changed, 57 insertions(+), 209 deletions(-) rewrite testcases/kernel/syscalls/unlink/unlink05.c (88%)<div class="se-preview-section-delimiter"></div>

3.3.3 生成patch并校验

[wxs@bogon ltp]$ git format-patch -30001-syscalls-getpagesize-Convert-to-new-API-for-test-get.patch0002-syscalls-getpid-Convert-to-new-API-for-test-getpid-2.patch0003-syscalls-unlink-Convert-to-new-API-for-test-unlink-1.patch[wxs@bogon ltp]$ ../linux/scripts/checkpatch.pl 0003-syscalls-unlink-Convert-to-new-API-for-test-unlink-1.patch total: 0 errors, 0 warnings, 235 lines checked0003-syscalls-unlink-Convert-to-new-API-for-test-unlink-1.patch has no obvious style problems and is ready for submission.<div class="se-preview-section-delimiter"></div>

3.4 Convert getppid02

3.4.1 重写代码

进入getppid目录,删除原有getppid02文件,并重写getppid02.c

[wxs@bogon ltp]$ cd testcases/kernel/syscalls/getppid/[wxs@bogon getppid]$ lsgetppid01  getppid01.c  getppid02  getppid02.c  Makefile[wxs@bogon getppid]$ rm getppid02*[wxs@bogon getppid]$ vim getppid02.c<div class="se-preview-section-delimiter"></div>
/* * This program is free software; you can redistribute it and/or modify it * under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it would be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write the Free Software Foundation, Inc. *//********************************************************** * *    TEST IDENTIFIER   : getppid02 * *    TEST TITLE        : Basic tests for getppid(2) * *    TEST CASE TOTAL   : 1 * *    AUTHOR            : jitwxs *                        <jitwxs@foxmail.com> * *    DESCRIPTION *      Testcase to check the basic functionality of the getppid() syscall. * **********************************************************/<div class="se-preview-section-delimiter"></div>#include <errno.h><div class="se-preview-section-delimiter"></div>#include <sys/wait.h><div class="se-preview-section-delimiter"></div>#include "tst_test.h"static void testGetppid(void){    int status;    pid_t pid, ppid;    ppid = getpid();    pid = SAFE_FORK();    if (pid == -1)        tst_res(TFAIL, "fork failed");    else if (pid == 0) {        TEST(getppid());        if (ppid != TEST_RETURN)            tst_res(TFAIL | TERRNO,                    "getppid Failed (%ld != %d)",                    TEST_RETURN, ppid);        else            tst_res(TPASS,                    "getppid Success (%ld == %d)",                    TEST_RETURN, ppid);    } else {        if (wait(&status) == -1)            tst_res(TBROK | TERRNO,                    "wait failed");        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)            tst_res(TFAIL,                    "getppid functionality incorrect");    }}static struct tst_test test = {    .test_all = testGetppid,    .forks_child = 1};<div class="se-preview-section-delimiter"></div>

make并测试正确性:

[wxs@bogon getppid]$ makemake -C "/home/wxs/ltp/lib" -f "/home/wxs/ltp/lib/Makefile" allmake[1]: 进入目录“/home/wxs/ltp/lib”make[2]: 进入目录“/home/wxs/ltp/lib/newlib_tests”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/home/wxs/ltp/lib/newlib_tests”make[2]: 进入目录“/home/wxs/ltp/lib/tests”make[2]: 对“all”无需做任何事。make[2]: 离开目录“/home/wxs/ltp/lib/tests”make[1]: 离开目录“/home/wxs/ltp/lib”gcc -g -O2 -g -O2 -fno-strict-aliasing -pipe -Wall -W -Wold-style-definition -D_FORTIFY_SOURCE=2 -I../../../../include -I../../../../include -I../../../../include/old/   -L../../../../lib  getppid02.c   -lltp -o getppid02[wxs@bogon getppid]$ ./getppid02 tst_test.c:934: INFO: Timeout per run is 0h 05m 00sgetppid02.c:54: PASS: getppid Success (18017 == 18017)Summary:passed   1failed   0skipped  0warnings 0<div class="se-preview-section-delimiter"></div>

3.4.2 提交git

[wxs@bogon getppid]$ cd ~/ltp[wxs@bogon ltp]$ git status<div class="se-preview-section-delimiter"></div># 位于分支 mybranch<div class="se-preview-section-delimiter"></div># 尚未暂存以备提交的变更:<div class="se-preview-section-delimiter"></div>#   (使用 "git add <file>..." 更新要提交的内容)<div class="se-preview-section-delimiter"></div>#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)#<div class="se-preview-section-delimiter"></div>#   修改:      testcases/kernel/syscalls/getppid/getppid02.c#<div class="se-preview-section-delimiter"></div># 未跟踪的文件:<div class="se-preview-section-delimiter"></div>#   (使用 "git add <file>..." 以包含要提交的内容)#<div class="se-preview-section-delimiter"></div>#   testcases/kernel/log修改尚未加入提交(使用 "git add" 和/或 "git commit -a")[wxs@bogon ltp]$ git add testcases/kernel/syscalls/getppid/getppid02.c[wxs@bogon ltp]$ git commit -s -m "syscalls/getppid: Convert to new API for test getppid(2)"[mybranch 80d5955] syscalls/getppid: Convert to new API for test getppid(2) 1 file changed, 69 insertions(+), 107 deletions(-) rewrite testcases/kernel/syscalls/getppid/getppid02.c (95%)<div class="se-preview-section-delimiter"></div>

3.4.3 生成patch并校验

[wxs@bogon ltp]$ git format-patch -40001-syscalls-getpagesize-Convert-to-new-API-for-test-get.patch0002-syscalls-getpid-Convert-to-new-API-for-test-getpid-2.patch0003-syscalls-unlink-Convert-to-new-API-for-test-unlink-1.patch0004-syscalls-getppid-Convert-to-new-API-for-test-getppid.patch[wxs@bogon ltp]$ ../linux/scripts/checkpatch.pl 0004-syscalls-getppid-Convert-to-new-API-for-test-getppid.patch total: 0 errors, 0 warnings, 151 lines checked0004-syscalls-getppid-Convert-to-new-API-for-test-getppid.patch has no obvious style problems and is ready for submission.