Mail Archives: djgpp-workers/2001/03/08/00:03:46
This patch adds O_TEMPORARY tests to the other tests for open.
Index: open.c
===================================================================
RCS file: /cvs/djgpp/djgpp/tests/libc/posix/fcntl/open.c,v
retrieving revision 1.3
diff -c -p -r1.3 open.c
*** open.c 2001/01/14 21:00:16 1.3
--- open.c 2001/03/08 04:59:50
***************
*** 19,28 ****
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
! static void test_success(int testnum, const char * fn, int flags,
const char * data);
int main(void)
{
int fd;
--- 19,32 ----
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+ #include <sys/stat.h>
! static void test_success(const char * fn, int flags,
const char * data);
+ static void test_o_temporary(void);
+ static int testnum;
+
int main(void)
{
int fd;
*************** int main(void)
*** 32,40 ****
fprintf(stderr, "Required data file is missing\n");
exit(1);
}
! test_success(1, "test1", O_RDONLY, "file1");
! test_success(2, "test1", O_RDONLY | O_NOLINK, "!<symlink>");
! test_success(3, "test2/file1", O_RDONLY | O_NOFOLLOW, "file1");
fd = open("test2/test1", O_RDONLY | O_NOFOLLOW);
if (fd != -1)
{
--- 36,45 ----
fprintf(stderr, "Required data file is missing\n");
exit(1);
}
! test_success("test1", O_RDONLY, "file1");
! test_success("test1", O_RDONLY | O_NOLINK, "!<symlink>");
! test_success("test2/file1", O_RDONLY | O_NOFOLLOW, "file1");
! ++testnum;
fd = open("test2/test1", O_RDONLY | O_NOFOLLOW);
if (fd != -1)
{
*************** int main(void)
*** 47,58 ****
exit(1);
}
printf("Test 4 passed\n");
! test_success(5, "test2/test1", O_RDONLY | O_NOLINK, "!<symlink>");
! test_success(6, "dir/test2", O_RDONLY, "tstlink2");
return 0;
}
! static void test_success(int testnum, const char * fn, int flags,
const char * data)
{
char err_buf[50];
--- 52,64 ----
exit(1);
}
printf("Test 4 passed\n");
! test_success("test2/test1", O_RDONLY | O_NOLINK, "!<symlink>");
! test_success("dir/test2", O_RDONLY, "tstlink2");
! test_o_temporary();
return 0;
}
! static void test_success(const char * fn, int flags,
const char * data)
{
char err_buf[50];
*************** static void test_success(int testnum, co
*** 60,65 ****
--- 66,73 ----
char buffer[100];
int fd = open(fn, flags);
int data_size = strlen(data);
+
+ ++testnum;
if (fd == -1)
{
sprintf(err_buf, "Test %d failed - unexpected open() failure ", testnum);
*************** static void test_success(int testnum, co
*** 93,95 ****
--- 101,196 ----
close(fd);
printf("Test %d passed\n", testnum);
}
+
+ const char temp_test_file[]="otemp";
+
+ int open_temp_test_file(int flags)
+ {
+ char err_buf[64];
+ int fd = open(temp_test_file, flags, S_IWUSR);
+
+ if (fd == -1)
+ {
+ sprintf(err_buf, "Test %d failed - unexpected open() failure ", testnum);
+ perror(err_buf);
+ exit(1);
+ }
+ return fd;
+ }
+
+ void start_temp_test(void)
+ {
+ int fd;
+
+ ++testnum;
+ fd = open_temp_test_file(O_WRONLY | O_CREAT | O_TRUNC);
+ write(fd, temp_test_file, sizeof(temp_test_file) - 1);
+ close(fd);
+ }
+
+ #define FILE_SHOULD_NOT_EXIST 0
+ #define FILE_SHOULD_STILL_EXIST 1
+
+ void close_temp_test_file(int fd, int should_exist)
+ {
+ const char *msg[]={"File was not deleted.",
+ "File was deleted too soon."};
+ int exists;
+
+ close(fd);
+ exists = __file_exists(temp_test_file) ? 1 : 0;
+ if (exists ^ should_exist)
+ {
+ fprintf(stderr, "Test %d failed - %s\n", testnum, msg[should_exist]);
+ exit(1);
+ }
+ }
+
+ void test_o_temporary(void)
+ {
+ int fd1, fd2;
+
+ start_temp_test();
+ fd1 = open_temp_test_file(O_RDONLY | O_TEMPORARY);
+ close_temp_test_file(fd1, FILE_SHOULD_NOT_EXIST);
+ printf("Test %d passed\n", testnum);
+
+ start_temp_test();
+ fd1 = open_temp_test_file(O_RDONLY | O_TEMPORARY);
+ fd2 = dup(fd1);
+ close_temp_test_file(fd1, FILE_SHOULD_STILL_EXIST);
+ close_temp_test_file(fd2, FILE_SHOULD_NOT_EXIST);
+ printf("Test %d passed\n", testnum);
+
+ start_temp_test();
+ fd1 = open_temp_test_file(O_RDONLY | O_TEMPORARY);
+ fd2 = 128;
+ dup2(fd1, fd2);
+ close_temp_test_file(fd1, FILE_SHOULD_STILL_EXIST);
+ close_temp_test_file(fd2, FILE_SHOULD_NOT_EXIST);
+ printf("Test %d passed\n", testnum);
+
+ start_temp_test();
+ fd1 = open_temp_test_file(O_RDONLY | O_TEMPORARY);
+ fd2 = open_temp_test_file(O_RDONLY);
+ close_temp_test_file(fd1, FILE_SHOULD_STILL_EXIST);
+ close_temp_test_file(fd2, FILE_SHOULD_NOT_EXIST);
+ printf("Test %d passed\n", testnum);
+
+ start_temp_test();
+ fd1 = open_temp_test_file(O_RDONLY);
+ fd2 = open_temp_test_file(O_RDONLY | O_TEMPORARY);
+ dup2(fd1, fd2);
+ close_temp_test_file(fd1, FILE_SHOULD_STILL_EXIST);
+ close_temp_test_file(fd2, FILE_SHOULD_NOT_EXIST);
+ printf("Test %d passed\n", testnum);
+
+ start_temp_test();
+ fd1 = open_temp_test_file(O_RDONLY | O_TEMPORARY);
+ fd2 = open_temp_test_file(O_RDONLY | O_TEMPORARY);
+ dup2(fd1, fd2);
+ close_temp_test_file(fd1, FILE_SHOULD_STILL_EXIST);
+ close_temp_test_file(fd2, FILE_SHOULD_NOT_EXIST);
+ printf("Test %d passed\n", testnum);
+ }
+
- Raw text -