blob: d0243a5432d153aa6b543a5c86ced66e2f65df1f [file] [log] [blame]
yang-g8068f862015-07-06 15:05:48 -07001/*
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++/auth_context.h>
35#include <gtest/gtest.h>
36#include "src/cpp/common/secure_auth_context.h"
37#include "src/core/security/security_context.h"
38
39namespace grpc {
40namespace {
41
42class SecureAuthContextTest : public ::testing::Test {};
43
44// Created with nullptr
45TEST_F(SecureAuthContextTest, EmptyContext) {
46 SecureAuthContext context(nullptr);
47 EXPECT_TRUE(context.GetPeerIdentity().empty());
48 EXPECT_TRUE(context.GetPeerIdentityPropertyName().empty());
49 EXPECT_TRUE(context.FindPropertyValues("").empty());
50 EXPECT_TRUE(context.FindPropertyValues("whatever").empty());
yang-g811536e2015-07-15 00:50:15 -070051 EXPECT_TRUE(context.begin() == context.end());
yang-g8068f862015-07-06 15:05:48 -070052}
53
54TEST_F(SecureAuthContextTest, Properties) {
55 grpc_auth_context* ctx = grpc_auth_context_create(NULL, 3);
56 ctx->properties[0] = grpc_auth_property_init_from_cstring("name", "chapi");
57 ctx->properties[1] = grpc_auth_property_init_from_cstring("name", "chapo");
58 ctx->properties[2] = grpc_auth_property_init_from_cstring("foo", "bar");
59 ctx->peer_identity_property_name = ctx->properties[0].name;
60
61 SecureAuthContext context(ctx);
62 std::vector<grpc::string> peer_identity = context.GetPeerIdentity();
Craig Tillerd0955f32015-07-10 07:52:37 -070063 EXPECT_EQ(2u, peer_identity.size());
yang-g8068f862015-07-06 15:05:48 -070064 EXPECT_EQ("chapi", peer_identity[0]);
65 EXPECT_EQ("chapo", peer_identity[1]);
66 EXPECT_EQ("name", context.GetPeerIdentityPropertyName());
67 std::vector<grpc::string> bar = context.FindPropertyValues("foo");
Craig Tillerd0955f32015-07-10 07:52:37 -070068 EXPECT_EQ(1u, bar.size());
yang-g8068f862015-07-06 15:05:48 -070069 EXPECT_EQ("bar", bar[0]);
yang-g8068f862015-07-06 15:05:48 -070070}
71
yang-g811536e2015-07-15 00:50:15 -070072TEST_F(SecureAuthContextTest, Iterators) {
73 grpc_auth_context* ctx = grpc_auth_context_create(NULL, 3);
74 ctx->properties[0] = grpc_auth_property_init_from_cstring("name", "chapi");
75 ctx->properties[1] = grpc_auth_property_init_from_cstring("name", "chapo");
76 ctx->properties[2] = grpc_auth_property_init_from_cstring("foo", "bar");
77 ctx->peer_identity_property_name = ctx->properties[0].name;
78
79 SecureAuthContext context(ctx);
yang-g7ef72322015-07-16 21:00:51 -070080 AuthPropertyIterator iter = context.begin();
yang-g811536e2015-07-15 00:50:15 -070081 EXPECT_TRUE(context.end() != iter);
yang-g7ef72322015-07-16 21:00:51 -070082 AuthProperty p0 = *iter;
yang-g811536e2015-07-15 00:50:15 -070083 ++iter;
yang-g7ef72322015-07-16 21:00:51 -070084 AuthProperty p1 = *iter;
yang-g811536e2015-07-15 00:50:15 -070085 iter++;
yang-g7ef72322015-07-16 21:00:51 -070086 AuthProperty p2 = *iter;
yang-g811536e2015-07-15 00:50:15 -070087 EXPECT_EQ("name", p0.first);
88 EXPECT_EQ("chapi", p0.second);
89 EXPECT_EQ("name", p1.first);
90 EXPECT_EQ("chapo", p1.second);
91 EXPECT_EQ("foo", p2.first);
92 EXPECT_EQ("bar", p2.second);
93 ++iter;
94 EXPECT_EQ(context.end(), iter);
95}
96
yang-g8068f862015-07-06 15:05:48 -070097} // namespace
98} // namespace grpc
99
100int main(int argc, char **argv) {
101 ::testing::InitGoogleTest(&argc, argv);
102 return RUN_ALL_TESTS();
103}