Skip to content

Commit 54d4e46

Browse files
committed
issue: HPCINFRA-3949 simplify remote address conf
Simplify test configuration by using supplied addresses from --addr flag for all connection tests, eliminating gateway detection complexity. Key improvements: 1. Use supplied addresses for connection tests - tcp_event.ti_2: Connect to localhost (127.0.0.1/::1) port 8889 - udp_connect.ti_2: Connect to server_addr from --addr flag - Simpler configuration: only remote address parameter (-r) needed 2. Improve semantic correctness - udp_connect.ti_3: Use remote_unreachable_addr instead of bogus_addr to match test description ("connect to unreachable ip") Changes: - tcp_event.ti_2: Use localhost port 8889 for connection refusal test - udp_connect.ti_2: Use server_addr instead of gateway address - udp_connect.ti_3: Use remote_unreachable_addr instead of bogus_addr - Simplify main.cc configuration logic Signed-off-by: Tomer Cabouly <[email protected]>
1 parent 5412ecf commit 54d4e46

File tree

11 files changed

+84
-118
lines changed

11 files changed

+84
-118
lines changed

tests/gtest/common/base.cc

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ uint16_t test_base::m_port = 0;
1313
int test_base::m_family = PF_INET;
1414
int test_base::m_break_signal = 0;
1515

16+
static void convert_and_copy_address(const sockaddr_store_t &source, sockaddr_store_t &dest,
17+
sa_family_t target_family)
18+
{
19+
sa_family_t source_family = ((struct sockaddr *)&source)->sa_family;
20+
21+
if (source_family != target_family) {
22+
// Only IPv4→IPv6 conversion is currently supported
23+
ASSERT_EQ(AF_INET, source_family) << "Address conversion requires IPv4 source";
24+
ASSERT_EQ(AF_INET6, target_family) << "Address conversion requires IPv6 target";
25+
26+
memset(&dest, 0, sizeof(dest));
27+
((struct sockaddr *)&dest)->sa_family = target_family;
28+
sys_ipv4_to_ipv6(&((struct sockaddr_in *)&source)->sin_addr,
29+
&((struct sockaddr_in6 *)&dest)->sin6_addr);
30+
sys_set_port((struct sockaddr *)&dest, sys_get_port((struct sockaddr *)&source));
31+
} else {
32+
memcpy(&dest, &source, sizeof(dest));
33+
}
34+
}
35+
1636
test_base::test_base()
1737
{
1838
m_port = gtest_conf.port;
@@ -23,38 +43,9 @@ test_base::test_base()
2343
m_family = PF_INET6;
2444
}
2545

26-
if (((struct sockaddr *)&gtest_conf.client_addr)->sa_family != m_family) {
27-
memset(&client_addr, 0, sizeof(client_addr));
28-
((struct sockaddr *)&client_addr)->sa_family = m_family;
29-
sys_ipv4_to_ipv6(&((struct sockaddr_in *)&gtest_conf.client_addr)->sin_addr,
30-
&((struct sockaddr_in6 *)&client_addr)->sin6_addr);
31-
sys_set_port((struct sockaddr *)&client_addr,
32-
sys_get_port((struct sockaddr *)&gtest_conf.client_addr));
33-
} else {
34-
memcpy(&client_addr, &gtest_conf.client_addr, sizeof(client_addr));
35-
}
36-
37-
if (((struct sockaddr *)&gtest_conf.server_addr)->sa_family != m_family) {
38-
memset(&server_addr, 0, sizeof(server_addr));
39-
((struct sockaddr *)&server_addr)->sa_family = m_family;
40-
sys_ipv4_to_ipv6(&((struct sockaddr_in *)&gtest_conf.server_addr)->sin_addr,
41-
&((struct sockaddr_in6 *)&server_addr)->sin6_addr);
42-
sys_set_port((struct sockaddr *)&server_addr,
43-
sys_get_port((struct sockaddr *)&gtest_conf.server_addr));
44-
} else {
45-
memcpy(&server_addr, &gtest_conf.server_addr, sizeof(server_addr));
46-
}
47-
48-
if (((struct sockaddr *)&gtest_conf.remote_addr)->sa_family != m_family) {
49-
memset(&remote_addr, 0, sizeof(remote_addr));
50-
((struct sockaddr *)&remote_addr)->sa_family = m_family;
51-
sys_ipv4_to_ipv6(&((struct sockaddr_in *)&gtest_conf.remote_addr)->sin_addr,
52-
&((struct sockaddr_in6 *)&remote_addr)->sin6_addr);
53-
sys_set_port((struct sockaddr *)&remote_addr,
54-
sys_get_port((struct sockaddr *)&gtest_conf.remote_addr));
55-
} else {
56-
memcpy(&remote_addr, &gtest_conf.remote_addr, sizeof(remote_addr));
57-
}
46+
convert_and_copy_address(gtest_conf.client_addr, client_addr, m_family);
47+
convert_and_copy_address(gtest_conf.server_addr, server_addr, m_family);
48+
convert_and_copy_address(gtest_conf.remote_unreachable_addr, remote_unreachable_addr, m_family);
5849

