1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "os/files.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <filesystem>
23 
24 namespace testing {
25 
26 using bluetooth::os::FileExists;
27 using bluetooth::os::ReadSmallFile;
28 using bluetooth::os::RenameFile;
29 using bluetooth::os::WriteToFile;
30 
TEST(FilesTest,exist_test)31 TEST(FilesTest, exist_test) {
32   auto temp_dir = std::filesystem::temp_directory_path();
33   auto temp_file = temp_dir / "file_1.txt";
34   std::string text = "Hello world!\n";
35   ASSERT_TRUE(WriteToFile(temp_file.string(), text));
36   EXPECT_TRUE(FileExists(temp_file.string()));
37   auto none_file = temp_dir / "file_nope.txt";
38   EXPECT_FALSE(FileExists(none_file.string()));
39 }
40 
TEST(FilesTest,rename_test)41 TEST(FilesTest, rename_test) {
42   auto temp_dir = std::filesystem::temp_directory_path();
43   auto temp_file = temp_dir / "file_1.txt";
44   std::string text = "Hello world!\n";
45   ASSERT_TRUE(WriteToFile(temp_file.string(), text));
46   EXPECT_THAT(ReadSmallFile(temp_file.string()), Optional(StrEq(text)));
47   auto to_file = temp_dir / "file_2.txt";
48   ASSERT_TRUE(RenameFile(temp_file.string(), to_file.string()));
49   EXPECT_FALSE(std::filesystem::exists(temp_file));
50   EXPECT_THAT(ReadSmallFile(to_file.string()), Optional(StrEq(text)));
51   // rename files that do not exist should return false
52   ASSERT_FALSE(RenameFile(temp_file.string(), to_file.string()));
53 }
54 
TEST(FilesTest,write_read_loopback_test)55 TEST(FilesTest, write_read_loopback_test) {
56   auto temp_dir = std::filesystem::temp_directory_path();
57   auto temp_file = temp_dir / "file_1.txt";
58   std::string text = "Hello world!\n";
59   ASSERT_TRUE(WriteToFile(temp_file.string(), text));
60   EXPECT_THAT(ReadSmallFile(temp_file.string()), Optional(StrEq(text)));
61   EXPECT_TRUE(std::filesystem::remove(temp_file));
62 }
63 
TEST(FilesTest,overwrite_test)64 TEST(FilesTest, overwrite_test) {
65   auto temp_dir = std::filesystem::temp_directory_path();
66   auto temp_file = temp_dir / "file_1.txt";
67   std::string text = "Hello world!\n";
68   ASSERT_TRUE(WriteToFile(temp_file.string(), text));
69   EXPECT_THAT(ReadSmallFile(temp_file.string()), Optional(StrEq(text)));
70   text = "Foo bar!\n";
71   ASSERT_TRUE(WriteToFile(temp_file.string(), text));
72   EXPECT_THAT(ReadSmallFile(temp_file.string()), Optional(StrEq(text)));
73   EXPECT_TRUE(std::filesystem::remove(temp_file));
74 }
75 
TEST(FilesTest,write_read_empty_string_test)76 TEST(FilesTest, write_read_empty_string_test) {
77   auto temp_dir = std::filesystem::temp_directory_path();
78   auto temp_file = temp_dir / "file_1.txt";
79   std::string text;
80   ASSERT_TRUE(WriteToFile(temp_file.string(), text));
81   EXPECT_THAT(ReadSmallFile(temp_file.string()), Optional(StrEq(text)));
82   EXPECT_TRUE(std::filesystem::remove(temp_file));
83 }
84 
TEST(FilesTest,read_non_existing_file_test)85 TEST(FilesTest, read_non_existing_file_test) {
86   EXPECT_FALSE(ReadSmallFile("/woof"));
87 }
88 
89 }  // namespace testing