David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include "socket_mock.h" |
| 30 | |
| 31 | #include <gtest/gtest.h> |
| 32 | |
| 33 | SocketMock::SocketMock() : Socket(INVALID_SOCKET) {} |
| 34 | |
| 35 | SocketMock::~SocketMock() { |
| 36 | if (!events_.empty()) { |
| 37 | ADD_FAILURE() << events_.size() << " event(s) were not handled"; |
| 38 | } |
| 39 | } |
| 40 | |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 41 | bool SocketMock::Send(const void* data, size_t length) { |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 42 | if (events_.empty()) { |
| 43 | ADD_FAILURE() << "Send() was called when no message was expected"; |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 44 | return false; |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | if (events_.front().type != EventType::kSend) { |
| 48 | ADD_FAILURE() << "Send() was called out-of-order"; |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 49 | return false; |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | std::string message(reinterpret_cast<const char*>(data), length); |
| 53 | if (events_.front().message != message) { |
| 54 | ADD_FAILURE() << "Send() expected " << events_.front().message << ", but got " << message; |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 55 | return false; |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 56 | } |
| 57 | |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 58 | bool return_value = events_.front().status; |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 59 | events_.pop(); |
David Pursell | 2c094f7 | 2016-02-03 10:23:05 -0800 | [diff] [blame] | 60 | return return_value; |
David Pursell | b34e4a0 | 2016-02-01 09:42:09 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Mock out multi-buffer send to be one large send, since that's what it should looks like from |
| 64 | // the user's perspective. |
| 65 | bool SocketMock::Send(std::vector<cutils_socket_buffer_t> buffers) { |
| 66 | std::string data; |
| 67 | for (const auto& buffer : buffers) { |
| 68 | data.append(reinterpret_cast<const char*>(buffer.data), buffer.length); |
| 69 | } |
| 70 | return Send(data.data(), data.size()); |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | ssize_t SocketMock::Receive(void* data, size_t length, int /*timeout_ms*/) { |
| 74 | if (events_.empty()) { |
| 75 | ADD_FAILURE() << "Receive() was called when no message was ready"; |
| 76 | return -1; |
| 77 | } |
| 78 | |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 79 | const Event& event = events_.front(); |
| 80 | if (event.type != EventType::kReceive) { |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 81 | ADD_FAILURE() << "Receive() was called out-of-order"; |
| 82 | return -1; |
| 83 | } |
| 84 | |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 85 | const std::string& message = event.message; |
| 86 | if (message.length() > length) { |
| 87 | ADD_FAILURE() << "Receive(): not enough bytes (" << length << ") for " << message; |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 88 | return -1; |
| 89 | } |
| 90 | |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 91 | receive_timed_out_ = event.status; |
| 92 | ssize_t return_value = message.length(); |
| 93 | |
| 94 | // Empty message indicates failure. |
| 95 | if (message.empty()) { |
| 96 | return_value = -1; |
| 97 | } else { |
| 98 | memcpy(data, message.data(), message.length()); |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 99 | } |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 100 | |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 101 | events_.pop(); |
| 102 | return return_value; |
| 103 | } |
| 104 | |
| 105 | int SocketMock::Close() { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | std::unique_ptr<Socket> SocketMock::Accept() { |
| 110 | if (events_.empty()) { |
| 111 | ADD_FAILURE() << "Accept() was called when no socket was ready"; |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
| 115 | if (events_.front().type != EventType::kAccept) { |
| 116 | ADD_FAILURE() << "Accept() was called out-of-order"; |
| 117 | return nullptr; |
| 118 | } |
| 119 | |
| 120 | std::unique_ptr<Socket> sock = std::move(events_.front().sock); |
| 121 | events_.pop(); |
| 122 | return sock; |
| 123 | } |
| 124 | |
| 125 | void SocketMock::ExpectSend(std::string message) { |
David Pursell | 2c094f7 | 2016-02-03 10:23:05 -0800 | [diff] [blame] | 126 | events_.push(Event(EventType::kSend, std::move(message), true, nullptr)); |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 127 | } |
| 128 | |
David Pursell | 2c094f7 | 2016-02-03 10:23:05 -0800 | [diff] [blame] | 129 | void SocketMock::ExpectSendFailure(std::string message) { |
| 130 | events_.push(Event(EventType::kSend, std::move(message), false, nullptr)); |
| 131 | } |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 132 | |
| 133 | void SocketMock::AddReceive(std::string message) { |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 134 | events_.push(Event(EventType::kReceive, std::move(message), false, nullptr)); |
| 135 | } |
| 136 | |
| 137 | void SocketMock::AddReceiveTimeout() { |
| 138 | events_.push(Event(EventType::kReceive, "", true, nullptr)); |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void SocketMock::AddReceiveFailure() { |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 142 | events_.push(Event(EventType::kReceive, "", false, nullptr)); |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void SocketMock::AddAccept(std::unique_ptr<Socket> sock) { |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 146 | events_.push(Event(EventType::kAccept, "", false, std::move(sock))); |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 147 | } |
| 148 | |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 149 | SocketMock::Event::Event(EventType _type, std::string _message, ssize_t _status, |
David Pursell | c3a4669 | 2016-01-29 08:10:50 -0800 | [diff] [blame] | 150 | std::unique_ptr<Socket> _sock) |
David Pursell | c742a7f | 2016-02-04 15:21:58 -0800 | [diff] [blame^] | 151 | : type(_type), message(_message), status(_status), sock(std::move(_sock)) {} |