gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Bobby R. Bruce
29  */
30 
31 #include <gtest/gtest.h>
32 
33 #include "base/socket.hh"
34 
35 #define TEST_PORT_1 7893
36 #define TEST_PORT_2 7894
37 
38 /*
39  * Socket.test tests socket.cc. It should be noted that some features of
40  * socket.cc have not been fully tested due to interaction with system-calls.
41  */
42 
44 {
45  public:
46  /*
47  * This mock Listen Socket is used to ensure the static variables are reset
48  * back to their default values after deconstruction (i.e., after a test
49  * has completed).
50  */
52  {
53  cleanup();
54  }
55 };
56 
57 TEST(SocketTest, DefaultBehavior)
58 {
59  /*
60  * Tests the default behavior where listenSocket is constructed, and is
61  * not listening to a port.
62  */
63  MockListenSocket listen_socket;
64  EXPECT_EQ(-1, listen_socket.getfd());
65  EXPECT_FALSE(listen_socket.islistening());
66  EXPECT_FALSE(listen_socket.allDisabled());
67 }
68 
69 TEST(SocketTest, DisableAll)
70 {
71  MockListenSocket listen_socket;
72  listen_socket.disableAll();
73  EXPECT_EQ(-1, listen_socket.getfd());
74  EXPECT_FALSE(listen_socket.islistening());
75  EXPECT_TRUE(listen_socket.allDisabled());
76 }
77 
78 TEST(SocketTest, ListenToPort)
79 {
80  MockListenSocket listen_socket;
81  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
82  EXPECT_NE(-1, listen_socket.getfd());
83  EXPECT_TRUE(listen_socket.islistening());
84  EXPECT_FALSE(listen_socket.allDisabled());
85 }
86 
87 TEST(SocketTest, ListenToPortReuseFalse)
88 {
89  MockListenSocket listen_socket;
90  /*
91  * The ListenSocket object should have the same state regardless as to
92  * whether reuse is true or false (it is true by default).
93  */
94  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1, false));
95  EXPECT_NE(-1, listen_socket.getfd());
96  EXPECT_TRUE(listen_socket.islistening());
97  EXPECT_FALSE(listen_socket.allDisabled());
98 }
99 
100 TEST(SocketTest, RelistenWithSameInstanceSamePort)
101 {
102  MockListenSocket listen_socket;
103  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
104 
105  /*
106  * You cannot listen to another port if you are already listening to one.
107  */
108  testing::internal::CaptureStderr();
109  EXPECT_ANY_THROW(listen_socket.listen(TEST_PORT_1));
110  std::string expected = "panic: Socket already listening!\n";
111  std::string actual = testing::internal::GetCapturedStderr().substr();
112 
113  /*
114  * The GoogleExitLogger will output using the following:
115  * `std::cerr << loc.file << ":" << loc.line << ": " << s;`
116  * As we do not care about the file and line where the error originated
117  * (this may change, and it shouldn't break the test when this happens),
118  * we strip out the leading `<file>:<line>: ` (we simply remove everything
119  * prior to two characters after the second colon in the string).
120  */
121  actual = actual.substr(actual.find(":", actual.find(":") + 1) + 2);
122  EXPECT_EQ(expected, actual);
123 }
124 
125 TEST(SocketTest, RelistenWithSameInstanceDifferentPort)
126 {
127  MockListenSocket listen_socket;
128  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
129 
130  /*
131  * You cannot listen to another port if you are already listening to one.
132  */
133  testing::internal::CaptureStderr();
134  EXPECT_ANY_THROW(listen_socket.listen(TEST_PORT_2));
135 
136  std::string expected = "panic: Socket already listening!\n";
137  std::string actual = testing::internal::GetCapturedStderr().substr();
138  actual = actual.substr(actual.find(":", actual.find(":") + 1) + 2);
139  EXPECT_EQ(expected, actual);
140 }
141 
142 TEST(SocketTest, RelistenWithDifferentInstanceOnDifferentPort)
143 {
144  MockListenSocket listen_socket;
145  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
146 
147  /*
148  * You can listen to another port with a different instance.
149  */
150  MockListenSocket listen_socket_2;
151  EXPECT_TRUE(listen_socket_2.listen(TEST_PORT_2));
152 }
153 
154 TEST(SocketTest, RelistenWithDifferentInstanceOnSamePort)
155 {
156  MockListenSocket listen_socket;
157  EXPECT_TRUE(listen_socket.listen(TEST_PORT_1));
158 
159  /*
160  * You cannot listen to a port that's already being listened to.
161  */
162  MockListenSocket listen_socket_2;
163  EXPECT_FALSE(listen_socket_2.listen(TEST_PORT_1));
164 }
165 
166 TEST(SocketTest, AcceptError)
167 {
168  MockListenSocket listen_socket;
169  EXPECT_EQ(-1, listen_socket.accept());
170 }
virtual bool listen(int port, bool reuse=true)
Definition: socket.cc:102
bool islistening() const
Definition: socket.hh:66
#define TEST_PORT_1
Definition: socket.test.cc:35
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:105
static bool allDisabled()
Definition: socket.cc:73
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })
int getfd() const
Definition: socket.hh:65
#define TEST_PORT_2
Definition: socket.test.cc:36
virtual int accept(bool nodelay=false)
Definition: socket.cc:148
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:108
static void disableAll()
Definition: socket.cc:65
TEST(SocketTest, DefaultBehavior)
Definition: socket.test.cc:57
static void cleanup()
Definition: socket.cc:57
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:112

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13