1 /*
2 * Copyright (C) 2020 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 "MockBuffer.h"
18
19 #include <android/hardware/neuralnetworks/1.3/IBuffer.h>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <nnapi/IBuffer.h>
23 #include <nnapi/SharedMemory.h>
24 #include <nnapi/TypeUtils.h>
25 #include <nnapi/Types.h>
26 #include <nnapi/hal/1.3/Buffer.h>
27
28 #include <functional>
29 #include <memory>
30
31 namespace android::hardware::neuralnetworks::V1_3::utils {
32 namespace {
33
34 using ::testing::_;
35 using ::testing::Invoke;
36 using ::testing::InvokeWithoutArgs;
37 using ::testing::Return;
38
39 const auto kMemory = nn::createSharedMemory(4).value();
40 const sp<V1_3::IBuffer> kInvalidBuffer;
41 constexpr auto kInvalidToken = nn::Request::MemoryDomainToken{0};
42 constexpr auto kToken = nn::Request::MemoryDomainToken{1};
43
makeFunctionReturn(V1_3::ErrorStatus status)44 std::function<hardware::Return<V1_3::ErrorStatus>()> makeFunctionReturn(V1_3::ErrorStatus status) {
45 return [status]() -> hardware::Return<V1_3::ErrorStatus> { return status; };
46 }
47
makeTransportFailure(status_t status)48 std::function<hardware::Status()> makeTransportFailure(status_t status) {
49 return [status] { return hardware::Status::fromStatusT(status); };
50 }
51
52 const auto makeSuccessful = makeFunctionReturn(V1_3::ErrorStatus::NONE);
53 const auto makeGeneralError = makeFunctionReturn(V1_3::ErrorStatus::GENERAL_FAILURE);
54 const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
55 const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
56
57 } // namespace
58
TEST(BufferTest,invalidBuffer)59 TEST(BufferTest, invalidBuffer) {
60 // run test
61 const auto result = Buffer::create(kInvalidBuffer, kToken);
62
63 // verify result
64 ASSERT_FALSE(result.has_value());
65 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
66 }
67
TEST(BufferTest,invalidToken)68 TEST(BufferTest, invalidToken) {
69 // setup call
70 const auto mockBuffer = MockBuffer::create();
71
72 // run test
73 const auto result = Buffer::create(mockBuffer, kInvalidToken);
74
75 // verify result
76 ASSERT_FALSE(result.has_value());
77 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
78 }
79
TEST(BufferTest,create)80 TEST(BufferTest, create) {
81 // setup call
82 const auto mockBuffer = MockBuffer::create();
83 const auto buffer = Buffer::create(mockBuffer, kToken).value();
84
85 // run test
86 const auto token = buffer->getToken();
87
88 // verify result
89 EXPECT_EQ(token, kToken);
90 }
91
TEST(BufferTest,copyTo)92 TEST(BufferTest, copyTo) {
93 // setup call
94 const auto mockBuffer = MockBuffer::create();
95 const auto buffer = Buffer::create(mockBuffer, kToken).value();
96 EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeSuccessful));
97
98 // run test
99 const auto result = buffer->copyTo(kMemory);
100
101 // verify result
102 EXPECT_TRUE(result.has_value()) << result.error().message;
103 }
104
TEST(BufferTest,copyToError)105 TEST(BufferTest, copyToError) {
106 // setup test
107 const auto mockBuffer = MockBuffer::create();
108 const auto buffer = Buffer::create(mockBuffer, kToken).value();
109 EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralError));
110
111 // run test
112 const auto result = buffer->copyTo(kMemory);
113
114 // verify result
115 ASSERT_FALSE(result.has_value());
116 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
117 }
118
TEST(BufferTest,copyToTransportFailure)119 TEST(BufferTest, copyToTransportFailure) {
120 // setup test
121 const auto mockBuffer = MockBuffer::create();
122 const auto buffer = Buffer::create(mockBuffer, kToken).value();
123 EXPECT_CALL(*mockBuffer, copyTo(_))
124 .Times(1)
125 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
126
127 // run test
128 const auto result = buffer->copyTo(kMemory);
129
130 // verify result
131 ASSERT_FALSE(result.has_value());
132 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
133 }
134
TEST(BufferTest,copyToDeadObject)135 TEST(BufferTest, copyToDeadObject) {
136 // setup test
137 const auto mockBuffer = MockBuffer::create();
138 const auto buffer = Buffer::create(mockBuffer, kToken).value();
139 EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
140
141 // run test
142 const auto result = buffer->copyTo(kMemory);
143
144 // verify result
145 ASSERT_FALSE(result.has_value());
146 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
147 }
148
TEST(BufferTest,copyFrom)149 TEST(BufferTest, copyFrom) {
150 // setup call
151 const auto mockBuffer = MockBuffer::create();
152 const auto buffer = Buffer::create(mockBuffer, kToken).value();
153 EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(InvokeWithoutArgs(makeSuccessful));
154
155 // run test
156 const auto result = buffer->copyFrom(kMemory, {});
157
158 // verify result
159 EXPECT_TRUE(result.has_value());
160 }
161
TEST(BufferTest,copyFromError)162 TEST(BufferTest, copyFromError) {
163 // setup test
164 const auto mockBuffer = MockBuffer::create();
165 const auto buffer = Buffer::create(mockBuffer, kToken).value();
166 EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralError));
167
168 // run test
169 const auto result = buffer->copyFrom(kMemory, {});
170
171 // verify result
172 ASSERT_FALSE(result.has_value());
173 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
174 }
175
TEST(BufferTest,copyFromTransportFailure)176 TEST(BufferTest, copyFromTransportFailure) {
177 // setup test
178 const auto mockBuffer = MockBuffer::create();
179 const auto buffer = Buffer::create(mockBuffer, kToken).value();
180 EXPECT_CALL(*mockBuffer, copyFrom(_, _))
181 .Times(1)
182 .WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
183
184 // run test
185 const auto result = buffer->copyFrom(kMemory, {});
186
187 // verify result
188 ASSERT_FALSE(result.has_value());
189 EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
190 }
191
TEST(BufferTest,copyFromDeadObject)192 TEST(BufferTest, copyFromDeadObject) {
193 // setup test
194 const auto mockBuffer = MockBuffer::create();
195 const auto buffer = Buffer::create(mockBuffer, kToken).value();
196 EXPECT_CALL(*mockBuffer, copyFrom(_, _))
197 .Times(1)
198 .WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
199
200 // run test
201 const auto result = buffer->copyFrom(kMemory, {});
202
203 // verify result
204 ASSERT_FALSE(result.has_value());
205 EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
206 }
207
208 } // namespace android::hardware::neuralnetworks::V1_3::utils
209