xref: /aosp_15_r20/external/grpc-grpc/test/cpp/common/auth_property_iterator_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <gtest/gtest.h>
20 
21 #include <grpc/grpc_security.h>
22 #include <grpcpp/security/auth_context.h>
23 
24 #include "src/core/lib/security/context/security_context.h"
25 #include "src/cpp/common/secure_auth_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 TestAuthPropertyIterator : public AuthPropertyIterator {
34  public:
TestAuthPropertyIterator()35   TestAuthPropertyIterator() {}
TestAuthPropertyIterator(const grpc_auth_property * property,const grpc_auth_property_iterator * iter)36   TestAuthPropertyIterator(const grpc_auth_property* property,
37                            const grpc_auth_property_iterator* iter)
38       : AuthPropertyIterator(property, iter) {}
39 };
40 
41 class AuthPropertyIteratorTest : public ::testing::Test {
42  protected:
SetUp()43   void SetUp() override {
44     ctx_ = grpc_core::MakeRefCounted<grpc_auth_context>(nullptr);
45     grpc_auth_context_add_cstring_property(ctx_.get(), "name", "chapi");
46     grpc_auth_context_add_cstring_property(ctx_.get(), "name", "chapo");
47     grpc_auth_context_add_cstring_property(ctx_.get(), "foo", "bar");
48     EXPECT_EQ(1, grpc_auth_context_set_peer_identity_property_name(ctx_.get(),
49                                                                    "name"));
50   }
51   grpc_core::RefCountedPtr<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_.get());
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