1
1
#include < mock_network/functions.h>
2
2
3
+ #include < mock_network/connection.h>
4
+ #include < mock_network/mock.h>
3
5
#include < mock_network/socket.h>
4
6
5
7
#include < thread>
8
10
#include < unistd.h>
9
11
10
12
namespace mock_network {
13
+ namespace fake {
11
14
12
- extern std::chrono::seconds connection_duration;
13
- extern int connection_result;
14
- extern Map<String, void *> originals;
15
+ int accept (int sockfd, struct sockaddr * addr, socklen_t * addrlen) {
16
+ // TODO: implement this.
17
+ return -1 ;
18
+ }
15
19
16
- namespace fake {
20
+ int bind (int sockfd, const struct sockaddr * addr, socklen_t addrlen) {
21
+ Socket socket = Socket::Get (sockfd);
22
+ if (!socket.IsValid ()) {
23
+ // We can't detect, if the |sockfd| is a valid descriptor at a system level,
24
+ // so we always indicate that it's just not a socket.
25
+ errno = ENOTSOCK;
26
+ return -1 ;
27
+ }
28
+
29
+ if (!socket.SetState (Socket::BIND, addr, addrlen)) {
30
+ // Assume |errno| is already set by |Socket::SetState()|.
31
+ return -1 ;
32
+ }
33
+
34
+ return 0 ;
35
+ }
17
36
18
37
int close (int fd) {
19
38
Socket socket = Socket::Get (fd);
20
39
if (socket.IsValid ()) {
21
40
return socket.Close ();
22
41
}
23
42
24
- return reinterpret_cast <int (*)( int )>(originals[ " close" ])( fd);
43
+ return Mock::CallOriginal <int >( " close" , fd);
25
44
}
26
45
27
46
int connect (int sockfd, const struct sockaddr * addr, socklen_t addrlen) {
@@ -33,32 +52,52 @@ int connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen) {
33
52
return -1 ;
34
53
}
35
54
36
- if (!socket.SetState (Socket::CONNECTING)) {
37
- if (socket.GetState () == Socket::CONNECTING) {
38
- errno = EALREADY;
39
- } else if (socket.GetState () >= Socket::CONNECTED) {
40
- errno = EISCONN;
41
- }
42
-
55
+ if (!socket.SetState (Socket::CONNECTING, addr, addrlen)) {
56
+ // Assume |errno| is already set by |Socket::SetState()|.
43
57
return -1 ;
44
58
}
45
59
46
60
if (!socket.IsNonBlocking ()) {
47
- std::this_thread::sleep_for (connection_duration);
48
- // TODO: handle peer address.
49
- socket.SetState (Socket::CONNECTED);
61
+ std::this_thread::sleep_for (Connection::duration_);
62
+ if (!socket.SetState (Socket::CONNECTED)) {
63
+ errno = EINVAL;
64
+ return -1 ;
65
+ }
50
66
} else {
51
67
// TODO: implement non-blocking connection, keeping in mind, that |select()|
52
68
// or |epoll()| should somehow indicate this fact using pipes.
53
69
// TODO: return EINPROGRESS, if the |connection_duration| is non-zero.
54
70
}
55
71
56
- if (connection_result == 0 ) {
72
+ if (Connection::result_ == 0 ) {
57
73
return 0 ;
58
74
} else {
59
- errno = connection_result;
75
+ errno = Connection::result_;
76
+ return -1 ;
77
+ }
78
+ }
79
+
80
+ int listen (int sockfd, int backlog) {
81
+ Socket socket = Socket::Get (sockfd);
82
+ if (!socket.IsValid ()) {
83
+ // We can't detect, if the |sockfd| is a valid descriptor at a system level,
84
+ // so we always indicate that it's just not a socket.
85
+ errno = ENOTSOCK;
86
+ return -1 ;
87
+ }
88
+
89
+ if (!socket.SetState (Socket::PASSIVE)) {
90
+ // Assume |errno| is already set by |Socket::SetState()|.
60
91
return -1 ;
61
92
}
93
+
94
+ return 0 ;
95
+ }
96
+
97
+ int setsockopt (int sockfd, int level, int optname, const void * optval,
98
+ socklen_t optlen) {
99
+ // TODO: implement this.
100
+ return 0 ;
62
101
}
63
102
64
103
int socket (int domain, int type, int protocol) {
0 commit comments