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 | |
Julien Boeuf | ea456fc | 2015-07-07 15:23:30 -0700 | [diff] [blame] | 34 | #include <grpc/grpc_security.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 35 | #include <grpc++/support/auth_context.h> |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 36 | #include <gtest/gtest.h> |
| 37 | #include "src/cpp/common/secure_auth_context.h" |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 38 | |
Julien Boeuf | 54b5018 | 2015-08-05 21:40:49 -0700 | [diff] [blame] | 39 | extern "C" { |
| 40 | #include "src/core/security/security_context.h" |
| 41 | } |
| 42 | |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 43 | namespace grpc { |
| 44 | namespace { |
| 45 | |
| 46 | class SecureAuthContextTest : public ::testing::Test {}; |
| 47 | |
| 48 | // Created with nullptr |
| 49 | TEST_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-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 55 | EXPECT_TRUE(context.begin() == context.end()); |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | TEST_F(SecureAuthContextTest, Properties) { |
Julien Boeuf | ea456fc | 2015-07-07 15:23:30 -0700 | [diff] [blame] | 59 | 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-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 64 | |
| 65 | SecureAuthContext context(ctx); |
| 66 | std::vector<grpc::string> peer_identity = context.GetPeerIdentity(); |
Craig Tiller | d0955f3 | 2015-07-10 07:52:37 -0700 | [diff] [blame] | 67 | EXPECT_EQ(2u, peer_identity.size()); |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 68 | 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 Tiller | d0955f3 | 2015-07-10 07:52:37 -0700 | [diff] [blame] | 72 | EXPECT_EQ(1u, bar.size()); |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 73 | EXPECT_EQ("bar", bar[0]); |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 74 | } |
| 75 | |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 76 | TEST_F(SecureAuthContextTest, Iterators) { |
Julien Boeuf | ea456fc | 2015-07-07 15:23:30 -0700 | [diff] [blame] | 77 | 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-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 82 | |
| 83 | SecureAuthContext context(ctx); |
yang-g | 7ef7232 | 2015-07-16 21:00:51 -0700 | [diff] [blame] | 84 | AuthPropertyIterator iter = context.begin(); |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(context.end() != iter); |
yang-g | 7ef7232 | 2015-07-16 21:00:51 -0700 | [diff] [blame] | 86 | AuthProperty p0 = *iter; |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 87 | ++iter; |
yang-g | 7ef7232 | 2015-07-16 21:00:51 -0700 | [diff] [blame] | 88 | AuthProperty p1 = *iter; |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 89 | iter++; |
yang-g | 7ef7232 | 2015-07-16 21:00:51 -0700 | [diff] [blame] | 90 | AuthProperty p2 = *iter; |
yang-g | 811536e | 2015-07-15 00:50:15 -0700 | [diff] [blame] | 91 | 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-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 101 | } // namespace |
| 102 | } // namespace grpc |
| 103 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 104 | int main(int argc, char** argv) { |
yang-g | 8068f86 | 2015-07-06 15:05:48 -0700 | [diff] [blame] | 105 | ::testing::InitGoogleTest(&argc, argv); |
| 106 | return RUN_ALL_TESTS(); |
| 107 | } |