yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 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++/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 | |
| 39 | namespace grpc { |
| 40 | namespace { |
| 41 | |
| 42 | class SecureAuthContextTest : public ::testing::Test {}; |
| 43 | |
| 44 | // Created with nullptr |
| 45 | TEST_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-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 51 | EXPECT_TRUE(context.begin() == context.end()); |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST_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 Tiller | d0955f3 | 2015-07-10 07:52:37 -0700 | [diff] [blame] | 63 | EXPECT_EQ(2u, peer_identity.size()); |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 64 | 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 Tiller | d0955f3 | 2015-07-10 07:52:37 -0700 | [diff] [blame] | 68 | EXPECT_EQ(1u, bar.size()); |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 69 | EXPECT_EQ("bar", bar[0]); |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 70 | } |
| 71 | |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 72 | TEST_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-g | 7ef7232 | 2015-07-16 21:00:51 -0700 | [diff] [blame] | 80 | AuthPropertyIterator iter = context.begin(); |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 81 | EXPECT_TRUE(context.end() != iter); |
yang-g | 7ef7232 | 2015-07-16 21:00:51 -0700 | [diff] [blame] | 82 | AuthProperty p0 = *iter; |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 83 | ++iter; |
yang-g | 7ef7232 | 2015-07-16 21:00:51 -0700 | [diff] [blame] | 84 | AuthProperty p1 = *iter; |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 85 | iter++; |
yang-g | 7ef7232 | 2015-07-16 21:00:51 -0700 | [diff] [blame] | 86 | AuthProperty p2 = *iter; |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 87 | 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-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 97 | } // namespace |
| 98 | } // namespace grpc |
| 99 | |
| 100 | int main(int argc, char **argv) { |
| 101 | ::testing::InitGoogleTest(&argc, argv); |
| 102 | return RUN_ALL_TESTS(); |
| 103 | } |