blob: 6e95b16022164a71439b1288babe429065978741 [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
Elliott Hughes6ebec932018-04-10 14:22:13 -070029#pragma once
David Pursellc3a46692016-01-29 08:10:50 -080030
31#include <memory>
32#include <queue>
33#include <string>
34
35#include <android-base/macros.h>
36
37#include "socket.h"
38
39// A mock Socket implementation to be used for testing. Tests can set expectations for messages
40// to be sent and provide messages to be received in order to verify protocol behavior.
41//
42// Example: testing sending "foo" and receiving "bar".
43// SocketMock mock;
44// mock.ExpectSend("foo");
45// mock.AddReceive("bar");
46// EXPECT_TRUE(DoFooBar(&mock));
47//
48// Example: testing sending "foo" and expecting "bar", but receiving "baz" instead.
49// SocketMock mock;
50// mock.ExpectSend("foo");
51// mock.AddReceive("baz");
52// EXPECT_FALSE(DoFooBar(&mock));
53class SocketMock : public Socket {
54 public:
55 SocketMock();
56 ~SocketMock() override;
57
David Pursellb34e4a02016-02-01 09:42:09 -080058 bool Send(const void* data, size_t length) override;
59 bool Send(std::vector<cutils_socket_buffer_t> buffers) override;
David Pursellc3a46692016-01-29 08:10:50 -080060 ssize_t Receive(void* data, size_t length, int timeout_ms) override;
61 int Close() override;
62 virtual std::unique_ptr<Socket> Accept();
63
64 // Adds an expectation for Send().
65 void ExpectSend(std::string message);
66
David Pursell2c094f72016-02-03 10:23:05 -080067 // Adds an expectation for Send() that returns false.
68 void ExpectSendFailure(std::string message);
69
David Pursellc3a46692016-01-29 08:10:50 -080070 // Adds data to provide for Receive().
71 void AddReceive(std::string message);
72
David Pursellc742a7f2016-02-04 15:21:58 -080073 // Adds a Receive() timeout after which ReceiveTimedOut() will return true.
74 void AddReceiveTimeout();
75
76 // Adds a Receive() failure after which ReceiveTimedOut() will return false.
David Pursellc3a46692016-01-29 08:10:50 -080077 void AddReceiveFailure();
78
79 // Adds a Socket to return from Accept().
80 void AddAccept(std::unique_ptr<Socket> sock);
81
82 private:
83 enum class EventType { kSend, kReceive, kAccept };
84
85 struct Event {
David Pursellc742a7f2016-02-04 15:21:58 -080086 Event(EventType _type, std::string _message, ssize_t _status,
David Pursellc3a46692016-01-29 08:10:50 -080087 std::unique_ptr<Socket> _sock);
88
89 EventType type;
90 std::string message;
David Pursellc742a7f2016-02-04 15:21:58 -080091 bool status; // Return value for Send() or timeout status for Receive().
David Pursellc3a46692016-01-29 08:10:50 -080092 std::unique_ptr<Socket> sock;
93 };
94
95 std::queue<Event> events_;
96
97 DISALLOW_COPY_AND_ASSIGN(SocketMock);
98};