xref: /aosp_15_r20/external/icing/icing/file/destructible-file_test.cc (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1*8b6cd535SAndroid Build Coastguard Worker // Copyright (C) 2021 Google LLC
2*8b6cd535SAndroid Build Coastguard Worker //
3*8b6cd535SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*8b6cd535SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*8b6cd535SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*8b6cd535SAndroid Build Coastguard Worker //
7*8b6cd535SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
8*8b6cd535SAndroid Build Coastguard Worker //
9*8b6cd535SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*8b6cd535SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*8b6cd535SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*8b6cd535SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*8b6cd535SAndroid Build Coastguard Worker // limitations under the License.
14*8b6cd535SAndroid Build Coastguard Worker 
15*8b6cd535SAndroid Build Coastguard Worker #include "icing/file/destructible-file.h"
16*8b6cd535SAndroid Build Coastguard Worker 
17*8b6cd535SAndroid Build Coastguard Worker #include "gmock/gmock.h"
18*8b6cd535SAndroid Build Coastguard Worker #include "gtest/gtest.h"
19*8b6cd535SAndroid Build Coastguard Worker #include "icing/file/filesystem.h"
20*8b6cd535SAndroid Build Coastguard Worker #include "icing/testing/tmp-directory.h"
21*8b6cd535SAndroid Build Coastguard Worker 
22*8b6cd535SAndroid Build Coastguard Worker namespace icing {
23*8b6cd535SAndroid Build Coastguard Worker namespace lib {
24*8b6cd535SAndroid Build Coastguard Worker 
25*8b6cd535SAndroid Build Coastguard Worker namespace {
26*8b6cd535SAndroid Build Coastguard Worker 
TEST(DestructibleFileTest,DeletesFileProperly)27*8b6cd535SAndroid Build Coastguard Worker TEST(DestructibleFileTest, DeletesFileProperly) {
28*8b6cd535SAndroid Build Coastguard Worker   Filesystem filesystem;
29*8b6cd535SAndroid Build Coastguard Worker   std::string filepath1 = GetTestTempDir() + "/file1";
30*8b6cd535SAndroid Build Coastguard Worker 
31*8b6cd535SAndroid Build Coastguard Worker   {
32*8b6cd535SAndroid Build Coastguard Worker     // 1. Create the file
33*8b6cd535SAndroid Build Coastguard Worker     ScopedFd sfd(filesystem.OpenForWrite(filepath1.c_str()));
34*8b6cd535SAndroid Build Coastguard Worker     ASSERT_TRUE(sfd.is_valid());
35*8b6cd535SAndroid Build Coastguard Worker     int i = 127;
36*8b6cd535SAndroid Build Coastguard Worker     ASSERT_TRUE(filesystem.Write(sfd.get(), &i, sizeof(i)));
37*8b6cd535SAndroid Build Coastguard Worker   }
38*8b6cd535SAndroid Build Coastguard Worker 
39*8b6cd535SAndroid Build Coastguard Worker   {
40*8b6cd535SAndroid Build Coastguard Worker     // 2. Open with a Destructible file.
41*8b6cd535SAndroid Build Coastguard Worker     DestructibleFile destructible(filepath1, &filesystem);
42*8b6cd535SAndroid Build Coastguard Worker     ASSERT_TRUE(destructible.is_valid());
43*8b6cd535SAndroid Build Coastguard Worker   }
44*8b6cd535SAndroid Build Coastguard Worker 
45*8b6cd535SAndroid Build Coastguard Worker   // 3. Ensure that the file doesn't exist.
46*8b6cd535SAndroid Build Coastguard Worker   EXPECT_FALSE(filesystem.FileExists(filepath1.c_str()));
47*8b6cd535SAndroid Build Coastguard Worker }
48*8b6cd535SAndroid Build Coastguard Worker 
TEST(DestructibleFileTest,MoveAssignDeletesFileProperly)49*8b6cd535SAndroid Build Coastguard Worker TEST(DestructibleFileTest, MoveAssignDeletesFileProperly) {
50*8b6cd535SAndroid Build Coastguard Worker   Filesystem filesystem;
51*8b6cd535SAndroid Build Coastguard Worker   std::string filepath1 = GetTestTempDir() + "/file1";
52*8b6cd535SAndroid Build Coastguard Worker   std::string filepath2 = GetTestTempDir() + "/file2";
53*8b6cd535SAndroid Build Coastguard Worker 
54*8b6cd535SAndroid Build Coastguard Worker   // 1. Create file1
55*8b6cd535SAndroid Build Coastguard Worker   DestructibleFile destructible1(filepath1, &filesystem);
56*8b6cd535SAndroid Build Coastguard Worker   ASSERT_TRUE(destructible1.is_valid());
57*8b6cd535SAndroid Build Coastguard Worker   int i = 127;
58*8b6cd535SAndroid Build Coastguard Worker   ASSERT_TRUE(filesystem.Write(destructible1.get_fd(), &i, sizeof(i)));
59*8b6cd535SAndroid Build Coastguard Worker 
60*8b6cd535SAndroid Build Coastguard Worker   {
61*8b6cd535SAndroid Build Coastguard Worker     // 2. Create file2
62*8b6cd535SAndroid Build Coastguard Worker     DestructibleFile destructible2(filepath2, &filesystem);
63*8b6cd535SAndroid Build Coastguard Worker     ASSERT_TRUE(destructible2.is_valid());
64*8b6cd535SAndroid Build Coastguard Worker     i = 458;
65*8b6cd535SAndroid Build Coastguard Worker     ASSERT_TRUE(filesystem.Write(destructible2.get_fd(), &i, sizeof(i)));
66*8b6cd535SAndroid Build Coastguard Worker 
67*8b6cd535SAndroid Build Coastguard Worker     // Move assign destructible2 into destructible1
68*8b6cd535SAndroid Build Coastguard Worker     destructible1 = std::move(destructible2);
69*8b6cd535SAndroid Build Coastguard Worker   }
70*8b6cd535SAndroid Build Coastguard Worker 
71*8b6cd535SAndroid Build Coastguard Worker   // 3. file1 shouldn't exist because it was destroyed when destructible1 was
72*8b6cd535SAndroid Build Coastguard Worker   // move assigned to.
73*8b6cd535SAndroid Build Coastguard Worker   EXPECT_FALSE(filesystem.FileExists(filepath1.c_str()));
74*8b6cd535SAndroid Build Coastguard Worker 
75*8b6cd535SAndroid Build Coastguard Worker   // 4. file2 should still exist because it moved into destructible1 from
76*8b6cd535SAndroid Build Coastguard Worker   // destructible2.
77*8b6cd535SAndroid Build Coastguard Worker   EXPECT_TRUE(filesystem.FileExists(filepath2.c_str()));
78*8b6cd535SAndroid Build Coastguard Worker }
79*8b6cd535SAndroid Build Coastguard Worker 
TEST(DestructibleFileTest,MoveConstructionDeletesFileProperly)80*8b6cd535SAndroid Build Coastguard Worker TEST(DestructibleFileTest, MoveConstructionDeletesFileProperly) {
81*8b6cd535SAndroid Build Coastguard Worker   Filesystem filesystem;
82*8b6cd535SAndroid Build Coastguard Worker   std::string filepath1 = GetTestTempDir() + "/file1";
83*8b6cd535SAndroid Build Coastguard Worker 
84*8b6cd535SAndroid Build Coastguard Worker   // 1. Create destructible1, it'll be reconstructed soon anyways.
85*8b6cd535SAndroid Build Coastguard Worker   std::unique_ptr<DestructibleFile> destructible1;
86*8b6cd535SAndroid Build Coastguard Worker   {
87*8b6cd535SAndroid Build Coastguard Worker     // 2. Create file1
88*8b6cd535SAndroid Build Coastguard Worker     DestructibleFile destructible2(filepath1, &filesystem);
89*8b6cd535SAndroid Build Coastguard Worker     ASSERT_TRUE(destructible2.is_valid());
90*8b6cd535SAndroid Build Coastguard Worker     int i = 458;
91*8b6cd535SAndroid Build Coastguard Worker     ASSERT_TRUE(filesystem.Write(destructible2.get_fd(), &i, sizeof(i)));
92*8b6cd535SAndroid Build Coastguard Worker 
93*8b6cd535SAndroid Build Coastguard Worker     // Move construct destructible1 from destructible2
94*8b6cd535SAndroid Build Coastguard Worker     destructible1 =
95*8b6cd535SAndroid Build Coastguard Worker         std::make_unique<DestructibleFile>(std::move(destructible2));
96*8b6cd535SAndroid Build Coastguard Worker   }
97*8b6cd535SAndroid Build Coastguard Worker 
98*8b6cd535SAndroid Build Coastguard Worker   // 3. file1 should still exist because it moved into destructible1 from
99*8b6cd535SAndroid Build Coastguard Worker   // destructible2.
100*8b6cd535SAndroid Build Coastguard Worker   ASSERT_TRUE(destructible1->is_valid());
101*8b6cd535SAndroid Build Coastguard Worker   EXPECT_TRUE(filesystem.FileExists(filepath1.c_str()));
102*8b6cd535SAndroid Build Coastguard Worker 
103*8b6cd535SAndroid Build Coastguard Worker   {
104*8b6cd535SAndroid Build Coastguard Worker     // 4. Move construct destructible3 from destructible1
105*8b6cd535SAndroid Build Coastguard Worker     DestructibleFile destructible3(std::move(*destructible1));
106*8b6cd535SAndroid Build Coastguard Worker     ASSERT_TRUE(destructible3.is_valid());
107*8b6cd535SAndroid Build Coastguard Worker   }
108*8b6cd535SAndroid Build Coastguard Worker 
109*8b6cd535SAndroid Build Coastguard Worker   // 5. file1 shouldn't exist because it was destroyed when destructible3 was
110*8b6cd535SAndroid Build Coastguard Worker   // destroyed.
111*8b6cd535SAndroid Build Coastguard Worker   EXPECT_FALSE(filesystem.FileExists(filepath1.c_str()));
112*8b6cd535SAndroid Build Coastguard Worker }
113*8b6cd535SAndroid Build Coastguard Worker 
114*8b6cd535SAndroid Build Coastguard Worker }  // namespace
115*8b6cd535SAndroid Build Coastguard Worker 
116*8b6cd535SAndroid Build Coastguard Worker }  // namespace lib
117*8b6cd535SAndroid Build Coastguard Worker }  // namespace icing
118