1 /*
2  * Copyright (C) 2018 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 "types.h"
18 
19 #include <gtest/gtest.h>
20 
21 #include <memory>
22 #include <type_traits>
23 #include <utility>
24 
25 #include "fdevent/fdevent_test.h"
26 
create_block(const std::string & string)27 static IOVector::block_type create_block(const std::string& string) {
28     return IOVector::block_type(string.begin(), string.end());
29 }
30 
create_block(char value,size_t len)31 static IOVector::block_type create_block(char value, size_t len) {
32     auto block = IOVector::block_type();
33     block.resize(len);
34 
35     static_assert(std::is_standard_layout<decltype(block)>());
36     memset(&(block)[0], value, len);
37 
38     return block;
39 }
40 
41 template <typename T>
copy_block(const T & block)42 static IOVector::block_type copy_block(const T& block) {
43     auto copy = IOVector::block_type();
44     copy.assign(block.begin(), block.end());
45     return copy;
46 }
47 
TEST(IOVector,empty)48 TEST(IOVector, empty) {
49     // Empty IOVector.
50     IOVector bc;
51     CHECK_EQ(0ULL, bc.coalesce().size());
52 }
53 
TEST(IOVector,move_constructor)54 TEST(IOVector, move_constructor) {
55     IOVector x;
56     size_t xsize = x.coalesce().size();
57     IOVector y(std::move(x));
58     CHECK_EQ(xsize, y.coalesce().size());
59 }
60 
TEST(IOVector,single_block)61 TEST(IOVector, single_block) {
62     // A single block.
63     auto block = create_block('x', 100);
64     IOVector bc;
65     bc.append(copy_block(block));
66     ASSERT_EQ(100ULL, bc.size());
67     auto coalesced = bc.coalesce();
68     ASSERT_EQ(block, coalesced);
69 }
70 
TEST(IOVector,single_block_split)71 TEST(IOVector, single_block_split) {
72     // One block split.
73     IOVector bc;
74     bc.append(create_block("foobar"));
75     IOVector foo = bc.take_front(3);
76     ASSERT_EQ(3ULL, foo.size());
77     ASSERT_EQ(3ULL, bc.size());
78     ASSERT_EQ(create_block("foo"), foo.coalesce());
79     ASSERT_EQ(create_block("bar"), bc.coalesce());
80 }
81 
TEST(IOVector,aligned_split)82 TEST(IOVector, aligned_split) {
83     IOVector bc;
84     bc.append(create_block("foo"));
85     bc.append(create_block("bar"));
86     bc.append(create_block("baz"));
87     ASSERT_EQ(9ULL, bc.size());
88 
89     IOVector foo = bc.take_front(3);
90     ASSERT_EQ(3ULL, foo.size());
91     ASSERT_EQ(create_block("foo"), foo.coalesce());
92 
93     IOVector bar = bc.take_front(3);
94     ASSERT_EQ(3ULL, bar.size());
95     ASSERT_EQ(create_block("bar"), bar.coalesce());
96 
97     IOVector baz = bc.take_front(3);
98     ASSERT_EQ(3ULL, baz.size());
99     ASSERT_EQ(create_block("baz"), baz.coalesce());
100 
101     ASSERT_EQ(0ULL, bc.size());
102 }
103 
TEST(IOVector,misaligned_split)104 TEST(IOVector, misaligned_split) {
105     IOVector bc;
106     bc.append(create_block("foo"));
107     bc.append(create_block("bar"));
108     bc.append(create_block("baz"));
109     bc.append(create_block("qux"));
110     bc.append(create_block("quux"));
111 
112     // Aligned left, misaligned right, across multiple blocks.
113     IOVector foob = bc.take_front(4);
114     ASSERT_EQ(4ULL, foob.size());
115     ASSERT_EQ(create_block("foob"), foob.coalesce());
116 
117     // Misaligned left, misaligned right, in one block.
118     IOVector a = bc.take_front(1);
119     ASSERT_EQ(1ULL, a.size());
120     ASSERT_EQ(create_block("a"), a.coalesce());
121 
122     // Misaligned left, misaligned right, across two blocks.
123     IOVector rba = bc.take_front(3);
124     ASSERT_EQ(3ULL, rba.size());
125     ASSERT_EQ(create_block("rba"), rba.coalesce());
126 
127     // Misaligned left, misaligned right, across three blocks.
128     IOVector zquxquu = bc.take_front(7);
129     ASSERT_EQ(7ULL, zquxquu.size());
130     ASSERT_EQ(create_block("zquxquu"), zquxquu.coalesce());
131 
132     ASSERT_EQ(1ULL, bc.size());
133     ASSERT_EQ(create_block("x"), bc.coalesce());
134 }
135 
TEST(IOVector,drop_front)136 TEST(IOVector, drop_front) {
137     IOVector vec;
138 
139     vec.append(create_block('x', 2));
140     vec.append(create_block('y', 1000));
141     ASSERT_EQ(2U, vec.front_size());
142     ASSERT_EQ(1002U, vec.size());
143 
144     vec.drop_front(1);
145     ASSERT_EQ(1U, vec.front_size());
146     ASSERT_EQ(1001U, vec.size());
147 
148     vec.drop_front(1);
149     ASSERT_EQ(1000U, vec.front_size());
150     ASSERT_EQ(1000U, vec.size());
151 }
152 
TEST(IOVector,take_front)153 TEST(IOVector, take_front) {
154     IOVector vec;
155     ASSERT_TRUE(vec.take_front(0).empty());
156 
157     vec.append(create_block('x', 2));
158     ASSERT_EQ(2ULL, vec.size());
159 
160     ASSERT_EQ(1ULL, vec.take_front(1).size());
161     ASSERT_EQ(1ULL, vec.size());
162 
163     ASSERT_EQ(1ULL, vec.take_front(1).size());
164     ASSERT_EQ(0ULL, vec.size());
165 }
166 
TEST(IOVector,trim_front)167 TEST(IOVector, trim_front) {
168     IOVector vec;
169     vec.append(create_block('x', 2));
170 
171     ASSERT_EQ(1ULL, vec.take_front(1).size());
172     ASSERT_EQ(1ULL, vec.size());
173     vec.trim_front();
174     ASSERT_EQ(1ULL, vec.size());
175 }
176 
177 class weak_ptr_test : public FdeventTest {};
178 
179 struct Destructor : public enable_weak_from_this<Destructor> {
DestructorDestructor180     Destructor(bool* destroyed) : destroyed_(destroyed) {}
~DestructorDestructor181     ~Destructor() { *destroyed_ = true; }
182 
183     bool* destroyed_;
184 };
185 
TEST_F(weak_ptr_test,smoke)186 TEST_F(weak_ptr_test, smoke) {
187     PrepareThread();
188 
189     Destructor* destructor = nullptr;
190     bool destroyed = false;
191     std::optional<weak_ptr<Destructor>> p;
192 
193     fdevent_run_on_looper([&p, &destructor, &destroyed]() {
194         destructor = new Destructor(&destroyed);
195         p = destructor->weak();
196         ASSERT_TRUE(p->get());
197 
198         p->reset();
199         ASSERT_FALSE(p->get());
200 
201         p->reset(destructor);
202         ASSERT_TRUE(p->get());
203     });
204     WaitForFdeventLoop();
205     ASSERT_TRUE(destructor);
206     ASSERT_FALSE(destroyed);
207 
208     destructor->schedule_deletion();
209     WaitForFdeventLoop();
210 
211     ASSERT_TRUE(destroyed);
212     fdevent_run_on_looper([&p]() {
213         ASSERT_FALSE(p->get());
214         p.reset();
215     });
216 
217     TerminateThread();
218 }
219