From 88b1846a434188abadb3f5d10becda0bf3929908 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 15 Jan 2024 14:03:24 +0100 Subject: [PATCH 1/6] tests: fix paths to the other periodic tests --- programs/tests/t_periodic1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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"); } From f124be1cf79b664956c5fef72b8b54052ac8c1ff Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 13 Feb 2024 21:39:53 +0100 Subject: [PATCH 2/6] tests: add simple test for small consecutive writes This test was written for: 4d4504486debb69b0f05e181a1c071f2b916d63a. --- programs/tests/CMakeLists.txt | 1 + programs/tests/t_write_read.c | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 programs/tests/t_write_read.c diff --git a/programs/tests/CMakeLists.txt b/programs/tests/CMakeLists.txt index 3b366ed..d1dc66b 100644 --- a/programs/tests/CMakeLists.txt +++ b/programs/tests/CMakeLists.txt @@ -1,5 +1,6 @@ # List of programs. set(TEST_LIST + t_write_read.c t_gid.c t_alarm.c t_periodic3.c 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; +} From afa3d4fb66952739219e438ee8351780787bd94f Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 14 Feb 2024 14:03:35 +0100 Subject: [PATCH 3/6] tests: remove the not used t_exec_callee --- programs/tests/CMakeLists.txt | 1 - programs/tests/t_exec_callee.c | 18 ------------------ 2 files changed, 19 deletions(-) delete mode 100644 programs/tests/t_exec_callee.c diff --git a/programs/tests/CMakeLists.txt b/programs/tests/CMakeLists.txt index d1dc66b..eca7307 100644 --- a/programs/tests/CMakeLists.txt +++ b/programs/tests/CMakeLists.txt @@ -15,7 +15,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_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; -} From 65393843a566e389892f5309ad4901ff5f096ba3 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 14 Feb 2024 14:14:32 +0100 Subject: [PATCH 4/6] tests: use files that actually exist Commit af79958f1ad526b45e025b8df9527237d940d3ac removed a lot of files including /home/user/test7.txt used in some tests. Replace /home/user/test7.txt with /README. --- programs/tests/t_msgget.c | 2 +- programs/tests/t_semget.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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_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; From 7b54ff8d2a15e27ac23f2fedb591ab8178468e5c Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 14 Feb 2024 14:24:10 +0100 Subject: [PATCH 5/6] tests: exit with 0 when the test succeeds. --- programs/tests/t_abort.c | 2 ++ programs/tests/t_sigfpe.c | 14 +------------- programs/tests/t_siginfo.c | 2 +- programs/tests/t_sigusr.c | 4 ++-- 4 files changed, 6 insertions(+), 16 deletions(-) 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_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; } From 43054529d5cf0e26fa14957f9cbddbe5db71c750 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Thu, 29 Feb 2024 09:49:24 +0100 Subject: [PATCH 6/6] add creat test Tesing commit f7e53f9b0066060c0f7c7119eaad1da99bcc1d4c. --- programs/tests/CMakeLists.txt | 1 + programs/tests/t_creat.c | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 programs/tests/t_creat.c diff --git a/programs/tests/CMakeLists.txt b/programs/tests/CMakeLists.txt index eca7307..7d7635e 100644 --- a/programs/tests/CMakeLists.txt +++ b/programs/tests/CMakeLists.txt @@ -1,5 +1,6 @@ # List of programs. set(TEST_LIST + t_creat.c t_write_read.c t_gid.c t_alarm.c 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; +}