| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include <zircon/syscalls.h> |
| #include <zircon/syscalls/object.h> |
| #include <fbl/vector.h> |
| #include <lib/zx/event.h> |
| #include <zxtest/zxtest.h> |
| |
| namespace { |
| |
| constexpr uint32_t kEventOption = 0u; |
| |
| TEST(HandleDup, ReplaceSuccessOrigInvalid) { |
| zx::event orig_event; |
| ASSERT_OK(zx::event::create(kEventOption, &orig_event)); |
| |
| zx::event replaced_event; |
| ASSERT_OK(orig_event.replace(ZX_RIGHTS_BASIC, &replaced_event)); |
| EXPECT_FALSE(orig_event.is_valid()); |
| EXPECT_TRUE(replaced_event.is_valid()); |
| } |
| |
| TEST(HandleDup, ReplaceFailureBothInvalid) { |
| zx::event orig_event; |
| ASSERT_OK(zx::event::create(kEventOption, &orig_event)); |
| |
| zx::event failed_event; |
| EXPECT_EQ(orig_event.replace(ZX_RIGHT_EXECUTE, &failed_event), ZX_ERR_INVALID_ARGS); |
| // Even on failure, a replaced object is now invalid. |
| EXPECT_FALSE(orig_event.is_valid()); |
| EXPECT_FALSE(failed_event.is_valid()); |
| } |
| |
| TEST(HandleDup, Replace) { |
| // Call handle_replace with an invalid destination slot. This will cause the handle to get |
| // duplicated in the kernel, but then have to get deleted at the point the copy-out happens. |
| zx::event event; |
| |
| ASSERT_OK(zx::event::create(kEventOption, &event)); |
| |
| // This should fail and not cause the kernel to panic. |
| ASSERT_STATUS(ZX_ERR_INVALID_ARGS, zx_handle_replace(event.get(), 0, nullptr)); |
| } |
| |
| TEST(HandleDup, Duplicate) { |
| // Same as above, but using the handle_duplicate to cause the dup to happen in the kernel. |
| zx::event event; |
| |
| ASSERT_OK(zx::event::create(kEventOption, &event)); |
| |
| // This should fail and not cause the kernel to panic. |
| ASSERT_STATUS(ZX_ERR_INVALID_ARGS, zx_handle_duplicate(event.get(), 0, nullptr)); |
| } |
| |
| } // namespace |