diff --git a/src/test/pmem2_source/TESTS.py b/src/test/pmem2_source/TESTS.py index 1d6f972bc6429852a35d9f6312701683789bc178..d4308dd95810ea5cb3ad30d02e24b66c01b8c4d5 100755 --- a/src/test/pmem2_source/TESTS.py +++ b/src/test/pmem2_source/TESTS.py @@ -94,3 +94,27 @@ class TEST11(PMEM2_SOURCE): def run(self, ctx): ctx.exec('pmem2_source', self.test_case, ctx.testdir) + + +@t.windows_only +class TEST12(PMEM2_SOURCE): + """get handle from the source""" + test_case = "test_get_handle" + + +@t.windows_exclude +class TEST13(PMEM2_SOURCE): + """get file descriptor from the source""" + test_case = "test_get_fd" + + +@t.windows_only +class TEST14(PMEM2_SOURCE_NO_DIR): + """get handle from the invalid source type""" + test_case = "test_get_handle_inval_type" + + +@t.windows_exclude +class TEST15(PMEM2_SOURCE_NO_DIR): + """get file descriptor from the invalid source type""" + test_case = "test_get_fd_inval_type" diff --git a/src/test/pmem2_source/pmem2_source.c b/src/test/pmem2_source/pmem2_source.c index 6e1b836f099378b3349a864268d316185439d43c..cfc40b272d00fd0666eeee1cff1560d10af225ad 100644 --- a/src/test/pmem2_source/pmem2_source.c +++ b/src/test/pmem2_source/pmem2_source.c @@ -293,6 +293,54 @@ test_set_mutex_handle(const struct test_case *tc, int argc, char *argv[]) return 0; } + +/* + * test_get_handle - test getting handle value + */ +static int +test_get_handle(const struct test_case *tc, int argc, char *argv[]) +{ + if (argc < 1) + UT_FATAL("usage: test_get_handle <file>"); + + char *file = argv[0]; + HANDLE h = CreateFile(file, GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_ALWAYS, 0, NULL); + UT_ASSERTne(h, INVALID_HANDLE_VALUE); + + struct pmem2_source *src; + int ret = pmem2_source_from_handle(&src, h); + UT_PMEM2_EXPECT_RETURN(ret, 0); + + HANDLE handle_from_pmem2; + ret = pmem2_source_get_handle(src, &handle_from_pmem2); + UT_ASSERTeq(handle_from_pmem2, h); + UT_PMEM2_EXPECT_RETURN(ret, 0); + + CloseHandle(h); + pmem2_source_delete(&src); + + return 1; +} + +/* + * test_get_handle_inval_type - test getting handle value from invalid type + */ +static int +test_get_handle_inval_type(const struct test_case *tc, int argc, char *argv[]) +{ + struct pmem2_source *src; + int ret = pmem2_source_from_anon(&src, 0); + UT_PMEM2_EXPECT_RETURN(ret, 0); + + HANDLE handle_from_pmem2; + ret = pmem2_source_get_handle(src, &handle_from_pmem2); + UT_PMEM2_EXPECT_RETURN(ret, PMEM2_E_FILE_HANDLE_NOT_SET); + + pmem2_source_delete(&src); + + return 0; +} #else /* * test_set_directory_handle - test setting directory's fd @@ -315,6 +363,53 @@ test_set_directory_fd(const struct test_case *tc, int argc, char *argv[]) return 1; } + +/* + * test_get_fd - test getting file descriptor value + */ +static int +test_get_fd(const struct test_case *tc, int argc, char *argv[]) +{ + if (argc < 1) + UT_FATAL("usage: test_get_fd <file>"); + + char *file = argv[0]; + int fd = OPEN(file, O_RDONLY); + UT_ASSERTne(fd, -1); + + struct pmem2_source *src; + int ret = pmem2_source_from_fd(&src, fd); + UT_PMEM2_EXPECT_RETURN(ret, 0); + + int fd_from_pmem2; + ret = pmem2_source_get_fd(src, &fd_from_pmem2); + UT_ASSERTeq(fd_from_pmem2, fd); + UT_PMEM2_EXPECT_RETURN(ret, 0); + + CLOSE(fd); + pmem2_source_delete(&src); + + return 1; +} + +/* + * test_get_fd_inval_type - test getting fd value from invalid type + */ +static int +test_get_fd_inval_type(const struct test_case *tc, int argc, char *argv[]) +{ + struct pmem2_source *src; + int ret = pmem2_source_from_anon(&src, 0); + UT_PMEM2_EXPECT_RETURN(ret, 0); + + int fd_from_pmem2; + ret = pmem2_source_get_fd(src, &fd_from_pmem2); + UT_PMEM2_EXPECT_RETURN(ret, PMEM2_E_FILE_DESCRIPTOR_NOT_SET); + + pmem2_source_delete(&src); + + return 0; +} #endif /* @@ -333,8 +428,12 @@ static struct test_case test_cases[] = { TEST_CASE(test_set_invalid_handle), TEST_CASE(test_set_directory_handle), TEST_CASE(test_set_mutex_handle), + TEST_CASE(test_get_handle), + TEST_CASE(test_get_handle_inval_type), #else TEST_CASE(test_set_directory_fd), + TEST_CASE(test_get_fd), + TEST_CASE(test_get_fd_inval_type), #endif };