blob: bcb91ecf3dff21169defc166d59f3fa32e5cb2ab [file] [log] [blame]
David Pursellc3a46692016-01-29 08:10:50 -08001/*
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
33SocketMock::SocketMock() : Socket(INVALID_SOCKET) {}
34
35SocketMock::~SocketMock() {
36 if (!events_.empty()) {
37 ADD_FAILURE() << events_.size() << " event(s) were not handled";
38 }
39}
40
David Pursellb34e4a02016-02-01 09:42:09 -080041bool SocketMock::Send(const void* data, size_t length) {
David Pursellc3a46692016-01-29 08:10:50 -080042 if (events_.empty()) {
43 ADD_FAILURE() << "Send() was called when no message was expected";
David Pursellb34e4a02016-02-01 09:42:09 -080044 return false;
David Pursellc3a46692016-01-29 08:10:50 -080045 }
46
47 if (events_.front().type != EventType::kSend) {
48 ADD_FAILURE() << "Send() was called out-of-order";
David Pursellb34e4a02016-02-01 09:42:09 -080049 return false;
David Pursellc3a46692016-01-29 08:10:50 -080050 }
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 Pursellb34e4a02016-02-01 09:42:09 -080055 return false;
David Pursellc3a46692016-01-29 08:10:50 -080056 }
57
David Pursellc3a46692016-01-29 08:10:50 -080058 events_.pop();
David Pursellb34e4a02016-02-01 09:42:09 -080059 return true;
60}
61
62// Mock out multi-buffer send to be one large send, since that's what it should looks like from
63// the user's perspective.
64bool SocketMock::Send(std::vector<cutils_socket_buffer_t> buffers) {
65 std::string data;
66 for (const auto& buffer : buffers) {
67 data.append(reinterpret_cast<const char*>(buffer.data), buffer.length);
68 }
69 return Send(data.data(), data.size());
David Pursellc3a46692016-01-29 08:10:50 -080070}
71
72ssize_t SocketMock::Receive(void* data, size_t length, int /*timeout_ms*/) {
73 if (events_.empty()) {
74 ADD_FAILURE() << "Receive() was called when no message was ready";
75 return -1;
76 }
77
78 if (events_.front().type != EventType::kReceive) {
79 ADD_FAILURE() << "Receive() was called out-of-order";
80 return -1;
81 }
82
83 if (events_.front().return_value > static_cast<ssize_t>(length)) {
84 ADD_FAILURE() << "Receive(): not enough bytes (" << length << ") for "
85 << events_.front().message;
86 return -1;
87 }
88
89 ssize_t return_value = events_.front().return_value;
90 if (return_value > 0) {
91 memcpy(data, events_.front().message.data(), return_value);
92 }
93 events_.pop();
94 return return_value;
95}
96
97int SocketMock::Close() {
98 return 0;
99}
100
101std::unique_ptr<Socket> SocketMock::Accept() {
102 if (events_.empty()) {
103 ADD_FAILURE() << "Accept() was called when no socket was ready";
104 return nullptr;
105 }
106
107 if (events_.front().type != EventType::kAccept) {
108 ADD_FAILURE() << "Accept() was called out-of-order";
109 return nullptr;
110 }
111
112 std::unique_ptr<Socket> sock = std::move(events_.front().sock);
113 events_.pop();
114 return sock;
115}
116
117void SocketMock::ExpectSend(std::string message) {
David Pursellb34e4a02016-02-01 09:42:09 -0800118 events_.push(Event(EventType::kSend, std::move(message), 0, nullptr));
David Pursellc3a46692016-01-29 08:10:50 -0800119}
120
David Pursellb34e4a02016-02-01 09:42:09 -0800121// TODO: make this properly return false to the caller.
122//void SocketMock::ExpectSendFailure(std::string message) {
123// events_.push(Event(EventType::kSend, std::move(message), 0, nullptr));
124//}
David Pursellc3a46692016-01-29 08:10:50 -0800125
126void SocketMock::AddReceive(std::string message) {
127 ssize_t return_value = message.length();
128 events_.push(Event(EventType::kReceive, std::move(message), return_value, nullptr));
129}
130
131void SocketMock::AddReceiveFailure() {
132 events_.push(Event(EventType::kReceive, "", -1, nullptr));
133}
134
135void SocketMock::AddAccept(std::unique_ptr<Socket> sock) {
136 events_.push(Event(EventType::kAccept, "", 0, std::move(sock)));
137}
138
139SocketMock::Event::Event(EventType _type, std::string _message, ssize_t _return_value,
140 std::unique_ptr<Socket> _sock)
141 : type(_type), message(_message), return_value(_return_value), sock(std::move(_sock)) {}