| 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 "src/cpp/common/secure_auth_context.h" |
| 20 |
|
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include <grpc/grpc_security.h> |
| 24 |
#include <grpcpp/security/auth_context.h> |
| 25 |
|
| 26 |
#include "src/core/lib/security/context/security_context.h" |
| 27 |
#include "test/cpp/util/string_ref_helper.h" |
| 28 |
|
| 29 |
using grpc::testing::ToString; |
| 30 |
|
| 31 |
namespace grpc { |
| 32 |
namespace { |
| 33 |
|
| 34 |
class SecureAuthContextTest : public ::testing::Test {}; |
| 35 |
|
| 36 |
// Created with nullptr |
| 37 |
TEST_F(SecureAuthContextTest, EmptyContext) { |
| 38 |
SecureAuthContext context(nullptr); |
| 39 |
EXPECT_TRUE(context.GetPeerIdentity().empty()); |
| 40 |
EXPECT_TRUE(context.GetPeerIdentityPropertyName().empty()); |
| 41 |
EXPECT_TRUE(context.FindPropertyValues("").empty()); |
| 42 |
EXPECT_TRUE(context.FindPropertyValues("whatever").empty()); |
| 43 |
EXPECT_TRUE(context.begin() == context.end()); |
| 44 |
} |
| 45 |
|
| 46 |
TEST_F(SecureAuthContextTest, Properties) { |
| 47 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 48 |
grpc_core::MakeRefCounted<grpc_auth_context>(nullptr); |
| 49 |
SecureAuthContext context(ctx.get()); |
| 50 |
ctx.reset(); |
| 51 |
context.AddProperty("name", "chapi"); |
| 52 |
context.AddProperty("name", "chapo"); |
| 53 |
context.AddProperty("foo", "bar"); |
| 54 |
EXPECT_TRUE(context.SetPeerIdentityPropertyName("name")); |
| 55 |
|
| 56 |
std::vector<grpc::string_ref> peer_identity = context.GetPeerIdentity(); |
| 57 |
EXPECT_EQ(2u, peer_identity.size()); |
| 58 |
EXPECT_EQ("chapi", ToString(peer_identity[0])); |
| 59 |
EXPECT_EQ("chapo", ToString(peer_identity[1])); |
| 60 |
EXPECT_EQ("name", context.GetPeerIdentityPropertyName()); |
| 61 |
std::vector<grpc::string_ref> bar = context.FindPropertyValues("foo"); |
| 62 |
EXPECT_EQ(1u, bar.size()); |
| 63 |
EXPECT_EQ("bar", ToString(bar[0])); |
| 64 |
} |
| 65 |
|
| 66 |
TEST_F(SecureAuthContextTest, Iterators) { |
| 67 |
grpc_core::RefCountedPtr<grpc_auth_context> ctx = |
| 68 |
grpc_core::MakeRefCounted<grpc_auth_context>(nullptr); |
| 69 |
SecureAuthContext context(ctx.get()); |
| 70 |
ctx.reset(); |
| 71 |
context.AddProperty("name", "chapi"); |
| 72 |
context.AddProperty("name", "chapo"); |
| 73 |
context.AddProperty("foo", "bar"); |
| 74 |
EXPECT_TRUE(context.SetPeerIdentityPropertyName("name")); |
| 75 |
|
| 76 |
AuthPropertyIterator iter = context.begin(); |
| 77 |
EXPECT_TRUE(context.end() != iter); |
| 78 |
AuthProperty p0 = *iter; |
| 79 |
++iter; |
| 80 |
AuthProperty p1 = *iter; |
| 81 |
iter++; |
| 82 |
AuthProperty p2 = *iter; |
| 83 |
EXPECT_EQ("name", ToString(p0.first)); |
| 84 |
EXPECT_EQ("chapi", ToString(p0.second)); |
| 85 |
EXPECT_EQ("name", ToString(p1.first)); |
| 86 |
EXPECT_EQ("chapo", ToString(p1.second)); |
| 87 |
EXPECT_EQ("foo", ToString(p2.first)); |
| 88 |
EXPECT_EQ("bar", ToString(p2.second)); |
| 89 |
++iter; |
| 90 |
EXPECT_EQ(context.end(), iter); |
| 91 |
} |
| 92 |
|
| 93 |
} // namespace |
| 94 |
} // namespace grpc |
| 95 |
|
| 96 |
int main(int argc, char** argv) { |
| 97 |
::testing::InitGoogleTest(&argc, argv); |
| 98 |
return RUN_ALL_TESTS(); |
| 99 |
} |