5950
memset(&bogus_addr, 0, sizeof(bogus_addr));
6051
((struct sockaddr *)&bogus_addr)->sa_family = m_family;
@@ -67,8 +58,6 @@ test_base::test_base()
6758
&(((struct sockaddr_in6 *)&bogus_addr)->sin6_addr));
6859
}
6960

70-
def_gw_exists = gtest_conf.def_gw_exists;
71-
7261
m_efd_signal = 0;
7362
m_efd = eventfd(m_efd_signal, 0);
7463

tests/gtest/common/base.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ class test_base {
7979

8080
sockaddr_store_t client_addr;
8181
sockaddr_store_t server_addr;
82-
sockaddr_store_t remote_addr;
82+
sockaddr_store_t remote_unreachable_addr;
8383
sockaddr_store_t bogus_addr;
84-
bool def_gw_exists;
8584
static uint16_t m_port;
8685
static int m_family;
8786

tests/gtest/common/def.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ struct gtest_configure_t {
104104
int random_seed;
105105
sockaddr_store_t client_addr;
106106
sockaddr_store_t server_addr;
107-
sockaddr_store_t remote_addr;
107+
sockaddr_store_t remote_unreachable_addr;
108108
uint16_t port;
109-
bool def_gw_exists;
110109
};
111110

112111
#endif /* TESTS_GTEST_COMMON_DEF_H_ */

tests/gtest/common/sys.cc

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -157,39 +157,6 @@ int sys_dev2addr(const char *dev, struct sockaddr *addr)
157157
return rc;
158158
}
159159

