blob: c48aa7bd9d4083825ce7db6bae3610d624348a3a [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 Pursellc3a46692016-01-29 08:10:50 -080068 // Adds data to provide for Receive().
69 void AddReceive(std::string message);
70
71 // Adds a Receive() failure.
72 void AddReceiveFailure();
73
74 // Adds a Socket to return from Accept().
75 void AddAccept(std::unique_ptr<Socket> sock);
76
77 private:
78 enum class EventType { kSend, kReceive, kAccept };
79
80 struct Event {
81 Event(EventType _type, std::string _message, ssize_t _return_value,
82 std::unique_ptr<Socket> _sock);
83
84 EventType type;
85 std::string message;
86 ssize_t return_value;
87 std::unique_ptr<Socket> sock;
88 };
89
90 std::queue<Event> events_;
91
92 DISALLOW_COPY_AND_ASSIGN(SocketMock);
93};
94
95#endif // SOCKET_MOCK_H_