• 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 <grpc/grpc_security.h>
20 #include <grpcpp/security/auth_context.h>
21 #include <gtest/gtest.h>
22 #include "src/cpp/common/secure_auth_context.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 TestAuthPropertyIterator : public AuthPropertyIterator {
33  public:
TestAuthPropertyIterator()34   TestAuthPropertyIterator() {}
TestAuthPropertyIterator(const grpc_auth_property * property,const grpc_auth_property_iterator * iter)35   TestAuthPropertyIterator(const grpc_auth_property* property,
36                            const grpc_auth_property_iterator* iter)
37       : AuthPropertyIterator(property, iter) {}
38 };
39 
40 class AuthPropertyIteratorTest : public ::testing::Test {
41  protected:
SetUp()42   void SetUp() override {
43     ctx_ = grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
44     grpc_auth_context_add_cstring_property(ctx_.get(), "name", "chapi");
45     grpc_auth_context_add_cstring_property(ctx_.get(), "name", "chapo");
46     grpc_auth_context_add_cstring_property(ctx_.get(), "foo", "bar");
47     EXPECT_EQ(1, grpc_auth_context_set_peer_identity_property_name(ctx_.get(),
48                                                                    "name"));
49   }
50   grpc_core::RefCountedPtr<grpc_auth_context> ctx_;
51 };
52 
TEST_F(AuthPropertyIteratorTest,DefaultCtor)53 TEST_F(AuthPropertyIteratorTest, DefaultCtor) {
54   TestAuthPropertyIterator iter1;
55   TestAuthPropertyIterator iter2;
56   EXPECT_EQ(iter1, iter2);
57 }
58 
TEST_F(AuthPropertyIteratorTest,GeneralTest)59 TEST_F(AuthPropertyIteratorTest, GeneralTest) {
60   grpc_auth_property_iterator c_iter =
61       grpc_auth_context_property_iterator(ctx_.get());
62   const grpc_auth_property* property =
63       grpc_auth_property_iterator_next(&c_iter);
64   TestAuthPropertyIterator iter(property, &c_iter);
65   TestAuthPropertyIterator empty_iter;
66   EXPECT_FALSE(iter == empty_iter);
67   AuthProperty p0 = *iter;
68   ++iter;
69   AuthProperty p1 = *iter;
70   iter++;
71   AuthProperty p2 = *iter;
72   EXPECT_EQ("name", ToString(p0.first));
73   EXPECT_EQ("chapi", ToString(p0.second));
74   EXPECT_EQ("name", ToString(p1.first));
75   EXPECT_EQ("chapo", ToString(p1.second));
76   EXPECT_EQ("foo", ToString(p2.first));
77   EXPECT_EQ("bar", ToString(p2.second));
78   ++iter;
79   EXPECT_EQ(empty_iter, iter);
80 }
81 
82 }  // namespace
83 }  // namespace grpc
84 
main(int argc,char ** argv)85 int main(int argc, char** argv) {
86   ::testing::InitGoogleTest(&argc, argv);
87   return RUN_ALL_TESTS();
88 }
89