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

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17