Line data Source code
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/grpc_security.h>
35 : #include <grpc++/security/auth_context.h>
36 : #include <gtest/gtest.h>
37 : #include "src/cpp/common/secure_auth_context.h"
38 : #include "test/cpp/util/string_ref_helper.h"
39 :
40 : extern "C" {
41 : #include "src/core/security/security_context.h"
42 : }
43 :
44 : using ::grpc::testing::ToString;
45 :
46 : namespace grpc {
47 : namespace {
48 :
49 4 : class TestAuthPropertyIterator : public AuthPropertyIterator {
50 : public:
51 3 : TestAuthPropertyIterator() {}
52 1 : TestAuthPropertyIterator(const grpc_auth_property* property,
53 : const grpc_auth_property_iterator* iter)
54 1 : : AuthPropertyIterator(property, iter) {}
55 : };
56 :
57 4 : class AuthPropertyIteratorTest : public ::testing::Test {
58 : protected:
59 2 : void SetUp() GRPC_OVERRIDE {
60 2 : ctx_ = grpc_auth_context_create(NULL);
61 2 : grpc_auth_context_add_cstring_property(ctx_, "name", "chapi");
62 2 : grpc_auth_context_add_cstring_property(ctx_, "name", "chapo");
63 2 : grpc_auth_context_add_cstring_property(ctx_, "foo", "bar");
64 2 : EXPECT_EQ(1,
65 2 : grpc_auth_context_set_peer_identity_property_name(ctx_, "name"));
66 2 : }
67 2 : void TearDown() GRPC_OVERRIDE { grpc_auth_context_release(ctx_); }
68 : grpc_auth_context* ctx_;
69 : };
70 :
71 5 : TEST_F(AuthPropertyIteratorTest, DefaultCtor) {
72 1 : TestAuthPropertyIterator iter1;
73 2 : TestAuthPropertyIterator iter2;
74 2 : EXPECT_EQ(iter1, iter2);
75 1 : }
76 :
77 5 : TEST_F(AuthPropertyIteratorTest, GeneralTest) {
78 : grpc_auth_property_iterator c_iter =
79 1 : grpc_auth_context_property_iterator(ctx_);
80 : const grpc_auth_property* property =
81 1 : grpc_auth_property_iterator_next(&c_iter);
82 1 : TestAuthPropertyIterator iter(property, &c_iter);
83 2 : TestAuthPropertyIterator empty_iter;
84 1 : EXPECT_FALSE(iter == empty_iter);
85 1 : AuthProperty p0 = *iter;
86 1 : ++iter;
87 1 : AuthProperty p1 = *iter;
88 1 : iter++;
89 1 : AuthProperty p2 = *iter;
90 1 : EXPECT_EQ("name", ToString(p0.first));
91 1 : EXPECT_EQ("chapi", ToString(p0.second));
92 1 : EXPECT_EQ("name", ToString(p1.first));
93 1 : EXPECT_EQ("chapo", ToString(p1.second));
94 1 : EXPECT_EQ("foo", ToString(p2.first));
95 1 : EXPECT_EQ("bar", ToString(p2.second));
96 1 : ++iter;
97 2 : EXPECT_EQ(empty_iter, iter);
98 1 : }
99 :
100 : } // namespace
101 : } // namespace grpc
102 :
103 1 : int main(int argc, char** argv) {
104 1 : ::testing::InitGoogleTest(&argc, argv);
105 1 : return RUN_ALL_TESTS();
106 3 : }
|