mirror of https://github.com/grpc/grpc.git
parent
c79bef55ee
commit
5820e833fc
26 changed files with 702 additions and 16 deletions
@ -0,0 +1,76 @@ |
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <gtest/gtest.h> |
||||
#include "absl/strings/string_view.h" |
||||
|
||||
#include "src/core/lib/security/authorization/evaluate_args.h" |
||||
#include "test/core/util/eval_args_mock_endpoint.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
class EvaluateArgsTest : public ::testing::Test { |
||||
protected: |
||||
void SetUp() override { |
||||
local_address_ = "255.255.255.255"; |
||||
peer_address_ = "128.128.128.128"; |
||||
local_port_ = 413; |
||||
peer_port_ = 314; |
||||
endpoint_ = CreateEvalArgsMockEndpoint(local_address_.c_str(), local_port_, |
||||
peer_address_.c_str(), peer_port_); |
||||
evaluate_args_ = |
||||
absl::make_unique<EvaluateArgs>(nullptr, nullptr, endpoint_); |
||||
} |
||||
void TearDown() override { grpc_endpoint_destroy(endpoint_); } |
||||
grpc_endpoint* endpoint_; |
||||
std::unique_ptr<EvaluateArgs> evaluate_args_; |
||||
std::string local_address_; |
||||
std::string peer_address_; |
||||
int local_port_; |
||||
int peer_port_; |
||||
}; |
||||
|
||||
TEST_F(EvaluateArgsTest, TestEvaluateArgsLocalAddress) { |
||||
absl::string_view src_address = evaluate_args_->GetLocalAddress(); |
||||
EXPECT_EQ(src_address, local_address_) |
||||
<< "Error: Failed to extract correct Local address from EvaluateArgs."; |
||||
} |
||||
|
||||
TEST_F(EvaluateArgsTest, TestEvaluateArgsLocalPort) { |
||||
int src_port = evaluate_args_->GetLocalPort(); |
||||
EXPECT_EQ(src_port, local_port_) |
||||
<< "Error: Failed to extract correct Local port from EvaluateArgs."; |
||||
} |
||||
|
||||
TEST_F(EvaluateArgsTest, TestEvaluateArgsPeerAddress) { |
||||
absl::string_view dest_address = evaluate_args_->GetPeerAddress(); |
||||
EXPECT_EQ(dest_address, peer_address_) |
||||
<< "Error: Failed to extract correct Peer address from " |
||||
"EvaluateArgs. "; |
||||
} |
||||
|
||||
TEST_F(EvaluateArgsTest, TestEvaluateArgsPeerPort) { |
||||
int dest_port = evaluate_args_->GetPeerPort(); |
||||
EXPECT_EQ(dest_port, peer_port_) |
||||
<< "Error: Failed to extract correct Peer port from EvaluateArgs."; |
||||
} |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
@ -0,0 +1,118 @@ |
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "test/core/util/eval_args_mock_endpoint.h" |
||||
|
||||
#include <inttypes.h> |
||||
|
||||
#include <string> |
||||
|
||||
#include "absl/strings/str_format.h" |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include "src/core/lib/iomgr/sockaddr.h" |
||||
#include "src/core/lib/iomgr/sockaddr_utils.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
class EvalArgsMockEndpoint { |
||||
public: |
||||
EvalArgsMockEndpoint(absl::string_view local_uri, absl::string_view peer_uri) |
||||
: local_address_(local_uri), peer_(peer_uri) { |
||||
base_.vtable = &vtable_; |
||||
} |
||||
grpc_endpoint* base() const { return const_cast<grpc_endpoint*>(&base_); } |
||||
static void Read(grpc_endpoint* ep, grpc_slice_buffer* slices, |
||||
grpc_closure* cb, bool unused) {} |
||||
static void Write(grpc_endpoint* ep, grpc_slice_buffer* slices, |
||||
grpc_closure* cb, void* unused) {} |
||||
static void AddToPollset(grpc_endpoint* ep, grpc_pollset* unused) {} |
||||
static void AddToPollsetSet(grpc_endpoint* ep, grpc_pollset_set* unused) {} |
||||
static void DeleteFromPollsetSet(grpc_endpoint* ep, |
||||
grpc_pollset_set* unused) {} |
||||
static void Shutdown(grpc_endpoint* ep, grpc_error* why) {} |
||||
static void Destroy(grpc_endpoint* ep) { |
||||
EvalArgsMockEndpoint* m = reinterpret_cast<EvalArgsMockEndpoint*>(ep); |
||||
delete m; |
||||
} |
||||
|
||||
static absl::string_view GetPeer(grpc_endpoint* ep) { |
||||
EvalArgsMockEndpoint* m = reinterpret_cast<EvalArgsMockEndpoint*>(ep); |
||||
return m->peer_; |
||||
} |
||||
|
||||
static absl::string_view GetLocalAddress(grpc_endpoint* ep) { |
||||
EvalArgsMockEndpoint* m = reinterpret_cast<EvalArgsMockEndpoint*>(ep); |
||||
return m->local_address_; |
||||
} |
||||
|
||||
static grpc_resource_user* GetResourceUser(grpc_endpoint* ep) { |
||||
return nullptr; |
||||
} |
||||
|
||||
static int GetFd(grpc_endpoint* unused) { return -1; } |
||||
static bool CanTrackErr(grpc_endpoint* unused) { return false; } |
||||
|
||||
private: |
||||
static constexpr grpc_endpoint_vtable vtable_ = { |
||||
EvalArgsMockEndpoint::Read, |
||||
EvalArgsMockEndpoint::Write, |
||||
EvalArgsMockEndpoint::AddToPollset, |
||||
EvalArgsMockEndpoint::AddToPollsetSet, |
||||
EvalArgsMockEndpoint::DeleteFromPollsetSet, |
||||
EvalArgsMockEndpoint::Shutdown, |
||||
EvalArgsMockEndpoint::Destroy, |
||||
EvalArgsMockEndpoint::GetResourceUser, |
||||
EvalArgsMockEndpoint::GetPeer, |
||||
EvalArgsMockEndpoint::GetLocalAddress, |
||||
EvalArgsMockEndpoint::GetFd, |
||||
EvalArgsMockEndpoint::CanTrackErr}; |
||||
grpc_endpoint base_; |
||||
std::string local_address_; |
||||
std::string peer_; |
||||
}; |
||||
|
||||
constexpr grpc_endpoint_vtable EvalArgsMockEndpoint::vtable_; |
||||
|
||||
namespace { |
||||
|
||||
std::string NameAndPortToURI(const char* addr, const int port) { |
||||
grpc_sockaddr_in address; |
||||
memset(&address, 0, sizeof(address)); |
||||
address.sin_family = AF_INET; |
||||
address.sin_port = htons(port); |
||||
inet_pton(AF_INET, addr, &address.sin_addr); |
||||
grpc_resolved_address resolved; |
||||
memset(&resolved, 0, sizeof(resolved)); |
||||
memcpy(resolved.addr, &address, sizeof(address)); |
||||
resolved.len = sizeof(address); |
||||
return grpc_sockaddr_to_uri(&resolved); |
||||
} |
||||
|
||||
} // namespace
|
||||
|
||||
grpc_endpoint* CreateEvalArgsMockEndpoint(const char* local_address, |
||||
const int local_port, |
||||
const char* peer_address, |
||||
const int peer_port) { |
||||
EvalArgsMockEndpoint* m = |
||||
new EvalArgsMockEndpoint(NameAndPortToURI(local_address, local_port), |
||||
NameAndPortToURI(peer_address, peer_port)); |
||||
return m->base(); |
||||
} |
||||
|
||||
} // namespace grpc_core
|
@ -0,0 +1,31 @@ |
||||
// Copyright 2020 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef GRPC_TEST_CORE_UTIL_EVAL_ARGS_MOCK_ENDPOINT_H |
||||
#define GRPC_TEST_CORE_UTIL_EVAL_ARGS_MOCK_ENDPOINT_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/iomgr/endpoint.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
grpc_endpoint* CreateEvalArgsMockEndpoint(const char* local_address, |
||||
const int local_port, |
||||
const char* peer_address, |
||||
const int peer_port); |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_TEST_CORE_UTIL_EVAL_ARGS_MOCK_ENDPOINT_H
|
Loading…
Reference in new issue