1 // Copyright 2023 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <errno.h>
16 #include <sys/socket.h>
17
18 #include <string>
19
20 #include "absl/status/status.h"
21 #include "absl/status/statusor.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 #include "src/core/lib/event_engine/posix_engine/tcp_socket_utils.h"
26 #include "src/core/lib/event_engine/tcp_socket_utils.h"
27 #include "src/core/lib/gprpp/strerror.h"
28 #include "test/core/util/test_config.h"
29
30 using ::grpc_event_engine::experimental::PosixSocketWrapper;
31
32 // There is a special code path in create_socket to log errors upon EMFILE.
33 // Goal of this test is just to exercise that code path and also make sure
34 // it doesn't mess up "errno", so that we get the right error message.
TEST(LogTooManyOpenFilesTest,MainTest)35 TEST(LogTooManyOpenFilesTest, MainTest) {
36 const auto mock_socket_factory = [](int, int, int) {
37 errno = EMFILE;
38 return -1;
39 };
40 auto addr = grpc_event_engine::experimental::URIToResolvedAddress(
41 "ipv4:127.0.0.1:80");
42 ASSERT_TRUE(addr.ok());
43 PosixSocketWrapper::DSMode dsmode;
44 absl::StatusOr<PosixSocketWrapper> result =
45 PosixSocketWrapper::CreateDualStackSocket(mock_socket_factory, *addr,
46 SOCK_STREAM, AF_INET, dsmode);
47 EXPECT_FALSE(result.ok());
48 std::string emfile_message = grpc_core::StrError(EMFILE);
49 EXPECT_THAT(result.status().message(), ::testing::HasSubstr(emfile_message));
50 }
51
main(int argc,char ** argv)52 int main(int argc, char** argv) {
53 grpc::testing::TestEnvironment env(&argc, argv);
54 ::testing::InitGoogleTest(&argc, argv);
55 return RUN_ALL_TESTS();
56 }
57