diff --git a/programs/tests/CMakeLists.txt b/programs/tests/CMakeLists.txt index 3b366ed..7d7635e 100644 --- a/programs/tests/CMakeLists.txt +++ b/programs/tests/CMakeLists.txt @@ -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 diff --git a/programs/tests/t_abort.c b/programs/tests/t_abort.c index adfc2c8..b29ea93 100644 --- a/programs/tests/t_abort.c +++ b/programs/tests/t_abort.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); diff --git a/programs/tests/t_creat.c b/programs/tests/t_creat.c new file mode 100644 index 0000000..0589adb --- /dev/null +++ b/programs/tests/t_creat.c @@ -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 +#include +#include +#include +#include +#include +#include + +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; +} diff --git a/programs/tests/t_exec_callee.c b/programs/tests/t_exec_callee.c deleted file mode 100644 index f0c4228..0000000 --- a/programs/tests/t_exec_callee.c +++ /dev/null @@ -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 -#include - -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; -} diff --git a/programs/tests/t_msgget.c b/programs/tests/t_msgget.c index ebf4b05..1d3e933 100644 --- a/programs/tests/t_msgget.c +++ b/programs/tests/t_msgget.c @@ -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; diff --git a/programs/tests/t_periodic1.c b/programs/tests/t_periodic1.c index 08e4a40..ec86cd8 100644 --- a/programs/tests/t_periodic1.c +++ b/programs/tests/t_periodic1.c @@ -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"); } diff --git a/programs/tests/t_semget.c b/programs/tests/t_semget.c index 64c9ddf..ccfc81e 100644 --- a/programs/tests/t_semget.c +++ b/programs/tests/t_semget.c @@ -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; diff --git a/programs/tests/t_sigfpe.c b/programs/tests/t_sigfpe.c index 6c11366..b67efd3 100644 --- a/programs/tests/t_sigfpe.c +++ b/programs/tests/t_sigfpe.c @@ -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; diff --git a/programs/tests/t_siginfo.c b/programs/tests/t_siginfo.c index 80359da..8ad52d7 100644 --- a/programs/tests/t_siginfo.c +++ b/programs/tests/t_siginfo.c @@ -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); } diff --git a/programs/tests/t_sigusr.c b/programs/tests/t_sigusr.c index 274b346..768e9cc 100644 --- a/programs/tests/t_sigusr.c +++ b/programs/tests/t_sigusr.c @@ -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; } diff --git a/programs/tests/t_write_read.c b/programs/tests/t_write_read.c new file mode 100644 index 0000000..1cd9603 --- /dev/null +++ b/programs/tests/t_write_read.c @@ -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 +#include +#include +#include +#include +#include +#include + +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; +}