blob: 3e62b330ec4bc14d0fe9e4ae858564a4d183b025 [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
59 ssize_t Send(const void* data, size_t length) override;
60 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
67 // Adds an expectation for Send() that returns -1.
68 void ExpectSendFailure(std::string message);
69
70 // Adds data to provide for Receive().
71 void AddReceive(std::string message);
72
73 // Adds a Receive() failure.
74 void AddReceiveFailure();
75
76 // Adds a Socket to return from Accept().
77 void AddAccept(std::unique_ptr<Socket> sock);
78
79 private:
80 enum class EventType { kSend, kReceive, kAccept };
81
82 struct Event {
83 Event(EventType _type, std::string _message, ssize_t _return_value,
84 std::unique_ptr<Socket> _sock);
85
86 EventType type;
87 std::string message;
88 ssize_t return_value;
89 std::unique_ptr<Socket> sock;
90 };
91
92 std::queue<Event> events_;
93
94 DISALLOW_COPY_AND_ASSIGN(SocketMock);
95};
96
97#endif // SOCKET_MOCK_H_