gem5  v20.1.0.0
socket.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 The Regents of the University of California
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 are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include "base/socket.hh"
32 
33 #define TEST_PORT_1 7893
34 #define TEST_PORT_2 7894
35 
36 /*
37  * Socket.test tests socket.cc. It should be noted that some features of
38  * socket.cc have not been fully tested due to interaction with system-calls.
39  */
40 
42 {
43  public:
44  /*
45  * This mock Listen Socket is used to ensure the static variables are reset
46  * back to their default values after deconstruction (i.e., after a test
47  * has completed).
48  */
50  {
51  cleanup();
52  }
53 };
54 
55 TEST(SocketTest, DefaultBehavior)
56 {
57  /*
58  * Tests the default behavior where listenSocket is constructed, and is
59  * not listening to a port.
60  */
61  MockListenSocket listen_socket;
62  EXPECT_EQ(-1, listen_socket.getfd());
63  EXPECT_FALSE(listen_socket.islistening());
64  EXPECT_FALSE(listen_socket.allDisabled());
65 }
66 
67 TEST(SocketTest, DisableAll)
68 {
69  MockListenSocket listen_socket;
70  listen_socket.disableAll();
71  EXPECT_EQ(-1, listen_socket.getfd());
72  EXPECT_FALSE(listen_socket.islistening());
73  EXPECT_TRUE(listen_socket.allDisabled());
74 }
75 
76 TEST(SocketTest, ListenToPort)
77 {
78  MockListenSocket listen_socket;
79  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
80  EXPECT_NE(-1, listen_socket.getfd());
81  EXPECT_TRUE(listen_socket.islistening());
82  EXPECT_FALSE(listen_socket.allDisabled());
83 }
84 
85 TEST(SocketTest, ListenToPortReuseFalse)
86 {
87  MockListenSocket listen_socket;
88  /*
89  * The ListenSocket object should have the same state regardless as to
90  * whether reuse is true or false (it is true by default).
91  */
92  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1, false));
93  EXPECT_NE(-1, listen_socket.getfd());
94  EXPECT_TRUE(listen_socket.islistening());
95  EXPECT_FALSE(listen_socket.allDisabled());
96 }
97 
98 TEST(SocketTest, RelistenWithSameInstanceSamePort)
99 {
100  MockListenSocket listen_socket;
101  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
102 
103  /*
104  * You cannot listen to another port if you are already listening to one.
105  */
106  testing::internal::CaptureStderr();
107  EXPECT_ANY_THROW(listen_socket.listen(TEST_PORT_1));
108  std::string expected = "panic: Socket already listening!\n";
109  std::string actual = testing::internal::GetCapturedStderr().substr();
110 
111  /*
112  * The GoogleExitLogger will output using the following:
113  * `std::cerr << loc.file << ":" << loc.line << ": " << s;`
114  * As we do not care about the file and line where the error originated
115  * (this may change, and it shouldn't break the test when this happens),
116  * we strip out the leading `<file>:<line>: ` (we simply remove everything
117  * prior to two characters after the second colon in the string).
118  */
119  actual = actual.substr(actual.find(":", actual.find(":") + 1) + 2);
120  EXPECT_EQ(expected, actual);
121 }
122 
123 TEST(SocketTest, RelistenWithSameInstanceDifferentPort)
124 {
125  MockListenSocket listen_socket;
126  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
127 
128  /*
129  * You cannot listen to another port if you are already listening to one.
130  */
131  testing::internal::CaptureStderr();
132  EXPECT_ANY_THROW(listen_socket.listen(TEST_PORT_2));
133 
134  std::string expected = "panic: Socket already listening!\n";
135  std::string actual = testing::internal::GetCapturedStderr().substr();
136  actual = actual.substr(actual.find(":", actual.find(":") + 1) + 2);
137  EXPECT_EQ(expected, actual);
138 }
139 
140 TEST(SocketTest, RelistenWithDifferentInstanceOnDifferentPort)
141 {
142  MockListenSocket listen_socket;
143  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
144 
145  /*
146  * You can listen to another port with a different instance.
147  */
148  MockListenSocket listen_socket_2;
149  EXPECT_TRUE(listen_socket_2.listen(TEST_PORT_2));
150 }
151 
152 TEST(SocketTest, RelistenWithDifferentInstanceOnSamePort)
153 {
154  MockListenSocket listen_socket;
155  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
156 
157  /*
158  * You cannot listen to a port that's already being listened to.
159  */
160  MockListenSocket listen_socket_2;
161  EXPECT_FALSE(listen_socket_2.listen(TEST_PORT_1));
162 }
163 
164 TEST(SocketTest, AcceptError)
165 {
166  MockListenSocket listen_socket;
167  EXPECT_EQ(-1, listen_socket.accept());
168 }
socket.hh
ListenSocket
Definition: socket.hh:32
ListenSocket::listen
virtual bool listen(int port, bool reuse=true)
Definition: socket.cc:99
ListenSocket::accept
virtual int accept(bool nodelay=false)
Definition: socket.cc:148
TEST_PORT_1
#define TEST_PORT_1
Definition: socket.test.cc:33
MockListenSocket
Definition: socket.test.cc:41
TEST_PORT_2
#define TEST_PORT_2
Definition: socket.test.cc:34
EXPECT_EQ
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:110
EXPECT_FALSE
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:106
MockListenSocket::~MockListenSocket
~MockListenSocket()
Definition: socket.test.cc:49
ListenSocket::getfd
int getfd() const
Definition: socket.hh:72
EXPECT_TRUE
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:103
ListenSocket::islistening
bool islistening() const
Definition: socket.hh:73
ListenSocket::cleanup
static void cleanup()
Definition: socket.cc:54
TEST
TEST(SocketTest, DefaultBehavior)
Definition: socket.test.cc:55
ListenSocket::allDisabled
static bool allDisabled()
Definition: socket.cc:70
ListenSocket::disableAll
static void disableAll()
Definition: socket.cc:62
expected
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17