From 2357f8ed1cdd243f6543e44fbf719a772ec78234 Mon Sep 17 00:00:00 2001 From: He Sun Date: Wed, 27 Nov 2019 10:14:19 +0800 Subject: [PATCH] Refactor THROW_ERROR macro in tests 1. Rename the macro name as all uppercase letters 2. Rewrite the macro in `do { ... } while (0)` instead of `while (1) { ... }` --- test/cpuid/main.c | 32 +++++------ test/device/main.c | 18 +++---- test/env/main.c | 24 ++++----- test/include/test.h | 10 ++-- test/ioctl/main.c | 8 +-- test/mmap/main.c | 114 ++++++++++++++++++++-------------------- test/sched/main.c | 44 ++++++++-------- test/sleep/main.c | 10 ++-- test/time/main.c | 6 +-- test/unix_socket/main.c | 5 -- 10 files changed, 133 insertions(+), 138 deletions(-) diff --git a/test/cpuid/main.c b/test/cpuid/main.c index bf9ee27a..4870a7bc 100644 --- a/test/cpuid/main.c +++ b/test/cpuid/main.c @@ -63,7 +63,7 @@ static int test_cpuid_with_basic_leaf_zero() { native_cpuid(leaf, subleaf, &cpu); // cpu should support sgx if (cpu.eax < 0x12) { - throw_error("failed to call cpuid with eax=0"); + THROW_ERROR("failed to call cpuid with eax=0"); } g_max_basic_leaf = cpu.eax; return 0; @@ -76,7 +76,7 @@ static int test_cpuid_with_basic_leaf_zero_with_subleaf() { native_cpuid(leaf, subleaf, &cpu); if (cpu.eax != g_max_basic_leaf) { - throw_error("failed to call cpuid with eax=0 and subleaf"); + THROW_ERROR("failed to call cpuid with eax=0 and subleaf"); } return 0; } @@ -88,7 +88,7 @@ static int test_cpuid_with_extend_leaf_zero() { native_cpuid(leaf, subleaf, &cpu); if (cpu.eax < 0x80000000) { - throw_error("failed to call cpuid with eax=0x80000000"); + THROW_ERROR("failed to call cpuid with eax=0x80000000"); } g_max_extend_leaf = cpu.eax; return 0; @@ -101,7 +101,7 @@ static int test_cpuid_with_extend_leaf_zero_with_subleaf() { native_cpuid(leaf, subleaf, &cpu); if (cpu.eax != g_max_extend_leaf) { - throw_error("failed to call cpuid with eax=0x80000000"); + THROW_ERROR("failed to call cpuid with eax=0x80000000"); } return 0; } @@ -119,7 +119,7 @@ static int test_cpuid_with_basic_leaf_one() { printf("Extended Model %d\n", (cpu.eax >> 16) & 0xF); // Bit 19-16 printf("Extended Family %d\n", (cpu.eax >> 20) & 0xFF); // Bit 27-20 if (cpu.eax == 0) { - throw_error("faild to call cpuid with eax=1"); + THROW_ERROR("faild to call cpuid with eax=1"); } return 0; } @@ -133,7 +133,7 @@ static int test_cpuid_with_sgx_verify() { //CPUID.(EAX=07H, ECX=0H):EBX.SGX = 1, // Bit 02: SGX. Supports IntelĀ® Software Guard Extensions (IntelĀ® SGX Extensions) if 1. if (((cpu.ebx >> 2) & 0x1) != 1) { - throw_error("failed to call cpuid to verify sgx"); + THROW_ERROR("failed to call cpuid to verify sgx"); } return 0; } @@ -147,16 +147,16 @@ static int test_cpuid_with_sgx_enumeration() { printf("Sgx 1 supported: %d\n", cpu.eax & 0x1); printf("Sgx 2 supported: %d\n", (cpu.eax >> 1) & 0x1); if (((cpu.eax & 0x1) | ((cpu.eax >> 1) & 0x1)) == 0) { - throw_error("failed to call cpuid to get SGX Capbilities"); + THROW_ERROR("failed to call cpuid to get SGX Capbilities"); } if (((cpu.edx & 0xFF) | ((cpu.edx >> 8) & 0xFF)) == 0) { - throw_error("get MaxEnclaveSize failed"); + THROW_ERROR("get MaxEnclaveSize failed"); } leaf = 0x12; subleaf = 1; native_cpuid(leaf, subleaf, &cpu); if ((cpu.eax | cpu.ebx | cpu.ecx | cpu.edx) == 0) { - throw_error("failed to call cpuid to get SGX Attributes"); + THROW_ERROR("failed to call cpuid to get SGX Attributes"); } return 0; } @@ -168,25 +168,25 @@ static int test_cpuid_with_invalid_leaf() { native_cpuid(leaf, subleaf, &cpu); if (cpu.eax | cpu.ebx | cpu.ecx | cpu.edx) { - throw_error("faild to call cpuid with invalid leaf 0x8"); + THROW_ERROR("faild to call cpuid with invalid leaf 0x8"); } leaf = 0xC; native_cpuid(leaf, subleaf, &cpu); if (cpu.eax | cpu.ebx | cpu.ecx | cpu.edx) { - throw_error("faild to call cpuid with invalid leaf 0xC"); + THROW_ERROR("faild to call cpuid with invalid leaf 0xC"); } leaf = 0xE; native_cpuid(leaf, subleaf, &cpu); if (cpu.eax | cpu.ebx | cpu.ecx | cpu.edx) { - throw_error("faild to call cpuid with invalid leaf 0xE"); + THROW_ERROR("faild to call cpuid with invalid leaf 0xE"); } leaf = 0x11; native_cpuid(leaf, subleaf, &cpu); if (cpu.eax | cpu.ebx | cpu.ecx | cpu.edx) { - throw_error("faild to call cpuid with invalid leaf 0x11"); + THROW_ERROR("faild to call cpuid with invalid leaf 0x11"); } return 0; } @@ -204,7 +204,7 @@ static int test_cpuid_with_oversized_leaf() { if ((cpu.eax != cpu_max.eax) || (cpu.ebx != cpu_max.ebx) || (cpu.ecx != cpu_max.ecx) || (cpu.edx != cpu_max.edx)) { - throw_error("failed to call cpuid with oversize leaf"); + THROW_ERROR("failed to call cpuid with oversize leaf"); } return 0; } @@ -230,7 +230,7 @@ static int test_cpuid_with_host_cpuidinfo() { char buff[BUFF_SIZE] = {0}; FILE * fp = fopen("./test_cpuid.txt","r"); if (fp == NULL) { - throw_error("failed to open host cpuid.txt"); + THROW_ERROR("failed to open host cpuid.txt"); } while (fgets(buff, BUFF_SIZE, fp)) { uint32_t leaf = 0; @@ -249,7 +249,7 @@ static int test_cpuid_with_host_cpuidinfo() { cpu.eax, cpu.ebx, cpu.ecx, cpu.edx); printf("sgx_eax:0x%x sgx_ebx:0x%x sgx_ecx:0x%x sgx_edx:0x%x\n", cpu_sgx.eax, cpu_sgx.ebx, cpu_sgx.ecx, cpu_sgx.edx); - throw_error("failed to check cpuid info"); + THROW_ERROR("failed to check cpuid info"); } } fclose(fp); diff --git a/test/device/main.c b/test/device/main.c index a8a49b03..8d767709 100644 --- a/test/device/main.c +++ b/test/device/main.c @@ -12,10 +12,10 @@ static int check_file_readable(const char* filename) { char buf[512] = {0}; int len; if ((fd = open(filename, O_RDONLY)) < 0) { - throw_error("failed to open the file"); + THROW_ERROR("failed to open the file"); } if ((len = read(fd, buf, sizeof(buf))) != sizeof(buf)) { - throw_error("failed to read the file"); + THROW_ERROR("failed to read the file"); } close(fd); return 0; @@ -26,10 +26,10 @@ static int check_file_writable(const char* filename) { char buf[512] = {0}; int len; if ((fd = open(filename, O_WRONLY)) < 0) { - throw_error("failed to open the file"); + THROW_ERROR("failed to open the file"); } if ((len = write(fd, buf, sizeof(buf))) != sizeof(buf)) { - throw_error("failed to read the file"); + THROW_ERROR("failed to read the file"); } close(fd); return 0; @@ -41,35 +41,35 @@ static int check_file_writable(const char* filename) { int test_dev_null() { if (check_file_writable("/dev/null")) { - throw_error("failed to write to /dev/null"); + THROW_ERROR("failed to write to /dev/null"); } return 0; } int test_dev_zero() { if (check_file_readable("/dev/zero")) { - throw_error("failed to read from /dev/null"); + THROW_ERROR("failed to read from /dev/null"); } return 0; } int test_dev_random() { if (check_file_readable("/dev/random")) { - throw_error("failed to read from /dev/random"); + THROW_ERROR("failed to read from /dev/random"); } return 0; } int test_dev_urandom() { if (check_file_readable("/dev/urandom")) { - throw_error("failed to read from /dev/urandom"); + THROW_ERROR("failed to read from /dev/urandom"); } return 0; } int test_dev_arandom() { if (check_file_readable("/dev/arandom")) { - throw_error("failed to read from /dev/arandom"); + THROW_ERROR("failed to read from /dev/arandom"); } return 0; } diff --git a/test/env/main.c b/test/env/main.c index b63ea913..93126186 100644 --- a/test/env/main.c +++ b/test/env/main.c @@ -74,11 +74,11 @@ static int test_env_getargv() { // Test argc if (g_argc != EXPECT_ARGC) { printf("ERROR: expect %d arguments, but %d are given\n", EXPECT_ARGC, g_argc); - throw_error("arguments count is not expected"); + THROW_ERROR("arguments count is not expected"); } // Test argv if (test_argv_val(expect_argv) < 0) { - throw_error("argument variables are not expected"); + THROW_ERROR("argument variables are not expected"); } return 0; } @@ -90,7 +90,7 @@ static int test_env_getargv() { static int test_env_getauxval() { unsigned long page_size = getauxval(AT_PAGESZ); if (errno != 0 || page_size != 4096) { - throw_error("auxilary vector does not pass correct the value"); + THROW_ERROR("auxilary vector does not pass correct the value"); } return 0; @@ -103,13 +103,13 @@ static int test_env_getauxval() { // The environment variables are specified in Occlum.json static int test_env_getenv() { if (test_env_val("OCCLUM", "yes") < 0) { - throw_error("get environment variable failed"); + THROW_ERROR("get environment variable failed"); } // Here we call getenv() again to make sure that // LibOS can handle several environment variables in Occlum.json correctly if (test_env_val("TEST", "true") < 0) { - throw_error("get environment variable failed"); + THROW_ERROR("get environment variable failed"); } return 0; } @@ -121,15 +121,15 @@ static int test_env_set_child_env_and_argv() { (char *const *)child_argv, (char *const *)child_envp); if (ret < 0) { - throw_error("spawn process error"); + THROW_ERROR("spawn process error"); } printf("Spawn a child process with pid=%d\n", child_pid); ret = wait4(-1, &status, 0, NULL); if (ret < 0) { - throw_error("failed to wait4 the child process"); + THROW_ERROR("failed to wait4 the child process"); } if (!WIFEXITED(status)) { - throw_error("test cases in child faild"); + THROW_ERROR("test cases in child faild"); } return 0; } @@ -142,11 +142,11 @@ static int test_env_child_getargv() { // Test argc if (g_argc != child_argc) { printf("ERROR: expect %d arguments, but %d are given\n", child_argc, g_argc); - throw_error("arguments count is not expected"); + THROW_ERROR("arguments count is not expected"); } // Test argv if (test_argv_val(child_argv) < 0) { - throw_error("argument variables are not expected"); + THROW_ERROR("argument variables are not expected"); } return 0; } @@ -162,10 +162,10 @@ static int test_env_child_getenv() { for (int i = 0; child_envp[i] != NULL; ++i) { int num = sscanf(child_envp[i], "%[^=]=%s", env_key, env_val); if (num != 2) { - throw_error("parse environment variable failed"); + THROW_ERROR("parse environment variable failed"); } if (test_env_val(env_key, env_val) < 0) { - throw_error("get environment variable failed"); + THROW_ERROR("get environment variable failed"); } } return 0; diff --git a/test/include/test.h b/test/include/test.h index 7fe53187..a04149c1 100644 --- a/test/include/test.h +++ b/test/include/test.h @@ -19,11 +19,11 @@ typedef struct { #define TEST_CASE(name) { STR(name), name } -#define throw_error(msg) while(1) { \ - printf("\t\tERROR: %s in func %s at line %d of file %s\n", \ - (msg), __func__, __LINE__, __FILE__); \ - return -1; \ -} +#define THROW_ERROR(msg) do { \ + printf("\t\tERROR: %s in func %s at line %d of file %s\n", \ + (msg), __func__, __LINE__, __FILE__); \ + return -1; \ +} while (0) int test_suite_run(test_case_t* test_cases, int num_test_cases) { for (int ti = 0; ti < num_test_cases; ti++) { diff --git a/test/ioctl/main.c b/test/ioctl/main.c index 326bd319..c4dfdef8 100644 --- a/test/ioctl/main.c +++ b/test/ioctl/main.c @@ -14,7 +14,7 @@ int test_tty_ioctl_TIOCGWINSZ(void) { struct winsize winsize; if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) < 0) { - throw_error("failed to ioctl TIOCGWINSZ"); + THROW_ERROR("failed to ioctl TIOCGWINSZ"); } return 0; } @@ -28,15 +28,15 @@ int test_tty_ioctl_TIOCGWINSZ(void) { int test_sgx_ioctl_SGXIOC_IS_EDDM_SUPPORTED(void) { int sgx_fd; if ((sgx_fd = open("/dev/sgx", O_RDONLY)) < 0) { - throw_error("failed to open /dev/sgx "); + THROW_ERROR("failed to open /dev/sgx "); } int is_edmm_supported = 0; if (ioctl(sgx_fd, SGXIOC_IS_EDDM_SUPPORTED, &is_edmm_supported) < 0) { - throw_error("failed to ioctl /dev/sgx"); + THROW_ERROR("failed to ioctl /dev/sgx"); } if (is_edmm_supported != 0) { - throw_error("SGX EDMM supported are not expected to be enabled"); + THROW_ERROR("SGX EDMM supported are not expected to be enabled"); } close(sgx_fd); diff --git a/test/mmap/main.c b/test/mmap/main.c index b46f7d72..82dc3456 100644 --- a/test/mmap/main.c +++ b/test/mmap/main.c @@ -35,7 +35,7 @@ static int fill_file_with_repeated_bytes(int fd, size_t len, int byte_val) { int to_write_bytes = MIN(sizeof(buf), remain_bytes); int written_bytes = write(fd, buf, to_write_bytes); if (written_bytes != to_write_bytes) { - throw_error("file write failed"); + THROW_ERROR("file write failed"); } remain_bytes -= written_bytes; } @@ -70,11 +70,11 @@ static int get_a_valid_range_of_hints(size_t *hint_begin, size_t *hint_end) { int flags = MAP_PRIVATE | MAP_ANONYMOUS; void* big_buf = mmap(NULL, big_buf_len, prot, flags, -1, 0); if (big_buf == MAP_FAILED) { - throw_error("mmap failed"); + THROW_ERROR("mmap failed"); } int ret = munmap(big_buf, big_buf_len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } *hint_begin = (size_t)big_buf; *hint_end = *hint_begin + big_buf_len; @@ -85,7 +85,7 @@ static size_t HINT_BEGIN, HINT_END; int test_suite_init() { if (get_a_valid_range_of_hints(&HINT_BEGIN, &HINT_END) < 0) { - throw_error("get_a_valid_range_of_hints failed"); + THROW_ERROR("get_a_valid_range_of_hints failed"); } return 0; } @@ -101,16 +101,16 @@ int test_anonymous_mmap() { for (size_t len = PAGE_SIZE; len <= MAX_MMAP_USED_MEMORY; len *= 2) { void* buf = mmap(NULL, len, prot, flags, -1, 0); if (buf == MAP_FAILED) { - throw_error("mmap failed"); + THROW_ERROR("mmap failed"); } if (check_bytes_in_buf(buf, len, 0) < 0) { - throw_error("the buffer is not initialized to zeros"); + THROW_ERROR("the buffer is not initialized to zeros"); } int ret = munmap(buf, len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } } return 0; @@ -138,7 +138,7 @@ int test_anonymous_mmap_randomly() { // Do mmap void* buf = mmap(NULL, len, prot, flags, -1, 0); if (buf == MAP_FAILED) { - throw_error("mmap failed"); + THROW_ERROR("mmap failed"); } bufs[num_bufs] = buf; lens[num_bufs] = len; @@ -153,7 +153,7 @@ int test_anonymous_mmap_randomly() { size_t len = lens[bi]; int ret = munmap(buf, len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } bufs[bi] = NULL; @@ -179,12 +179,12 @@ int test_anonymous_mmap_randomly_with_good_hints() { void* addr = mmap((void*)hint, len, prot, flags, -1, 0); if (addr != (void*)hint) { - throw_error("mmap with hint failed"); + THROW_ERROR("mmap with hint failed"); } int ret = munmap(addr, len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } } return 0; @@ -204,14 +204,14 @@ int test_anonymous_mmap_with_bad_hints() { void* bad_hint = (void*)bad_hints[hi]; void* addr = mmap(bad_hint, len, prot, flags, -1, 0); if (addr == MAP_FAILED) { - throw_error("mmap should have tolerated a bad hint"); + THROW_ERROR("mmap should have tolerated a bad hint"); } if (addr == bad_hint) { - throw_error("mmap should not have accepted a bad hint"); + THROW_ERROR("mmap should not have accepted a bad hint"); } int ret = munmap(addr, len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } } return 0; @@ -223,7 +223,7 @@ int test_anonymous_mmap_with_zero_len() { int len = 0; // invalid! void* buf = mmap(NULL, len, prot, flags, -1, 0); if (buf != MAP_FAILED) { - throw_error("mmap with zero len should have been failed"); + THROW_ERROR("mmap with zero len should have been failed"); } return 0; } @@ -234,17 +234,17 @@ int test_anonymous_mmap_with_non_page_aligned_len() { int flags = MAP_PRIVATE | MAP_ANONYMOUS; void* buf = mmap(NULL, len, prot, flags, -1, 0); if (buf == MAP_FAILED) { - throw_error("mmap with non-page aligned len should have worked"); + THROW_ERROR("mmap with non-page aligned len should have worked"); } // Even the length is not page aligned, the page mmaping is done in pages if (check_bytes_in_buf(buf, ALIGN_UP(len, PAGE_SIZE), 0) < 0) { - throw_error("the buffer is not initialized to zeros"); + THROW_ERROR("the buffer is not initialized to zeros"); } int ret = munmap(buf, len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } return 0; } @@ -257,7 +257,7 @@ int test_file_mmap() { const char* file_path = "/root/mmap_file.data"; int fd = open(file_path, O_CREAT | O_TRUNC | O_WRONLY, 0644); if (fd < 0) { - throw_error("file creation failed"); + THROW_ERROR("file creation failed"); } int file_len = 12 * KB + 128; int byte_val = 0xab; @@ -268,22 +268,22 @@ int test_file_mmap() { int flags = MAP_PRIVATE; fd = open(file_path, O_RDONLY); if (fd < 0) { - throw_error("file open failed"); + THROW_ERROR("file open failed"); } off_t offset = 0; for (size_t len = PAGE_SIZE; len <= file_len; len *= 2) { char* buf = mmap(NULL, len, prot, flags, fd, offset); if (buf == MAP_FAILED) { - throw_error("mmap failed"); + THROW_ERROR("mmap failed"); } if (check_bytes_in_buf(buf, len, byte_val) < 0) { - throw_error("the buffer is not initialized according to the file"); + THROW_ERROR("the buffer is not initialized according to the file"); } int ret = munmap(buf, len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } } close(fd); @@ -295,7 +295,7 @@ int test_file_mmap_with_offset() { const char* file_path = "/root/mmap_file.data"; int fd = open(file_path, O_CREAT | O_TRUNC | O_RDWR, 0644); if (fd < 0) { - throw_error("file creation failed"); + THROW_ERROR("file creation failed"); } size_t first_len = 4 * KB + 47; int first_val = 0xab; @@ -312,25 +312,25 @@ int test_file_mmap_with_offset() { assert(offset <= first_len); char* buf = mmap(NULL, len, prot, flags, fd, offset); if (buf == MAP_FAILED) { - throw_error("mmap failed"); + THROW_ERROR("mmap failed"); } char* buf_cursor = buf; if (check_bytes_in_buf(buf_cursor, first_len - offset, first_val) < 0) { - throw_error("the buffer is not initialized according to the file"); + THROW_ERROR("the buffer is not initialized according to the file"); } buf_cursor += first_len - offset; if (check_bytes_in_buf(buf_cursor, second_len, second_val) < 0) { - throw_error("the buffer is not initialized according to the file"); + THROW_ERROR("the buffer is not initialized according to the file"); } buf_cursor += second_len; if (check_bytes_in_buf(buf_cursor, ALIGN_UP(len, PAGE_SIZE) - (buf_cursor - buf), 0) < 0) { - throw_error("the remaining of the last page occupied by the buffer is not initialized to zeros"); + THROW_ERROR("the remaining of the last page occupied by the buffer is not initialized to zeros"); } int ret = munmap(buf, len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } close(fd); @@ -346,7 +346,7 @@ int test_file_mmap_with_invalid_fd() { off_t offset = 0; void* buf = mmap(NULL, len, prot, flags, fd, offset); if (buf != MAP_FAILED) { - throw_error("file mmap with an invalid fd should have been failed"); + THROW_ERROR("file mmap with an invalid fd should have been failed"); } return 0; } @@ -355,7 +355,7 @@ int test_file_mmap_with_non_page_aligned_offset() { const char* file_path = "/root/mmap_file.data"; int fd = open(file_path, O_CREAT | O_TRUNC | O_RDWR, 0644); if (fd < 0) { - throw_error("file creation failed"); + THROW_ERROR("file creation failed"); } int file_len = 12 * KB + 128; int byte_val = 0xab; @@ -367,7 +367,7 @@ int test_file_mmap_with_non_page_aligned_offset() { off_t offset = PAGE_SIZE + 127; // Invalid! void* buf = mmap(NULL, len, prot, flags, fd, offset); if (buf != MAP_FAILED) { - throw_error("mmap with non-page aligned len should have been failed"); + THROW_ERROR("mmap with non-page aligned len should have been failed"); } close(fd); @@ -390,12 +390,12 @@ int test_fixed_mmap_that_does_not_override_any_mmaping() { int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED; void* addr = mmap((void*)hint, len, prot, flags, -1, 0); if (addr != (void*)hint) { - throw_error("mmap with fixed address failed"); + THROW_ERROR("mmap with fixed address failed"); } int ret = munmap(addr, len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } return 0; @@ -414,7 +414,7 @@ int test_fixed_mmap_that_overrides_existing_mmaping() { int flags = MAP_PRIVATE | MAP_ANONYMOUS; void* parent_buf = mmap(NULL, parent_len, prot, flags, -1, 0); if (parent_buf == MAP_FAILED) { - throw_error("mmap for parent failed"); + THROW_ERROR("mmap for parent failed"); } int parent_val = 0xab; memset(parent_buf, parent_val, parent_len); @@ -422,25 +422,25 @@ int test_fixed_mmap_that_overrides_existing_mmaping() { // Allocate child_buf void* child_buf = (char*)parent_buf + pre_child_len; if (mmap(child_buf, child_len, prot, flags | MAP_FIXED, -1, 0) != child_buf) { - throw_error("mmap with fixed address failed"); + THROW_ERROR("mmap with fixed address failed"); } // Check that child_buf, which overrides parent_buf, is initialized to zeros if (check_bytes_in_buf(child_buf, child_len, 0) < 0) { - throw_error("the content of child mmap memory is not initialized"); + THROW_ERROR("the content of child mmap memory is not initialized"); } // Check that the rest of parent_buf are kept intact if (check_bytes_in_buf((char*)child_buf - pre_child_len, pre_child_len, parent_val) < 0 || check_bytes_in_buf((char*)child_buf + child_len, post_child_len, parent_val) < 0) { - throw_error("the content of parent mmap memory is broken"); + THROW_ERROR("the content of parent mmap memory is broken"); } // Deallocate parent_buf along with child_buf int ret = munmap(parent_buf, parent_len); if (ret < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } return 0; @@ -453,7 +453,7 @@ int test_fixed_mmap_with_non_page_aligned_addr() { int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED; void* addr = mmap((void*)hint, len, prot, flags, -1, 0); if (addr != MAP_FAILED) { - throw_error("fixed mmap with non-page aligned hint should have failed"); + THROW_ERROR("fixed mmap with non-page aligned hint should have failed"); } return 0; } @@ -472,7 +472,7 @@ static int check_buf_is_munmapped(void* target_addr, size_t len) { int flags = MAP_PRIVATE | MAP_ANONYMOUS; void* real_addr = mmap(target_addr, len, prot, flags, -1, 0); if (real_addr != target_addr) { - throw_error("address is already mmaped"); + THROW_ERROR("address is already mmaped"); } munmap(target_addr, len); return 0; @@ -484,21 +484,21 @@ static int mmap_then_munmap(size_t mmap_len, ssize_t munmap_offset, size_t munma // Make sure that we are manipulating memory between [HINT_BEGIN, HINT_END) void* mmap_addr = (void*)(munmap_offset >= 0 ? HINT_BEGIN : HINT_BEGIN - munmap_offset); if (mmap(mmap_addr, mmap_len, prot, flags, -1, 0) != mmap_addr) { - throw_error("mmap failed"); + THROW_ERROR("mmap failed"); } void* munmap_addr = (char*)mmap_addr + munmap_offset; if (munmap(munmap_addr, munmap_len) < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } if (check_buf_is_munmapped(munmap_addr, munmap_len) < 0) { - throw_error("munmap does not really free the memory"); + THROW_ERROR("munmap does not really free the memory"); } // Make sure that when this function returns, there are no memory mappings // within [HINT_BEGIN, HINT_END) if (munmap((void*)HINT_BEGIN, HINT_END - HINT_BEGIN) < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } return 0; } @@ -508,7 +508,7 @@ int test_munmap_whose_range_is_a_subset_of_a_mmap_region() { ssize_t munmap_offset = 1 * PAGE_SIZE; size_t munmap_len = 2 * PAGE_SIZE; if (mmap_then_munmap(mmap_len, munmap_offset, munmap_len) < 0) { - throw_error("first mmap and then munmap failed"); + THROW_ERROR("first mmap and then munmap failed"); } return 0; } @@ -518,7 +518,7 @@ int test_munmap_whose_range_is_a_superset_of_a_mmap_region() { ssize_t munmap_offset = -2 * PAGE_SIZE; size_t munmap_len = 7 * PAGE_SIZE; if (mmap_then_munmap(mmap_len, munmap_offset, munmap_len) < 0) { - throw_error("first mmap and then munmap failed"); + THROW_ERROR("first mmap and then munmap failed"); } return 0; } @@ -528,7 +528,7 @@ int test_munmap_whose_range_intersects_with_a_mmap_region() { size_t munmap_offset = 100 * PAGE_SIZE + 10 * PAGE_SIZE; size_t munmap_len = 4 * PAGE_SIZE; if (mmap_then_munmap(mmap_len, munmap_offset, munmap_len) < 0) { - throw_error("first mmap and then munmap failed"); + THROW_ERROR("first mmap and then munmap failed"); } return 0; } @@ -538,7 +538,7 @@ int test_munmap_whose_range_intersects_with_no_mmap_regions() { size_t munmap_offset = 1 * PAGE_SIZE; size_t munmap_len = 1 * PAGE_SIZE; if (mmap_then_munmap(mmap_len, munmap_offset, munmap_len) < 0) { - throw_error("first mmap and then munmap failed"); + THROW_ERROR("first mmap and then munmap failed"); } return 0; } @@ -550,13 +550,13 @@ int test_munmap_whose_range_intersects_with_multiple_mmap_regions() { size_t mmap_len1 = 100 * PAGE_SIZE; void* mmap_addr1 = mmap(NULL, mmap_len1, prot, flags, -1, 0); if (mmap_addr1 == MAP_FAILED) { - throw_error("mmap failed"); + THROW_ERROR("mmap failed"); } size_t mmap_len2 = 12 * PAGE_SIZE; void* mmap_addr2 = mmap(NULL, mmap_len2, prot, flags, -1, 0); if (mmap_addr2 == MAP_FAILED) { - throw_error("mmap failed"); + THROW_ERROR("mmap failed"); } size_t mmap_min = MIN((size_t)mmap_addr1, (size_t)mmap_addr2); @@ -566,10 +566,10 @@ int test_munmap_whose_range_intersects_with_multiple_mmap_regions() { void* munmap_addr = (void*)mmap_min; size_t munmap_len = mmap_max - mmap_min; if (munmap(munmap_addr, munmap_len) < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } if (check_buf_is_munmapped(munmap_addr, munmap_len) < 0) { - throw_error("munmap does not really free the memory"); + THROW_ERROR("munmap does not really free the memory"); } return 0; @@ -584,7 +584,7 @@ int test_munmap_with_null_addr() { void* munmap_addr = NULL; size_t munmap_len = PAGE_SIZE; if (munmap(munmap_addr, munmap_len) < 0) { - throw_error("munmap failed"); + THROW_ERROR("munmap failed"); } return 0; } @@ -594,7 +594,7 @@ int test_munmap_with_zero_len() { // Set the length for munmap to 0! This is invalid! size_t munmap_len = 0; if (munmap(munmap_addr, munmap_len) == 0) { - throw_error("munmap with zero length should have failed"); + THROW_ERROR("munmap with zero length should have failed"); } return 0; } @@ -610,7 +610,7 @@ int test_munmap_with_non_page_aligned_len() { // not considered as an error! size_t munmap_len = 1 * PAGE_SIZE + 123; if (mmap_then_munmap(mmap_len, munmap_offset, munmap_len) < 0) { - throw_error("first mmap and then munmap failed"); + THROW_ERROR("first mmap and then munmap failed"); } return 0; } @@ -645,7 +645,7 @@ static test_case_t test_cases[] = { int main() { if (test_suite_init() < 0) { - throw_error("test_suite_init failed"); + THROW_ERROR("test_suite_init failed"); } return test_suite_run(test_cases, ARRAY_SIZE(test_cases)); diff --git a/test/sched/main.c b/test/sched/main.c index 89d7312d..16f50a0e 100644 --- a/test/sched/main.c +++ b/test/sched/main.c @@ -18,13 +18,13 @@ static int test_sched_getaffinity_with_self_pid() { cpu_set_t mask; if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) < 0) { - throw_error("failed to call sched_getaffinity"); + THROW_ERROR("failed to call sched_getaffinity"); } if (CPU_COUNT(&mask) <= 0) { - throw_error("failed to get cpuset mask"); + THROW_ERROR("failed to get cpuset mask"); } if (sysconf(_SC_NPROCESSORS_ONLN) != CPU_COUNT(&mask)) { - throw_error("cpuset num wrong"); + THROW_ERROR("cpuset num wrong"); } return 0; } @@ -39,17 +39,17 @@ static int test_sched_setaffinity_with_self_pid() { CPU_ZERO(&mask); CPU_SET(0, &mask); if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) < 0) { - throw_error("failed to call sched_setaffinity \n"); + THROW_ERROR("failed to call sched_setaffinity \n"); } cpu_set_t mask2; if (sched_getaffinity(0, sizeof(cpu_set_t), &mask2) < 0) { - throw_error("failed to call sched_getaffinity"); + THROW_ERROR("failed to call sched_getaffinity"); } if (!CPU_EQUAL(&mask, &mask2)) { - throw_error("cpuset is wrong after get"); + THROW_ERROR("cpuset is wrong after get"); } if (sched_setaffinity(0, sizeof(cpu_set_t), &mask_old) < 0) { - throw_error("recover cpuset error"); + THROW_ERROR("recover cpuset error"); } return 0; } @@ -58,29 +58,29 @@ static int test_sched_xetaffinity_with_child_pid() { int status, child_pid; int num = sysconf(_SC_NPROCESSORS_CONF); if (num <= 0) { - throw_error("failed to get cpu number"); + THROW_ERROR("failed to get cpu number"); } cpu_set_t mask; CPU_ZERO(&mask); CPU_SET(num - 1 , &mask); int ret = posix_spawn(&child_pid, "/bin/getpid", NULL, NULL, NULL, NULL); if (ret < 0 ) { - throw_error("spawn process error"); + THROW_ERROR("spawn process error"); } printf("Spawn a child process with pid=%d\n", child_pid); if (sched_setaffinity(child_pid, sizeof(cpu_set_t), &mask) < 0) { - throw_error("failed to set child affinity"); + THROW_ERROR("failed to set child affinity"); } cpu_set_t mask2; if (sched_getaffinity(child_pid, sizeof(cpu_set_t), &mask2) < 0) { - throw_error("failed to get child affinity"); + THROW_ERROR("failed to get child affinity"); } if (!CPU_EQUAL(&mask, &mask2)) { - throw_error("cpuset is wrong in child"); + THROW_ERROR("cpuset is wrong in child"); } ret = wait4(-1, &status, 0, NULL); if (ret < 0) { - throw_error("failed to wait4 the child proces"); + THROW_ERROR("failed to wait4 the child proces"); } return 0; } @@ -91,7 +91,7 @@ static int test_sched_getaffinity_via_explicit_syscall() { unsigned char buf[CPU_SET_SIZE_LIMIT] = { 0 }; int ret = syscall(__NR_sched_getaffinity, 0, CPU_SET_SIZE_LIMIT, buf); if (ret <= 0) { - throw_error("failed to call __NR_sched_getaffinity"); + THROW_ERROR("failed to call __NR_sched_getaffinity"); } return 0; } @@ -106,18 +106,18 @@ static int test_sched_setaffinity_via_explicit_syscall() { CPU_ZERO(&mask); CPU_SET(0, &mask); if (syscall(__NR_sched_setaffinity, 0, sizeof(cpu_set_t), &mask) < 0) { - throw_error("failed to call __NR_sched_setaffinity"); + THROW_ERROR("failed to call __NR_sched_setaffinity"); } cpu_set_t mask2; int ret_nproc = syscall(__NR_sched_getaffinity, 0, sizeof(cpu_set_t), &mask2); if (ret_nproc <= 0) { - throw_error("failed to call __NR_sched_getaffinity"); + THROW_ERROR("failed to call __NR_sched_getaffinity"); } if (!CPU_EQUAL(&mask, &mask2)) { - throw_error("explicit syscall cpuset is wrong"); + THROW_ERROR("explicit syscall cpuset is wrong"); } if (syscall(__NR_sched_setaffinity, 0, sizeof(cpu_set_t), &mask_old) < 0) { - throw_error("recover cpuset error"); + THROW_ERROR("recover cpuset error"); } return 0; } @@ -125,7 +125,7 @@ static int test_sched_setaffinity_via_explicit_syscall() { static int test_sched_getaffinity_with_zero_cpusetsize() { cpu_set_t mask; if (sched_getaffinity(0, 0, &mask) != -1) { - throw_error("check invalid cpusetsize(0) fail"); + THROW_ERROR("check invalid cpusetsize(0) fail"); } return 0; } @@ -133,7 +133,7 @@ static int test_sched_getaffinity_with_zero_cpusetsize() { static int test_sched_setaffinity_with_zero_cpusetsize() { cpu_set_t mask; if (sched_setaffinity(0, 0, &mask) != -1) { - throw_error("check invalid cpusetsize(0) fail"); + THROW_ERROR("check invalid cpusetsize(0) fail"); } return 0; } @@ -141,7 +141,7 @@ static int test_sched_setaffinity_with_zero_cpusetsize() { static int test_sched_getaffinity_with_null_buffer() { unsigned char *buf = NULL; if (sched_getaffinity(0, sizeof(cpu_set_t), (cpu_set_t*)buf) != -1) { - throw_error("check invalid buffer pointer(NULL) fail"); + THROW_ERROR("check invalid buffer pointer(NULL) fail"); } return 0; } @@ -149,7 +149,7 @@ static int test_sched_getaffinity_with_null_buffer() { static int test_sched_setaffinity_with_null_buffer() { unsigned char *buf = NULL; if (sched_setaffinity(0, sizeof(cpu_set_t), (cpu_set_t*)buf) != -1) { - throw_error("check invalid buffer pointer(NULL) fail"); + THROW_ERROR("check invalid buffer pointer(NULL) fail"); } return 0; } diff --git a/test/sleep/main.c b/test/sleep/main.c index cd025702..7de13d82 100644 --- a/test/sleep/main.c +++ b/test/sleep/main.c @@ -84,7 +84,7 @@ static int check_nanosleep(struct timespec* expected_sleep_period) { clock_gettime(CLOCK_MONOTONIC, &begin_timestamp); if (nanosleep(expected_sleep_period, NULL) != 0) { - throw_error("nanosleep failed"); + THROW_ERROR("nanosleep failed"); } clock_gettime(CLOCK_MONOTONIC, &end_timestamp); @@ -120,7 +120,7 @@ int test_nanosleep_10ms() { int test_nanosleep_with_null_req() { if (nanosleep(NULL, NULL) != -1 && errno != EINVAL) { - throw_error("nanosleep should report error"); + THROW_ERROR("nanosleep should report error"); } return 0; } @@ -129,7 +129,7 @@ int test_nanosleep_with_negative_tv_sec() { // nanosleep returns EINVAL if the value in the tv_sec field is negative struct timespec invalid_period = { .tv_sec = -1, .tv_nsec = 0}; if (nanosleep(&invalid_period, NULL) != -1 && errno != EINVAL) { - throw_error("nanosleep should report EINVAL error"); + THROW_ERROR("nanosleep should report EINVAL error"); } return 0; } @@ -139,7 +139,7 @@ int test_nanosleep_with_negative_tv_nsec() { // was not in the range 0 to 999999999. struct timespec invalid_period = { .tv_sec = 0, .tv_nsec = -1}; if (nanosleep(&invalid_period, NULL) != -1 && errno != EINVAL) { - throw_error("nanosleep should report EINVAL error"); + THROW_ERROR("nanosleep should report EINVAL error"); } return 0; } @@ -149,7 +149,7 @@ int test_nanosleep_with_too_large_tv_nsec() { // was not in the range 0 to 999999999 (10^6 - 1). struct timespec invalid_period = { .tv_sec = 0, .tv_nsec = S}; if (nanosleep(&invalid_period, NULL) != -1 && errno != EINVAL) { - throw_error("nanosleep should report EINVAL error"); + THROW_ERROR("nanosleep should report EINVAL error"); } return 0; } diff --git a/test/time/main.c b/test/time/main.c index f21c8902..d7e1c53a 100644 --- a/test/time/main.c +++ b/test/time/main.c @@ -9,7 +9,7 @@ int test_gettimeofday() { struct timeval tv; if (gettimeofday(&tv, NULL)) { - throw_error("gettimeofday failed"); + THROW_ERROR("gettimeofday failed"); } return 0; } @@ -21,10 +21,10 @@ int test_gettimeofday() { int test_clock_gettime() { struct timespec ts; if (clock_gettime(CLOCK_REALTIME, &ts)) { - throw_error("clock_gettime(CLOCK_REALTIME, ...) failed"); + THROW_ERROR("clock_gettime(CLOCK_REALTIME, ...) failed"); } if (clock_gettime(CLOCK_MONOTONIC, &ts)) { - throw_error("clock_gettime(CLOCK_MONOTONIC, ...) failed"); + THROW_ERROR("clock_gettime(CLOCK_MONOTONIC, ...) failed"); } return 0; } diff --git a/test/unix_socket/main.c b/test/unix_socket/main.c index 07e25ecb..9c76bec3 100644 --- a/test/unix_socket/main.c +++ b/test/unix_socket/main.c @@ -12,11 +12,6 @@ #include "test.h" #define ECHO_MSG "echo msg for unix_socket test" -#define THROW_ERROR(msg) do { \ - printf("\t\tERROR: %s in func %s at line %d of file %s\n", \ - (msg), __func__, __LINE__, __FILE__); \ - return -1; \ -} while(0) int create_connected_sockets(int *sockets, char *sock_path) { int listen_fd = socket(AF_UNIX, SOCK_STREAM, 0);