binder_resolver_test.cc
Go to the documentation of this file.
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 <gtest/gtest.h>
16 
20 
21 #ifdef GRPC_HAVE_UNIX_SOCKET
22 
23 #include <sys/un.h>
24 
25 #include <cstring>
26 
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
31 
35 
36 // Registers the factory with `grpc_core::ResolverRegistry`. Defined in
37 // binder_resolver.cc
38 namespace grpc_core {
39 void RegisterBinderResolver(CoreConfiguration::Builder* builder);
40 }
41 
42 namespace {
43 
44 class BinderResolverTest : public ::testing::Test {
45  public:
46  BinderResolverTest() {
49  .LookupResolverFactory("binder");
50  }
51  ~BinderResolverTest() override {}
52  static void SetUpTestSuite() {
57  if (!builder->resolver_registry()->HasResolverFactory("binder")) {
58  // Binder resolver will only be registered on platforms that support
59  // binder transport. If it is not registered on current platform, we
60  // manually register it here for testing purpose.
61  RegisterBinderResolver(builder);
63  builder->resolver_registry()->HasResolverFactory("binder"));
64  }
65  });
66  grpc_init();
68  .resolver_registry()
69  .LookupResolverFactory("binder") == nullptr) {
70  }
71  }
72  static void TearDownTestSuite() { grpc_shutdown(); }
73 
74  void SetUp() override { ASSERT_TRUE(factory_); }
75 
77  public:
78  ResultHandler() = default;
79 
80  explicit ResultHandler(const std::string& expected_binder_id)
81  : expect_result_(true), expected_binder_id_(expected_binder_id) {}
82 
83  void ReportResult(grpc_core::Resolver::Result result) override {
84  EXPECT_TRUE(expect_result_);
85  ASSERT_TRUE(result.addresses.ok());
86  ASSERT_EQ(result.addresses->size(), 1);
87  grpc_core::ServerAddress addr = (*result.addresses)[0];
88  const struct sockaddr_un* un =
89  reinterpret_cast<const struct sockaddr_un*>(addr.address().addr);
90  EXPECT_EQ(addr.address().len,
91  sizeof(un->sun_family) + expected_binder_id_.length() + 1);
92  EXPECT_EQ(un->sun_family, AF_MAX);
93  EXPECT_EQ(un->sun_path, expected_binder_id_);
94  }
95 
96  private:
97  // Whether we expect ReportResult function to be invoked
98  bool expect_result_ = false;
99 
100  std::string expected_binder_id_;
101  };
102 
103  void TestSucceeds(const char* string, const std::string& expected_path) {
104  gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", string,
105  std::string(factory_->scheme()).c_str());
108  ASSERT_TRUE(uri.ok()) << uri.status().ToString();
110  args.uri = std::move(*uri);
111  args.result_handler =
112  absl::make_unique<BinderResolverTest::ResultHandler>(expected_path);
114  factory_->CreateResolver(std::move(args));
115  ASSERT_TRUE(resolver != nullptr);
116  resolver->StartLocked();
117  }
118 
119  void TestFails(const char* string) {
120  gpr_log(GPR_DEBUG, "test: '%s' should be invalid for '%s'", string,
121  std::string(factory_->scheme()).c_str());
124  ASSERT_TRUE(uri.ok()) << uri.status().ToString();
126  args.uri = std::move(*uri);
127  args.result_handler =
128  absl::make_unique<BinderResolverTest::ResultHandler>();
130  factory_->CreateResolver(std::move(args));
131  EXPECT_TRUE(resolver == nullptr);
132  }
133 
134  private:
135  grpc_core::ResolverFactory* factory_;
136 };
137 
138 } // namespace
139 
140 // Authority is not allowed
141 TEST_F(BinderResolverTest, AuthorityPresents) {
142  TestFails("binder://example");
143  TestFails("binder://google.com");
144  TestFails("binder://google.com/test");
145 }
146 
147 // Path cannot be empty
148 TEST_F(BinderResolverTest, EmptyPath) {
149  TestFails("binder:");
150  TestFails("binder:/");
151  TestFails("binder://");
152 }
153 
154 TEST_F(BinderResolverTest, PathLength) {
155  // Note that we have a static assert in binder_resolver.cc that checks
156  // sizeof(sockaddr_un::sun_path) is greater than 100
157 
158  // 100 character path should be fine
159  TestSucceeds(("binder:l" + std::string(98, 'o') + "g").c_str(),
160  "l" + std::string(98, 'o') + "g");
161 
162  // 200 character path most likely will fail
163  TestFails(("binder:l" + std::string(198, 'o') + "g").c_str());
164 }
165 
166 TEST_F(BinderResolverTest, SlashPrefixes) {
167  TestSucceeds("binder:///test", "test");
168  TestSucceeds("binder:////test", "/test");
169 }
170 
171 TEST_F(BinderResolverTest, ValidCases) {
172  TestSucceeds("binder:[[", "[[");
173  TestSucceeds("binder:google!com", "google!com");
174  TestSucceeds("binder:test/", "test/");
175  TestSucceeds("binder:test:", "test:");
176 
177  TestSucceeds("binder:e", "e");
178  TestSucceeds("binder:example", "example");
179  TestSucceeds("binder:google.com", "google.com");
180  TestSucceeds("binder:~", "~");
181  TestSucceeds("binder:12345", "12345");
182  TestSucceeds(
183  "binder:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._"
184  "~",
185  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~");
186 }
187 
188 #endif
189 
190 int main(int argc, char** argv) {
191  ::testing::InitGoogleTest(&argc, argv);
192  grpc::testing::TestEnvironment env(&argc, argv);
193  return RUN_ALL_TESTS();
194 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc_core::ResolverRegistry::LookupResolverFactory
ResolverFactory * LookupResolverFactory(absl::string_view scheme) const
Definition: resolver_registry.cc:108
log.h
core_configuration.h
generate.env
env
Definition: generate.py:37
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::CoreConfiguration::Builder
Definition: core_configuration.h:41
testing::Test::SetUpTestSuite
static void SetUpTestSuite()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:417
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::ServerAddress
Definition: server_address.h:49
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
tap2pcap_test.expected_path
expected_path
Definition: tap2pcap_test.py:16
gen_build_yaml.struct
def struct(**kwargs)
Definition: test/core/end2end/gen_build_yaml.py:30
true
#define true
Definition: setup_once.h:324
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc_core::CoreConfiguration::BuildSpecialConfiguration
static void BuildSpecialConfiguration(BuildFunc build)
Definition: core_configuration.h:100
string_util.h
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
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
grpc_core::CoreConfiguration::Get
static const CoreConfiguration & Get()
Definition: core_configuration.h:82
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_core::Resolver::Result
Results returned by the resolver.
Definition: resolver/resolver.h:56
grpc.h
grpc_core::ResolverArgs
Definition: resolver_factory.h:41
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::Resolver::ResultHandler
Definition: resolver/resolver.h:84
grpc_core::ExecCtx
Definition: exec_ctx.h:97
resolver_registry.h
test_config.h
testing::Test::SetUp
virtual void SetUp()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:2264
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
testing::Test::TearDownTestSuite
static void TearDownTestSuite()
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:427
port.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
grpc_core::BuildCoreConfiguration
void BuildCoreConfiguration(CoreConfiguration::Builder *builder)
Definition: grpc_plugin_registry.cc:109
grpc_core::OrphanablePtr
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:64
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::CoreConfiguration::Reset
static void Reset()
Definition: core_configuration.cc:96
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
grpc_core::CoreConfiguration::resolver_registry
const ResolverRegistry & resolver_registry() const
Definition: core_configuration.h:157
exec_ctx.h
Builder
struct Builder Builder
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:68
channel_args.h
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_core::ResolverFactory
Definition: resolver_factory.h:54
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
main
int main(int argc, char **argv)
Definition: binder_resolver_test.cc:190
absl::StatusOr::status
const Status & status() const &
Definition: abseil-cpp/absl/status/statusor.h:678
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2367


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48