1/* 2 * Copyright (C) 2017 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 <gmock/gmock.h> 18#include <gtest/gtest.h> 19 20#include "../libese.h" 21 22namespace android { 23namespace esed { 24 25using ::testing::_; 26using ::testing::ContainerEq; 27using ::testing::Invoke; 28 29struct MockEseInterface : public EseInterface { 30 MOCK_METHOD0(init, void()); 31 MOCK_METHOD0(open, int()); 32 MOCK_METHOD0(close, void()); 33 MOCK_METHOD0(name, const char*()); 34 MOCK_METHOD2(transceive, int(const std::vector<uint8_t>&, std::vector<uint8_t>&)); 35 MOCK_METHOD0(error, bool()); 36 MOCK_METHOD0(error_message, const char*()); 37 MOCK_METHOD0(error_code, int()); 38}; 39 40inline auto EseReceive(const std::vector<uint8_t>& response) { 41 return Invoke([response](const std::vector<uint8_t>& /* tx */, std::vector<uint8_t>& rx) { 42 const auto end = (rx.size() >= response.size()) 43 ? response.end() : (response.begin() + rx.size()); 44 std::copy(response.begin(), end, rx.begin()); 45 return (int) response.size(); 46 }); 47} 48 49// Returns a mocked device that expects a command, will pass the response to the 50// callback and finally returns the given value. 51// Needs to allocate on heap as it can't copy/move the mock. 52inline std::unique_ptr<MockEseInterface> singleCommandEseMock( 53 const std::vector<uint8_t>& command, const std::vector<uint8_t>& response) { 54 auto mockEse = std::make_unique<MockEseInterface>(); 55 EXPECT_CALL(*mockEse, transceive(ContainerEq(command), _)).WillOnce(EseReceive(response)); 56 return mockEse; 57} 58 59} // namespace esed 60} // namespace android 61