Skip to content

Commit 5cfb7f5

Browse files
acervpevik
authored andcommitted
Add utime07 test
This test has been extracted from symlink01 test and it verifies that utime() is working correctly on symlink() generated files. Link: https://lore.kernel.org/ltp/[email protected]/ Reviewed-by: Avinesh Kumar <[email protected]> Reviewed-by: Petr Vorel <[email protected]> Signed-off-by: Andrea Cervesato <[email protected]> [ pvorel: extend doc ] Signed-off-by: Petr Vorel <[email protected]>
1 parent b8a5974 commit 5cfb7f5

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

runtest/smoketest

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ wait02 wait02
99
write01 write01
1010
symlink01 symlink01
1111
stat04 symlink01 -T stat04
12-
utime01A symlink01 -T utime01
12+
utime07 utime07
1313
rename01A symlink01 -T rename01
1414
splice02 splice02 -s 20
1515
df01_sh df01.sh

runtest/syscalls

+1-1
Original file line numberDiff line numberDiff line change
@@ -1677,12 +1677,12 @@ ustat01 ustat01
16771677
ustat02 ustat02
16781678

16791679
utime01 utime01
1680-
utime01A symlink01 -T utime01
16811680
utime02 utime02
16821681
utime03 utime03
16831682
utime04 utime04
16841683
utime05 utime05
16851684
utime06 utime06
1685+
utime07 utime07
16861686

16871687
utimes01 utimes01
16881688

testcases/kernel/syscalls/utime/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/utime04
55
/utime05
66
/utime06
7+
/utime07
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4+
* Authors: David Fenner, Jon Hendrickson
5+
* Copyright (C) 2024 Andrea Cervesato <[email protected]>
6+
*/
7+
8+
/*\
9+
* [Description]
10+
*
11+
* This test verifies that utime() is working correctly on symlink()
12+
* generated files.
13+
*
14+
* Also verify if utime() fails:
15+
*
16+
* - ENOENT when symlink points to nowhere
17+
* - ELOOP when symlink is looping
18+
*/
19+
20+
#include <utime.h>
21+
#include "tst_test.h"
22+
23+
#define TIME_DIFF 100
24+
25+
static void create_symlink(const char *path, const char *symname)
26+
{
27+
struct stat asymlink;
28+
29+
SAFE_SYMLINK(path, symname);
30+
SAFE_LSTAT(symname, &asymlink);
31+
32+
if ((asymlink.st_mode & S_IFMT) != S_IFLNK) {
33+
tst_brk(TBROK, "symlink generated a non-symbolic link %s to %s",
34+
symname, path);
35+
}
36+
}
37+
38+
static void test_utime(void)
39+
{
40+
char *symname = "my_symlink0";
41+
struct stat oldsym_stat;
42+
struct stat newsym_stat;
43+
44+
tst_res(TINFO, "Test if utime() changes access time");
45+
46+
create_symlink(tst_get_tmpdir(), symname);
47+
SAFE_STAT(symname, &oldsym_stat);
48+
49+
struct utimbuf utimes = {
50+
.actime = oldsym_stat.st_atime + TIME_DIFF,
51+
.modtime = oldsym_stat.st_mtime + TIME_DIFF
52+
};
53+
54+
TST_EXP_PASS(utime(symname, &utimes));
55+
SAFE_STAT(symname, &newsym_stat);
56+
57+
TST_EXP_EQ_LI(newsym_stat.st_atime - oldsym_stat.st_atime, TIME_DIFF);
58+
TST_EXP_EQ_LI(newsym_stat.st_mtime - oldsym_stat.st_mtime, TIME_DIFF);
59+
60+
SAFE_UNLINK(symname);
61+
}
62+
63+
static void test_utime_no_path(void)
64+
{
65+
char *symname = "my_symlink1";
66+
struct utimbuf utimes;
67+
68+
tst_res(TINFO, "Test if utime() raises ENOENT when symlink points to nowhere");
69+
70+
create_symlink("bc+eFhi!k", symname);
71+
TST_EXP_FAIL(utime(symname, &utimes), ENOENT);
72+
73+
SAFE_UNLINK(symname);
74+
}
75+
76+
static void test_utime_loop(void)
77+
{
78+
char *symname = "my_symlink2";
79+
struct utimbuf utimes;
80+
81+
tst_res(TINFO, "Test if utime() raises ELOOP when symlink is looping");
82+
83+
create_symlink(symname, symname);
84+
TST_EXP_FAIL(utime(symname, &utimes), ELOOP);
85+
86+
SAFE_UNLINK(symname);
87+
}
88+
89+
static void run(void)
90+
{
91+
test_utime();
92+
test_utime_no_path();
93+
test_utime_loop();
94+
}
95+
96+
static struct tst_test test = {
97+
.test_all = run,
98+
.needs_tmpdir = 1,
99+
};

0 commit comments

Comments
 (0)