Extend the test to include checks on truncate.
This commit is contained in:
@@ -11,48 +11,115 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/unistd.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int test_write_read()
|
||||
{
|
||||
char *file = "t_write_read_file";
|
||||
char *filename = "t_write_read_file";
|
||||
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
|
||||
|
||||
int fd = creat(file, mode);
|
||||
int fd = creat(filename, mode);
|
||||
if (fd < 0) {
|
||||
printf("Failed to open file %s: %s\n", file, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
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", file, strerror(errno));
|
||||
exit(1);
|
||||
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", file, strerror(errno));
|
||||
exit(1);
|
||||
printf("Second write to %s failed: %s\n", filename, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
fd = open(file, O_RDONLY, 0);
|
||||
fd = open(filename, O_RDONLY, 0);
|
||||
if (fd < 0) {
|
||||
printf("Failed to open file %s: %s\n", file, strerror(errno));
|
||||
exit(1);
|
||||
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", file, strerror(errno));
|
||||
exit(1);
|
||||
printf("Reading from file %s failed: %s\n", filename, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
if (strcmp(buf, "foobar") != 0) {
|
||||
printf("Unexpected file content: %s\n", buf);
|
||||
exit(1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unlink(file);
|
||||
unlink(filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_truncate()
|
||||
{
|
||||
char *filename = "test.txt";
|
||||
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
|
||||
char buffer[7];
|
||||
ssize_t bytes_read;
|
||||
int fd;
|
||||
|
||||
// Create the file.
|
||||
fd = creat(filename, mode);
|
||||
if (fd < 0) {
|
||||
printf("Failed to open file %s: %s\n", filename, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
if (write(fd, "foobar", 6) != 6) {
|
||||
printf("First write to %s failed: %s\n", filename, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
// Override the content.
|
||||
fd = open(filename, O_RDWR | O_TRUNC, 0);
|
||||
if (fd < 0) {
|
||||
printf("Failed to open file %s: %s\n", filename, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Delete the file.
|
||||
unlink(filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user