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