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 <gtest/gtest.h>
18 #include <ui/BufferHubMetadata.h>
19
20 namespace android {
21 namespace dvr {
22
23 constexpr size_t kEmptyUserMetadataSize = 0;
24
25 class BufferHubMetadataTest : public ::testing::Test {};
26
TEST_F(BufferHubMetadataTest,Create_UserMetdataSizeTooBig)27 TEST_F(BufferHubMetadataTest, Create_UserMetdataSizeTooBig) {
28 BufferHubMetadata m1 = BufferHubMetadata::create(std::numeric_limits<uint32_t>::max());
29 EXPECT_FALSE(m1.isValid());
30 }
31
TEST_F(BufferHubMetadataTest,Create_Success)32 TEST_F(BufferHubMetadataTest, Create_Success) {
33 BufferHubMetadata m1 = BufferHubMetadata::create(kEmptyUserMetadataSize);
34 EXPECT_TRUE(m1.isValid());
35 EXPECT_NE(m1.metadataHeader(), nullptr);
36 }
37
TEST_F(BufferHubMetadataTest,Import_Success)38 TEST_F(BufferHubMetadataTest, Import_Success) {
39 BufferHubMetadata m1 = BufferHubMetadata::create(kEmptyUserMetadataSize);
40 EXPECT_TRUE(m1.isValid());
41 EXPECT_NE(m1.metadataHeader(), nullptr);
42
43 unique_fd h2 = unique_fd(dup(m1.ashmemFd().get()));
44 EXPECT_NE(h2.get(), -1);
45
46 BufferHubMetadata m2 = BufferHubMetadata::import(std::move(h2));
47 EXPECT_EQ(h2.get(), -1);
48 EXPECT_TRUE(m1.isValid());
49 BufferHubDefs::MetadataHeader* mh1 = m1.metadataHeader();
50 EXPECT_NE(mh1, nullptr);
51
52 // Check if the newly allocated buffer is initialized in released state (i.e.
53 // state equals to 0U).
54 EXPECT_TRUE(mh1->bufferState.load() == 0U);
55
56 EXPECT_TRUE(m2.isValid());
57 BufferHubDefs::MetadataHeader* mh2 = m2.metadataHeader();
58 EXPECT_NE(mh2, nullptr);
59
60 // Check if the newly allocated buffer is initialized in released state (i.e.
61 // state equals to 0U).
62 EXPECT_TRUE(mh2->bufferState.load() == 0U);
63 }
64
TEST_F(BufferHubMetadataTest,MoveMetadataInvalidatesOldOne)65 TEST_F(BufferHubMetadataTest, MoveMetadataInvalidatesOldOne) {
66 BufferHubMetadata m1 = BufferHubMetadata::create(sizeof(int));
67 EXPECT_TRUE(m1.isValid());
68 EXPECT_NE(m1.metadataHeader(), nullptr);
69 EXPECT_NE(m1.ashmemFd().get(), -1);
70 EXPECT_EQ(m1.userMetadataSize(), sizeof(int));
71
72 BufferHubMetadata m2 = std::move(m1);
73
74 // After the move, the metadata header (a raw pointer) should be reset in the older buffer.
75 EXPECT_EQ(m1.metadataHeader(), nullptr);
76 EXPECT_NE(m2.metadataHeader(), nullptr);
77
78 EXPECT_EQ(m1.ashmemFd().get(), -1);
79 EXPECT_NE(m2.ashmemFd().get(), -1);
80
81 EXPECT_EQ(m1.userMetadataSize(), 0U);
82 EXPECT_EQ(m2.userMetadataSize(), sizeof(int));
83
84 BufferHubMetadata m3{std::move(m2)};
85
86 // After the move, the metadata header (a raw pointer) should be reset in the older buffer.
87 EXPECT_EQ(m2.metadataHeader(), nullptr);
88 EXPECT_NE(m3.metadataHeader(), nullptr);
89
90 EXPECT_EQ(m2.ashmemFd().get(), -1);
91 EXPECT_NE(m3.ashmemFd().get(), -1);
92
93 EXPECT_EQ(m2.userMetadataSize(), 0U);
94 EXPECT_EQ(m3.userMetadataSize(), sizeof(int));
95 }
96
97 } // namespace dvr
98 } // namespace android
99