sockaddr_resolver_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <string.h>
20 
21 #include <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
25 
31 
32 static std::shared_ptr<grpc_core::WorkSerializer>* g_work_serializer;
33 
35  public:
36  void ReportResult(grpc_core::Resolver::Result /*result*/) override {}
37 };
38 
40  const char* string) {
41  gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
42  std::string(factory->scheme()).c_str());
45  if (!uri.ok()) {
46  gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
47  GPR_ASSERT(uri.ok());
48  }
50  args.uri = std::move(*uri);
51  args.work_serializer = *g_work_serializer;
52  args.result_handler = absl::make_unique<ResultHandler>();
54  factory->CreateResolver(std::move(args));
55  GPR_ASSERT(resolver != nullptr);
56  resolver->StartLocked();
57  /* Flush ExecCtx to avoid stack-use-after-scope on on_res_arg which is
58  * accessed in the closure on_resolution_cb */
60 }
61 
63  const char* string) {
64  gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
65  std::string(factory->scheme()).c_str());
68  if (!uri.ok()) {
69  gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str());
70  GPR_ASSERT(uri.ok());
71  }
73  args.uri = std::move(*uri);
74  args.work_serializer = *g_work_serializer;
75  args.result_handler = absl::make_unique<ResultHandler>();
77  factory->CreateResolver(std::move(args));
78  GPR_ASSERT(resolver == nullptr);
79 }
80 
81 int main(int argc, char** argv) {
83  grpc_init();
84 
85  auto work_serializer = std::make_shared<grpc_core::WorkSerializer>();
86  g_work_serializer = &work_serializer;
87 
90  .LookupResolverFactory("ipv4");
93  .LookupResolverFactory("ipv6");
94 
95  test_fails(ipv4, "ipv4:10.2.1.1");
96  test_succeeds(ipv4, "ipv4:10.2.1.1:1234");
97  test_succeeds(ipv4, "ipv4:10.2.1.1:1234,127.0.0.1:4321");
98  test_fails(ipv4, "ipv4:10.2.1.1:123456");
99  test_fails(ipv4, "ipv4:www.google.com");
100  test_fails(ipv4, "ipv4:[");
101  test_fails(ipv4, "ipv4://8.8.8.8/8.8.8.8:8888");
102 
103  test_fails(ipv6, "ipv6:[");
104  test_fails(ipv6, "ipv6:[::]");
105  test_succeeds(ipv6, "ipv6:[::]:1234");
106  test_fails(ipv6, "ipv6:[::]:123456");
107  test_fails(ipv6, "ipv6:www.google.com");
108 
109 #ifdef GRPC_HAVE_UNIX_SOCKET
112  .LookupResolverFactory("unix");
113  grpc_core::ResolverFactory* uds_abstract =
116  .LookupResolverFactory("unix-abstract");
117 
118  test_succeeds(uds, "unix:///tmp/sockaddr_resolver_test");
119  test_succeeds(uds_abstract, "unix-abstract:sockaddr_resolver_test");
120 #endif // GRPC_HAVE_UNIX_SOCKET
121 
122  grpc_shutdown();
123 
124  return 0;
125 }
grpc_core::ResolverRegistry::LookupResolverFactory
ResolverFactory * LookupResolverFactory(absl::string_view scheme) const
Definition: resolver_registry.cc:108
log.h
core_configuration.h
grpc_core::ResolverFactory::CreateResolver
virtual OrphanablePtr< Resolver > CreateResolver(ResolverArgs args) const =0
Returns a new resolver instance.
generate.env
env
Definition: generate.py:37
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
main
int main(int argc, char **argv)
Definition: sockaddr_resolver_test.cc:81
ResultHandler
Definition: dns_resolver_cooldown_test.cc:202
grpc_core::URI::Parse
static absl::StatusOr< URI > Parse(absl::string_view uri_text)
Definition: uri_parser.cc:209
string_util.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::CoreConfiguration::Get
static const CoreConfiguration & Get()
Definition: core_configuration.h:82
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
work_serializer.h
grpc_core::Resolver::Result
Results returned by the resolver.
Definition: resolver/resolver.h:56
grpc.h
grpc_core::ResolverArgs
Definition: resolver_factory.h:41
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::Resolver::ResultHandler
Definition: resolver/resolver.h:84
grpc_core::ExecCtx
Definition: exec_ctx.h:97
resolver_registry.h
test_config.h
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
test_succeeds
static void test_succeeds(grpc_core::ResolverFactory *factory, const char *string)
Definition: sockaddr_resolver_test.cc:39
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
grpc_core::OrphanablePtr
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:64
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
ResultHandler::ReportResult
void ReportResult(grpc_core::Resolver::Result) override
Reports a result to the channel.
Definition: sockaddr_resolver_test.cc:36
grpc_core::CoreConfiguration::resolver_registry
const ResolverRegistry & resolver_registry() const
Definition: core_configuration.h:157
channel_args.h
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
g_work_serializer
static std::shared_ptr< grpc_core::WorkSerializer > * g_work_serializer
Definition: sockaddr_resolver_test.cc:32
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_core::ResolverFactory::scheme
virtual absl::string_view scheme() const =0
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
test_fails
static void test_fails(grpc_core::ResolverFactory *factory, const char *string)
Definition: sockaddr_resolver_test.cc:62
grpc_core::ResolverFactory
Definition: resolver_factory.h:54
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
absl::StatusOr::status
const Status & status() const &
Definition: abseil-cpp/absl/status/statusor.h:678


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:13