tests: Work on more thorough error handling

This commit is contained in:
Tibor Nagy
2019-02-21 16:15:49 +01:00
parent d1a424c002
commit f60c95d2ca
11 changed files with 161 additions and 34 deletions
+1 -2
View File
@@ -2,8 +2,7 @@
#include <unistd.h>
int main(int argc, char *argv[]) {
int i;
for(i = 0; i < argc; i++) {
for(int i = 0; i < argc; i++) {
write(STDOUT_FILENO, argv[i], strlen(argv[i]));
write(STDOUT_FILENO, " ", 1);
}
+6 -1
View File
@@ -1,5 +1,6 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
void ptimeval(struct timeval* val) {
@@ -8,14 +9,18 @@ void ptimeval(struct timeval* val) {
int main(void) {
struct rusage r_usage;
if (getrusage(RUSAGE_SELF, &r_usage) < 0) {
if (getrusage(RUSAGE_SELF, &r_usage) == -1) {
perror("getrusage");
exit(EXIT_FAILURE);
}
printf("ru_utime:");
ptimeval(&r_usage.ru_utime);
printf("ru_stime:");
ptimeval(&r_usage.ru_utime);
printf("ru_maxrss: %ld\n", r_usage.ru_maxrss);
printf("ru_ixrss: %ld\n", r_usage.ru_ixrss);
printf("ru_idrss: %ld\n", r_usage.ru_idrss);
+9 -1
View File
@@ -1,7 +1,15 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int status = brk((void*)100);
printf("brk exited with status code %d\n", status);
if (status == -1) {
perror("brk");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("brk returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
}
+34 -9
View File
@@ -1,15 +1,40 @@
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char* cwd1 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char));
getcwd(cwd1, 4096);
printf("initial cwd: %s\n", cwd1);
free(cwd1);
chdir("..");
char* cwd2 = malloc(4096*sizeof(char));//(char*) calloc(4096 + 1, sizeof(char));
getcwd(cwd2, 4096);
printf("final cwd: %s\n", cwd2);
free(cwd2);
char cwd[PATH_MAX] = { 0 };
char *cwd_result = NULL;
cwd_result = getcwd(cwd, PATH_MAX);
if (cwd_result == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
} else if (cwd_result != cwd) {
puts("getcwd returned something else than the buf argument");
exit(EXIT_FAILURE);
}
printf("getcwd before chdir: %s\n", cwd);
int status = chdir("..");
if (status == -1) {
perror("chdir");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("chdir returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
cwd_result = getcwd(cwd, PATH_MAX);
if (cwd_result == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
} else if (cwd_result != cwd) {
puts("getcwd returned something else than the buf argument");
exit(EXIT_FAILURE);
}
printf("getcwd after chdir: %s\n", cwd);
}
+7 -2
View File
@@ -1,8 +1,13 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char* args[] = {"sh", "-c", "echo 'exec works :D'", NULL};
execv("/bin/sh", args);
perror("execv");
int status = execv("/bin/sh", args);
if (status == -1) {
perror("execv");
exit(EXIT_FAILURE);
}
}
+26 -4
View File
@@ -1,11 +1,33 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int fd = open("..", 0, 0);
int status;
status = fchdir(fd);
printf("fchdir exited with status code %d\n", status);
close(fd);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
} else if (fd < 0) {
printf("open returned %d, unexpected result\n", fd);
exit(EXIT_FAILURE);
}
int status = fchdir(fd);
if (status == -1) {
perror("fchdir");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("fchdir returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
int c = close(fd);
if (c == -1) {
perror("close");
exit(EXIT_FAILURE);
} else if (c != 0) {
printf("close returned %d, unexpected result\n", c);
exit(EXIT_FAILURE);
}
}
+26 -4
View File
@@ -1,11 +1,33 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int fd = open(".", 0, 0);
int status;
status = fsync(fd);
printf("fsync exited with status code %d\n", status);
close(fd);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
} else if (fd < 0) {
printf("open returned %d, unexpected result\n", fd);
exit(EXIT_FAILURE);
}
int status = fsync(fd);
if (status == -1) {
perror("fsync");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("fsync returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
int c = close(fd);
if (c == -1) {
perror("close");
exit(EXIT_FAILURE);
} else if (c != 0) {
printf("close returned %d, unexpected result\n", c);
exit(EXIT_FAILURE);
}
}
+26 -4
View File
@@ -1,11 +1,33 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int fd = creat("ftruncate.out", 0777);
int status;
status = ftruncate(fd, 100);
printf("ftruncate exited with status code %d\n", status);
close(fd);
if (fd == -1) {
perror("creat");
exit(EXIT_FAILURE);
} else if (fd < 0) {
printf("creat returned %d, unexpected result\n", fd);
exit(EXIT_FAILURE);
}
int status = ftruncate(fd, 100);
if (status == -1) {
perror("ftruncate");
exit(EXIT_FAILURE);
} else if (status != 0) {
printf("ftruncate returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
int c = close(fd);
if (c == -1) {
perror("close");
exit(EXIT_FAILURE);
} else if (c != 0) {
printf("close returned %d, unexpected result\n", c);
exit(EXIT_FAILURE);
}
}
+1 -1
View File
@@ -5,7 +5,7 @@
#include <unistd.h>
int main(void) {
char first[PATH_MAX];
char first[PATH_MAX] = { 0 };
getcwd(first, PATH_MAX);
puts(first);
+9 -3
View File
@@ -3,10 +3,16 @@
#include <unistd.h>
int main(void) {
char* hostname = malloc(256);
if (gethostname(hostname, 256) == 0) {
char hostname[256] = { 0 };
int status = gethostname(hostname, 256);
if (status == 0) {
printf("Hostname: %s\n", hostname);
} else if (status == -1) {
perror("gethostname");
exit(EXIT_FAILURE);
} else {
puts("error getting hostname");
printf("gethostname returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
}
+16 -3
View File
@@ -1,11 +1,24 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void) {
// 1 is stdout
if (isatty(1)) {
int status = isatty(STDOUT_FILENO);
if (status == 1) {
puts("'Tis a tty :D");
} else if (status == 0) {
if (errno == ENOTTY) {
// I wouldn't consider stdout not being a TTY an error
// (CI runners, etc.)
puts("Whatever a tty is, it's not me");
} else {
perror("isatty");
exit(EXIT_FAILURE);
}
} else {
puts("Whatever a tty is, it's not me");
printf("isatty returned %d, unexpected result\n", status);
exit(EXIT_FAILURE);
}
}