summaryrefslogtreecommitdiff
path: root/transport/allocator/1.0/vts/functional/VtsHidlAllocatorV1_0TargetTest.cpp
blob: 39ce6068c18afd31087b9b4ab335961f519f29a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <VtsHalHidlTargetTestBase.h>
#include <VtsHalHidlTargetTestEnvBase.h>
#include <android-base/logging.h>
#include <android/hidl/allocator/1.0/IAllocator.h>
#include <android/hidl/memory/1.0/IMemory.h>
#include <hidlmemory/mapping.h>

using ::android::sp;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hidl::allocator::V1_0::IAllocator;
using ::android::hidl::memory::V1_0::IMemory;

#define ASSERT_OK(ret) ASSERT_TRUE((ret).isOk())
#define EXPECT_OK(ret) EXPECT_TRUE((ret).isOk())

class AllocatorEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
   public:
    virtual void registerTestServices() override { registerTestService<IAllocator>(); }

    static AllocatorEnvironment* instance() {
        static AllocatorEnvironment* instance = new AllocatorEnvironment();
        return instance;
    };
};

class AllocatorHidlTest : public ::testing::VtsHalHidlTargetTestBase {
   public:
    virtual void SetUp() override {
        allocator = getService<IAllocator>(AllocatorEnvironment::instance());
        ASSERT_NE(allocator, nullptr);
    }

    sp<IMemory> expectAllocateSuccess(size_t size) {
        sp<IMemory> memory;
        EXPECT_OK(allocator->allocate(size, [&](bool success, const hidl_memory& mem) {
            ASSERT_TRUE(success) << "Allocate failed for size: " << size;
            EXPECT_EQ(mem.size(), size)
                << "Allocated " << size << " but got hidl_memory with size " << mem.size();
            memory = mapMemory(mem);
        }));
        EXPECT_NE(nullptr, memory.get());
        EXPECT_EQ(memory->getSize(), size)
            << "Allocated " << size << " but got IMemory with size " << memory->getSize();
        return memory;
    }

    std::vector<sp<IMemory>> expectBatchAllocateSuccess(size_t size, size_t count) {
        std::vector<sp<IMemory>> memories;
        memories.reserve(count);
        EXPECT_OK(allocator->batchAllocate(
            size, count, [&](bool success, const hidl_vec<hidl_memory>& mems) {
                EXPECT_EQ(count, mems.size());

                for (const hidl_memory& mem : mems) {
                    ASSERT_TRUE(success) << "Allocate failed for size: " << size;
                    EXPECT_EQ(mem.size(), size)
                        << "Allocated " << size << " but got hidl_memory with size " << mem.size();
                    memories.push_back(mapMemory(mem));
                }
            }));
        for (const sp<IMemory>& memory : memories) {
            EXPECT_NE(nullptr, memory.get());
            EXPECT_EQ(memory->getSize(), size)
                << "Allocated " << size << " but got IMemory with size " << memory->getSize();
        }
        return memories;
    }

    sp<IAllocator> allocator;
};

TEST_F(AllocatorHidlTest, TestAllocateSizes) {
    for (size_t size : {1, 1023, 1024, 1025, 4096}) {
        expectAllocateSuccess(size);
    }
}

TEST_F(AllocatorHidlTest, TestBatchAllocateSizes) {
    for (size_t count : {1, 1, 2, 3, 10}) {
        for (size_t size : {1, 1023, 1024, 1025, 4096}) {
            expectBatchAllocateSuccess(size, count);
        }
    }
}

TEST_F(AllocatorHidlTest, TestCommit) {
    constexpr size_t kSize = 1337;

    sp<IMemory> memory = expectAllocateSuccess(kSize);
    for (int i = 0; i < 100; i++) {
        EXPECT_OK(memory->read());
        EXPECT_OK(memory->update());
        EXPECT_OK(memory->commit());

        EXPECT_OK(memory->read());
        EXPECT_OK(memory->commit());

        EXPECT_OK(memory->update());
        EXPECT_OK(memory->commit());
    }

    for (int i = 0; i < kSize; i++) {
        EXPECT_OK(memory->readRange(i, kSize));
        EXPECT_OK(memory->updateRange(i, kSize));
        EXPECT_OK(memory->commit());

        EXPECT_OK(memory->readRange(i, kSize));
        EXPECT_OK(memory->commit());

        EXPECT_OK(memory->updateRange(i, kSize));
        EXPECT_OK(memory->commit());
    }
}

int main(int argc, char** argv) {
    ::testing::AddGlobalTestEnvironment(AllocatorEnvironment::instance());
    ::testing::InitGoogleTest(&argc, argv);
    AllocatorEnvironment::instance()->init(&argc, argv);
    return RUN_ALL_TESTS();
}