File: file_unittest.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (162 lines) | stat: -rw-r--r-- 6,047 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string_view>

#include "base/containers/span.h"
#include "base/files/scoped_temp_dir.h"
#include "base/sync_socket.h"
#include "build/build_config.h"
#include "mojo/buildflags.h"
#include "mojo/public/cpp/base/file_mojom_traits.h"
#include "mojo/public/cpp/base/read_only_file_mojom_traits.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "mojo/public/mojom/base/file.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo_base {
namespace file_unittest {

TEST(FileTest, File) {
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  base::File file(
      temp_dir.GetPath().AppendASCII("test_file.txt"),
      base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ);
  const std::string_view test_content =
      "A test string to be stored in a test file";
  file.WriteAtCurrentPos(base::as_byte_span(test_content));

  base::File file_out;
  ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::File>(file, file_out));
  ASSERT_TRUE(file_out.IsValid());
  ASSERT_FALSE(file_out.async());

  std::string content(test_content.size(), '\0');
  ASSERT_TRUE(file_out.ReadAndCheck(0, base::as_writable_byte_span(content)));
  EXPECT_EQ(test_content, content);
}

TEST(FileTest, AsyncFile) {
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  base::FilePath path = temp_dir.GetPath().AppendASCII("async_test_file.txt");

  base::File write_file(path, base::File::FLAG_CREATE | base::File::FLAG_WRITE);
  const std::string_view test_content = "test string";
  write_file.WriteAtCurrentPos(base::as_byte_span(test_content));
  write_file.Close();

  base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ |
                            base::File::FLAG_ASYNC);
  base::File file_out;
  ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::File>(file, file_out));
  ASSERT_TRUE(file_out.async());
}

TEST(FileTest, InvalidFile) {
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  // Test that |file_out| is set to an invalid file.
  base::File file_out(
      temp_dir.GetPath().AppendASCII("test_file.txt"),
      base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ);

  base::File file = base::File();
  ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::File>(file, file_out));
  EXPECT_FALSE(file_out.IsValid());
}

TEST(FileTest, ReadOnlyFile) {
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  base::File file(
      temp_dir.GetPath().AppendASCII("test_file.txt"),
      base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ);
  const std::string_view test_content =
      "A test string to be stored in a test file";
  file.WriteAtCurrentPos(base::as_byte_span(test_content));
  file.Close();

  base::File readonly(temp_dir.GetPath().AppendASCII("test_file.txt"),
                      base::File::FLAG_OPEN | base::File::FLAG_READ);

  base::File file_out;
  ASSERT_TRUE(mojo::test::SerializeAndDeserialize<mojom::ReadOnlyFile>(
      readonly, file_out));
  ASSERT_TRUE(file_out.IsValid());
  ASSERT_FALSE(file_out.async());

  std::string content(test_content.size(), '\0');
  ASSERT_TRUE(file_out.ReadAndCheck(0, base::as_writable_byte_span(content)));
  EXPECT_EQ(test_content, content);
}

// This dies only if we can interrogate the underlying platform handle.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if !BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_AIX)
TEST(FileTest, ReadOnlyFileDeath) {
#if defined(OFFICIAL_BUILD)
  const char kReadOnlyFileCheckFailedRegex[] = "";
#else
  const char kReadOnlyFileCheckFailedRegex[] = "Check failed: IsReadOnlyFile";
#endif

  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());

  base::File file(
      temp_dir.GetPath().AppendASCII("test_file.txt"),
      base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ);
  const std::string_view test_content =
      "A test string to be stored in a test file";
  file.WriteAtCurrentPos(base::as_byte_span(test_content));
  file.Close();

  base::File writable(
      temp_dir.GetPath().AppendASCII("test_file.txt"),
      base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE);

  base::File file_out;
  EXPECT_DEATH_IF_SUPPORTED(
      mojo::test::SerializeAndDeserialize<mojom::ReadOnlyFile>(writable,
                                                               file_out),
      kReadOnlyFileCheckFailedRegex);
}
#endif  // !BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_AIX)
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)

// This should work on all platforms. This check might be relaxed in which case
// this test can be removed. iOS without blink does not build SyncSocket, so do
// not build this when blink isn't used.
#if DCHECK_IS_ON() && (!BUILDFLAG(IS_IOS) || BUILDFLAG(MOJO_USE_APPLE_CHANNEL))
TEST(FileTest, NonPhysicalFileDeath) {
#if defined(OFFICIAL_BUILD)
  const char kPhysicalFileCheckFailedRegex[] = "";
#else
  const char kPhysicalFileCheckFailedRegex[] = "Check failed: IsPhysicalFile";
#endif

  base::SyncSocket sync_a;
  base::SyncSocket sync_b;
  ASSERT_TRUE(base::SyncSocket::CreatePair(&sync_a, &sync_b));
  base::File file_pipe_a(sync_a.Take());
  base::File file_pipe_b(sync_b.Take());

  base::File file_out;
  EXPECT_DEATH_IF_SUPPORTED(
      mojo::test::SerializeAndDeserialize<mojom::ReadOnlyFile>(file_pipe_a,
                                                               file_out),
      kPhysicalFileCheckFailedRegex);
  EXPECT_DEATH_IF_SUPPORTED(
      mojo::test::SerializeAndDeserialize<mojom::ReadOnlyFile>(file_pipe_b,
                                                               file_out),
      kPhysicalFileCheckFailedRegex);
}
#endif  // DCHECK_IS_ON()

}  // namespace file_unittest
}  // namespace mojo_base