forked from piaw/google-gtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_socket.h
86 lines (67 loc) · 2.37 KB
/
mock_socket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2007 Google Inc. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Author: [email protected] (Nigel D'souza)
//
// This is a mock socket library to simplify socket-based testing.
#ifndef TOOLS_TAGS_MOCK_SOCKET_H__
#define TOOLS_TAGS_MOCK_SOCKET_H__
#include "pollserver.h"
#include "socket.h"
namespace gtags {
extern const char* kBadAddress;
extern const char* kLocalhostName;
extern const char* kLocalhostIP;
class LoopCountingPollServer : public PollServer {
static const int kShortLoopCount = 5;
static const int kMediumLoopCount = 50;
public:
LoopCountingPollServer(unsigned int max_fds);
virtual ~LoopCountingPollServer() {}
virtual void ShortLoop() { LoopFor(kShortLoopCount); }
virtual void MediumLoop() { LoopFor(kMediumLoopCount); }
virtual void LoopFor(int max_count);
};
class MockClientSocket : public Socket {
public:
MockClientSocket(int fd, PollServer *pollserver, int port);
virtual ~MockClientSocket();
virtual void HandleWrite();
struct sockaddr_in addr_;
bool connected_;
};
class MockListenerSocket : public Socket {
public:
MockListenerSocket(int fd, PollServer *pollserver,
int port, bool keep_alive = false);
virtual ~MockListenerSocket();
virtual void HandleRead();
int accepted_;
int accepted_fd_;
bool keep_alive_;
};
class MockConnectedSocket : public ConnectedSocket {
public:
MockConnectedSocket(int fd, PollServer *pollserver);
virtual ~MockConnectedSocket() {}
virtual void HandleDisconnected();
int disconnected_count_;
// publicize ConnectedSocket's buffers
using ConnectedSocket::inbuf_;
using ConnectedSocket::outbuf_;
};
} // namespace gtags
#endif // TOOLS_TAGS_MOCK_SOCKET_H__