| // Copyright 2018 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 "lib/component/cpp/testing/test_util.h" |
| |
| #include <lib/fdio/limits.h> |
| #include <lib/fdio/util.h> |
| |
| namespace component { |
| namespace testing { |
| |
| fuchsia::sys::FileDescriptorPtr CloneFileDescriptor(int fd) { |
| zx_handle_t handles[FDIO_MAX_HANDLES] = {0, 0, 0}; |
| uint32_t types[FDIO_MAX_HANDLES] = { |
| ZX_HANDLE_INVALID, |
| ZX_HANDLE_INVALID, |
| ZX_HANDLE_INVALID, |
| }; |
| zx_status_t status = fdio_clone_fd(fd, 0, handles, types); |
| if (status <= 0) |
| return nullptr; |
| fuchsia::sys::FileDescriptorPtr result = fuchsia::sys::FileDescriptor::New(); |
| result->type0 = types[0]; |
| result->handle0 = zx::handle(handles[0]); |
| result->type1 = types[1]; |
| result->handle1 = zx::handle(handles[1]); |
| result->type2 = types[2]; |
| result->handle2 = zx::handle(handles[2]); |
| return result; |
| } |
| |
| zx::channel OpenAsDirectory(fs::Vfs* vfs, fbl::RefPtr<fs::Vnode> node) { |
| zx::channel h1, h2; |
| if (zx::channel::create(0, &h1, &h2) != ZX_OK) |
| return zx::channel(); |
| if (vfs->ServeDirectory(std::move(node), std::move(h1)) != ZX_OK) |
| return zx::channel(); |
| return h2; |
| } |
| |
| } // namespace testing |
| } // namespace component |