diff --git a/programs/tests/t_write_read.c b/programs/tests/t_write_read.c index 1826d7a..c13b0e9 100644 --- a/programs/tests/t_write_read.c +++ b/programs/tests/t_write_read.c @@ -10,116 +10,165 @@ #include #include #include +#include +#include -int test_write_read() +int create_file(const char *filename, mode_t mode) { - char *filename = "t_write_read_file"; - mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; - int fd = creat(filename, mode); if (fd < 0) { - printf("Failed to open file %s: %s\n", filename, strerror(errno)); - return 1; - } - - if (write(fd, "foo", 3) != 3) { - printf("First write to %s failed: %s\n", filename, strerror(errno)); - return 1; - } - - if (write(fd, "bar", 3) != 3) { - printf("Second write to %s failed: %s\n", filename, strerror(errno)); - return 1; - } - - close(fd); - - fd = open(filename, O_RDONLY, 0); - if (fd < 0) { - printf("Failed to open file %s: %s\n", filename, strerror(errno)); - return 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", filename, strerror(errno)); - return 1; + printf("Failed to create file %s: %s\n", filename, strerror(errno)); + return EXIT_FAILURE; } close(fd); - - if (strcmp(buf, "foobar") != 0) { - printf("Unexpected file content: %s\n", buf); - return 1; - } - - unlink(filename); - return 0; + return EXIT_SUCCESS; } -int test_truncate() +int check_content(const char *filename, const char *content, int length) { - char *filename = "test.txt"; - mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; - char buffer[7]; - ssize_t bytes_read; - int fd; + pr_notice("Check content(%s, %s, %d)\n", filename, content, length); - // Create the file. - fd = creat(filename, mode); + char buffer[256]; + memset(buffer, 0, 256); + // Open the file. + int fd = open(filename, O_RDONLY, 0); if (fd < 0) { printf("Failed to open file %s: %s\n", filename, strerror(errno)); - return 1; + return EXIT_FAILURE; } - if (write(fd, "foobar", 6) != 6) { - printf("First write to %s failed: %s\n", filename, strerror(errno)); - return 1; + // Read the content. + if (read(fd, &buffer, max(min(length, 256), 0)) < 0) { + printf("Reading from file %s failed: %s\n", filename, strerror(errno)); + close(fd); + return EXIT_FAILURE; + } + if (strcmp(buffer, content) != 0) { + printf("Unexpected file content `%s`, expecting `%s`.\n", buffer, content); + close(fd); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +int write_content(const char *filename, const char *content, int length, int truncate, int append) +{ + pr_notice("Write content(%s, %s, %d, TRUNC: %d, APP: %d)\n", filename, content, length, truncate, append); + + int flags = O_WRONLY | (truncate ? O_TRUNC : append ? O_APPEND : + 0); + // Open the file. + int fd = open(filename, flags, 0); + if (fd < 0) { + printf("Failed to open file %s: %s\n", filename, strerror(errno)); + return EXIT_FAILURE; + } + // Read the content. + if (write(fd, content, length) < 0) { + printf("Writing on file %s failed: %s\n", filename, strerror(errno)); + close(fd); + return EXIT_FAILURE; } close(fd); + return EXIT_SUCCESS; +} - // Override the content. - fd = open(filename, O_RDWR | O_TRUNC, 0); +int test_write_read(const char *filename) +{ + char buf[7] = { 0 }; + // Create the file. + if (create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) { + return EXIT_FAILURE; + } + // Open the file. + int fd = open(filename, O_WRONLY, 0); if (fd < 0) { printf("Failed to open file %s: %s\n", filename, strerror(errno)); - return 1; + return EXIT_FAILURE; + } + if (write(fd, "foo", 3) != 3) { + printf("First write to %s failed: %s\n", filename, strerror(errno)); + close(fd); + return EXIT_FAILURE; } if (write(fd, "bar", 3) != 3) { - printf("Overriding the content of %s failed: %s\n", filename, strerror(errno)); - return 1; - } - // Go back to the beginning. - lseek(fd, 0, SEEK_SET); - // Read the content. - if ((bytes_read = read(fd, &buffer, 6)) < 0) { - printf("Reading from file %s failed: %s\n", filename, strerror(errno)); - return 1; + printf("Second write to %s failed: %s\n", filename, strerror(errno)); + close(fd); + return EXIT_FAILURE; } // Close the file. close(fd); - - printf("We found `%s`\n", buffer); - - if (strcmp(buffer, "bar") != 0) { - printf("Unexpected file content: %s\n", buffer); - return 1; + // Check the content. + if (check_content(filename, "foobar", 6)) { + return EXIT_FAILURE; } + return EXIT_SUCCESS; +} - // Delete the file. - unlink(filename); - return 0; +int test_truncate(const char *filename) +{ + if (create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) { + return EXIT_FAILURE; + } + if (write_content(filename, "foobar", 6, 0, 0)) { + return EXIT_FAILURE; + } + if (check_content(filename, "foobar", 6)) { + return EXIT_FAILURE; + } + if (write_content(filename, "bark", 4, 0, 0)) { + return EXIT_FAILURE; + } + if (check_content(filename, "barkar", 6)) { + return EXIT_FAILURE; + } + if (write_content(filename, "barf", 4, 1, 0)) { + return EXIT_FAILURE; + } + if (check_content(filename, "barf", 6)) { + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +int test_append(const char *filename) +{ + if (create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) { + return EXIT_FAILURE; + } + if (write_content(filename, "fusro", 5, 0, 0)) { + return EXIT_FAILURE; + } + if (write_content(filename, "dah", 3, 0, 1)) { + return EXIT_FAILURE; + } + if (check_content(filename, "fusrodah", 8)) { + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } int main(int argc, char *argv[]) { - printf("Running `test_write_read`...\n"); - if (test_write_read()) { - exit(EXIT_FAILURE); - } - printf("Running `test_truncate`...\n"); - if (test_truncate()) { - exit(EXIT_FAILURE); - } + char *filename = "test.txt"; + // printf("Running `test_write_read`...\n"); + // if (test_write_read(filename)) { + // unlink(filename); + // return EXIT_FAILURE; + // } + // unlink(filename); + + printf("Running `test_truncate`...\n"); + if (test_truncate(filename)) { + unlink(filename); + return EXIT_FAILURE; + } + unlink(filename); + + printf("Running `test_append`...\n"); + if (test_append(filename)) { + unlink(filename); + return EXIT_FAILURE; + } return EXIT_SUCCESS; }