xref: /aosp_15_r20/external/cronet/base/fuchsia/file_utils_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
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/fuchsia/file_utils.h"
6 
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace base {
12 
13 class OpenDirectoryTest : public testing::Test {
14  protected:
SetUp()15   void SetUp() override {
16     EXPECT_TRUE(temp_dir.CreateUniqueTempDirUnderPath(
17         base::FilePath(kPersistedDataDirectoryPath)));
18   }
19 
20   ScopedTempDir temp_dir;
21 };
22 
TEST_F(OpenDirectoryTest,Open)23 TEST_F(OpenDirectoryTest, Open) {
24   auto dir = OpenDirectoryHandle(temp_dir.GetPath());
25   ASSERT_TRUE(dir);
26 }
27 
28 // OpenDirectoryHandle() should fail when opening a directory that doesn't
29 // exist.
TEST_F(OpenDirectoryTest,OpenNonExistent)30 TEST_F(OpenDirectoryTest, OpenNonExistent) {
31   auto dir =
32       OpenDirectoryHandle(temp_dir.GetPath().AppendASCII("non_existent"));
33   ASSERT_FALSE(dir);
34 }
35 
36 // OpenDirectoryHandle() should open only directories.
TEST_F(OpenDirectoryTest,OpenFile)37 TEST_F(OpenDirectoryTest, OpenFile) {
38   auto file_path = temp_dir.GetPath().AppendASCII("test_file");
39   ASSERT_TRUE(WriteFile(file_path, "foo"));
40   auto dir = OpenDirectoryHandle(file_path);
41   ASSERT_FALSE(dir);
42 }
43 
44 }  // namespace base
45