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