1 // SPDX-License-Identifier: GPL-2.0+
7 #include <sys/socket.h>
9 #include <sys/sysmacros.h>
10 #include <sys/types.h>
12 #include "../kselftest_harness.h"
14 /* Remove a file, ignoring the result if it didn't exist. */
15 void rm(struct __test_metadata
*_metadata
, const char *pathname
,
23 rc
= unlink(pathname
);
26 ASSERT_EQ(errno
, ENOENT
) {
27 TH_LOG("Not ENOENT: %s", pathname
);
31 TH_LOG("Failed to remove: %s", pathname
);
46 void (*setup
)(struct __test_metadata
*_metadata
,
47 FIXTURE_DATA(file
) *self
,
48 const FIXTURE_VARIANT(file
) *variant
);
49 int major
, minor
, mode
; /* for mknod() */
52 void setup_link(struct __test_metadata
*_metadata
,
53 FIXTURE_DATA(file
) *self
,
54 const FIXTURE_VARIANT(file
) *variant
)
56 const char * const paths
[] = {
62 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
63 if (access(paths
[i
], X_OK
) == 0) {
64 ASSERT_EQ(symlink(paths
[i
], self
->pathname
), 0);
69 TH_LOG("Could not find viable 'true' binary");
73 FIXTURE_VARIANT_ADD(file
, S_IFLNK
)
80 void setup_dir(struct __test_metadata
*_metadata
,
81 FIXTURE_DATA(file
) *self
,
82 const FIXTURE_VARIANT(file
) *variant
)
84 ASSERT_EQ(mkdir(self
->pathname
, 0755), 0);
87 FIXTURE_VARIANT_ADD(file
, S_IFDIR
)
95 void setup_node(struct __test_metadata
*_metadata
,
96 FIXTURE_DATA(file
) *self
,
97 const FIXTURE_VARIANT(file
) *variant
)
102 dev
= makedev(variant
->major
, variant
->minor
);
103 rc
= mknod(self
->pathname
, 0755 | variant
->mode
, dev
);
106 SKIP(return, "Please run as root; cannot mknod(%s)",
111 FIXTURE_VARIANT_ADD(file
, S_IFBLK
)
122 FIXTURE_VARIANT_ADD(file
, S_IFCHR
)
133 void setup_fifo(struct __test_metadata
*_metadata
,
134 FIXTURE_DATA(file
) *self
,
135 const FIXTURE_VARIANT(file
) *variant
)
137 ASSERT_EQ(mkfifo(self
->pathname
, 0755), 0);
140 FIXTURE_VARIANT_ADD(file
, S_IFIFO
)
149 ASSERT_GT(asprintf(&self
->pathname
, "%s.test", variant
->name
), 6);
150 self
->is_dir
= variant
->is_dir
;
152 rm(_metadata
, self
->pathname
, variant
->is_dir
);
153 variant
->setup(_metadata
, self
, variant
);
156 FIXTURE_TEARDOWN(file
)
158 rm(_metadata
, self
->pathname
, self
->is_dir
);
161 TEST_F(file
, exec_errno
)
163 char * const argv
[2] = { (char * const)self
->pathname
, NULL
};
165 EXPECT_LT(execv(argv
[0], argv
), 0);
166 EXPECT_EQ(errno
, variant
->expected
);
177 self
->fd
= socket(AF_INET
, SOCK_STREAM
, 0);
178 ASSERT_GE(self
->fd
, 0);
181 FIXTURE_TEARDOWN(sock
)
184 ASSERT_EQ(close(self
->fd
), 0);
187 TEST_F(sock
, exec_errno
)
189 char * const argv
[2] = { " magic socket ", NULL
};
190 char * const envp
[1] = { NULL
};
192 EXPECT_LT(fexecve(self
->fd
, argv
, envp
), 0);
193 EXPECT_EQ(errno
, EACCES
);