1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17
18 #include "aemu/base/ManagedDescriptor.hpp"
19
20 namespace android {
21 namespace base {
22 namespace {
23
24 using ::testing::_;
25 using ::testing::AtLeast;
26
27 struct PlatformTraitTest {
28 using DescriptorType = int;
29 MOCK_METHOD(void, closeDescriptor, (DescriptorType));
30 };
31
32 using ManagedDescriptorForTesting = ManagedDescriptorBase<PlatformTraitTest>;
33
TEST(ManagedDescriptorTest,ShouldCloseAnInitializedDescriptorOutOfScope)34 TEST(ManagedDescriptorTest, ShouldCloseAnInitializedDescriptorOutOfScope) {
35 PlatformTraitTest::DescriptorType rawDescriptor = 42;
36 PlatformTraitTest platformTrait;
37 {
38 EXPECT_CALL(platformTrait, closeDescriptor(rawDescriptor)).Times(1);
39 { ManagedDescriptorForTesting descriptor(rawDescriptor, platformTrait); }
40 }
41 {
42 EXPECT_CALL(platformTrait, closeDescriptor(rawDescriptor)).Times(1);
43 {
44 ManagedDescriptorForTesting descriptor(rawDescriptor, platformTrait);
45 descriptor = ManagedDescriptorForTesting(platformTrait);
46 }
47 }
48 }
49
TEST(ManagedDescriptorTest,ShouldNotCloseForAnUninitializedDescriptorOutOfScope)50 TEST(ManagedDescriptorTest, ShouldNotCloseForAnUninitializedDescriptorOutOfScope) {
51 PlatformTraitTest platformTrait;
52 EXPECT_CALL(platformTrait, closeDescriptor(_)).Times(0);
53 { ManagedDescriptorForTesting descriptor(platformTrait); }
54 {
55 ManagedDescriptorForTesting descriptor(42, platformTrait);
56 descriptor.release();
57 }
58 }
59
TEST(ManagedDescriptorTest,ShouldGetTheCorrectRawDescriptor)60 TEST(ManagedDescriptorTest, ShouldGetTheCorrectRawDescriptor) {
61 PlatformTraitTest platformTrait;
62 PlatformTraitTest::DescriptorType rawDescriptor = 42;
63 EXPECT_CALL(platformTrait, closeDescriptor(_)).Times(AtLeast(1));
64 {
65 ManagedDescriptorForTesting descriptor(rawDescriptor, platformTrait);
66 EXPECT_EQ(descriptor.release(), rawDescriptor);
67 }
68 {
69 ManagedDescriptorForTesting descriptor(rawDescriptor, platformTrait);
70 EXPECT_EQ(descriptor.get(), rawDescriptor);
71 }
72 {
73 ManagedDescriptorForTesting descriptor(platformTrait);
74 EXPECT_EQ(descriptor.release(), std::nullopt);
75 }
76 {
77 ManagedDescriptorForTesting descriptor(platformTrait);
78 EXPECT_EQ(descriptor.get(), std::nullopt);
79 }
80 }
81
TEST(ManagedDescriptorTest,ShouldCloseOnceIfMoved)82 TEST(ManagedDescriptorTest, ShouldCloseOnceIfMoved) {
83 PlatformTraitTest platformTrait;
84 PlatformTraitTest::DescriptorType rawDescriptor = 42;
85 {
86 EXPECT_CALL(platformTrait, closeDescriptor(rawDescriptor)).Times(1);
87 {
88 ManagedDescriptorForTesting descriptor1(rawDescriptor, platformTrait),
89 descriptor2(platformTrait);
90 descriptor2 = std::move(descriptor1);
91 }
92 }
93 {
94 EXPECT_CALL(platformTrait, closeDescriptor(rawDescriptor)).Times(1);
95 {
96 ManagedDescriptorForTesting descriptor1(rawDescriptor, platformTrait);
97 ManagedDescriptorForTesting descriptor2(std::move(descriptor1));
98 }
99 }
100 {
101 EXPECT_CALL(platformTrait, closeDescriptor(rawDescriptor)).Times(1);
102 {
103 ManagedDescriptorForTesting descriptor(rawDescriptor, platformTrait);
104 descriptor = std::move(descriptor);
105 }
106 }
107 {
108 PlatformTraitTest::DescriptorType anotherRawDescriptor = 2871;
109 EXPECT_CALL(platformTrait, closeDescriptor(rawDescriptor)).Times(1);
110 EXPECT_CALL(platformTrait, closeDescriptor(anotherRawDescriptor)).Times(1);
111 {
112 ManagedDescriptorForTesting descriptor1(rawDescriptor, platformTrait);
113 ManagedDescriptorForTesting descriptor2(anotherRawDescriptor, platformTrait);
114 descriptor1 = std::move(descriptor2);
115 }
116 }
117 }
118
119 } // namespace
120 } // namespace base
121 } // namespace android