Remove tabs in the source files of test cases

This commit is contained in:
LI Qing 2020-04-29 06:36:32 +00:00 committed by Tate, Hongliang Tian
parent 1dcabb09cd
commit 614ec88242
6 changed files with 290 additions and 290 deletions

@ -37,7 +37,7 @@ static int remove_file(const char *file_path) {
static int __test_write_read(const char *file_path) {
char *write_str = "Write to hostfs successfully!";
char read_buf[128] = { 0 };
char read_buf[128] = { 0 };
int fd;
fd = open(file_path, O_WRONLY);
@ -52,34 +52,34 @@ static int __test_write_read(const char *file_path) {
if (fd < 0) {
THROW_ERROR("failed to open a file to read");
}
if (read(fd, read_buf, sizeof(read_buf)) != strlen(write_str)) {
THROW_ERROR("failed to read to the file");
}
if (strcmp(write_str, read_buf) != 0) {
THROW_ERROR("the message read from the file is not as it was written");
}
if (read(fd, read_buf, sizeof(read_buf)) != strlen(write_str)) {
THROW_ERROR("failed to read to the file");
}
if (strcmp(write_str, read_buf) != 0) {
THROW_ERROR("the message read from the file is not as it was written");
}
close(fd);
return 0;
}
static int __test_rename(const char *file_path) {
char *rename_path = "/host/hostfs_rename.txt";
struct stat stat_buf;
int ret;
char *rename_path = "/host/hostfs_rename.txt";
struct stat stat_buf;
int ret;
ret = rename(file_path, rename_path);
if (ret < 0) {
ret = rename(file_path, rename_path);
if (ret < 0) {
THROW_ERROR("failed to rename");
}
ret = stat(file_path, &stat_buf);
if (!(ret < 0 && errno == ENOENT)) {
THROW_ERROR("stat should return ENOENT");
}
ret = stat(rename_path, &stat_buf);
if (ret < 0) {
THROW_ERROR("failed to stat the file");
}
if (rename(rename_path, file_path) < 0) {
ret = stat(file_path, &stat_buf);
if (!(ret < 0 && errno == ENOENT)) {
THROW_ERROR("stat should return ENOENT");
}
ret = stat(rename_path, &stat_buf);
if (ret < 0) {
THROW_ERROR("failed to stat the file");
}
if (rename(rename_path, file_path) < 0) {
THROW_ERROR("failed to rename back");
}
return 0;
@ -90,9 +90,9 @@ typedef int(*test_hostfs_func_t)(const char *);
static int test_hostfs_framework(test_hostfs_func_t fn) {
const char *file_path = "/host/hostfs_test.txt";
if (create_file(file_path) < 0)
return -1;
if (fn(file_path) < 0)
if (create_file(file_path) < 0)
return -1;
if (fn(file_path) < 0)
return -1;
if (remove_file(file_path) < 0)
return -1;

@ -7,84 +7,84 @@
#include <errno.h>
int main(int argc, const char* argv[]) {
const char* file_name = "/root/test_filesystem_link.txt";
const char* link_name = "/root/link.txt";
const char* write_msg = "Hello World\n";
char read_buf[128] = {0};
int ret;
const char* file_name = "/root/test_filesystem_link.txt";
const char* link_name = "/root/link.txt";
const char* write_msg = "Hello World\n";
char read_buf[128] = {0};
int ret;
// create a file and write message
int flags = O_WRONLY | O_CREAT| O_TRUNC;
int mode = 00666;
int fd = open(file_name, flags, mode);
if (fd < 0) {
printf("ERROR: failed to open a file for write\n");
return -1;
}
int len = write(fd, write_msg, strlen(write_msg));
if (len <= 0) {
printf("ERROR: failed to write to the file\n");
return -1;
}
close(fd);
// create a file and write message
int flags = O_WRONLY | O_CREAT| O_TRUNC;
int mode = 00666;
int fd = open(file_name, flags, mode);
if (fd < 0) {
printf("ERROR: failed to open a file for write\n");
return -1;
}
int len = write(fd, write_msg, strlen(write_msg));
if (len <= 0) {
printf("ERROR: failed to write to the file\n");
return -1;
}
close(fd);
// link
ret = link(file_name, link_name);
if(ret < 0) {
printf("ERROR: failed to link the file\n");
return -1;
}
// link
ret = link(file_name, link_name);
if(ret < 0) {
printf("ERROR: failed to link the file\n");
return -1;
}
// read the link file
fd = open(link_name, O_RDONLY, 00666);
if (fd < 0) {
printf("ERROR: failed to open the file for read\n");
return -1;
}
len = read(fd, read_buf, sizeof(read_buf));
if (len != strlen(write_msg)) {
printf("ERROR: failed to read to the file\n");
return -1;
}
ret = strcmp(write_msg, read_buf);
if (ret != 0) {
printf("ERROR: the message read from the file is not as it was written\n");
return -1;
}
// read the link file
fd = open(link_name, O_RDONLY, 00666);
if (fd < 0) {
printf("ERROR: failed to open the file for read\n");
return -1;
}
len = read(fd, read_buf, sizeof(read_buf));
if (len != strlen(write_msg)) {
printf("ERROR: failed to read to the file\n");
return -1;
}
ret = strcmp(write_msg, read_buf);
if (ret != 0) {
printf("ERROR: the message read from the file is not as it was written\n");
return -1;
}
// unlink
ret = unlink(link_name);
if(ret < 0) {
printf("ERROR: failed to link the file\n");
return -1;
}
// unlink
ret = unlink(link_name);
if(ret < 0) {
printf("ERROR: failed to link the file\n");
return -1;
}
// stat
struct stat stat_buf;
ret = stat(link_name, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
printf("ERROR: stat on \"%s\" should return ENOENT", link_name);
return -1;
}
// stat
struct stat stat_buf;
ret = stat(link_name, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
printf("ERROR: stat on \"%s\" should return ENOENT", link_name);
return -1;
}
// rename
ret = rename(file_name, link_name);
if(ret < 0) {
printf("ERROR: failed to rename the file");
return -1;
}
// rename
ret = rename(file_name, link_name);
if(ret < 0) {
printf("ERROR: failed to rename the file");
return -1;
}
// stat
ret = stat(file_name, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
printf("ERROR: stat on \"%s\" should return ENOENT", file_name);
return -1;
}
ret = stat(link_name, &stat_buf);
if(ret < 0) {
printf("ERROR: failed to stat the file");
return -1;
}
// stat
ret = stat(file_name, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
printf("ERROR: stat on \"%s\" should return ENOENT", file_name);
return -1;
}
ret = stat(link_name, &stat_buf);
if(ret < 0) {
printf("ERROR: failed to stat the file");
return -1;
}
printf("link, unlink, rename test successful\n");
return 0;

@ -7,62 +7,62 @@
int main(int argc, const char* argv[]) {
const int BUF_SIZE = 20;
char buf[10];
int ret;
const int BUF_SIZE = 20;
char buf[10];
int ret;
char* cwd = getcwd(buf, BUF_SIZE);
if(cwd != buf) {
printf("failed to getcwd\n");
return -1;
}
char* cwd = getcwd(buf, BUF_SIZE);
if(cwd != buf) {
printf("failed to getcwd\n");
return -1;
}
const char expect_cwd[] = "/";
if(strcmp(buf, expect_cwd)) {
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, expect_cwd);
return -1;
}
const char expect_cwd[] = "/";
if(strcmp(buf, expect_cwd)) {
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, expect_cwd);
return -1;
}
//const char DIR_NAME[] = "test_dir";
//const char DIR_PATH[] = "/test_dir";
const char DIR_NAME[] = "/root/test_dir";
const char DIR_PATH[] = "/root/test_dir";
const int DIR_MODE = 0664;
ret = mkdir(DIR_NAME, DIR_MODE);
if(ret < 0) {
printf("failed to mkdir \"%s\"", DIR_NAME);
return ret;
}
//const char DIR_NAME[] = "test_dir";
//const char DIR_PATH[] = "/test_dir";
const char DIR_NAME[] = "/root/test_dir";
const char DIR_PATH[] = "/root/test_dir";
const int DIR_MODE = 0664;
ret = mkdir(DIR_NAME, DIR_MODE);
if(ret < 0) {
printf("failed to mkdir \"%s\"", DIR_NAME);
return ret;
}
ret = chdir(DIR_NAME);
if(ret < 0) {
printf("failed to chdir to \"%s\"", DIR_NAME);
return ret;
}
ret = chdir(DIR_NAME);
if(ret < 0) {
printf("failed to chdir to \"%s\"", DIR_NAME);
return ret;
}
cwd = getcwd(buf, BUF_SIZE);
if(cwd != buf) {
printf("failed to getcwd\n");
return -1;
}
cwd = getcwd(buf, BUF_SIZE);
if(cwd != buf) {
printf("failed to getcwd\n");
return -1;
}
if(strcmp(buf, DIR_PATH)) {
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, DIR_PATH);
return -1;
}
if(strcmp(buf, DIR_PATH)) {
printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, DIR_PATH);
return -1;
}
ret = rmdir(DIR_PATH);
if(ret < 0) {
printf("failed to rmdir \"%s\"", DIR_PATH);
return ret;
}
ret = rmdir(DIR_PATH);
if(ret < 0) {
printf("failed to rmdir \"%s\"", DIR_PATH);
return ret;
}
struct stat stat_buf;
ret = stat(DIR_PATH, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
printf("stat on \"%s\" should return ENOENT", DIR_PATH);
return ret;
}
struct stat stat_buf;
ret = stat(DIR_PATH, &stat_buf);
if(!(ret < 0 && errno == ENOENT)) {
printf("stat on \"%s\" should return ENOENT", DIR_PATH);
return ret;
}
printf("getcwd, mkdir, rmdir, chdir test successful\n");
return 0;

@ -45,35 +45,35 @@ int main(int argc, const char* argv[]) {
return -1;
}
int file_mode = stat_buf.st_mode & MODE_MASK;
if (file_mode != mode) {
printf("Incorrect file mode %o. Expected %o\n", file_mode, mode);
return -1;
}
int file_type = stat_buf.st_mode & S_IFMT;
if (file_type != S_IFREG) {
printf("Incorrect file type %o. Expected %o\n", file_type, S_IFREG);
return -1;
}
if (file_mode != mode) {
printf("Incorrect file mode %o. Expected %o\n", file_mode, mode);
return -1;
}
int file_type = stat_buf.st_mode & S_IFMT;
if (file_type != S_IFREG) {
printf("Incorrect file type %o. Expected %o\n", file_type, S_IFREG);
return -1;
}
close(fd);
ret = truncate(FILE_NAME, TRUNC_LEN1);
if (ret < 0) {
printf("failed to truncate the file\n");
return ret;
}
ret = truncate(FILE_NAME, TRUNC_LEN1);
if (ret < 0) {
printf("failed to truncate the file\n");
return ret;
}
ret = stat(FILE_NAME, &stat_buf);
if (ret < 0) {
printf("failed to stat the file\n");
return ret;
}
ret = stat(FILE_NAME, &stat_buf);
if (ret < 0) {
printf("failed to stat the file\n");
return ret;
}
file_size = stat_buf.st_size;
if (file_size != TRUNC_LEN1) {
printf("Incorrect file size %d. Expected %d\n", file_size, TRUNC_LEN1);
return -1;
}
file_size = stat_buf.st_size;
if (file_size != TRUNC_LEN1) {
printf("Incorrect file size %d. Expected %d\n", file_size, TRUNC_LEN1);
return -1;
}
printf("(f)truncate, (f)stat test successful\n");
return 0;

@ -133,7 +133,7 @@ int test_multiple_socketpairs() {
ret = -1;
goto cleanup;
}
if(verify_connection(sockets[i][1], sockets[i][0]) < 0) {
ret = -1;
goto cleanup;

@ -21,150 +21,150 @@
const char SOCK_PATH[] = "echo_socket";
int create_server_socket() {
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
printf("ERROR: failed to create a unix socket\n");
return -1;
}
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
printf("ERROR: failed to create a unix socket\n");
return -1;
}
struct sockaddr_un local;
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
socklen_t len = strlen(local.sun_path) + sizeof(local.sun_family);
struct sockaddr_un local;
local.sun_family = AF_UNIX;
strcpy(local.sun_path, SOCK_PATH);
unlink(local.sun_path);
socklen_t len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(fd, (struct sockaddr *)&local, len) == -1) {
printf("ERROR: failed to bind\n");
return -1;
}
if (bind(fd, (struct sockaddr *)&local, len) == -1) {
printf("ERROR: failed to bind\n");
return -1;
}
if (listen(fd, 5) == -1) {
printf("ERROR: failed to listen\n");
return -1;
}
return fd;
if (listen(fd, 5) == -1) {
printf("ERROR: failed to listen\n");
return -1;
}
return fd;
}
int create_client_socket() {
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
printf("ERROR: failed to create a unix socket\n");
return -1;
}
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd == -1) {
printf("ERROR: failed to create a unix socket\n");
return -1;
}
struct sockaddr_un remote;
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
socklen_t len = strlen(remote.sun_path) + sizeof(remote.sun_family);
struct sockaddr_un remote;
remote.sun_family = AF_UNIX;
strcpy(remote.sun_path, SOCK_PATH);
socklen_t len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(fd, (struct sockaddr *)&remote, len) == -1) {
printf("ERROR: failed to connect\n");
return -1;
}
return fd;
if (connect(fd, (struct sockaddr *)&remote, len) == -1) {
printf("ERROR: failed to connect\n");
return -1;
}
return fd;
}
int main(int argc, const char* argv[]) {
size_t buf_size, total_bytes;
if (argc >= 2) {
buf_size = atol(argv[1]);
} else {
buf_size = BUF_SIZE;
}
if (argc >= 3) {
total_bytes = atol(argv[2]);
} else {
// BUG: throughput fall down when buf_size > 65536
total_bytes = buf_size > 65536? buf_size << 15: buf_size << 21;
}
printf("buf_size = 0x%zx\n", buf_size);
printf("total_bytes = 0x%zx\n", total_bytes);
size_t buf_size, total_bytes;
if (argc >= 2) {
buf_size = atol(argv[1]);
} else {
buf_size = BUF_SIZE;
}
if (argc >= 3) {
total_bytes = atol(argv[2]);
} else {
// BUG: throughput fall down when buf_size > 65536
total_bytes = buf_size > 65536? buf_size << 15: buf_size << 21;
}
printf("buf_size = 0x%zx\n", buf_size);
printf("total_bytes = 0x%zx\n", total_bytes);
int listen_fd = create_server_socket();
if (listen_fd == -1) {
printf("ERROR: failed to create server socket\n");
return -1;
}
int listen_fd = create_server_socket();
if (listen_fd == -1) {
printf("ERROR: failed to create server socket\n");
return -1;
}
int socket_rd_fd = create_client_socket();
if (socket_rd_fd == -1) {
printf("ERROR: failed to create client socket\n");
return -1;
}
int socket_rd_fd = create_client_socket();
if (socket_rd_fd == -1) {
printf("ERROR: failed to create client socket\n");
return -1;
}
struct sockaddr_un remote;
socklen_t len = sizeof(remote);
int socket_wr_fd = accept(listen_fd, (struct sockaddr *)&remote, &len);
if (socket_wr_fd == -1) {
printf("ERROR: failed to accept socket\n");
return -1;
}
struct sockaddr_un remote;
socklen_t len = sizeof(remote);
int socket_wr_fd = accept(listen_fd, (struct sockaddr *)&remote, &len);
if (socket_wr_fd == -1) {
printf("ERROR: failed to accept socket\n");
return -1;
}
// The following is same as 'pipe_throughput'
// The following is same as 'pipe_throughput'
// Spawn a child process that reads from the pipe
posix_spawn_file_actions_t file_actions;
posix_spawn_file_actions_init(&file_actions);
posix_spawn_file_actions_adddup2(&file_actions, socket_rd_fd, STDIN_FILENO);
posix_spawn_file_actions_addclose(&file_actions, socket_wr_fd);
// Spawn a child process that reads from the pipe
posix_spawn_file_actions_t file_actions;
posix_spawn_file_actions_init(&file_actions);
posix_spawn_file_actions_adddup2(&file_actions, socket_rd_fd, STDIN_FILENO);
posix_spawn_file_actions_addclose(&file_actions, socket_wr_fd);
int child_pid;
extern char ** environ;
char* new_argv[] = {"/bin/data_sink", NULL};
if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions,
NULL, new_argv, environ) < 0) {
printf("ERROR: failed to spawn a child process\n");
return -1;
}
close(socket_rd_fd);
int child_pid;
extern char ** environ;
char* new_argv[] = {"/bin/data_sink", NULL};
if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions,
NULL, new_argv, environ) < 0) {
printf("ERROR: failed to spawn a child process\n");
return -1;
}
close(socket_rd_fd);
// Start the timer
struct timeval tv_start, tv_end;
gettimeofday(&tv_start, NULL);
// Start the timer
struct timeval tv_start, tv_end;
gettimeofday(&tv_start, NULL);
// Tell the reader how many data are to be transfered
size_t remain_bytes = total_bytes;
if (write(socket_wr_fd, &remain_bytes, sizeof(remain_bytes)) != sizeof(remain_bytes)) {
printf("ERROR: failed to write to pipe\n");
return -1;
}
// Tell the reader how many data are to be transfered
size_t remain_bytes = total_bytes;
if (write(socket_wr_fd, &remain_bytes, sizeof(remain_bytes)) != sizeof(remain_bytes)) {
printf("ERROR: failed to write to pipe\n");
return -1;
}
// Tell the reader the buffer size that it should use
if (write(socket_wr_fd, &buf_size, sizeof(buf_size)) != sizeof(buf_size)) {
printf("ERROR: failed to write to pipe\n");
return -1;
}
// Tell the reader the buffer size that it should use
if (write(socket_wr_fd, &buf_size, sizeof(buf_size)) != sizeof(buf_size)) {
printf("ERROR: failed to write to pipe\n");
return -1;
}
// Write a specified amount of data in a buffer of specified size
char buf[BUF_SIZE] = {0};
while (remain_bytes > 0) {
size_t len = MIN(buf_size, remain_bytes);
if ((len = write(socket_wr_fd, &buf, len)) < 0) {
printf("ERROR: failed to write to pipe\n");
return -1;
}
remain_bytes -= len;
}
// Write a specified amount of data in a buffer of specified size
char buf[BUF_SIZE] = {0};
while (remain_bytes > 0) {
size_t len = MIN(buf_size, remain_bytes);
if ((len = write(socket_wr_fd, &buf, len)) < 0) {
printf("ERROR: failed to write to pipe\n");
return -1;
}
remain_bytes -= len;
}
// Wait for the child process to read all data and exit
int status = 0;
if (wait4(child_pid, &status, 0, NULL) < 0) {
printf("ERROR: failed to wait4 the child process\n");
return -1;
}
// Wait for the child process to read all data and exit
int status = 0;
if (wait4(child_pid, &status, 0, NULL) < 0) {
printf("ERROR: failed to wait4 the child process\n");
return -1;
}
// Stop the timer
gettimeofday(&tv_end, NULL);
// Stop the timer
gettimeofday(&tv_end, NULL);
// Calculate the throughput
double total_s = (tv_end.tv_sec - tv_start.tv_sec)
+ (double)(tv_end.tv_usec - tv_start.tv_usec) / 1000000;
if (total_s < 1.0) {
printf("WARNING: run long enough to get meaningful results\n");
if (total_s == 0) { return 0; }
}
double total_mb = (double)total_bytes / MB;
double throughput = total_mb / total_s;
printf("Throughput of unix socket is %.2f MB/s\n", throughput);
return 0;
// Calculate the throughput
double total_s = (tv_end.tv_sec - tv_start.tv_sec)
+ (double)(tv_end.tv_usec - tv_start.tv_usec) / 1000000;
if (total_s < 1.0) {
printf("WARNING: run long enough to get meaningful results\n");
if (total_s == 0) { return 0; }
}
double total_mb = (double)total_bytes / MB;
double throughput = total_mb / total_s;
printf("Throughput of unix socket is %.2f MB/s\n", throughput);
return 0;
}