1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

Userland: Tests: Use mkstemp temporary files in tests

This commit is contained in:
Brendan Coles 2020-11-14 23:18:39 +00:00 committed by Andreas Kling
parent 12d923bb7e
commit d739483ee8
3 changed files with 24 additions and 15 deletions

View file

@ -28,8 +28,10 @@
#include <fcntl.h> #include <fcntl.h>
#include <pthread.h> #include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
@ -95,9 +97,15 @@ int main()
header.e_entry = 0; header.e_entry = 0;
int fd = open("/tmp/x", O_RDWR | O_CREAT, 0777); char path[] = "/tmp/x.XXXXXX";
auto fd = mkstemp(path);
if (fd < 0) { if (fd < 0) {
perror("open"); perror("mkstemp");
return 1;
}
if (fchmod(fd, 0777) < 0) {
perror("chmod");
return 1; return 1;
} }
@ -143,7 +151,7 @@ int main()
if (!fork()) { if (!fork()) {
try_again: try_again:
printf("exec\n"); printf("exec\n");
if (execl("/tmp/x", "x", nullptr) < 0) { if (execl(path, "x", nullptr) < 0) {
} }
goto try_again; goto try_again;
} }

View file

@ -35,11 +35,11 @@
static void test_change_file_contents() static void test_change_file_contents()
{ {
const char* path = "suid"; char path[] = "/tmp/suid.XXXXXX";
auto fd = mkstemp(path);
int fd = open(path, O_CREAT | O_RDWR, 06755);
assert(fd != -1); assert(fd != -1);
ftruncate(fd, 0); ftruncate(fd, 0);
assert(fchmod(fd, 06755) != -1);
char buffer[8]; char buffer[8];
memset(&buffer, 0, sizeof(buffer)); memset(&buffer, 0, sizeof(buffer));
@ -57,11 +57,11 @@ static void test_change_file_contents()
static void test_change_file_ownership() static void test_change_file_ownership()
{ {
const char* path = "suid"; char path[] = "/tmp/suid.XXXXXX";
auto fd = mkstemp(path);
int fd = open(path, O_CREAT | O_RDWR, 06755);
assert(fd != -1); assert(fd != -1);
ftruncate(fd, 0); ftruncate(fd, 0);
assert(fchmod(fd, 06755) != -1);
fchown(fd, getuid(), getgid()); fchown(fd, getuid(), getgid());
@ -77,11 +77,11 @@ static void test_change_file_ownership()
static void test_change_file_permissions() static void test_change_file_permissions()
{ {
const char* path = "suid"; char path[] = "/tmp/suid.XXXXXX";
auto fd = mkstemp(path);
int fd = open(path, O_CREAT | O_RDWR, 06755);
assert(fd != -1); assert(fd != -1);
ftruncate(fd, 0); ftruncate(fd, 0);
assert(fchmod(fd, 06755) != -1);
fchmod(fd, 0755); fchmod(fd, 0755);

View file

@ -137,9 +137,10 @@ static void test_write_to_file()
u8 glyph_width = 1; u8 glyph_width = 1;
auto font = Gfx::Font::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default); auto font = Gfx::Font::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
const char* font_path = "/tmp/new.font"; char path[] = "/tmp/new.font.XXXXXX";
assert(font->write_to_file(font_path)); assert(mkstemp(path) != -1);
unlink(font_path); assert(font->write_to_file(path));
unlink(path);
} }
int main(int, char**) int main(int, char**)