| 1 |
// Copyright 2021 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 <memory> |
| 16 |
#include <string> |
| 17 |
#include <utility> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "absl/status/status.h" |
| 21 |
#include "absl/status/statusor.h" |
| 22 |
#include "absl/strings/string_view.h" |
| 23 |
#include "gtest/gtest.h" |
| 24 |
|
| 25 |
#include "src/core/lib/config/core_configuration.h" |
| 26 |
#include "src/core/lib/gprpp/orphanable.h" |
| 27 |
#include "src/core/lib/iomgr/port.h" |
| 28 |
#include "src/core/lib/iomgr/resolved_address.h" |
| 29 |
#include "src/core/lib/resolver/resolver.h" |
| 30 |
#include "src/core/lib/resolver/resolver_factory.h" |
| 31 |
#include "src/core/lib/resolver/server_address.h" |
| 32 |
#include "src/core/lib/uri/uri_parser.h" |
| 33 |
#include "test/core/util/test_config.h" |
| 34 |
|
| 35 |
#ifdef GRPC_HAVE_UNIX_SOCKET |
| 36 |
|
| 37 |
#include <sys/socket.h> |
| 38 |
#include <sys/un.h> |
| 39 |
|
| 40 |
#include <grpc/grpc.h> |
| 41 |
#include <grpc/support/log.h> |
| 42 |
|
| 43 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 44 |
#include "src/core/lib/resolver/resolver_registry.h" |
| 45 |
|
| 46 |
// Registers the factory with `grpc_core::ResolverRegistry`. Defined in |
| 47 |
// binder_resolver.cc |
| 48 |
namespace grpc_core { |
| 49 |
void RegisterBinderResolver(CoreConfiguration::Builder* builder); |
| 50 |
} |
| 51 |
|
| 52 |
namespace { |
| 53 |
|
| 54 |
class BinderResolverTest : public ::testing::Test { |
| 55 |
public: |
| 56 |
BinderResolverTest() { |
| 57 |
factory_ = grpc_core::CoreConfiguration::Get() |
| 58 |
.resolver_registry() |
| 59 |
.LookupResolverFactory("binder"); |
| 60 |
} |
| 61 |
~BinderResolverTest() override {} |
| 62 |
static void SetUpTestSuite() { |
| 63 |
builder_ = |
| 64 |
std::make_unique<grpc_core::CoreConfiguration::WithSubstituteBuilder>( |
| 65 |
[](grpc_core::CoreConfiguration::Builder* builder) { |
| 66 |
BuildCoreConfiguration(builder); |
| 67 |
if (!builder->resolver_registry()->HasResolverFactory("binder")) { |
| 68 |
// Binder resolver will only be registered on platforms that |
| 69 |
// support binder transport. If it is not registered on current |
| 70 |
// platform, we manually register it here for testing purpose. |
| 71 |
RegisterBinderResolver(builder); |
| 72 |
ASSERT_TRUE( |
| 73 |
builder->resolver_registry()->HasResolverFactory("binder")); |
| 74 |
} |
| 75 |
}); |
| 76 |
grpc_init(); |
| 77 |
if (grpc_core::CoreConfiguration::Get() |
| 78 |
.resolver_registry() |
| 79 |
.LookupResolverFactory("binder") == nullptr) { |
| 80 |
} |
| 81 |
} |
| 82 |
static void TearDownTestSuite() { |
| 83 |
grpc_shutdown(); |
| 84 |
builder_.reset(); |
| 85 |
} |
| 86 |
|
| 87 |
void SetUp() override { ASSERT_TRUE(factory_); } |
| 88 |
|
| 89 |
class ResultHandler : public grpc_core::Resolver::ResultHandler { |
| 90 |
public: |
| 91 |
ResultHandler() = default; |
| 92 |
|
| 93 |
explicit ResultHandler(const std::string& expected_binder_id) |
| 94 |
: expect_result_(true), expected_binder_id_(expected_binder_id) {} |
| 95 |
|
| 96 |
void ReportResult(grpc_core::Resolver::Result result) override { |
| 97 |
EXPECT_TRUE(expect_result_); |
| 98 |
ASSERT_TRUE(result.addresses.ok()); |
| 99 |
ASSERT_EQ(result.addresses->size(), 1); |
| 100 |
grpc_core::ServerAddress addr = (*result.addresses)[0]; |
| 101 |
const struct sockaddr_un* un = |
| 102 |
reinterpret_cast<const struct sockaddr_un*>(addr.address().addr); |
| 103 |
EXPECT_EQ(addr.address().len, |
| 104 |
sizeof(un->sun_family) + expected_binder_id_.length() + 1); |
| 105 |
EXPECT_EQ(un->sun_family, AF_MAX); |
| 106 |
EXPECT_EQ(un->sun_path, expected_binder_id_); |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
// Whether we expect ReportResult function to be invoked |
| 111 |
bool expect_result_ = false; |
| 112 |
|
| 113 |
std::string expected_binder_id_; |
| 114 |
}; |
| 115 |
|
| 116 |
void TestSucceeds(const char* string, const std::string& expected_path) { |
| 117 |
gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string, |
| 118 |
std::string(factory_->scheme()).c_str()); |
| 119 |
grpc_core::ExecCtx exec_ctx; |
| 120 |
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string); |
| 121 |
ASSERT_TRUE(uri.ok()) << uri.status().ToString(); |
| 122 |
grpc_core::ResolverArgs args; |
| 123 |
args.uri = std::move(*uri); |
| 124 |
args.result_handler = |
| 125 |
std::make_unique<BinderResolverTest::ResultHandler>(expected_path); |
| 126 |
grpc_core::OrphanablePtr<grpc_core::Resolver> resolver = |
| 127 |
factory_->CreateResolver(std::move(args)); |
| 128 |
ASSERT_TRUE(resolver != nullptr); |
| 129 |
resolver->StartLocked(); |
| 130 |
} |
| 131 |
|
| 132 |
void TestFails(const char* string) { |
| 133 |
gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string, |
| 134 |
std::string(factory_->scheme()).c_str()); |
| 135 |
grpc_core::ExecCtx exec_ctx; |
| 136 |
absl::StatusOr<grpc_core::URI> uri = grpc_core::URI::Parse(string); |
| 137 |
ASSERT_TRUE(uri.ok()) << uri.status().ToString(); |
| 138 |
grpc_core::ResolverArgs args; |
| 139 |
args.uri = std::move(*uri); |
| 140 |
args.result_handler = std::make_unique<BinderResolverTest::ResultHandler>(); |
| 141 |
grpc_core::OrphanablePtr<grpc_core::Resolver> resolver = |
| 142 |
factory_->CreateResolver(std::move(args)); |
| 143 |
EXPECT_TRUE(resolver == nullptr); |
| 144 |
} |
| 145 |
|
| 146 |
private: |
| 147 |
grpc_core::ResolverFactory* factory_; |
| 148 |
static std::unique_ptr<grpc_core::CoreConfiguration::WithSubstituteBuilder> |
| 149 |
builder_; |
| 150 |
}; |
| 151 |
|
| 152 |
std::unique_ptr<grpc_core::CoreConfiguration::WithSubstituteBuilder> |
| 153 |
BinderResolverTest::builder_; |
| 154 |
|
| 155 |
} // namespace |
| 156 |
|
| 157 |
// Authority is not allowed |
| 158 |
TEST_F(BinderResolverTest, AuthorityPresents) { |
| 159 |
TestFails("binder://example"); |
| 160 |
TestFails("binder://google.com"); |
| 161 |
TestFails("binder://google.com/test"); |
| 162 |
} |
| 163 |
|
| 164 |
// Path cannot be empty |
| 165 |
TEST_F(BinderResolverTest, EmptyPath) { |
| 166 |
TestFails("binder:"); |
| 167 |
TestFails("binder:/"); |
| 168 |
TestFails("binder://"); |
| 169 |
} |
| 170 |
|
| 171 |
TEST_F(BinderResolverTest, PathLength) { |
| 172 |
// Note that we have a static assert in binder_resolver.cc that checks |
| 173 |
// sizeof(sockaddr_un::sun_path) is greater than 100 |
| 174 |
|
| 175 |
// 100 character path should be fine |
| 176 |
TestSucceeds(("binder:l" + std::string(98, 'o') + "g").c_str(), |
| 177 |
"l" + std::string(98, 'o') + "g"); |
| 178 |
|
| 179 |
// 200 character path most likely will fail |
| 180 |
TestFails(("binder:l" + std::string(198, 'o') + "g").c_str()); |
| 181 |
} |
| 182 |
|
| 183 |
TEST_F(BinderResolverTest, SlashPrefixes) { |
| 184 |
TestSucceeds("binder:///test", "test"); |
| 185 |
TestSucceeds("binder:////test", "/test"); |
| 186 |
} |
| 187 |
|
| 188 |
TEST_F(BinderResolverTest, ValidCases) { |
| 189 |
TestSucceeds("binder:[[", "[["); |
| 190 |
TestSucceeds("binder:google!com", "google!com"); |
| 191 |
TestSucceeds("binder:test/", "test/"); |
| 192 |
TestSucceeds("binder:test:", "test:"); |
| 193 |
|
| 194 |
TestSucceeds("binder:e", "e"); |
| 195 |
TestSucceeds("binder:example", "example"); |
| 196 |
TestSucceeds("binder:google.com", "google.com"); |
| 197 |
TestSucceeds("binder:~", "~"); |
| 198 |
TestSucceeds("binder:12345", "12345"); |
| 199 |
TestSucceeds( |
| 200 |
"binder:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._" |
| 201 |
"~", |
| 202 |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"); |
| 203 |
} |
| 204 |
|
| 205 |
#endif |
| 206 |
|
| 207 |
int main(int argc, char** argv) { |
| 208 |
::testing::InitGoogleTest(&argc, argv); |
| 209 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 210 |
return RUN_ALL_TESTS(); |
| 211 |
} |