gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
38static const int TestPort1 = 7893;
39static const int TestPort2 = 7894;
40
41using 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
63TEST(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
75TEST(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
84TEST(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
93TEST(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
110TEST(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
122TEST(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
134TEST(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}
MockListenSocket(int port)
virtual bool listen(int port)
Definition socket.cc:197
int accept() override
Definition socket.cc:182
static void cleanup()
Definition socket.cc:74
static bool allDisabled()
Definition socket.cc:90
static void disableAll()
Definition socket.cc:82
bool islistening() const
Definition socket.hh:95
int getfd() const
Definition socket.hh:94
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
thread_local GTestLogOutput gtestLogOutput
Definition logging.cc:33
static const int TestPort2
TEST(SocketTest, DefaultBehavior)
static const int TestPort1

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0