secure_auth_context_test.cc
Go to the documentation of this file.
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 
20 
21 #include <gtest/gtest.h>
22 
23 #include <grpc/grpc_security.h>
25 
28 
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) {
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) {
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 }
main
int main(int argc, char **argv)
Definition: secure_auth_context_test.cc:96
ctx
Definition: benchmark-async.c:30
grpc
Definition: grpcpp/alarm.h:33
bar
Definition: bloaty/third_party/googletest/googletest/test/googletest-output-test_.cc:562
secure_auth_context.h
grpc_security.h
grpc::EXPECT_TRUE
EXPECT_TRUE(status.ok())
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
grpc_core::RefCountedPtr< grpc_auth_context >
ToString
std::string ToString(const grpc::string_ref &r)
Definition: string_ref_helper.cc:24
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
security_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc::testing::ToString
std::string ToString(const grpc::string_ref &r)
Definition: string_ref_helper.cc:24
grpc::EXPECT_EQ
EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code())
grpc::AuthProperty
std::pair< string_ref, string_ref > AuthProperty
Definition: grpcpp/impl/codegen/security/auth_context.h:35
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
iter
Definition: test_winkernel.cpp:47
auth_context.h
string_ref_helper.h
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 Thu Mar 13 2025 03:01:15