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_auth_context_create(nullptr);
44     grpc_auth_context_add_cstring_property(ctx_, "name", "chapi");
45     grpc_auth_context_add_cstring_property(ctx_, "name", "chapo");
46     grpc_auth_context_add_cstring_property(ctx_, "foo", "bar");
47     EXPECT_EQ(1,
48               grpc_auth_context_set_peer_identity_property_name(ctx_, "name"));
49   }
TearDown()50   void TearDown() override { grpc_auth_context_release(ctx_); }
51   grpc_auth_context* ctx_;
52 };
53 
TEST_F(AuthPropertyIteratorTest,DefaultCtor)54 TEST_F(AuthPropertyIteratorTest, DefaultCtor) {
55   TestAuthPropertyIterator iter1;
56   TestAuthPropertyIterator iter2;
57   EXPECT_EQ(iter1, iter2);
58 }
59 
TEST_F(AuthPropertyIteratorTest,GeneralTest)60 TEST_F(AuthPropertyIteratorTest, GeneralTest) {
61   grpc_auth_property_iterator c_iter =
62       grpc_auth_context_property_iterator(ctx_);
63   const grpc_auth_property* property =
64       grpc_auth_property_iterator_next(&c_iter);
65   TestAuthPropertyIterator iter(property, &c_iter);
66   TestAuthPropertyIterator empty_iter;
67   EXPECT_FALSE(iter == empty_iter);
68   AuthProperty p0 = *iter;
69   ++iter;
70   AuthProperty p1 = *iter;
71   iter++;
72   AuthProperty p2 = *iter;
73   EXPECT_EQ("name", ToString(p0.first));
74   EXPECT_EQ("chapi", ToString(p0.second));
75   EXPECT_EQ("name", ToString(p1.first));
76   EXPECT_EQ("chapo", ToString(p1.second));
77   EXPECT_EQ("foo", ToString(p2.first));
78   EXPECT_EQ("bar", ToString(p2.second));
79   ++iter;
80   EXPECT_EQ(empty_iter, iter);
81 }
82 
83 }  // namespace
84 }  // namespace grpc
85 
main(int argc,char ** argv)86 int main(int argc, char** argv) {
87   ::testing::InitGoogleTest(&argc, argv);
88   return RUN_ALL_TESTS();
89 }
90