Flutter Engine
file_descriptor_unittest.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <fcntl.h>
6 #include <sys/types.h>
7 
8 #include <string>
9 #include <vector>
10 
11 #include "filesystem/scoped_temp_dir.h"
12 #include "filesystem/unique_fd.h"
13 #include "gtest/gtest.h"
14 
15 namespace filesystem {
16 
17 TEST(FileDescriptor, WriteAndRead) {
18  files::ScopedTempDir temp_dir;
19  std::string path;
20  ASSERT_TRUE(temp_dir.NewTempFile(&path));
21 
22  fxl::UniqueFD fd(open(path.c_str(), O_RDWR));
23  ASSERT_TRUE(fd.is_valid());
24 
25  std::string string = "one, two, three";
26  EXPECT_TRUE(WriteFileDescriptor(fd.get(), string.data(), string.size()));
27  EXPECT_EQ(0, lseek(fd.get(), 0, SEEK_SET));
28 
29  std::vector<char> buffer;
30  buffer.resize(1024);
31  ssize_t read = ReadFileDescriptor(fd.get(), buffer.data(), 1024);
32  EXPECT_EQ(static_cast<ssize_t>(string.size()), read);
33  EXPECT_EQ(string, buffer.data());
34 }
35 
36 } // namespace filesystem
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
UniqueObject< int, internal::os_unix::UniqueFDTraits > UniqueFD
Definition: unique_fd.h:61
TEST(Directory, CreateDirectory)