1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #include "src/cpp/common/secure_auth_context.h" 20 #include <grpc/grpc_security.h> 21 #include <grpcpp/security/auth_context.h> 22 #include <gtest/gtest.h> 23 #include "test/cpp/util/string_ref_helper.h" 24 25 #include "src/core/lib/security/context/security_context.h" 26 27 using grpc::testing::ToString; 28 29 namespace grpc { 30 namespace { 31 32 class SecureAuthContextTest : public ::testing::Test {}; 33 34 // Created with nullptr 35 TEST_F(SecureAuthContextTest, EmptyContext) { 36 SecureAuthContext context(nullptr, true); 37 EXPECT_TRUE(context.GetPeerIdentity().empty()); 38 EXPECT_TRUE(context.GetPeerIdentityPropertyName().empty()); 39 EXPECT_TRUE(context.FindPropertyValues("").empty()); 40 EXPECT_TRUE(context.FindPropertyValues("whatever").empty()); 41 EXPECT_TRUE(context.begin() == context.end()); 42 } 43 44 TEST_F(SecureAuthContextTest, Properties) { 45 grpc_auth_context* ctx = grpc_auth_context_create(nullptr); 46 SecureAuthContext context(ctx, true); 47 context.AddProperty("name", "chapi"); 48 context.AddProperty("name", "chapo"); 49 context.AddProperty("foo", "bar"); 50 EXPECT_TRUE(context.SetPeerIdentityPropertyName("name")); 51 52 std::vector<grpc::string_ref> peer_identity = context.GetPeerIdentity(); 53 EXPECT_EQ(2u, peer_identity.size()); 54 EXPECT_EQ("chapi", ToString(peer_identity[0])); 55 EXPECT_EQ("chapo", ToString(peer_identity[1])); 56 EXPECT_EQ("name", context.GetPeerIdentityPropertyName()); 57 std::vector<grpc::string_ref> bar = context.FindPropertyValues("foo"); 58 EXPECT_EQ(1u, bar.size()); 59 EXPECT_EQ("bar", ToString(bar[0])); 60 } 61 62 TEST_F(SecureAuthContextTest, Iterators) { 63 grpc_auth_context* ctx = grpc_auth_context_create(nullptr); 64 SecureAuthContext context(ctx, true); 65 context.AddProperty("name", "chapi"); 66 context.AddProperty("name", "chapo"); 67 context.AddProperty("foo", "bar"); 68 EXPECT_TRUE(context.SetPeerIdentityPropertyName("name")); 69 70 AuthPropertyIterator iter = context.begin(); 71 EXPECT_TRUE(context.end() != iter); 72 AuthProperty p0 = *iter; 73 ++iter; 74 AuthProperty p1 = *iter; 75 iter++; 76 AuthProperty p2 = *iter; 77 EXPECT_EQ("name", ToString(p0.first)); 78 EXPECT_EQ("chapi", ToString(p0.second)); 79 EXPECT_EQ("name", ToString(p1.first)); 80 EXPECT_EQ("chapo", ToString(p1.second)); 81 EXPECT_EQ("foo", ToString(p2.first)); 82 EXPECT_EQ("bar", ToString(p2.second)); 83 ++iter; 84 EXPECT_EQ(context.end(), iter); 85 } 86 87 } // namespace 88 } // namespace grpc 89 90 int main(int argc, char** argv) { 91 ::testing::InitGoogleTest(&argc, argv); 92 return RUN_ALL_TESTS(); 93 } 94