Skip to content

Commit

Permalink
Merge pull request #1843 from wwbmmm/fix-uds-ut-on-mac
Browse files Browse the repository at this point in the history
Fix UDS ut failed on MacOS
  • Loading branch information
jamesge authored Jul 19, 2022
2 parents f7d897b + 30dfef1 commit 0e10bdb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/butil/details/extended_endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ class ExtendedEndPoint {
eep->_u.in6.sin6_flowinfo = 0u;
eep->_u.in6.sin6_scope_id = 0u;
eep->_socklen = sizeof(_u.in6);
#if defined(OS_MACOSX)
eep->_u.in6.sin6_len = eep->_socklen;
#endif
}
} else if (sp.starts_with("unix:")) { // ignore port
sp.remove_prefix(5); // remove `unix:'
Expand All @@ -174,6 +177,9 @@ class ExtendedEndPoint {
int size = sp.copy(eep->_u.un.sun_path, sp.size());
eep->_u.un.sun_path[size] = '\0';
eep->_socklen = offsetof(sockaddr_un, sun_path) + size + 1;
#if defined(OS_MACOSX)
eep->_u.un.sun_len = eep->_socklen;
#endif
}
}
if (eep) {
Expand Down Expand Up @@ -374,4 +380,4 @@ inline bool GlobalEndPointSet::Equals::operator()(ExtendedEndPoint* const& p1, E
} // namespace details
} // namespace butil

#endif // BUTIL_DETAILS_EXTENDED_ENDPOINT_H
#endif // BUTIL_DETAILS_EXTENDED_ENDPOINT_H
23 changes: 10 additions & 13 deletions test/endpoint_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,6 @@ void* server_proc(void* arg) {
sockaddr_storage ss;
socklen_t len = sizeof(ss);
int fd = accept(listen_fd, (sockaddr*)&ss, &len);
if (fd > 0) {
close(fd);
}
return (void*)(int64_t)fd;
}

Expand Down Expand Up @@ -190,7 +187,9 @@ static void test_listen_connect(const std::string& server_addr, const std::strin

void* ret = nullptr;
pthread_join(pid, &ret);
ASSERT_GT((int64_t)ret, 0);
int server_fd = (int)(int64_t)ret;
ASSERT_GT(server_fd, 0);
close(server_fd);
close(listen_fd);
}

Expand Down Expand Up @@ -238,12 +237,12 @@ TEST(EndPointTest, unix_socket) {
ASSERT_EQ(0, butil::str2endpoint("unix://a.sock", 123, &point));
ASSERT_EQ(std::string("unix://a.sock"), butil::endpoint2str(point).c_str());

ASSERT_EQ(-1, butil::str2endpoint("unix:tooloooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooooooooooooooooooooooooooooooooooooooooooooooong.sock", &point));
ASSERT_EQ(0, butil::str2endpoint(" unix:loooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooooooooooooooooooooooooooooooooooooooooooooooong.sock", &point));
ASSERT_EQ(std::string("unix:loooooooooooooooooooooooooooooooooooooooooooooooooooo"
"ooooooooooooooooooooooooooooooooooooooooooooooong.sock"), butil::endpoint2str(point).c_str());
std::string long_path = "unix:";
long_path.append(sizeof(sockaddr_un::sun_path) - 1, 'a');
ASSERT_EQ(0, butil::str2endpoint(long_path.c_str(), &point));
ASSERT_EQ(long_path, butil::endpoint2str(point).c_str());
long_path.push_back('a');
ASSERT_EQ(-1, butil::str2endpoint(long_path.c_str(), &point));
char buf[128] = {0}; // braft use this size of buffer
size_t ret = snprintf(buf, sizeof(buf), "%s:%d", butil::endpoint2str(point).c_str(), INT_MAX);
ASSERT_LT(ret, sizeof(buf) - 1);
Expand Down Expand Up @@ -383,9 +382,7 @@ TEST(EndPointTest, endpoint_sockaddr_conv_ipv6) {

in6_addr expect_in6_addr;
bzero(&expect_in6_addr, sizeof(expect_in6_addr));
expect_in6_addr.__in6_u.__u6_addr8[15] = 1;
// jge: mac monterey上应该这样,但准确判定条件不明
//expect_in6_addr.__u6_addr.__u6_addr8[15] = 1;
expect_in6_addr.s6_addr[15] = 1;

sockaddr_storage ss;
const sockaddr_in6* sa6 = (sockaddr_in6*) &ss;
Expand Down

0 comments on commit 0e10bdb

Please sign in to comment.