blob: c71ef58023f0958e1e692fb6a74c79d62bdb5dd7 [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
Julien Boeufea456fc2015-07-07 15:23:30 -070034#include <grpc/grpc_security.h>
yang-g9e2f90c2015-08-21 15:35:03 -070035#include <grpc++/support/auth_context.h>
yang-g8068f862015-07-06 15:05:48 -070036#include <gtest/gtest.h>
37#include "src/cpp/common/secure_auth_context.h"
yang-g8068f862015-07-06 15:05:48 -070038
Julien Boeuf54b50182015-08-05 21:40:49 -070039extern "C" {
40#include "src/core/security/security_context.h"
41}
42
yang-g8068f862015-07-06 15:05:48 -070043namespace grpc {
44namespace {
45
46class SecureAuthContextTest : public ::testing::Test {};
47
48// Created with nullptr
49TEST_F(SecureAuthContextTest, EmptyContext) {
50 SecureAuthContext context(nullptr);
51 EXPECT_TRUE(context.GetPeerIdentity().empty());
52 EXPECT_TRUE(context.GetPeerIdentityPropertyName().empty());
53 EXPECT_TRUE(context.FindPropertyValues("").empty());
54 EXPECT_TRUE(context.FindPropertyValues("whatever").empty());
yang-g811536e2015-07-15 00:50:15 -070055 EXPECT_TRUE(context.begin() == context.end());
yang-g8068f862015-07-06 15:05:48 -070056}
57
58TEST_F(SecureAuthContextTest, Properties) {
Julien Boeufea456fc2015-07-07 15:23:30 -070059 grpc_auth_context* ctx = grpc_auth_context_create(NULL);
60 grpc_auth_context_add_cstring_property(ctx, "name", "chapi");
61 grpc_auth_context_add_cstring_property(ctx, "name", "chapo");
62 grpc_auth_context_add_cstring_property(ctx, "foo", "bar");
63 EXPECT_EQ(1, grpc_auth_context_set_peer_identity_property_name(ctx, "name"));
yang-g8068f862015-07-06 15:05:48 -070064
65 SecureAuthContext context(ctx);
66 std::vector<grpc::string> peer_identity = context.GetPeerIdentity();
Craig Tillerd0955f32015-07-10 07:52:37 -070067 EXPECT_EQ(2u, peer_identity.size());
yang-g8068f862015-07-06 15:05:48 -070068 EXPECT_EQ("chapi", peer_identity[0]);
69 EXPECT_EQ("chapo", peer_identity[1]);
70 EXPECT_EQ("name", context.GetPeerIdentityPropertyName());
71 std::vector<grpc::string> bar = context.FindPropertyValues("foo");
Craig Tillerd0955f32015-07-10 07:52:37 -070072 EXPECT_EQ(1u, bar.size());
yang-g8068f862015-07-06 15:05:48 -070073 EXPECT_EQ("bar", bar[0]);
yang-g8068f862015-07-06 15:05:48 -070074}
75
yang-g811536e2015-07-15 00:50:15 -070076TEST_F(SecureAuthContextTest, Iterators) {
Julien Boeufea456fc2015-07-07 15:23:30 -070077 grpc_auth_context* ctx = grpc_auth_context_create(NULL);
78 grpc_auth_context_add_cstring_property(ctx, "name", "chapi");
79 grpc_auth_context_add_cstring_property(ctx, "name", "chapo");
80 grpc_auth_context_add_cstring_property(ctx, "foo", "bar");
81 EXPECT_EQ(1, grpc_auth_context_set_peer_identity_property_name(ctx, "name"));
yang-g811536e2015-07-15 00:50:15 -070082
83 SecureAuthContext context(ctx);
yang-g7ef72322015-07-16 21:00:51 -070084 AuthPropertyIterator iter = context.begin();
yang-g811536e2015-07-15 00:50:15 -070085 EXPECT_TRUE(context.end() != iter);
yang-g7ef72322015-07-16 21:00:51 -070086 AuthProperty p0 = *iter;
yang-g811536e2015-07-15 00:50:15 -070087 ++iter;
yang-g7ef72322015-07-16 21:00:51 -070088 AuthProperty p1 = *iter;
yang-g811536e2015-07-15 00:50:15 -070089 iter++;
yang-g7ef72322015-07-16 21:00:51 -070090 AuthProperty p2 = *iter;
yang-g811536e2015-07-15 00:50:15 -070091 EXPECT_EQ("name", p0.first);
92 EXPECT_EQ("chapi", p0.second);
93 EXPECT_EQ("name", p1.first);
94 EXPECT_EQ("chapo", p1.second);
95 EXPECT_EQ("foo", p2.first);
96 EXPECT_EQ("bar", p2.second);
97 ++iter;
98 EXPECT_EQ(context.end(), iter);
99}
100
yang-g8068f862015-07-06 15:05:48 -0700101} // namespace
102} // namespace grpc
103
Craig Tillerd6c98df2015-08-18 09:33:44 -0700104int main(int argc, char** argv) {
yang-g8068f862015-07-06 15:05:48 -0700105 ::testing::InitGoogleTest(&argc, argv);
106 return RUN_ALL_TESTS();
107}