| 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 <memory> |
| 20 |
#include <string> |
| 21 |
#include <utility> |
| 22 |
|
| 23 |
#include "absl/status/status.h" |
| 24 |
#include "absl/status/statusor.h" |
| 25 |
#include "absl/strings/string_view.h" |
| 26 |
#include "gtest/gtest.h" |
| 27 |
|
| 28 |
#include <grpc/support/log.h> |
| 29 |
|
| 30 |
#include "src/core/lib/config/core_configuration.h" |
| 31 |
#include "src/core/lib/gprpp/orphanable.h" |
| 32 |
#include "src/core/lib/gprpp/work_serializer.h" |
| 33 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 34 |
#include "src/core/lib/iomgr/port.h" |
| 35 |
#include "src/core/lib/resolver/resolver.h" |
| 36 |
#include "src/core/lib/resolver/resolver_factory.h" |
| 37 |
#include "src/core/lib/resolver/resolver_registry.h" |
| 38 |
#include "src/core/lib/uri/uri_parser.h" |
| 39 |
#include "test/core/util/test_config.h" |
| 40 |
|
| 41 |
static std::shared_ptr<grpc_core::WorkSerializer>* g_work_serializer; |
| 42 |
|
| 43 |
class ResultHandler : public grpc_core::Resolver::ResultHandler { |
| 44 |
public: |
| 45 |
void ReportResult(grpc_core::Resolver::Result /*result*/) override {} |
| 46 |
}; |
| 47 |
|
| 48 |
static void test_succeeds(grpc_core::ResolverFactory* factory, |
| 49 |
const char* string) { |
| 50 |
gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string, |
| 51 |
std::string(factory->scheme()).c_str()); |
| 52 |
grpc_core::ExecCtx exec_ctx; |
| 53 |
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string); |
| 54 |
if (!uri.ok()) { |
| 55 |
gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str()); |
| 56 |
ASSERT_TRUE(uri.ok()); |
| 57 |
} |
| 58 |
grpc_core::ResolverArgs args; |
| 59 |
args.uri = std::move(*uri); |
| 60 |
args.work_serializer = *g_work_serializer; |
| 61 |
args.result_handler = std::make_unique<ResultHandler>(); |
| 62 |
grpc_core::OrphanablePtr<grpc_core::Resolver> resolver = |
| 63 |
factory->CreateResolver(std::move(args)); |
| 64 |
ASSERT_NE(resolver, nullptr); |
| 65 |
resolver->StartLocked(); |
| 66 |
/* Flush ExecCtx to avoid stack-use-after-scope on on_res_arg which is |
| 67 |
* accessed in the closure on_resolution_cb */ |
| 68 |
grpc_core::ExecCtx::Get()->Flush(); |
| 69 |
} |
| 70 |
|
| 71 |
static void test_fails(grpc_core::ResolverFactory* factory, |
| 72 |
const char* string) { |
| 73 |
gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string, |
| 74 |
std::string(factory->scheme()).c_str()); |
| 75 |
grpc_core::ExecCtx exec_ctx; |
| 76 |
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string); |
| 77 |
if (!uri.ok()) { |
| 78 |
gpr_log(GPR_ERROR, "%s", uri.status().ToString().c_str()); |
| 79 |
ASSERT_TRUE(uri.ok()); |
| 80 |
} |
| 81 |
grpc_core::ResolverArgs args; |
| 82 |
args.uri = std::move(*uri); |
| 83 |
args.work_serializer = *g_work_serializer; |
| 84 |
args.result_handler = std::make_unique<ResultHandler>(); |
| 85 |
grpc_core::OrphanablePtr<grpc_core::Resolver> resolver = |
| 86 |
factory->CreateResolver(std::move(args)); |
| 87 |
ASSERT_EQ(resolver, nullptr); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(SockaddrResolverTest, MainTest) { |
| 91 |
auto work_serializer = std::make_shared<grpc_core::WorkSerializer>(); |
| 92 |
g_work_serializer = &work_serializer; |
| 93 |
|
| 94 |
grpc_core::ResolverFactory* ipv4 = grpc_core::CoreConfiguration::Get() |
| 95 |
.resolver_registry() |
| 96 |
.LookupResolverFactory("ipv4"); |
| 97 |
grpc_core::ResolverFactory* ipv6 = grpc_core::CoreConfiguration::Get() |
| 98 |
.resolver_registry() |
| 99 |
.LookupResolverFactory("ipv6"); |
| 100 |
|
| 101 |
test_fails(ipv4, "ipv4:10.2.1.1"); |
| 102 |
test_succeeds(ipv4, "ipv4:10.2.1.1:1234"); |
| 103 |
test_succeeds(ipv4, "ipv4:10.2.1.1:1234,127.0.0.1:4321"); |
| 104 |
test_fails(ipv4, "ipv4:10.2.1.1:123456"); |
| 105 |
test_fails(ipv4, "ipv4:www.google.com"); |
| 106 |
test_fails(ipv4, "ipv4:["); |
| 107 |
test_fails(ipv4, "ipv4://8.8.8.8/8.8.8.8:8888"); |
| 108 |
|
| 109 |
test_fails(ipv6, "ipv6:["); |
| 110 |
test_fails(ipv6, "ipv6:[::]"); |
| 111 |
test_succeeds(ipv6, "ipv6:[::]:1234"); |
| 112 |
test_fails(ipv6, "ipv6:[::]:123456"); |
| 113 |
test_fails(ipv6, "ipv6:www.google.com"); |
| 114 |
|
| 115 |
#ifdef GRPC_HAVE_UNIX_SOCKET |
| 116 |
grpc_core::ResolverFactory* uds = grpc_core::CoreConfiguration::Get() |
| 117 |
.resolver_registry() |
| 118 |
.LookupResolverFactory("unix"); |
| 119 |
grpc_core::ResolverFactory* uds_abstract = |
| 120 |
grpc_core::CoreConfiguration::Get() |
| 121 |
.resolver_registry() |
| 122 |
.LookupResolverFactory("unix-abstract"); |
| 123 |
|
| 124 |
test_succeeds(uds, "unix:///tmp/sockaddr_resolver_test"); |
| 125 |
test_succeeds(uds_abstract, "unix-abstract:sockaddr_resolver_test"); |
| 126 |
#endif // GRPC_HAVE_UNIX_SOCKET |
| 127 |
} |
| 128 |
|
| 129 |
int main(int argc, char** argv) { |
| 130 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 131 |
::testing::InitGoogleTest(&argc, argv); |
| 132 |
grpc::testing::TestGrpcScope grpc_scope; |
| 133 |
return RUN_ALL_TESTS(); |
| 134 |
} |