160-
bool sys_gateway(struct sockaddr *addr, sa_family_t family)
161-
{
162-
sockaddr_store_t temp_addr;
163-
bool found = false;
164-
char line[256];
165-
const char cmd4[] = "route -n | grep 'UG[ \t]' | awk '{print $2}'";
166-
const char cmd6[] = "route -6 -n | grep 'UG[ \t]' | awk '{print $2}'";
167-
const char *cmd_ptr = (family == AF_INET ? cmd4 : cmd6);
168-
169-
FILE *file = popen(cmd_ptr, "r");
170-
if (!file) {
171-
log_warn("Unable to execute '%s'.\n", cmd_ptr);
172-
return false;
173-
}
174-
175-
while (fgets(line, sizeof(line), file) != NULL && !found) {
176-
size_t len = strlen(line);
177-
if (line[len - 1] == '\n' || line[len - 1] == '\r') {
178-
line[len - 1] = 0;
179-
}
180-
sys_str2addr(line, &temp_addr.addr, false);
181-
found = (temp_addr.addr.sa_family == family);
182-
if (found) {
183-
sys_str2addr(line, addr, false);
184-
log_trace("%s found gateway ip: %s\n", line, sys_addr2str(addr));
185-
}
186-
}
187-
188-
pclose(file);
189-
190-
return found;
191-
}
192-
193160
// Converts address to sockaddr structure. On an error, addr->sa_family is AF_UNSPEC.
194161
void sys_str2addr(const char *buf, struct sockaddr *addr, bool port)
195162
{

tests/gtest/common/sys.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ char *sys_addr2dev(const struct sockaddr *addr, char *buf, size_t size);
111111

112112
int sys_dev2addr(const char *dev, struct sockaddr *addr);
113113

114-
bool sys_gateway(struct sockaddr *addr, sa_family_t family);
115-
116114
void sys_str2addr(const char *buf, struct sockaddr *addr, bool port = true);
117115

118116
static INLINE char *sys_addr2str(const struct sockaddr *addr, bool port = true)

tests/gtest/main.cc

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,47 @@ static int _def_config(void)
5252

5353
sys_str2addr("0.0.0.0[0]", (struct sockaddr *)&gtest_conf.client_addr, true);
5454
sys_str2addr("0.0.0.0[0]", (struct sockaddr *)&gtest_conf.server_addr, true);
55-
sys_str2addr("0.0.0.0[8888]", (struct sockaddr *)&gtest_conf.remote_addr, true);
55+
sys_str2addr("192.0.2.1[8888]", (struct sockaddr *)&gtest_conf.remote_unreachable_addr, true);
5656

5757
gtest_conf.port = 55555;
5858

5959
return rc;
6060
}
6161

