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 #include "src/core/lib/event_engine/posix_engine/tcp_socket_utils.h"
25 #include "src/core/lib/event_engine/tcp_socket_utils.h"
26 #include "src/core/util/strerror.h"
27 #include "test/core/test_util/test_config.h"
28
29 using ::grpc_event_engine::experimental::PosixSocketWrapper;
30
31 // There is a special code path in create_socket to log errors upon EMFILE.
32 // Goal of this test is just to exercise that code path and also make sure
33 // it doesn't mess up "errno", so that we get the right error message.
TEST(LogTooManyOpenFilesTest,MainTest)34 TEST(LogTooManyOpenFilesTest, MainTest) {
35 const auto mock_socket_factory = [](int, int, int) {
36 errno = EMFILE;
37 return -1;
38 };
39 auto addr = grpc_event_engine::experimental::URIToResolvedAddress(
40 "ipv4:127.0.0.1:80");
41 ASSERT_TRUE(addr.ok());
42 PosixSocketWrapper::DSMode dsmode;
43 absl::StatusOr<PosixSocketWrapper> result =
44 PosixSocketWrapper::CreateDualStackSocket(mock_socket_factory, *addr,
45 SOCK_STREAM, AF_INET, dsmode);
46 EXPECT_FALSE(result.ok());
47 std::string emfile_message = grpc_core::StrError(EMFILE);
48 EXPECT_THAT(result.status().message(), ::testing::HasSubstr(emfile_message));
49 }
50
main(int argc,char ** argv)51 int main(int argc, char** argv) {
52 grpc::testing::TestEnvironment env(&argc, argv);
53 ::testing::InitGoogleTest(&argc, argv);
54 return RUN_ALL_TESTS();
55 }
56