// 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { bool test_vmofile_basic() { BEGIN_TEST; async::Loop loop(&kAsyncLoopConfigNoAttachToThread); ASSERT_EQ(loop.StartThread(), ZX_OK); async_dispatcher_t* dispatcher = loop.dispatcher(); zx::channel client, server; ASSERT_EQ(zx::channel::create(0, &client, &server), ZX_OK); memfs::Vfs vfs; vfs.SetDispatcher(dispatcher); fbl::RefPtr root; ASSERT_EQ(memfs::CreateFilesystem("", &vfs, &root), ZX_OK); zx::vmo backing_vmo; ASSERT_EQ(zx::vmo::create(64, 0, &backing_vmo), ZX_OK); ASSERT_EQ(backing_vmo.write("hello, world!", 0, 13), ZX_OK); ASSERT_EQ(vfs.CreateFromVmo(root.get(), "greeting", backing_vmo.get(), 0, 13), ZX_OK); ASSERT_EQ(vfs.ServeDirectory(std::move(root), std::move(server)), ZX_OK); zx::channel h, request; ASSERT_EQ(zx::channel::create(0, &h, &request), ZX_OK); ASSERT_EQ(fuchsia_io_DirectoryOpen(client.get(), ZX_FS_RIGHT_READABLE, 0, "greeting", 8, request.release()), ZX_OK); fuchsia_io_NodeInfo info = {}; ASSERT_EQ(fuchsia_io_FileDescribe(h.get(), &info), ZX_OK); ASSERT_EQ(info.tag, fuchsia_io_NodeInfoTag_vmofile); ASSERT_EQ(info.vmofile.offset, 0u); ASSERT_EQ(info.vmofile.length, 13u); zx_handle_close(info.vmofile.vmo); zx_status_t status = ZX_OK; uint64_t seek = 0u; ASSERT_EQ(fuchsia_io_FileSeek(h.get(), 7u, fuchsia_io_SeekOrigin_START, &status, &seek), ZX_OK); ASSERT_EQ(status, ZX_OK); ASSERT_EQ(seek, 7u); memset(&info, 0, sizeof(info)); ASSERT_EQ(fuchsia_io_FileDescribe(h.get(), &info), ZX_OK); ASSERT_EQ(info.tag, fuchsia_io_NodeInfoTag_vmofile); ASSERT_EQ(info.vmofile.offset, 0u); ASSERT_EQ(info.vmofile.length, 13u); zx_handle_close(info.vmofile.vmo); h.reset(); vfs.Shutdown([](zx_status_t status) { EXPECT_EQ(status, ZX_OK); }); loop.Shutdown(); END_TEST; } } // namespace BEGIN_TEST_CASE(vmofile_tests) RUN_TEST(test_vmofile_basic) END_TEST_CASE(vmofile_tests)