• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #pragma once
30 
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));
53 class SocketMock : public Socket {
54   public:
55     SocketMock();
56     ~SocketMock() override;
57 
58     bool Send(const void* data, size_t length) override;
59     bool Send(std::vector<cutils_socket_buffer_t> buffers) 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 false.
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() timeout after which ReceiveTimedOut() will return true.
74     void AddReceiveTimeout();
75 
76     // Adds a Receive() failure after which ReceiveTimedOut() will return false.
77     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 {
86         Event(EventType _type, std::string _message, ssize_t _status,
87               std::unique_ptr<Socket> _sock);
88 
89         EventType type;
90         std::string message;
91         bool status;  // Return value for Send() or timeout status for Receive().
92         std::unique_ptr<Socket> sock;
93     };
94 
95     std::queue<Event> events_;
96 
97     DISALLOW_COPY_AND_ASSIGN(SocketMock);
98 };
99