• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium 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 "base/files/scoped_temp_dir.h"
6 #include "mojo/public/cpp/base/file_mojom_traits.h"
7 #include "mojo/public/cpp/test_support/test_utils.h"
8 #include "mojo/public/mojom/base/file.mojom.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace mojo_base {
12 namespace file_unittest {
13 
TEST(FileTest,File)14 TEST(FileTest, File) {
15   base::ScopedTempDir temp_dir;
16   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
17 
18   base::File file(
19       temp_dir.GetPath().AppendASCII("test_file.txt"),
20       base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ);
21   const base::StringPiece test_content =
22       "A test string to be stored in a test file";
23   file.WriteAtCurrentPos(test_content.data(),
24                          base::checked_cast<int>(test_content.size()));
25 
26   base::File file_out;
27   ASSERT_TRUE(
28       mojo::test::SerializeAndDeserialize<mojom::File>(&file, &file_out));
29   std::vector<char> content(test_content.size());
30   ASSERT_TRUE(file_out.IsValid());
31   ASSERT_FALSE(file_out.async());
32   ASSERT_EQ(static_cast<int>(test_content.size()),
33             file_out.Read(0, content.data(),
34                           base::checked_cast<int>(test_content.size())));
35   EXPECT_EQ(test_content,
36             base::StringPiece(content.data(), test_content.size()));
37 }
38 
TEST(FileTest,AsyncFile)39 TEST(FileTest, AsyncFile) {
40   base::ScopedTempDir temp_dir;
41   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
42   base::FilePath path = temp_dir.GetPath().AppendASCII("async_test_file.txt");
43 
44   base::File write_file(path, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
45   const base::StringPiece test_content = "test string";
46   write_file.WriteAtCurrentPos(test_content.data(),
47                                base::checked_cast<int>(test_content.size()));
48   write_file.Close();
49 
50   base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ |
51                             base::File::FLAG_ASYNC);
52   base::File file_out;
53   ASSERT_TRUE(
54       mojo::test::SerializeAndDeserialize<mojom::File>(&file, &file_out));
55   ASSERT_TRUE(file_out.async());
56 }
57 
TEST(FileTest,InvalidFile)58 TEST(FileTest, InvalidFile) {
59   base::ScopedTempDir temp_dir;
60   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
61   // Test that |file_out| is set to an invalid file.
62   base::File file_out(
63       temp_dir.GetPath().AppendASCII("test_file.txt"),
64       base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ);
65 
66   base::File file = base::File();
67   ASSERT_TRUE(
68       mojo::test::SerializeAndDeserialize<mojom::File>(&file, &file_out));
69   EXPECT_FALSE(file_out.IsValid());
70 }
71 
72 }  // namespace file_unittest
73 }  // namespace mojo_base
74