From afc4d7cfe753669b08562eba8f58cbceefed334f Mon Sep 17 00:00:00 2001 From: Michael Butler Date: Thu, 10 Dec 2020 15:38:45 -0800 Subject: Create unit tests for NN interface utility code This CL introduces unit tests to validate the V1_X::utils::Device, *PreparedModel, and *Buffer adapter classes. It does so by mocking the underlying HIDL interface in order to simulate a driver returning bad data, HIDL transport failures, and service crashes. Note that the purpose of these new tests is to validate the adapter classes themselves, not the HIDL interfaces they use. For example, because nn::IPreparedModel does not currently define a method for configuring a burst execution, V1_[23]::utils::PreparedModel similarly does not use hardware::neuralnetworks::V1_[23]::IPreparedModel's configureExecutionBurst method. This CL also introduces unit tests to validate the utils::Resilient* adapter classes, and mocks DEAD_OBJECT failures to ensure that the underyling object can be recovered appropriately. Bug: 163801800 Test: mma Test: atest neuralnetworks_utils_hal_common_test Test: atest neuralnetworks_utils_hal_1_[0-3]_test Change-Id: I2c79865bf666d3f4bf53061ff5090746403583e9 --- .../utils/common/test/ResilientBufferTest.cpp | 266 +++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 neuralnetworks/utils/common/test/ResilientBufferTest.cpp (limited to 'neuralnetworks/utils/common/test/ResilientBufferTest.cpp') diff --git a/neuralnetworks/utils/common/test/ResilientBufferTest.cpp b/neuralnetworks/utils/common/test/ResilientBufferTest.cpp new file mode 100644 index 0000000000..deb9b7cf21 --- /dev/null +++ b/neuralnetworks/utils/common/test/ResilientBufferTest.cpp @@ -0,0 +1,266 @@ +/* + * Copyright (C) 2020 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 +#include +#include +#include +#include +#include +#include "MockBuffer.h" + +namespace android::hardware::neuralnetworks::utils { +namespace { + +using ::testing::_; +using ::testing::InvokeWithoutArgs; +using ::testing::Return; + +constexpr auto kToken = nn::Request::MemoryDomainToken{1}; + +using SharedMockBuffer = std::shared_ptr; +using MockBufferFactory = ::testing::MockFunction()>; + +SharedMockBuffer createConfiguredMockBuffer() { + return std::make_shared(); +} + +std::tuple, std::unique_ptr, + std::shared_ptr> +setup() { + auto mockBuffer = std::make_shared(); + + auto mockBufferFactory = std::make_unique(); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(mockBuffer)); + + auto buffer = ResilientBuffer::create(mockBufferFactory->AsStdFunction()).value(); + return std::make_tuple(std::move(mockBuffer), std::move(mockBufferFactory), std::move(buffer)); +} + +constexpr auto makeError = [](nn::ErrorStatus status) { + return [status](const auto&... /*args*/) { return nn::error(status); }; +}; +const auto kReturnGeneralFailure = makeError(nn::ErrorStatus::GENERAL_FAILURE); +const auto kReturnDeadObject = makeError(nn::ErrorStatus::DEAD_OBJECT); + +const auto kNoError = nn::GeneralResult{}; + +} // namespace + +TEST(ResilientBufferTest, invalidBufferFactory) { + // setup call + const auto invalidBufferFactory = ResilientBuffer::Factory{}; + + // run test + const auto result = ResilientBuffer::create(invalidBufferFactory); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT); +} + +TEST(ResilientBufferTest, bufferFactoryFailure) { + // setup call + const auto invalidBufferFactory = kReturnGeneralFailure; + + // run test + const auto result = ResilientBuffer::create(invalidBufferFactory); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(ResilientBufferTest, getBuffer) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + + // run test + const auto result = buffer->getBuffer(); + + // verify result + EXPECT_TRUE(result == mockBuffer); +} + +TEST(ResilientBufferTest, getToken) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, getToken()).Times(1).WillOnce(Return(kToken)); + + // run test + const auto token = buffer->getToken(); + + // verify result + EXPECT_EQ(token, kToken); +} + +TEST(ResilientBufferTest, copyTo) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(Return(kNoError)); + + // run test + const auto result = buffer->copyTo({}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(ResilientBufferTest, copyToError) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->copyTo({}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(ResilientBufferTest, copyToDeadObjectFailedRecovery) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(kReturnDeadObject); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->copyTo({}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(ResilientBufferTest, copyToDeadObjectSuccessfulRecovery) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(kReturnDeadObject); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*recoveredMockBuffer, copyTo(_)).Times(1).WillOnce(Return(kNoError)); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(recoveredMockBuffer)); + + // run test + const auto result = buffer->copyTo({}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(ResilientBufferTest, copyFrom) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(Return(kNoError)); + + // run test + const auto result = buffer->copyFrom({}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(ResilientBufferTest, copyFromError) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->copyFrom({}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(ResilientBufferTest, copyFromDeadObjectFailedRecovery) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(kReturnDeadObject); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->copyFrom({}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(ResilientBufferTest, copyFromDeadObjectSuccessfulRecovery) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(kReturnDeadObject); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*recoveredMockBuffer, copyFrom(_, _)).Times(1).WillOnce(Return(kNoError)); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(recoveredMockBuffer)); + + // run test + const auto result = buffer->copyFrom({}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(ResilientBufferTest, recover) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(recoveredMockBuffer)); + + // run test + const auto result = buffer->recover(mockBuffer.get()); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + EXPECT_TRUE(result.value() == recoveredMockBuffer); +} + +TEST(ResilientBufferTest, recoverFailure) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->recover(mockBuffer.get()); + + // verify result + EXPECT_FALSE(result.has_value()); +} + +TEST(ResilientBufferTest, someoneElseRecovered) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(recoveredMockBuffer)); + buffer->recover(mockBuffer.get()); + + // run test + const auto result = buffer->recover(mockBuffer.get()); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + EXPECT_TRUE(result.value() == recoveredMockBuffer); +} + +} // namespace android::hardware::neuralnetworks::utils -- cgit v1.2.3