62-
static void set_def_remote_address()
62+
static void set_def_remote_address(bool user_defined_unreachable)
6363
{
64-
gtest_conf.def_gw_exists = sys_gateway((struct sockaddr *)&gtest_conf.remote_addr,
65-
gtest_conf.server_addr.addr.sa_family);
66-
if (gtest_conf.server_addr.addr.sa_family == AF_INET6) {
67-
if (!gtest_conf.def_gw_exists) {
68-
sys_str2addr("::[8888]", (struct sockaddr *)&gtest_conf.remote_addr, true);
69-
} else {
70-
gtest_conf.remote_addr.addr6.sin6_port = htons(8888);
71-
}
64+
sa_family_t family = gtest_conf.server_addr.addr.sa_family;
65+
66+
// Set default unreachable address for IPv6 if user didn't provide one
67+
if (family == AF_INET6 && !user_defined_unreachable) {
68+
// RFC 3849: 2001:db8::/32 is IPv6 documentation prefix (equivalent to 192.0.2.0/24 for
69+
// IPv4)
70+
sys_str2addr("2001:db8::1[8888]", (struct sockaddr *)&gtest_conf.remote_unreachable_addr,
71+
true);
72+
}
73+
74+
// If user provided an address without a port, default to 8888
75+
if (user_defined_unreachable &&
76+
sys_get_port((struct sockaddr *)&gtest_conf.remote_unreachable_addr) == 0) {
77+
sys_set_port((struct sockaddr *)&gtest_conf.remote_unreachable_addr, 8888);
7278
}
7379
}
7480

7581
static int _set_config(int argc, char **argv)
7682
{
7783
int rc = 0;
7884
static struct option long_options[] = {
79-
{"addr", required_argument, 0, 'a'}, {"if", required_argument, 0, 'i'},
80-
{"remote", required_argument, 0, 'r'}, {"port", required_argument, 0, 'p'},
81-
{"random", required_argument, 0, 's'}, {"debug", required_argument, 0, 'd'},
85+
{"addr", required_argument, 0, 'a'},
86+
{"if", required_argument, 0, 'i'},
87+
{"remote-non-routable", required_argument, 0, 'r'},
88+
{"port", required_argument, 0, 'p'},
89+
{"random", required_argument, 0, 's'},
90+
{"debug", required_argument, 0, 'd'},
8291
{"help", no_argument, 0, 'h'},
8392
};
8493
int op;
8594
int option_index;
86-
bool user_defined_remote = false;
95+
bool user_defined_unreachable = false;
8796

8897
while ((op = getopt_long(argc, argv, "a:i:r:p:d:h", long_options, &option_index)) != -1) {
8998
switch (op) {
@@ -147,12 +156,12 @@ static int _set_config(int argc, char **argv)
147156
}
148157
} break;
149158
case 'r': {
150-
rc = sys_get_addr(optarg, (struct sockaddr *)&gtest_conf.remote_addr);
159+
rc = sys_get_addr(optarg, (struct sockaddr *)&gtest_conf.remote_unreachable_addr);
151160
if (rc < 0) {
152161
rc = -EINVAL;
153162
log_fatal("Failed to resolve ip address %s\n", optarg);
154163
} else {
155-
user_defined_remote = true;
164+
user_defined_unreachable = true;
156165
}
157166
} break;
158167
case 'p':
@@ -195,16 +204,15 @@ static int _set_config(int argc, char **argv)
195204
srand(gtest_conf.random_seed);
196205
sys_set_port((struct sockaddr *)&gtest_conf.server_addr, gtest_conf.port);
197206

198-
if (!user_defined_remote) {
199-
set_def_remote_address();
200-
}
207+
set_def_remote_address(user_defined_unreachable);
201208

202209
log_info("CONFIGURATION:\n");
203210
log_info("log level: %d\n", gtest_conf.log_level);
204211
log_info("seed: %d\n", gtest_conf.random_seed);
205212
log_info("client ip: %s\n", sys_addr2str((struct sockaddr *)&gtest_conf.client_addr));
206213
log_info("server ip: %s\n", sys_addr2str((struct sockaddr *)&gtest_conf.server_addr));
207-
log_info("remote ip: %s\n", sys_addr2str((struct sockaddr *)&gtest_conf.remote_addr));
214+
log_info("remote unreachable ip: %s\n",
215+
sys_addr2str((struct sockaddr *)&gtest_conf.remote_unreachable_addr));
208216
log_info("port: %d\n", gtest_conf.port);
209217
}
210218

@@ -214,13 +222,13 @@ static int _set_config(int argc, char **argv)
214222
static void _usage(void)
215223
{
216224
printf("Usage: gtest [options]\n"
217-
"\t--addr,-a <ip,ip> IP address client,server\n"
218-
"\t--if,-i <ip,ip> Interface client,server\n"
219-
"\t--remote,-r <ip> IP address remote\n"
220-
"\t--port,-p <num> Listen/connect to port <num> (default %d).\n"
221-
"\t--random,-s <count> Seed (default %d).\n"
222-
"\t--debug,-d <level> Output verbose level (default: %d).\n"
223-
"\t--help,-h Print help and exit\n",
225+
"\t--addr,-a <ip,ip> IP address client,server\n"
226+
"\t--if,-i <ip,ip> Interface client,server\n"
227+
"\t--remote-unreachable,-r <ip> IP address not reachable\n"
228+
"\t--port,-p <num> Listen/connect to port <num> (default %d).\n"
229+
"\t--random,-s <count> Seed (default %d).\n"
230+
"\t--debug,-d <level> Output verbose level (default: %d).\n"
231+
"\t--help,-h Print help and exit\n",
224232

225233
gtest_conf.port, gtest_conf.random_seed, gtest_conf.log_level);
226234
exit(0);

tests/gtest/tcp/tcp_bind.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ TEST_F(tcp_bind, ti_2)
115115
int rc = EOK;
116116
int fd;
117117

118-
SKIP_TRUE(def_gw_exists, "No Default Gateway");
119-
120118
fd = tcp_base::sock_create();
121119
ASSERT_LE(0, fd);
122120

123121
errno = EOK;
124-
rc = bind(fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
122+
rc = bind(fd, (struct sockaddr *)&remote_unreachable_addr, sizeof(remote_unreachable_addr));
125123
EXPECT_EQ(EADDRNOTAVAIL, errno);
126124
EXPECT_GT(0, rc);
127125

tests/gtest/tcp/tcp_connect.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ TEST_F(tcp_connect, DISABLED_ti_2)
5555
int rc = EOK;
5656
int fd;
5757
int i;
58+
sockaddr_store_t connect_addr;
5859

5960
fd = socket(m_family, SOCK_STREAM, IPPROTO_IP);
6061
ASSERT_LE(0, fd);
@@ -63,14 +64,18 @@ TEST_F(tcp_connect, DISABLED_ti_2)
6364
ASSERT_EQ(EOK, errno);
6465
ASSERT_EQ(0, rc);
6566

67+
// Use server_addr with port 8889 (expecting no listener and ECONNREFUSED)
68+
memcpy(&connect_addr, &server_addr, sizeof(connect_addr));
69+
sys_set_port((struct sockaddr *)&connect_addr, 8889);
70+
6671
for (i = 0; i < 10; i++) {
67-
rc = connect(fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
72+
rc = connect(fd, (struct sockaddr *)&connect_addr, sizeof(connect_addr));
6873
ASSERT_TRUE(ECONNREFUSED == errno || ETIMEDOUT == errno) << "connect() attempt = " << i;
6974
ASSERT_EQ((-1), rc) << "connect() attempt = " << i;
7075
usleep(500);
7176
if (ETIMEDOUT == errno) {
7277
log_warn("Routing issue, consider another remote address instead of %s\n",
73-
sys_addr2str((struct sockaddr *)&remote_addr));
78+
sys_addr2str((struct sockaddr *)&connect_addr));
7479
break;
7580
}
7681
}

tests/gtest/tcp/tcp_event.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ TEST_F(tcp_event, ti_2)
4141
int rc = EOK;
4242
int fd;
4343
struct epoll_event event;
44+
sockaddr_store_t addr;
4445

45-
SKIP_TRUE(def_gw_exists, "No Default Gateway");
46+
static const std::string FREE_PORT = std::to_string(8889);
47+
const std::string localhost =
48+
(m_family == AF_INET6) ? "::1[" + FREE_PORT + "]" : "127.0.0.1[" + FREE_PORT + "]";
49+
sys_str2addr(localhost.c_str(), (struct sockaddr *)&addr, true);
4650

4751
fd = tcp_base::sock_create_nb();
4852
ASSERT_LE(0, fd);
4953

50-
rc = connect(fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
54+
rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
5155
ASSERT_EQ(EINPROGRESS, errno);
5256
ASSERT_EQ((-1), rc);
5357

@@ -92,7 +96,7 @@ TEST_F(tcp_event, DISABLED_ti_4)
9296
fd = tcp_base::sock_create_nb();
9397
ASSERT_LE(0, fd);
9498

95-
rc = connect(fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
99+
rc = connect(fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
96100
ASSERT_EQ(EINPROGRESS, errno);
97101
ASSERT_EQ((-1), rc);
98102

tests/gtest/udp/udp_bind.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ TEST_F(udp_bind, ti_2)
110110
int rc = EOK;
111111
int fd;
112112

113-
SKIP_TRUE(def_gw_exists, "No Default Gateway");
114-
115113
fd = udp_base::sock_create();
116114
ASSERT_LE(0, fd);
117115

118116
errno = EOK;
119-
rc = bind(fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
117+
rc = bind(fd, (struct sockaddr *)&remote_unreachable_addr, sizeof(remote_unreachable_addr));
120118
EXPECT_EQ(EADDRNOTAVAIL, errno);
121119
EXPECT_GT(0, rc);
122120

0 commit comments

Comments
 (0)