Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <grpc/grpc_security.h>
35 : #include <grpc++/security/auth_context.h>
36 : #include <gtest/gtest.h>
37 : #include "src/cpp/common/secure_auth_context.h"
38 : #include "test/cpp/util/string_ref_helper.h"
39 :
40 : extern "C" {
41 : #include "src/core/security/security_context.h"
42 : }
43 :
44 : using grpc::testing::ToString;
45 :
46 : namespace grpc {
47 : namespace {
48 :
49 6 : class SecureAuthContextTest : public ::testing::Test {};
50 :
51 : // Created with nullptr
52 5 : TEST_F(SecureAuthContextTest, EmptyContext) {
53 1 : SecureAuthContext context(nullptr, true);
54 1 : EXPECT_TRUE(context.GetPeerIdentity().empty());
55 1 : EXPECT_TRUE(context.GetPeerIdentityPropertyName().empty());
56 1 : EXPECT_TRUE(context.FindPropertyValues("").empty());
57 1 : EXPECT_TRUE(context.FindPropertyValues("whatever").empty());
58 1 : EXPECT_TRUE(context.begin() == context.end());
59 1 : }
60 :
61 5 : TEST_F(SecureAuthContextTest, Properties) {
62 1 : grpc_auth_context* ctx = grpc_auth_context_create(NULL);
63 1 : SecureAuthContext context(ctx, true);
64 1 : context.AddProperty("name", "chapi");
65 1 : context.AddProperty("name", "chapo");
66 1 : context.AddProperty("foo", "bar");
67 1 : EXPECT_TRUE(context.SetPeerIdentityPropertyName("name"));
68 :
69 1 : std::vector<grpc::string_ref> peer_identity = context.GetPeerIdentity();
70 1 : EXPECT_EQ(2u, peer_identity.size());
71 1 : EXPECT_EQ("chapi", ToString(peer_identity[0]));
72 1 : EXPECT_EQ("chapo", ToString(peer_identity[1]));
73 1 : EXPECT_EQ("name", context.GetPeerIdentityPropertyName());
74 1 : std::vector<grpc::string_ref> bar = context.FindPropertyValues("foo");
75 1 : EXPECT_EQ(1u, bar.size());
76 1 : EXPECT_EQ("bar", ToString(bar[0]));
77 1 : }
78 :
79 5 : TEST_F(SecureAuthContextTest, Iterators) {
80 1 : grpc_auth_context* ctx = grpc_auth_context_create(NULL);
81 1 : SecureAuthContext context(ctx, true);
82 1 : context.AddProperty("name", "chapi");
83 1 : context.AddProperty("name", "chapo");
84 1 : context.AddProperty("foo", "bar");
85 1 : EXPECT_TRUE(context.SetPeerIdentityPropertyName("name"));
86 :
87 1 : AuthPropertyIterator iter = context.begin();
88 1 : EXPECT_TRUE(context.end() != iter);
89 1 : AuthProperty p0 = *iter;
90 1 : ++iter;
91 1 : AuthProperty p1 = *iter;
92 1 : iter++;
93 1 : AuthProperty p2 = *iter;
94 1 : EXPECT_EQ("name", ToString(p0.first));
95 1 : EXPECT_EQ("chapi", ToString(p0.second));
96 1 : EXPECT_EQ("name", ToString(p1.first));
97 1 : EXPECT_EQ("chapo", ToString(p1.second));
98 1 : EXPECT_EQ("foo", ToString(p2.first));
99 1 : EXPECT_EQ("bar", ToString(p2.second));
100 1 : ++iter;
101 1 : EXPECT_EQ(context.end(), iter);
102 1 : }
103 :
104 : } // namespace
105 : } // namespace grpc
106 :
107 1 : int main(int argc, char** argv) {
108 1 : ::testing::InitGoogleTest(&argc, argv);
109 1 : return RUN_ALL_TESTS();
110 3 : }
|