gem5  v21.1.0.2
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/gtest/logging.hh"
32 #include "base/socket.hh"
33 
34 static const int TestPort1 = 7893;
35 static const int TestPort2 = 7894;
36 
37 using namespace gem5;
38 
39 /*
40  * Socket.test tests socket.cc. It should be noted that some features of
41  * socket.cc have not been fully tested due to interaction with system-calls.
42  */
43 
45 {
46  public:
47  /*
48  * This mock Listen Socket is used to ensure the static variables are reset
49  * back to their default values after deconstruction (i.e., after a test
50  * has completed).
51  */
53  {
54  cleanup();
55  }
56 };
57 
58 TEST(SocketTest, DefaultBehavior)
59 {
60  /*
61  * Tests the default behavior where listenSocket is constructed, and is
62  * not listening to a port.
63  */
64  MockListenSocket listen_socket;
65  EXPECT_EQ(-1, listen_socket.getfd());
66  EXPECT_FALSE(listen_socket.islistening());
67  EXPECT_FALSE(listen_socket.allDisabled());
68 }
69 
70 TEST(SocketTest, DisableAll)
71 {
72  MockListenSocket listen_socket;
73  listen_socket.disableAll();
74  EXPECT_EQ(-1, listen_socket.getfd());
75  EXPECT_FALSE(listen_socket.islistening());
76  EXPECT_TRUE(listen_socket.allDisabled());
77 }
78 
79 TEST(SocketTest, ListenToPort)
80 {
81  MockListenSocket listen_socket;
82  EXPECT_TRUE(listen_socket.listen(TestPort1));
83  EXPECT_NE(-1, listen_socket.getfd());
84  EXPECT_TRUE(listen_socket.islistening());
85  EXPECT_FALSE(listen_socket.allDisabled());
86 }
87 
88 TEST(SocketTest, ListenToPortReuseFalse)
89 {
90  MockListenSocket listen_socket;
91  /*
92  * The ListenSocket object should have the same state regardless as to
93  * whether reuse is true or false (it is true by default).
94  */
95  EXPECT_TRUE(listen_socket.listen(TestPort1, false));
96  EXPECT_NE(-1, listen_socket.getfd());
97  EXPECT_TRUE(listen_socket.islistening());
98  EXPECT_FALSE(listen_socket.allDisabled());
99 }
100 
101 TEST(SocketTest, RelistenWithSameInstanceSamePort)
102 {
103  MockListenSocket listen_socket;
104  EXPECT_TRUE(listen_socket.listen(TestPort1));
105 
106  /*
107  * You cannot listen to another port if you are already listening to one.
108  */
109  gtestLogOutput.str("");
110  EXPECT_ANY_THROW(listen_socket.listen(TestPort1));
111  std::string expected = "panic: Socket already listening!\n";
112  std::string actual = gtestLogOutput.str();
113  EXPECT_EQ(expected, actual);
114 }
115 
116 TEST(SocketTest, RelistenWithSameInstanceDifferentPort)
117 {
118  MockListenSocket listen_socket;
119  EXPECT_TRUE(listen_socket.listen(TestPort1));
120 
121  /*
122  * You cannot listen to another port if you are already listening to one.
123  */
124  gtestLogOutput.str("");
125  EXPECT_ANY_THROW(listen_socket.listen(TestPort2));
126 
127  std::string expected = "panic: Socket already listening!\n";
128  std::string actual = gtestLogOutput.str();
129  EXPECT_EQ(expected, actual);
130 }
131 
132 TEST(SocketTest, RelistenWithDifferentInstanceOnDifferentPort)
133 {
134  MockListenSocket listen_socket;
135  EXPECT_TRUE(listen_socket.listen(TestPort1));
136 
137  /*
138  * You can listen to another port with a different instance.
139  */
140  MockListenSocket listen_socket_2;
141  EXPECT_TRUE(listen_socket_2.listen(TestPort2));
142 }
143 
144 TEST(SocketTest, RelistenWithDifferentInstanceOnSamePort)
145 {
146  MockListenSocket listen_socket;
147  EXPECT_TRUE(listen_socket.listen(TestPort1));
148 
149  /*
150  * You cannot listen to a port that's already being listened to.
151  */
152  MockListenSocket listen_socket_2;
153  EXPECT_FALSE(listen_socket_2.listen(TestPort1));
154 }
155 
156 TEST(SocketTest, AcceptError)
157 {
158  MockListenSocket listen_socket;
159  EXPECT_EQ(-1, listen_socket.accept());
160 }
gem5::ListenSocket::disableAll
static void disableAll()
Definition: socket.cc:63
socket.hh
logging.hh
gem5::ListenSocket
Definition: socket.hh:35
TestPort1
static const int TestPort1
Definition: socket.test.cc:34
MockListenSocket
Definition: socket.test.cc:44
MockListenSocket::~MockListenSocket
~MockListenSocket()
Definition: socket.test.cc:52
gem5::ListenSocket::listen
virtual bool listen(int port, bool reuse=true)
Definition: socket.cc:100
gem5::ListenSocket::getfd
int getfd() const
Definition: socket.hh:75
gem5::ListenSocket::allDisabled
static bool allDisabled()
Definition: socket.cc:71
TEST
TEST(SocketTest, DefaultBehavior)
Definition: socket.test.cc:58
gem5::gtestLogOutput
thread_local GTestLogOutput gtestLogOutput
Definition: logging.cc:33
gem5::ListenSocket::accept
virtual int accept(bool nodelay=false)
Definition: socket.cc:149
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::ListenSocket::islistening
bool islistening() const
Definition: socket.hh:76
TestPort2
static const int TestPort2
Definition: socket.test.cc:35
expected
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })

Generated on Tue Sep 21 2021 12:24:57 for gem5 by doxygen 1.8.17