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 <string.h>
35 :
36 : #include "src/core/security/security_context.h"
37 : #include "src/core/support/string.h"
38 : #include "test/core/util/test_config.h"
39 :
40 : #include <grpc/support/log.h>
41 :
42 1 : static void test_empty_context(void) {
43 1 : grpc_auth_context *ctx = grpc_auth_context_create(NULL);
44 : grpc_auth_property_iterator it;
45 :
46 1 : gpr_log(GPR_INFO, "test_empty_context");
47 1 : GPR_ASSERT(ctx != NULL);
48 1 : GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx) == NULL);
49 1 : it = grpc_auth_context_peer_identity(ctx);
50 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
51 1 : it = grpc_auth_context_property_iterator(ctx);
52 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
53 1 : it = grpc_auth_context_find_properties_by_name(ctx, "foo");
54 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
55 1 : GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx, "bar") ==
56 : 0);
57 1 : GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx) == NULL);
58 1 : GRPC_AUTH_CONTEXT_UNREF(ctx, "test");
59 1 : }
60 :
61 1 : static void test_simple_context(void) {
62 1 : grpc_auth_context *ctx = grpc_auth_context_create(NULL);
63 : grpc_auth_property_iterator it;
64 : size_t i;
65 :
66 1 : gpr_log(GPR_INFO, "test_simple_context");
67 1 : GPR_ASSERT(ctx != NULL);
68 1 : grpc_auth_context_add_cstring_property(ctx, "name", "chapi");
69 1 : grpc_auth_context_add_cstring_property(ctx, "name", "chapo");
70 1 : grpc_auth_context_add_cstring_property(ctx, "foo", "bar");
71 1 : GPR_ASSERT(ctx->properties.count == 3);
72 1 : GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx, "name") ==
73 : 1);
74 :
75 1 : GPR_ASSERT(
76 : strcmp(grpc_auth_context_peer_identity_property_name(ctx), "name") == 0);
77 1 : it = grpc_auth_context_property_iterator(ctx);
78 4 : for (i = 0; i < ctx->properties.count; i++) {
79 3 : const grpc_auth_property *p = grpc_auth_property_iterator_next(&it);
80 3 : GPR_ASSERT(p == &ctx->properties.array[i]);
81 : }
82 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
83 :
84 1 : it = grpc_auth_context_find_properties_by_name(ctx, "foo");
85 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
86 : &ctx->properties.array[2]);
87 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
88 :
89 1 : it = grpc_auth_context_peer_identity(ctx);
90 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
91 : &ctx->properties.array[0]);
92 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
93 : &ctx->properties.array[1]);
94 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
95 :
96 1 : GRPC_AUTH_CONTEXT_UNREF(ctx, "test");
97 1 : }
98 :
99 1 : static void test_chained_context(void) {
100 1 : grpc_auth_context *chained = grpc_auth_context_create(NULL);
101 1 : grpc_auth_context *ctx = grpc_auth_context_create(chained);
102 : grpc_auth_property_iterator it;
103 : size_t i;
104 :
105 1 : gpr_log(GPR_INFO, "test_chained_context");
106 1 : GRPC_AUTH_CONTEXT_UNREF(chained, "chained");
107 1 : grpc_auth_context_add_cstring_property(chained, "name", "padapo");
108 1 : grpc_auth_context_add_cstring_property(chained, "foo", "baz");
109 1 : grpc_auth_context_add_cstring_property(ctx, "name", "chapi");
110 1 : grpc_auth_context_add_cstring_property(ctx, "name", "chap0");
111 1 : grpc_auth_context_add_cstring_property(ctx, "foo", "bar");
112 1 : GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx, "name") ==
113 : 1);
114 :
115 1 : GPR_ASSERT(
116 : strcmp(grpc_auth_context_peer_identity_property_name(ctx), "name") == 0);
117 1 : it = grpc_auth_context_property_iterator(ctx);
118 4 : for (i = 0; i < ctx->properties.count; i++) {
119 3 : const grpc_auth_property *p = grpc_auth_property_iterator_next(&it);
120 3 : GPR_ASSERT(p == &ctx->properties.array[i]);
121 : }
122 3 : for (i = 0; i < chained->properties.count; i++) {
123 2 : const grpc_auth_property *p = grpc_auth_property_iterator_next(&it);
124 2 : GPR_ASSERT(p == &chained->properties.array[i]);
125 : }
126 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
127 :
128 1 : it = grpc_auth_context_find_properties_by_name(ctx, "foo");
129 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
130 : &ctx->properties.array[2]);
131 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
132 : &chained->properties.array[1]);
133 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
134 :
135 1 : it = grpc_auth_context_peer_identity(ctx);
136 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
137 : &ctx->properties.array[0]);
138 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
139 : &ctx->properties.array[1]);
140 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
141 : &chained->properties.array[0]);
142 1 : GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
143 :
144 1 : GRPC_AUTH_CONTEXT_UNREF(ctx, "test");
145 1 : }
146 :
147 1 : int main(int argc, char **argv) {
148 1 : grpc_test_init(argc, argv);
149 1 : test_empty_context();
150 1 : test_simple_context();
151 1 : test_chained_context();
152 1 : return 0;
153 : }
|