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