Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some miscellaneous cleanups for the included tests #30

Merged
merged 6 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion programs/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions programs/tests/t_abort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions programs/tests/t_creat.c
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 0 additions & 18 deletions programs/tests/t_exec_callee.c

This file was deleted.

2 changes: 1 addition & 1 deletion programs/tests/t_msgget.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions programs/tests/t_periodic1.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ int main(int argc, char *argv[])
sched_setparam(cpid, &param);
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");
}
Expand Down
2 changes: 1 addition & 1 deletion programs/tests/t_semget.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 1 addition & 13 deletions programs/tests/t_sigfpe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion programs/tests/t_siginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions programs/tests/t_sigusr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -51,5 +51,5 @@ int main(int argc, char *argv[])
kill(getpid(), SIGUSR2);

while(1) { };
return 0;
return 1;
}
58 changes: 58 additions & 0 deletions programs/tests/t_write_read.c
Original file line number Diff line number Diff line change
@@ -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;
}
Loading