Merge pull request #30 from fischerling/cleanup-tests
Some miscellaneous cleanups for the included tests
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
# List of programs.
|
||||
set(TEST_LIST
|
||||
t_creat.c
|
||||
t_write_read.c
|
||||
t_gid.c
|
||||
t_alarm.c
|
||||
t_periodic3.c
|
||||
@@ -14,7 +16,6 @@ set(TEST_LIST
|
||||
t_sigmask.c
|
||||
t_getenv.c
|
||||
t_sigusr.c
|
||||
t_exec_callee.c
|
||||
t_shmget.c
|
||||
t_stopcont.c
|
||||
t_semop.c
|
||||
|
||||
@@ -23,6 +23,8 @@ void sig_handler(int sig)
|
||||
printf("handler(%d) : Correct signal. ABRT (%d/3)\n", sig, counter);
|
||||
if (counter < 3)
|
||||
abort();
|
||||
else
|
||||
exit(0);
|
||||
|
||||
} else {
|
||||
printf("handler(%d) : Wrong signal.\n", sig);
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/// @file t_creat.c
|
||||
/// @brief test the creat syscall
|
||||
/// @copyright (c) 2024 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <strerror.h>
|
||||
#include <sys/errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/unistd.h>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int fd = creat("foo", 0660);
|
||||
if (fd < 0) {
|
||||
printf("creat: foo: %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (write(fd, "foo", 3) != 3) {
|
||||
printf("write: foo: %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct stat_t st;
|
||||
if (stat("foo", &st) < 0) {
|
||||
printf("stat: foo: %s\n", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (st.st_size != 3) {
|
||||
printf("Wrong file size. (expected: 3, is: %u)\n", st.st_size);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
/// @file t_exec_callee.c
|
||||
/// @brief
|
||||
/// @copyright (c) 2014-2024 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *ENV_VAR = getenv("ENV_VAR");
|
||||
if (ENV_VAR == NULL) {
|
||||
printf("Failed to get env: `ENV_VAR`\n");
|
||||
return 1;
|
||||
}
|
||||
printf("ENV_VAR = %s\n", ENV_VAR);
|
||||
return 0;
|
||||
}
|
||||
@@ -55,7 +55,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
// ========================================================================
|
||||
// Generating a key using ftok
|
||||
key = ftok("/home/user/test7.txt", 5);
|
||||
key = ftok("/README", 5);
|
||||
if (key < 0) {
|
||||
perror("Failed to generate key using ftok");
|
||||
return 1;
|
||||
|
||||
@@ -22,12 +22,12 @@ int main(int argc, char *argv[])
|
||||
sched_setparam(cpid, ¶m);
|
||||
int counter = 0;
|
||||
if (fork() == 0) {
|
||||
char *_argv[] = { "/bin/periodic2", NULL };
|
||||
char *_argv[] = { "/bin/tests/t_periodic2", NULL };
|
||||
execv(_argv[0], _argv);
|
||||
printf("This is bad, I should not be here! EXEC NOT WORKING\n");
|
||||
}
|
||||
if (fork() == 0) {
|
||||
char *_argv[] = { "/bin/periodic3", NULL };
|
||||
char *_argv[] = { "/bin/tests/t_periodic3", NULL };
|
||||
execv(_argv[0], _argv);
|
||||
printf("This is bad, I should not be here! EXEC NOT WORKING\n");
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
// ========================================================================
|
||||
// Generating a key using ftok
|
||||
key = ftok("/home/user/test7.txt", 5);
|
||||
key = ftok("/README", 5);
|
||||
if (key < 0) {
|
||||
perror("Failed to generate key using ftok.");
|
||||
return 1;
|
||||
|
||||
@@ -18,25 +18,13 @@ void sig_handler(int sig)
|
||||
if (sig == SIGFPE) {
|
||||
printf("handler(%d) : Correct signal. FPE\n", sig);
|
||||
printf("handler(%d) : Exiting\n", sig);
|
||||
// exit(1);
|
||||
exit(0);
|
||||
} else {
|
||||
printf("handler(%d) : Wrong signal.\n", sig);
|
||||
}
|
||||
printf("handler(%d) : Ending handler.\n", sig);
|
||||
}
|
||||
|
||||
long int fact(int n)
|
||||
{
|
||||
if (n == 0 || n == 1) {
|
||||
return 1;
|
||||
}
|
||||
long int pro = 1;
|
||||
while (n != 1) {
|
||||
pro *= n--;
|
||||
}
|
||||
return pro;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
sigaction_t action;
|
||||
|
||||
@@ -19,7 +19,7 @@ void sig_handler_info(int sig, siginfo_t *siginfo)
|
||||
printf("handler(%d, %p) : Correct signal.\n", sig, siginfo);
|
||||
printf("handler(%d, %p) : Code : %d\n", sig, siginfo, siginfo->si_code);
|
||||
printf("handler(%d, %p) : Exiting\n", sig, siginfo);
|
||||
exit(1);
|
||||
exit(0);
|
||||
} else {
|
||||
printf("handler(%d, %p) : Wrong signal.\n", sig, siginfo);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ void sig_handler(int sig)
|
||||
counter += 1;
|
||||
|
||||
if (counter == 2)
|
||||
exit(1);
|
||||
exit(0);
|
||||
|
||||
} else {
|
||||
printf("handler(%d) : Wrong signal.\n", sig);
|
||||
@@ -51,5 +51,5 @@ int main(int argc, char *argv[])
|
||||
kill(getpid(), SIGUSR2);
|
||||
|
||||
while(1) { };
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/// @file t_write_read.c
|
||||
/// @brief Test consecutive writes
|
||||
/// @copyright (c) 2024 This file is distributed under the MIT License.
|
||||
/// See LICENSE.md for details.
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <strerror.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/unistd.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char *file = "t_write_read_file";
|
||||
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
|
||||
|
||||
int fd = creat(file, mode);
|
||||
if (fd < 0) {
|
||||
printf("Failed to open file %s: %s\n", file, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (write(fd, "foo", 3) != 3) {
|
||||
printf("First write to %s failed: %s\n", file, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (write(fd, "bar", 3) != 3) {
|
||||
printf("Second write to %s failed: %s\n", file, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
fd = open(file, O_RDONLY, 0);
|
||||
if (fd < 0) {
|
||||
printf("Failed to open file %s: %s\n", file, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ssize_t bytes_read;
|
||||
char buf[7];
|
||||
buf[6] = 0;
|
||||
if ((bytes_read = read(fd, &buf, 6)) < 0) {
|
||||
printf("Reading from file %s failed: %s\n", file, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (strcmp(buf, "foobar") != 0) {
|
||||
printf("Unexpected file content: %s\n", buf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
unlink(file);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user